blob: b90b03bea70329367b9d0fd6702ecaaa3a080456 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import static android.os.LocalPowerManager.CHEEK_EVENT;
20import static android.os.LocalPowerManager.OTHER_EVENT;
21import static android.os.LocalPowerManager.TOUCH_EVENT;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -070022import static android.os.LocalPowerManager.LONG_TOUCH_EVENT;
23import static android.os.LocalPowerManager.TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
25import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
26import static android.view.WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070027import static android.view.WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
29import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -070030import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
32import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
33import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070034import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
36import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
38import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
39import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
40import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
41import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070042import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44import com.android.internal.app.IBatteryStats;
45import com.android.internal.policy.PolicyManager;
46import com.android.internal.view.IInputContext;
47import com.android.internal.view.IInputMethodClient;
48import com.android.internal.view.IInputMethodManager;
49import com.android.server.KeyInputQueue.QueuedEvent;
50import com.android.server.am.BatteryStatsService;
51
52import android.Manifest;
53import android.app.ActivityManagerNative;
54import android.app.IActivityManager;
55import android.content.Context;
56import android.content.pm.ActivityInfo;
57import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070058import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.res.Configuration;
60import android.graphics.Matrix;
61import android.graphics.PixelFormat;
62import android.graphics.Rect;
63import android.graphics.Region;
64import android.os.BatteryStats;
65import android.os.Binder;
Dianne Hackborn75804932009-10-20 20:15:20 -070066import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Debug;
68import android.os.Handler;
69import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070070import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.LocalPowerManager;
72import android.os.Looper;
73import android.os.Message;
74import android.os.Parcel;
75import android.os.ParcelFileDescriptor;
76import android.os.Power;
77import android.os.PowerManager;
78import android.os.Process;
79import android.os.RemoteException;
80import android.os.ServiceManager;
81import android.os.SystemClock;
82import android.os.SystemProperties;
83import android.os.TokenWatcher;
84import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070085import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.util.EventLog;
87import android.util.Log;
88import android.util.SparseIntArray;
89import android.view.Display;
90import android.view.Gravity;
91import android.view.IApplicationToken;
92import android.view.IOnKeyguardExitResult;
93import android.view.IRotationWatcher;
94import android.view.IWindow;
95import android.view.IWindowManager;
96import android.view.IWindowSession;
97import android.view.KeyEvent;
98import android.view.MotionEvent;
99import android.view.RawInputEvent;
100import android.view.Surface;
101import android.view.SurfaceSession;
102import android.view.View;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700103import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.ViewTreeObserver;
105import android.view.WindowManager;
106import android.view.WindowManagerImpl;
107import android.view.WindowManagerPolicy;
108import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700109import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.animation.Animation;
111import android.view.animation.AnimationUtils;
112import android.view.animation.Transformation;
113
114import java.io.BufferedWriter;
115import java.io.File;
116import java.io.FileDescriptor;
117import java.io.IOException;
118import java.io.OutputStream;
119import java.io.OutputStreamWriter;
120import java.io.PrintWriter;
121import java.io.StringWriter;
122import java.net.Socket;
123import java.util.ArrayList;
124import java.util.HashMap;
125import java.util.HashSet;
126import java.util.Iterator;
127import java.util.List;
128
129/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700130public class WindowManagerService extends IWindowManager.Stub
131 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 static final String TAG = "WindowManager";
133 static final boolean DEBUG = false;
134 static final boolean DEBUG_FOCUS = false;
135 static final boolean DEBUG_ANIM = false;
Dianne Hackborn9b52a212009-12-11 14:51:35 -0800136 static final boolean DEBUG_LAYOUT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final boolean DEBUG_LAYERS = false;
138 static final boolean DEBUG_INPUT = false;
139 static final boolean DEBUG_INPUT_METHOD = false;
140 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700141 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 static final boolean DEBUG_ORIENTATION = false;
143 static final boolean DEBUG_APP_TRANSITIONS = false;
144 static final boolean DEBUG_STARTING_WINDOW = false;
145 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700146 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final boolean SHOW_TRANSACTIONS = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700148 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700149 static final boolean MEASURE_LATENCY = false;
150 static private LatencyTimer lt;
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final boolean PROFILE_ORIENTATION = false;
153 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700154 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 /** How long to wait for subsequent key repeats, in milliseconds */
159 static final int KEY_REPEAT_DELAY = 50;
160
161 /** How much to multiply the policy's type layer, to reserve room
162 * for multiple windows of the same type and Z-ordering adjustment
163 * with TYPE_LAYER_OFFSET. */
164 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
167 * or below others in the same layer. */
168 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 /** How much to increment the layer for each window, to reserve room
171 * for effect surfaces between them.
172 */
173 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 /** The maximum length we will accept for a loaded animation duration:
176 * this is 10 seconds.
177 */
178 static final int MAX_ANIMATION_DURATION = 10*1000;
179
180 /** Amount of time (in milliseconds) to animate the dim surface from one
181 * value to another, when no window animation is driving it.
182 */
183 static final int DEFAULT_DIM_DURATION = 200;
184
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700185 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
186 * compatible windows.
187 */
188 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 /** Adjustment to time to perform a dim, to make it more dramatic.
191 */
192 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700193
Dianne Hackborncfaef692009-06-15 14:24:44 -0700194 static final int INJECT_FAILED = 0;
195 static final int INJECT_SUCCEEDED = 1;
196 static final int INJECT_NO_PERMISSION = -1;
197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 static final int UPDATE_FOCUS_NORMAL = 0;
199 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
200 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
201 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700202
Michael Chane96440f2009-05-06 10:27:36 -0700203 /** The minimum time between dispatching touch events. */
204 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
205
206 // Last touch event time
207 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700208
Michael Chane96440f2009-05-06 10:27:36 -0700209 // Last touch event type
210 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700211
Michael Chane96440f2009-05-06 10:27:36 -0700212 // Time to wait before calling useractivity again. This saves CPU usage
213 // when we get a flood of touch events.
214 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
215
216 // Last time we call user activity
217 long mLastUserActivityCallTime = 0;
218
Romain Guy06882f82009-06-10 13:36:04 -0700219 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700220 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700223 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224
225 /**
226 * Condition waited on by {@link #reenableKeyguard} to know the call to
227 * the window policy has finished.
Mike Lockwood983ee092009-11-22 01:42:24 -0500228 * This is set to true only if mKeyguardTokenWatcher.acquired() has
229 * actually disabled the keyguard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 */
Mike Lockwood983ee092009-11-22 01:42:24 -0500231 private boolean mKeyguardDisabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
Mike Lockwood983ee092009-11-22 01:42:24 -0500233 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher(
234 new Handler(), "WindowManagerService.mKeyguardTokenWatcher") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 public void acquired() {
236 mPolicy.enableKeyguard(false);
Mike Lockwood983ee092009-11-22 01:42:24 -0500237 mKeyguardDisabled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
239 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700240 mPolicy.enableKeyguard(true);
Mike Lockwood983ee092009-11-22 01:42:24 -0500241 synchronized (mKeyguardTokenWatcher) {
242 mKeyguardDisabled = false;
243 mKeyguardTokenWatcher.notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 }
245 }
246 };
247
248 final Context mContext;
249
250 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
255
256 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * All currently active sessions with clients.
262 */
263 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /**
266 * Mapping from an IWindow IBinder to the server's Window object.
267 * This is also used as the lock for all of our state.
268 */
269 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
270
271 /**
272 * Mapping from a token IBinder to a WindowToken object.
273 */
274 final HashMap<IBinder, WindowToken> mTokenMap =
275 new HashMap<IBinder, WindowToken>();
276
277 /**
278 * The same tokens as mTokenMap, stored in a list for efficient iteration
279 * over them.
280 */
281 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * Window tokens that are in the process of exiting, but still
285 * on screen for animations.
286 */
287 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
288
289 /**
290 * Z-ordered (bottom-most first) list of all application tokens, for
291 * controlling the ordering of windows in different applications. This
292 * contains WindowToken objects.
293 */
294 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
295
296 /**
297 * Application tokens that are in the process of exiting, but still
298 * on screen for animations.
299 */
300 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
301
302 /**
303 * List of window tokens that have finished starting their application,
304 * and now need to have the policy remove their windows.
305 */
306 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
307
308 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700309 * This was the app token that was used to retrieve the last enter
310 * animation. It will be used for the next exit animation.
311 */
312 AppWindowToken mLastEnterAnimToken;
313
314 /**
315 * These were the layout params used to retrieve the last enter animation.
316 * They will be used for the next exit animation.
317 */
318 LayoutParams mLastEnterAnimParams;
319
320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 * Z-ordered (bottom-most first) list of all Window objects.
322 */
323 final ArrayList mWindows = new ArrayList();
324
325 /**
326 * Windows that are being resized. Used so we can tell the client about
327 * the resize after closing the transaction in which we resized the
328 * underlying surface.
329 */
330 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
331
332 /**
333 * Windows whose animations have ended and now must be removed.
334 */
335 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
336
337 /**
338 * Windows whose surface should be destroyed.
339 */
340 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
341
342 /**
343 * Windows that have lost input focus and are waiting for the new
344 * focus window to be displayed before they are told about this.
345 */
346 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
347
348 /**
349 * This is set when we have run out of memory, and will either be an empty
350 * list or contain windows that need to be force removed.
351 */
352 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700357 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 Surface mBlurSurface;
359 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 final float[] mTmpFloats = new float[9];
364
365 boolean mSafeMode;
366 boolean mDisplayEnabled = false;
367 boolean mSystemBooted = false;
368 int mRotation = 0;
369 int mRequestedRotation = 0;
370 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700371 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 ArrayList<IRotationWatcher> mRotationWatchers
373 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 boolean mLayoutNeeded = true;
376 boolean mAnimationPending = false;
377 boolean mDisplayFrozen = false;
378 boolean mWindowsFreezingScreen = false;
379 long mFreezeGcPending = 0;
380 int mAppsFreezingScreen = 0;
381
382 // This is held as long as we have the screen frozen, to give us time to
383 // perform a rotation animation when turning off shows the lock screen which
384 // changes the orientation.
385 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 // State management of app transitions. When we are preparing for a
388 // transition, mNextAppTransition will be the kind of transition to
389 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
390 // mOpeningApps and mClosingApps are the lists of tokens that will be
391 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700392 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700393 String mNextAppTransitionPackage;
394 int mNextAppTransitionEnter;
395 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700397 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 boolean mAppTransitionTimeout = false;
399 boolean mStartingIconInTransition = false;
400 boolean mSkipAppTransitionAnimation = false;
401 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
402 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700403 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
404 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 //flag to detect fat touch events
407 boolean mFatTouch = false;
408 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 H mH = new H();
411
412 WindowState mCurrentFocus = null;
413 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 // This just indicates the window the input method is on top of, not
416 // necessarily the window its input is going to.
417 WindowState mInputMethodTarget = null;
418 WindowState mUpcomingInputMethodTarget = null;
419 boolean mInputMethodTargetWaitingAnim;
420 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 WindowState mInputMethodWindow = null;
423 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
424
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700425 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
426
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700427 // If non-null, this is the currently visible window that is associated
428 // with the wallpaper.
429 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700430 // If non-null, we are in the middle of animating from one wallpaper target
431 // to another, and this is the lower one in Z-order.
432 WindowState mLowerWallpaperTarget = null;
433 // If non-null, we are in the middle of animating from one wallpaper target
434 // to another, and this is the higher one in Z-order.
435 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700436 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700437 float mLastWallpaperX = -1;
438 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800439 float mLastWallpaperXStep = -1;
440 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800441 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700442 // This is set when we are waiting for a wallpaper to tell us it is done
443 // changing its scroll position.
444 WindowState mWaitingOnWallpaper;
445 // The last time we had a timeout when waiting for a wallpaper.
446 long mLastWallpaperTimeoutTime;
447 // We give a wallpaper up to 150ms to finish scrolling.
448 static final long WALLPAPER_TIMEOUT = 150;
449 // Time we wait after a timeout before trying to wait again.
450 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 AppWindowToken mFocusedApp = null;
453
454 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 float mWindowAnimationScale = 1.0f;
457 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 final KeyWaiter mKeyWaiter = new KeyWaiter();
460 final KeyQ mQueue;
461 final InputDispatcherThread mInputThread;
462
463 // Who is holding the screen on.
464 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700465
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700466 boolean mTurnOnScreen;
467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 /**
469 * Whether the UI is currently running in touch mode (not showing
470 * navigational focus because the user is directly pressing the screen).
471 */
472 boolean mInTouchMode = false;
473
474 private ViewServer mViewServer;
475
476 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700477
Dianne Hackbornc485a602009-03-24 22:39:49 -0700478 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700479 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700480
481 // The frame use to limit the size of the app running in compatibility mode.
482 Rect mCompatibleScreenFrame = new Rect();
483 // The surface used to fill the outer rim of the app running in compatibility mode.
484 Surface mBackgroundFillerSurface = null;
485 boolean mBackgroundFillerShown = false;
486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 public static WindowManagerService main(Context context,
488 PowerManagerService pm, boolean haveInputMethods) {
489 WMThread thr = new WMThread(context, pm, haveInputMethods);
490 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 synchronized (thr) {
493 while (thr.mService == null) {
494 try {
495 thr.wait();
496 } catch (InterruptedException e) {
497 }
498 }
499 }
Romain Guy06882f82009-06-10 13:36:04 -0700500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 return thr.mService;
502 }
Romain Guy06882f82009-06-10 13:36:04 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 static class WMThread extends Thread {
505 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 private final Context mContext;
508 private final PowerManagerService mPM;
509 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 public WMThread(Context context, PowerManagerService pm,
512 boolean haveInputMethods) {
513 super("WindowManager");
514 mContext = context;
515 mPM = pm;
516 mHaveInputMethods = haveInputMethods;
517 }
Romain Guy06882f82009-06-10 13:36:04 -0700518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 public void run() {
520 Looper.prepare();
521 WindowManagerService s = new WindowManagerService(mContext, mPM,
522 mHaveInputMethods);
523 android.os.Process.setThreadPriority(
524 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 synchronized (this) {
527 mService = s;
528 notifyAll();
529 }
Romain Guy06882f82009-06-10 13:36:04 -0700530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 Looper.loop();
532 }
533 }
534
535 static class PolicyThread extends Thread {
536 private final WindowManagerPolicy mPolicy;
537 private final WindowManagerService mService;
538 private final Context mContext;
539 private final PowerManagerService mPM;
540 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 public PolicyThread(WindowManagerPolicy policy,
543 WindowManagerService service, Context context,
544 PowerManagerService pm) {
545 super("WindowManagerPolicy");
546 mPolicy = policy;
547 mService = service;
548 mContext = context;
549 mPM = pm;
550 }
Romain Guy06882f82009-06-10 13:36:04 -0700551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 public void run() {
553 Looper.prepare();
554 //Looper.myLooper().setMessageLogging(new LogPrinter(
555 // Log.VERBOSE, "WindowManagerPolicy"));
556 android.os.Process.setThreadPriority(
557 android.os.Process.THREAD_PRIORITY_FOREGROUND);
558 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 synchronized (this) {
561 mRunning = true;
562 notifyAll();
563 }
Romain Guy06882f82009-06-10 13:36:04 -0700564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 Looper.loop();
566 }
567 }
568
569 private WindowManagerService(Context context, PowerManagerService pm,
570 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700571 if (MEASURE_LATENCY) {
572 lt = new LatencyTimer(100, 1000);
573 }
574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 mContext = context;
576 mHaveInputMethods = haveInputMethods;
577 mLimitedAlphaCompositing = context.getResources().getBoolean(
578 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 mPowerManager = pm;
581 mPowerManager.setPolicy(mPolicy);
582 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
583 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
584 "SCREEN_FROZEN");
585 mScreenFrozenLock.setReferenceCounted(false);
586
587 mActivityManager = ActivityManagerNative.getDefault();
588 mBatteryStats = BatteryStatsService.getService();
589
590 // Get persisted window scale setting
591 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
592 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
593 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
594 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700595
Michael Chan9f028e62009-08-04 17:37:46 -0700596 int max_events_per_sec = 35;
597 try {
598 max_events_per_sec = Integer.parseInt(SystemProperties
599 .get("windowsmgr.max_events_per_sec"));
600 if (max_events_per_sec < 1) {
601 max_events_per_sec = 35;
602 }
603 } catch (NumberFormatException e) {
604 }
605 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 mQueue = new KeyQ();
608
609 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
612 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 synchronized (thr) {
615 while (!thr.mRunning) {
616 try {
617 thr.wait();
618 } catch (InterruptedException e) {
619 }
620 }
621 }
Romain Guy06882f82009-06-10 13:36:04 -0700622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 // Add ourself to the Watchdog monitors.
626 Watchdog.getInstance().addMonitor(this);
627 }
628
629 @Override
630 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
631 throws RemoteException {
632 try {
633 return super.onTransact(code, data, reply, flags);
634 } catch (RuntimeException e) {
635 // The window manager only throws security exceptions, so let's
636 // log all others.
637 if (!(e instanceof SecurityException)) {
638 Log.e(TAG, "Window Manager Crash", e);
639 }
640 throw e;
641 }
642 }
643
644 private void placeWindowAfter(Object pos, WindowState window) {
645 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700646 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 TAG, "Adding window " + window + " at "
648 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
649 mWindows.add(i+1, window);
650 }
651
652 private void placeWindowBefore(Object pos, WindowState window) {
653 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700654 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 TAG, "Adding window " + window + " at "
656 + i + " of " + mWindows.size() + " (before " + pos + ")");
657 mWindows.add(i, window);
658 }
659
660 //This method finds out the index of a window that has the same app token as
661 //win. used for z ordering the windows in mWindows
662 private int findIdxBasedOnAppTokens(WindowState win) {
663 //use a local variable to cache mWindows
664 ArrayList localmWindows = mWindows;
665 int jmax = localmWindows.size();
666 if(jmax == 0) {
667 return -1;
668 }
669 for(int j = (jmax-1); j >= 0; j--) {
670 WindowState wentry = (WindowState)localmWindows.get(j);
671 if(wentry.mAppToken == win.mAppToken) {
672 return j;
673 }
674 }
675 return -1;
676 }
Romain Guy06882f82009-06-10 13:36:04 -0700677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
679 final IWindow client = win.mClient;
680 final WindowToken token = win.mToken;
681 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 final int N = localmWindows.size();
684 final WindowState attached = win.mAttachedWindow;
685 int i;
686 if (attached == null) {
687 int tokenWindowsPos = token.windows.size();
688 if (token.appWindowToken != null) {
689 int index = tokenWindowsPos-1;
690 if (index >= 0) {
691 // If this application has existing windows, we
692 // simply place the new window on top of them... but
693 // keep the starting window on top.
694 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
695 // Base windows go behind everything else.
696 placeWindowBefore(token.windows.get(0), win);
697 tokenWindowsPos = 0;
698 } else {
699 AppWindowToken atoken = win.mAppToken;
700 if (atoken != null &&
701 token.windows.get(index) == atoken.startingWindow) {
702 placeWindowBefore(token.windows.get(index), win);
703 tokenWindowsPos--;
704 } else {
705 int newIdx = findIdxBasedOnAppTokens(win);
706 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700707 //there is a window above this one associated with the same
708 //apptoken note that the window could be a floating window
709 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700711 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
712 TAG, "Adding window " + win + " at "
713 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717 }
718 } else {
719 if (localLOGV) Log.v(
720 TAG, "Figuring out where to add app window "
721 + client.asBinder() + " (token=" + token + ")");
722 // Figure out where the window should go, based on the
723 // order of applications.
724 final int NA = mAppTokens.size();
725 Object pos = null;
726 for (i=NA-1; i>=0; i--) {
727 AppWindowToken t = mAppTokens.get(i);
728 if (t == token) {
729 i--;
730 break;
731 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700732
733 // We haven't reached the token yet; if this token
734 // is not going to the bottom and has windows, we can
735 // use it as an anchor for when we do reach the token.
736 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 pos = t.windows.get(0);
738 }
739 }
740 // We now know the index into the apps. If we found
741 // an app window above, that gives us the position; else
742 // we need to look some more.
743 if (pos != null) {
744 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700745 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 mTokenMap.get(((WindowState)pos).mClient.asBinder());
747 if (atoken != null) {
748 final int NC = atoken.windows.size();
749 if (NC > 0) {
750 WindowState bottom = atoken.windows.get(0);
751 if (bottom.mSubLayer < 0) {
752 pos = bottom;
753 }
754 }
755 }
756 placeWindowBefore(pos, win);
757 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700758 // Continue looking down until we find the first
759 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 while (i >= 0) {
761 AppWindowToken t = mAppTokens.get(i);
762 final int NW = t.windows.size();
763 if (NW > 0) {
764 pos = t.windows.get(NW-1);
765 break;
766 }
767 i--;
768 }
769 if (pos != null) {
770 // Move in front of any windows attached to this
771 // one.
772 WindowToken atoken =
773 mTokenMap.get(((WindowState)pos).mClient.asBinder());
774 if (atoken != null) {
775 final int NC = atoken.windows.size();
776 if (NC > 0) {
777 WindowState top = atoken.windows.get(NC-1);
778 if (top.mSubLayer >= 0) {
779 pos = top;
780 }
781 }
782 }
783 placeWindowAfter(pos, win);
784 } else {
785 // Just search for the start of this layer.
786 final int myLayer = win.mBaseLayer;
787 for (i=0; i<N; i++) {
788 WindowState w = (WindowState)localmWindows.get(i);
789 if (w.mBaseLayer > myLayer) {
790 break;
791 }
792 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700793 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
794 TAG, "Adding window " + win + " at "
795 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 localmWindows.add(i, win);
797 }
798 }
799 }
800 } else {
801 // Figure out where window should go, based on layer.
802 final int myLayer = win.mBaseLayer;
803 for (i=N-1; i>=0; i--) {
804 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
805 i++;
806 break;
807 }
808 }
809 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700810 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
811 TAG, "Adding window " + win + " at "
812 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 localmWindows.add(i, win);
814 }
815 if (addToToken) {
816 token.windows.add(tokenWindowsPos, win);
817 }
818
819 } else {
820 // Figure out this window's ordering relative to the window
821 // it is attached to.
822 final int NA = token.windows.size();
823 final int sublayer = win.mSubLayer;
824 int largestSublayer = Integer.MIN_VALUE;
825 WindowState windowWithLargestSublayer = null;
826 for (i=0; i<NA; i++) {
827 WindowState w = token.windows.get(i);
828 final int wSublayer = w.mSubLayer;
829 if (wSublayer >= largestSublayer) {
830 largestSublayer = wSublayer;
831 windowWithLargestSublayer = w;
832 }
833 if (sublayer < 0) {
834 // For negative sublayers, we go below all windows
835 // in the same sublayer.
836 if (wSublayer >= sublayer) {
837 if (addToToken) {
838 token.windows.add(i, win);
839 }
840 placeWindowBefore(
841 wSublayer >= 0 ? attached : w, win);
842 break;
843 }
844 } else {
845 // For positive sublayers, we go above all windows
846 // in the same sublayer.
847 if (wSublayer > sublayer) {
848 if (addToToken) {
849 token.windows.add(i, win);
850 }
851 placeWindowBefore(w, win);
852 break;
853 }
854 }
855 }
856 if (i >= NA) {
857 if (addToToken) {
858 token.windows.add(win);
859 }
860 if (sublayer < 0) {
861 placeWindowBefore(attached, win);
862 } else {
863 placeWindowAfter(largestSublayer >= 0
864 ? windowWithLargestSublayer
865 : attached,
866 win);
867 }
868 }
869 }
Romain Guy06882f82009-06-10 13:36:04 -0700870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 if (win.mAppToken != null && addToToken) {
872 win.mAppToken.allAppWindows.add(win);
873 }
874 }
Romain Guy06882f82009-06-10 13:36:04 -0700875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 static boolean canBeImeTarget(WindowState w) {
877 final int fl = w.mAttrs.flags
878 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
879 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
880 return w.isVisibleOrAdding();
881 }
882 return false;
883 }
Romain Guy06882f82009-06-10 13:36:04 -0700884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
886 final ArrayList localmWindows = mWindows;
887 final int N = localmWindows.size();
888 WindowState w = null;
889 int i = N;
890 while (i > 0) {
891 i--;
892 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
895 // + Integer.toHexString(w.mAttrs.flags));
896 if (canBeImeTarget(w)) {
897 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 // Yet more tricksyness! If this window is a "starting"
900 // window, we do actually want to be on top of it, but
901 // it is not -really- where input will go. So if the caller
902 // is not actually looking to move the IME, look down below
903 // for a real window to target...
904 if (!willMove
905 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
906 && i > 0) {
907 WindowState wb = (WindowState)localmWindows.get(i-1);
908 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
909 i--;
910 w = wb;
911 }
912 }
913 break;
914 }
915 }
Romain Guy06882f82009-06-10 13:36:04 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
920 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 if (willMove && w != null) {
923 final WindowState curTarget = mInputMethodTarget;
924 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 // Now some fun for dealing with window animations that
927 // modify the Z order. We need to look at all windows below
928 // the current target that are in this app, finding the highest
929 // visible one in layering.
930 AppWindowToken token = curTarget.mAppToken;
931 WindowState highestTarget = null;
932 int highestPos = 0;
933 if (token.animating || token.animation != null) {
934 int pos = 0;
935 pos = localmWindows.indexOf(curTarget);
936 while (pos >= 0) {
937 WindowState win = (WindowState)localmWindows.get(pos);
938 if (win.mAppToken != token) {
939 break;
940 }
941 if (!win.mRemoved) {
942 if (highestTarget == null || win.mAnimLayer >
943 highestTarget.mAnimLayer) {
944 highestTarget = win;
945 highestPos = pos;
946 }
947 }
948 pos--;
949 }
950 }
Romain Guy06882f82009-06-10 13:36:04 -0700951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700953 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 + mNextAppTransition + " " + highestTarget
955 + " animating=" + highestTarget.isAnimating()
956 + " layer=" + highestTarget.mAnimLayer
957 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700958
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700959 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 // If we are currently setting up for an animation,
961 // hold everything until we can find out what will happen.
962 mInputMethodTargetWaitingAnim = true;
963 mInputMethodTarget = highestTarget;
964 return highestPos + 1;
965 } else if (highestTarget.isAnimating() &&
966 highestTarget.mAnimLayer > w.mAnimLayer) {
967 // If the window we are currently targeting is involved
968 // with an animation, and it is on top of the next target
969 // we will be over, then hold off on moving until
970 // that is done.
971 mInputMethodTarget = highestTarget;
972 return highestPos + 1;
973 }
974 }
975 }
976 }
Romain Guy06882f82009-06-10 13:36:04 -0700977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 //Log.i(TAG, "Placing input method @" + (i+1));
979 if (w != null) {
980 if (willMove) {
981 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700982 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
984 + mInputMethodTarget + " to " + w, e);
985 mInputMethodTarget = w;
986 if (w.mAppToken != null) {
987 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
988 } else {
989 setInputMethodAnimLayerAdjustment(0);
990 }
991 }
992 return i+1;
993 }
994 if (willMove) {
995 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700996 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
998 + mInputMethodTarget + " to null", e);
999 mInputMethodTarget = null;
1000 setInputMethodAnimLayerAdjustment(0);
1001 }
1002 return -1;
1003 }
Romain Guy06882f82009-06-10 13:36:04 -07001004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 void addInputMethodWindowToListLocked(WindowState win) {
1006 int pos = findDesiredInputMethodWindowIndexLocked(true);
1007 if (pos >= 0) {
1008 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001009 if (DEBUG_WINDOW_MOVEMENT) Log.v(
1010 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 mWindows.add(pos, win);
1012 moveInputMethodDialogsLocked(pos+1);
1013 return;
1014 }
1015 win.mTargetAppToken = null;
1016 addWindowToListInOrderLocked(win, true);
1017 moveInputMethodDialogsLocked(pos);
1018 }
Romain Guy06882f82009-06-10 13:36:04 -07001019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 void setInputMethodAnimLayerAdjustment(int adj) {
1021 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1022 mInputMethodAnimLayerAdjustment = adj;
1023 WindowState imw = mInputMethodWindow;
1024 if (imw != null) {
1025 imw.mAnimLayer = imw.mLayer + adj;
1026 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1027 + " anim layer: " + imw.mAnimLayer);
1028 int wi = imw.mChildWindows.size();
1029 while (wi > 0) {
1030 wi--;
1031 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1032 cw.mAnimLayer = cw.mLayer + adj;
1033 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1034 + " anim layer: " + cw.mAnimLayer);
1035 }
1036 }
1037 int di = mInputMethodDialogs.size();
1038 while (di > 0) {
1039 di --;
1040 imw = mInputMethodDialogs.get(di);
1041 imw.mAnimLayer = imw.mLayer + adj;
1042 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1043 + " anim layer: " + imw.mAnimLayer);
1044 }
1045 }
Romain Guy06882f82009-06-10 13:36:04 -07001046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1048 int wpos = mWindows.indexOf(win);
1049 if (wpos >= 0) {
1050 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001051 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 mWindows.remove(wpos);
1053 int NC = win.mChildWindows.size();
1054 while (NC > 0) {
1055 NC--;
1056 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1057 int cpos = mWindows.indexOf(cw);
1058 if (cpos >= 0) {
1059 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001060 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1061 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 mWindows.remove(cpos);
1063 }
1064 }
1065 }
1066 return interestingPos;
1067 }
Romain Guy06882f82009-06-10 13:36:04 -07001068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 private void reAddWindowToListInOrderLocked(WindowState win) {
1070 addWindowToListInOrderLocked(win, false);
1071 // This is a hack to get all of the child windows added as well
1072 // at the right position. Child windows should be rare and
1073 // this case should be rare, so it shouldn't be that big a deal.
1074 int wpos = mWindows.indexOf(win);
1075 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001076 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1077 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 mWindows.remove(wpos);
1079 reAddWindowLocked(wpos, win);
1080 }
1081 }
Romain Guy06882f82009-06-10 13:36:04 -07001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 void logWindowList(String prefix) {
1084 int N = mWindows.size();
1085 while (N > 0) {
1086 N--;
1087 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1088 }
1089 }
Romain Guy06882f82009-06-10 13:36:04 -07001090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 void moveInputMethodDialogsLocked(int pos) {
1092 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 final int N = dialogs.size();
1095 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1096 for (int i=0; i<N; i++) {
1097 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1098 }
1099 if (DEBUG_INPUT_METHOD) {
1100 Log.v(TAG, "Window list w/pos=" + pos);
1101 logWindowList(" ");
1102 }
Romain Guy06882f82009-06-10 13:36:04 -07001103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 if (pos >= 0) {
1105 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1106 if (pos < mWindows.size()) {
1107 WindowState wp = (WindowState)mWindows.get(pos);
1108 if (wp == mInputMethodWindow) {
1109 pos++;
1110 }
1111 }
1112 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1113 for (int i=0; i<N; i++) {
1114 WindowState win = dialogs.get(i);
1115 win.mTargetAppToken = targetAppToken;
1116 pos = reAddWindowLocked(pos, win);
1117 }
1118 if (DEBUG_INPUT_METHOD) {
1119 Log.v(TAG, "Final window list:");
1120 logWindowList(" ");
1121 }
1122 return;
1123 }
1124 for (int i=0; i<N; i++) {
1125 WindowState win = dialogs.get(i);
1126 win.mTargetAppToken = null;
1127 reAddWindowToListInOrderLocked(win);
1128 if (DEBUG_INPUT_METHOD) {
1129 Log.v(TAG, "No IM target, final list:");
1130 logWindowList(" ");
1131 }
1132 }
1133 }
Romain Guy06882f82009-06-10 13:36:04 -07001134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1136 final WindowState imWin = mInputMethodWindow;
1137 final int DN = mInputMethodDialogs.size();
1138 if (imWin == null && DN == 0) {
1139 return false;
1140 }
Romain Guy06882f82009-06-10 13:36:04 -07001141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1143 if (imPos >= 0) {
1144 // In this case, the input method windows are to be placed
1145 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 // First check to see if the input method windows are already
1148 // located here, and contiguous.
1149 final int N = mWindows.size();
1150 WindowState firstImWin = imPos < N
1151 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 // Figure out the actual input method window that should be
1154 // at the bottom of their stack.
1155 WindowState baseImWin = imWin != null
1156 ? imWin : mInputMethodDialogs.get(0);
1157 if (baseImWin.mChildWindows.size() > 0) {
1158 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1159 if (cw.mSubLayer < 0) baseImWin = cw;
1160 }
Romain Guy06882f82009-06-10 13:36:04 -07001161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 if (firstImWin == baseImWin) {
1163 // The windows haven't moved... but are they still contiguous?
1164 // First find the top IM window.
1165 int pos = imPos+1;
1166 while (pos < N) {
1167 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1168 break;
1169 }
1170 pos++;
1171 }
1172 pos++;
1173 // Now there should be no more input method windows above.
1174 while (pos < N) {
1175 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1176 break;
1177 }
1178 pos++;
1179 }
1180 if (pos >= N) {
1181 // All is good!
1182 return false;
1183 }
1184 }
Romain Guy06882f82009-06-10 13:36:04 -07001185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 if (imWin != null) {
1187 if (DEBUG_INPUT_METHOD) {
1188 Log.v(TAG, "Moving IM from " + imPos);
1189 logWindowList(" ");
1190 }
1191 imPos = tmpRemoveWindowLocked(imPos, imWin);
1192 if (DEBUG_INPUT_METHOD) {
1193 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1194 logWindowList(" ");
1195 }
1196 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1197 reAddWindowLocked(imPos, imWin);
1198 if (DEBUG_INPUT_METHOD) {
1199 Log.v(TAG, "List after moving IM to " + imPos + ":");
1200 logWindowList(" ");
1201 }
1202 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1203 } else {
1204 moveInputMethodDialogsLocked(imPos);
1205 }
Romain Guy06882f82009-06-10 13:36:04 -07001206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 } else {
1208 // In this case, the input method windows go in a fixed layer,
1209 // because they aren't currently associated with a focus window.
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) Log.v(TAG, "Moving IM from " + imPos);
1213 tmpRemoveWindowLocked(0, imWin);
1214 imWin.mTargetAppToken = null;
1215 reAddWindowToListInOrderLocked(imWin);
1216 if (DEBUG_INPUT_METHOD) {
1217 Log.v(TAG, "List with no IM target:");
1218 logWindowList(" ");
1219 }
1220 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1221 } else {
1222 moveInputMethodDialogsLocked(-1);;
1223 }
Romain Guy06882f82009-06-10 13:36:04 -07001224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 }
Romain Guy06882f82009-06-10 13:36:04 -07001226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 if (needAssignLayers) {
1228 assignLayersLocked();
1229 }
Romain Guy06882f82009-06-10 13:36:04 -07001230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 return true;
1232 }
Romain Guy06882f82009-06-10 13:36:04 -07001233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 void adjustInputMethodDialogsLocked() {
1235 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1236 }
Romain Guy06882f82009-06-10 13:36:04 -07001237
Dianne Hackborn25994b42009-09-04 14:21:19 -07001238 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1239 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1240 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1241 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1242 ? wallpaperTarget.mAppToken.animation : null)
1243 + " upper=" + mUpperWallpaperTarget
1244 + " lower=" + mLowerWallpaperTarget);
1245 return (wallpaperTarget != null
1246 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1247 && wallpaperTarget.mAppToken.animation != null)))
1248 || mUpperWallpaperTarget != null
1249 || mLowerWallpaperTarget != null;
1250 }
1251
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001252 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1253 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1254
1255 int adjustWallpaperWindowsLocked() {
1256 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001257
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001258 final int dw = mDisplay.getWidth();
1259 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001260
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001261 // First find top-most window that has asked to be on top of the
1262 // wallpaper; all wallpapers go behind it.
1263 final ArrayList localmWindows = mWindows;
1264 int N = localmWindows.size();
1265 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001266 WindowState foundW = null;
1267 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001268 WindowState topCurW = null;
1269 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001270 int i = N;
1271 while (i > 0) {
1272 i--;
1273 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001274 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1275 if (topCurW == null) {
1276 topCurW = w;
1277 topCurI = i;
1278 }
1279 continue;
1280 }
1281 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001282 if (w.mAppToken != null) {
1283 // If this window's app token is hidden and not animating,
1284 // it is of no interest to us.
1285 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1286 if (DEBUG_WALLPAPER) Log.v(TAG,
1287 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001288 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001289 continue;
1290 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001291 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001292 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1293 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1294 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001295 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001296 && (mWallpaperTarget == w
1297 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001298 if (DEBUG_WALLPAPER) Log.v(TAG,
1299 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001300 foundW = w;
1301 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001302 if (w == mWallpaperTarget && ((w.mAppToken != null
1303 && w.mAppToken.animation != null)
1304 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001305 // The current wallpaper target is animating, so we'll
1306 // look behind it for another possible target and figure
1307 // out what is going on below.
1308 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1309 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001310 continue;
1311 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001312 break;
1313 }
1314 }
1315
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001316 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001317 // If we are currently waiting for an app transition, and either
1318 // the current target or the next target are involved with it,
1319 // then hold off on doing anything with the wallpaper.
1320 // Note that we are checking here for just whether the target
1321 // is part of an app token... which is potentially overly aggressive
1322 // (the app token may not be involved in the transition), but good
1323 // enough (we'll just wait until whatever transition is pending
1324 // executes).
1325 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001326 if (DEBUG_WALLPAPER) Log.v(TAG,
1327 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001328 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001329 }
1330 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001331 if (DEBUG_WALLPAPER) Log.v(TAG,
1332 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001333 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001334 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001335 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001336
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001337 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001338 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001339 Log.v(TAG, "New wallpaper target: " + foundW
1340 + " oldTarget: " + mWallpaperTarget);
1341 }
1342
1343 mLowerWallpaperTarget = null;
1344 mUpperWallpaperTarget = null;
1345
1346 WindowState oldW = mWallpaperTarget;
1347 mWallpaperTarget = foundW;
1348
1349 // Now what is happening... if the current and new targets are
1350 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001351 if (foundW != null && oldW != null) {
1352 boolean oldAnim = oldW.mAnimation != null
1353 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1354 boolean foundAnim = foundW.mAnimation != null
1355 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001356 if (DEBUG_WALLPAPER) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001357 Log.v(TAG, "New animation: " + foundAnim
1358 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001359 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001360 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001361 int oldI = localmWindows.indexOf(oldW);
1362 if (DEBUG_WALLPAPER) {
1363 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1364 }
1365 if (oldI >= 0) {
1366 if (DEBUG_WALLPAPER) {
1367 Log.v(TAG, "Animating wallpapers: old#" + oldI
1368 + "=" + oldW + "; new#" + foundI
1369 + "=" + foundW);
1370 }
1371
1372 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001373 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001374 if (DEBUG_WALLPAPER) {
1375 Log.v(TAG, "Old wallpaper still the target.");
1376 }
1377 mWallpaperTarget = oldW;
1378 }
1379
1380 // Now set the upper and lower wallpaper targets
1381 // correctly, and make sure that we are positioning
1382 // the wallpaper below the lower.
1383 if (foundI > oldI) {
1384 // The new target is on top of the old one.
1385 if (DEBUG_WALLPAPER) {
1386 Log.v(TAG, "Found target above old target.");
1387 }
1388 mUpperWallpaperTarget = foundW;
1389 mLowerWallpaperTarget = oldW;
1390 foundW = oldW;
1391 foundI = oldI;
1392 } else {
1393 // The new target is below the old one.
1394 if (DEBUG_WALLPAPER) {
1395 Log.v(TAG, "Found target below old target.");
1396 }
1397 mUpperWallpaperTarget = oldW;
1398 mLowerWallpaperTarget = foundW;
1399 }
1400 }
1401 }
1402 }
1403
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001404 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001405 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001406 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1407 || (mLowerWallpaperTarget.mAppToken != null
1408 && mLowerWallpaperTarget.mAppToken.animation != null);
1409 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1410 || (mUpperWallpaperTarget.mAppToken != null
1411 && mUpperWallpaperTarget.mAppToken.animation != null);
1412 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001413 if (DEBUG_WALLPAPER) {
1414 Log.v(TAG, "No longer animating wallpaper targets!");
1415 }
1416 mLowerWallpaperTarget = null;
1417 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001418 }
1419 }
1420
1421 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001422 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001423 // The window is visible to the compositor... but is it visible
1424 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001425 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001426 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001427
1428 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001429 // its layer adjustment. Only do this if we are not transfering
1430 // between two wallpaper targets.
1431 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001432 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001433 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001434
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001435 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1436 * TYPE_LAYER_MULTIPLIER
1437 + TYPE_LAYER_OFFSET;
1438
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001439 // Now w is the window we are supposed to be behind... but we
1440 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001441 // AND any starting window associated with it, AND below the
1442 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001443 while (foundI > 0) {
1444 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001445 if (wb.mBaseLayer < maxLayer &&
1446 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001447 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001448 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001449 // This window is not related to the previous one in any
1450 // interesting way, so stop here.
1451 break;
1452 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001453 foundW = wb;
1454 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001455 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001456 } else {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001457 if (DEBUG_WALLPAPER) Log.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001458 }
1459
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001460 if (foundW == null && topCurW != null) {
1461 // There is no wallpaper target, so it goes at the bottom.
1462 // We will assume it is the same place as last time, if known.
1463 foundW = topCurW;
1464 foundI = topCurI+1;
1465 } else {
1466 // Okay i is the position immediately above the wallpaper. Look at
1467 // what is below it for later.
1468 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1469 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001470
Dianne Hackborn284ac932009-08-28 10:34:25 -07001471 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001472 if (mWallpaperTarget.mWallpaperX >= 0) {
1473 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001474 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001475 }
1476 if (mWallpaperTarget.mWallpaperY >= 0) {
1477 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001478 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001479 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001480 }
1481
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001482 // Start stepping backwards from here, ensuring that our wallpaper windows
1483 // are correctly placed.
1484 int curTokenIndex = mWallpaperTokens.size();
1485 while (curTokenIndex > 0) {
1486 curTokenIndex--;
1487 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001488 if (token.hidden == visible) {
1489 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1490 token.hidden = !visible;
1491 // Need to do a layout to ensure the wallpaper now has the
1492 // correct size.
1493 mLayoutNeeded = true;
1494 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001495
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001496 int curWallpaperIndex = token.windows.size();
1497 while (curWallpaperIndex > 0) {
1498 curWallpaperIndex--;
1499 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001500
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001501 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001502 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001503 }
1504
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001505 // First, make sure the client has the current visibility
1506 // state.
1507 if (wallpaper.mWallpaperVisible != visible) {
1508 wallpaper.mWallpaperVisible = visible;
1509 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001510 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001511 "Setting visibility of wallpaper " + wallpaper
1512 + ": " + visible);
1513 wallpaper.mClient.dispatchAppVisibility(visible);
1514 } catch (RemoteException e) {
1515 }
1516 }
1517
1518 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001519 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1520 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001521
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001522 // First, if this window is at the current index, then all
1523 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001524 if (wallpaper == foundW) {
1525 foundI--;
1526 foundW = foundI > 0
1527 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001528 continue;
1529 }
1530
1531 // The window didn't match... the current wallpaper window,
1532 // wherever it is, is in the wrong place, so make sure it is
1533 // not in the list.
1534 int oldIndex = localmWindows.indexOf(wallpaper);
1535 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001536 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1537 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001538 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001539 if (oldIndex < foundI) {
1540 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001541 }
1542 }
1543
1544 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001545 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1546 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001547 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001548
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001549 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001550 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001551 }
1552 }
1553
1554 return changed;
1555 }
1556
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001557 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001558 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1559 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001560 mWallpaperAnimLayerAdjustment = adj;
1561 int curTokenIndex = mWallpaperTokens.size();
1562 while (curTokenIndex > 0) {
1563 curTokenIndex--;
1564 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1565 int curWallpaperIndex = token.windows.size();
1566 while (curWallpaperIndex > 0) {
1567 curWallpaperIndex--;
1568 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1569 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001570 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1571 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001572 }
1573 }
1574 }
1575
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001576 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1577 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001578 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001579 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001580 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001581 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001582 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1583 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1584 changed = wallpaperWin.mXOffset != offset;
1585 if (changed) {
1586 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1587 + wallpaperWin + " x: " + offset);
1588 wallpaperWin.mXOffset = offset;
1589 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001590 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001591 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001592 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001593 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001594 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001595
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001596 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001597 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001598 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1599 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1600 if (wallpaperWin.mYOffset != offset) {
1601 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1602 + wallpaperWin + " y: " + offset);
1603 changed = true;
1604 wallpaperWin.mYOffset = offset;
1605 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001606 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001607 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001608 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001609 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001610 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001611
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001612 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001613 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001614 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1615 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1616 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001617 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001618 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001619 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001620 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001621 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1622 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001623 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001624 if (mWaitingOnWallpaper != null) {
1625 long start = SystemClock.uptimeMillis();
1626 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1627 < start) {
1628 try {
1629 if (DEBUG_WALLPAPER) Log.v(TAG,
1630 "Waiting for offset complete...");
1631 mWindowMap.wait(WALLPAPER_TIMEOUT);
1632 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001633 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001634 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1635 if ((start+WALLPAPER_TIMEOUT)
1636 < SystemClock.uptimeMillis()) {
1637 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1638 + wallpaperWin);
1639 mLastWallpaperTimeoutTime = start;
1640 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001641 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001642 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001643 }
1644 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001645 } catch (RemoteException e) {
1646 }
1647 }
1648
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001649 return changed;
1650 }
1651
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001652 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001653 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001654 if (mWaitingOnWallpaper != null &&
1655 mWaitingOnWallpaper.mClient.asBinder() == window) {
1656 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001657 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001658 }
1659 }
1660 }
1661
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001662 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001663 final int dw = mDisplay.getWidth();
1664 final int dh = mDisplay.getHeight();
1665
1666 boolean changed = false;
1667
1668 WindowState target = mWallpaperTarget;
1669 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001670 if (target.mWallpaperX >= 0) {
1671 mLastWallpaperX = target.mWallpaperX;
1672 } else if (changingTarget.mWallpaperX >= 0) {
1673 mLastWallpaperX = changingTarget.mWallpaperX;
1674 }
1675 if (target.mWallpaperY >= 0) {
1676 mLastWallpaperY = target.mWallpaperY;
1677 } else if (changingTarget.mWallpaperY >= 0) {
1678 mLastWallpaperY = changingTarget.mWallpaperY;
1679 }
1680 }
1681
1682 int curTokenIndex = mWallpaperTokens.size();
1683 while (curTokenIndex > 0) {
1684 curTokenIndex--;
1685 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1686 int curWallpaperIndex = token.windows.size();
1687 while (curWallpaperIndex > 0) {
1688 curWallpaperIndex--;
1689 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1690 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1691 wallpaper.computeShownFrameLocked();
1692 changed = true;
1693 // We only want to be synchronous with one wallpaper.
1694 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001695 }
1696 }
1697 }
1698
1699 return changed;
1700 }
1701
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001702 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001703 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001704 final int dw = mDisplay.getWidth();
1705 final int dh = mDisplay.getHeight();
1706
1707 int curTokenIndex = mWallpaperTokens.size();
1708 while (curTokenIndex > 0) {
1709 curTokenIndex--;
1710 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001711 if (token.hidden == visible) {
1712 token.hidden = !visible;
1713 // Need to do a layout to ensure the wallpaper now has the
1714 // correct size.
1715 mLayoutNeeded = true;
1716 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001717
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001718 int curWallpaperIndex = token.windows.size();
1719 while (curWallpaperIndex > 0) {
1720 curWallpaperIndex--;
1721 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1722 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001723 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001724 }
1725
1726 if (wallpaper.mWallpaperVisible != visible) {
1727 wallpaper.mWallpaperVisible = visible;
1728 try {
1729 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001730 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001731 + ": " + visible);
1732 wallpaper.mClient.dispatchAppVisibility(visible);
1733 } catch (RemoteException e) {
1734 }
1735 }
1736 }
1737 }
1738 }
1739
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001740 void sendPointerToWallpaperLocked(WindowState srcWin,
1741 MotionEvent pointer, long eventTime) {
1742 int curTokenIndex = mWallpaperTokens.size();
1743 while (curTokenIndex > 0) {
1744 curTokenIndex--;
1745 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1746 int curWallpaperIndex = token.windows.size();
1747 while (curWallpaperIndex > 0) {
1748 curWallpaperIndex--;
1749 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1750 if ((wallpaper.mAttrs.flags &
1751 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1752 continue;
1753 }
1754 try {
1755 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001756 if (srcWin != null) {
1757 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1758 srcWin.mFrame.top-wallpaper.mFrame.top);
1759 } else {
1760 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1761 }
1762 switch (pointer.getAction()) {
1763 case MotionEvent.ACTION_DOWN:
1764 mSendingPointersToWallpaper = true;
1765 break;
1766 case MotionEvent.ACTION_UP:
1767 mSendingPointersToWallpaper = false;
1768 break;
1769 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001770 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1771 } catch (RemoteException e) {
1772 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1773 }
1774 }
1775 }
1776 }
1777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 public int addWindow(Session session, IWindow client,
1779 WindowManager.LayoutParams attrs, int viewVisibility,
1780 Rect outContentInsets) {
1781 int res = mPolicy.checkAddPermission(attrs);
1782 if (res != WindowManagerImpl.ADD_OKAY) {
1783 return res;
1784 }
Romain Guy06882f82009-06-10 13:36:04 -07001785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 boolean reportNewConfig = false;
1787 WindowState attachedWindow = null;
1788 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 synchronized(mWindowMap) {
1791 // Instantiating a Display requires talking with the simulator,
1792 // so don't do it until we know the system is mostly up and
1793 // running.
1794 if (mDisplay == null) {
1795 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1796 mDisplay = wm.getDefaultDisplay();
1797 mQueue.setDisplay(mDisplay);
1798 reportNewConfig = true;
1799 }
Romain Guy06882f82009-06-10 13:36:04 -07001800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 if (mWindowMap.containsKey(client.asBinder())) {
1802 Log.w(TAG, "Window " + client + " is already added");
1803 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1804 }
1805
1806 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001807 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 if (attachedWindow == null) {
1809 Log.w(TAG, "Attempted to add window with token that is not a window: "
1810 + attrs.token + ". Aborting.");
1811 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1812 }
1813 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1814 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1815 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1816 + attrs.token + ". Aborting.");
1817 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1818 }
1819 }
1820
1821 boolean addToken = false;
1822 WindowToken token = mTokenMap.get(attrs.token);
1823 if (token == null) {
1824 if (attrs.type >= FIRST_APPLICATION_WINDOW
1825 && attrs.type <= LAST_APPLICATION_WINDOW) {
1826 Log.w(TAG, "Attempted to add application window with unknown token "
1827 + attrs.token + ". Aborting.");
1828 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1829 }
1830 if (attrs.type == TYPE_INPUT_METHOD) {
1831 Log.w(TAG, "Attempted to add input method window with unknown token "
1832 + attrs.token + ". Aborting.");
1833 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1834 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001835 if (attrs.type == TYPE_WALLPAPER) {
1836 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1837 + attrs.token + ". Aborting.");
1838 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 token = new WindowToken(attrs.token, -1, false);
1841 addToken = true;
1842 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1843 && attrs.type <= LAST_APPLICATION_WINDOW) {
1844 AppWindowToken atoken = token.appWindowToken;
1845 if (atoken == null) {
1846 Log.w(TAG, "Attempted to add window with non-application token "
1847 + token + ". Aborting.");
1848 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1849 } else if (atoken.removed) {
1850 Log.w(TAG, "Attempted to add window with exiting application token "
1851 + token + ". Aborting.");
1852 return WindowManagerImpl.ADD_APP_EXITING;
1853 }
1854 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1855 // No need for this guy!
1856 if (localLOGV) Log.v(
1857 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1858 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1859 }
1860 } else if (attrs.type == TYPE_INPUT_METHOD) {
1861 if (token.windowType != TYPE_INPUT_METHOD) {
1862 Log.w(TAG, "Attempted to add input method window with bad token "
1863 + attrs.token + ". Aborting.");
1864 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1865 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001866 } else if (attrs.type == TYPE_WALLPAPER) {
1867 if (token.windowType != TYPE_WALLPAPER) {
1868 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1869 + attrs.token + ". Aborting.");
1870 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 }
1873
1874 win = new WindowState(session, client, token,
1875 attachedWindow, attrs, viewVisibility);
1876 if (win.mDeathRecipient == null) {
1877 // Client has apparently died, so there is no reason to
1878 // continue.
1879 Log.w(TAG, "Adding window client " + client.asBinder()
1880 + " that is dead, aborting.");
1881 return WindowManagerImpl.ADD_APP_EXITING;
1882 }
1883
1884 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 res = mPolicy.prepareAddWindowLw(win, attrs);
1887 if (res != WindowManagerImpl.ADD_OKAY) {
1888 return res;
1889 }
1890
1891 // From now on, no exceptions or errors allowed!
1892
1893 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 if (addToken) {
1898 mTokenMap.put(attrs.token, token);
1899 mTokenList.add(token);
1900 }
1901 win.attach();
1902 mWindowMap.put(client.asBinder(), win);
1903
1904 if (attrs.type == TYPE_APPLICATION_STARTING &&
1905 token.appWindowToken != null) {
1906 token.appWindowToken.startingWindow = win;
1907 }
1908
1909 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 if (attrs.type == TYPE_INPUT_METHOD) {
1912 mInputMethodWindow = win;
1913 addInputMethodWindowToListLocked(win);
1914 imMayMove = false;
1915 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1916 mInputMethodDialogs.add(win);
1917 addWindowToListInOrderLocked(win, true);
1918 adjustInputMethodDialogsLocked();
1919 imMayMove = false;
1920 } else {
1921 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001922 if (attrs.type == TYPE_WALLPAPER) {
1923 mLastWallpaperTimeoutTime = 0;
1924 adjustWallpaperWindowsLocked();
1925 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001926 adjustWallpaperWindowsLocked();
1927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
Romain Guy06882f82009-06-10 13:36:04 -07001929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 if (mInTouchMode) {
1935 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1936 }
1937 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1938 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1939 }
Romain Guy06882f82009-06-10 13:36:04 -07001940
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001941 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001943 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1944 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 imMayMove = false;
1946 }
1947 }
Romain Guy06882f82009-06-10 13:36:04 -07001948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001950 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
Romain Guy06882f82009-06-10 13:36:04 -07001952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 assignLayersLocked();
1954 // Don't do layout here, the window must call
1955 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 //dump();
1958
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001959 if (focusChanged) {
1960 if (mCurrentFocus != null) {
1961 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1962 }
1963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 if (localLOGV) Log.v(
1965 TAG, "New client " + client.asBinder()
1966 + ": window=" + win);
1967 }
1968
1969 // sendNewConfiguration() checks caller permissions so we must call it with
1970 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1971 // identity anyway, so it's safe to just clear & restore around this whole
1972 // block.
1973 final long origId = Binder.clearCallingIdentity();
1974 if (reportNewConfig) {
1975 sendNewConfiguration();
1976 } else {
1977 // Update Orientation after adding a window, only if the window needs to be
1978 // displayed right away
1979 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001980 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 sendNewConfiguration();
1982 }
1983 }
1984 }
1985 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 return res;
1988 }
Romain Guy06882f82009-06-10 13:36:04 -07001989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 public void removeWindow(Session session, IWindow client) {
1991 synchronized(mWindowMap) {
1992 WindowState win = windowForClientLocked(session, client);
1993 if (win == null) {
1994 return;
1995 }
1996 removeWindowLocked(session, win);
1997 }
1998 }
Romain Guy06882f82009-06-10 13:36:04 -07001999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 public void removeWindowLocked(Session session, WindowState win) {
2001
2002 if (localLOGV || DEBUG_FOCUS) Log.v(
2003 TAG, "Remove " + win + " client="
2004 + Integer.toHexString(System.identityHashCode(
2005 win.mClient.asBinder()))
2006 + ", surface=" + win.mSurface);
2007
2008 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 if (DEBUG_APP_TRANSITIONS) Log.v(
2011 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2012 + " mExiting=" + win.mExiting
2013 + " isAnimating=" + win.isAnimating()
2014 + " app-animation="
2015 + (win.mAppToken != null ? win.mAppToken.animation : null)
2016 + " inPendingTransaction="
2017 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2018 + " mDisplayFrozen=" + mDisplayFrozen);
2019 // Visibility of the removed window. Will be used later to update orientation later on.
2020 boolean wasVisible = false;
2021 // First, see if we need to run an animation. If we do, we have
2022 // to hold off on removing the window until the animation is done.
2023 // If the display is frozen, just remove immediately, since the
2024 // animation wouldn't be seen.
2025 if (win.mSurface != null && !mDisplayFrozen) {
2026 // If we are not currently running the exit animation, we
2027 // need to see about starting one.
2028 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2031 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2032 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2033 }
2034 // Try starting an animation.
2035 if (applyAnimationLocked(win, transit, false)) {
2036 win.mExiting = true;
2037 }
2038 }
2039 if (win.mExiting || win.isAnimating()) {
2040 // The exit animation is running... wait for it!
2041 //Log.i(TAG, "*** Running exit animation...");
2042 win.mExiting = true;
2043 win.mRemoveOnExit = true;
2044 mLayoutNeeded = true;
2045 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2046 performLayoutAndPlaceSurfacesLocked();
2047 if (win.mAppToken != null) {
2048 win.mAppToken.updateReportedVisibilityLocked();
2049 }
2050 //dump();
2051 Binder.restoreCallingIdentity(origId);
2052 return;
2053 }
2054 }
2055
2056 removeWindowInnerLocked(session, win);
2057 // Removing a visible window will effect the computed orientation
2058 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002059 if (wasVisible && computeForcedAppOrientationLocked()
2060 != mForcedAppOrientation) {
2061 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
2063 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2064 Binder.restoreCallingIdentity(origId);
2065 }
Romain Guy06882f82009-06-10 13:36:04 -07002066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002068 mKeyWaiter.finishedKey(session, win.mClient, true,
2069 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2071 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 if (mInputMethodTarget == win) {
2076 moveInputMethodWindowsIfNeededLocked(false);
2077 }
Romain Guy06882f82009-06-10 13:36:04 -07002078
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002079 if (false) {
2080 RuntimeException e = new RuntimeException("here");
2081 e.fillInStackTrace();
2082 Log.w(TAG, "Removing window " + win, e);
2083 }
2084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 mPolicy.removeWindowLw(win);
2086 win.removeLocked();
2087
2088 mWindowMap.remove(win.mClient.asBinder());
2089 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002090 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091
2092 if (mInputMethodWindow == win) {
2093 mInputMethodWindow = null;
2094 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2095 mInputMethodDialogs.remove(win);
2096 }
Romain Guy06882f82009-06-10 13:36:04 -07002097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 final WindowToken token = win.mToken;
2099 final AppWindowToken atoken = win.mAppToken;
2100 token.windows.remove(win);
2101 if (atoken != null) {
2102 atoken.allAppWindows.remove(win);
2103 }
2104 if (localLOGV) Log.v(
2105 TAG, "**** Removing window " + win + ": count="
2106 + token.windows.size());
2107 if (token.windows.size() == 0) {
2108 if (!token.explicit) {
2109 mTokenMap.remove(token.token);
2110 mTokenList.remove(token);
2111 } else if (atoken != null) {
2112 atoken.firstWindowDrawn = false;
2113 }
2114 }
2115
2116 if (atoken != null) {
2117 if (atoken.startingWindow == win) {
2118 atoken.startingWindow = null;
2119 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2120 // If this is the last window and we had requested a starting
2121 // transition window, well there is no point now.
2122 atoken.startingData = null;
2123 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2124 // If this is the last window except for a starting transition
2125 // window, we need to get rid of the starting transition.
2126 if (DEBUG_STARTING_WINDOW) {
2127 Log.v(TAG, "Schedule remove starting " + token
2128 + ": no more real windows");
2129 }
2130 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2131 mH.sendMessage(m);
2132 }
2133 }
Romain Guy06882f82009-06-10 13:36:04 -07002134
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002135 if (win.mAttrs.type == TYPE_WALLPAPER) {
2136 mLastWallpaperTimeoutTime = 0;
2137 adjustWallpaperWindowsLocked();
2138 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002139 adjustWallpaperWindowsLocked();
2140 }
2141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 if (!mInLayout) {
2143 assignLayersLocked();
2144 mLayoutNeeded = true;
2145 performLayoutAndPlaceSurfacesLocked();
2146 if (win.mAppToken != null) {
2147 win.mAppToken.updateReportedVisibilityLocked();
2148 }
2149 }
2150 }
2151
2152 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2153 long origId = Binder.clearCallingIdentity();
2154 try {
2155 synchronized (mWindowMap) {
2156 WindowState w = windowForClientLocked(session, client);
2157 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002158 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 Surface.openTransaction();
2160 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002161 if (SHOW_TRANSACTIONS) Log.i(
2162 TAG, " SURFACE " + w.mSurface
2163 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 w.mSurface.setTransparentRegionHint(region);
2165 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002166 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 Surface.closeTransaction();
2168 }
2169 }
2170 }
2171 } finally {
2172 Binder.restoreCallingIdentity(origId);
2173 }
2174 }
2175
2176 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002177 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 Rect visibleInsets) {
2179 long origId = Binder.clearCallingIdentity();
2180 try {
2181 synchronized (mWindowMap) {
2182 WindowState w = windowForClientLocked(session, client);
2183 if (w != null) {
2184 w.mGivenInsetsPending = false;
2185 w.mGivenContentInsets.set(contentInsets);
2186 w.mGivenVisibleInsets.set(visibleInsets);
2187 w.mTouchableInsets = touchableInsets;
2188 mLayoutNeeded = true;
2189 performLayoutAndPlaceSurfacesLocked();
2190 }
2191 }
2192 } finally {
2193 Binder.restoreCallingIdentity(origId);
2194 }
2195 }
Romain Guy06882f82009-06-10 13:36:04 -07002196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 public void getWindowDisplayFrame(Session session, IWindow client,
2198 Rect outDisplayFrame) {
2199 synchronized(mWindowMap) {
2200 WindowState win = windowForClientLocked(session, client);
2201 if (win == null) {
2202 outDisplayFrame.setEmpty();
2203 return;
2204 }
2205 outDisplayFrame.set(win.mDisplayFrame);
2206 }
2207 }
2208
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002209 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2210 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002211 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2212 window.mWallpaperX = x;
2213 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002214 window.mWallpaperXStep = xStep;
2215 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002216 if (updateWallpaperOffsetLocked(window, true)) {
2217 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002218 }
2219 }
2220 }
2221
Dianne Hackborn75804932009-10-20 20:15:20 -07002222 void wallpaperCommandComplete(IBinder window, Bundle result) {
2223 synchronized (mWindowMap) {
2224 if (mWaitingOnWallpaper != null &&
2225 mWaitingOnWallpaper.mClient.asBinder() == window) {
2226 mWaitingOnWallpaper = null;
2227 mWindowMap.notifyAll();
2228 }
2229 }
2230 }
2231
2232 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2233 String action, int x, int y, int z, Bundle extras, boolean sync) {
2234 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2235 || window == mUpperWallpaperTarget) {
2236 boolean doWait = sync;
2237 int curTokenIndex = mWallpaperTokens.size();
2238 while (curTokenIndex > 0) {
2239 curTokenIndex--;
2240 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2241 int curWallpaperIndex = token.windows.size();
2242 while (curWallpaperIndex > 0) {
2243 curWallpaperIndex--;
2244 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2245 try {
2246 wallpaper.mClient.dispatchWallpaperCommand(action,
2247 x, y, z, extras, sync);
2248 // We only want to be synchronous with one wallpaper.
2249 sync = false;
2250 } catch (RemoteException e) {
2251 }
2252 }
2253 }
2254
2255 if (doWait) {
2256 // XXX Need to wait for result.
2257 }
2258 }
2259
2260 return null;
2261 }
2262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 public int relayoutWindow(Session session, IWindow client,
2264 WindowManager.LayoutParams attrs, int requestedWidth,
2265 int requestedHeight, int viewVisibility, boolean insetsPending,
2266 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2267 Surface outSurface) {
2268 boolean displayed = false;
2269 boolean inTouchMode;
2270 Configuration newConfig = null;
2271 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 synchronized(mWindowMap) {
2274 WindowState win = windowForClientLocked(session, client);
2275 if (win == null) {
2276 return 0;
2277 }
2278 win.mRequestedWidth = requestedWidth;
2279 win.mRequestedHeight = requestedHeight;
2280
2281 if (attrs != null) {
2282 mPolicy.adjustWindowParamsLw(attrs);
2283 }
Romain Guy06882f82009-06-10 13:36:04 -07002284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 int attrChanges = 0;
2286 int flagChanges = 0;
2287 if (attrs != null) {
2288 flagChanges = win.mAttrs.flags ^= attrs.flags;
2289 attrChanges = win.mAttrs.copyFrom(attrs);
2290 }
2291
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002292 if (DEBUG_LAYOUT) Log.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293
2294 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2295 win.mAlpha = attrs.alpha;
2296 }
2297
2298 final boolean scaledWindow =
2299 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2300
2301 if (scaledWindow) {
2302 // requested{Width|Height} Surface's physical size
2303 // attrs.{width|height} Size on screen
2304 win.mHScale = (attrs.width != requestedWidth) ?
2305 (attrs.width / (float)requestedWidth) : 1.0f;
2306 win.mVScale = (attrs.height != requestedHeight) ?
2307 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002308 } else {
2309 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 }
2311
2312 boolean imMayMove = (flagChanges&(
2313 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2314 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 boolean focusMayChange = win.mViewVisibility != viewVisibility
2317 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2318 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002319
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002320 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2321 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 win.mRelayoutCalled = true;
2324 final int oldVisibility = win.mViewVisibility;
2325 win.mViewVisibility = viewVisibility;
2326 if (viewVisibility == View.VISIBLE &&
2327 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2328 displayed = !win.isVisibleLw();
2329 if (win.mExiting) {
2330 win.mExiting = false;
2331 win.mAnimation = null;
2332 }
2333 if (win.mDestroying) {
2334 win.mDestroying = false;
2335 mDestroySurface.remove(win);
2336 }
2337 if (oldVisibility == View.GONE) {
2338 win.mEnterAnimationPending = true;
2339 }
2340 if (displayed && win.mSurface != null && !win.mDrawPending
2341 && !win.mCommitDrawPending && !mDisplayFrozen) {
2342 applyEnterAnimationLocked(win);
2343 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002344 if (displayed && (win.mAttrs.flags
2345 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2346 win.mTurnOnScreen = true;
2347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2349 // To change the format, we need to re-build the surface.
2350 win.destroySurfaceLocked();
2351 displayed = true;
2352 }
2353 try {
2354 Surface surface = win.createSurfaceLocked();
2355 if (surface != null) {
2356 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002357 win.mReportDestroySurface = false;
2358 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002359 if (SHOW_TRANSACTIONS) Log.i(TAG,
2360 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002362 // For some reason there isn't a surface. Clear the
2363 // caller's object so they see the same state.
2364 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 }
2366 } catch (Exception e) {
2367 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002368 + client + " (" + win.mAttrs.getTitle() + ")",
2369 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 Binder.restoreCallingIdentity(origId);
2371 return 0;
2372 }
2373 if (displayed) {
2374 focusMayChange = true;
2375 }
2376 if (win.mAttrs.type == TYPE_INPUT_METHOD
2377 && mInputMethodWindow == null) {
2378 mInputMethodWindow = win;
2379 imMayMove = true;
2380 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002381 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2382 && win.mAppToken != null
2383 && win.mAppToken.startingWindow != null) {
2384 // Special handling of starting window over the base
2385 // window of the app: propagate lock screen flags to it,
2386 // to provide the correct semantics while starting.
2387 final int mask =
2388 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
2389 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
2390 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2391 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 } else {
2394 win.mEnterAnimationPending = false;
2395 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002396 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2397 + ": mExiting=" + win.mExiting
2398 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 // If we are not currently running the exit animation, we
2400 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002401 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 // Try starting an animation; if there isn't one, we
2403 // can destroy the surface right away.
2404 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2405 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2406 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2407 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002408 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002410 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 win.mExiting = true;
2412 mKeyWaiter.finishedKey(session, client, true,
2413 KeyWaiter.RETURN_NOTHING);
2414 } else if (win.isAnimating()) {
2415 // Currently in a hide animation... turn this into
2416 // an exit.
2417 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002418 } else if (win == mWallpaperTarget) {
2419 // If the wallpaper is currently behind this
2420 // window, we need to change both of them inside
2421 // of a transaction to avoid artifacts.
2422 win.mExiting = true;
2423 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 } else {
2425 if (mInputMethodWindow == win) {
2426 mInputMethodWindow = null;
2427 }
2428 win.destroySurfaceLocked();
2429 }
2430 }
2431 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002432
2433 if (win.mSurface == null || (win.getAttrs().flags
2434 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2435 || win.mSurfacePendingDestroy) {
2436 // We are being called from a local process, which
2437 // means outSurface holds its current surface. Ensure the
2438 // surface object is cleared, but we don't want it actually
2439 // destroyed at this point.
2440 win.mSurfacePendingDestroy = false;
2441 outSurface.release();
2442 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2443 } else if (win.mSurface != null) {
2444 if (DEBUG_VISIBILITY) Log.i(TAG,
2445 "Keeping surface, will report destroy: " + win);
2446 win.mReportDestroySurface = true;
2447 outSurface.copyFrom(win.mSurface);
2448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 }
2450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 if (focusMayChange) {
2452 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2453 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 imMayMove = false;
2455 }
2456 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2457 }
Romain Guy06882f82009-06-10 13:36:04 -07002458
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002459 // updateFocusedWindowLocked() already assigned layers so we only need to
2460 // reassign them at this point if the IM window state gets shuffled
2461 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002464 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2465 // Little hack here -- we -should- be able to rely on the
2466 // function to return true if the IME has moved and needs
2467 // its layer recomputed. However, if the IME was hidden
2468 // and isn't actually moved in the list, its layer may be
2469 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 assignLayers = true;
2471 }
2472 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002473 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002474 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002475 assignLayers = true;
2476 }
2477 }
Romain Guy06882f82009-06-10 13:36:04 -07002478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 mLayoutNeeded = true;
2480 win.mGivenInsetsPending = insetsPending;
2481 if (assignLayers) {
2482 assignLayersLocked();
2483 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002484 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002486 if (displayed && win.mIsWallpaper) {
2487 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002488 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 if (win.mAppToken != null) {
2491 win.mAppToken.updateReportedVisibilityLocked();
2492 }
2493 outFrame.set(win.mFrame);
2494 outContentInsets.set(win.mContentInsets);
2495 outVisibleInsets.set(win.mVisibleInsets);
2496 if (localLOGV) Log.v(
2497 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002498 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 + ", requestedHeight=" + requestedHeight
2500 + ", viewVisibility=" + viewVisibility
2501 + "\nRelayout returning frame=" + outFrame
2502 + ", surface=" + outSurface);
2503
2504 if (localLOGV || DEBUG_FOCUS) Log.v(
2505 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2506
2507 inTouchMode = mInTouchMode;
2508 }
2509
2510 if (newConfig != null) {
2511 sendNewConfiguration();
2512 }
Romain Guy06882f82009-06-10 13:36:04 -07002513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2517 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2518 }
2519
2520 public void finishDrawingWindow(Session session, IWindow client) {
2521 final long origId = Binder.clearCallingIdentity();
2522 synchronized(mWindowMap) {
2523 WindowState win = windowForClientLocked(session, client);
2524 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002525 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2526 adjustWallpaperWindowsLocked();
2527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 mLayoutNeeded = true;
2529 performLayoutAndPlaceSurfacesLocked();
2530 }
2531 }
2532 Binder.restoreCallingIdentity(origId);
2533 }
2534
2535 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2536 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2537 + (lp != null ? lp.packageName : null)
2538 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2539 if (lp != null && lp.windowAnimations != 0) {
2540 // If this is a system resource, don't try to load it from the
2541 // application resources. It is nice to avoid loading application
2542 // resources if we can.
2543 String packageName = lp.packageName != null ? lp.packageName : "android";
2544 int resId = lp.windowAnimations;
2545 if ((resId&0xFF000000) == 0x01000000) {
2546 packageName = "android";
2547 }
2548 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2549 + packageName);
2550 return AttributeCache.instance().get(packageName, resId,
2551 com.android.internal.R.styleable.WindowAnimation);
2552 }
2553 return null;
2554 }
Romain Guy06882f82009-06-10 13:36:04 -07002555
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002556 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2557 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2558 + packageName + " resId=0x" + Integer.toHexString(resId));
2559 if (packageName != null) {
2560 if ((resId&0xFF000000) == 0x01000000) {
2561 packageName = "android";
2562 }
2563 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2564 + packageName);
2565 return AttributeCache.instance().get(packageName, resId,
2566 com.android.internal.R.styleable.WindowAnimation);
2567 }
2568 return null;
2569 }
2570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 private void applyEnterAnimationLocked(WindowState win) {
2572 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2573 if (win.mEnterAnimationPending) {
2574 win.mEnterAnimationPending = false;
2575 transit = WindowManagerPolicy.TRANSIT_ENTER;
2576 }
2577
2578 applyAnimationLocked(win, transit, true);
2579 }
2580
2581 private boolean applyAnimationLocked(WindowState win,
2582 int transit, boolean isEntrance) {
2583 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2584 // If we are trying to apply an animation, but already running
2585 // an animation of the same type, then just leave that one alone.
2586 return true;
2587 }
Romain Guy06882f82009-06-10 13:36:04 -07002588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 // Only apply an animation if the display isn't frozen. If it is
2590 // frozen, there is no reason to animate and it can cause strange
2591 // artifacts when we unfreeze the display if some different animation
2592 // is running.
2593 if (!mDisplayFrozen) {
2594 int anim = mPolicy.selectAnimationLw(win, transit);
2595 int attr = -1;
2596 Animation a = null;
2597 if (anim != 0) {
2598 a = AnimationUtils.loadAnimation(mContext, anim);
2599 } else {
2600 switch (transit) {
2601 case WindowManagerPolicy.TRANSIT_ENTER:
2602 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2603 break;
2604 case WindowManagerPolicy.TRANSIT_EXIT:
2605 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2606 break;
2607 case WindowManagerPolicy.TRANSIT_SHOW:
2608 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2609 break;
2610 case WindowManagerPolicy.TRANSIT_HIDE:
2611 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2612 break;
2613 }
2614 if (attr >= 0) {
2615 a = loadAnimation(win.mAttrs, attr);
2616 }
2617 }
2618 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2619 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2620 + " mAnimation=" + win.mAnimation
2621 + " isEntrance=" + isEntrance);
2622 if (a != null) {
2623 if (DEBUG_ANIM) {
2624 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002625 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2627 }
2628 win.setAnimation(a);
2629 win.mAnimationIsEntrance = isEntrance;
2630 }
2631 } else {
2632 win.clearAnimation();
2633 }
2634
2635 return win.mAnimation != null;
2636 }
2637
2638 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2639 int anim = 0;
2640 Context context = mContext;
2641 if (animAttr >= 0) {
2642 AttributeCache.Entry ent = getCachedAnimations(lp);
2643 if (ent != null) {
2644 context = ent.context;
2645 anim = ent.array.getResourceId(animAttr, 0);
2646 }
2647 }
2648 if (anim != 0) {
2649 return AnimationUtils.loadAnimation(context, anim);
2650 }
2651 return null;
2652 }
Romain Guy06882f82009-06-10 13:36:04 -07002653
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002654 private Animation loadAnimation(String packageName, int resId) {
2655 int anim = 0;
2656 Context context = mContext;
2657 if (resId >= 0) {
2658 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2659 if (ent != null) {
2660 context = ent.context;
2661 anim = resId;
2662 }
2663 }
2664 if (anim != 0) {
2665 return AnimationUtils.loadAnimation(context, anim);
2666 }
2667 return null;
2668 }
2669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 private boolean applyAnimationLocked(AppWindowToken wtoken,
2671 WindowManager.LayoutParams lp, int transit, boolean enter) {
2672 // Only apply an animation if the display isn't frozen. If it is
2673 // frozen, there is no reason to animate and it can cause strange
2674 // artifacts when we unfreeze the display if some different animation
2675 // is running.
2676 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002677 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002678 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002679 a = new FadeInOutAnimation(enter);
2680 if (DEBUG_ANIM) Log.v(TAG,
2681 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002682 } else if (mNextAppTransitionPackage != null) {
2683 a = loadAnimation(mNextAppTransitionPackage, enter ?
2684 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002685 } else {
2686 int animAttr = 0;
2687 switch (transit) {
2688 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2689 animAttr = enter
2690 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2691 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2692 break;
2693 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2694 animAttr = enter
2695 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2696 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2697 break;
2698 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2699 animAttr = enter
2700 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2701 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2702 break;
2703 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2704 animAttr = enter
2705 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2706 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2707 break;
2708 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2709 animAttr = enter
2710 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2711 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2712 break;
2713 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2714 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002715 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002716 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2717 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002718 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002719 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002720 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2721 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002722 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002723 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002724 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002725 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2726 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2727 break;
2728 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2729 animAttr = enter
2730 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2731 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2732 break;
2733 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2734 animAttr = enter
2735 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2736 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002737 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002738 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002739 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002740 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2741 + " anim=" + a
2742 + " animAttr=0x" + Integer.toHexString(animAttr)
2743 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 if (a != null) {
2746 if (DEBUG_ANIM) {
2747 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002748 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2750 }
2751 wtoken.setAnimation(a);
2752 }
2753 } else {
2754 wtoken.clearAnimation();
2755 }
2756
2757 return wtoken.animation != null;
2758 }
2759
2760 // -------------------------------------------------------------
2761 // Application Window Tokens
2762 // -------------------------------------------------------------
2763
2764 public void validateAppTokens(List tokens) {
2765 int v = tokens.size()-1;
2766 int m = mAppTokens.size()-1;
2767 while (v >= 0 && m >= 0) {
2768 AppWindowToken wtoken = mAppTokens.get(m);
2769 if (wtoken.removed) {
2770 m--;
2771 continue;
2772 }
2773 if (tokens.get(v) != wtoken.token) {
2774 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2775 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2776 }
2777 v--;
2778 m--;
2779 }
2780 while (v >= 0) {
2781 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2782 v--;
2783 }
2784 while (m >= 0) {
2785 AppWindowToken wtoken = mAppTokens.get(m);
2786 if (!wtoken.removed) {
2787 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2788 }
2789 m--;
2790 }
2791 }
2792
2793 boolean checkCallingPermission(String permission, String func) {
2794 // Quick check: if the calling permission is me, it's all okay.
2795 if (Binder.getCallingPid() == Process.myPid()) {
2796 return true;
2797 }
Romain Guy06882f82009-06-10 13:36:04 -07002798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 if (mContext.checkCallingPermission(permission)
2800 == PackageManager.PERMISSION_GRANTED) {
2801 return true;
2802 }
2803 String msg = "Permission Denial: " + func + " from pid="
2804 + Binder.getCallingPid()
2805 + ", uid=" + Binder.getCallingUid()
2806 + " requires " + permission;
2807 Log.w(TAG, msg);
2808 return false;
2809 }
Romain Guy06882f82009-06-10 13:36:04 -07002810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 AppWindowToken findAppWindowToken(IBinder token) {
2812 WindowToken wtoken = mTokenMap.get(token);
2813 if (wtoken == null) {
2814 return null;
2815 }
2816 return wtoken.appWindowToken;
2817 }
Romain Guy06882f82009-06-10 13:36:04 -07002818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 public void addWindowToken(IBinder token, int type) {
2820 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2821 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002822 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
Romain Guy06882f82009-06-10 13:36:04 -07002824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 synchronized(mWindowMap) {
2826 WindowToken wtoken = mTokenMap.get(token);
2827 if (wtoken != null) {
2828 Log.w(TAG, "Attempted to add existing input method token: " + token);
2829 return;
2830 }
2831 wtoken = new WindowToken(token, type, true);
2832 mTokenMap.put(token, wtoken);
2833 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002834 if (type == TYPE_WALLPAPER) {
2835 mWallpaperTokens.add(wtoken);
2836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 }
2838 }
Romain Guy06882f82009-06-10 13:36:04 -07002839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 public void removeWindowToken(IBinder token) {
2841 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2842 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002843 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 }
2845
2846 final long origId = Binder.clearCallingIdentity();
2847 synchronized(mWindowMap) {
2848 WindowToken wtoken = mTokenMap.remove(token);
2849 mTokenList.remove(wtoken);
2850 if (wtoken != null) {
2851 boolean delayed = false;
2852 if (!wtoken.hidden) {
2853 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 final int N = wtoken.windows.size();
2856 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 for (int i=0; i<N; i++) {
2859 WindowState win = wtoken.windows.get(i);
2860
2861 if (win.isAnimating()) {
2862 delayed = true;
2863 }
Romain Guy06882f82009-06-10 13:36:04 -07002864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 if (win.isVisibleNow()) {
2866 applyAnimationLocked(win,
2867 WindowManagerPolicy.TRANSIT_EXIT, false);
2868 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2869 KeyWaiter.RETURN_NOTHING);
2870 changed = true;
2871 }
2872 }
2873
2874 if (changed) {
2875 mLayoutNeeded = true;
2876 performLayoutAndPlaceSurfacesLocked();
2877 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2878 }
Romain Guy06882f82009-06-10 13:36:04 -07002879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 if (delayed) {
2881 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002882 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2883 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 }
2885 }
Romain Guy06882f82009-06-10 13:36:04 -07002886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 } else {
2888 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2889 }
2890 }
2891 Binder.restoreCallingIdentity(origId);
2892 }
2893
2894 public void addAppToken(int addPos, IApplicationToken token,
2895 int groupId, int requestedOrientation, boolean fullscreen) {
2896 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2897 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002898 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 }
Romain Guy06882f82009-06-10 13:36:04 -07002900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 synchronized(mWindowMap) {
2902 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2903 if (wtoken != null) {
2904 Log.w(TAG, "Attempted to add existing app token: " + token);
2905 return;
2906 }
2907 wtoken = new AppWindowToken(token);
2908 wtoken.groupId = groupId;
2909 wtoken.appFullscreen = fullscreen;
2910 wtoken.requestedOrientation = requestedOrientation;
2911 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002912 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 mTokenMap.put(token.asBinder(), wtoken);
2914 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 // Application tokens start out hidden.
2917 wtoken.hidden = true;
2918 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 //dump();
2921 }
2922 }
Romain Guy06882f82009-06-10 13:36:04 -07002923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 public void setAppGroupId(IBinder token, int groupId) {
2925 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2926 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002927 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 }
2929
2930 synchronized(mWindowMap) {
2931 AppWindowToken wtoken = findAppWindowToken(token);
2932 if (wtoken == null) {
2933 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2934 return;
2935 }
2936 wtoken.groupId = groupId;
2937 }
2938 }
Romain Guy06882f82009-06-10 13:36:04 -07002939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 public int getOrientationFromWindowsLocked() {
2941 int pos = mWindows.size() - 1;
2942 while (pos >= 0) {
2943 WindowState wtoken = (WindowState) mWindows.get(pos);
2944 pos--;
2945 if (wtoken.mAppToken != null) {
2946 // We hit an application window. so the orientation will be determined by the
2947 // app window. No point in continuing further.
2948 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2949 }
2950 if (!wtoken.isVisibleLw()) {
2951 continue;
2952 }
2953 int req = wtoken.mAttrs.screenOrientation;
2954 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2955 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2956 continue;
2957 } else {
2958 return req;
2959 }
2960 }
2961 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2962 }
Romain Guy06882f82009-06-10 13:36:04 -07002963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 public int getOrientationFromAppTokensLocked() {
2965 int pos = mAppTokens.size() - 1;
2966 int curGroup = 0;
2967 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002968 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002970 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 while (pos >= 0) {
2972 AppWindowToken wtoken = mAppTokens.get(pos);
2973 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002974 // if we're about to tear down this window and not seek for
2975 // the behind activity, don't use it for orientation
2976 if (!findingBehind
2977 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002978 continue;
2979 }
2980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 if (!haveGroup) {
2982 // We ignore any hidden applications on the top.
2983 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2984 continue;
2985 }
2986 haveGroup = true;
2987 curGroup = wtoken.groupId;
2988 lastOrientation = wtoken.requestedOrientation;
2989 } else if (curGroup != wtoken.groupId) {
2990 // If we have hit a new application group, and the bottom
2991 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002992 // the orientation behind it, and the last app was
2993 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002995 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2996 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 return lastOrientation;
2998 }
2999 }
3000 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07003001 // If this application is fullscreen, and didn't explicitly say
3002 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08003004 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07003005 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07003006 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 return or;
3008 }
3009 // If this application has requested an explicit orientation,
3010 // then use it.
3011 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3012 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3013 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3014 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3015 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3016 return or;
3017 }
Owen Lin3413b892009-05-01 17:12:32 -07003018 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 }
3020 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3021 }
Romain Guy06882f82009-06-10 13:36:04 -07003022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003024 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003025 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3026 "updateOrientationFromAppTokens()")) {
3027 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3028 }
3029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 Configuration config;
3031 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003032 config = updateOrientationFromAppTokensUnchecked(currentConfig,
3033 freezeThisOneIfNeeded);
3034 Binder.restoreCallingIdentity(ident);
3035 return config;
3036 }
3037
3038 Configuration updateOrientationFromAppTokensUnchecked(
3039 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
3040 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003042 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003043 if (config != null) {
3044 mLayoutNeeded = true;
3045 performLayoutAndPlaceSurfacesLocked();
3046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 return config;
3049 }
Romain Guy06882f82009-06-10 13:36:04 -07003050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 /*
3052 * The orientation is computed from non-application windows first. If none of
3053 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003054 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3056 * android.os.IBinder)
3057 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003058 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07003059 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 long ident = Binder.clearCallingIdentity();
3062 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003063 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 if (req != mForcedAppOrientation) {
3066 changed = true;
3067 mForcedAppOrientation = req;
3068 //send a message to Policy indicating orientation change to take
3069 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003070 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 }
Romain Guy06882f82009-06-10 13:36:04 -07003072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 if (changed) {
3074 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07003075 WindowManagerPolicy.USE_LAST_ROTATION,
3076 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 if (changed) {
3078 if (freezeThisOneIfNeeded != null) {
3079 AppWindowToken wtoken = findAppWindowToken(
3080 freezeThisOneIfNeeded);
3081 if (wtoken != null) {
3082 startAppFreezingScreenLocked(wtoken,
3083 ActivityInfo.CONFIG_ORIENTATION);
3084 }
3085 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003086 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
3088 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003089
3090 // No obvious action we need to take, but if our current
3091 // state mismatches the activity maanager's, update it
3092 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003093 mTempConfiguration.setToDefaults();
3094 if (computeNewConfigurationLocked(mTempConfiguration)) {
3095 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003096 return new Configuration(mTempConfiguration);
3097 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003098 }
3099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 } finally {
3101 Binder.restoreCallingIdentity(ident);
3102 }
Romain Guy06882f82009-06-10 13:36:04 -07003103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 return null;
3105 }
Romain Guy06882f82009-06-10 13:36:04 -07003106
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003107 int computeForcedAppOrientationLocked() {
3108 int req = getOrientationFromWindowsLocked();
3109 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3110 req = getOrientationFromAppTokensLocked();
3111 }
3112 return req;
3113 }
Romain Guy06882f82009-06-10 13:36:04 -07003114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3116 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3117 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003118 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 }
Romain Guy06882f82009-06-10 13:36:04 -07003120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 synchronized(mWindowMap) {
3122 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3123 if (wtoken == null) {
3124 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3125 return;
3126 }
Romain Guy06882f82009-06-10 13:36:04 -07003127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 wtoken.requestedOrientation = requestedOrientation;
3129 }
3130 }
Romain Guy06882f82009-06-10 13:36:04 -07003131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 public int getAppOrientation(IApplicationToken token) {
3133 synchronized(mWindowMap) {
3134 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3135 if (wtoken == null) {
3136 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3137 }
Romain Guy06882f82009-06-10 13:36:04 -07003138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 return wtoken.requestedOrientation;
3140 }
3141 }
Romain Guy06882f82009-06-10 13:36:04 -07003142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3144 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3145 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003146 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
3148
3149 synchronized(mWindowMap) {
3150 boolean changed = false;
3151 if (token == null) {
3152 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3153 changed = mFocusedApp != null;
3154 mFocusedApp = null;
3155 mKeyWaiter.tickle();
3156 } else {
3157 AppWindowToken newFocus = findAppWindowToken(token);
3158 if (newFocus == null) {
3159 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3160 return;
3161 }
3162 changed = mFocusedApp != newFocus;
3163 mFocusedApp = newFocus;
3164 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3165 mKeyWaiter.tickle();
3166 }
3167
3168 if (moveFocusNow && changed) {
3169 final long origId = Binder.clearCallingIdentity();
3170 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3171 Binder.restoreCallingIdentity(origId);
3172 }
3173 }
3174 }
3175
3176 public void prepareAppTransition(int transit) {
3177 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3178 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003179 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 }
Romain Guy06882f82009-06-10 13:36:04 -07003181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 synchronized(mWindowMap) {
3183 if (DEBUG_APP_TRANSITIONS) Log.v(
3184 TAG, "Prepare app transition: transit=" + transit
3185 + " mNextAppTransition=" + mNextAppTransition);
3186 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003187 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3188 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003190 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3191 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3192 // Opening a new task always supersedes a close for the anim.
3193 mNextAppTransition = transit;
3194 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3195 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3196 // Opening a new activity always supersedes a close for the anim.
3197 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 }
3199 mAppTransitionReady = false;
3200 mAppTransitionTimeout = false;
3201 mStartingIconInTransition = false;
3202 mSkipAppTransitionAnimation = false;
3203 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3204 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3205 5000);
3206 }
3207 }
3208 }
3209
3210 public int getPendingAppTransition() {
3211 return mNextAppTransition;
3212 }
Romain Guy06882f82009-06-10 13:36:04 -07003213
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003214 public void overridePendingAppTransition(String packageName,
3215 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003216 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003217 mNextAppTransitionPackage = packageName;
3218 mNextAppTransitionEnter = enterAnim;
3219 mNextAppTransitionExit = exitAnim;
3220 }
3221 }
3222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 public void executeAppTransition() {
3224 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3225 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003226 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
Romain Guy06882f82009-06-10 13:36:04 -07003228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003230 if (DEBUG_APP_TRANSITIONS) {
3231 RuntimeException e = new RuntimeException("here");
3232 e.fillInStackTrace();
3233 Log.w(TAG, "Execute app transition: mNextAppTransition="
3234 + mNextAppTransition, e);
3235 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003236 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 mAppTransitionReady = true;
3238 final long origId = Binder.clearCallingIdentity();
3239 performLayoutAndPlaceSurfacesLocked();
3240 Binder.restoreCallingIdentity(origId);
3241 }
3242 }
3243 }
3244
3245 public void setAppStartingWindow(IBinder token, String pkg,
3246 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3247 IBinder transferFrom, boolean createIfNeeded) {
3248 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3249 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003250 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 }
3252
3253 synchronized(mWindowMap) {
3254 if (DEBUG_STARTING_WINDOW) Log.v(
3255 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3256 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 AppWindowToken wtoken = findAppWindowToken(token);
3259 if (wtoken == null) {
3260 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3261 return;
3262 }
3263
3264 // If the display is frozen, we won't do anything until the
3265 // actual window is displayed so there is no reason to put in
3266 // the starting window.
3267 if (mDisplayFrozen) {
3268 return;
3269 }
Romain Guy06882f82009-06-10 13:36:04 -07003270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 if (wtoken.startingData != null) {
3272 return;
3273 }
Romain Guy06882f82009-06-10 13:36:04 -07003274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 if (transferFrom != null) {
3276 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3277 if (ttoken != null) {
3278 WindowState startingWindow = ttoken.startingWindow;
3279 if (startingWindow != null) {
3280 if (mStartingIconInTransition) {
3281 // In this case, the starting icon has already
3282 // been displayed, so start letting windows get
3283 // shown immediately without any more transitions.
3284 mSkipAppTransitionAnimation = true;
3285 }
3286 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3287 "Moving existing starting from " + ttoken
3288 + " to " + wtoken);
3289 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 // Transfer the starting window over to the new
3292 // token.
3293 wtoken.startingData = ttoken.startingData;
3294 wtoken.startingView = ttoken.startingView;
3295 wtoken.startingWindow = startingWindow;
3296 ttoken.startingData = null;
3297 ttoken.startingView = null;
3298 ttoken.startingWindow = null;
3299 ttoken.startingMoved = true;
3300 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003301 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003303 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3304 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 mWindows.remove(startingWindow);
3306 ttoken.windows.remove(startingWindow);
3307 ttoken.allAppWindows.remove(startingWindow);
3308 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 // Propagate other interesting state between the
3311 // tokens. If the old token is displayed, we should
3312 // immediately force the new one to be displayed. If
3313 // it is animating, we need to move that animation to
3314 // the new one.
3315 if (ttoken.allDrawn) {
3316 wtoken.allDrawn = true;
3317 }
3318 if (ttoken.firstWindowDrawn) {
3319 wtoken.firstWindowDrawn = true;
3320 }
3321 if (!ttoken.hidden) {
3322 wtoken.hidden = false;
3323 wtoken.hiddenRequested = false;
3324 wtoken.willBeHidden = false;
3325 }
3326 if (wtoken.clientHidden != ttoken.clientHidden) {
3327 wtoken.clientHidden = ttoken.clientHidden;
3328 wtoken.sendAppVisibilityToClients();
3329 }
3330 if (ttoken.animation != null) {
3331 wtoken.animation = ttoken.animation;
3332 wtoken.animating = ttoken.animating;
3333 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3334 ttoken.animation = null;
3335 ttoken.animLayerAdjustment = 0;
3336 wtoken.updateLayers();
3337 ttoken.updateLayers();
3338 }
Romain Guy06882f82009-06-10 13:36:04 -07003339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 mLayoutNeeded = true;
3342 performLayoutAndPlaceSurfacesLocked();
3343 Binder.restoreCallingIdentity(origId);
3344 return;
3345 } else if (ttoken.startingData != null) {
3346 // The previous app was getting ready to show a
3347 // starting window, but hasn't yet done so. Steal it!
3348 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3349 "Moving pending starting from " + ttoken
3350 + " to " + wtoken);
3351 wtoken.startingData = ttoken.startingData;
3352 ttoken.startingData = null;
3353 ttoken.startingMoved = true;
3354 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3355 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3356 // want to process the message ASAP, before any other queued
3357 // messages.
3358 mH.sendMessageAtFrontOfQueue(m);
3359 return;
3360 }
3361 }
3362 }
3363
3364 // There is no existing starting window, and the caller doesn't
3365 // want us to create one, so that's it!
3366 if (!createIfNeeded) {
3367 return;
3368 }
Romain Guy06882f82009-06-10 13:36:04 -07003369
Dianne Hackborn284ac932009-08-28 10:34:25 -07003370 // If this is a translucent or wallpaper window, then don't
3371 // show a starting window -- the current effect (a full-screen
3372 // opaque starting window that fades away to the real contents
3373 // when it is ready) does not work for this.
3374 if (theme != 0) {
3375 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3376 com.android.internal.R.styleable.Window);
3377 if (ent.array.getBoolean(
3378 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3379 return;
3380 }
3381 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003382 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3383 return;
3384 }
3385 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003386 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3387 return;
3388 }
3389 }
3390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 mStartingIconInTransition = true;
3392 wtoken.startingData = new StartingData(
3393 pkg, theme, nonLocalizedLabel,
3394 labelRes, icon);
3395 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3396 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3397 // want to process the message ASAP, before any other queued
3398 // messages.
3399 mH.sendMessageAtFrontOfQueue(m);
3400 }
3401 }
3402
3403 public void setAppWillBeHidden(IBinder token) {
3404 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3405 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003406 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 }
3408
3409 AppWindowToken wtoken;
3410
3411 synchronized(mWindowMap) {
3412 wtoken = findAppWindowToken(token);
3413 if (wtoken == null) {
3414 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3415 return;
3416 }
3417 wtoken.willBeHidden = true;
3418 }
3419 }
Romain Guy06882f82009-06-10 13:36:04 -07003420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3422 boolean visible, int transit, boolean performLayout) {
3423 boolean delayed = false;
3424
3425 if (wtoken.clientHidden == visible) {
3426 wtoken.clientHidden = !visible;
3427 wtoken.sendAppVisibilityToClients();
3428 }
Romain Guy06882f82009-06-10 13:36:04 -07003429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 wtoken.willBeHidden = false;
3431 if (wtoken.hidden == visible) {
3432 final int N = wtoken.allAppWindows.size();
3433 boolean changed = false;
3434 if (DEBUG_APP_TRANSITIONS) Log.v(
3435 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3436 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003439
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003440 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003441 if (wtoken.animation == sDummyAnimation) {
3442 wtoken.animation = null;
3443 }
3444 applyAnimationLocked(wtoken, lp, transit, visible);
3445 changed = true;
3446 if (wtoken.animation != null) {
3447 delayed = runningAppAnimation = true;
3448 }
3449 }
Romain Guy06882f82009-06-10 13:36:04 -07003450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 for (int i=0; i<N; i++) {
3452 WindowState win = wtoken.allAppWindows.get(i);
3453 if (win == wtoken.startingWindow) {
3454 continue;
3455 }
3456
3457 if (win.isAnimating()) {
3458 delayed = true;
3459 }
Romain Guy06882f82009-06-10 13:36:04 -07003460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3462 //win.dump(" ");
3463 if (visible) {
3464 if (!win.isVisibleNow()) {
3465 if (!runningAppAnimation) {
3466 applyAnimationLocked(win,
3467 WindowManagerPolicy.TRANSIT_ENTER, true);
3468 }
3469 changed = true;
3470 }
3471 } else if (win.isVisibleNow()) {
3472 if (!runningAppAnimation) {
3473 applyAnimationLocked(win,
3474 WindowManagerPolicy.TRANSIT_EXIT, false);
3475 }
3476 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3477 KeyWaiter.RETURN_NOTHING);
3478 changed = true;
3479 }
3480 }
3481
3482 wtoken.hidden = wtoken.hiddenRequested = !visible;
3483 if (!visible) {
3484 unsetAppFreezingScreenLocked(wtoken, true, true);
3485 } else {
3486 // If we are being set visible, and the starting window is
3487 // not yet displayed, then make sure it doesn't get displayed.
3488 WindowState swin = wtoken.startingWindow;
3489 if (swin != null && (swin.mDrawPending
3490 || swin.mCommitDrawPending)) {
3491 swin.mPolicyVisibility = false;
3492 swin.mPolicyVisibilityAfterAnim = false;
3493 }
3494 }
Romain Guy06882f82009-06-10 13:36:04 -07003495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3497 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3498 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003499
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003500 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003502 if (performLayout) {
3503 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3504 performLayoutAndPlaceSurfacesLocked();
3505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 }
3507 }
3508
3509 if (wtoken.animation != null) {
3510 delayed = true;
3511 }
Romain Guy06882f82009-06-10 13:36:04 -07003512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 return delayed;
3514 }
3515
3516 public void setAppVisibility(IBinder token, boolean visible) {
3517 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3518 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003519 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 }
3521
3522 AppWindowToken wtoken;
3523
3524 synchronized(mWindowMap) {
3525 wtoken = findAppWindowToken(token);
3526 if (wtoken == null) {
3527 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3528 return;
3529 }
3530
3531 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3532 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003533 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3535 + "): mNextAppTransition=" + mNextAppTransition
3536 + " hidden=" + wtoken.hidden
3537 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3538 }
Romain Guy06882f82009-06-10 13:36:04 -07003539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 // If we are preparing an app transition, then delay changing
3541 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003542 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 // Already in requested state, don't do anything more.
3544 if (wtoken.hiddenRequested != visible) {
3545 return;
3546 }
3547 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 if (DEBUG_APP_TRANSITIONS) Log.v(
3550 TAG, "Setting dummy animation on: " + wtoken);
3551 wtoken.setDummyAnimation();
3552 mOpeningApps.remove(wtoken);
3553 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003554 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 wtoken.inPendingTransaction = true;
3556 if (visible) {
3557 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 wtoken.startingDisplayed = false;
3559 wtoken.startingMoved = false;
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003560
3561 // If the token is currently hidden (should be the
3562 // common case), then we need to set up to wait for
3563 // its windows to be ready.
3564 if (wtoken.hidden) {
3565 wtoken.allDrawn = false;
3566 wtoken.waitingToShow = true;
3567
3568 if (wtoken.clientHidden) {
3569 // In the case where we are making an app visible
3570 // but holding off for a transition, we still need
3571 // to tell the client to make its windows visible so
3572 // they get drawn. Otherwise, we will wait on
3573 // performing the transition until all windows have
3574 // been drawn, they never will be, and we are sad.
3575 wtoken.clientHidden = false;
3576 wtoken.sendAppVisibilityToClients();
3577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 }
3579 } else {
3580 mClosingApps.add(wtoken);
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003581
3582 // If the token is currently visible (should be the
3583 // common case), then set up to wait for it to be hidden.
3584 if (!wtoken.hidden) {
3585 wtoken.waitingToHide = true;
3586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 }
3588 return;
3589 }
Romain Guy06882f82009-06-10 13:36:04 -07003590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003592 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 wtoken.updateReportedVisibilityLocked();
3594 Binder.restoreCallingIdentity(origId);
3595 }
3596 }
3597
3598 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3599 boolean unfreezeSurfaceNow, boolean force) {
3600 if (wtoken.freezingScreen) {
3601 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3602 + " force=" + force);
3603 final int N = wtoken.allAppWindows.size();
3604 boolean unfrozeWindows = false;
3605 for (int i=0; i<N; i++) {
3606 WindowState w = wtoken.allAppWindows.get(i);
3607 if (w.mAppFreezing) {
3608 w.mAppFreezing = false;
3609 if (w.mSurface != null && !w.mOrientationChanging) {
3610 w.mOrientationChanging = true;
3611 }
3612 unfrozeWindows = true;
3613 }
3614 }
3615 if (force || unfrozeWindows) {
3616 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3617 wtoken.freezingScreen = false;
3618 mAppsFreezingScreen--;
3619 }
3620 if (unfreezeSurfaceNow) {
3621 if (unfrozeWindows) {
3622 mLayoutNeeded = true;
3623 performLayoutAndPlaceSurfacesLocked();
3624 }
3625 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3626 stopFreezingDisplayLocked();
3627 }
3628 }
3629 }
3630 }
Romain Guy06882f82009-06-10 13:36:04 -07003631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3633 int configChanges) {
3634 if (DEBUG_ORIENTATION) {
3635 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003636 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 Log.i(TAG, "Set freezing of " + wtoken.appToken
3638 + ": hidden=" + wtoken.hidden + " freezing="
3639 + wtoken.freezingScreen, e);
3640 }
3641 if (!wtoken.hiddenRequested) {
3642 if (!wtoken.freezingScreen) {
3643 wtoken.freezingScreen = true;
3644 mAppsFreezingScreen++;
3645 if (mAppsFreezingScreen == 1) {
3646 startFreezingDisplayLocked();
3647 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3648 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3649 5000);
3650 }
3651 }
3652 final int N = wtoken.allAppWindows.size();
3653 for (int i=0; i<N; i++) {
3654 WindowState w = wtoken.allAppWindows.get(i);
3655 w.mAppFreezing = true;
3656 }
3657 }
3658 }
Romain Guy06882f82009-06-10 13:36:04 -07003659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 public void startAppFreezingScreen(IBinder token, int configChanges) {
3661 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3662 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003663 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 }
3665
3666 synchronized(mWindowMap) {
3667 if (configChanges == 0 && !mDisplayFrozen) {
3668 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3669 return;
3670 }
Romain Guy06882f82009-06-10 13:36:04 -07003671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 AppWindowToken wtoken = findAppWindowToken(token);
3673 if (wtoken == null || wtoken.appToken == null) {
3674 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3675 return;
3676 }
3677 final long origId = Binder.clearCallingIdentity();
3678 startAppFreezingScreenLocked(wtoken, configChanges);
3679 Binder.restoreCallingIdentity(origId);
3680 }
3681 }
Romain Guy06882f82009-06-10 13:36:04 -07003682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 public void stopAppFreezingScreen(IBinder token, boolean force) {
3684 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3685 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003686 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 }
3688
3689 synchronized(mWindowMap) {
3690 AppWindowToken wtoken = findAppWindowToken(token);
3691 if (wtoken == null || wtoken.appToken == null) {
3692 return;
3693 }
3694 final long origId = Binder.clearCallingIdentity();
3695 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3696 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3697 unsetAppFreezingScreenLocked(wtoken, true, force);
3698 Binder.restoreCallingIdentity(origId);
3699 }
3700 }
Romain Guy06882f82009-06-10 13:36:04 -07003701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 public void removeAppToken(IBinder token) {
3703 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3704 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003705 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 }
3707
3708 AppWindowToken wtoken = null;
3709 AppWindowToken startingToken = null;
3710 boolean delayed = false;
3711
3712 final long origId = Binder.clearCallingIdentity();
3713 synchronized(mWindowMap) {
3714 WindowToken basewtoken = mTokenMap.remove(token);
3715 mTokenList.remove(basewtoken);
3716 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3717 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003718 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 wtoken.inPendingTransaction = false;
3720 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003721 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 if (mClosingApps.contains(wtoken)) {
3723 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003724 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003726 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 delayed = true;
3728 }
3729 if (DEBUG_APP_TRANSITIONS) Log.v(
3730 TAG, "Removing app " + wtoken + " delayed=" + delayed
3731 + " animation=" + wtoken.animation
3732 + " animating=" + wtoken.animating);
3733 if (delayed) {
3734 // set the token aside because it has an active animation to be finished
3735 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003736 } else {
3737 // Make sure there is no animation running on this token,
3738 // so any windows associated with it will be removed as
3739 // soon as their animations are complete
3740 wtoken.animation = null;
3741 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 }
3743 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003744 if (mLastEnterAnimToken == wtoken) {
3745 mLastEnterAnimToken = null;
3746 mLastEnterAnimParams = null;
3747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 wtoken.removed = true;
3749 if (wtoken.startingData != null) {
3750 startingToken = wtoken;
3751 }
3752 unsetAppFreezingScreenLocked(wtoken, true, true);
3753 if (mFocusedApp == wtoken) {
3754 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3755 mFocusedApp = null;
3756 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3757 mKeyWaiter.tickle();
3758 }
3759 } else {
3760 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3761 }
Romain Guy06882f82009-06-10 13:36:04 -07003762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 if (!delayed && wtoken != null) {
3764 wtoken.updateReportedVisibilityLocked();
3765 }
3766 }
3767 Binder.restoreCallingIdentity(origId);
3768
3769 if (startingToken != null) {
3770 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3771 + startingToken + ": app token removed");
3772 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3773 mH.sendMessage(m);
3774 }
3775 }
3776
3777 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3778 final int NW = token.windows.size();
3779 for (int i=0; i<NW; i++) {
3780 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003781 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 mWindows.remove(win);
3783 int j = win.mChildWindows.size();
3784 while (j > 0) {
3785 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003786 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3787 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3788 "Tmp removing child window " + cwin);
3789 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 }
3791 }
3792 return NW > 0;
3793 }
3794
3795 void dumpAppTokensLocked() {
3796 for (int i=mAppTokens.size()-1; i>=0; i--) {
3797 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3798 }
3799 }
Romain Guy06882f82009-06-10 13:36:04 -07003800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 void dumpWindowsLocked() {
3802 for (int i=mWindows.size()-1; i>=0; i--) {
3803 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3804 }
3805 }
Romain Guy06882f82009-06-10 13:36:04 -07003806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 private int findWindowOffsetLocked(int tokenPos) {
3808 final int NW = mWindows.size();
3809
3810 if (tokenPos >= mAppTokens.size()) {
3811 int i = NW;
3812 while (i > 0) {
3813 i--;
3814 WindowState win = (WindowState)mWindows.get(i);
3815 if (win.getAppToken() != null) {
3816 return i+1;
3817 }
3818 }
3819 }
3820
3821 while (tokenPos > 0) {
3822 // Find the first app token below the new position that has
3823 // a window displayed.
3824 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3825 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3826 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003827 if (wtoken.sendingToBottom) {
3828 if (DEBUG_REORDER) Log.v(TAG,
3829 "Skipping token -- currently sending to bottom");
3830 tokenPos--;
3831 continue;
3832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 int i = wtoken.windows.size();
3834 while (i > 0) {
3835 i--;
3836 WindowState win = wtoken.windows.get(i);
3837 int j = win.mChildWindows.size();
3838 while (j > 0) {
3839 j--;
3840 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003841 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 for (int pos=NW-1; pos>=0; pos--) {
3843 if (mWindows.get(pos) == cwin) {
3844 if (DEBUG_REORDER) Log.v(TAG,
3845 "Found child win @" + (pos+1));
3846 return pos+1;
3847 }
3848 }
3849 }
3850 }
3851 for (int pos=NW-1; pos>=0; pos--) {
3852 if (mWindows.get(pos) == win) {
3853 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3854 return pos+1;
3855 }
3856 }
3857 }
3858 tokenPos--;
3859 }
3860
3861 return 0;
3862 }
3863
3864 private final int reAddWindowLocked(int index, WindowState win) {
3865 final int NCW = win.mChildWindows.size();
3866 boolean added = false;
3867 for (int j=0; j<NCW; j++) {
3868 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3869 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003870 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3871 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 mWindows.add(index, win);
3873 index++;
3874 added = true;
3875 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003876 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3877 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 mWindows.add(index, cwin);
3879 index++;
3880 }
3881 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003882 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3883 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 mWindows.add(index, win);
3885 index++;
3886 }
3887 return index;
3888 }
Romain Guy06882f82009-06-10 13:36:04 -07003889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3891 final int NW = token.windows.size();
3892 for (int i=0; i<NW; i++) {
3893 index = reAddWindowLocked(index, token.windows.get(i));
3894 }
3895 return index;
3896 }
3897
3898 public void moveAppToken(int index, IBinder token) {
3899 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3900 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003901 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 }
3903
3904 synchronized(mWindowMap) {
3905 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3906 if (DEBUG_REORDER) dumpAppTokensLocked();
3907 final AppWindowToken wtoken = findAppWindowToken(token);
3908 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3909 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3910 + token + " (" + wtoken + ")");
3911 return;
3912 }
3913 mAppTokens.add(index, wtoken);
3914 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3915 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 final long origId = Binder.clearCallingIdentity();
3918 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3919 if (DEBUG_REORDER) dumpWindowsLocked();
3920 if (tmpRemoveAppWindowsLocked(wtoken)) {
3921 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3922 if (DEBUG_REORDER) dumpWindowsLocked();
3923 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3924 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3925 if (DEBUG_REORDER) dumpWindowsLocked();
3926 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 mLayoutNeeded = true;
3928 performLayoutAndPlaceSurfacesLocked();
3929 }
3930 Binder.restoreCallingIdentity(origId);
3931 }
3932 }
3933
3934 private void removeAppTokensLocked(List<IBinder> tokens) {
3935 // XXX This should be done more efficiently!
3936 // (take advantage of the fact that both lists should be
3937 // ordered in the same way.)
3938 int N = tokens.size();
3939 for (int i=0; i<N; i++) {
3940 IBinder token = tokens.get(i);
3941 final AppWindowToken wtoken = findAppWindowToken(token);
3942 if (!mAppTokens.remove(wtoken)) {
3943 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3944 + token + " (" + wtoken + ")");
3945 i--;
3946 N--;
3947 }
3948 }
3949 }
3950
Dianne Hackborna8f60182009-09-01 19:01:50 -07003951 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3952 boolean updateFocusAndLayout) {
3953 // First remove all of the windows from the list.
3954 tmpRemoveAppWindowsLocked(wtoken);
3955
3956 // Where to start adding?
3957 int pos = findWindowOffsetLocked(tokenPos);
3958
3959 // And now add them back at the correct place.
3960 pos = reAddAppWindowsLocked(pos, wtoken);
3961
3962 if (updateFocusAndLayout) {
3963 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3964 assignLayersLocked();
3965 }
3966 mLayoutNeeded = true;
3967 performLayoutAndPlaceSurfacesLocked();
3968 }
3969 }
3970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3972 // First remove all of the windows from the list.
3973 final int N = tokens.size();
3974 int i;
3975 for (i=0; i<N; i++) {
3976 WindowToken token = mTokenMap.get(tokens.get(i));
3977 if (token != null) {
3978 tmpRemoveAppWindowsLocked(token);
3979 }
3980 }
3981
3982 // Where to start adding?
3983 int pos = findWindowOffsetLocked(tokenPos);
3984
3985 // And now add them back at the correct place.
3986 for (i=0; i<N; i++) {
3987 WindowToken token = mTokenMap.get(tokens.get(i));
3988 if (token != null) {
3989 pos = reAddAppWindowsLocked(pos, token);
3990 }
3991 }
3992
Dianne Hackborna8f60182009-09-01 19:01:50 -07003993 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3994 assignLayersLocked();
3995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 mLayoutNeeded = true;
3997 performLayoutAndPlaceSurfacesLocked();
3998
3999 //dump();
4000 }
4001
4002 public void moveAppTokensToTop(List<IBinder> tokens) {
4003 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4004 "moveAppTokensToTop()")) {
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 final long origId = Binder.clearCallingIdentity();
4009 synchronized(mWindowMap) {
4010 removeAppTokensLocked(tokens);
4011 final int N = tokens.size();
4012 for (int i=0; i<N; i++) {
4013 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4014 if (wt != null) {
4015 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004016 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004017 mToTopApps.remove(wt);
4018 mToBottomApps.remove(wt);
4019 mToTopApps.add(wt);
4020 wt.sendingToBottom = false;
4021 wt.sendingToTop = true;
4022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 }
4024 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07004025
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004026 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004027 moveAppWindowsLocked(tokens, mAppTokens.size());
4028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 }
4030 Binder.restoreCallingIdentity(origId);
4031 }
4032
4033 public void moveAppTokensToBottom(List<IBinder> tokens) {
4034 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4035 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004036 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004037 }
4038
4039 final long origId = Binder.clearCallingIdentity();
4040 synchronized(mWindowMap) {
4041 removeAppTokensLocked(tokens);
4042 final int N = tokens.size();
4043 int pos = 0;
4044 for (int i=0; i<N; i++) {
4045 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4046 if (wt != null) {
4047 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004048 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004049 mToTopApps.remove(wt);
4050 mToBottomApps.remove(wt);
4051 mToBottomApps.add(i, wt);
4052 wt.sendingToTop = false;
4053 wt.sendingToBottom = true;
4054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 pos++;
4056 }
4057 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07004058
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004059 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004060 moveAppWindowsLocked(tokens, 0);
4061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 }
4063 Binder.restoreCallingIdentity(origId);
4064 }
4065
4066 // -------------------------------------------------------------
4067 // Misc IWindowSession methods
4068 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004071 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 != PackageManager.PERMISSION_GRANTED) {
4073 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4074 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004075 synchronized (mKeyguardTokenWatcher) {
4076 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 }
4079
4080 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004081 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 != PackageManager.PERMISSION_GRANTED) {
4083 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4084 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004085 synchronized (mKeyguardTokenWatcher) {
4086 mKeyguardTokenWatcher.release(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087
Mike Lockwood983ee092009-11-22 01:42:24 -05004088 if (!mKeyguardTokenWatcher.isAcquired()) {
4089 // If we are the last one to reenable the keyguard wait until
4090 // we have actaully finished reenabling until returning.
4091 // It is possible that reenableKeyguard() can be called before
4092 // the previous disableKeyguard() is handled, in which case
4093 // neither mKeyguardTokenWatcher.acquired() or released() would
4094 // be called. In that case mKeyguardDisabled will be false here
4095 // and we have nothing to wait for.
4096 while (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 try {
Mike Lockwood983ee092009-11-22 01:42:24 -05004098 mKeyguardTokenWatcher.wait();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 } catch (InterruptedException e) {
4100 Thread.currentThread().interrupt();
4101 }
4102 }
4103 }
4104 }
4105 }
4106
4107 /**
4108 * @see android.app.KeyguardManager#exitKeyguardSecurely
4109 */
4110 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004111 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 != PackageManager.PERMISSION_GRANTED) {
4113 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4114 }
4115 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4116 public void onKeyguardExitResult(boolean success) {
4117 try {
4118 callback.onKeyguardExitResult(success);
4119 } catch (RemoteException e) {
4120 // Client has died, we don't care.
4121 }
4122 }
4123 });
4124 }
4125
4126 public boolean inKeyguardRestrictedInputMode() {
4127 return mPolicy.inKeyguardRestrictedKeyInputMode();
4128 }
Romain Guy06882f82009-06-10 13:36:04 -07004129
Dianne Hackbornffa42482009-09-23 22:20:11 -07004130 public void closeSystemDialogs(String reason) {
4131 synchronized(mWindowMap) {
4132 for (int i=mWindows.size()-1; i>=0; i--) {
4133 WindowState w = (WindowState)mWindows.get(i);
4134 if (w.mSurface != null) {
4135 try {
4136 w.mClient.closeSystemDialogs(reason);
4137 } catch (RemoteException e) {
4138 }
4139 }
4140 }
4141 }
4142 }
4143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 static float fixScale(float scale) {
4145 if (scale < 0) scale = 0;
4146 else if (scale > 20) scale = 20;
4147 return Math.abs(scale);
4148 }
Romain Guy06882f82009-06-10 13:36:04 -07004149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 public void setAnimationScale(int which, float scale) {
4151 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4152 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004153 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 }
4155
4156 if (scale < 0) scale = 0;
4157 else if (scale > 20) scale = 20;
4158 scale = Math.abs(scale);
4159 switch (which) {
4160 case 0: mWindowAnimationScale = fixScale(scale); break;
4161 case 1: mTransitionAnimationScale = fixScale(scale); break;
4162 }
Romain Guy06882f82009-06-10 13:36:04 -07004163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 // Persist setting
4165 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4166 }
Romain Guy06882f82009-06-10 13:36:04 -07004167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 public void setAnimationScales(float[] scales) {
4169 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4170 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004171 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 }
4173
4174 if (scales != null) {
4175 if (scales.length >= 1) {
4176 mWindowAnimationScale = fixScale(scales[0]);
4177 }
4178 if (scales.length >= 2) {
4179 mTransitionAnimationScale = fixScale(scales[1]);
4180 }
4181 }
Romain Guy06882f82009-06-10 13:36:04 -07004182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183 // Persist setting
4184 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4185 }
Romain Guy06882f82009-06-10 13:36:04 -07004186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 public float getAnimationScale(int which) {
4188 switch (which) {
4189 case 0: return mWindowAnimationScale;
4190 case 1: return mTransitionAnimationScale;
4191 }
4192 return 0;
4193 }
Romain Guy06882f82009-06-10 13:36:04 -07004194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 public float[] getAnimationScales() {
4196 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4197 }
Romain Guy06882f82009-06-10 13:36:04 -07004198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 public int getSwitchState(int sw) {
4200 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4201 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004202 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203 }
4204 return KeyInputQueue.getSwitchState(sw);
4205 }
Romain Guy06882f82009-06-10 13:36:04 -07004206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 public int getSwitchStateForDevice(int devid, int sw) {
4208 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4209 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004210 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 }
4212 return KeyInputQueue.getSwitchState(devid, sw);
4213 }
Romain Guy06882f82009-06-10 13:36:04 -07004214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215 public int getScancodeState(int sw) {
4216 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4217 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004218 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004220 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 }
Romain Guy06882f82009-06-10 13:36:04 -07004222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 public int getScancodeStateForDevice(int devid, int sw) {
4224 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4225 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004226 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004228 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 }
Romain Guy06882f82009-06-10 13:36:04 -07004230
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004231 public int getTrackballScancodeState(int sw) {
4232 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4233 "getTrackballScancodeState()")) {
4234 throw new SecurityException("Requires READ_INPUT_STATE permission");
4235 }
4236 return mQueue.getTrackballScancodeState(sw);
4237 }
4238
4239 public int getDPadScancodeState(int sw) {
4240 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4241 "getDPadScancodeState()")) {
4242 throw new SecurityException("Requires READ_INPUT_STATE permission");
4243 }
4244 return mQueue.getDPadScancodeState(sw);
4245 }
4246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 public int getKeycodeState(int sw) {
4248 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4249 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004250 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004252 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 }
Romain Guy06882f82009-06-10 13:36:04 -07004254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 public int getKeycodeStateForDevice(int devid, int sw) {
4256 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4257 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004258 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004260 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 }
Romain Guy06882f82009-06-10 13:36:04 -07004262
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004263 public int getTrackballKeycodeState(int sw) {
4264 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4265 "getTrackballKeycodeState()")) {
4266 throw new SecurityException("Requires READ_INPUT_STATE permission");
4267 }
4268 return mQueue.getTrackballKeycodeState(sw);
4269 }
4270
4271 public int getDPadKeycodeState(int sw) {
4272 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4273 "getDPadKeycodeState()")) {
4274 throw new SecurityException("Requires READ_INPUT_STATE permission");
4275 }
4276 return mQueue.getDPadKeycodeState(sw);
4277 }
4278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4280 return KeyInputQueue.hasKeys(keycodes, keyExists);
4281 }
Romain Guy06882f82009-06-10 13:36:04 -07004282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 public void enableScreenAfterBoot() {
4284 synchronized(mWindowMap) {
4285 if (mSystemBooted) {
4286 return;
4287 }
4288 mSystemBooted = true;
4289 }
Romain Guy06882f82009-06-10 13:36:04 -07004290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 performEnableScreen();
4292 }
Romain Guy06882f82009-06-10 13:36:04 -07004293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 public void enableScreenIfNeededLocked() {
4295 if (mDisplayEnabled) {
4296 return;
4297 }
4298 if (!mSystemBooted) {
4299 return;
4300 }
4301 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4302 }
Romain Guy06882f82009-06-10 13:36:04 -07004303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304 public void performEnableScreen() {
4305 synchronized(mWindowMap) {
4306 if (mDisplayEnabled) {
4307 return;
4308 }
4309 if (!mSystemBooted) {
4310 return;
4311 }
Romain Guy06882f82009-06-10 13:36:04 -07004312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313 // Don't enable the screen until all existing windows
4314 // have been drawn.
4315 final int N = mWindows.size();
4316 for (int i=0; i<N; i++) {
4317 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08004318 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 return;
4320 }
4321 }
Romain Guy06882f82009-06-10 13:36:04 -07004322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 mDisplayEnabled = true;
4324 if (false) {
4325 Log.i(TAG, "ENABLING SCREEN!");
4326 StringWriter sw = new StringWriter();
4327 PrintWriter pw = new PrintWriter(sw);
4328 this.dump(null, pw, null);
4329 Log.i(TAG, sw.toString());
4330 }
4331 try {
4332 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4333 if (surfaceFlinger != null) {
4334 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4335 Parcel data = Parcel.obtain();
4336 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4337 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4338 data, null, 0);
4339 data.recycle();
4340 }
4341 } catch (RemoteException ex) {
4342 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4343 }
4344 }
Romain Guy06882f82009-06-10 13:36:04 -07004345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004349 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4350 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 }
Romain Guy06882f82009-06-10 13:36:04 -07004352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004353 public void setInTouchMode(boolean mode) {
4354 synchronized(mWindowMap) {
4355 mInTouchMode = mode;
4356 }
4357 }
4358
Romain Guy06882f82009-06-10 13:36:04 -07004359 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004360 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004362 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004363 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
4365
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004366 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 }
Romain Guy06882f82009-06-10 13:36:04 -07004368
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004369 public void setRotationUnchecked(int rotation,
4370 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 if(DEBUG_ORIENTATION) Log.v(TAG,
4372 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 long origId = Binder.clearCallingIdentity();
4375 boolean changed;
4376 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004377 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 }
Romain Guy06882f82009-06-10 13:36:04 -07004379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 if (changed) {
4381 sendNewConfiguration();
4382 synchronized(mWindowMap) {
4383 mLayoutNeeded = true;
4384 performLayoutAndPlaceSurfacesLocked();
4385 }
4386 } else if (alwaysSendConfiguration) {
4387 //update configuration ignoring orientation change
4388 sendNewConfiguration();
4389 }
Romain Guy06882f82009-06-10 13:36:04 -07004390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 Binder.restoreCallingIdentity(origId);
4392 }
Romain Guy06882f82009-06-10 13:36:04 -07004393
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004394 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 boolean changed;
4396 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4397 rotation = mRequestedRotation;
4398 } else {
4399 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004400 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 }
4402 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004403 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 mRotation, mDisplayEnabled);
4405 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4406 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004409 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 "Rotation changed to " + rotation
4411 + " from " + mRotation
4412 + " (forceApp=" + mForcedAppOrientation
4413 + ", req=" + mRequestedRotation + ")");
4414 mRotation = rotation;
4415 mWindowsFreezingScreen = true;
4416 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4417 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4418 2000);
4419 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004420 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 mQueue.setOrientation(rotation);
4422 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004423 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 }
4425 for (int i=mWindows.size()-1; i>=0; i--) {
4426 WindowState w = (WindowState)mWindows.get(i);
4427 if (w.mSurface != null) {
4428 w.mOrientationChanging = true;
4429 }
4430 }
4431 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4432 try {
4433 mRotationWatchers.get(i).onRotationChanged(rotation);
4434 } catch (RemoteException e) {
4435 }
4436 }
4437 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 return changed;
4440 }
Romain Guy06882f82009-06-10 13:36:04 -07004441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004442 public int getRotation() {
4443 return mRotation;
4444 }
4445
4446 public int watchRotation(IRotationWatcher watcher) {
4447 final IBinder watcherBinder = watcher.asBinder();
4448 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4449 public void binderDied() {
4450 synchronized (mWindowMap) {
4451 for (int i=0; i<mRotationWatchers.size(); i++) {
4452 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004453 IRotationWatcher removed = mRotationWatchers.remove(i);
4454 if (removed != null) {
4455 removed.asBinder().unlinkToDeath(this, 0);
4456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 i--;
4458 }
4459 }
4460 }
4461 }
4462 };
Romain Guy06882f82009-06-10 13:36:04 -07004463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 synchronized (mWindowMap) {
4465 try {
4466 watcher.asBinder().linkToDeath(dr, 0);
4467 mRotationWatchers.add(watcher);
4468 } catch (RemoteException e) {
4469 // Client died, no cleanup needed.
4470 }
Romain Guy06882f82009-06-10 13:36:04 -07004471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 return mRotation;
4473 }
4474 }
4475
4476 /**
4477 * Starts the view server on the specified port.
4478 *
4479 * @param port The port to listener to.
4480 *
4481 * @return True if the server was successfully started, false otherwise.
4482 *
4483 * @see com.android.server.ViewServer
4484 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4485 */
4486 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004487 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004488 return false;
4489 }
4490
4491 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4492 return false;
4493 }
4494
4495 if (port < 1024) {
4496 return false;
4497 }
4498
4499 if (mViewServer != null) {
4500 if (!mViewServer.isRunning()) {
4501 try {
4502 return mViewServer.start();
4503 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004504 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 }
4506 }
4507 return false;
4508 }
4509
4510 try {
4511 mViewServer = new ViewServer(this, port);
4512 return mViewServer.start();
4513 } catch (IOException e) {
4514 Log.w(TAG, "View server did not start");
4515 }
4516 return false;
4517 }
4518
Romain Guy06882f82009-06-10 13:36:04 -07004519 private boolean isSystemSecure() {
4520 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4521 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4522 }
4523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 /**
4525 * Stops the view server if it exists.
4526 *
4527 * @return True if the server stopped, false if it wasn't started or
4528 * couldn't be stopped.
4529 *
4530 * @see com.android.server.ViewServer
4531 */
4532 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004533 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 return false;
4535 }
4536
4537 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4538 return false;
4539 }
4540
4541 if (mViewServer != null) {
4542 return mViewServer.stop();
4543 }
4544 return false;
4545 }
4546
4547 /**
4548 * Indicates whether the view server is running.
4549 *
4550 * @return True if the server is running, false otherwise.
4551 *
4552 * @see com.android.server.ViewServer
4553 */
4554 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004555 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 return false;
4557 }
4558
4559 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4560 return false;
4561 }
4562
4563 return mViewServer != null && mViewServer.isRunning();
4564 }
4565
4566 /**
4567 * Lists all availble windows in the system. The listing is written in the
4568 * specified Socket's output stream with the following syntax:
4569 * windowHashCodeInHexadecimal windowName
4570 * Each line of the ouput represents a different window.
4571 *
4572 * @param client The remote client to send the listing to.
4573 * @return False if an error occured, true otherwise.
4574 */
4575 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004576 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 return false;
4578 }
4579
4580 boolean result = true;
4581
4582 Object[] windows;
4583 synchronized (mWindowMap) {
4584 windows = new Object[mWindows.size()];
4585 //noinspection unchecked
4586 windows = mWindows.toArray(windows);
4587 }
4588
4589 BufferedWriter out = null;
4590
4591 // Any uncaught exception will crash the system process
4592 try {
4593 OutputStream clientStream = client.getOutputStream();
4594 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4595
4596 final int count = windows.length;
4597 for (int i = 0; i < count; i++) {
4598 final WindowState w = (WindowState) windows[i];
4599 out.write(Integer.toHexString(System.identityHashCode(w)));
4600 out.write(' ');
4601 out.append(w.mAttrs.getTitle());
4602 out.write('\n');
4603 }
4604
4605 out.write("DONE.\n");
4606 out.flush();
4607 } catch (Exception e) {
4608 result = false;
4609 } finally {
4610 if (out != null) {
4611 try {
4612 out.close();
4613 } catch (IOException e) {
4614 result = false;
4615 }
4616 }
4617 }
4618
4619 return result;
4620 }
4621
4622 /**
4623 * Sends a command to a target window. The result of the command, if any, will be
4624 * written in the output stream of the specified socket.
4625 *
4626 * The parameters must follow this syntax:
4627 * windowHashcode extra
4628 *
4629 * Where XX is the length in characeters of the windowTitle.
4630 *
4631 * The first parameter is the target window. The window with the specified hashcode
4632 * will be the target. If no target can be found, nothing happens. The extra parameters
4633 * will be delivered to the target window and as parameters to the command itself.
4634 *
4635 * @param client The remote client to sent the result, if any, to.
4636 * @param command The command to execute.
4637 * @param parameters The command parameters.
4638 *
4639 * @return True if the command was successfully delivered, false otherwise. This does
4640 * not indicate whether the command itself was successful.
4641 */
4642 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004643 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 return false;
4645 }
4646
4647 boolean success = true;
4648 Parcel data = null;
4649 Parcel reply = null;
4650
4651 // Any uncaught exception will crash the system process
4652 try {
4653 // Find the hashcode of the window
4654 int index = parameters.indexOf(' ');
4655 if (index == -1) {
4656 index = parameters.length();
4657 }
4658 final String code = parameters.substring(0, index);
4659 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4660
4661 // Extract the command's parameter after the window description
4662 if (index < parameters.length()) {
4663 parameters = parameters.substring(index + 1);
4664 } else {
4665 parameters = "";
4666 }
4667
4668 final WindowManagerService.WindowState window = findWindow(hashCode);
4669 if (window == null) {
4670 return false;
4671 }
4672
4673 data = Parcel.obtain();
4674 data.writeInterfaceToken("android.view.IWindow");
4675 data.writeString(command);
4676 data.writeString(parameters);
4677 data.writeInt(1);
4678 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4679
4680 reply = Parcel.obtain();
4681
4682 final IBinder binder = window.mClient.asBinder();
4683 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4684 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4685
4686 reply.readException();
4687
4688 } catch (Exception e) {
4689 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4690 success = false;
4691 } finally {
4692 if (data != null) {
4693 data.recycle();
4694 }
4695 if (reply != null) {
4696 reply.recycle();
4697 }
4698 }
4699
4700 return success;
4701 }
4702
4703 private WindowState findWindow(int hashCode) {
4704 if (hashCode == -1) {
4705 return getFocusedWindow();
4706 }
4707
4708 synchronized (mWindowMap) {
4709 final ArrayList windows = mWindows;
4710 final int count = windows.size();
4711
4712 for (int i = 0; i < count; i++) {
4713 WindowState w = (WindowState) windows.get(i);
4714 if (System.identityHashCode(w) == hashCode) {
4715 return w;
4716 }
4717 }
4718 }
4719
4720 return null;
4721 }
4722
4723 /*
4724 * Instruct the Activity Manager to fetch the current configuration and broadcast
4725 * that to config-changed listeners if appropriate.
4726 */
4727 void sendNewConfiguration() {
4728 try {
4729 mActivityManager.updateConfiguration(null);
4730 } catch (RemoteException e) {
4731 }
4732 }
Romain Guy06882f82009-06-10 13:36:04 -07004733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 public Configuration computeNewConfiguration() {
4735 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004736 return computeNewConfigurationLocked();
4737 }
4738 }
Romain Guy06882f82009-06-10 13:36:04 -07004739
Dianne Hackbornc485a602009-03-24 22:39:49 -07004740 Configuration computeNewConfigurationLocked() {
4741 Configuration config = new Configuration();
4742 if (!computeNewConfigurationLocked(config)) {
4743 return null;
4744 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004745 return config;
4746 }
Romain Guy06882f82009-06-10 13:36:04 -07004747
Dianne Hackbornc485a602009-03-24 22:39:49 -07004748 boolean computeNewConfigurationLocked(Configuration config) {
4749 if (mDisplay == null) {
4750 return false;
4751 }
4752 mQueue.getInputConfiguration(config);
4753 final int dw = mDisplay.getWidth();
4754 final int dh = mDisplay.getHeight();
4755 int orientation = Configuration.ORIENTATION_SQUARE;
4756 if (dw < dh) {
4757 orientation = Configuration.ORIENTATION_PORTRAIT;
4758 } else if (dw > dh) {
4759 orientation = Configuration.ORIENTATION_LANDSCAPE;
4760 }
4761 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004762
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004763 DisplayMetrics dm = new DisplayMetrics();
4764 mDisplay.getMetrics(dm);
4765 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4766
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004767 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004768 // Note we only do this once because at this point we don't
4769 // expect the screen to change in this way at runtime, and want
4770 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004771 int longSize = dw;
4772 int shortSize = dh;
4773 if (longSize < shortSize) {
4774 int tmp = longSize;
4775 longSize = shortSize;
4776 shortSize = tmp;
4777 }
4778 longSize = (int)(longSize/dm.density);
4779 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004780
Dianne Hackborn723738c2009-06-25 19:48:04 -07004781 // These semi-magic numbers define our compatibility modes for
4782 // applications with different screens. Don't change unless you
4783 // make sure to test lots and lots of apps!
4784 if (longSize < 470) {
4785 // This is shorter than an HVGA normal density screen (which
4786 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004787 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4788 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004789 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004790 // Is this a large screen?
4791 if (longSize > 640 && shortSize >= 480) {
4792 // VGA or larger screens at medium density are the point
4793 // at which we consider it to be a large screen.
4794 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4795 } else {
4796 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4797
4798 // If this screen is wider than normal HVGA, or taller
4799 // than FWVGA, then for old apps we want to run in size
4800 // compatibility mode.
4801 if (shortSize > 321 || longSize > 570) {
4802 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4803 }
4804 }
4805
4806 // Is this a long screen?
4807 if (((longSize*3)/5) >= (shortSize-1)) {
4808 // Anything wider than WVGA (5:3) is considering to be long.
4809 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4810 } else {
4811 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4812 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004813 }
4814 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004815 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004816
Dianne Hackbornc485a602009-03-24 22:39:49 -07004817 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4818 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4819 mPolicy.adjustConfigurationLw(config);
4820 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 }
Romain Guy06882f82009-06-10 13:36:04 -07004822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 // -------------------------------------------------------------
4824 // Input Events and Focus Management
4825 // -------------------------------------------------------------
4826
4827 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004828 long curTime = SystemClock.uptimeMillis();
4829
Michael Chane10de972009-05-18 11:24:50 -07004830 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004831 if (mLastTouchEventType == eventType &&
4832 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4833 return;
4834 }
4835 mLastUserActivityCallTime = curTime;
4836 mLastTouchEventType = eventType;
4837 }
4838
4839 if (targetWin == null
4840 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4841 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004842 }
4843 }
4844
4845 // tells if it's a cheek event or not -- this function is stateful
4846 private static final int EVENT_NONE = 0;
4847 private static final int EVENT_UNKNOWN = 0;
4848 private static final int EVENT_CHEEK = 0;
4849 private static final int EVENT_IGNORE_DURATION = 300; // ms
4850 private static final float CHEEK_THRESHOLD = 0.6f;
4851 private int mEventState = EVENT_NONE;
4852 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004854 private int eventType(MotionEvent ev) {
4855 float size = ev.getSize();
4856 switch (ev.getAction()) {
4857 case MotionEvent.ACTION_DOWN:
4858 mEventSize = size;
4859 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4860 case MotionEvent.ACTION_UP:
4861 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004862 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004863 case MotionEvent.ACTION_MOVE:
4864 final int N = ev.getHistorySize();
4865 if (size > mEventSize) mEventSize = size;
4866 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4867 for (int i=0; i<N; i++) {
4868 size = ev.getHistoricalSize(i);
4869 if (size > mEventSize) mEventSize = size;
4870 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4871 }
4872 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4873 return TOUCH_EVENT;
4874 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004875 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 }
4877 default:
4878 // not good
4879 return OTHER_EVENT;
4880 }
4881 }
4882
4883 /**
4884 * @return Returns true if event was dispatched, false if it was dropped for any reason
4885 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004886 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4888 "dispatchPointer " + ev);
4889
Michael Chan53071d62009-05-13 17:29:48 -07004890 if (MEASURE_LATENCY) {
4891 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4892 }
4893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004895 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896
Michael Chan53071d62009-05-13 17:29:48 -07004897 if (MEASURE_LATENCY) {
4898 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4899 }
4900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903 if (action == MotionEvent.ACTION_UP) {
4904 // let go of our target
4905 mKeyWaiter.mMotionTarget = null;
4906 mPowerManager.logPointerUpEvent();
4907 } else if (action == MotionEvent.ACTION_DOWN) {
4908 mPowerManager.logPointerDownEvent();
4909 }
4910
4911 if (targetObj == null) {
4912 // In this case we are either dropping the event, or have received
4913 // a move or up without a down. It is common to receive move
4914 // events in such a way, since this means the user is moving the
4915 // pointer without actually pressing down. All other cases should
4916 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004917 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004918 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4919 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08004920 synchronized (mWindowMap) {
4921 if (mSendingPointersToWallpaper) {
4922 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4923 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4924 }
4925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 if (qev != null) {
4927 mQueue.recycleEvent(qev);
4928 }
4929 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004930 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004931 }
4932 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004933 synchronized (mWindowMap) {
4934 if (mSendingPointersToWallpaper) {
4935 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4936 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4937 }
4938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 if (qev != null) {
4940 mQueue.recycleEvent(qev);
4941 }
4942 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004943 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 }
Romain Guy06882f82009-06-10 13:36:04 -07004945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004946 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004948 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004949 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004950
4951 //Log.i(TAG, "Sending " + ev + " to " + target);
4952
4953 if (uid != 0 && uid != target.mSession.mUid) {
4954 if (mContext.checkPermission(
4955 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4956 != PackageManager.PERMISSION_GRANTED) {
4957 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4958 + pid + " uid " + uid + " to window " + target
4959 + " owned by uid " + target.mSession.mUid);
4960 if (qev != null) {
4961 mQueue.recycleEvent(qev);
4962 }
4963 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004964 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 }
4966 }
4967
Michael Chan53071d62009-05-13 17:29:48 -07004968 if (MEASURE_LATENCY) {
4969 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4970 }
4971
Romain Guy06882f82009-06-10 13:36:04 -07004972 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4974 //target wants to ignore fat touch events
4975 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4976 //explicit flag to return without processing event further
4977 boolean returnFlag = false;
4978 if((action == MotionEvent.ACTION_DOWN)) {
4979 mFatTouch = false;
4980 if(cheekPress) {
4981 mFatTouch = true;
4982 returnFlag = true;
4983 }
4984 } else {
4985 if(action == MotionEvent.ACTION_UP) {
4986 if(mFatTouch) {
4987 //earlier even was invalid doesnt matter if current up is cheekpress or not
4988 mFatTouch = false;
4989 returnFlag = true;
4990 } else if(cheekPress) {
4991 //cancel the earlier event
4992 ev.setAction(MotionEvent.ACTION_CANCEL);
4993 action = MotionEvent.ACTION_CANCEL;
4994 }
4995 } else if(action == MotionEvent.ACTION_MOVE) {
4996 if(mFatTouch) {
4997 //two cases here
4998 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004999 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 returnFlag = true;
5001 } else if(cheekPress) {
5002 //valid down followed by invalid moves
5003 //an invalid move have to cancel earlier action
5004 ev.setAction(MotionEvent.ACTION_CANCEL);
5005 action = MotionEvent.ACTION_CANCEL;
5006 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
5007 //note that the subsequent invalid moves will not get here
5008 mFatTouch = true;
5009 }
5010 }
5011 } //else if action
5012 if(returnFlag) {
5013 //recycle que, ev
5014 if (qev != null) {
5015 mQueue.recycleEvent(qev);
5016 }
5017 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005018 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005019 }
5020 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005021
Michael Chan9f028e62009-08-04 17:37:46 -07005022 // Enable this for testing the "right" value
5023 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005024 int max_events_per_sec = 35;
5025 try {
5026 max_events_per_sec = Integer.parseInt(SystemProperties
5027 .get("windowsmgr.max_events_per_sec"));
5028 if (max_events_per_sec < 1) {
5029 max_events_per_sec = 35;
5030 }
5031 } catch (NumberFormatException e) {
5032 }
5033 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5034 }
5035
5036 /*
5037 * Throttle events to minimize CPU usage when there's a flood of events
5038 * e.g. constant contact with the screen
5039 */
5040 if (action == MotionEvent.ACTION_MOVE) {
5041 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5042 long now = SystemClock.uptimeMillis();
5043 if (now < nextEventTime) {
5044 try {
5045 Thread.sleep(nextEventTime - now);
5046 } catch (InterruptedException e) {
5047 }
5048 mLastTouchEventTime = nextEventTime;
5049 } else {
5050 mLastTouchEventTime = now;
5051 }
5052 }
5053
Michael Chan53071d62009-05-13 17:29:48 -07005054 if (MEASURE_LATENCY) {
5055 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5056 }
5057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005059 if (!target.isVisibleLw()) {
5060 // During this motion dispatch, the target window has become
5061 // invisible.
5062 if (mSendingPointersToWallpaper) {
5063 sendPointerToWallpaperLocked(null, ev, eventTime);
5064 }
5065 if (qev != null) {
5066 mQueue.recycleEvent(qev);
5067 }
5068 ev.recycle();
5069 return INJECT_SUCCEEDED;
5070 }
5071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5073 mKeyWaiter.bindTargetWindowLocked(target,
5074 KeyWaiter.RETURN_PENDING_POINTER, qev);
5075 ev = null;
5076 } else {
5077 if (action == MotionEvent.ACTION_DOWN) {
5078 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5079 if (out != null) {
5080 MotionEvent oev = MotionEvent.obtain(ev);
5081 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5082 do {
5083 final Rect frame = out.mFrame;
5084 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5085 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005086 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 } catch (android.os.RemoteException e) {
5088 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
5089 }
5090 oev.offsetLocation((float)frame.left, (float)frame.top);
5091 out = out.mNextOutsideTouch;
5092 } while (out != null);
5093 mKeyWaiter.mOutsideTouchTargets = null;
5094 }
5095 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005096
5097 // If we are on top of the wallpaper, then the wallpaper also
5098 // gets to see this movement.
Dianne Hackborn9b52a212009-12-11 14:51:35 -08005099 if ((mWallpaperTarget == target &&
5100 target.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD)
5101 || mSendingPointersToWallpaper) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005102 sendPointerToWallpaperLocked(null, ev, eventTime);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005103 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005104
5105 final Rect frame = target.mFrame;
5106 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5107 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108 }
5109 }
Romain Guy06882f82009-06-10 13:36:04 -07005110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111 // finally offset the event to the target's coordinate system and
5112 // dispatch the event.
5113 try {
5114 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
5115 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
5116 }
Michael Chan53071d62009-05-13 17:29:48 -07005117
5118 if (MEASURE_LATENCY) {
5119 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5120 }
5121
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005122 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005123
5124 if (MEASURE_LATENCY) {
5125 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5126 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005127 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128 } catch (android.os.RemoteException e) {
5129 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
5130 mKeyWaiter.mMotionTarget = null;
5131 try {
5132 removeWindow(target.mSession, target.mClient);
5133 } catch (java.util.NoSuchElementException ex) {
5134 // This will happen if the window has already been
5135 // removed.
5136 }
5137 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005138 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 }
Romain Guy06882f82009-06-10 13:36:04 -07005140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005141 /**
5142 * @return Returns true if event was dispatched, false if it was dropped for any reason
5143 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005144 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 if (DEBUG_INPUT) Log.v(
5146 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005149 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 if (focusObj == null) {
5151 Log.w(TAG, "No focus window, dropping trackball: " + ev);
5152 if (qev != null) {
5153 mQueue.recycleEvent(qev);
5154 }
5155 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005156 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 }
5158 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5159 if (qev != null) {
5160 mQueue.recycleEvent(qev);
5161 }
5162 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005163 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 }
Romain Guy06882f82009-06-10 13:36:04 -07005165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 if (uid != 0 && uid != focus.mSession.mUid) {
5169 if (mContext.checkPermission(
5170 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5171 != PackageManager.PERMISSION_GRANTED) {
5172 Log.w(TAG, "Permission denied: injecting key event from pid "
5173 + pid + " uid " + uid + " to window " + focus
5174 + " owned by uid " + focus.mSession.mUid);
5175 if (qev != null) {
5176 mQueue.recycleEvent(qev);
5177 }
5178 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005179 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 }
5181 }
Romain Guy06882f82009-06-10 13:36:04 -07005182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005183 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 synchronized(mWindowMap) {
5186 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5187 mKeyWaiter.bindTargetWindowLocked(focus,
5188 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5189 // We don't deliver movement events to the client, we hold
5190 // them and wait for them to call back.
5191 ev = null;
5192 } else {
5193 mKeyWaiter.bindTargetWindowLocked(focus);
5194 }
5195 }
Romain Guy06882f82009-06-10 13:36:04 -07005196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005198 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005199 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 } catch (android.os.RemoteException e) {
5201 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5202 try {
5203 removeWindow(focus.mSession, focus.mClient);
5204 } catch (java.util.NoSuchElementException ex) {
5205 // This will happen if the window has already been
5206 // removed.
5207 }
5208 }
Romain Guy06882f82009-06-10 13:36:04 -07005209
Dianne Hackborncfaef692009-06-15 14:24:44 -07005210 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 }
Romain Guy06882f82009-06-10 13:36:04 -07005212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 /**
5214 * @return Returns true if event was dispatched, false if it was dropped for any reason
5215 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005216 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5218
5219 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005220 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 if (focusObj == null) {
5222 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005223 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 }
5225 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005226 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 }
Romain Guy06882f82009-06-10 13:36:04 -07005228
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005229 // Okay we have finished waiting for the last event to be processed.
5230 // First off, if this is a repeat event, check to see if there is
5231 // a corresponding up event in the queue. If there is, we will
5232 // just drop the repeat, because it makes no sense to repeat after
5233 // the user has released a key. (This is especially important for
5234 // long presses.)
5235 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5236 return INJECT_SUCCEEDED;
5237 }
5238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241 if (DEBUG_INPUT) Log.v(
5242 TAG, "Dispatching to " + focus + ": " + event);
5243
5244 if (uid != 0 && uid != focus.mSession.mUid) {
5245 if (mContext.checkPermission(
5246 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5247 != PackageManager.PERMISSION_GRANTED) {
5248 Log.w(TAG, "Permission denied: injecting key event from pid "
5249 + pid + " uid " + uid + " to window " + focus
5250 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005251 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 }
5253 }
Romain Guy06882f82009-06-10 13:36:04 -07005254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 synchronized(mWindowMap) {
5256 mKeyWaiter.bindTargetWindowLocked(focus);
5257 }
5258
5259 // NOSHIP extra state logging
5260 mKeyWaiter.recordDispatchState(event, focus);
5261 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 try {
5264 if (DEBUG_INPUT || DEBUG_FOCUS) {
5265 Log.v(TAG, "Delivering key " + event.getKeyCode()
5266 + " to " + focus);
5267 }
5268 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005269 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005270 } catch (android.os.RemoteException e) {
5271 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5272 try {
5273 removeWindow(focus.mSession, focus.mClient);
5274 } catch (java.util.NoSuchElementException ex) {
5275 // This will happen if the window has already been
5276 // removed.
5277 }
5278 }
Romain Guy06882f82009-06-10 13:36:04 -07005279
Dianne Hackborncfaef692009-06-15 14:24:44 -07005280 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 }
Romain Guy06882f82009-06-10 13:36:04 -07005282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 public void pauseKeyDispatching(IBinder _token) {
5284 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5285 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005286 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005287 }
5288
5289 synchronized (mWindowMap) {
5290 WindowToken token = mTokenMap.get(_token);
5291 if (token != null) {
5292 mKeyWaiter.pauseDispatchingLocked(token);
5293 }
5294 }
5295 }
5296
5297 public void resumeKeyDispatching(IBinder _token) {
5298 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5299 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005300 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 }
5302
5303 synchronized (mWindowMap) {
5304 WindowToken token = mTokenMap.get(_token);
5305 if (token != null) {
5306 mKeyWaiter.resumeDispatchingLocked(token);
5307 }
5308 }
5309 }
5310
5311 public void setEventDispatching(boolean enabled) {
5312 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5313 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005314 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005315 }
5316
5317 synchronized (mWindowMap) {
5318 mKeyWaiter.setEventDispatchingLocked(enabled);
5319 }
5320 }
Romain Guy06882f82009-06-10 13:36:04 -07005321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005322 /**
5323 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005324 *
5325 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 * {@link SystemClock#uptimeMillis()} as the timebase.)
5327 * @param sync If true, wait for the event to be completed before returning to the caller.
5328 * @return Returns true if event was dispatched, false if it was dropped for any reason
5329 */
5330 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5331 long downTime = ev.getDownTime();
5332 long eventTime = ev.getEventTime();
5333
5334 int action = ev.getAction();
5335 int code = ev.getKeyCode();
5336 int repeatCount = ev.getRepeatCount();
5337 int metaState = ev.getMetaState();
5338 int deviceId = ev.getDeviceId();
5339 int scancode = ev.getScanCode();
5340
5341 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5342 if (downTime == 0) downTime = eventTime;
5343
5344 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005345 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005346
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005347 final int pid = Binder.getCallingPid();
5348 final int uid = Binder.getCallingUid();
5349 final long ident = Binder.clearCallingIdentity();
5350 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005352 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005353 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005354 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005355 switch (result) {
5356 case INJECT_NO_PERMISSION:
5357 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305358 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005359 case INJECT_SUCCEEDED:
5360 return true;
5361 }
5362 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 }
5364
5365 /**
5366 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005367 *
5368 * @param ev A motion event describing the pointer (touch) action. (As noted in
5369 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 * {@link SystemClock#uptimeMillis()} as the timebase.)
5371 * @param sync If true, wait for the event to be completed before returning to the caller.
5372 * @return Returns true if event was dispatched, false if it was dropped for any reason
5373 */
5374 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005375 final int pid = Binder.getCallingPid();
5376 final int uid = Binder.getCallingUid();
5377 final long ident = Binder.clearCallingIdentity();
5378 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005379 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005380 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005382 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005383 switch (result) {
5384 case INJECT_NO_PERMISSION:
5385 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305386 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005387 case INJECT_SUCCEEDED:
5388 return true;
5389 }
5390 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 }
Romain Guy06882f82009-06-10 13:36:04 -07005392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 /**
5394 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005395 *
5396 * @param ev A motion event describing the trackball action. (As noted in
5397 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 * {@link SystemClock#uptimeMillis()} as the timebase.)
5399 * @param sync If true, wait for the event to be completed before returning to the caller.
5400 * @return Returns true if event was dispatched, false if it was dropped for any reason
5401 */
5402 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005403 final int pid = Binder.getCallingPid();
5404 final int uid = Binder.getCallingUid();
5405 final long ident = Binder.clearCallingIdentity();
5406 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005408 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005410 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005411 switch (result) {
5412 case INJECT_NO_PERMISSION:
5413 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305414 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005415 case INJECT_SUCCEEDED:
5416 return true;
5417 }
5418 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 }
Romain Guy06882f82009-06-10 13:36:04 -07005420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 private WindowState getFocusedWindow() {
5422 synchronized (mWindowMap) {
5423 return getFocusedWindowLocked();
5424 }
5425 }
5426
5427 private WindowState getFocusedWindowLocked() {
5428 return mCurrentFocus;
5429 }
Romain Guy06882f82009-06-10 13:36:04 -07005430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 /**
5432 * This class holds the state for dispatching key events. This state
5433 * is protected by the KeyWaiter instance, NOT by the window lock. You
5434 * can be holding the main window lock while acquire the KeyWaiter lock,
5435 * but not the other way around.
5436 */
5437 final class KeyWaiter {
5438 // NOSHIP debugging
5439 public class DispatchState {
5440 private KeyEvent event;
5441 private WindowState focus;
5442 private long time;
5443 private WindowState lastWin;
5444 private IBinder lastBinder;
5445 private boolean finished;
5446 private boolean gotFirstWindow;
5447 private boolean eventDispatching;
5448 private long timeToSwitch;
5449 private boolean wasFrozen;
5450 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005451 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005453 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5454 focus = theFocus;
5455 event = theEvent;
5456 time = System.currentTimeMillis();
5457 // snapshot KeyWaiter state
5458 lastWin = mLastWin;
5459 lastBinder = mLastBinder;
5460 finished = mFinished;
5461 gotFirstWindow = mGotFirstWindow;
5462 eventDispatching = mEventDispatching;
5463 timeToSwitch = mTimeToSwitch;
5464 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005465 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 // cache the paused state at ctor time as well
5467 if (theFocus == null || theFocus.mToken == null) {
5468 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5469 focusPaused = false;
5470 } else {
5471 focusPaused = theFocus.mToken.paused;
5472 }
5473 }
Romain Guy06882f82009-06-10 13:36:04 -07005474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 public String toString() {
5476 return "{{" + event + " to " + focus + " @ " + time
5477 + " lw=" + lastWin + " lb=" + lastBinder
5478 + " fin=" + finished + " gfw=" + gotFirstWindow
5479 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005480 + " wf=" + wasFrozen + " fp=" + focusPaused
Johan Alfven6c3da7f2010-05-12 09:00:28 +02005481 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 }
5483 };
5484 private DispatchState mDispatchState = null;
5485 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5486 mDispatchState = new DispatchState(theEvent, theFocus);
5487 }
5488 // END NOSHIP
5489
5490 public static final int RETURN_NOTHING = 0;
5491 public static final int RETURN_PENDING_POINTER = 1;
5492 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005494 final Object SKIP_TARGET_TOKEN = new Object();
5495 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497 private WindowState mLastWin = null;
5498 private IBinder mLastBinder = null;
5499 private boolean mFinished = true;
5500 private boolean mGotFirstWindow = false;
5501 private boolean mEventDispatching = true;
5502 private long mTimeToSwitch = 0;
5503 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005505 // Target of Motion events
5506 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 // Windows above the target who would like to receive an "outside"
5509 // touch event for any down events outside of them.
5510 WindowState mOutsideTouchTargets;
5511
5512 /**
5513 * Wait for the last event dispatch to complete, then find the next
5514 * target that should receive the given event and wait for that one
5515 * to be ready to receive it.
5516 */
5517 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5518 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005519 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 long startTime = SystemClock.uptimeMillis();
5521 long keyDispatchingTimeout = 5 * 1000;
5522 long waitedFor = 0;
5523
5524 while (true) {
5525 // Figure out which window we care about. It is either the
5526 // last window we are waiting to have process the event or,
5527 // if none, then the next window we think the event should go
5528 // to. Note: we retrieve mLastWin outside of the lock, so
5529 // it may change before we lock. Thus we must check it again.
5530 WindowState targetWin = mLastWin;
5531 boolean targetIsNew = targetWin == null;
5532 if (DEBUG_INPUT) Log.v(
5533 TAG, "waitForLastKey: mFinished=" + mFinished +
5534 ", mLastWin=" + mLastWin);
5535 if (targetIsNew) {
5536 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005537 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 if (target == SKIP_TARGET_TOKEN) {
5539 // The user has pressed a special key, and we are
5540 // dropping all pending events before it.
5541 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5542 + " " + nextMotion);
5543 return null;
5544 }
5545 if (target == CONSUMED_EVENT_TOKEN) {
5546 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5547 + " " + nextMotion);
5548 return target;
5549 }
5550 targetWin = (WindowState)target;
5551 }
Romain Guy06882f82009-06-10 13:36:04 -07005552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005553 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005555 // Now: is it okay to send the next event to this window?
5556 synchronized (this) {
5557 // First: did we come here based on the last window not
5558 // being null, but it changed by the time we got here?
5559 // If so, try again.
5560 if (!targetIsNew && mLastWin == null) {
5561 continue;
5562 }
Romain Guy06882f82009-06-10 13:36:04 -07005563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564 // We never dispatch events if not finished with the
5565 // last one, or the display is frozen.
5566 if (mFinished && !mDisplayFrozen) {
5567 // If event dispatching is disabled, then we
5568 // just consume the events.
5569 if (!mEventDispatching) {
5570 if (DEBUG_INPUT) Log.v(TAG,
5571 "Skipping event; dispatching disabled: "
5572 + nextKey + " " + nextMotion);
5573 return null;
5574 }
5575 if (targetWin != null) {
5576 // If this is a new target, and that target is not
5577 // paused or unresponsive, then all looks good to
5578 // handle the event.
5579 if (targetIsNew && !targetWin.mToken.paused) {
5580 return targetWin;
5581 }
Romain Guy06882f82009-06-10 13:36:04 -07005582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 // If we didn't find a target window, and there is no
5584 // focused app window, then just eat the events.
5585 } else if (mFocusedApp == null) {
5586 if (DEBUG_INPUT) Log.v(TAG,
5587 "Skipping event; no focused app: "
5588 + nextKey + " " + nextMotion);
5589 return null;
5590 }
5591 }
Romain Guy06882f82009-06-10 13:36:04 -07005592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005593 if (DEBUG_INPUT) Log.v(
5594 TAG, "Waiting for last key in " + mLastBinder
5595 + " target=" + targetWin
5596 + " mFinished=" + mFinished
5597 + " mDisplayFrozen=" + mDisplayFrozen
5598 + " targetIsNew=" + targetIsNew
5599 + " paused="
5600 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005601 + " mFocusedApp=" + mFocusedApp
5602 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 targetApp = targetWin != null
5605 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005607 long curTimeout = keyDispatchingTimeout;
5608 if (mTimeToSwitch != 0) {
5609 long now = SystemClock.uptimeMillis();
5610 if (mTimeToSwitch <= now) {
5611 // If an app switch key has been pressed, and we have
5612 // waited too long for the current app to finish
5613 // processing keys, then wait no more!
5614 doFinishedKeyLocked(true);
5615 continue;
5616 }
5617 long switchTimeout = mTimeToSwitch - now;
5618 if (curTimeout > switchTimeout) {
5619 curTimeout = switchTimeout;
5620 }
5621 }
Romain Guy06882f82009-06-10 13:36:04 -07005622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005623 try {
5624 // after that continue
5625 // processing keys, so we don't get stuck.
5626 if (DEBUG_INPUT) Log.v(
5627 TAG, "Waiting for key dispatch: " + curTimeout);
5628 wait(curTimeout);
5629 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5630 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005631 + startTime + " switchTime=" + mTimeToSwitch
5632 + " target=" + targetWin + " mLW=" + mLastWin
5633 + " mLB=" + mLastBinder + " fin=" + mFinished
5634 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005635 } catch (InterruptedException e) {
5636 }
5637 }
5638
5639 // If we were frozen during configuration change, restart the
5640 // timeout checks from now; otherwise look at whether we timed
5641 // out before awakening.
5642 if (mWasFrozen) {
5643 waitedFor = 0;
5644 mWasFrozen = false;
5645 } else {
5646 waitedFor = SystemClock.uptimeMillis() - startTime;
5647 }
5648
5649 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5650 IApplicationToken at = null;
5651 synchronized (this) {
5652 Log.w(TAG, "Key dispatching timed out sending to " +
5653 (targetWin != null ? targetWin.mAttrs.getTitle()
5654 : "<null>"));
5655 // NOSHIP debugging
5656 Log.w(TAG, "Dispatch state: " + mDispatchState);
5657 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5658 // END NOSHIP
5659 //dump();
5660 if (targetWin != null) {
5661 at = targetWin.getAppToken();
5662 } else if (targetApp != null) {
5663 at = targetApp.appToken;
5664 }
5665 }
5666
5667 boolean abort = true;
5668 if (at != null) {
5669 try {
5670 long timeout = at.getKeyDispatchingTimeout();
5671 if (timeout > waitedFor) {
5672 // we did not wait the proper amount of time for this application.
5673 // set the timeout to be the real timeout and wait again.
5674 keyDispatchingTimeout = timeout - waitedFor;
5675 continue;
5676 } else {
5677 abort = at.keyDispatchingTimedOut();
5678 }
5679 } catch (RemoteException ex) {
5680 }
5681 }
5682
5683 synchronized (this) {
5684 if (abort && (mLastWin == targetWin || targetWin == null)) {
5685 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005686 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 if (DEBUG_INPUT) Log.v(TAG,
5688 "Window " + mLastWin +
5689 " timed out on key input");
5690 if (mLastWin.mToken.paused) {
5691 Log.w(TAG, "Un-pausing dispatching to this window");
5692 mLastWin.mToken.paused = false;
5693 }
5694 }
5695 if (mMotionTarget == targetWin) {
5696 mMotionTarget = null;
5697 }
5698 mLastWin = null;
5699 mLastBinder = null;
5700 if (failIfTimeout || targetWin == null) {
5701 return null;
5702 }
5703 } else {
5704 Log.w(TAG, "Continuing to wait for key to be dispatched");
5705 startTime = SystemClock.uptimeMillis();
5706 }
5707 }
5708 }
5709 }
5710 }
Romain Guy06882f82009-06-10 13:36:04 -07005711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005713 MotionEvent nextMotion, boolean isPointerEvent,
5714 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005717 if (nextKey != null) {
5718 // Find the target window for a normal key event.
5719 final int keycode = nextKey.getKeyCode();
5720 final int repeatCount = nextKey.getRepeatCount();
5721 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5722 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005725 if (callingUid == 0 ||
5726 mContext.checkPermission(
5727 android.Manifest.permission.INJECT_EVENTS,
5728 callingPid, callingUid)
5729 == PackageManager.PERMISSION_GRANTED) {
5730 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005731 nextKey.getMetaState(), down, repeatCount,
5732 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 Log.w(TAG, "Event timeout during app switch: dropping "
5735 + nextKey);
5736 return SKIP_TARGET_TOKEN;
5737 }
Romain Guy06882f82009-06-10 13:36:04 -07005738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 WindowState focus = null;
5742 synchronized(mWindowMap) {
5743 focus = getFocusedWindowLocked();
5744 }
Romain Guy06882f82009-06-10 13:36:04 -07005745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005747
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005748 if (callingUid == 0 ||
5749 (focus != null && callingUid == focus.mSession.mUid) ||
5750 mContext.checkPermission(
5751 android.Manifest.permission.INJECT_EVENTS,
5752 callingPid, callingUid)
5753 == PackageManager.PERMISSION_GRANTED) {
5754 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005755 keycode, nextKey.getMetaState(), down, repeatCount,
5756 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005757 return CONSUMED_EVENT_TOKEN;
5758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 }
Romain Guy06882f82009-06-10 13:36:04 -07005760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 } else if (!isPointerEvent) {
5764 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5765 if (!dispatch) {
5766 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5767 + nextMotion);
5768 return SKIP_TARGET_TOKEN;
5769 }
Romain Guy06882f82009-06-10 13:36:04 -07005770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771 WindowState focus = null;
5772 synchronized(mWindowMap) {
5773 focus = getFocusedWindowLocked();
5774 }
Romain Guy06882f82009-06-10 13:36:04 -07005775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5777 return focus;
5778 }
Romain Guy06882f82009-06-10 13:36:04 -07005779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 if (nextMotion == null) {
5781 return SKIP_TARGET_TOKEN;
5782 }
Romain Guy06882f82009-06-10 13:36:04 -07005783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5785 KeyEvent.KEYCODE_UNKNOWN);
5786 if (!dispatch) {
5787 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5788 + nextMotion);
5789 return SKIP_TARGET_TOKEN;
5790 }
Romain Guy06882f82009-06-10 13:36:04 -07005791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 // Find the target window for a pointer event.
5793 int action = nextMotion.getAction();
5794 final float xf = nextMotion.getX();
5795 final float yf = nextMotion.getY();
5796 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 final boolean screenWasOff = qev != null
5799 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005801 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 synchronized(mWindowMap) {
5804 synchronized (this) {
5805 if (action == MotionEvent.ACTION_DOWN) {
5806 if (mMotionTarget != null) {
5807 // this is weird, we got a pen down, but we thought it was
5808 // already down!
5809 // XXX: We should probably send an ACTION_UP to the current
5810 // target.
5811 Log.w(TAG, "Pointer down received while already down in: "
5812 + mMotionTarget);
5813 mMotionTarget = null;
5814 }
Romain Guy06882f82009-06-10 13:36:04 -07005815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 // ACTION_DOWN is special, because we need to lock next events to
5817 // the window we'll land onto.
5818 final int x = (int)xf;
5819 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005821 final ArrayList windows = mWindows;
5822 final int N = windows.size();
5823 WindowState topErrWindow = null;
5824 final Rect tmpRect = mTempRect;
5825 for (int i=N-1; i>=0; i--) {
5826 WindowState child = (WindowState)windows.get(i);
5827 //Log.i(TAG, "Checking dispatch to: " + child);
5828 final int flags = child.mAttrs.flags;
5829 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5830 if (topErrWindow == null) {
5831 topErrWindow = child;
5832 }
5833 }
5834 if (!child.isVisibleLw()) {
5835 //Log.i(TAG, "Not visible!");
5836 continue;
5837 }
5838 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5839 //Log.i(TAG, "Not touchable!");
5840 if ((flags & WindowManager.LayoutParams
5841 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5842 child.mNextOutsideTouch = mOutsideTouchTargets;
5843 mOutsideTouchTargets = child;
5844 }
5845 continue;
5846 }
5847 tmpRect.set(child.mFrame);
5848 if (child.mTouchableInsets == ViewTreeObserver
5849 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5850 // The touch is inside of the window if it is
5851 // inside the frame, AND the content part of that
5852 // frame that was given by the application.
5853 tmpRect.left += child.mGivenContentInsets.left;
5854 tmpRect.top += child.mGivenContentInsets.top;
5855 tmpRect.right -= child.mGivenContentInsets.right;
5856 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5857 } else if (child.mTouchableInsets == ViewTreeObserver
5858 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5859 // The touch is inside of the window if it is
5860 // inside the frame, AND the visible part of that
5861 // frame that was given by the application.
5862 tmpRect.left += child.mGivenVisibleInsets.left;
5863 tmpRect.top += child.mGivenVisibleInsets.top;
5864 tmpRect.right -= child.mGivenVisibleInsets.right;
5865 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5866 }
5867 final int touchFlags = flags &
5868 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5869 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5870 if (tmpRect.contains(x, y) || touchFlags == 0) {
5871 //Log.i(TAG, "Using this target!");
5872 if (!screenWasOff || (flags &
5873 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5874 mMotionTarget = child;
5875 } else {
5876 //Log.i(TAG, "Waking, skip!");
5877 mMotionTarget = null;
5878 }
5879 break;
5880 }
Romain Guy06882f82009-06-10 13:36:04 -07005881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 if ((flags & WindowManager.LayoutParams
5883 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5884 child.mNextOutsideTouch = mOutsideTouchTargets;
5885 mOutsideTouchTargets = child;
5886 //Log.i(TAG, "Adding to outside target list: " + child);
5887 }
5888 }
5889
5890 // if there's an error window but it's not accepting
5891 // focus (typically because it is not yet visible) just
5892 // wait for it -- any other focused window may in fact
5893 // be in ANR state.
5894 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5895 mMotionTarget = null;
5896 }
5897 }
Romain Guy06882f82009-06-10 13:36:04 -07005898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005899 target = mMotionTarget;
5900 }
5901 }
Romain Guy06882f82009-06-10 13:36:04 -07005902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005905 // Pointer events are a little different -- if there isn't a
5906 // target found for any event, then just drop it.
5907 return target != null ? target : SKIP_TARGET_TOKEN;
5908 }
Romain Guy06882f82009-06-10 13:36:04 -07005909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 boolean checkShouldDispatchKey(int keycode) {
5911 synchronized (this) {
5912 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5913 mTimeToSwitch = 0;
5914 return true;
5915 }
5916 if (mTimeToSwitch != 0
5917 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5918 return false;
5919 }
5920 return true;
5921 }
5922 }
Romain Guy06882f82009-06-10 13:36:04 -07005923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005924 void bindTargetWindowLocked(WindowState win,
5925 int pendingWhat, QueuedEvent pendingMotion) {
5926 synchronized (this) {
5927 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5928 }
5929 }
Romain Guy06882f82009-06-10 13:36:04 -07005930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 void bindTargetWindowLocked(WindowState win) {
5932 synchronized (this) {
5933 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5934 }
5935 }
5936
5937 void bindTargetWindowLockedLocked(WindowState win,
5938 int pendingWhat, QueuedEvent pendingMotion) {
5939 mLastWin = win;
5940 mLastBinder = win.mClient.asBinder();
5941 mFinished = false;
5942 if (pendingMotion != null) {
5943 final Session s = win.mSession;
5944 if (pendingWhat == RETURN_PENDING_POINTER) {
5945 releasePendingPointerLocked(s);
5946 s.mPendingPointerMove = pendingMotion;
5947 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005948 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 "bindTargetToWindow " + s.mPendingPointerMove);
5950 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5951 releasePendingTrackballLocked(s);
5952 s.mPendingTrackballMove = pendingMotion;
5953 s.mPendingTrackballWindow = win;
5954 }
5955 }
5956 }
Romain Guy06882f82009-06-10 13:36:04 -07005957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 void releasePendingPointerLocked(Session s) {
5959 if (DEBUG_INPUT) Log.v(TAG,
5960 "releasePendingPointer " + s.mPendingPointerMove);
5961 if (s.mPendingPointerMove != null) {
5962 mQueue.recycleEvent(s.mPendingPointerMove);
5963 s.mPendingPointerMove = null;
5964 }
5965 }
Romain Guy06882f82009-06-10 13:36:04 -07005966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 void releasePendingTrackballLocked(Session s) {
5968 if (s.mPendingTrackballMove != null) {
5969 mQueue.recycleEvent(s.mPendingTrackballMove);
5970 s.mPendingTrackballMove = null;
5971 }
5972 }
Romain Guy06882f82009-06-10 13:36:04 -07005973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5975 int returnWhat) {
5976 if (DEBUG_INPUT) Log.v(
5977 TAG, "finishedKey: client=" + client + ", force=" + force);
5978
5979 if (client == null) {
5980 return null;
5981 }
5982
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005983 MotionEvent res = null;
5984 QueuedEvent qev = null;
5985 WindowState win = null;
5986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005987 synchronized (this) {
5988 if (DEBUG_INPUT) Log.v(
5989 TAG, "finishedKey: client=" + client.asBinder()
5990 + ", force=" + force + ", last=" + mLastBinder
5991 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 if (returnWhat == RETURN_PENDING_POINTER) {
5994 qev = session.mPendingPointerMove;
5995 win = session.mPendingPointerWindow;
5996 session.mPendingPointerMove = null;
5997 session.mPendingPointerWindow = null;
5998 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5999 qev = session.mPendingTrackballMove;
6000 win = session.mPendingTrackballWindow;
6001 session.mPendingTrackballMove = null;
6002 session.mPendingTrackballWindow = null;
6003 }
Romain Guy06882f82009-06-10 13:36:04 -07006004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 if (mLastBinder == client.asBinder()) {
6006 if (DEBUG_INPUT) Log.v(
6007 TAG, "finishedKey: last paused="
6008 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6009 if (mLastWin != null && (!mLastWin.mToken.paused || force
6010 || !mEventDispatching)) {
6011 doFinishedKeyLocked(false);
6012 } else {
6013 // Make sure to wake up anyone currently waiting to
6014 // dispatch a key, so they can re-evaluate their
6015 // current situation.
6016 mFinished = true;
6017 notifyAll();
6018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 }
Romain Guy06882f82009-06-10 13:36:04 -07006020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006022 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 if (DEBUG_INPUT) Log.v(TAG,
6024 "Returning pending motion: " + res);
6025 mQueue.recycleEvent(qev);
6026 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6027 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006030 }
6031
6032 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6033 synchronized (mWindowMap) {
6034 if ((mWallpaperTarget == win &&
6035 win.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD)
6036 || mSendingPointersToWallpaper) {
6037 sendPointerToWallpaperLocked(win, res, res.getEventTime());
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006038 }
6039 }
6040 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006041
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006042 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006043 }
6044
6045 void tickle() {
6046 synchronized (this) {
6047 notifyAll();
6048 }
6049 }
Romain Guy06882f82009-06-10 13:36:04 -07006050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 void handleNewWindowLocked(WindowState newWindow) {
6052 if (!newWindow.canReceiveKeys()) {
6053 return;
6054 }
6055 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006056 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006057 TAG, "New key dispatch window: win="
6058 + newWindow.mClient.asBinder()
6059 + ", last=" + mLastBinder
6060 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6061 + "), finished=" + mFinished + ", paused="
6062 + newWindow.mToken.paused);
6063
6064 // Displaying a window implicitly causes dispatching to
6065 // be unpaused. (This is to protect against bugs if someone
6066 // pauses dispatching but forgets to resume.)
6067 newWindow.mToken.paused = false;
6068
6069 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070
6071 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
6072 if (DEBUG_INPUT) Log.v(TAG,
6073 "New SYSTEM_ERROR window; resetting state");
6074 mLastWin = null;
6075 mLastBinder = null;
6076 mMotionTarget = null;
6077 mFinished = true;
6078 } else if (mLastWin != null) {
6079 // If the new window is above the window we are
6080 // waiting on, then stop waiting and let key dispatching
6081 // start on the new guy.
6082 if (DEBUG_INPUT) Log.v(
6083 TAG, "Last win layer=" + mLastWin.mLayer
6084 + ", new win layer=" + newWindow.mLayer);
6085 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006086 // The new window is above the old; finish pending input to the last
6087 // window and start directing it to the new one.
6088 mLastWin.mToken.paused = false;
6089 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006091 // Either the new window is lower, so there is no need to wake key waiters,
6092 // or we just finished key input to the previous window, which implicitly
6093 // notified the key waiters. In both cases, we don't need to issue the
6094 // notification here.
6095 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006096 }
6097
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006098 // Now that we've put a new window state in place, make the event waiter
6099 // take notice and retarget its attentions.
6100 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 }
6102 }
6103
6104 void pauseDispatchingLocked(WindowToken token) {
6105 synchronized (this)
6106 {
6107 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
6108 token.paused = true;
6109
6110 /*
6111 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6112 mPaused = true;
6113 } else {
6114 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07006115 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07006117 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07006119 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006120 }
6121 }
6122 */
6123 }
6124 }
6125
6126 void resumeDispatchingLocked(WindowToken token) {
6127 synchronized (this) {
6128 if (token.paused) {
6129 if (DEBUG_INPUT) Log.v(
6130 TAG, "Resuming WindowToken " + token
6131 + ", last=" + mLastBinder
6132 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6133 + "), finished=" + mFinished + ", paused="
6134 + token.paused);
6135 token.paused = false;
6136 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
6137 doFinishedKeyLocked(true);
6138 } else {
6139 notifyAll();
6140 }
6141 }
6142 }
6143 }
6144
6145 void setEventDispatchingLocked(boolean enabled) {
6146 synchronized (this) {
6147 mEventDispatching = enabled;
6148 notifyAll();
6149 }
6150 }
Romain Guy06882f82009-06-10 13:36:04 -07006151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 void appSwitchComing() {
6153 synchronized (this) {
6154 // Don't wait for more than .5 seconds for app to finish
6155 // processing the pending events.
6156 long now = SystemClock.uptimeMillis() + 500;
6157 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6158 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6159 mTimeToSwitch = now;
6160 }
6161 notifyAll();
6162 }
6163 }
Romain Guy06882f82009-06-10 13:36:04 -07006164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006165 private final void doFinishedKeyLocked(boolean doRecycle) {
6166 if (mLastWin != null) {
6167 releasePendingPointerLocked(mLastWin.mSession);
6168 releasePendingTrackballLocked(mLastWin.mSession);
6169 }
Romain Guy06882f82009-06-10 13:36:04 -07006170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006171 if (mLastWin == null || !mLastWin.mToken.paused
6172 || !mLastWin.isVisibleLw()) {
6173 // If the current window has been paused, we aren't -really-
6174 // finished... so let the waiters still wait.
6175 mLastWin = null;
6176 mLastBinder = null;
6177 }
6178 mFinished = true;
6179 notifyAll();
6180 }
6181 }
6182
6183 private class KeyQ extends KeyInputQueue
6184 implements KeyInputQueue.FilterCallback {
6185 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006188 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6190 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6191 "KEEP_SCREEN_ON_FLAG");
6192 mHoldingScreen.setReferenceCounted(false);
6193 }
6194
6195 @Override
6196 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6197 if (mPolicy.preprocessInputEventTq(event)) {
6198 return true;
6199 }
Romain Guy06882f82009-06-10 13:36:04 -07006200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 switch (event.type) {
6202 case RawInputEvent.EV_KEY: {
6203 // XXX begin hack
6204 if (DEBUG) {
6205 if (event.keycode == KeyEvent.KEYCODE_G) {
6206 if (event.value != 0) {
6207 // G down
6208 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6209 }
6210 return false;
6211 }
6212 if (event.keycode == KeyEvent.KEYCODE_D) {
6213 if (event.value != 0) {
6214 //dump();
6215 }
6216 return false;
6217 }
6218 }
6219 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006220
Charles Mendis322591c2009-10-29 11:06:59 -07006221 boolean screenIsOff = !mPowerManager.isScreenOn();
6222 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6226 mPowerManager.goToSleep(event.when);
6227 }
6228
6229 if (screenIsOff) {
6230 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6231 }
6232 if (screenIsDim) {
6233 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6234 }
6235 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6236 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006237 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006238 }
Romain Guy06882f82009-06-10 13:36:04 -07006239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006240 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6241 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6242 filterQueue(this);
6243 mKeyWaiter.appSwitchComing();
6244 }
6245 return true;
6246 } else {
6247 return false;
6248 }
6249 }
Romain Guy06882f82009-06-10 13:36:04 -07006250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006252 boolean screenIsOff = !mPowerManager.isScreenOn();
6253 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 if (screenIsOff) {
6255 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6256 device.classes, event)) {
6257 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6258 return false;
6259 }
6260 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6261 }
6262 if (screenIsDim) {
6263 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6264 }
6265 return true;
6266 }
Romain Guy06882f82009-06-10 13:36:04 -07006267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006269 boolean screenIsOff = !mPowerManager.isScreenOn();
6270 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 if (screenIsOff) {
6272 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6273 device.classes, event)) {
6274 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6275 return false;
6276 }
6277 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6278 }
6279 if (screenIsDim) {
6280 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6281 }
6282 return true;
6283 }
Romain Guy06882f82009-06-10 13:36:04 -07006284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 default:
6286 return true;
6287 }
6288 }
6289
6290 public int filterEvent(QueuedEvent ev) {
6291 switch (ev.classType) {
6292 case RawInputEvent.CLASS_KEYBOARD:
6293 KeyEvent ke = (KeyEvent)ev.event;
6294 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6295 Log.w(TAG, "Dropping movement key during app switch: "
6296 + ke.getKeyCode() + ", action=" + ke.getAction());
6297 return FILTER_REMOVE;
6298 }
6299 return FILTER_ABORT;
6300 default:
6301 return FILTER_KEEP;
6302 }
6303 }
Romain Guy06882f82009-06-10 13:36:04 -07006304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006305 /**
6306 * Must be called with the main window manager lock held.
6307 */
6308 void setHoldScreenLocked(boolean holding) {
6309 boolean state = mHoldingScreen.isHeld();
6310 if (holding != state) {
6311 if (holding) {
6312 mHoldingScreen.acquire();
6313 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006314 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 mHoldingScreen.release();
6316 }
6317 }
6318 }
Michael Chan53071d62009-05-13 17:29:48 -07006319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006320
6321 public boolean detectSafeMode() {
6322 mSafeMode = mPolicy.detectSafeMode();
6323 return mSafeMode;
6324 }
Romain Guy06882f82009-06-10 13:36:04 -07006325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006326 public void systemReady() {
6327 mPolicy.systemReady();
6328 }
Romain Guy06882f82009-06-10 13:36:04 -07006329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006330 private final class InputDispatcherThread extends Thread {
6331 // Time to wait when there is nothing to do: 9999 seconds.
6332 static final int LONG_WAIT=9999*1000;
6333
6334 public InputDispatcherThread() {
6335 super("InputDispatcher");
6336 }
Romain Guy06882f82009-06-10 13:36:04 -07006337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006338 @Override
6339 public void run() {
6340 while (true) {
6341 try {
6342 process();
6343 } catch (Exception e) {
6344 Log.e(TAG, "Exception in input dispatcher", e);
6345 }
6346 }
6347 }
Romain Guy06882f82009-06-10 13:36:04 -07006348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 private void process() {
6350 android.os.Process.setThreadPriority(
6351 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 // The last key event we saw
6354 KeyEvent lastKey = null;
6355
6356 // Last keydown time for auto-repeating keys
6357 long lastKeyTime = SystemClock.uptimeMillis();
6358 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006359 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360
Romain Guy06882f82009-06-10 13:36:04 -07006361 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006362 int keyRepeatCount = 0;
6363
6364 // Need to report that configuration has changed?
6365 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 while (true) {
6368 long curTime = SystemClock.uptimeMillis();
6369
6370 if (DEBUG_INPUT) Log.v(
6371 TAG, "Waiting for next key: now=" + curTime
6372 + ", repeat @ " + nextKeyTime);
6373
6374 // Retrieve next event, waiting only as long as the next
6375 // repeat timeout. If the configuration has changed, then
6376 // don't wait at all -- we'll report the change as soon as
6377 // we have processed all events.
6378 QueuedEvent ev = mQueue.getEvent(
6379 (int)((!configChanged && curTime < nextKeyTime)
6380 ? (nextKeyTime-curTime) : 0));
6381
6382 if (DEBUG_INPUT && ev != null) Log.v(
6383 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6384
Michael Chan53071d62009-05-13 17:29:48 -07006385 if (MEASURE_LATENCY) {
6386 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6387 }
6388
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006389 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6390 // cancel key repeat at the request of the policy.
6391 lastKey = null;
6392 downTime = 0;
6393 lastKeyTime = curTime;
6394 nextKeyTime = curTime + LONG_WAIT;
6395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 try {
6397 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006398 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006399 int eventType;
6400 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6401 eventType = eventType((MotionEvent)ev.event);
6402 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6403 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6404 eventType = LocalPowerManager.BUTTON_EVENT;
6405 } else {
6406 eventType = LocalPowerManager.OTHER_EVENT;
6407 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006408 try {
Michael Chan53071d62009-05-13 17:29:48 -07006409 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006410 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006411 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006412 mBatteryStats.noteInputEvent();
6413 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006414 } catch (RemoteException e) {
6415 // Ignore
6416 }
Michael Chane10de972009-05-18 11:24:50 -07006417
Mike Lockwood5db42402009-11-30 14:51:51 -05006418 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6419 // do not wake screen in this case
6420 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006421 && eventType != LONG_TOUCH_EVENT
6422 && eventType != CHEEK_EVENT) {
6423 mPowerManager.userActivity(curTime, false,
6424 eventType, false);
6425 } else if (mLastTouchEventType != eventType
6426 || (curTime - mLastUserActivityCallTime)
6427 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6428 mLastUserActivityCallTime = curTime;
6429 mLastTouchEventType = eventType;
6430 mPowerManager.userActivity(curTime, false,
6431 eventType, false);
6432 }
6433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006434 switch (ev.classType) {
6435 case RawInputEvent.CLASS_KEYBOARD:
6436 KeyEvent ke = (KeyEvent)ev.event;
6437 if (ke.isDown()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 lastKeyTime = curTime;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006439 if (lastKey != null &&
6440 ke.getKeyCode() == lastKey.getKeyCode()) {
6441 keyRepeatCount++;
6442 // Arbitrary long timeout to block
6443 // repeating here since we know that
6444 // the device driver takes care of it.
6445 nextKeyTime = lastKeyTime + LONG_WAIT;
6446 if (DEBUG_INPUT) Log.v(
6447 TAG, "Received repeated key down");
6448 } else {
6449 downTime = curTime;
6450 keyRepeatCount = 0;
6451 nextKeyTime = lastKeyTime
6452 + ViewConfiguration.getLongPressTimeout();
6453 if (DEBUG_INPUT) Log.v(
6454 TAG, "Received key down: first repeat @ "
6455 + nextKeyTime);
6456 }
6457 lastKey = ke;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 } else {
6459 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006460 downTime = 0;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006461 keyRepeatCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 // Arbitrary long timeout.
6463 lastKeyTime = curTime;
6464 nextKeyTime = curTime + LONG_WAIT;
6465 if (DEBUG_INPUT) Log.v(
6466 TAG, "Received key up: ignore repeat @ "
6467 + nextKeyTime);
6468 }
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006469 if (keyRepeatCount > 0) {
6470 dispatchKey(KeyEvent.changeTimeRepeat(ke,
6471 ke.getEventTime(), keyRepeatCount), 0, 0);
6472 } else {
6473 dispatchKey(ke, 0, 0);
6474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006475 mQueue.recycleEvent(ev);
6476 break;
6477 case RawInputEvent.CLASS_TOUCHSCREEN:
6478 //Log.i(TAG, "Read next event " + ev);
6479 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6480 break;
6481 case RawInputEvent.CLASS_TRACKBALL:
6482 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6483 break;
6484 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6485 configChanged = true;
6486 break;
6487 default:
6488 mQueue.recycleEvent(ev);
6489 break;
6490 }
Romain Guy06882f82009-06-10 13:36:04 -07006491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006492 } else if (configChanged) {
6493 configChanged = false;
6494 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006496 } else if (lastKey != null) {
6497 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 // Timeout occurred while key was down. If it is at or
6500 // past the key repeat time, dispatch the repeat.
6501 if (DEBUG_INPUT) Log.v(
6502 TAG, "Key timeout: repeat=" + nextKeyTime
6503 + ", now=" + curTime);
6504 if (curTime < nextKeyTime) {
6505 continue;
6506 }
Romain Guy06882f82009-06-10 13:36:04 -07006507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508 lastKeyTime = nextKeyTime;
6509 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6510 keyRepeatCount++;
6511 if (DEBUG_INPUT) Log.v(
6512 TAG, "Key repeat: count=" + keyRepeatCount
6513 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006514 KeyEvent newEvent;
6515 if (downTime != 0 && (downTime
6516 + ViewConfiguration.getLongPressTimeout())
6517 <= curTime) {
6518 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6519 curTime, keyRepeatCount,
6520 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6521 downTime = 0;
6522 } else {
6523 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6524 curTime, keyRepeatCount);
6525 }
6526 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006528 } else {
6529 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006531 lastKeyTime = curTime;
6532 nextKeyTime = curTime + LONG_WAIT;
6533 }
Romain Guy06882f82009-06-10 13:36:04 -07006534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 } catch (Exception e) {
6536 Log.e(TAG,
6537 "Input thread received uncaught exception: " + e, e);
6538 }
6539 }
6540 }
6541 }
6542
6543 // -------------------------------------------------------------
6544 // Client Session State
6545 // -------------------------------------------------------------
6546
6547 private final class Session extends IWindowSession.Stub
6548 implements IBinder.DeathRecipient {
6549 final IInputMethodClient mClient;
6550 final IInputContext mInputContext;
6551 final int mUid;
6552 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006553 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006554 SurfaceSession mSurfaceSession;
6555 int mNumWindow = 0;
6556 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006558 /**
6559 * Current pointer move event being dispatched to client window... must
6560 * hold key lock to access.
6561 */
6562 QueuedEvent mPendingPointerMove;
6563 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006565 /**
6566 * Current trackball move event being dispatched to client window... must
6567 * hold key lock to access.
6568 */
6569 QueuedEvent mPendingTrackballMove;
6570 WindowState mPendingTrackballWindow;
6571
6572 public Session(IInputMethodClient client, IInputContext inputContext) {
6573 mClient = client;
6574 mInputContext = inputContext;
6575 mUid = Binder.getCallingUid();
6576 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006577 StringBuilder sb = new StringBuilder();
6578 sb.append("Session{");
6579 sb.append(Integer.toHexString(System.identityHashCode(this)));
6580 sb.append(" uid ");
6581 sb.append(mUid);
6582 sb.append("}");
6583 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006585 synchronized (mWindowMap) {
6586 if (mInputMethodManager == null && mHaveInputMethods) {
6587 IBinder b = ServiceManager.getService(
6588 Context.INPUT_METHOD_SERVICE);
6589 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6590 }
6591 }
6592 long ident = Binder.clearCallingIdentity();
6593 try {
6594 // Note: it is safe to call in to the input method manager
6595 // here because we are not holding our lock.
6596 if (mInputMethodManager != null) {
6597 mInputMethodManager.addClient(client, inputContext,
6598 mUid, mPid);
6599 } else {
6600 client.setUsingInputMethod(false);
6601 }
6602 client.asBinder().linkToDeath(this, 0);
6603 } catch (RemoteException e) {
6604 // The caller has died, so we can just forget about this.
6605 try {
6606 if (mInputMethodManager != null) {
6607 mInputMethodManager.removeClient(client);
6608 }
6609 } catch (RemoteException ee) {
6610 }
6611 } finally {
6612 Binder.restoreCallingIdentity(ident);
6613 }
6614 }
Romain Guy06882f82009-06-10 13:36:04 -07006615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 @Override
6617 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6618 throws RemoteException {
6619 try {
6620 return super.onTransact(code, data, reply, flags);
6621 } catch (RuntimeException e) {
6622 // Log all 'real' exceptions thrown to the caller
6623 if (!(e instanceof SecurityException)) {
6624 Log.e(TAG, "Window Session Crash", e);
6625 }
6626 throw e;
6627 }
6628 }
6629
6630 public void binderDied() {
6631 // Note: it is safe to call in to the input method manager
6632 // here because we are not holding our lock.
6633 try {
6634 if (mInputMethodManager != null) {
6635 mInputMethodManager.removeClient(mClient);
6636 }
6637 } catch (RemoteException e) {
6638 }
6639 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006640 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 mClientDead = true;
6642 killSessionLocked();
6643 }
6644 }
6645
6646 public int add(IWindow window, WindowManager.LayoutParams attrs,
6647 int viewVisibility, Rect outContentInsets) {
6648 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6649 }
Romain Guy06882f82009-06-10 13:36:04 -07006650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651 public void remove(IWindow window) {
6652 removeWindow(this, window);
6653 }
Romain Guy06882f82009-06-10 13:36:04 -07006654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006655 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6656 int requestedWidth, int requestedHeight, int viewFlags,
6657 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6658 Rect outVisibleInsets, Surface outSurface) {
6659 return relayoutWindow(this, window, attrs,
6660 requestedWidth, requestedHeight, viewFlags, insetsPending,
6661 outFrame, outContentInsets, outVisibleInsets, outSurface);
6662 }
Romain Guy06882f82009-06-10 13:36:04 -07006663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 public void setTransparentRegion(IWindow window, Region region) {
6665 setTransparentRegionWindow(this, window, region);
6666 }
Romain Guy06882f82009-06-10 13:36:04 -07006667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006668 public void setInsets(IWindow window, int touchableInsets,
6669 Rect contentInsets, Rect visibleInsets) {
6670 setInsetsWindow(this, window, touchableInsets, contentInsets,
6671 visibleInsets);
6672 }
Romain Guy06882f82009-06-10 13:36:04 -07006673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006674 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6675 getWindowDisplayFrame(this, window, outDisplayFrame);
6676 }
Romain Guy06882f82009-06-10 13:36:04 -07006677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 public void finishDrawing(IWindow window) {
6679 if (localLOGV) Log.v(
6680 TAG, "IWindow finishDrawing called for " + window);
6681 finishDrawingWindow(this, window);
6682 }
6683
6684 public void finishKey(IWindow window) {
6685 if (localLOGV) Log.v(
6686 TAG, "IWindow finishKey called for " + window);
6687 mKeyWaiter.finishedKey(this, window, false,
6688 KeyWaiter.RETURN_NOTHING);
6689 }
6690
6691 public MotionEvent getPendingPointerMove(IWindow window) {
6692 if (localLOGV) Log.v(
6693 TAG, "IWindow getPendingMotionEvent called for " + window);
6694 return mKeyWaiter.finishedKey(this, window, false,
6695 KeyWaiter.RETURN_PENDING_POINTER);
6696 }
Romain Guy06882f82009-06-10 13:36:04 -07006697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006698 public MotionEvent getPendingTrackballMove(IWindow window) {
6699 if (localLOGV) Log.v(
6700 TAG, "IWindow getPendingMotionEvent called for " + window);
6701 return mKeyWaiter.finishedKey(this, window, false,
6702 KeyWaiter.RETURN_PENDING_TRACKBALL);
6703 }
6704
6705 public void setInTouchMode(boolean mode) {
6706 synchronized(mWindowMap) {
6707 mInTouchMode = mode;
6708 }
6709 }
6710
6711 public boolean getInTouchMode() {
6712 synchronized(mWindowMap) {
6713 return mInTouchMode;
6714 }
6715 }
6716
6717 public boolean performHapticFeedback(IWindow window, int effectId,
6718 boolean always) {
6719 synchronized(mWindowMap) {
6720 long ident = Binder.clearCallingIdentity();
6721 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006722 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006723 windowForClientLocked(this, window), effectId, always);
6724 } finally {
6725 Binder.restoreCallingIdentity(ident);
6726 }
6727 }
6728 }
Romain Guy06882f82009-06-10 13:36:04 -07006729
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006730 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006731 synchronized(mWindowMap) {
6732 long ident = Binder.clearCallingIdentity();
6733 try {
6734 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006735 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006736 } finally {
6737 Binder.restoreCallingIdentity(ident);
6738 }
6739 }
6740 }
6741
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006742 public void wallpaperOffsetsComplete(IBinder window) {
6743 WindowManagerService.this.wallpaperOffsetsComplete(window);
6744 }
6745
Dianne Hackborn75804932009-10-20 20:15:20 -07006746 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6747 int z, Bundle extras, boolean sync) {
6748 synchronized(mWindowMap) {
6749 long ident = Binder.clearCallingIdentity();
6750 try {
6751 return sendWindowWallpaperCommandLocked(
6752 windowForClientLocked(this, window),
6753 action, x, y, z, extras, sync);
6754 } finally {
6755 Binder.restoreCallingIdentity(ident);
6756 }
6757 }
6758 }
6759
6760 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6761 WindowManagerService.this.wallpaperCommandComplete(window, result);
6762 }
6763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 void windowAddedLocked() {
6765 if (mSurfaceSession == null) {
6766 if (localLOGV) Log.v(
6767 TAG, "First window added to " + this + ", creating SurfaceSession");
6768 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006769 if (SHOW_TRANSACTIONS) Log.i(
6770 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 mSessions.add(this);
6772 }
6773 mNumWindow++;
6774 }
6775
6776 void windowRemovedLocked() {
6777 mNumWindow--;
6778 killSessionLocked();
6779 }
Romain Guy06882f82009-06-10 13:36:04 -07006780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 void killSessionLocked() {
6782 if (mNumWindow <= 0 && mClientDead) {
6783 mSessions.remove(this);
6784 if (mSurfaceSession != null) {
6785 if (localLOGV) Log.v(
6786 TAG, "Last window removed from " + this
6787 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006788 if (SHOW_TRANSACTIONS) Log.i(
6789 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006790 try {
6791 mSurfaceSession.kill();
6792 } catch (Exception e) {
6793 Log.w(TAG, "Exception thrown when killing surface session "
6794 + mSurfaceSession + " in session " + this
6795 + ": " + e.toString());
6796 }
6797 mSurfaceSession = null;
6798 }
6799 }
6800 }
Romain Guy06882f82009-06-10 13:36:04 -07006801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006803 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6804 pw.print(" mClientDead="); pw.print(mClientDead);
6805 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6806 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6807 pw.print(prefix);
6808 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6809 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6810 }
6811 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6812 pw.print(prefix);
6813 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6814 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006816 }
6817
6818 @Override
6819 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006820 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006821 }
6822 }
6823
6824 // -------------------------------------------------------------
6825 // Client Window State
6826 // -------------------------------------------------------------
6827
6828 private final class WindowState implements WindowManagerPolicy.WindowState {
6829 final Session mSession;
6830 final IWindow mClient;
6831 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006832 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 AppWindowToken mAppToken;
6834 AppWindowToken mTargetAppToken;
6835 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6836 final DeathRecipient mDeathRecipient;
6837 final WindowState mAttachedWindow;
6838 final ArrayList mChildWindows = new ArrayList();
6839 final int mBaseLayer;
6840 final int mSubLayer;
6841 final boolean mLayoutAttached;
6842 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006843 final boolean mIsWallpaper;
6844 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006845 int mViewVisibility;
6846 boolean mPolicyVisibility = true;
6847 boolean mPolicyVisibilityAfterAnim = true;
6848 boolean mAppFreezing;
6849 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006850 boolean mReportDestroySurface;
6851 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006852 boolean mAttachedHidden; // is our parent window hidden?
6853 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006854 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855 int mRequestedWidth;
6856 int mRequestedHeight;
6857 int mLastRequestedWidth;
6858 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859 int mLayer;
6860 int mAnimLayer;
6861 int mLastLayer;
6862 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006863 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006864 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865
6866 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006868 // Actual frame shown on-screen (may be modified by animation)
6869 final Rect mShownFrame = new Rect();
6870 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006872 /**
6873 * Insets that determine the actually visible area
6874 */
6875 final Rect mVisibleInsets = new Rect();
6876 final Rect mLastVisibleInsets = new Rect();
6877 boolean mVisibleInsetsChanged;
6878
6879 /**
6880 * Insets that are covered by system windows
6881 */
6882 final Rect mContentInsets = new Rect();
6883 final Rect mLastContentInsets = new Rect();
6884 boolean mContentInsetsChanged;
6885
6886 /**
6887 * Set to true if we are waiting for this window to receive its
6888 * given internal insets before laying out other windows based on it.
6889 */
6890 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006892 /**
6893 * These are the content insets that were given during layout for
6894 * this window, to be applied to windows behind it.
6895 */
6896 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006898 /**
6899 * These are the visible insets that were given during layout for
6900 * this window, to be applied to windows behind it.
6901 */
6902 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 /**
6905 * Flag indicating whether the touchable region should be adjusted by
6906 * the visible insets; if false the area outside the visible insets is
6907 * NOT touchable, so we must use those to adjust the frame during hit
6908 * tests.
6909 */
6910 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006912 // Current transformation being applied.
6913 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6914 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6915 float mHScale=1, mVScale=1;
6916 float mLastHScale=1, mLastVScale=1;
6917 final Matrix mTmpMatrix = new Matrix();
6918
6919 // "Real" frame that the application sees.
6920 final Rect mFrame = new Rect();
6921 final Rect mLastFrame = new Rect();
6922
6923 final Rect mContainingFrame = new Rect();
6924 final Rect mDisplayFrame = new Rect();
6925 final Rect mContentFrame = new Rect();
6926 final Rect mVisibleFrame = new Rect();
6927
6928 float mShownAlpha = 1;
6929 float mAlpha = 1;
6930 float mLastAlpha = 1;
6931
6932 // Set to true if, when the window gets displayed, it should perform
6933 // an enter animation.
6934 boolean mEnterAnimationPending;
6935
6936 // Currently running animation.
6937 boolean mAnimating;
6938 boolean mLocalAnimating;
6939 Animation mAnimation;
6940 boolean mAnimationIsEntrance;
6941 boolean mHasTransformation;
6942 boolean mHasLocalTransformation;
6943 final Transformation mTransformation = new Transformation();
6944
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006945 // If a window showing a wallpaper: the requested offset for the
6946 // wallpaper; if a wallpaper window: the currently applied offset.
6947 float mWallpaperX = -1;
6948 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006949
6950 // If a window showing a wallpaper: what fraction of the offset
6951 // range corresponds to a full virtual screen.
6952 float mWallpaperXStep = -1;
6953 float mWallpaperYStep = -1;
6954
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006955 // Wallpaper windows: pixels offset based on above variables.
6956 int mXOffset;
6957 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 // This is set after IWindowSession.relayout() has been called at
6960 // least once for the window. It allows us to detect the situation
6961 // where we don't yet have a surface, but should have one soon, so
6962 // we can give the window focus before waiting for the relayout.
6963 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006965 // This is set after the Surface has been created but before the
6966 // window has been drawn. During this time the surface is hidden.
6967 boolean mDrawPending;
6968
6969 // This is set after the window has finished drawing for the first
6970 // time but before its surface is shown. The surface will be
6971 // displayed when the next layout is run.
6972 boolean mCommitDrawPending;
6973
6974 // This is set during the time after the window's drawing has been
6975 // committed, and before its surface is actually shown. It is used
6976 // to delay showing the surface until all windows in a token are ready
6977 // to be shown.
6978 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006980 // Set when the window has been shown in the screen the first time.
6981 boolean mHasDrawn;
6982
6983 // Currently running an exit animation?
6984 boolean mExiting;
6985
6986 // Currently on the mDestroySurface list?
6987 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 // Completely remove from window manager after exit animation?
6990 boolean mRemoveOnExit;
6991
6992 // Set when the orientation is changing and this window has not yet
6993 // been updated for the new orientation.
6994 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 // Is this window now (or just being) removed?
6997 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 WindowState(Session s, IWindow c, WindowToken token,
7000 WindowState attachedWindow, WindowManager.LayoutParams a,
7001 int viewVisibility) {
7002 mSession = s;
7003 mClient = c;
7004 mToken = token;
7005 mAttrs.copyFrom(a);
7006 mViewVisibility = viewVisibility;
7007 DeathRecipient deathRecipient = new DeathRecipient();
7008 mAlpha = a.alpha;
7009 if (localLOGV) Log.v(
7010 TAG, "Window " + this + " client=" + c.asBinder()
7011 + " token=" + token + " (" + mAttrs.token + ")");
7012 try {
7013 c.asBinder().linkToDeath(deathRecipient, 0);
7014 } catch (RemoteException e) {
7015 mDeathRecipient = null;
7016 mAttachedWindow = null;
7017 mLayoutAttached = false;
7018 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007019 mIsWallpaper = false;
7020 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007021 mBaseLayer = 0;
7022 mSubLayer = 0;
7023 return;
7024 }
7025 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007027 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7028 mAttrs.type <= LAST_SUB_WINDOW)) {
7029 // The multiplier here is to reserve space for multiple
7030 // windows in the same type layer.
7031 mBaseLayer = mPolicy.windowTypeToLayerLw(
7032 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7033 + TYPE_LAYER_OFFSET;
7034 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7035 mAttachedWindow = attachedWindow;
7036 mAttachedWindow.mChildWindows.add(this);
7037 mLayoutAttached = mAttrs.type !=
7038 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7039 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7040 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007041 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7042 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007043 } else {
7044 // The multiplier here is to reserve space for multiple
7045 // windows in the same type layer.
7046 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7047 * TYPE_LAYER_MULTIPLIER
7048 + TYPE_LAYER_OFFSET;
7049 mSubLayer = 0;
7050 mAttachedWindow = null;
7051 mLayoutAttached = false;
7052 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7053 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007054 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7055 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007056 }
7057
7058 WindowState appWin = this;
7059 while (appWin.mAttachedWindow != null) {
7060 appWin = mAttachedWindow;
7061 }
7062 WindowToken appToken = appWin.mToken;
7063 while (appToken.appWindowToken == null) {
7064 WindowToken parent = mTokenMap.get(appToken.token);
7065 if (parent == null || appToken == parent) {
7066 break;
7067 }
7068 appToken = parent;
7069 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007070 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007071 mAppToken = appToken.appWindowToken;
7072
7073 mSurface = null;
7074 mRequestedWidth = 0;
7075 mRequestedHeight = 0;
7076 mLastRequestedWidth = 0;
7077 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007078 mXOffset = 0;
7079 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007080 mLayer = 0;
7081 mAnimLayer = 0;
7082 mLastLayer = 0;
7083 }
7084
7085 void attach() {
7086 if (localLOGV) Log.v(
7087 TAG, "Attaching " + this + " token=" + mToken
7088 + ", list=" + mToken.windows);
7089 mSession.windowAddedLocked();
7090 }
7091
7092 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7093 mHaveFrame = true;
7094
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007095 final Rect container = mContainingFrame;
7096 container.set(pf);
7097
7098 final Rect display = mDisplayFrame;
7099 display.set(df);
7100
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007101 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007102 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007103 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7104 display.intersect(mCompatibleScreenFrame);
7105 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007106 }
7107
7108 final int pw = container.right - container.left;
7109 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110
7111 int w,h;
7112 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7113 w = mAttrs.width < 0 ? pw : mAttrs.width;
7114 h = mAttrs.height< 0 ? ph : mAttrs.height;
7115 } else {
7116 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
7117 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
7118 }
Romain Guy06882f82009-06-10 13:36:04 -07007119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 final Rect content = mContentFrame;
7121 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 final Rect visible = mVisibleFrame;
7124 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007126 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007127 final int fw = frame.width();
7128 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007130 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7131 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7132
7133 Gravity.apply(mAttrs.gravity, w, h, container,
7134 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7135 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7136
7137 //System.out.println("Out: " + mFrame);
7138
7139 // Now make sure the window fits in the overall display.
7140 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007142 // Make sure the content and visible frames are inside of the
7143 // final window frame.
7144 if (content.left < frame.left) content.left = frame.left;
7145 if (content.top < frame.top) content.top = frame.top;
7146 if (content.right > frame.right) content.right = frame.right;
7147 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7148 if (visible.left < frame.left) visible.left = frame.left;
7149 if (visible.top < frame.top) visible.top = frame.top;
7150 if (visible.right > frame.right) visible.right = frame.right;
7151 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007153 final Rect contentInsets = mContentInsets;
7154 contentInsets.left = content.left-frame.left;
7155 contentInsets.top = content.top-frame.top;
7156 contentInsets.right = frame.right-content.right;
7157 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007159 final Rect visibleInsets = mVisibleInsets;
7160 visibleInsets.left = visible.left-frame.left;
7161 visibleInsets.top = visible.top-frame.top;
7162 visibleInsets.right = frame.right-visible.right;
7163 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007164
Dianne Hackborn284ac932009-08-28 10:34:25 -07007165 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7166 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007167 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007168 }
7169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007170 if (localLOGV) {
7171 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7172 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7173 Log.v(TAG, "Resolving (mRequestedWidth="
7174 + mRequestedWidth + ", mRequestedheight="
7175 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7176 + "): frame=" + mFrame.toShortString()
7177 + " ci=" + contentInsets.toShortString()
7178 + " vi=" + visibleInsets.toShortString());
7179 //}
7180 }
7181 }
Romain Guy06882f82009-06-10 13:36:04 -07007182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183 public Rect getFrameLw() {
7184 return mFrame;
7185 }
7186
7187 public Rect getShownFrameLw() {
7188 return mShownFrame;
7189 }
7190
7191 public Rect getDisplayFrameLw() {
7192 return mDisplayFrame;
7193 }
7194
7195 public Rect getContentFrameLw() {
7196 return mContentFrame;
7197 }
7198
7199 public Rect getVisibleFrameLw() {
7200 return mVisibleFrame;
7201 }
7202
7203 public boolean getGivenInsetsPendingLw() {
7204 return mGivenInsetsPending;
7205 }
7206
7207 public Rect getGivenContentInsetsLw() {
7208 return mGivenContentInsets;
7209 }
Romain Guy06882f82009-06-10 13:36:04 -07007210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007211 public Rect getGivenVisibleInsetsLw() {
7212 return mGivenVisibleInsets;
7213 }
Romain Guy06882f82009-06-10 13:36:04 -07007214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007215 public WindowManager.LayoutParams getAttrs() {
7216 return mAttrs;
7217 }
7218
7219 public int getSurfaceLayer() {
7220 return mLayer;
7221 }
Romain Guy06882f82009-06-10 13:36:04 -07007222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007223 public IApplicationToken getAppToken() {
7224 return mAppToken != null ? mAppToken.appToken : null;
7225 }
7226
7227 public boolean hasAppShownWindows() {
7228 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7229 }
7230
7231 public boolean hasAppStartingIcon() {
7232 return mAppToken != null ? (mAppToken.startingData != null) : false;
7233 }
7234
7235 public WindowManagerPolicy.WindowState getAppStartingWindow() {
7236 return mAppToken != null ? mAppToken.startingWindow : null;
7237 }
7238
7239 public void setAnimation(Animation anim) {
7240 if (localLOGV) Log.v(
7241 TAG, "Setting animation in " + this + ": " + anim);
7242 mAnimating = false;
7243 mLocalAnimating = false;
7244 mAnimation = anim;
7245 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7246 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7247 }
7248
7249 public void clearAnimation() {
7250 if (mAnimation != null) {
7251 mAnimating = true;
7252 mLocalAnimating = false;
7253 mAnimation = null;
7254 }
7255 }
Romain Guy06882f82009-06-10 13:36:04 -07007256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 Surface createSurfaceLocked() {
7258 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007259 mReportDestroySurface = false;
7260 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007261 mDrawPending = true;
7262 mCommitDrawPending = false;
7263 mReadyToShow = false;
7264 if (mAppToken != null) {
7265 mAppToken.allDrawn = false;
7266 }
7267
7268 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007269 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 flags |= Surface.PUSH_BUFFERS;
7271 }
7272
7273 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7274 flags |= Surface.SECURE;
7275 }
7276 if (DEBUG_VISIBILITY) Log.v(
7277 TAG, "Creating surface in session "
7278 + mSession.mSurfaceSession + " window " + this
7279 + " w=" + mFrame.width()
7280 + " h=" + mFrame.height() + " format="
7281 + mAttrs.format + " flags=" + flags);
7282
7283 int w = mFrame.width();
7284 int h = mFrame.height();
7285 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7286 // for a scaled surface, we always want the requested
7287 // size.
7288 w = mRequestedWidth;
7289 h = mRequestedHeight;
7290 }
7291
Romain Guy9825ec62009-10-01 00:58:09 -07007292 // Something is wrong and SurfaceFlinger will not like this,
7293 // try to revert to sane values
7294 if (w <= 0) w = 1;
7295 if (h <= 0) h = 1;
7296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007297 try {
7298 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007299 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007300 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007301 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7302 + mSurface + " IN SESSION "
7303 + mSession.mSurfaceSession
7304 + ": pid=" + mSession.mPid + " format="
7305 + mAttrs.format + " flags=0x"
7306 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007307 } catch (Surface.OutOfResourcesException e) {
7308 Log.w(TAG, "OutOfResourcesException creating surface");
7309 reclaimSomeSurfaceMemoryLocked(this, "create");
7310 return null;
7311 } catch (Exception e) {
7312 Log.e(TAG, "Exception creating surface", e);
7313 return null;
7314 }
Romain Guy06882f82009-06-10 13:36:04 -07007315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007316 if (localLOGV) Log.v(
7317 TAG, "Got surface: " + mSurface
7318 + ", set left=" + mFrame.left + " top=" + mFrame.top
7319 + ", animLayer=" + mAnimLayer);
7320 if (SHOW_TRANSACTIONS) {
7321 Log.i(TAG, ">>> OPEN TRANSACTION");
7322 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7323 + mAttrs.getTitle() + ") pos=(" +
7324 mFrame.left + "," + mFrame.top + ") (" +
7325 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7326 mAnimLayer + " HIDE");
7327 }
7328 Surface.openTransaction();
7329 try {
7330 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007331 mSurface.setPosition(mFrame.left + mXOffset,
7332 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007333 mSurface.setLayer(mAnimLayer);
7334 mSurface.hide();
7335 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007336 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7337 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007338 mSurface.setFlags(Surface.SURFACE_DITHER,
7339 Surface.SURFACE_DITHER);
7340 }
7341 } catch (RuntimeException e) {
7342 Log.w(TAG, "Error creating surface in " + w, e);
7343 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7344 }
7345 mLastHidden = true;
7346 } finally {
7347 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7348 Surface.closeTransaction();
7349 }
7350 if (localLOGV) Log.v(
7351 TAG, "Created surface " + this);
7352 }
7353 return mSurface;
7354 }
Romain Guy06882f82009-06-10 13:36:04 -07007355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007356 void destroySurfaceLocked() {
7357 // Window is no longer on-screen, so can no longer receive
7358 // key events... if we were waiting for it to finish
7359 // handling a key event, the wait is over!
7360 mKeyWaiter.finishedKey(mSession, mClient, true,
7361 KeyWaiter.RETURN_NOTHING);
7362 mKeyWaiter.releasePendingPointerLocked(mSession);
7363 mKeyWaiter.releasePendingTrackballLocked(mSession);
7364
7365 if (mAppToken != null && this == mAppToken.startingWindow) {
7366 mAppToken.startingDisplayed = false;
7367 }
Romain Guy06882f82009-06-10 13:36:04 -07007368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007370 mDrawPending = false;
7371 mCommitDrawPending = false;
7372 mReadyToShow = false;
7373
7374 int i = mChildWindows.size();
7375 while (i > 0) {
7376 i--;
7377 WindowState c = (WindowState)mChildWindows.get(i);
7378 c.mAttachedHidden = true;
7379 }
7380
7381 if (mReportDestroySurface) {
7382 mReportDestroySurface = false;
7383 mSurfacePendingDestroy = true;
7384 try {
7385 mClient.dispatchGetNewSurface();
7386 // We'll really destroy on the next time around.
7387 return;
7388 } catch (RemoteException e) {
7389 }
7390 }
7391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007393 if (DEBUG_VISIBILITY) {
7394 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007395 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007396 Log.w(TAG, "Window " + this + " destroying surface "
7397 + mSurface + ", session " + mSession, e);
7398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007399 if (SHOW_TRANSACTIONS) {
7400 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007401 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7403 + mAttrs.getTitle() + ")", ex);
7404 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007405 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007406 } catch (RuntimeException e) {
7407 Log.w(TAG, "Exception thrown when destroying Window " + this
7408 + " surface " + mSurface + " session " + mSession
7409 + ": " + e.toString());
7410 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007413 }
7414 }
7415
7416 boolean finishDrawingLocked() {
7417 if (mDrawPending) {
7418 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7419 TAG, "finishDrawingLocked: " + mSurface);
7420 mCommitDrawPending = true;
7421 mDrawPending = false;
7422 return true;
7423 }
7424 return false;
7425 }
7426
7427 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007428 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007429 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7430 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007431 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007432 }
7433 mCommitDrawPending = false;
7434 mReadyToShow = true;
7435 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7436 final AppWindowToken atoken = mAppToken;
7437 if (atoken == null || atoken.allDrawn || starting) {
7438 performShowLocked();
7439 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007440 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007441 }
7442
7443 // This must be called while inside a transaction.
7444 boolean performShowLocked() {
7445 if (DEBUG_VISIBILITY) {
7446 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007447 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 Log.v(TAG, "performShow on " + this
7449 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7450 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7451 }
7452 if (mReadyToShow && isReadyForDisplay()) {
7453 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7454 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7455 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7456 + " during animation: policyVis=" + mPolicyVisibility
7457 + " attHidden=" + mAttachedHidden
7458 + " tok.hiddenRequested="
7459 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007460 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007461 + (mAppToken != null ? mAppToken.hidden : false)
7462 + " animating=" + mAnimating
7463 + " tok animating="
7464 + (mAppToken != null ? mAppToken.animating : false));
7465 if (!showSurfaceRobustlyLocked(this)) {
7466 return false;
7467 }
7468 mLastAlpha = -1;
7469 mHasDrawn = true;
7470 mLastHidden = false;
7471 mReadyToShow = false;
7472 enableScreenIfNeededLocked();
7473
7474 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476 int i = mChildWindows.size();
7477 while (i > 0) {
7478 i--;
7479 WindowState c = (WindowState)mChildWindows.get(i);
7480 if (c.mSurface != null && c.mAttachedHidden) {
7481 c.mAttachedHidden = false;
7482 c.performShowLocked();
Dianne Hackborn9b52a212009-12-11 14:51:35 -08007483 // It hadn't been shown, which means layout not
7484 // performed on it, so now we want to make sure to
7485 // do a layout. If called from within the transaction
7486 // loop, this will cause it to restart with a new
7487 // layout.
7488 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007489 }
7490 }
Romain Guy06882f82009-06-10 13:36:04 -07007491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007492 if (mAttrs.type != TYPE_APPLICATION_STARTING
7493 && mAppToken != null) {
7494 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007495
7496 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007497 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7498 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007499 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007500 // If this initial window is animating, stop it -- we
7501 // will do an animation to reveal it from behind the
7502 // starting window, so there is no need for it to also
7503 // be doing its own stuff.
7504 if (mAnimation != null) {
7505 mAnimation = null;
7506 // Make sure we clean up the animation.
7507 mAnimating = true;
7508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 mFinishedStarting.add(mAppToken);
7510 mH.sendEmptyMessage(H.FINISHED_STARTING);
7511 }
7512 mAppToken.updateReportedVisibilityLocked();
7513 }
7514 }
7515 return true;
7516 }
Romain Guy06882f82009-06-10 13:36:04 -07007517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 // This must be called while inside a transaction. Returns true if
7519 // there is more animation to run.
7520 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7521 if (!mDisplayFrozen) {
7522 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007524 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7525 mHasTransformation = true;
7526 mHasLocalTransformation = true;
7527 if (!mLocalAnimating) {
7528 if (DEBUG_ANIM) Log.v(
7529 TAG, "Starting animation in " + this +
7530 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7531 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7532 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7533 mAnimation.setStartTime(currentTime);
7534 mLocalAnimating = true;
7535 mAnimating = true;
7536 }
7537 mTransformation.clear();
7538 final boolean more = mAnimation.getTransformation(
7539 currentTime, mTransformation);
7540 if (DEBUG_ANIM) Log.v(
7541 TAG, "Stepped animation in " + this +
7542 ": more=" + more + ", xform=" + mTransformation);
7543 if (more) {
7544 // we're not done!
7545 return true;
7546 }
7547 if (DEBUG_ANIM) Log.v(
7548 TAG, "Finished animation in " + this +
7549 " @ " + currentTime);
7550 mAnimation = null;
7551 //WindowManagerService.this.dump();
7552 }
7553 mHasLocalTransformation = false;
7554 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007555 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 // When our app token is animating, we kind-of pretend like
7557 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7558 // part of this check means that we will only do this if
7559 // our window is not currently exiting, or it is not
7560 // locally animating itself. The idea being that one that
7561 // is exiting and doing a local animation should be removed
7562 // once that animation is done.
7563 mAnimating = true;
7564 mHasTransformation = true;
7565 mTransformation.clear();
7566 return false;
7567 } else if (mHasTransformation) {
7568 // Little trick to get through the path below to act like
7569 // we have finished an animation.
7570 mAnimating = true;
7571 } else if (isAnimating()) {
7572 mAnimating = true;
7573 }
7574 } else if (mAnimation != null) {
7575 // If the display is frozen, and there is a pending animation,
7576 // clear it and make sure we run the cleanup code.
7577 mAnimating = true;
7578 mLocalAnimating = true;
7579 mAnimation = null;
7580 }
Romain Guy06882f82009-06-10 13:36:04 -07007581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007582 if (!mAnimating && !mLocalAnimating) {
7583 return false;
7584 }
7585
7586 if (DEBUG_ANIM) Log.v(
7587 TAG, "Animation done in " + this + ": exiting=" + mExiting
7588 + ", reportedVisible="
7589 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007591 mAnimating = false;
7592 mLocalAnimating = false;
7593 mAnimation = null;
7594 mAnimLayer = mLayer;
7595 if (mIsImWindow) {
7596 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007597 } else if (mIsWallpaper) {
7598 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007599 }
7600 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7601 + " anim layer: " + mAnimLayer);
7602 mHasTransformation = false;
7603 mHasLocalTransformation = false;
7604 mPolicyVisibility = mPolicyVisibilityAfterAnim;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007605 if (!mPolicyVisibility) {
7606 // Window is no longer visible -- make sure if we were waiting
7607 // for it to be displayed before enabling the display, that
7608 // we allow the display to be enabled now.
7609 enableScreenIfNeededLocked();
7610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007611 mTransformation.clear();
7612 if (mHasDrawn
7613 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7614 && mAppToken != null
7615 && mAppToken.firstWindowDrawn
7616 && mAppToken.startingData != null) {
7617 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7618 + mToken + ": first real window done animating");
7619 mFinishedStarting.add(mAppToken);
7620 mH.sendEmptyMessage(H.FINISHED_STARTING);
7621 }
Romain Guy06882f82009-06-10 13:36:04 -07007622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007623 finishExit();
7624
7625 if (mAppToken != null) {
7626 mAppToken.updateReportedVisibilityLocked();
7627 }
7628
7629 return false;
7630 }
7631
7632 void finishExit() {
7633 if (DEBUG_ANIM) Log.v(
7634 TAG, "finishExit in " + this
7635 + ": exiting=" + mExiting
7636 + " remove=" + mRemoveOnExit
7637 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007639 final int N = mChildWindows.size();
7640 for (int i=0; i<N; i++) {
7641 ((WindowState)mChildWindows.get(i)).finishExit();
7642 }
Romain Guy06882f82009-06-10 13:36:04 -07007643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007644 if (!mExiting) {
7645 return;
7646 }
Romain Guy06882f82009-06-10 13:36:04 -07007647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 if (isWindowAnimating()) {
7649 return;
7650 }
7651
7652 if (localLOGV) Log.v(
7653 TAG, "Exit animation finished in " + this
7654 + ": remove=" + mRemoveOnExit);
7655 if (mSurface != null) {
7656 mDestroySurface.add(this);
7657 mDestroying = true;
7658 if (SHOW_TRANSACTIONS) Log.i(
7659 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7660 try {
7661 mSurface.hide();
7662 } catch (RuntimeException e) {
7663 Log.w(TAG, "Error hiding surface in " + this, e);
7664 }
7665 mLastHidden = true;
7666 mKeyWaiter.releasePendingPointerLocked(mSession);
7667 }
7668 mExiting = false;
7669 if (mRemoveOnExit) {
7670 mPendingRemove.add(this);
7671 mRemoveOnExit = false;
7672 }
7673 }
Romain Guy06882f82009-06-10 13:36:04 -07007674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007675 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7676 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7677 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7678 if (dtdx < -.000001f || dtdx > .000001f) return false;
7679 if (dsdy < -.000001f || dsdy > .000001f) return false;
7680 return true;
7681 }
Romain Guy06882f82009-06-10 13:36:04 -07007682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007683 void computeShownFrameLocked() {
7684 final boolean selfTransformation = mHasLocalTransformation;
7685 Transformation attachedTransformation =
7686 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7687 ? mAttachedWindow.mTransformation : null;
7688 Transformation appTransformation =
7689 (mAppToken != null && mAppToken.hasTransformation)
7690 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007691
7692 // Wallpapers are animated based on the "real" window they
7693 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007694 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007695 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007696 if (mWallpaperTarget.mHasLocalTransformation &&
7697 mWallpaperTarget.mAnimation != null &&
7698 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007699 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007700 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7701 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7702 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007703 }
7704 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007705 mWallpaperTarget.mAppToken.hasTransformation &&
7706 mWallpaperTarget.mAppToken.animation != null &&
7707 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007708 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007709 if (DEBUG_WALLPAPER && appTransformation != null) {
7710 Log.v(TAG, "WP target app xform: " + appTransformation);
7711 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007712 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007713 }
7714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007715 if (selfTransformation || attachedTransformation != null
7716 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007717 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007718 final Rect frame = mFrame;
7719 final float tmpFloats[] = mTmpFloats;
7720 final Matrix tmpMatrix = mTmpMatrix;
7721
7722 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007723 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007724 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007725 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007726 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007727 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007728 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007729 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007730 }
7731 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007732 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007733 }
7734
7735 // "convert" it into SurfaceFlinger's format
7736 // (a 2x2 matrix + an offset)
7737 // Here we must not transform the position of the surface
7738 // since it is already included in the transformation.
7739 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 tmpMatrix.getValues(tmpFloats);
7742 mDsDx = tmpFloats[Matrix.MSCALE_X];
7743 mDtDx = tmpFloats[Matrix.MSKEW_X];
7744 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7745 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007746 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7747 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007748 int w = frame.width();
7749 int h = frame.height();
7750 mShownFrame.set(x, y, x+w, y+h);
7751
7752 // Now set the alpha... but because our current hardware
7753 // can't do alpha transformation on a non-opaque surface,
7754 // turn it off if we are running an animation that is also
7755 // transforming since it is more important to have that
7756 // animation be smooth.
7757 mShownAlpha = mAlpha;
7758 if (!mLimitedAlphaCompositing
7759 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7760 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7761 && x == frame.left && y == frame.top))) {
7762 //Log.i(TAG, "Applying alpha transform");
7763 if (selfTransformation) {
7764 mShownAlpha *= mTransformation.getAlpha();
7765 }
7766 if (attachedTransformation != null) {
7767 mShownAlpha *= attachedTransformation.getAlpha();
7768 }
7769 if (appTransformation != null) {
7770 mShownAlpha *= appTransformation.getAlpha();
7771 }
7772 } else {
7773 //Log.i(TAG, "Not applying alpha transform");
7774 }
Romain Guy06882f82009-06-10 13:36:04 -07007775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007776 if (localLOGV) Log.v(
7777 TAG, "Continuing animation in " + this +
7778 ": " + mShownFrame +
7779 ", alpha=" + mTransformation.getAlpha());
7780 return;
7781 }
Romain Guy06882f82009-06-10 13:36:04 -07007782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007784 if (mXOffset != 0 || mYOffset != 0) {
7785 mShownFrame.offset(mXOffset, mYOffset);
7786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007787 mShownAlpha = mAlpha;
7788 mDsDx = 1;
7789 mDtDx = 0;
7790 mDsDy = 0;
7791 mDtDy = 1;
7792 }
Romain Guy06882f82009-06-10 13:36:04 -07007793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007794 /**
7795 * Is this window visible? It is not visible if there is no
7796 * surface, or we are in the process of running an exit animation
7797 * that will remove the surface, or its app token has been hidden.
7798 */
7799 public boolean isVisibleLw() {
7800 final AppWindowToken atoken = mAppToken;
7801 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7802 && (atoken == null || !atoken.hiddenRequested)
7803 && !mExiting && !mDestroying;
7804 }
7805
7806 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007807 * Like {@link #isVisibleLw}, but also counts a window that is currently
7808 * "hidden" behind the keyguard as visible. This allows us to apply
7809 * things like window flags that impact the keyguard.
7810 * XXX I am starting to think we need to have ANOTHER visibility flag
7811 * for this "hidden behind keyguard" state rather than overloading
7812 * mPolicyVisibility. Ungh.
7813 */
7814 public boolean isVisibleOrBehindKeyguardLw() {
7815 final AppWindowToken atoken = mAppToken;
7816 return mSurface != null && !mAttachedHidden
7817 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7818 && !mExiting && !mDestroying;
7819 }
7820
7821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 * Is this window visible, ignoring its app token? It is not visible
7823 * if there is no surface, or we are in the process of running an exit animation
7824 * that will remove the surface.
7825 */
7826 public boolean isWinVisibleLw() {
7827 final AppWindowToken atoken = mAppToken;
7828 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7829 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7830 && !mExiting && !mDestroying;
7831 }
7832
7833 /**
7834 * The same as isVisible(), but follows the current hidden state of
7835 * the associated app token, not the pending requested hidden state.
7836 */
7837 boolean isVisibleNow() {
7838 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007839 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 }
7841
7842 /**
7843 * Same as isVisible(), but we also count it as visible between the
7844 * call to IWindowSession.add() and the first relayout().
7845 */
7846 boolean isVisibleOrAdding() {
7847 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007848 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7850 && mPolicyVisibility && !mAttachedHidden
7851 && (atoken == null || !atoken.hiddenRequested)
7852 && !mExiting && !mDestroying;
7853 }
7854
7855 /**
7856 * Is this window currently on-screen? It is on-screen either if it
7857 * is visible or it is currently running an animation before no longer
7858 * being visible.
7859 */
7860 boolean isOnScreen() {
7861 final AppWindowToken atoken = mAppToken;
7862 if (atoken != null) {
7863 return mSurface != null && mPolicyVisibility && !mDestroying
7864 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007865 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007866 } else {
7867 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007868 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 }
7870 }
Romain Guy06882f82009-06-10 13:36:04 -07007871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 /**
7873 * Like isOnScreen(), but we don't return true if the window is part
7874 * of a transition that has not yet been started.
7875 */
7876 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007877 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007878 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007879 return false;
7880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007881 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007882 final boolean animating = atoken != null
7883 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007885 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7886 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007887 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007888 }
7889
7890 /** Is the window or its container currently animating? */
7891 boolean isAnimating() {
7892 final WindowState attached = mAttachedWindow;
7893 final AppWindowToken atoken = mAppToken;
7894 return mAnimation != null
7895 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007896 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 (atoken.animation != null
7898 || atoken.inPendingTransaction));
7899 }
7900
7901 /** Is this window currently animating? */
7902 boolean isWindowAnimating() {
7903 return mAnimation != null;
7904 }
7905
7906 /**
7907 * Like isOnScreen, but returns false if the surface hasn't yet
7908 * been drawn.
7909 */
7910 public boolean isDisplayedLw() {
7911 final AppWindowToken atoken = mAppToken;
7912 return mSurface != null && mPolicyVisibility && !mDestroying
7913 && !mDrawPending && !mCommitDrawPending
7914 && ((!mAttachedHidden &&
7915 (atoken == null || !atoken.hiddenRequested))
7916 || mAnimating);
7917 }
7918
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007919 /**
7920 * Returns true if the window has a surface that it has drawn a
7921 * complete UI in to.
7922 */
7923 public boolean isDrawnLw() {
7924 final AppWindowToken atoken = mAppToken;
7925 return mSurface != null && !mDestroying
7926 && !mDrawPending && !mCommitDrawPending;
7927 }
7928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007929 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7930 boolean shownFrame, boolean onlyOpaque) {
7931 if (mSurface == null) {
7932 return false;
7933 }
7934 if (mAppToken != null && !mAppToken.appFullscreen) {
7935 return false;
7936 }
7937 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7938 return false;
7939 }
7940 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007941
7942 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7943 return frame.left <= mCompatibleScreenFrame.left &&
7944 frame.top <= mCompatibleScreenFrame.top &&
7945 frame.right >= mCompatibleScreenFrame.right &&
7946 frame.bottom >= mCompatibleScreenFrame.bottom;
7947 } else {
7948 return frame.left <= 0 && frame.top <= 0
7949 && frame.right >= screenWidth
7950 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 }
Romain Guy06882f82009-06-10 13:36:04 -07007953
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007954 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007955 * Return true if the window is opaque and fully drawn. This indicates
7956 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007957 */
7958 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007959 return (mAttrs.format == PixelFormat.OPAQUE
7960 || mAttrs.type == TYPE_WALLPAPER)
7961 && mSurface != null && mAnimation == null
7962 && (mAppToken == null || mAppToken.animation == null)
7963 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007964 }
7965
7966 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7967 return
7968 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007969 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7970 // only if it's visible
7971 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007972 // and only if the application fills the compatible screen
7973 mFrame.left <= mCompatibleScreenFrame.left &&
7974 mFrame.top <= mCompatibleScreenFrame.top &&
7975 mFrame.right >= mCompatibleScreenFrame.right &&
7976 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007977 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007978 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007979 }
7980
7981 boolean isFullscreen(int screenWidth, int screenHeight) {
7982 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007983 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007984 }
7985
7986 void removeLocked() {
7987 if (mAttachedWindow != null) {
7988 mAttachedWindow.mChildWindows.remove(this);
7989 }
7990 destroySurfaceLocked();
7991 mSession.windowRemovedLocked();
7992 try {
7993 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7994 } catch (RuntimeException e) {
7995 // Ignore if it has already been removed (usually because
7996 // we are doing this as part of processing a death note.)
7997 }
7998 }
7999
8000 private class DeathRecipient implements IBinder.DeathRecipient {
8001 public void binderDied() {
8002 try {
8003 synchronized(mWindowMap) {
8004 WindowState win = windowForClientLocked(mSession, mClient);
8005 Log.i(TAG, "WIN DEATH: " + win);
8006 if (win != null) {
8007 removeWindowLocked(mSession, win);
8008 }
8009 }
8010 } catch (IllegalArgumentException ex) {
8011 // This will happen if the window has already been
8012 // removed.
8013 }
8014 }
8015 }
8016
8017 /** Returns true if this window desires key events. */
8018 public final boolean canReceiveKeys() {
8019 return isVisibleOrAdding()
8020 && (mViewVisibility == View.VISIBLE)
8021 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8022 }
8023
8024 public boolean hasDrawnLw() {
8025 return mHasDrawn;
8026 }
8027
8028 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008029 return showLw(doAnimation, true);
8030 }
8031
8032 boolean showLw(boolean doAnimation, boolean requestAnim) {
8033 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8034 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008035 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008036 mPolicyVisibility = true;
8037 mPolicyVisibilityAfterAnim = true;
8038 if (doAnimation) {
8039 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8040 }
8041 if (requestAnim) {
8042 requestAnimationLocked(0);
8043 }
8044 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008045 }
8046
8047 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008048 return hideLw(doAnimation, true);
8049 }
8050
8051 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008052 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8053 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008054 if (!current) {
8055 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008057 if (doAnimation) {
8058 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8059 if (mAnimation == null) {
8060 doAnimation = false;
8061 }
8062 }
8063 if (doAnimation) {
8064 mPolicyVisibilityAfterAnim = false;
8065 } else {
8066 mPolicyVisibilityAfterAnim = false;
8067 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008068 // Window is no longer visible -- make sure if we were waiting
8069 // for it to be displayed before enabling the display, that
8070 // we allow the display to be enabled now.
8071 enableScreenIfNeededLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008072 }
8073 if (requestAnim) {
8074 requestAnimationLocked(0);
8075 }
8076 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008077 }
8078
8079 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008080 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07008081
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008082 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8083 pw.print(" mClient="); pw.println(mClient.asBinder());
8084 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8085 if (mAttachedWindow != null || mLayoutAttached) {
8086 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8087 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8088 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008089 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8090 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8091 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008092 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8093 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008094 }
8095 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8096 pw.print(" mSubLayer="); pw.print(mSubLayer);
8097 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8098 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8099 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8100 pw.print("="); pw.print(mAnimLayer);
8101 pw.print(" mLastLayer="); pw.println(mLastLayer);
8102 if (mSurface != null) {
8103 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8104 }
8105 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8106 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8107 if (mAppToken != null) {
8108 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8109 }
8110 if (mTargetAppToken != null) {
8111 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8112 }
8113 pw.print(prefix); pw.print("mViewVisibility=0x");
8114 pw.print(Integer.toHexString(mViewVisibility));
8115 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008116 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8117 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008118 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8119 pw.print(prefix); pw.print("mPolicyVisibility=");
8120 pw.print(mPolicyVisibility);
8121 pw.print(" mPolicyVisibilityAfterAnim=");
8122 pw.print(mPolicyVisibilityAfterAnim);
8123 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8124 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008125 if (!mRelayoutCalled) {
8126 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8127 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008128 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008129 pw.print(" h="); pw.println(mRequestedHeight);
8130 if (mXOffset != 0 || mYOffset != 0) {
8131 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8132 pw.print(" y="); pw.println(mYOffset);
8133 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008134 pw.print(prefix); pw.print("mGivenContentInsets=");
8135 mGivenContentInsets.printShortString(pw);
8136 pw.print(" mGivenVisibleInsets=");
8137 mGivenVisibleInsets.printShortString(pw);
8138 pw.println();
8139 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8140 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8141 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8142 }
8143 pw.print(prefix); pw.print("mShownFrame=");
8144 mShownFrame.printShortString(pw);
8145 pw.print(" last="); mLastShownFrame.printShortString(pw);
8146 pw.println();
8147 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8148 pw.print(" last="); mLastFrame.printShortString(pw);
8149 pw.println();
8150 pw.print(prefix); pw.print("mContainingFrame=");
8151 mContainingFrame.printShortString(pw);
8152 pw.print(" mDisplayFrame=");
8153 mDisplayFrame.printShortString(pw);
8154 pw.println();
8155 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8156 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8157 pw.println();
8158 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8159 pw.print(" last="); mLastContentInsets.printShortString(pw);
8160 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8161 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8162 pw.println();
8163 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8164 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8165 pw.print(" mAlpha="); pw.print(mAlpha);
8166 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8167 }
8168 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8169 || mAnimation != null) {
8170 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8171 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8172 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8173 pw.print(" mAnimation="); pw.println(mAnimation);
8174 }
8175 if (mHasTransformation || mHasLocalTransformation) {
8176 pw.print(prefix); pw.print("XForm: has=");
8177 pw.print(mHasTransformation);
8178 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8179 pw.print(" "); mTransformation.printShortString(pw);
8180 pw.println();
8181 }
8182 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8183 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8184 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8185 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8186 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8187 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8188 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8189 pw.print(" mDestroying="); pw.print(mDestroying);
8190 pw.print(" mRemoved="); pw.println(mRemoved);
8191 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008192 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008193 pw.print(prefix); pw.print("mOrientationChanging=");
8194 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008195 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8196 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008197 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008198 if (mHScale != 1 || mVScale != 1) {
8199 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8200 pw.print(" mVScale="); pw.println(mVScale);
8201 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008202 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008203 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8204 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8205 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008206 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8207 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8208 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 }
8211
8212 @Override
8213 public String toString() {
8214 return "Window{"
8215 + Integer.toHexString(System.identityHashCode(this))
8216 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8217 }
8218 }
Romain Guy06882f82009-06-10 13:36:04 -07008219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008220 // -------------------------------------------------------------
8221 // Window Token State
8222 // -------------------------------------------------------------
8223
8224 class WindowToken {
8225 // The actual token.
8226 final IBinder token;
8227
8228 // The type of window this token is for, as per WindowManager.LayoutParams.
8229 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008231 // Set if this token was explicitly added by a client, so should
8232 // not be removed when all windows are removed.
8233 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008234
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008235 // For printing.
8236 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 // If this is an AppWindowToken, this is non-null.
8239 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008241 // All of the windows associated with this token.
8242 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8243
8244 // Is key dispatching paused for this token?
8245 boolean paused = false;
8246
8247 // Should this token's windows be hidden?
8248 boolean hidden;
8249
8250 // Temporary for finding which tokens no longer have visible windows.
8251 boolean hasVisible;
8252
Dianne Hackborna8f60182009-09-01 19:01:50 -07008253 // Set to true when this token is in a pending transaction where it
8254 // will be shown.
8255 boolean waitingToShow;
8256
8257 // Set to true when this token is in a pending transaction where it
8258 // will be hidden.
8259 boolean waitingToHide;
8260
8261 // Set to true when this token is in a pending transaction where its
8262 // windows will be put to the bottom of the list.
8263 boolean sendingToBottom;
8264
8265 // Set to true when this token is in a pending transaction where its
8266 // windows will be put to the top of the list.
8267 boolean sendingToTop;
8268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008269 WindowToken(IBinder _token, int type, boolean _explicit) {
8270 token = _token;
8271 windowType = type;
8272 explicit = _explicit;
8273 }
8274
8275 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008276 pw.print(prefix); pw.print("token="); pw.println(token);
8277 pw.print(prefix); pw.print("windows="); pw.println(windows);
8278 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8279 pw.print(" hidden="); pw.print(hidden);
8280 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008281 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8282 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8283 pw.print(" waitingToHide="); pw.print(waitingToHide);
8284 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8285 pw.print(" sendingToTop="); pw.println(sendingToTop);
8286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008287 }
8288
8289 @Override
8290 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008291 if (stringName == null) {
8292 StringBuilder sb = new StringBuilder();
8293 sb.append("WindowToken{");
8294 sb.append(Integer.toHexString(System.identityHashCode(this)));
8295 sb.append(" token="); sb.append(token); sb.append('}');
8296 stringName = sb.toString();
8297 }
8298 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008299 }
8300 };
8301
8302 class AppWindowToken extends WindowToken {
8303 // Non-null only for application tokens.
8304 final IApplicationToken appToken;
8305
8306 // All of the windows and child windows that are included in this
8307 // application token. Note this list is NOT sorted!
8308 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8309
8310 int groupId = -1;
8311 boolean appFullscreen;
8312 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008314 // These are used for determining when all windows associated with
8315 // an activity have been drawn, so they can be made visible together
8316 // at the same time.
8317 int lastTransactionSequence = mTransactionSequence-1;
8318 int numInterestingWindows;
8319 int numDrawnWindows;
8320 boolean inPendingTransaction;
8321 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 // Is this token going to be hidden in a little while? If so, it
8324 // won't be taken into account for setting the screen orientation.
8325 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008327 // Is this window's surface needed? This is almost like hidden, except
8328 // it will sometimes be true a little earlier: when the token has
8329 // been shown, but is still waiting for its app transition to execute
8330 // before making its windows shown.
8331 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008333 // Have we told the window clients to hide themselves?
8334 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008336 // Last visibility state we reported to the app token.
8337 boolean reportedVisible;
8338
8339 // Set to true when the token has been removed from the window mgr.
8340 boolean removed;
8341
8342 // Have we been asked to have this token keep the screen frozen?
8343 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008345 boolean animating;
8346 Animation animation;
8347 boolean hasTransformation;
8348 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008350 // Offset to the window of all layers in the token, for use by
8351 // AppWindowToken animations.
8352 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 // Information about an application starting window if displayed.
8355 StartingData startingData;
8356 WindowState startingWindow;
8357 View startingView;
8358 boolean startingDisplayed;
8359 boolean startingMoved;
8360 boolean firstWindowDrawn;
8361
8362 AppWindowToken(IApplicationToken _token) {
8363 super(_token.asBinder(),
8364 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8365 appWindowToken = this;
8366 appToken = _token;
8367 }
Romain Guy06882f82009-06-10 13:36:04 -07008368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008369 public void setAnimation(Animation anim) {
8370 if (localLOGV) Log.v(
8371 TAG, "Setting animation in " + this + ": " + anim);
8372 animation = anim;
8373 animating = false;
8374 anim.restrictDuration(MAX_ANIMATION_DURATION);
8375 anim.scaleCurrentDuration(mTransitionAnimationScale);
8376 int zorder = anim.getZAdjustment();
8377 int adj = 0;
8378 if (zorder == Animation.ZORDER_TOP) {
8379 adj = TYPE_LAYER_OFFSET;
8380 } else if (zorder == Animation.ZORDER_BOTTOM) {
8381 adj = -TYPE_LAYER_OFFSET;
8382 }
Romain Guy06882f82009-06-10 13:36:04 -07008383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384 if (animLayerAdjustment != adj) {
8385 animLayerAdjustment = adj;
8386 updateLayers();
8387 }
8388 }
Romain Guy06882f82009-06-10 13:36:04 -07008389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 public void setDummyAnimation() {
8391 if (animation == null) {
8392 if (localLOGV) Log.v(
8393 TAG, "Setting dummy animation in " + this);
8394 animation = sDummyAnimation;
8395 }
8396 }
8397
8398 public void clearAnimation() {
8399 if (animation != null) {
8400 animation = null;
8401 animating = true;
8402 }
8403 }
Romain Guy06882f82009-06-10 13:36:04 -07008404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008405 void updateLayers() {
8406 final int N = allAppWindows.size();
8407 final int adj = animLayerAdjustment;
8408 for (int i=0; i<N; i++) {
8409 WindowState w = allAppWindows.get(i);
8410 w.mAnimLayer = w.mLayer + adj;
8411 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8412 + w.mAnimLayer);
8413 if (w == mInputMethodTarget) {
8414 setInputMethodAnimLayerAdjustment(adj);
8415 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008416 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008417 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008419 }
8420 }
Romain Guy06882f82009-06-10 13:36:04 -07008421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008422 void sendAppVisibilityToClients() {
8423 final int N = allAppWindows.size();
8424 for (int i=0; i<N; i++) {
8425 WindowState win = allAppWindows.get(i);
8426 if (win == startingWindow && clientHidden) {
8427 // Don't hide the starting window.
8428 continue;
8429 }
8430 try {
8431 if (DEBUG_VISIBILITY) Log.v(TAG,
8432 "Setting visibility of " + win + ": " + (!clientHidden));
8433 win.mClient.dispatchAppVisibility(!clientHidden);
8434 } catch (RemoteException e) {
8435 }
8436 }
8437 }
Romain Guy06882f82009-06-10 13:36:04 -07008438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 void showAllWindowsLocked() {
8440 final int NW = allAppWindows.size();
8441 for (int i=0; i<NW; i++) {
8442 WindowState w = allAppWindows.get(i);
8443 if (DEBUG_VISIBILITY) Log.v(TAG,
8444 "performing show on: " + w);
8445 w.performShowLocked();
8446 }
8447 }
Romain Guy06882f82009-06-10 13:36:04 -07008448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008449 // This must be called while inside a transaction.
8450 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8451 if (!mDisplayFrozen) {
8452 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008454 if (animation == sDummyAnimation) {
8455 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008456 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008457 // when it is really time to animate, this will be set to
8458 // a real animation and the next call will execute normally.
8459 return false;
8460 }
Romain Guy06882f82009-06-10 13:36:04 -07008461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008462 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8463 if (!animating) {
8464 if (DEBUG_ANIM) Log.v(
8465 TAG, "Starting animation in " + this +
8466 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8467 + " scale=" + mTransitionAnimationScale
8468 + " allDrawn=" + allDrawn + " animating=" + animating);
8469 animation.initialize(dw, dh, dw, dh);
8470 animation.setStartTime(currentTime);
8471 animating = true;
8472 }
8473 transformation.clear();
8474 final boolean more = animation.getTransformation(
8475 currentTime, transformation);
8476 if (DEBUG_ANIM) Log.v(
8477 TAG, "Stepped animation in " + this +
8478 ": more=" + more + ", xform=" + transformation);
8479 if (more) {
8480 // we're done!
8481 hasTransformation = true;
8482 return true;
8483 }
8484 if (DEBUG_ANIM) Log.v(
8485 TAG, "Finished animation in " + this +
8486 " @ " + currentTime);
8487 animation = null;
8488 }
8489 } else if (animation != null) {
8490 // If the display is frozen, and there is a pending animation,
8491 // clear it and make sure we run the cleanup code.
8492 animating = true;
8493 animation = null;
8494 }
8495
8496 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008498 if (!animating) {
8499 return false;
8500 }
8501
8502 clearAnimation();
8503 animating = false;
8504 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8505 moveInputMethodWindowsIfNeededLocked(true);
8506 }
Romain Guy06882f82009-06-10 13:36:04 -07008507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008508 if (DEBUG_ANIM) Log.v(
8509 TAG, "Animation done in " + this
8510 + ": reportedVisible=" + reportedVisible);
8511
8512 transformation.clear();
8513 if (animLayerAdjustment != 0) {
8514 animLayerAdjustment = 0;
8515 updateLayers();
8516 }
Romain Guy06882f82009-06-10 13:36:04 -07008517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008518 final int N = windows.size();
8519 for (int i=0; i<N; i++) {
8520 ((WindowState)windows.get(i)).finishExit();
8521 }
8522 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008524 return false;
8525 }
8526
8527 void updateReportedVisibilityLocked() {
8528 if (appToken == null) {
8529 return;
8530 }
Romain Guy06882f82009-06-10 13:36:04 -07008531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008532 int numInteresting = 0;
8533 int numVisible = 0;
8534 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8537 final int N = allAppWindows.size();
8538 for (int i=0; i<N; i++) {
8539 WindowState win = allAppWindows.get(i);
Maciej Białka3779dd12010-03-24 10:20:52 +01008540 if (win == startingWindow || win.mAppFreezing
8541 || win.mAttrs.type == TYPE_APPLICATION_STARTING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008542 continue;
8543 }
8544 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008545 Log.v(TAG, "Win " + win + ": isDrawn="
8546 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008547 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008548 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008549 Log.v(TAG, "Not displayed: s=" + win.mSurface
8550 + " pv=" + win.mPolicyVisibility
8551 + " dp=" + win.mDrawPending
8552 + " cdp=" + win.mCommitDrawPending
8553 + " ah=" + win.mAttachedHidden
8554 + " th="
8555 + (win.mAppToken != null
8556 ? win.mAppToken.hiddenRequested : false)
8557 + " a=" + win.mAnimating);
8558 }
8559 }
8560 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008561 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008562 if (!win.isAnimating()) {
8563 numVisible++;
8564 }
8565 nowGone = false;
8566 } else if (win.isAnimating()) {
8567 nowGone = false;
8568 }
8569 }
Romain Guy06882f82009-06-10 13:36:04 -07008570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008571 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8572 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8573 + numInteresting + " visible=" + numVisible);
8574 if (nowVisible != reportedVisible) {
8575 if (DEBUG_VISIBILITY) Log.v(
8576 TAG, "Visibility changed in " + this
8577 + ": vis=" + nowVisible);
8578 reportedVisible = nowVisible;
8579 Message m = mH.obtainMessage(
8580 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8581 nowVisible ? 1 : 0,
8582 nowGone ? 1 : 0,
8583 this);
8584 mH.sendMessage(m);
8585 }
8586 }
Romain Guy06882f82009-06-10 13:36:04 -07008587
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008588 WindowState findMainWindow() {
8589 int j = windows.size();
8590 while (j > 0) {
8591 j--;
8592 WindowState win = windows.get(j);
8593 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8594 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8595 return win;
8596 }
8597 }
8598 return null;
8599 }
8600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008601 void dump(PrintWriter pw, String prefix) {
8602 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008603 if (appToken != null) {
8604 pw.print(prefix); pw.println("app=true");
8605 }
8606 if (allAppWindows.size() > 0) {
8607 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8608 }
8609 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008610 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008611 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8612 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8613 pw.print(" clientHidden="); pw.print(clientHidden);
8614 pw.print(" willBeHidden="); pw.print(willBeHidden);
8615 pw.print(" reportedVisible="); pw.println(reportedVisible);
8616 if (paused || freezingScreen) {
8617 pw.print(prefix); pw.print("paused="); pw.print(paused);
8618 pw.print(" freezingScreen="); pw.println(freezingScreen);
8619 }
8620 if (numInterestingWindows != 0 || numDrawnWindows != 0
8621 || inPendingTransaction || allDrawn) {
8622 pw.print(prefix); pw.print("numInterestingWindows=");
8623 pw.print(numInterestingWindows);
8624 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8625 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8626 pw.print(" allDrawn="); pw.println(allDrawn);
8627 }
8628 if (animating || animation != null) {
8629 pw.print(prefix); pw.print("animating="); pw.print(animating);
8630 pw.print(" animation="); pw.println(animation);
8631 }
8632 if (animLayerAdjustment != 0) {
8633 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8634 }
8635 if (hasTransformation) {
8636 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8637 pw.print(" transformation="); transformation.printShortString(pw);
8638 pw.println();
8639 }
8640 if (startingData != null || removed || firstWindowDrawn) {
8641 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8642 pw.print(" removed="); pw.print(removed);
8643 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8644 }
8645 if (startingWindow != null || startingView != null
8646 || startingDisplayed || startingMoved) {
8647 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8648 pw.print(" startingView="); pw.print(startingView);
8649 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8650 pw.print(" startingMoved"); pw.println(startingMoved);
8651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 }
8653
8654 @Override
8655 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008656 if (stringName == null) {
8657 StringBuilder sb = new StringBuilder();
8658 sb.append("AppWindowToken{");
8659 sb.append(Integer.toHexString(System.identityHashCode(this)));
8660 sb.append(" token="); sb.append(token); sb.append('}');
8661 stringName = sb.toString();
8662 }
8663 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 }
8665 }
Romain Guy06882f82009-06-10 13:36:04 -07008666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008667 // -------------------------------------------------------------
8668 // DummyAnimation
8669 // -------------------------------------------------------------
8670
8671 // This is an animation that does nothing: it just immediately finishes
8672 // itself every time it is called. It is used as a stub animation in cases
8673 // where we want to synchronize multiple things that may be animating.
8674 static final class DummyAnimation extends Animation {
8675 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8676 return false;
8677 }
8678 }
8679 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008681 // -------------------------------------------------------------
8682 // Async Handler
8683 // -------------------------------------------------------------
8684
8685 static final class StartingData {
8686 final String pkg;
8687 final int theme;
8688 final CharSequence nonLocalizedLabel;
8689 final int labelRes;
8690 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008692 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8693 int _labelRes, int _icon) {
8694 pkg = _pkg;
8695 theme = _theme;
8696 nonLocalizedLabel = _nonLocalizedLabel;
8697 labelRes = _labelRes;
8698 icon = _icon;
8699 }
8700 }
8701
8702 private final class H extends Handler {
8703 public static final int REPORT_FOCUS_CHANGE = 2;
8704 public static final int REPORT_LOSING_FOCUS = 3;
8705 public static final int ANIMATE = 4;
8706 public static final int ADD_STARTING = 5;
8707 public static final int REMOVE_STARTING = 6;
8708 public static final int FINISHED_STARTING = 7;
8709 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008710 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8711 public static final int HOLD_SCREEN_CHANGED = 12;
8712 public static final int APP_TRANSITION_TIMEOUT = 13;
8713 public static final int PERSIST_ANIMATION_SCALE = 14;
8714 public static final int FORCE_GC = 15;
8715 public static final int ENABLE_SCREEN = 16;
8716 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008717 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008719 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008721 public H() {
8722 }
Romain Guy06882f82009-06-10 13:36:04 -07008723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008724 @Override
8725 public void handleMessage(Message msg) {
8726 switch (msg.what) {
8727 case REPORT_FOCUS_CHANGE: {
8728 WindowState lastFocus;
8729 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 synchronized(mWindowMap) {
8732 lastFocus = mLastFocus;
8733 newFocus = mCurrentFocus;
8734 if (lastFocus == newFocus) {
8735 // Focus is not changing, so nothing to do.
8736 return;
8737 }
8738 mLastFocus = newFocus;
8739 //Log.i(TAG, "Focus moving from " + lastFocus
8740 // + " to " + newFocus);
8741 if (newFocus != null && lastFocus != null
8742 && !newFocus.isDisplayedLw()) {
8743 //Log.i(TAG, "Delaying loss of focus...");
8744 mLosingFocus.add(lastFocus);
8745 lastFocus = null;
8746 }
8747 }
8748
8749 if (lastFocus != newFocus) {
8750 //System.out.println("Changing focus from " + lastFocus
8751 // + " to " + newFocus);
8752 if (newFocus != null) {
8753 try {
8754 //Log.i(TAG, "Gaining focus: " + newFocus);
8755 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8756 } catch (RemoteException e) {
8757 // Ignore if process has died.
8758 }
8759 }
8760
8761 if (lastFocus != null) {
8762 try {
8763 //Log.i(TAG, "Losing focus: " + lastFocus);
8764 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8765 } catch (RemoteException e) {
8766 // Ignore if process has died.
8767 }
8768 }
8769 }
8770 } break;
8771
8772 case REPORT_LOSING_FOCUS: {
8773 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008775 synchronized(mWindowMap) {
8776 losers = mLosingFocus;
8777 mLosingFocus = new ArrayList<WindowState>();
8778 }
8779
8780 final int N = losers.size();
8781 for (int i=0; i<N; i++) {
8782 try {
8783 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8784 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8785 } catch (RemoteException e) {
8786 // Ignore if process has died.
8787 }
8788 }
8789 } break;
8790
8791 case ANIMATE: {
8792 synchronized(mWindowMap) {
8793 mAnimationPending = false;
8794 performLayoutAndPlaceSurfacesLocked();
8795 }
8796 } break;
8797
8798 case ADD_STARTING: {
8799 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8800 final StartingData sd = wtoken.startingData;
8801
8802 if (sd == null) {
8803 // Animation has been canceled... do nothing.
8804 return;
8805 }
Romain Guy06882f82009-06-10 13:36:04 -07008806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008807 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8808 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008810 View view = null;
8811 try {
8812 view = mPolicy.addStartingWindow(
8813 wtoken.token, sd.pkg,
8814 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8815 sd.icon);
8816 } catch (Exception e) {
8817 Log.w(TAG, "Exception when adding starting window", e);
8818 }
8819
8820 if (view != null) {
8821 boolean abort = false;
8822
8823 synchronized(mWindowMap) {
8824 if (wtoken.removed || wtoken.startingData == null) {
8825 // If the window was successfully added, then
8826 // we need to remove it.
8827 if (wtoken.startingWindow != null) {
8828 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8829 "Aborted starting " + wtoken
8830 + ": removed=" + wtoken.removed
8831 + " startingData=" + wtoken.startingData);
8832 wtoken.startingWindow = null;
8833 wtoken.startingData = null;
8834 abort = true;
8835 }
8836 } else {
8837 wtoken.startingView = view;
8838 }
8839 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8840 "Added starting " + wtoken
8841 + ": startingWindow="
8842 + wtoken.startingWindow + " startingView="
8843 + wtoken.startingView);
8844 }
8845
8846 if (abort) {
8847 try {
8848 mPolicy.removeStartingWindow(wtoken.token, view);
8849 } catch (Exception e) {
8850 Log.w(TAG, "Exception when removing starting window", e);
8851 }
8852 }
8853 }
8854 } break;
8855
8856 case REMOVE_STARTING: {
8857 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8858 IBinder token = null;
8859 View view = null;
8860 synchronized (mWindowMap) {
8861 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8862 + wtoken + ": startingWindow="
8863 + wtoken.startingWindow + " startingView="
8864 + wtoken.startingView);
8865 if (wtoken.startingWindow != null) {
8866 view = wtoken.startingView;
8867 token = wtoken.token;
8868 wtoken.startingData = null;
8869 wtoken.startingView = null;
8870 wtoken.startingWindow = null;
8871 }
8872 }
8873 if (view != null) {
8874 try {
8875 mPolicy.removeStartingWindow(token, view);
8876 } catch (Exception e) {
8877 Log.w(TAG, "Exception when removing starting window", e);
8878 }
8879 }
8880 } break;
8881
8882 case FINISHED_STARTING: {
8883 IBinder token = null;
8884 View view = null;
8885 while (true) {
8886 synchronized (mWindowMap) {
8887 final int N = mFinishedStarting.size();
8888 if (N <= 0) {
8889 break;
8890 }
8891 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8892
8893 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8894 "Finished starting " + wtoken
8895 + ": startingWindow=" + wtoken.startingWindow
8896 + " startingView=" + wtoken.startingView);
8897
8898 if (wtoken.startingWindow == null) {
8899 continue;
8900 }
8901
8902 view = wtoken.startingView;
8903 token = wtoken.token;
8904 wtoken.startingData = null;
8905 wtoken.startingView = null;
8906 wtoken.startingWindow = null;
8907 }
8908
8909 try {
8910 mPolicy.removeStartingWindow(token, view);
8911 } catch (Exception e) {
8912 Log.w(TAG, "Exception when removing starting window", e);
8913 }
8914 }
8915 } break;
8916
8917 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8918 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8919
8920 boolean nowVisible = msg.arg1 != 0;
8921 boolean nowGone = msg.arg2 != 0;
8922
8923 try {
8924 if (DEBUG_VISIBILITY) Log.v(
8925 TAG, "Reporting visible in " + wtoken
8926 + " visible=" + nowVisible
8927 + " gone=" + nowGone);
8928 if (nowVisible) {
8929 wtoken.appToken.windowsVisible();
8930 } else {
8931 wtoken.appToken.windowsGone();
8932 }
8933 } catch (RemoteException ex) {
8934 }
8935 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937 case WINDOW_FREEZE_TIMEOUT: {
8938 synchronized (mWindowMap) {
8939 Log.w(TAG, "Window freeze timeout expired.");
8940 int i = mWindows.size();
8941 while (i > 0) {
8942 i--;
8943 WindowState w = (WindowState)mWindows.get(i);
8944 if (w.mOrientationChanging) {
8945 w.mOrientationChanging = false;
8946 Log.w(TAG, "Force clearing orientation change: " + w);
8947 }
8948 }
8949 performLayoutAndPlaceSurfacesLocked();
8950 }
8951 break;
8952 }
Romain Guy06882f82009-06-10 13:36:04 -07008953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008954 case HOLD_SCREEN_CHANGED: {
8955 Session oldHold;
8956 Session newHold;
8957 synchronized (mWindowMap) {
8958 oldHold = mLastReportedHold;
8959 newHold = (Session)msg.obj;
8960 mLastReportedHold = newHold;
8961 }
Romain Guy06882f82009-06-10 13:36:04 -07008962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008963 if (oldHold != newHold) {
8964 try {
8965 if (oldHold != null) {
8966 mBatteryStats.noteStopWakelock(oldHold.mUid,
8967 "window",
8968 BatteryStats.WAKE_TYPE_WINDOW);
8969 }
8970 if (newHold != null) {
8971 mBatteryStats.noteStartWakelock(newHold.mUid,
8972 "window",
8973 BatteryStats.WAKE_TYPE_WINDOW);
8974 }
8975 } catch (RemoteException e) {
8976 }
8977 }
8978 break;
8979 }
Romain Guy06882f82009-06-10 13:36:04 -07008980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008981 case APP_TRANSITION_TIMEOUT: {
8982 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008983 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008984 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8985 "*** APP TRANSITION TIMEOUT");
8986 mAppTransitionReady = true;
8987 mAppTransitionTimeout = true;
8988 performLayoutAndPlaceSurfacesLocked();
8989 }
8990 }
8991 break;
8992 }
Romain Guy06882f82009-06-10 13:36:04 -07008993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008994 case PERSIST_ANIMATION_SCALE: {
8995 Settings.System.putFloat(mContext.getContentResolver(),
8996 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8997 Settings.System.putFloat(mContext.getContentResolver(),
8998 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8999 break;
9000 }
Romain Guy06882f82009-06-10 13:36:04 -07009001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009002 case FORCE_GC: {
9003 synchronized(mWindowMap) {
9004 if (mAnimationPending) {
9005 // If we are animating, don't do the gc now but
9006 // delay a bit so we don't interrupt the animation.
9007 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9008 2000);
9009 return;
9010 }
9011 // If we are currently rotating the display, it will
9012 // schedule a new message when done.
9013 if (mDisplayFrozen) {
9014 return;
9015 }
9016 mFreezeGcPending = 0;
9017 }
9018 Runtime.getRuntime().gc();
9019 break;
9020 }
Romain Guy06882f82009-06-10 13:36:04 -07009021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009022 case ENABLE_SCREEN: {
9023 performEnableScreen();
9024 break;
9025 }
Romain Guy06882f82009-06-10 13:36:04 -07009026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009027 case APP_FREEZE_TIMEOUT: {
9028 synchronized (mWindowMap) {
9029 Log.w(TAG, "App freeze timeout expired.");
9030 int i = mAppTokens.size();
9031 while (i > 0) {
9032 i--;
9033 AppWindowToken tok = mAppTokens.get(i);
9034 if (tok.freezingScreen) {
9035 Log.w(TAG, "Force clearing freeze: " + tok);
9036 unsetAppFreezingScreenLocked(tok, true, true);
9037 }
9038 }
9039 }
9040 break;
9041 }
Romain Guy06882f82009-06-10 13:36:04 -07009042
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009043 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07009044 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009045 sendNewConfiguration();
9046 }
9047 break;
9048 }
Romain Guy06882f82009-06-10 13:36:04 -07009049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009050 }
9051 }
9052 }
9053
9054 // -------------------------------------------------------------
9055 // IWindowManager API
9056 // -------------------------------------------------------------
9057
9058 public IWindowSession openSession(IInputMethodClient client,
9059 IInputContext inputContext) {
9060 if (client == null) throw new IllegalArgumentException("null client");
9061 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9062 return new Session(client, inputContext);
9063 }
9064
9065 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9066 synchronized (mWindowMap) {
9067 // The focus for the client is the window immediately below
9068 // where we would place the input method window.
9069 int idx = findDesiredInputMethodWindowIndexLocked(false);
9070 WindowState imFocus;
9071 if (idx > 0) {
9072 imFocus = (WindowState)mWindows.get(idx-1);
9073 if (imFocus != null) {
9074 if (imFocus.mSession.mClient != null &&
9075 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9076 return true;
9077 }
9078 }
9079 }
9080 }
9081 return false;
9082 }
Romain Guy06882f82009-06-10 13:36:04 -07009083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009084 // -------------------------------------------------------------
9085 // Internals
9086 // -------------------------------------------------------------
9087
9088 final WindowState windowForClientLocked(Session session, IWindow client) {
9089 return windowForClientLocked(session, client.asBinder());
9090 }
Romain Guy06882f82009-06-10 13:36:04 -07009091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009092 final WindowState windowForClientLocked(Session session, IBinder client) {
9093 WindowState win = mWindowMap.get(client);
9094 if (localLOGV) Log.v(
9095 TAG, "Looking up client " + client + ": " + win);
9096 if (win == null) {
9097 RuntimeException ex = new RuntimeException();
9098 Log.w(TAG, "Requested window " + client + " does not exist", ex);
9099 return null;
9100 }
9101 if (session != null && win.mSession != session) {
9102 RuntimeException ex = new RuntimeException();
9103 Log.w(TAG, "Requested window " + client + " is in session " +
9104 win.mSession + ", not " + session, ex);
9105 return null;
9106 }
9107
9108 return win;
9109 }
9110
Dianne Hackborna8f60182009-09-01 19:01:50 -07009111 final void rebuildAppWindowListLocked() {
9112 int NW = mWindows.size();
9113 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009114 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009115 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009116
9117 // First remove all existing app windows.
9118 i=0;
9119 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009120 WindowState w = (WindowState)mWindows.get(i);
9121 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009122 WindowState win = (WindowState)mWindows.remove(i);
9123 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
9124 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009125 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009126 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009127 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009128 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9129 && lastWallpaper == i-1) {
9130 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009131 }
9132 i++;
9133 }
9134
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009135 // The wallpaper window(s) typically live at the bottom of the stack,
9136 // so skip them before adding app tokens.
9137 lastWallpaper++;
9138 i = lastWallpaper;
9139
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009140 // First add all of the exiting app tokens... these are no longer
9141 // in the main app list, but still have windows shown. We put them
9142 // in the back because now that the animation is over we no longer
9143 // will care about them.
9144 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009145 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009146 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9147 }
9148
9149 // And add in the still active app tokens in Z order.
9150 NT = mAppTokens.size();
9151 for (int j=0; j<NT; j++) {
9152 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009153 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009154
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009155 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009156 if (i != numRemoved) {
9157 Log.w(TAG, "Rebuild removed " + numRemoved
9158 + " windows but added " + i);
9159 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009160 }
9161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009162 private final void assignLayersLocked() {
9163 int N = mWindows.size();
9164 int curBaseLayer = 0;
9165 int curLayer = 0;
9166 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009168 for (i=0; i<N; i++) {
9169 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009170 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9171 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009172 curLayer += WINDOW_LAYER_MULTIPLIER;
9173 w.mLayer = curLayer;
9174 } else {
9175 curBaseLayer = curLayer = w.mBaseLayer;
9176 w.mLayer = curLayer;
9177 }
9178 if (w.mTargetAppToken != null) {
9179 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9180 } else if (w.mAppToken != null) {
9181 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9182 } else {
9183 w.mAnimLayer = w.mLayer;
9184 }
9185 if (w.mIsImWindow) {
9186 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009187 } else if (w.mIsWallpaper) {
9188 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009189 }
9190 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9191 + w.mAnimLayer);
9192 //System.out.println(
9193 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9194 }
9195 }
9196
9197 private boolean mInLayout = false;
9198 private final void performLayoutAndPlaceSurfacesLocked() {
9199 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009200 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009201 throw new RuntimeException("Recursive call!");
9202 }
9203 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9204 return;
9205 }
9206
9207 boolean recoveringMemory = false;
9208 if (mForceRemoves != null) {
9209 recoveringMemory = true;
9210 // Wait a little it for things to settle down, and off we go.
9211 for (int i=0; i<mForceRemoves.size(); i++) {
9212 WindowState ws = mForceRemoves.get(i);
9213 Log.i(TAG, "Force removing: " + ws);
9214 removeWindowInnerLocked(ws.mSession, ws);
9215 }
9216 mForceRemoves = null;
9217 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9218 Object tmp = new Object();
9219 synchronized (tmp) {
9220 try {
9221 tmp.wait(250);
9222 } catch (InterruptedException e) {
9223 }
9224 }
9225 }
Romain Guy06882f82009-06-10 13:36:04 -07009226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009227 mInLayout = true;
9228 try {
9229 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009231 int i = mPendingRemove.size()-1;
9232 if (i >= 0) {
9233 while (i >= 0) {
9234 WindowState w = mPendingRemove.get(i);
9235 removeWindowInnerLocked(w.mSession, w);
9236 i--;
9237 }
9238 mPendingRemove.clear();
9239
9240 mInLayout = false;
9241 assignLayersLocked();
9242 mLayoutNeeded = true;
9243 performLayoutAndPlaceSurfacesLocked();
9244
9245 } else {
9246 mInLayout = false;
9247 if (mLayoutNeeded) {
9248 requestAnimationLocked(0);
9249 }
9250 }
9251 } catch (RuntimeException e) {
9252 mInLayout = false;
9253 Log.e(TAG, "Unhandled exception while layout out windows", e);
9254 }
9255 }
9256
9257 private final void performLayoutLockedInner() {
9258 final int dw = mDisplay.getWidth();
9259 final int dh = mDisplay.getHeight();
9260
9261 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009262 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009263 int i;
9264
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009265 if (DEBUG_LAYOUT) Log.v(TAG, "performLayout: needed="
9266 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009268 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009269
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009270 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009271 mPolicy.beginLayoutLw(dw, dh);
9272
9273 // First perform layout of any root windows (not attached
9274 // to another window).
9275 int topAttached = -1;
9276 for (i = N-1; i >= 0; i--) {
9277 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009278
9279 // Don't do layout of a window if it is not visible, or
9280 // soon won't be visible, to avoid wasting time and funky
9281 // changes while a window is animating away.
9282 final AppWindowToken atoken = win.mAppToken;
9283 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009284 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009285 || win.mRootToken.hidden
9286 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009287 || win.mAttachedHidden
9288 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009289
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009290 if (win.mLayoutAttached) {
9291 if (DEBUG_LAYOUT) Log.v(TAG, "First pass " + win
9292 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9293 + " mLayoutAttached=" + win.mLayoutAttached);
9294 if (DEBUG_LAYOUT && gone) Log.v(TAG, " (mViewVisibility="
9295 + win.mViewVisibility + " mRelayoutCalled="
9296 + win.mRelayoutCalled + " hidden="
9297 + win.mRootToken.hidden + " hiddenRequested="
9298 + (atoken != null && atoken.hiddenRequested)
9299 + " mAttachedHidden=" + win.mAttachedHidden);
9300 }
9301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009302 // If this view is GONE, then skip it -- keep the current
9303 // frame, and let the caller know so they can ignore it
9304 // if they want. (We do the normal layout for INVISIBLE
9305 // windows, since that means "perform layout as normal,
9306 // just don't display").
9307 if (!gone || !win.mHaveFrame) {
9308 if (!win.mLayoutAttached) {
9309 mPolicy.layoutWindowLw(win, win.mAttrs, null);
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009310 if (DEBUG_LAYOUT) Log.v(TAG, "-> mFrame="
9311 + win.mFrame + " mContainingFrame="
9312 + win.mContainingFrame + " mDisplayFrame="
9313 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009314 } else {
9315 if (topAttached < 0) topAttached = i;
9316 }
9317 }
9318 }
Romain Guy06882f82009-06-10 13:36:04 -07009319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009320 // Now perform layout of attached windows, which usually
9321 // depend on the position of the window they are attached to.
9322 // XXX does not deal with windows that are attached to windows
9323 // that are themselves attached.
9324 for (i = topAttached; i >= 0; i--) {
9325 WindowState win = (WindowState) mWindows.get(i);
9326
9327 // If this view is GONE, then skip it -- keep the current
9328 // frame, and let the caller know so they can ignore it
9329 // if they want. (We do the normal layout for INVISIBLE
9330 // windows, since that means "perform layout as normal,
9331 // just don't display").
9332 if (win.mLayoutAttached) {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009333 if (DEBUG_LAYOUT) Log.v(TAG, "Second pass " + win
9334 + " mHaveFrame=" + win.mHaveFrame
9335 + " mViewVisibility=" + win.mViewVisibility
9336 + " mRelayoutCalled=" + win.mRelayoutCalled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009337 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9338 || !win.mHaveFrame) {
9339 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009340 if (DEBUG_LAYOUT) Log.v(TAG, "-> mFrame="
9341 + win.mFrame + " mContainingFrame="
9342 + win.mContainingFrame + " mDisplayFrame="
9343 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009344 }
9345 }
9346 }
9347
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009348 int changes = mPolicy.finishLayoutLw();
9349 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9350 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9351 assignLayersLocked();
9352 }
9353 }
9354 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009355 mLayoutNeeded = false;
9356 } else if (repeats > 2) {
9357 Log.w(TAG, "Layout repeat aborted after too many iterations");
9358 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009359 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9360 Configuration newConfig = updateOrientationFromAppTokensLocked(
9361 null, null);
9362 if (newConfig != null) {
9363 mLayoutNeeded = true;
9364 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9365 }
9366 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009367 } else {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009368 if (DEBUG_LAYOUT) Log.v(TAG, "Repeating layout because changes=0x"
9369 + Integer.toHexString(changes));
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009370 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009371 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009372 if (DEBUG_LAYOUT) Log.v(TAG, "Computing new config from layout");
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009373 Configuration newConfig = updateOrientationFromAppTokensLocked(
9374 null, null);
9375 if (newConfig != null) {
9376 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9377 }
9378 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009380 }
9381 }
Romain Guy06882f82009-06-10 13:36:04 -07009382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009383 private final void performLayoutAndPlaceSurfacesLockedInner(
9384 boolean recoveringMemory) {
9385 final long currentTime = SystemClock.uptimeMillis();
9386 final int dw = mDisplay.getWidth();
9387 final int dh = mDisplay.getHeight();
9388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009389 int i;
9390
9391 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009392 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394 if (mFxSession == null) {
9395 mFxSession = new SurfaceSession();
9396 }
Romain Guy06882f82009-06-10 13:36:04 -07009397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009398 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9399
9400 // Initialize state of exiting tokens.
9401 for (i=mExitingTokens.size()-1; i>=0; i--) {
9402 mExitingTokens.get(i).hasVisible = false;
9403 }
9404
9405 // Initialize state of exiting applications.
9406 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9407 mExitingAppTokens.get(i).hasVisible = false;
9408 }
9409
9410 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009411 boolean orientationChangeComplete = true;
9412 Session holdScreen = null;
9413 float screenBrightness = -1;
9414 boolean focusDisplayed = false;
9415 boolean animating = false;
9416
9417 Surface.openTransaction();
9418 try {
9419 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009420 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009421
9422 do {
9423 final int transactionSequence = ++mTransactionSequence;
9424
9425 // Update animations of all applications, including those
9426 // associated with exiting/removed apps
9427 boolean tokensAnimating = false;
9428 final int NAT = mAppTokens.size();
9429 for (i=0; i<NAT; i++) {
9430 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9431 tokensAnimating = true;
9432 }
9433 }
9434 final int NEAT = mExitingAppTokens.size();
9435 for (i=0; i<NEAT; i++) {
9436 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9437 tokensAnimating = true;
9438 }
9439 }
9440
9441 animating = tokensAnimating;
9442 restart = false;
9443
9444 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009445 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009446 boolean focusMayChange = false;
9447 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009448
9449 mPolicy.beginAnimationLw(dw, dh);
9450
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009451 final int N = mWindows.size();
9452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009453 for (i=N-1; i>=0; i--) {
9454 WindowState w = (WindowState)mWindows.get(i);
9455
9456 final WindowManager.LayoutParams attrs = w.mAttrs;
9457
9458 if (w.mSurface != null) {
9459 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009460 if (w.commitFinishDrawingLocked(currentTime)) {
9461 if ((w.mAttrs.flags
9462 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009463 if (DEBUG_WALLPAPER) Log.v(TAG,
9464 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009465 wallpaperMayChange = true;
9466 }
9467 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009468
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009469 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9471 animating = true;
9472 //w.dump(" ");
9473 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009474 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9475 wallpaperMayChange = true;
9476 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009477
9478 if (mPolicy.doesForceHide(w, attrs)) {
9479 if (!wasAnimating && animating) {
9480 wallpaperForceHidingChanged = true;
9481 focusMayChange = true;
9482 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9483 forceHiding = true;
9484 }
9485 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9486 boolean changed;
9487 if (forceHiding) {
9488 changed = w.hideLw(false, false);
9489 } else {
9490 changed = w.showLw(false, false);
9491 if (changed && wallpaperForceHidingChanged
9492 && w.isReadyForDisplay()) {
9493 // Assume we will need to animate. If
9494 // we don't (because the wallpaper will
9495 // stay with the lock screen), then we will
9496 // clean up later.
9497 Animation a = mPolicy.createForceHideEnterAnimation();
9498 if (a != null) {
9499 w.setAnimation(a);
9500 }
9501 }
9502 }
9503 if (changed && (attrs.flags
9504 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9505 wallpaperMayChange = true;
9506 }
Christopher Tate405d0892009-10-27 20:23:28 -07009507 if (changed && !forceHiding
9508 && (mCurrentFocus == null)
9509 && (mFocusedApp != null)) {
9510 // It's possible that the last focus recalculation left no
9511 // current focused window even though the app has come to the
9512 // foreground already. In this case, we make sure to recalculate
9513 // focus when we show a window.
9514 focusMayChange = true;
9515 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009516 }
9517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009518 mPolicy.animatingWindowLw(w, attrs);
9519 }
9520
9521 final AppWindowToken atoken = w.mAppToken;
9522 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9523 if (atoken.lastTransactionSequence != transactionSequence) {
9524 atoken.lastTransactionSequence = transactionSequence;
9525 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9526 atoken.startingDisplayed = false;
9527 }
9528 if ((w.isOnScreen() || w.mAttrs.type
9529 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9530 && !w.mExiting && !w.mDestroying) {
9531 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009532 Log.v(TAG, "Eval win " + w + ": isDrawn="
9533 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009534 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009535 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009536 Log.v(TAG, "Not displayed: s=" + w.mSurface
9537 + " pv=" + w.mPolicyVisibility
9538 + " dp=" + w.mDrawPending
9539 + " cdp=" + w.mCommitDrawPending
9540 + " ah=" + w.mAttachedHidden
9541 + " th=" + atoken.hiddenRequested
9542 + " a=" + w.mAnimating);
9543 }
9544 }
9545 if (w != atoken.startingWindow) {
9546 if (!atoken.freezingScreen || !w.mAppFreezing) {
9547 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009548 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009549 atoken.numDrawnWindows++;
9550 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9551 "tokenMayBeDrawn: " + atoken
9552 + " freezingScreen=" + atoken.freezingScreen
9553 + " mAppFreezing=" + w.mAppFreezing);
9554 tokenMayBeDrawn = true;
9555 }
9556 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009557 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009558 atoken.startingDisplayed = true;
9559 }
9560 }
9561 } else if (w.mReadyToShow) {
9562 w.performShowLocked();
9563 }
9564 }
9565
9566 if (mPolicy.finishAnimationLw()) {
9567 restart = true;
9568 }
9569
9570 if (tokenMayBeDrawn) {
9571 // See if any windows have been drawn, so they (and others
9572 // associated with them) can now be shown.
9573 final int NT = mTokenList.size();
9574 for (i=0; i<NT; i++) {
9575 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9576 if (wtoken == null) {
9577 continue;
9578 }
9579 if (wtoken.freezingScreen) {
9580 int numInteresting = wtoken.numInterestingWindows;
9581 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9582 if (DEBUG_VISIBILITY) Log.v(TAG,
9583 "allDrawn: " + wtoken
9584 + " interesting=" + numInteresting
9585 + " drawn=" + wtoken.numDrawnWindows);
9586 wtoken.showAllWindowsLocked();
9587 unsetAppFreezingScreenLocked(wtoken, false, true);
9588 orientationChangeComplete = true;
9589 }
9590 } else if (!wtoken.allDrawn) {
9591 int numInteresting = wtoken.numInterestingWindows;
9592 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9593 if (DEBUG_VISIBILITY) Log.v(TAG,
9594 "allDrawn: " + wtoken
9595 + " interesting=" + numInteresting
9596 + " drawn=" + wtoken.numDrawnWindows);
9597 wtoken.allDrawn = true;
9598 restart = true;
9599
9600 // We can now show all of the drawn windows!
9601 if (!mOpeningApps.contains(wtoken)) {
9602 wtoken.showAllWindowsLocked();
9603 }
9604 }
9605 }
9606 }
9607 }
9608
9609 // If we are ready to perform an app transition, check through
9610 // all of the app tokens to be shown and see if they are ready
9611 // to go.
9612 if (mAppTransitionReady) {
9613 int NN = mOpeningApps.size();
9614 boolean goodToGo = true;
9615 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9616 "Checking " + NN + " opening apps (frozen="
9617 + mDisplayFrozen + " timeout="
9618 + mAppTransitionTimeout + ")...");
9619 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9620 // If the display isn't frozen, wait to do anything until
9621 // all of the apps are ready. Otherwise just go because
9622 // we'll unfreeze the display when everyone is ready.
9623 for (i=0; i<NN && goodToGo; i++) {
9624 AppWindowToken wtoken = mOpeningApps.get(i);
9625 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9626 "Check opening app" + wtoken + ": allDrawn="
9627 + wtoken.allDrawn + " startingDisplayed="
9628 + wtoken.startingDisplayed);
9629 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9630 && !wtoken.startingMoved) {
9631 goodToGo = false;
9632 }
9633 }
9634 }
9635 if (goodToGo) {
9636 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9637 int transit = mNextAppTransition;
9638 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009639 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009640 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009641 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009642 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009643 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009644 mAppTransitionTimeout = false;
9645 mStartingIconInTransition = false;
9646 mSkipAppTransitionAnimation = false;
9647
9648 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9649
Dianne Hackborna8f60182009-09-01 19:01:50 -07009650 // If there are applications waiting to come to the
9651 // top of the stack, now is the time to move their windows.
9652 // (Note that we don't do apps going to the bottom
9653 // here -- we want to keep their windows in the old
9654 // Z-order until the animation completes.)
9655 if (mToTopApps.size() > 0) {
9656 NN = mAppTokens.size();
9657 for (i=0; i<NN; i++) {
9658 AppWindowToken wtoken = mAppTokens.get(i);
9659 if (wtoken.sendingToTop) {
9660 wtoken.sendingToTop = false;
9661 moveAppWindowsLocked(wtoken, NN, false);
9662 }
9663 }
9664 mToTopApps.clear();
9665 }
9666
Dianne Hackborn25994b42009-09-04 14:21:19 -07009667 WindowState oldWallpaper = mWallpaperTarget;
9668
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009669 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009670 wallpaperMayChange = false;
9671
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009672 // The top-most window will supply the layout params,
9673 // and we will determine it below.
9674 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009675 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009676 int bestAnimLayer = -1;
9677
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009678 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009679 "New wallpaper target=" + mWallpaperTarget
9680 + ", lower target=" + mLowerWallpaperTarget
9681 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009682 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009683 // Do a first pass through the tokens for two
9684 // things:
9685 // (1) Determine if both the closing and opening
9686 // app token sets are wallpaper targets, in which
9687 // case special animations are needed
9688 // (since the wallpaper needs to stay static
9689 // behind them).
9690 // (2) Find the layout params of the top-most
9691 // application window in the tokens, which is
9692 // what will control the animation theme.
9693 final int NC = mClosingApps.size();
9694 NN = NC + mOpeningApps.size();
9695 for (i=0; i<NN; i++) {
9696 AppWindowToken wtoken;
9697 int mode;
9698 if (i < NC) {
9699 wtoken = mClosingApps.get(i);
9700 mode = 1;
9701 } else {
9702 wtoken = mOpeningApps.get(i-NC);
9703 mode = 2;
9704 }
9705 if (mLowerWallpaperTarget != null) {
9706 if (mLowerWallpaperTarget.mAppToken == wtoken
9707 || mUpperWallpaperTarget.mAppToken == wtoken) {
9708 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009709 }
9710 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009711 if (wtoken.appFullscreen) {
9712 WindowState ws = wtoken.findMainWindow();
9713 if (ws != null) {
9714 // If this is a compatibility mode
9715 // window, we will always use its anim.
9716 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9717 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009718 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009719 bestAnimLayer = Integer.MAX_VALUE;
9720 } else if (ws.mLayer > bestAnimLayer) {
9721 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009722 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009723 bestAnimLayer = ws.mLayer;
9724 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009725 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009726 }
9727 }
9728
Dianne Hackborn25994b42009-09-04 14:21:19 -07009729 if (foundWallpapers == 3) {
9730 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9731 "Wallpaper animation!");
9732 switch (transit) {
9733 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9734 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9735 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9736 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9737 break;
9738 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9739 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9740 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9741 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9742 break;
9743 }
9744 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9745 "New transit: " + transit);
9746 } else if (oldWallpaper != null) {
9747 // We are transitioning from an activity with
9748 // a wallpaper to one without.
9749 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9750 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9751 "New transit away from wallpaper: " + transit);
9752 } else if (mWallpaperTarget != null) {
9753 // We are transitioning from an activity without
9754 // a wallpaper to now showing the wallpaper
9755 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9756 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9757 "New transit into wallpaper: " + transit);
9758 }
9759
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009760 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9761 mLastEnterAnimToken = animToken;
9762 mLastEnterAnimParams = animLp;
9763 } else if (mLastEnterAnimParams != null) {
9764 animLp = mLastEnterAnimParams;
9765 mLastEnterAnimToken = null;
9766 mLastEnterAnimParams = null;
9767 }
9768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009769 NN = mOpeningApps.size();
9770 for (i=0; i<NN; i++) {
9771 AppWindowToken wtoken = mOpeningApps.get(i);
9772 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9773 "Now opening app" + wtoken);
9774 wtoken.reportedVisible = false;
9775 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009776 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009777 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009778 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009779 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780 wtoken.showAllWindowsLocked();
9781 }
9782 NN = mClosingApps.size();
9783 for (i=0; i<NN; i++) {
9784 AppWindowToken wtoken = mClosingApps.get(i);
9785 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9786 "Now closing app" + wtoken);
9787 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009788 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009789 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009790 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009791 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009792 // Force the allDrawn flag, because we want to start
9793 // this guy's animations regardless of whether it's
9794 // gotten drawn.
9795 wtoken.allDrawn = true;
9796 }
9797
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009798 mNextAppTransitionPackage = null;
9799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009800 mOpeningApps.clear();
9801 mClosingApps.clear();
9802
9803 // This has changed the visibility of windows, so perform
9804 // a new layout to get them all up-to-date.
9805 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009806 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9807 assignLayersLocked();
9808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009809 performLayoutLockedInner();
9810 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009811 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009812
9813 restart = true;
9814 }
9815 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009816
Dianne Hackborna8f60182009-09-01 19:01:50 -07009817 if (!animating && mAppTransitionRunning) {
9818 // We have finished the animation of an app transition. To do
9819 // this, we have delayed a lot of operations like showing and
9820 // hiding apps, moving apps in Z-order, etc. The app token list
9821 // reflects the correct Z-order, but the window list may now
9822 // be out of sync with it. So here we will just rebuild the
9823 // entire app window list. Fun!
9824 mAppTransitionRunning = false;
9825 // Clear information about apps that were moving.
9826 mToBottomApps.clear();
9827
9828 rebuildAppWindowListLocked();
9829 restart = true;
9830 moveInputMethodWindowsIfNeededLocked(false);
9831 wallpaperMayChange = true;
9832 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009833 // Since the window list has been rebuilt, focus might
9834 // have to be recomputed since the actual order of windows
9835 // might have changed again.
9836 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009837 }
9838
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009839 int adjResult = 0;
9840
9841 if (wallpaperForceHidingChanged) {
9842 // At this point, there was a window with a wallpaper that
9843 // was force hiding other windows behind it, but now it
9844 // is going away. This may be simple -- just animate
9845 // away the wallpaper and its window -- or it may be
9846 // hard -- the wallpaper now needs to be shown behind
9847 // something that was hidden.
9848 WindowState oldWallpaper = mWallpaperTarget;
9849 adjResult = adjustWallpaperWindowsLocked();
9850 wallpaperMayChange = false;
9851 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9852 + " NEW: " + mWallpaperTarget);
9853 if (mLowerWallpaperTarget == null) {
9854 // Whoops, we don't need a special wallpaper animation.
9855 // Clear them out.
9856 forceHiding = false;
9857 for (i=N-1; i>=0; i--) {
9858 WindowState w = (WindowState)mWindows.get(i);
9859 if (w.mSurface != null) {
9860 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009861 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9862 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009863 forceHiding = true;
9864 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9865 if (!w.mAnimating) {
9866 // We set the animation above so it
9867 // is not yet running.
9868 w.clearAnimation();
9869 }
9870 }
9871 }
9872 }
9873 }
9874 }
9875
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009876 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009877 if (DEBUG_WALLPAPER) Log.v(TAG,
9878 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009879 adjResult = adjustWallpaperWindowsLocked();
9880 }
9881
9882 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9883 if (DEBUG_WALLPAPER) Log.v(TAG,
9884 "Wallpaper layer changed: assigning layers + relayout");
9885 restart = true;
9886 mLayoutNeeded = true;
9887 assignLayersLocked();
9888 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9889 if (DEBUG_WALLPAPER) Log.v(TAG,
9890 "Wallpaper visibility changed: relayout");
9891 restart = true;
9892 mLayoutNeeded = true;
9893 }
9894
9895 if (focusMayChange) {
9896 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009897 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009898 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009899 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009900 }
9901
9902 if (mLayoutNeeded) {
9903 restart = true;
9904 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009905 }
9906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009907 } while (restart);
9908
9909 // THIRD LOOP: Update the surfaces of all windows.
9910
9911 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9912
9913 boolean obscured = false;
9914 boolean blurring = false;
9915 boolean dimming = false;
9916 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009917 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009918 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009920 final int N = mWindows.size();
9921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 for (i=N-1; i>=0; i--) {
9923 WindowState w = (WindowState)mWindows.get(i);
9924
9925 boolean displayed = false;
9926 final WindowManager.LayoutParams attrs = w.mAttrs;
9927 final int attrFlags = attrs.flags;
9928
9929 if (w.mSurface != null) {
9930 w.computeShownFrameLocked();
9931 if (localLOGV) Log.v(
9932 TAG, "Placing surface #" + i + " " + w.mSurface
9933 + ": new=" + w.mShownFrame + ", old="
9934 + w.mLastShownFrame);
9935
9936 boolean resize;
9937 int width, height;
9938 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9939 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9940 w.mLastRequestedHeight != w.mRequestedHeight;
9941 // for a scaled surface, we just want to use
9942 // the requested size.
9943 width = w.mRequestedWidth;
9944 height = w.mRequestedHeight;
9945 w.mLastRequestedWidth = width;
9946 w.mLastRequestedHeight = height;
9947 w.mLastShownFrame.set(w.mShownFrame);
9948 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009949 if (SHOW_TRANSACTIONS) Log.i(
9950 TAG, " SURFACE " + w.mSurface
9951 + ": POS " + w.mShownFrame.left
9952 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009953 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9954 } catch (RuntimeException e) {
9955 Log.w(TAG, "Error positioning surface in " + w, e);
9956 if (!recoveringMemory) {
9957 reclaimSomeSurfaceMemoryLocked(w, "position");
9958 }
9959 }
9960 } else {
9961 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9962 width = w.mShownFrame.width();
9963 height = w.mShownFrame.height();
9964 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009965 }
9966
9967 if (resize) {
9968 if (width < 1) width = 1;
9969 if (height < 1) height = 1;
9970 if (w.mSurface != null) {
9971 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009972 if (SHOW_TRANSACTIONS) Log.i(
9973 TAG, " SURFACE " + w.mSurface + ": POS "
9974 + w.mShownFrame.left + ","
9975 + w.mShownFrame.top + " SIZE "
9976 + w.mShownFrame.width() + "x"
9977 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009978 w.mSurface.setSize(width, height);
9979 w.mSurface.setPosition(w.mShownFrame.left,
9980 w.mShownFrame.top);
9981 } catch (RuntimeException e) {
9982 // If something goes wrong with the surface (such
9983 // as running out of memory), don't take down the
9984 // entire system.
9985 Log.e(TAG, "Failure updating surface of " + w
9986 + "size=(" + width + "x" + height
9987 + "), pos=(" + w.mShownFrame.left
9988 + "," + w.mShownFrame.top + ")", e);
9989 if (!recoveringMemory) {
9990 reclaimSomeSurfaceMemoryLocked(w, "size");
9991 }
9992 }
9993 }
9994 }
9995 if (!w.mAppFreezing) {
9996 w.mContentInsetsChanged =
9997 !w.mLastContentInsets.equals(w.mContentInsets);
9998 w.mVisibleInsetsChanged =
9999 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -070010000 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010001 || w.mContentInsetsChanged
10002 || w.mVisibleInsetsChanged) {
10003 w.mLastFrame.set(w.mFrame);
10004 w.mLastContentInsets.set(w.mContentInsets);
10005 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010006 // If the screen is currently frozen, then keep
10007 // it frozen until this window draws at its new
10008 // orientation.
10009 if (mDisplayFrozen) {
10010 if (DEBUG_ORIENTATION) Log.v(TAG,
10011 "Resizing while display frozen: " + w);
10012 w.mOrientationChanging = true;
10013 if (mWindowsFreezingScreen) {
10014 mWindowsFreezingScreen = true;
10015 // XXX should probably keep timeout from
10016 // when we first froze the display.
10017 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10018 mH.sendMessageDelayed(mH.obtainMessage(
10019 H.WINDOW_FREEZE_TIMEOUT), 2000);
10020 }
10021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010022 // If the orientation is changing, then we need to
10023 // hold off on unfreezing the display until this
10024 // window has been redrawn; to do that, we need
10025 // to go through the process of getting informed
10026 // by the application when it has finished drawing.
10027 if (w.mOrientationChanging) {
10028 if (DEBUG_ORIENTATION) Log.v(TAG,
10029 "Orientation start waiting for draw in "
10030 + w + ", surface " + w.mSurface);
10031 w.mDrawPending = true;
10032 w.mCommitDrawPending = false;
10033 w.mReadyToShow = false;
10034 if (w.mAppToken != null) {
10035 w.mAppToken.allDrawn = false;
10036 }
10037 }
Romain Guy06882f82009-06-10 13:36:04 -070010038 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010039 "Resizing window " + w + " to " + w.mFrame);
10040 mResizingWindows.add(w);
10041 } else if (w.mOrientationChanging) {
10042 if (!w.mDrawPending && !w.mCommitDrawPending) {
10043 if (DEBUG_ORIENTATION) Log.v(TAG,
10044 "Orientation not waiting for draw in "
10045 + w + ", surface " + w.mSurface);
10046 w.mOrientationChanging = false;
10047 }
10048 }
10049 }
10050
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010051 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010052 if (!w.mLastHidden) {
10053 //dump();
10054 w.mLastHidden = true;
10055 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010056 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010057 if (w.mSurface != null) {
10058 try {
10059 w.mSurface.hide();
10060 } catch (RuntimeException e) {
10061 Log.w(TAG, "Exception hiding surface in " + w);
10062 }
10063 }
10064 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10065 }
10066 // If we are waiting for this window to handle an
10067 // orientation change, well, it is hidden, so
10068 // doesn't really matter. Note that this does
10069 // introduce a potential glitch if the window
10070 // becomes unhidden before it has drawn for the
10071 // new orientation.
10072 if (w.mOrientationChanging) {
10073 w.mOrientationChanging = false;
10074 if (DEBUG_ORIENTATION) Log.v(TAG,
10075 "Orientation change skips hidden " + w);
10076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010077 } else if (w.mLastLayer != w.mAnimLayer
10078 || w.mLastAlpha != w.mShownAlpha
10079 || w.mLastDsDx != w.mDsDx
10080 || w.mLastDtDx != w.mDtDx
10081 || w.mLastDsDy != w.mDsDy
10082 || w.mLastDtDy != w.mDtDy
10083 || w.mLastHScale != w.mHScale
10084 || w.mLastVScale != w.mVScale
10085 || w.mLastHidden) {
10086 displayed = true;
10087 w.mLastAlpha = w.mShownAlpha;
10088 w.mLastLayer = w.mAnimLayer;
10089 w.mLastDsDx = w.mDsDx;
10090 w.mLastDtDx = w.mDtDx;
10091 w.mLastDsDy = w.mDsDy;
10092 w.mLastDtDy = w.mDtDy;
10093 w.mLastHScale = w.mHScale;
10094 w.mLastVScale = w.mVScale;
10095 if (SHOW_TRANSACTIONS) Log.i(
10096 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010097 + w.mShownAlpha + " layer=" + w.mAnimLayer
10098 + " matrix=[" + (w.mDsDx*w.mHScale)
10099 + "," + (w.mDtDx*w.mVScale)
10100 + "][" + (w.mDsDy*w.mHScale)
10101 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010102 if (w.mSurface != null) {
10103 try {
10104 w.mSurface.setAlpha(w.mShownAlpha);
10105 w.mSurface.setLayer(w.mAnimLayer);
10106 w.mSurface.setMatrix(
10107 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10108 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10109 } catch (RuntimeException e) {
10110 Log.w(TAG, "Error updating surface in " + w, e);
10111 if (!recoveringMemory) {
10112 reclaimSomeSurfaceMemoryLocked(w, "update");
10113 }
10114 }
10115 }
10116
10117 if (w.mLastHidden && !w.mDrawPending
10118 && !w.mCommitDrawPending
10119 && !w.mReadyToShow) {
10120 if (SHOW_TRANSACTIONS) Log.i(
10121 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
10122 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
10123 + " during relayout");
10124 if (showSurfaceRobustlyLocked(w)) {
10125 w.mHasDrawn = true;
10126 w.mLastHidden = false;
10127 } else {
10128 w.mOrientationChanging = false;
10129 }
10130 }
10131 if (w.mSurface != null) {
10132 w.mToken.hasVisible = true;
10133 }
10134 } else {
10135 displayed = true;
10136 }
10137
10138 if (displayed) {
10139 if (!covered) {
10140 if (attrs.width == LayoutParams.FILL_PARENT
10141 && attrs.height == LayoutParams.FILL_PARENT) {
10142 covered = true;
10143 }
10144 }
10145 if (w.mOrientationChanging) {
10146 if (w.mDrawPending || w.mCommitDrawPending) {
10147 orientationChangeComplete = false;
10148 if (DEBUG_ORIENTATION) Log.v(TAG,
10149 "Orientation continue waiting for draw in " + w);
10150 } else {
10151 w.mOrientationChanging = false;
10152 if (DEBUG_ORIENTATION) Log.v(TAG,
10153 "Orientation change complete in " + w);
10154 }
10155 }
10156 w.mToken.hasVisible = true;
10157 }
10158 } else if (w.mOrientationChanging) {
10159 if (DEBUG_ORIENTATION) Log.v(TAG,
10160 "Orientation change skips hidden " + w);
10161 w.mOrientationChanging = false;
10162 }
10163
10164 final boolean canBeSeen = w.isDisplayedLw();
10165
10166 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10167 focusDisplayed = true;
10168 }
10169
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010170 final boolean obscuredChanged = w.mObscured != obscured;
10171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010172 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010173 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010174 if (w.mSurface != null) {
10175 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10176 holdScreen = w.mSession;
10177 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010178 if (!syswin && w.mAttrs.screenBrightness >= 0
10179 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010180 screenBrightness = w.mAttrs.screenBrightness;
10181 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010182 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10183 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10184 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10185 syswin = true;
10186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010188
Dianne Hackborn25994b42009-09-04 14:21:19 -070010189 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10190 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010191 // This window completely covers everything behind it,
10192 // so we want to leave all of them as unblurred (for
10193 // performance reasons).
10194 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010195 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
10196 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010197 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010198 obscured = true;
10199 if (mBackgroundFillerSurface == null) {
10200 try {
10201 mBackgroundFillerSurface = new Surface(mFxSession, 0,
10202 0, dw, dh,
10203 PixelFormat.OPAQUE,
10204 Surface.FX_SURFACE_NORMAL);
10205 } catch (Exception e) {
10206 Log.e(TAG, "Exception creating filler surface", e);
10207 }
10208 }
10209 try {
10210 mBackgroundFillerSurface.setPosition(0, 0);
10211 mBackgroundFillerSurface.setSize(dw, dh);
10212 // Using the same layer as Dim because they will never be shown at the
10213 // same time.
10214 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10215 mBackgroundFillerSurface.show();
10216 } catch (RuntimeException e) {
10217 Log.e(TAG, "Exception showing filler surface");
10218 }
10219 backgroundFillerShown = true;
10220 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010221 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010222 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10223 if (localLOGV) Log.v(TAG, "Win " + w
10224 + ": blurring=" + blurring
10225 + " obscured=" + obscured
10226 + " displayed=" + displayed);
10227 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10228 if (!dimming) {
10229 //Log.i(TAG, "DIM BEHIND: " + w);
10230 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010231 if (mDimAnimator == null) {
10232 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010233 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010234 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010236 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010237 }
10238 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10239 if (!blurring) {
10240 //Log.i(TAG, "BLUR BEHIND: " + w);
10241 blurring = true;
10242 mBlurShown = true;
10243 if (mBlurSurface == null) {
10244 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10245 + mBlurSurface + ": CREATE");
10246 try {
Romain Guy06882f82009-06-10 13:36:04 -070010247 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010248 -1, 16, 16,
10249 PixelFormat.OPAQUE,
10250 Surface.FX_SURFACE_BLUR);
10251 } catch (Exception e) {
10252 Log.e(TAG, "Exception creating Blur surface", e);
10253 }
10254 }
10255 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10256 + mBlurSurface + ": SHOW pos=(0,0) (" +
10257 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10258 if (mBlurSurface != null) {
10259 mBlurSurface.setPosition(0, 0);
10260 mBlurSurface.setSize(dw, dh);
10261 try {
10262 mBlurSurface.show();
10263 } catch (RuntimeException e) {
10264 Log.w(TAG, "Failure showing blur surface", e);
10265 }
10266 }
10267 }
10268 mBlurSurface.setLayer(w.mAnimLayer-2);
10269 }
10270 }
10271 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010272
10273 if (obscuredChanged && mWallpaperTarget == w) {
10274 // This is the wallpaper target and its obscured state
10275 // changed... make sure the current wallaper's visibility
10276 // has been updated accordingly.
10277 updateWallpaperVisibilityLocked();
10278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010279 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010280
10281 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10282 mBackgroundFillerShown = false;
10283 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10284 try {
10285 mBackgroundFillerSurface.hide();
10286 } catch (RuntimeException e) {
10287 Log.e(TAG, "Exception hiding filler surface", e);
10288 }
10289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010290
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010291 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10292 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010293 }
Romain Guy06882f82009-06-10 13:36:04 -070010294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010295 if (!blurring && mBlurShown) {
10296 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10297 + ": HIDE");
10298 try {
10299 mBlurSurface.hide();
10300 } catch (IllegalArgumentException e) {
10301 Log.w(TAG, "Illegal argument exception hiding blur surface");
10302 }
10303 mBlurShown = false;
10304 }
10305
10306 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10307 } catch (RuntimeException e) {
10308 Log.e(TAG, "Unhandled exception in Window Manager", e);
10309 }
10310
10311 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010313 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10314 "With display frozen, orientationChangeComplete="
10315 + orientationChangeComplete);
10316 if (orientationChangeComplete) {
10317 if (mWindowsFreezingScreen) {
10318 mWindowsFreezingScreen = false;
10319 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10320 }
10321 if (mAppsFreezingScreen == 0) {
10322 stopFreezingDisplayLocked();
10323 }
10324 }
Romain Guy06882f82009-06-10 13:36:04 -070010325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010326 i = mResizingWindows.size();
10327 if (i > 0) {
10328 do {
10329 i--;
10330 WindowState win = mResizingWindows.get(i);
10331 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010332 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10333 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010334 win.mClient.resized(win.mFrame.width(),
10335 win.mFrame.height(), win.mLastContentInsets,
10336 win.mLastVisibleInsets, win.mDrawPending);
10337 win.mContentInsetsChanged = false;
10338 win.mVisibleInsetsChanged = false;
10339 } catch (RemoteException e) {
10340 win.mOrientationChanging = false;
10341 }
10342 } while (i > 0);
10343 mResizingWindows.clear();
10344 }
Romain Guy06882f82009-06-10 13:36:04 -070010345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010346 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010347 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 i = mDestroySurface.size();
10349 if (i > 0) {
10350 do {
10351 i--;
10352 WindowState win = mDestroySurface.get(i);
10353 win.mDestroying = false;
10354 if (mInputMethodWindow == win) {
10355 mInputMethodWindow = null;
10356 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010357 if (win == mWallpaperTarget) {
10358 wallpaperDestroyed = true;
10359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010360 win.destroySurfaceLocked();
10361 } while (i > 0);
10362 mDestroySurface.clear();
10363 }
10364
10365 // Time to remove any exiting tokens?
10366 for (i=mExitingTokens.size()-1; i>=0; i--) {
10367 WindowToken token = mExitingTokens.get(i);
10368 if (!token.hasVisible) {
10369 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010370 if (token.windowType == TYPE_WALLPAPER) {
10371 mWallpaperTokens.remove(token);
10372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010373 }
10374 }
10375
10376 // Time to remove any exiting applications?
10377 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10378 AppWindowToken token = mExitingAppTokens.get(i);
10379 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010380 // Make sure there is no animation running on this token,
10381 // so any windows associated with it will be removed as
10382 // soon as their animations are complete
10383 token.animation = null;
10384 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 mAppTokens.remove(token);
10386 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010387 if (mLastEnterAnimToken == token) {
10388 mLastEnterAnimToken = null;
10389 mLastEnterAnimParams = null;
10390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010391 }
10392 }
10393
Dianne Hackborna8f60182009-09-01 19:01:50 -070010394 boolean needRelayout = false;
10395
10396 if (!animating && mAppTransitionRunning) {
10397 // We have finished the animation of an app transition. To do
10398 // this, we have delayed a lot of operations like showing and
10399 // hiding apps, moving apps in Z-order, etc. The app token list
10400 // reflects the correct Z-order, but the window list may now
10401 // be out of sync with it. So here we will just rebuild the
10402 // entire app window list. Fun!
10403 mAppTransitionRunning = false;
10404 needRelayout = true;
10405 rebuildAppWindowListLocked();
10406 // Clear information about apps that were moving.
10407 mToBottomApps.clear();
10408 }
10409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 if (focusDisplayed) {
10411 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10412 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010413 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010414 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010415 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010416 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010417 requestAnimationLocked(0);
10418 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010419 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10420 }
10421 mQueue.setHoldScreenLocked(holdScreen != null);
10422 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10423 mPowerManager.setScreenBrightnessOverride(-1);
10424 } else {
10425 mPowerManager.setScreenBrightnessOverride((int)
10426 (screenBrightness * Power.BRIGHTNESS_ON));
10427 }
10428 if (holdScreen != mHoldingScreenOn) {
10429 mHoldingScreenOn = holdScreen;
10430 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10431 mH.sendMessage(m);
10432 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010433
10434 if (mTurnOnScreen) {
10435 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10436 LocalPowerManager.BUTTON_EVENT, true);
10437 mTurnOnScreen = false;
10438 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010439
10440 // Check to see if we are now in a state where the screen should
10441 // be enabled, because the window obscured flags have changed.
10442 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010443 }
10444
10445 void requestAnimationLocked(long delay) {
10446 if (!mAnimationPending) {
10447 mAnimationPending = true;
10448 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10449 }
10450 }
Romain Guy06882f82009-06-10 13:36:04 -070010451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010452 /**
10453 * Have the surface flinger show a surface, robustly dealing with
10454 * error conditions. In particular, if there is not enough memory
10455 * to show the surface, then we will try to get rid of other surfaces
10456 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010457 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010458 * @return Returns true if the surface was successfully shown.
10459 */
10460 boolean showSurfaceRobustlyLocked(WindowState win) {
10461 try {
10462 if (win.mSurface != null) {
10463 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010464 if (win.mTurnOnScreen) {
10465 win.mTurnOnScreen = false;
10466 mTurnOnScreen = true;
10467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010468 }
10469 return true;
10470 } catch (RuntimeException e) {
10471 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10472 }
Romain Guy06882f82009-06-10 13:36:04 -070010473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010474 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010476 return false;
10477 }
Romain Guy06882f82009-06-10 13:36:04 -070010478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10480 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010482 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
10483 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010485 if (mForceRemoves == null) {
10486 mForceRemoves = new ArrayList<WindowState>();
10487 }
Romain Guy06882f82009-06-10 13:36:04 -070010488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010489 long callingIdentity = Binder.clearCallingIdentity();
10490 try {
10491 // There was some problem... first, do a sanity check of the
10492 // window list to make sure we haven't left any dangling surfaces
10493 // around.
10494 int N = mWindows.size();
10495 boolean leakedSurface = false;
10496 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10497 for (int i=0; i<N; i++) {
10498 WindowState ws = (WindowState)mWindows.get(i);
10499 if (ws.mSurface != null) {
10500 if (!mSessions.contains(ws.mSession)) {
10501 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10502 + ws + " surface=" + ws.mSurface
10503 + " token=" + win.mToken
10504 + " pid=" + ws.mSession.mPid
10505 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010506 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 ws.mSurface = null;
10508 mForceRemoves.add(ws);
10509 i--;
10510 N--;
10511 leakedSurface = true;
10512 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10513 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10514 + ws + " surface=" + ws.mSurface
10515 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010516 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 ws.mSurface = null;
10518 leakedSurface = true;
10519 }
10520 }
10521 }
Romain Guy06882f82009-06-10 13:36:04 -070010522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010523 boolean killedApps = false;
10524 if (!leakedSurface) {
10525 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10526 SparseIntArray pidCandidates = new SparseIntArray();
10527 for (int i=0; i<N; i++) {
10528 WindowState ws = (WindowState)mWindows.get(i);
10529 if (ws.mSurface != null) {
10530 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10531 }
10532 }
10533 if (pidCandidates.size() > 0) {
10534 int[] pids = new int[pidCandidates.size()];
10535 for (int i=0; i<pids.length; i++) {
10536 pids[i] = pidCandidates.keyAt(i);
10537 }
10538 try {
10539 if (mActivityManager.killPidsForMemory(pids)) {
10540 killedApps = true;
10541 }
10542 } catch (RemoteException e) {
10543 }
10544 }
10545 }
Romain Guy06882f82009-06-10 13:36:04 -070010546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 if (leakedSurface || killedApps) {
10548 // We managed to reclaim some memory, so get rid of the trouble
10549 // surface and ask the app to request another one.
10550 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10551 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010552 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 win.mSurface = null;
10554 }
Romain Guy06882f82009-06-10 13:36:04 -070010555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 try {
10557 win.mClient.dispatchGetNewSurface();
10558 } catch (RemoteException e) {
10559 }
10560 }
10561 } finally {
10562 Binder.restoreCallingIdentity(callingIdentity);
10563 }
10564 }
Romain Guy06882f82009-06-10 13:36:04 -070010565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 private boolean updateFocusedWindowLocked(int mode) {
10567 WindowState newFocus = computeFocusedWindowLocked();
10568 if (mCurrentFocus != newFocus) {
10569 // This check makes sure that we don't already have the focus
10570 // change message pending.
10571 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10572 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10573 if (localLOGV) Log.v(
10574 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10575 final WindowState oldFocus = mCurrentFocus;
10576 mCurrentFocus = newFocus;
10577 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 final WindowState imWindow = mInputMethodWindow;
10580 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010581 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010582 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010583 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10584 mLayoutNeeded = true;
10585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10587 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010588 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10589 // Client will do the layout, but we need to assign layers
10590 // for handleNewWindowLocked() below.
10591 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010592 }
10593 }
Romain Guy06882f82009-06-10 13:36:04 -070010594
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010595 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10596 mKeyWaiter.handleNewWindowLocked(newFocus);
10597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010598 return true;
10599 }
10600 return false;
10601 }
10602
10603 private WindowState computeFocusedWindowLocked() {
10604 WindowState result = null;
10605 WindowState win;
10606
10607 int i = mWindows.size() - 1;
10608 int nextAppIndex = mAppTokens.size()-1;
10609 WindowToken nextApp = nextAppIndex >= 0
10610 ? mAppTokens.get(nextAppIndex) : null;
10611
10612 while (i >= 0) {
10613 win = (WindowState)mWindows.get(i);
10614
10615 if (localLOGV || DEBUG_FOCUS) Log.v(
10616 TAG, "Looking for focus: " + i
10617 + " = " + win
10618 + ", flags=" + win.mAttrs.flags
10619 + ", canReceive=" + win.canReceiveKeys());
10620
10621 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010623 // If this window's application has been removed, just skip it.
10624 if (thisApp != null && thisApp.removed) {
10625 i--;
10626 continue;
10627 }
Romain Guy06882f82009-06-10 13:36:04 -070010628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010629 // If there is a focused app, don't allow focus to go to any
10630 // windows below it. If this is an application window, step
10631 // through the app tokens until we find its app.
10632 if (thisApp != null && nextApp != null && thisApp != nextApp
10633 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10634 int origAppIndex = nextAppIndex;
10635 while (nextAppIndex > 0) {
10636 if (nextApp == mFocusedApp) {
10637 // Whoops, we are below the focused app... no focus
10638 // for you!
10639 if (localLOGV || DEBUG_FOCUS) Log.v(
10640 TAG, "Reached focused app: " + mFocusedApp);
10641 return null;
10642 }
10643 nextAppIndex--;
10644 nextApp = mAppTokens.get(nextAppIndex);
10645 if (nextApp == thisApp) {
10646 break;
10647 }
10648 }
10649 if (thisApp != nextApp) {
10650 // Uh oh, the app token doesn't exist! This shouldn't
10651 // happen, but if it does we can get totally hosed...
10652 // so restart at the original app.
10653 nextAppIndex = origAppIndex;
10654 nextApp = mAppTokens.get(nextAppIndex);
10655 }
10656 }
10657
10658 // Dispatch to this window if it is wants key events.
10659 if (win.canReceiveKeys()) {
10660 if (DEBUG_FOCUS) Log.v(
10661 TAG, "Found focus @ " + i + " = " + win);
10662 result = win;
10663 break;
10664 }
10665
10666 i--;
10667 }
10668
10669 return result;
10670 }
10671
10672 private void startFreezingDisplayLocked() {
10673 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010674 // Freezing the display also suspends key event delivery, to
10675 // keep events from going astray while the display is reconfigured.
10676 // If someone has changed orientation again while the screen is
10677 // still frozen, the events will continue to be blocked while the
10678 // successive orientation change is processed. To prevent spurious
10679 // ANRs, we reset the event dispatch timeout in this case.
10680 synchronized (mKeyWaiter) {
10681 mKeyWaiter.mWasFrozen = true;
10682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010683 return;
10684 }
Romain Guy06882f82009-06-10 13:36:04 -070010685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010688 long now = SystemClock.uptimeMillis();
10689 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10690 if (mFreezeGcPending != 0) {
10691 if (now > (mFreezeGcPending+1000)) {
10692 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10693 mH.removeMessages(H.FORCE_GC);
10694 Runtime.getRuntime().gc();
10695 mFreezeGcPending = now;
10696 }
10697 } else {
10698 mFreezeGcPending = now;
10699 }
Romain Guy06882f82009-06-10 13:36:04 -070010700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010701 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010702 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10703 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010704 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010705 mAppTransitionReady = true;
10706 }
Romain Guy06882f82009-06-10 13:36:04 -070010707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 if (PROFILE_ORIENTATION) {
10709 File file = new File("/data/system/frozen");
10710 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10711 }
10712 Surface.freezeDisplay(0);
10713 }
Romain Guy06882f82009-06-10 13:36:04 -070010714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 private void stopFreezingDisplayLocked() {
10716 if (!mDisplayFrozen) {
10717 return;
10718 }
Romain Guy06882f82009-06-10 13:36:04 -070010719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010720 mDisplayFrozen = false;
10721 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10722 if (PROFILE_ORIENTATION) {
10723 Debug.stopMethodTracing();
10724 }
10725 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010726
Chris Tate2ad63a92009-03-25 17:36:48 -070010727 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10728 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010729 synchronized (mKeyWaiter) {
10730 mKeyWaiter.mWasFrozen = true;
10731 mKeyWaiter.notifyAll();
10732 }
10733
10734 // A little kludge: a lot could have happened while the
10735 // display was frozen, so now that we are coming back we
10736 // do a gc so that any remote references the system
10737 // processes holds on others can be released if they are
10738 // no longer needed.
10739 mH.removeMessages(H.FORCE_GC);
10740 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10741 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010743 mScreenFrozenLock.release();
10744 }
Romain Guy06882f82009-06-10 13:36:04 -070010745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010746 @Override
10747 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10748 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10749 != PackageManager.PERMISSION_GRANTED) {
10750 pw.println("Permission Denial: can't dump WindowManager from from pid="
10751 + Binder.getCallingPid()
10752 + ", uid=" + Binder.getCallingUid());
10753 return;
10754 }
Romain Guy06882f82009-06-10 13:36:04 -070010755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010756 synchronized(mWindowMap) {
10757 pw.println("Current Window Manager state:");
10758 for (int i=mWindows.size()-1; i>=0; i--) {
10759 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010760 pw.print(" Window #"); pw.print(i); pw.print(' ');
10761 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 w.dump(pw, " ");
10763 }
10764 if (mInputMethodDialogs.size() > 0) {
10765 pw.println(" ");
10766 pw.println(" Input method dialogs:");
10767 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10768 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010769 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010770 }
10771 }
10772 if (mPendingRemove.size() > 0) {
10773 pw.println(" ");
10774 pw.println(" Remove pending for:");
10775 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10776 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010777 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10778 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010779 w.dump(pw, " ");
10780 }
10781 }
10782 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10783 pw.println(" ");
10784 pw.println(" Windows force removing:");
10785 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10786 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010787 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10788 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010789 w.dump(pw, " ");
10790 }
10791 }
10792 if (mDestroySurface.size() > 0) {
10793 pw.println(" ");
10794 pw.println(" Windows waiting to destroy their surface:");
10795 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10796 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010797 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10798 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010799 w.dump(pw, " ");
10800 }
10801 }
10802 if (mLosingFocus.size() > 0) {
10803 pw.println(" ");
10804 pw.println(" Windows losing focus:");
10805 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10806 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010807 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10808 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010809 w.dump(pw, " ");
10810 }
10811 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010812 if (mResizingWindows.size() > 0) {
10813 pw.println(" ");
10814 pw.println(" Windows waiting to resize:");
10815 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10816 WindowState w = mResizingWindows.get(i);
10817 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10818 pw.print(w); pw.println(":");
10819 w.dump(pw, " ");
10820 }
10821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010822 if (mSessions.size() > 0) {
10823 pw.println(" ");
10824 pw.println(" All active sessions:");
10825 Iterator<Session> it = mSessions.iterator();
10826 while (it.hasNext()) {
10827 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010828 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010829 s.dump(pw, " ");
10830 }
10831 }
10832 if (mTokenMap.size() > 0) {
10833 pw.println(" ");
10834 pw.println(" All tokens:");
10835 Iterator<WindowToken> it = mTokenMap.values().iterator();
10836 while (it.hasNext()) {
10837 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010838 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839 token.dump(pw, " ");
10840 }
10841 }
10842 if (mTokenList.size() > 0) {
10843 pw.println(" ");
10844 pw.println(" Window token list:");
10845 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010846 pw.print(" #"); pw.print(i); pw.print(": ");
10847 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010848 }
10849 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010850 if (mWallpaperTokens.size() > 0) {
10851 pw.println(" ");
10852 pw.println(" Wallpaper tokens:");
10853 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10854 WindowToken token = mWallpaperTokens.get(i);
10855 pw.print(" Wallpaper #"); pw.print(i);
10856 pw.print(' '); pw.print(token); pw.println(':');
10857 token.dump(pw, " ");
10858 }
10859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010860 if (mAppTokens.size() > 0) {
10861 pw.println(" ");
10862 pw.println(" Application tokens in Z order:");
10863 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010864 pw.print(" App #"); pw.print(i); pw.print(": ");
10865 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010866 }
10867 }
10868 if (mFinishedStarting.size() > 0) {
10869 pw.println(" ");
10870 pw.println(" Finishing start of application tokens:");
10871 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10872 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010873 pw.print(" Finished Starting #"); pw.print(i);
10874 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010875 token.dump(pw, " ");
10876 }
10877 }
10878 if (mExitingTokens.size() > 0) {
10879 pw.println(" ");
10880 pw.println(" Exiting tokens:");
10881 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10882 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010883 pw.print(" Exiting #"); pw.print(i);
10884 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885 token.dump(pw, " ");
10886 }
10887 }
10888 if (mExitingAppTokens.size() > 0) {
10889 pw.println(" ");
10890 pw.println(" Exiting application tokens:");
10891 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10892 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010893 pw.print(" Exiting App #"); pw.print(i);
10894 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010895 token.dump(pw, " ");
10896 }
10897 }
10898 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010899 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10900 pw.print(" mLastFocus="); pw.println(mLastFocus);
10901 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10902 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10903 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010904 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010905 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10906 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10907 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10908 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010909 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10910 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10911 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10912 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10913 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010914 if (mDimAnimator != null) {
10915 mDimAnimator.printTo(pw);
10916 } else {
10917 pw.print( " no DimAnimator ");
10918 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010919 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010920 pw.print(mInputMethodAnimLayerAdjustment);
10921 pw.print(" mWallpaperAnimLayerAdjustment=");
10922 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010923 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10924 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010925 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10926 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10927 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10928 pw.print(" mRotation="); pw.print(mRotation);
10929 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10930 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10931 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10932 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10933 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10934 pw.print(" mNextAppTransition=0x");
10935 pw.print(Integer.toHexString(mNextAppTransition));
10936 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010937 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010938 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010939 if (mNextAppTransitionPackage != null) {
10940 pw.print(" mNextAppTransitionPackage=");
10941 pw.print(mNextAppTransitionPackage);
10942 pw.print(", mNextAppTransitionEnter=0x");
10943 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10944 pw.print(", mNextAppTransitionExit=0x");
10945 pw.print(Integer.toHexString(mNextAppTransitionExit));
10946 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010947 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10948 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010949 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10950 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10951 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10952 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010953 if (mOpeningApps.size() > 0) {
10954 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10955 }
10956 if (mClosingApps.size() > 0) {
10957 pw.print(" mClosingApps="); pw.println(mClosingApps);
10958 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010959 if (mToTopApps.size() > 0) {
10960 pw.print(" mToTopApps="); pw.println(mToTopApps);
10961 }
10962 if (mToBottomApps.size() > 0) {
10963 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10964 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010965 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10966 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010967 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010968 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10969 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10970 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10971 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10972 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10973 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010974 }
10975 }
10976
10977 public void monitor() {
10978 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050010979 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010980 synchronized (mKeyWaiter) { }
10981 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010982
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010983 public void virtualKeyFeedback(KeyEvent event) {
10984 mPolicy.keyFeedbackFromInput(event);
10985 }
10986
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010987 /**
10988 * DimAnimator class that controls the dim animation. This holds the surface and
10989 * all state used for dim animation.
10990 */
10991 private static class DimAnimator {
10992 Surface mDimSurface;
10993 boolean mDimShown = false;
10994 float mDimCurrentAlpha;
10995 float mDimTargetAlpha;
10996 float mDimDeltaPerMs;
10997 long mLastDimAnimTime;
10998
10999 DimAnimator (SurfaceSession session) {
11000 if (mDimSurface == null) {
11001 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11002 + mDimSurface + ": CREATE");
11003 try {
11004 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
11005 Surface.FX_SURFACE_DIM);
Maciej Białka9ee5c222010-03-24 10:25:40 +010011006 mDimSurface.setAlpha(0.0f);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011007 } catch (Exception e) {
11008 Log.e(TAG, "Exception creating Dim surface", e);
11009 }
11010 }
11011 }
11012
11013 /**
11014 * Show the dim surface.
11015 */
11016 void show(int dw, int dh) {
11017 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
11018 dw + "x" + dh + ")");
11019 mDimShown = true;
11020 try {
11021 mDimSurface.setPosition(0, 0);
11022 mDimSurface.setSize(dw, dh);
11023 mDimSurface.show();
11024 } catch (RuntimeException e) {
11025 Log.w(TAG, "Failure showing dim surface", e);
11026 }
11027 }
11028
11029 /**
11030 * Set's the dim surface's layer and update dim parameters that will be used in
11031 * {@link updateSurface} after all windows are examined.
11032 */
11033 void updateParameters(WindowState w, long currentTime) {
11034 mDimSurface.setLayer(w.mAnimLayer-1);
11035
11036 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011037 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11038 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011039 if (mDimTargetAlpha != target) {
11040 // If the desired dim level has changed, then
11041 // start an animation to it.
11042 mLastDimAnimTime = currentTime;
11043 long duration = (w.mAnimating && w.mAnimation != null)
11044 ? w.mAnimation.computeDurationHint()
11045 : DEFAULT_DIM_DURATION;
11046 if (target > mDimTargetAlpha) {
11047 // This is happening behind the activity UI,
11048 // so we can make it run a little longer to
11049 // give a stronger impression without disrupting
11050 // the user.
11051 duration *= DIM_DURATION_MULTIPLIER;
11052 }
11053 if (duration < 1) {
11054 // Don't divide by zero
11055 duration = 1;
11056 }
11057 mDimTargetAlpha = target;
11058 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11059 }
11060 }
11061
11062 /**
11063 * Updating the surface's alpha. Returns true if the animation continues, or returns
11064 * false when the animation is finished and the dim surface is hidden.
11065 */
11066 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11067 if (!dimming) {
11068 if (mDimTargetAlpha != 0) {
11069 mLastDimAnimTime = currentTime;
11070 mDimTargetAlpha = 0;
11071 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11072 }
11073 }
11074
11075 boolean animating = false;
11076 if (mLastDimAnimTime != 0) {
11077 mDimCurrentAlpha += mDimDeltaPerMs
11078 * (currentTime-mLastDimAnimTime);
11079 boolean more = true;
11080 if (displayFrozen) {
11081 // If the display is frozen, there is no reason to animate.
11082 more = false;
11083 } else if (mDimDeltaPerMs > 0) {
11084 if (mDimCurrentAlpha > mDimTargetAlpha) {
11085 more = false;
11086 }
11087 } else if (mDimDeltaPerMs < 0) {
11088 if (mDimCurrentAlpha < mDimTargetAlpha) {
11089 more = false;
11090 }
11091 } else {
11092 more = false;
11093 }
11094
11095 // Do we need to continue animating?
11096 if (more) {
11097 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11098 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11099 mLastDimAnimTime = currentTime;
11100 mDimSurface.setAlpha(mDimCurrentAlpha);
11101 animating = true;
11102 } else {
11103 mDimCurrentAlpha = mDimTargetAlpha;
11104 mLastDimAnimTime = 0;
11105 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11106 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11107 mDimSurface.setAlpha(mDimCurrentAlpha);
11108 if (!dimming) {
11109 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11110 + ": HIDE");
11111 try {
11112 mDimSurface.hide();
11113 } catch (RuntimeException e) {
11114 Log.w(TAG, "Illegal argument exception hiding dim surface");
11115 }
11116 mDimShown = false;
11117 }
11118 }
11119 }
11120 return animating;
11121 }
11122
11123 public void printTo(PrintWriter pw) {
11124 pw.print(" mDimShown="); pw.print(mDimShown);
11125 pw.print(" current="); pw.print(mDimCurrentAlpha);
11126 pw.print(" target="); pw.print(mDimTargetAlpha);
11127 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11128 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11129 }
11130 }
11131
11132 /**
11133 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11134 * This is used for opening/closing transition for apps in compatible mode.
11135 */
11136 private static class FadeInOutAnimation extends Animation {
11137 int mWidth;
11138 boolean mFadeIn;
11139
11140 public FadeInOutAnimation(boolean fadeIn) {
11141 setInterpolator(new AccelerateInterpolator());
11142 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11143 mFadeIn = fadeIn;
11144 }
11145
11146 @Override
11147 protected void applyTransformation(float interpolatedTime, Transformation t) {
11148 float x = interpolatedTime;
11149 if (!mFadeIn) {
11150 x = 1.0f - x; // reverse the interpolation for fade out
11151 }
11152 if (x < 0.5) {
11153 // move the window out of the screen.
11154 t.getMatrix().setTranslate(mWidth, 0);
11155 } else {
11156 t.getMatrix().setTranslate(0, 0);// show
11157 t.setAlpha((x - 0.5f) * 2);
11158 }
11159 }
11160
11161 @Override
11162 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11163 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11164 mWidth = width;
11165 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011166
11167 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011168 public int getZAdjustment() {
11169 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011170 }
11171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011172}