blob: 4bad5b486321b8377a5af7a316b3b7999a4ed1f3 [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();
Christian Petersson15f66ce2010-06-03 11:16:39 +0200624 mQueue.start();
Romain Guy06882f82009-06-10 13:36:04 -0700625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 // Add ourself to the Watchdog monitors.
627 Watchdog.getInstance().addMonitor(this);
628 }
629
630 @Override
631 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
632 throws RemoteException {
633 try {
634 return super.onTransact(code, data, reply, flags);
635 } catch (RuntimeException e) {
636 // The window manager only throws security exceptions, so let's
637 // log all others.
638 if (!(e instanceof SecurityException)) {
639 Log.e(TAG, "Window Manager Crash", e);
640 }
641 throw e;
642 }
643 }
644
645 private void placeWindowAfter(Object pos, WindowState window) {
646 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700647 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 TAG, "Adding window " + window + " at "
649 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
650 mWindows.add(i+1, window);
651 }
652
653 private void placeWindowBefore(Object pos, WindowState window) {
654 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700655 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 TAG, "Adding window " + window + " at "
657 + i + " of " + mWindows.size() + " (before " + pos + ")");
658 mWindows.add(i, window);
659 }
660
661 //This method finds out the index of a window that has the same app token as
662 //win. used for z ordering the windows in mWindows
663 private int findIdxBasedOnAppTokens(WindowState win) {
664 //use a local variable to cache mWindows
665 ArrayList localmWindows = mWindows;
666 int jmax = localmWindows.size();
667 if(jmax == 0) {
668 return -1;
669 }
670 for(int j = (jmax-1); j >= 0; j--) {
671 WindowState wentry = (WindowState)localmWindows.get(j);
672 if(wentry.mAppToken == win.mAppToken) {
673 return j;
674 }
675 }
676 return -1;
677 }
Romain Guy06882f82009-06-10 13:36:04 -0700678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
680 final IWindow client = win.mClient;
681 final WindowToken token = win.mToken;
682 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 final int N = localmWindows.size();
685 final WindowState attached = win.mAttachedWindow;
686 int i;
687 if (attached == null) {
688 int tokenWindowsPos = token.windows.size();
689 if (token.appWindowToken != null) {
690 int index = tokenWindowsPos-1;
691 if (index >= 0) {
692 // If this application has existing windows, we
693 // simply place the new window on top of them... but
694 // keep the starting window on top.
695 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
696 // Base windows go behind everything else.
697 placeWindowBefore(token.windows.get(0), win);
698 tokenWindowsPos = 0;
699 } else {
700 AppWindowToken atoken = win.mAppToken;
701 if (atoken != null &&
702 token.windows.get(index) == atoken.startingWindow) {
703 placeWindowBefore(token.windows.get(index), win);
704 tokenWindowsPos--;
705 } else {
706 int newIdx = findIdxBasedOnAppTokens(win);
707 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700708 //there is a window above this one associated with the same
709 //apptoken note that the window could be a floating window
710 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700712 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
713 TAG, "Adding window " + win + " at "
714 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 }
718 }
719 } else {
720 if (localLOGV) Log.v(
721 TAG, "Figuring out where to add app window "
722 + client.asBinder() + " (token=" + token + ")");
723 // Figure out where the window should go, based on the
724 // order of applications.
725 final int NA = mAppTokens.size();
726 Object pos = null;
727 for (i=NA-1; i>=0; i--) {
728 AppWindowToken t = mAppTokens.get(i);
729 if (t == token) {
730 i--;
731 break;
732 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700733
734 // We haven't reached the token yet; if this token
735 // is not going to the bottom and has windows, we can
736 // use it as an anchor for when we do reach the token.
737 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 pos = t.windows.get(0);
739 }
740 }
741 // We now know the index into the apps. If we found
742 // an app window above, that gives us the position; else
743 // we need to look some more.
744 if (pos != null) {
745 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700746 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 mTokenMap.get(((WindowState)pos).mClient.asBinder());
748 if (atoken != null) {
749 final int NC = atoken.windows.size();
750 if (NC > 0) {
751 WindowState bottom = atoken.windows.get(0);
752 if (bottom.mSubLayer < 0) {
753 pos = bottom;
754 }
755 }
756 }
757 placeWindowBefore(pos, win);
758 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700759 // Continue looking down until we find the first
760 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 while (i >= 0) {
762 AppWindowToken t = mAppTokens.get(i);
763 final int NW = t.windows.size();
764 if (NW > 0) {
765 pos = t.windows.get(NW-1);
766 break;
767 }
768 i--;
769 }
770 if (pos != null) {
771 // Move in front of any windows attached to this
772 // one.
773 WindowToken atoken =
774 mTokenMap.get(((WindowState)pos).mClient.asBinder());
775 if (atoken != null) {
776 final int NC = atoken.windows.size();
777 if (NC > 0) {
778 WindowState top = atoken.windows.get(NC-1);
779 if (top.mSubLayer >= 0) {
780 pos = top;
781 }
782 }
783 }
784 placeWindowAfter(pos, win);
785 } else {
786 // Just search for the start of this layer.
787 final int myLayer = win.mBaseLayer;
788 for (i=0; i<N; i++) {
789 WindowState w = (WindowState)localmWindows.get(i);
790 if (w.mBaseLayer > myLayer) {
791 break;
792 }
793 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700794 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
795 TAG, "Adding window " + win + " at "
796 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 localmWindows.add(i, win);
798 }
799 }
800 }
801 } else {
802 // Figure out where window should go, based on layer.
803 final int myLayer = win.mBaseLayer;
804 for (i=N-1; i>=0; i--) {
805 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
806 i++;
807 break;
808 }
809 }
810 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700811 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
812 TAG, "Adding window " + win + " at "
813 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 localmWindows.add(i, win);
815 }
816 if (addToToken) {
817 token.windows.add(tokenWindowsPos, win);
818 }
819
820 } else {
821 // Figure out this window's ordering relative to the window
822 // it is attached to.
823 final int NA = token.windows.size();
824 final int sublayer = win.mSubLayer;
825 int largestSublayer = Integer.MIN_VALUE;
826 WindowState windowWithLargestSublayer = null;
827 for (i=0; i<NA; i++) {
828 WindowState w = token.windows.get(i);
829 final int wSublayer = w.mSubLayer;
830 if (wSublayer >= largestSublayer) {
831 largestSublayer = wSublayer;
832 windowWithLargestSublayer = w;
833 }
834 if (sublayer < 0) {
835 // For negative sublayers, we go below all windows
836 // in the same sublayer.
837 if (wSublayer >= sublayer) {
838 if (addToToken) {
839 token.windows.add(i, win);
840 }
841 placeWindowBefore(
842 wSublayer >= 0 ? attached : w, win);
843 break;
844 }
845 } else {
846 // For positive sublayers, we go above all windows
847 // in the same sublayer.
848 if (wSublayer > sublayer) {
849 if (addToToken) {
850 token.windows.add(i, win);
851 }
852 placeWindowBefore(w, win);
853 break;
854 }
855 }
856 }
857 if (i >= NA) {
858 if (addToToken) {
859 token.windows.add(win);
860 }
861 if (sublayer < 0) {
862 placeWindowBefore(attached, win);
863 } else {
864 placeWindowAfter(largestSublayer >= 0
865 ? windowWithLargestSublayer
866 : attached,
867 win);
868 }
869 }
870 }
Romain Guy06882f82009-06-10 13:36:04 -0700871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 if (win.mAppToken != null && addToToken) {
873 win.mAppToken.allAppWindows.add(win);
874 }
875 }
Romain Guy06882f82009-06-10 13:36:04 -0700876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 static boolean canBeImeTarget(WindowState w) {
878 final int fl = w.mAttrs.flags
879 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
880 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
881 return w.isVisibleOrAdding();
882 }
883 return false;
884 }
Romain Guy06882f82009-06-10 13:36:04 -0700885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
887 final ArrayList localmWindows = mWindows;
888 final int N = localmWindows.size();
889 WindowState w = null;
890 int i = N;
891 while (i > 0) {
892 i--;
893 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
896 // + Integer.toHexString(w.mAttrs.flags));
897 if (canBeImeTarget(w)) {
898 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 // Yet more tricksyness! If this window is a "starting"
901 // window, we do actually want to be on top of it, but
902 // it is not -really- where input will go. So if the caller
903 // is not actually looking to move the IME, look down below
904 // for a real window to target...
905 if (!willMove
906 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
907 && i > 0) {
908 WindowState wb = (WindowState)localmWindows.get(i-1);
909 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
910 i--;
911 w = wb;
912 }
913 }
914 break;
915 }
916 }
Romain Guy06882f82009-06-10 13:36:04 -0700917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
921 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 if (willMove && w != null) {
924 final WindowState curTarget = mInputMethodTarget;
925 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 // Now some fun for dealing with window animations that
928 // modify the Z order. We need to look at all windows below
929 // the current target that are in this app, finding the highest
930 // visible one in layering.
931 AppWindowToken token = curTarget.mAppToken;
932 WindowState highestTarget = null;
933 int highestPos = 0;
934 if (token.animating || token.animation != null) {
935 int pos = 0;
936 pos = localmWindows.indexOf(curTarget);
937 while (pos >= 0) {
938 WindowState win = (WindowState)localmWindows.get(pos);
939 if (win.mAppToken != token) {
940 break;
941 }
942 if (!win.mRemoved) {
943 if (highestTarget == null || win.mAnimLayer >
944 highestTarget.mAnimLayer) {
945 highestTarget = win;
946 highestPos = pos;
947 }
948 }
949 pos--;
950 }
951 }
Romain Guy06882f82009-06-10 13:36:04 -0700952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700954 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 + mNextAppTransition + " " + highestTarget
956 + " animating=" + highestTarget.isAnimating()
957 + " layer=" + highestTarget.mAnimLayer
958 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700959
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700960 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 // If we are currently setting up for an animation,
962 // hold everything until we can find out what will happen.
963 mInputMethodTargetWaitingAnim = true;
964 mInputMethodTarget = highestTarget;
965 return highestPos + 1;
966 } else if (highestTarget.isAnimating() &&
967 highestTarget.mAnimLayer > w.mAnimLayer) {
968 // If the window we are currently targeting is involved
969 // with an animation, and it is on top of the next target
970 // we will be over, then hold off on moving until
971 // that is done.
972 mInputMethodTarget = highestTarget;
973 return highestPos + 1;
974 }
975 }
976 }
977 }
Romain Guy06882f82009-06-10 13:36:04 -0700978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 //Log.i(TAG, "Placing input method @" + (i+1));
980 if (w != null) {
981 if (willMove) {
982 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700983 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
985 + mInputMethodTarget + " to " + w, e);
986 mInputMethodTarget = w;
987 if (w.mAppToken != null) {
988 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
989 } else {
990 setInputMethodAnimLayerAdjustment(0);
991 }
992 }
993 return i+1;
994 }
995 if (willMove) {
996 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700997 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
999 + mInputMethodTarget + " to null", e);
1000 mInputMethodTarget = null;
1001 setInputMethodAnimLayerAdjustment(0);
1002 }
1003 return -1;
1004 }
Romain Guy06882f82009-06-10 13:36:04 -07001005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 void addInputMethodWindowToListLocked(WindowState win) {
1007 int pos = findDesiredInputMethodWindowIndexLocked(true);
1008 if (pos >= 0) {
1009 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001010 if (DEBUG_WINDOW_MOVEMENT) Log.v(
1011 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 mWindows.add(pos, win);
1013 moveInputMethodDialogsLocked(pos+1);
1014 return;
1015 }
1016 win.mTargetAppToken = null;
1017 addWindowToListInOrderLocked(win, true);
1018 moveInputMethodDialogsLocked(pos);
1019 }
Romain Guy06882f82009-06-10 13:36:04 -07001020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 void setInputMethodAnimLayerAdjustment(int adj) {
1022 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1023 mInputMethodAnimLayerAdjustment = adj;
1024 WindowState imw = mInputMethodWindow;
1025 if (imw != null) {
1026 imw.mAnimLayer = imw.mLayer + adj;
1027 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1028 + " anim layer: " + imw.mAnimLayer);
1029 int wi = imw.mChildWindows.size();
1030 while (wi > 0) {
1031 wi--;
1032 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1033 cw.mAnimLayer = cw.mLayer + adj;
1034 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1035 + " anim layer: " + cw.mAnimLayer);
1036 }
1037 }
1038 int di = mInputMethodDialogs.size();
1039 while (di > 0) {
1040 di --;
1041 imw = mInputMethodDialogs.get(di);
1042 imw.mAnimLayer = imw.mLayer + adj;
1043 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1044 + " anim layer: " + imw.mAnimLayer);
1045 }
1046 }
Romain Guy06882f82009-06-10 13:36:04 -07001047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1049 int wpos = mWindows.indexOf(win);
1050 if (wpos >= 0) {
1051 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001052 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 mWindows.remove(wpos);
1054 int NC = win.mChildWindows.size();
1055 while (NC > 0) {
1056 NC--;
1057 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1058 int cpos = mWindows.indexOf(cw);
1059 if (cpos >= 0) {
1060 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001061 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1062 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 mWindows.remove(cpos);
1064 }
1065 }
1066 }
1067 return interestingPos;
1068 }
Romain Guy06882f82009-06-10 13:36:04 -07001069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 private void reAddWindowToListInOrderLocked(WindowState win) {
1071 addWindowToListInOrderLocked(win, false);
1072 // This is a hack to get all of the child windows added as well
1073 // at the right position. Child windows should be rare and
1074 // this case should be rare, so it shouldn't be that big a deal.
1075 int wpos = mWindows.indexOf(win);
1076 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001077 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1078 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 mWindows.remove(wpos);
1080 reAddWindowLocked(wpos, win);
1081 }
1082 }
Romain Guy06882f82009-06-10 13:36:04 -07001083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 void logWindowList(String prefix) {
1085 int N = mWindows.size();
1086 while (N > 0) {
1087 N--;
1088 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1089 }
1090 }
Romain Guy06882f82009-06-10 13:36:04 -07001091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 void moveInputMethodDialogsLocked(int pos) {
1093 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 final int N = dialogs.size();
1096 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1097 for (int i=0; i<N; i++) {
1098 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1099 }
1100 if (DEBUG_INPUT_METHOD) {
1101 Log.v(TAG, "Window list w/pos=" + pos);
1102 logWindowList(" ");
1103 }
Romain Guy06882f82009-06-10 13:36:04 -07001104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 if (pos >= 0) {
1106 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1107 if (pos < mWindows.size()) {
1108 WindowState wp = (WindowState)mWindows.get(pos);
1109 if (wp == mInputMethodWindow) {
1110 pos++;
1111 }
1112 }
1113 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1114 for (int i=0; i<N; i++) {
1115 WindowState win = dialogs.get(i);
1116 win.mTargetAppToken = targetAppToken;
1117 pos = reAddWindowLocked(pos, win);
1118 }
1119 if (DEBUG_INPUT_METHOD) {
1120 Log.v(TAG, "Final window list:");
1121 logWindowList(" ");
1122 }
1123 return;
1124 }
1125 for (int i=0; i<N; i++) {
1126 WindowState win = dialogs.get(i);
1127 win.mTargetAppToken = null;
1128 reAddWindowToListInOrderLocked(win);
1129 if (DEBUG_INPUT_METHOD) {
1130 Log.v(TAG, "No IM target, final list:");
1131 logWindowList(" ");
1132 }
1133 }
1134 }
Romain Guy06882f82009-06-10 13:36:04 -07001135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1137 final WindowState imWin = mInputMethodWindow;
1138 final int DN = mInputMethodDialogs.size();
1139 if (imWin == null && DN == 0) {
1140 return false;
1141 }
Romain Guy06882f82009-06-10 13:36:04 -07001142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1144 if (imPos >= 0) {
1145 // In this case, the input method windows are to be placed
1146 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 // First check to see if the input method windows are already
1149 // located here, and contiguous.
1150 final int N = mWindows.size();
1151 WindowState firstImWin = imPos < N
1152 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 // Figure out the actual input method window that should be
1155 // at the bottom of their stack.
1156 WindowState baseImWin = imWin != null
1157 ? imWin : mInputMethodDialogs.get(0);
1158 if (baseImWin.mChildWindows.size() > 0) {
1159 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1160 if (cw.mSubLayer < 0) baseImWin = cw;
1161 }
Romain Guy06882f82009-06-10 13:36:04 -07001162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 if (firstImWin == baseImWin) {
1164 // The windows haven't moved... but are they still contiguous?
1165 // First find the top IM window.
1166 int pos = imPos+1;
1167 while (pos < N) {
1168 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1169 break;
1170 }
1171 pos++;
1172 }
1173 pos++;
1174 // Now there should be no more input method windows above.
1175 while (pos < N) {
1176 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1177 break;
1178 }
1179 pos++;
1180 }
1181 if (pos >= N) {
1182 // All is good!
1183 return false;
1184 }
1185 }
Romain Guy06882f82009-06-10 13:36:04 -07001186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 if (imWin != null) {
1188 if (DEBUG_INPUT_METHOD) {
1189 Log.v(TAG, "Moving IM from " + imPos);
1190 logWindowList(" ");
1191 }
1192 imPos = tmpRemoveWindowLocked(imPos, imWin);
1193 if (DEBUG_INPUT_METHOD) {
1194 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1195 logWindowList(" ");
1196 }
1197 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1198 reAddWindowLocked(imPos, imWin);
1199 if (DEBUG_INPUT_METHOD) {
1200 Log.v(TAG, "List after moving IM to " + imPos + ":");
1201 logWindowList(" ");
1202 }
1203 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1204 } else {
1205 moveInputMethodDialogsLocked(imPos);
1206 }
Romain Guy06882f82009-06-10 13:36:04 -07001207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 } else {
1209 // In this case, the input method windows go in a fixed layer,
1210 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 if (imWin != null) {
1213 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1214 tmpRemoveWindowLocked(0, imWin);
1215 imWin.mTargetAppToken = null;
1216 reAddWindowToListInOrderLocked(imWin);
1217 if (DEBUG_INPUT_METHOD) {
1218 Log.v(TAG, "List with no IM target:");
1219 logWindowList(" ");
1220 }
1221 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1222 } else {
1223 moveInputMethodDialogsLocked(-1);;
1224 }
Romain Guy06882f82009-06-10 13:36:04 -07001225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
Romain Guy06882f82009-06-10 13:36:04 -07001227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 if (needAssignLayers) {
1229 assignLayersLocked();
1230 }
Romain Guy06882f82009-06-10 13:36:04 -07001231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 return true;
1233 }
Romain Guy06882f82009-06-10 13:36:04 -07001234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 void adjustInputMethodDialogsLocked() {
1236 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1237 }
Romain Guy06882f82009-06-10 13:36:04 -07001238
Dianne Hackborn25994b42009-09-04 14:21:19 -07001239 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1240 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1241 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1242 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1243 ? wallpaperTarget.mAppToken.animation : null)
1244 + " upper=" + mUpperWallpaperTarget
1245 + " lower=" + mLowerWallpaperTarget);
1246 return (wallpaperTarget != null
1247 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1248 && wallpaperTarget.mAppToken.animation != null)))
1249 || mUpperWallpaperTarget != null
1250 || mLowerWallpaperTarget != null;
1251 }
1252
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001253 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1254 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1255
1256 int adjustWallpaperWindowsLocked() {
1257 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001258
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001259 final int dw = mDisplay.getWidth();
1260 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001261
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001262 // First find top-most window that has asked to be on top of the
1263 // wallpaper; all wallpapers go behind it.
1264 final ArrayList localmWindows = mWindows;
1265 int N = localmWindows.size();
1266 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001267 WindowState foundW = null;
1268 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001269 WindowState topCurW = null;
1270 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001271 int i = N;
1272 while (i > 0) {
1273 i--;
1274 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001275 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1276 if (topCurW == null) {
1277 topCurW = w;
1278 topCurI = i;
1279 }
1280 continue;
1281 }
1282 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001283 if (w.mAppToken != null) {
1284 // If this window's app token is hidden and not animating,
1285 // it is of no interest to us.
1286 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1287 if (DEBUG_WALLPAPER) Log.v(TAG,
1288 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001289 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001290 continue;
1291 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001292 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001293 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1294 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1295 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001296 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001297 && (mWallpaperTarget == w
1298 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001299 if (DEBUG_WALLPAPER) Log.v(TAG,
1300 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001301 foundW = w;
1302 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001303 if (w == mWallpaperTarget && ((w.mAppToken != null
1304 && w.mAppToken.animation != null)
1305 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001306 // The current wallpaper target is animating, so we'll
1307 // look behind it for another possible target and figure
1308 // out what is going on below.
1309 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1310 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001311 continue;
1312 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001313 break;
1314 }
1315 }
1316
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001317 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001318 // If we are currently waiting for an app transition, and either
1319 // the current target or the next target are involved with it,
1320 // then hold off on doing anything with the wallpaper.
1321 // Note that we are checking here for just whether the target
1322 // is part of an app token... which is potentially overly aggressive
1323 // (the app token may not be involved in the transition), but good
1324 // enough (we'll just wait until whatever transition is pending
1325 // executes).
1326 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001327 if (DEBUG_WALLPAPER) Log.v(TAG,
1328 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001329 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001330 }
1331 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001332 if (DEBUG_WALLPAPER) Log.v(TAG,
1333 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001334 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001335 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001336 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001337
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001338 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001339 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001340 Log.v(TAG, "New wallpaper target: " + foundW
1341 + " oldTarget: " + mWallpaperTarget);
1342 }
1343
1344 mLowerWallpaperTarget = null;
1345 mUpperWallpaperTarget = null;
1346
1347 WindowState oldW = mWallpaperTarget;
1348 mWallpaperTarget = foundW;
1349
1350 // Now what is happening... if the current and new targets are
1351 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001352 if (foundW != null && oldW != null) {
1353 boolean oldAnim = oldW.mAnimation != null
1354 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1355 boolean foundAnim = foundW.mAnimation != null
1356 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001357 if (DEBUG_WALLPAPER) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001358 Log.v(TAG, "New animation: " + foundAnim
1359 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001360 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001361 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001362 int oldI = localmWindows.indexOf(oldW);
1363 if (DEBUG_WALLPAPER) {
1364 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1365 }
1366 if (oldI >= 0) {
1367 if (DEBUG_WALLPAPER) {
1368 Log.v(TAG, "Animating wallpapers: old#" + oldI
1369 + "=" + oldW + "; new#" + foundI
1370 + "=" + foundW);
1371 }
1372
1373 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001374 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001375 if (DEBUG_WALLPAPER) {
1376 Log.v(TAG, "Old wallpaper still the target.");
1377 }
1378 mWallpaperTarget = oldW;
1379 }
1380
1381 // Now set the upper and lower wallpaper targets
1382 // correctly, and make sure that we are positioning
1383 // the wallpaper below the lower.
1384 if (foundI > oldI) {
1385 // The new target is on top of the old one.
1386 if (DEBUG_WALLPAPER) {
1387 Log.v(TAG, "Found target above old target.");
1388 }
1389 mUpperWallpaperTarget = foundW;
1390 mLowerWallpaperTarget = oldW;
1391 foundW = oldW;
1392 foundI = oldI;
1393 } else {
1394 // The new target is below the old one.
1395 if (DEBUG_WALLPAPER) {
1396 Log.v(TAG, "Found target below old target.");
1397 }
1398 mUpperWallpaperTarget = oldW;
1399 mLowerWallpaperTarget = foundW;
1400 }
1401 }
1402 }
1403 }
1404
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001405 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001406 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001407 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1408 || (mLowerWallpaperTarget.mAppToken != null
1409 && mLowerWallpaperTarget.mAppToken.animation != null);
1410 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1411 || (mUpperWallpaperTarget.mAppToken != null
1412 && mUpperWallpaperTarget.mAppToken.animation != null);
1413 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001414 if (DEBUG_WALLPAPER) {
1415 Log.v(TAG, "No longer animating wallpaper targets!");
1416 }
1417 mLowerWallpaperTarget = null;
1418 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001419 }
1420 }
1421
1422 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001423 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001424 // The window is visible to the compositor... but is it visible
1425 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001426 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001427 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001428
1429 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001430 // its layer adjustment. Only do this if we are not transfering
1431 // between two wallpaper targets.
1432 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001433 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001434 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001435
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001436 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1437 * TYPE_LAYER_MULTIPLIER
1438 + TYPE_LAYER_OFFSET;
1439
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001440 // Now w is the window we are supposed to be behind... but we
1441 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001442 // AND any starting window associated with it, AND below the
1443 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001444 while (foundI > 0) {
1445 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001446 if (wb.mBaseLayer < maxLayer &&
1447 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001448 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001449 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001450 // This window is not related to the previous one in any
1451 // interesting way, so stop here.
1452 break;
1453 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001454 foundW = wb;
1455 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001456 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001457 } else {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001458 if (DEBUG_WALLPAPER) Log.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001459 }
1460
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001461 if (foundW == null && topCurW != null) {
1462 // There is no wallpaper target, so it goes at the bottom.
1463 // We will assume it is the same place as last time, if known.
1464 foundW = topCurW;
1465 foundI = topCurI+1;
1466 } else {
1467 // Okay i is the position immediately above the wallpaper. Look at
1468 // what is below it for later.
1469 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1470 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001471
Dianne Hackborn284ac932009-08-28 10:34:25 -07001472 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001473 if (mWallpaperTarget.mWallpaperX >= 0) {
1474 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001475 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001476 }
1477 if (mWallpaperTarget.mWallpaperY >= 0) {
1478 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001479 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001480 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001481 }
1482
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001483 // Start stepping backwards from here, ensuring that our wallpaper windows
1484 // are correctly placed.
1485 int curTokenIndex = mWallpaperTokens.size();
1486 while (curTokenIndex > 0) {
1487 curTokenIndex--;
1488 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001489 if (token.hidden == visible) {
1490 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1491 token.hidden = !visible;
1492 // Need to do a layout to ensure the wallpaper now has the
1493 // correct size.
1494 mLayoutNeeded = true;
1495 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001496
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001497 int curWallpaperIndex = token.windows.size();
1498 while (curWallpaperIndex > 0) {
1499 curWallpaperIndex--;
1500 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001501
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001502 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001503 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001504 }
1505
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001506 // First, make sure the client has the current visibility
1507 // state.
1508 if (wallpaper.mWallpaperVisible != visible) {
1509 wallpaper.mWallpaperVisible = visible;
1510 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001511 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001512 "Setting visibility of wallpaper " + wallpaper
1513 + ": " + visible);
1514 wallpaper.mClient.dispatchAppVisibility(visible);
1515 } catch (RemoteException e) {
1516 }
1517 }
1518
1519 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001520 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1521 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001522
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001523 // First, if this window is at the current index, then all
1524 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001525 if (wallpaper == foundW) {
1526 foundI--;
1527 foundW = foundI > 0
1528 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001529 continue;
1530 }
1531
1532 // The window didn't match... the current wallpaper window,
1533 // wherever it is, is in the wrong place, so make sure it is
1534 // not in the list.
1535 int oldIndex = localmWindows.indexOf(wallpaper);
1536 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001537 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1538 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001539 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001540 if (oldIndex < foundI) {
1541 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001542 }
1543 }
1544
1545 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001546 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1547 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001548 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001549
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001550 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001551 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001552 }
1553 }
1554
1555 return changed;
1556 }
1557
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001558 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001559 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1560 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001561 mWallpaperAnimLayerAdjustment = adj;
1562 int curTokenIndex = mWallpaperTokens.size();
1563 while (curTokenIndex > 0) {
1564 curTokenIndex--;
1565 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1566 int curWallpaperIndex = token.windows.size();
1567 while (curWallpaperIndex > 0) {
1568 curWallpaperIndex--;
1569 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1570 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001571 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1572 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001573 }
1574 }
1575 }
1576
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001577 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1578 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001579 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001580 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001581 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001582 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001583 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1584 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1585 changed = wallpaperWin.mXOffset != offset;
1586 if (changed) {
1587 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1588 + wallpaperWin + " x: " + offset);
1589 wallpaperWin.mXOffset = offset;
1590 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001591 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001592 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001593 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001594 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001595 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001596
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001597 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001598 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001599 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1600 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1601 if (wallpaperWin.mYOffset != offset) {
1602 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1603 + wallpaperWin + " y: " + offset);
1604 changed = true;
1605 wallpaperWin.mYOffset = offset;
1606 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001607 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001608 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001609 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001610 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001611 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001612
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001613 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001614 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001615 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1616 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1617 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001618 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001619 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001620 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001621 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001622 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1623 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001624 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001625 if (mWaitingOnWallpaper != null) {
1626 long start = SystemClock.uptimeMillis();
1627 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1628 < start) {
1629 try {
1630 if (DEBUG_WALLPAPER) Log.v(TAG,
1631 "Waiting for offset complete...");
1632 mWindowMap.wait(WALLPAPER_TIMEOUT);
1633 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001634 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001635 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1636 if ((start+WALLPAPER_TIMEOUT)
1637 < SystemClock.uptimeMillis()) {
1638 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1639 + wallpaperWin);
1640 mLastWallpaperTimeoutTime = start;
1641 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001642 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001643 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001644 }
1645 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001646 } catch (RemoteException e) {
1647 }
1648 }
1649
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001650 return changed;
1651 }
1652
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001653 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001654 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001655 if (mWaitingOnWallpaper != null &&
1656 mWaitingOnWallpaper.mClient.asBinder() == window) {
1657 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001658 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001659 }
1660 }
1661 }
1662
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001663 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001664 final int dw = mDisplay.getWidth();
1665 final int dh = mDisplay.getHeight();
1666
1667 boolean changed = false;
1668
1669 WindowState target = mWallpaperTarget;
1670 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001671 if (target.mWallpaperX >= 0) {
1672 mLastWallpaperX = target.mWallpaperX;
1673 } else if (changingTarget.mWallpaperX >= 0) {
1674 mLastWallpaperX = changingTarget.mWallpaperX;
1675 }
1676 if (target.mWallpaperY >= 0) {
1677 mLastWallpaperY = target.mWallpaperY;
1678 } else if (changingTarget.mWallpaperY >= 0) {
1679 mLastWallpaperY = changingTarget.mWallpaperY;
1680 }
1681 }
1682
1683 int curTokenIndex = mWallpaperTokens.size();
1684 while (curTokenIndex > 0) {
1685 curTokenIndex--;
1686 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1687 int curWallpaperIndex = token.windows.size();
1688 while (curWallpaperIndex > 0) {
1689 curWallpaperIndex--;
1690 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1691 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1692 wallpaper.computeShownFrameLocked();
1693 changed = true;
1694 // We only want to be synchronous with one wallpaper.
1695 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001696 }
1697 }
1698 }
1699
1700 return changed;
1701 }
1702
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001703 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001704 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001705 final int dw = mDisplay.getWidth();
1706 final int dh = mDisplay.getHeight();
1707
1708 int curTokenIndex = mWallpaperTokens.size();
1709 while (curTokenIndex > 0) {
1710 curTokenIndex--;
1711 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001712 if (token.hidden == visible) {
1713 token.hidden = !visible;
1714 // Need to do a layout to ensure the wallpaper now has the
1715 // correct size.
1716 mLayoutNeeded = true;
1717 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001718
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001719 int curWallpaperIndex = token.windows.size();
1720 while (curWallpaperIndex > 0) {
1721 curWallpaperIndex--;
1722 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1723 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001724 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001725 }
1726
1727 if (wallpaper.mWallpaperVisible != visible) {
1728 wallpaper.mWallpaperVisible = visible;
1729 try {
1730 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001731 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001732 + ": " + visible);
1733 wallpaper.mClient.dispatchAppVisibility(visible);
1734 } catch (RemoteException e) {
1735 }
1736 }
1737 }
1738 }
1739 }
1740
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001741 void sendPointerToWallpaperLocked(WindowState srcWin,
1742 MotionEvent pointer, long eventTime) {
1743 int curTokenIndex = mWallpaperTokens.size();
1744 while (curTokenIndex > 0) {
1745 curTokenIndex--;
1746 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1747 int curWallpaperIndex = token.windows.size();
1748 while (curWallpaperIndex > 0) {
1749 curWallpaperIndex--;
1750 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1751 if ((wallpaper.mAttrs.flags &
1752 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1753 continue;
1754 }
1755 try {
1756 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001757 if (srcWin != null) {
1758 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1759 srcWin.mFrame.top-wallpaper.mFrame.top);
1760 } else {
1761 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1762 }
1763 switch (pointer.getAction()) {
1764 case MotionEvent.ACTION_DOWN:
1765 mSendingPointersToWallpaper = true;
1766 break;
1767 case MotionEvent.ACTION_UP:
1768 mSendingPointersToWallpaper = false;
1769 break;
1770 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001771 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1772 } catch (RemoteException e) {
1773 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1774 }
1775 }
1776 }
1777 }
1778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 public int addWindow(Session session, IWindow client,
1780 WindowManager.LayoutParams attrs, int viewVisibility,
1781 Rect outContentInsets) {
1782 int res = mPolicy.checkAddPermission(attrs);
1783 if (res != WindowManagerImpl.ADD_OKAY) {
1784 return res;
1785 }
Romain Guy06882f82009-06-10 13:36:04 -07001786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 boolean reportNewConfig = false;
1788 WindowState attachedWindow = null;
1789 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 synchronized(mWindowMap) {
1792 // Instantiating a Display requires talking with the simulator,
1793 // so don't do it until we know the system is mostly up and
1794 // running.
1795 if (mDisplay == null) {
1796 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1797 mDisplay = wm.getDefaultDisplay();
1798 mQueue.setDisplay(mDisplay);
1799 reportNewConfig = true;
1800 }
Romain Guy06882f82009-06-10 13:36:04 -07001801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 if (mWindowMap.containsKey(client.asBinder())) {
1803 Log.w(TAG, "Window " + client + " is already added");
1804 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1805 }
1806
1807 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001808 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 if (attachedWindow == null) {
1810 Log.w(TAG, "Attempted to add window with token that is not a window: "
1811 + attrs.token + ". Aborting.");
1812 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1813 }
1814 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1815 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1816 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1817 + attrs.token + ". Aborting.");
1818 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1819 }
1820 }
1821
1822 boolean addToken = false;
1823 WindowToken token = mTokenMap.get(attrs.token);
1824 if (token == null) {
1825 if (attrs.type >= FIRST_APPLICATION_WINDOW
1826 && attrs.type <= LAST_APPLICATION_WINDOW) {
1827 Log.w(TAG, "Attempted to add application window with unknown token "
1828 + attrs.token + ". Aborting.");
1829 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1830 }
1831 if (attrs.type == TYPE_INPUT_METHOD) {
1832 Log.w(TAG, "Attempted to add input method window with unknown token "
1833 + attrs.token + ". Aborting.");
1834 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1835 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001836 if (attrs.type == TYPE_WALLPAPER) {
1837 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1838 + attrs.token + ". Aborting.");
1839 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 token = new WindowToken(attrs.token, -1, false);
1842 addToken = true;
1843 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1844 && attrs.type <= LAST_APPLICATION_WINDOW) {
1845 AppWindowToken atoken = token.appWindowToken;
1846 if (atoken == null) {
1847 Log.w(TAG, "Attempted to add window with non-application token "
1848 + token + ". Aborting.");
1849 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1850 } else if (atoken.removed) {
1851 Log.w(TAG, "Attempted to add window with exiting application token "
1852 + token + ". Aborting.");
1853 return WindowManagerImpl.ADD_APP_EXITING;
1854 }
1855 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1856 // No need for this guy!
1857 if (localLOGV) Log.v(
1858 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1859 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1860 }
1861 } else if (attrs.type == TYPE_INPUT_METHOD) {
1862 if (token.windowType != TYPE_INPUT_METHOD) {
1863 Log.w(TAG, "Attempted to add input method window with bad token "
1864 + attrs.token + ". Aborting.");
1865 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1866 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001867 } else if (attrs.type == TYPE_WALLPAPER) {
1868 if (token.windowType != TYPE_WALLPAPER) {
1869 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1870 + attrs.token + ". Aborting.");
1871 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 }
1874
1875 win = new WindowState(session, client, token,
1876 attachedWindow, attrs, viewVisibility);
1877 if (win.mDeathRecipient == null) {
1878 // Client has apparently died, so there is no reason to
1879 // continue.
1880 Log.w(TAG, "Adding window client " + client.asBinder()
1881 + " that is dead, aborting.");
1882 return WindowManagerImpl.ADD_APP_EXITING;
1883 }
1884
1885 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 res = mPolicy.prepareAddWindowLw(win, attrs);
1888 if (res != WindowManagerImpl.ADD_OKAY) {
1889 return res;
1890 }
1891
1892 // From now on, no exceptions or errors allowed!
1893
1894 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 if (addToken) {
1899 mTokenMap.put(attrs.token, token);
1900 mTokenList.add(token);
1901 }
1902 win.attach();
1903 mWindowMap.put(client.asBinder(), win);
1904
1905 if (attrs.type == TYPE_APPLICATION_STARTING &&
1906 token.appWindowToken != null) {
1907 token.appWindowToken.startingWindow = win;
1908 }
1909
1910 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 if (attrs.type == TYPE_INPUT_METHOD) {
1913 mInputMethodWindow = win;
1914 addInputMethodWindowToListLocked(win);
1915 imMayMove = false;
1916 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1917 mInputMethodDialogs.add(win);
1918 addWindowToListInOrderLocked(win, true);
1919 adjustInputMethodDialogsLocked();
1920 imMayMove = false;
1921 } else {
1922 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001923 if (attrs.type == TYPE_WALLPAPER) {
1924 mLastWallpaperTimeoutTime = 0;
1925 adjustWallpaperWindowsLocked();
1926 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001927 adjustWallpaperWindowsLocked();
1928 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 }
Romain Guy06882f82009-06-10 13:36:04 -07001930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 if (mInTouchMode) {
1936 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1937 }
1938 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1939 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1940 }
Romain Guy06882f82009-06-10 13:36:04 -07001941
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001942 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001944 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1945 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 imMayMove = false;
1947 }
1948 }
Romain Guy06882f82009-06-10 13:36:04 -07001949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001951 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
Romain Guy06882f82009-06-10 13:36:04 -07001953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 assignLayersLocked();
1955 // Don't do layout here, the window must call
1956 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 //dump();
1959
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001960 if (focusChanged) {
1961 if (mCurrentFocus != null) {
1962 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1963 }
1964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 if (localLOGV) Log.v(
1966 TAG, "New client " + client.asBinder()
1967 + ": window=" + win);
1968 }
1969
1970 // sendNewConfiguration() checks caller permissions so we must call it with
1971 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1972 // identity anyway, so it's safe to just clear & restore around this whole
1973 // block.
1974 final long origId = Binder.clearCallingIdentity();
1975 if (reportNewConfig) {
1976 sendNewConfiguration();
1977 } else {
1978 // Update Orientation after adding a window, only if the window needs to be
1979 // displayed right away
1980 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001981 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 sendNewConfiguration();
1983 }
1984 }
1985 }
1986 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 return res;
1989 }
Romain Guy06882f82009-06-10 13:36:04 -07001990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 public void removeWindow(Session session, IWindow client) {
1992 synchronized(mWindowMap) {
1993 WindowState win = windowForClientLocked(session, client);
1994 if (win == null) {
1995 return;
1996 }
1997 removeWindowLocked(session, win);
1998 }
1999 }
Romain Guy06882f82009-06-10 13:36:04 -07002000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 public void removeWindowLocked(Session session, WindowState win) {
2002
2003 if (localLOGV || DEBUG_FOCUS) Log.v(
2004 TAG, "Remove " + win + " client="
2005 + Integer.toHexString(System.identityHashCode(
2006 win.mClient.asBinder()))
2007 + ", surface=" + win.mSurface);
2008
2009 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 if (DEBUG_APP_TRANSITIONS) Log.v(
2012 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2013 + " mExiting=" + win.mExiting
2014 + " isAnimating=" + win.isAnimating()
2015 + " app-animation="
2016 + (win.mAppToken != null ? win.mAppToken.animation : null)
2017 + " inPendingTransaction="
2018 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2019 + " mDisplayFrozen=" + mDisplayFrozen);
2020 // Visibility of the removed window. Will be used later to update orientation later on.
2021 boolean wasVisible = false;
2022 // First, see if we need to run an animation. If we do, we have
2023 // to hold off on removing the window until the animation is done.
2024 // If the display is frozen, just remove immediately, since the
2025 // animation wouldn't be seen.
2026 if (win.mSurface != null && !mDisplayFrozen) {
2027 // If we are not currently running the exit animation, we
2028 // need to see about starting one.
2029 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2032 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2033 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2034 }
2035 // Try starting an animation.
2036 if (applyAnimationLocked(win, transit, false)) {
2037 win.mExiting = true;
2038 }
2039 }
2040 if (win.mExiting || win.isAnimating()) {
2041 // The exit animation is running... wait for it!
2042 //Log.i(TAG, "*** Running exit animation...");
2043 win.mExiting = true;
2044 win.mRemoveOnExit = true;
2045 mLayoutNeeded = true;
2046 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2047 performLayoutAndPlaceSurfacesLocked();
2048 if (win.mAppToken != null) {
2049 win.mAppToken.updateReportedVisibilityLocked();
2050 }
2051 //dump();
2052 Binder.restoreCallingIdentity(origId);
2053 return;
2054 }
2055 }
2056
2057 removeWindowInnerLocked(session, win);
2058 // Removing a visible window will effect the computed orientation
2059 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002060 if (wasVisible && computeForcedAppOrientationLocked()
2061 != mForcedAppOrientation) {
2062 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 }
2064 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2065 Binder.restoreCallingIdentity(origId);
2066 }
Romain Guy06882f82009-06-10 13:36:04 -07002067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002069 mKeyWaiter.finishedKey(session, win.mClient, true,
2070 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2072 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 if (mInputMethodTarget == win) {
2077 moveInputMethodWindowsIfNeededLocked(false);
2078 }
Romain Guy06882f82009-06-10 13:36:04 -07002079
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002080 if (false) {
2081 RuntimeException e = new RuntimeException("here");
2082 e.fillInStackTrace();
2083 Log.w(TAG, "Removing window " + win, e);
2084 }
2085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 mPolicy.removeWindowLw(win);
2087 win.removeLocked();
2088
2089 mWindowMap.remove(win.mClient.asBinder());
2090 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002091 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092
2093 if (mInputMethodWindow == win) {
2094 mInputMethodWindow = null;
2095 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2096 mInputMethodDialogs.remove(win);
2097 }
Romain Guy06882f82009-06-10 13:36:04 -07002098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 final WindowToken token = win.mToken;
2100 final AppWindowToken atoken = win.mAppToken;
2101 token.windows.remove(win);
2102 if (atoken != null) {
2103 atoken.allAppWindows.remove(win);
2104 }
2105 if (localLOGV) Log.v(
2106 TAG, "**** Removing window " + win + ": count="
2107 + token.windows.size());
2108 if (token.windows.size() == 0) {
2109 if (!token.explicit) {
2110 mTokenMap.remove(token.token);
2111 mTokenList.remove(token);
2112 } else if (atoken != null) {
2113 atoken.firstWindowDrawn = false;
2114 }
2115 }
2116
2117 if (atoken != null) {
2118 if (atoken.startingWindow == win) {
2119 atoken.startingWindow = null;
2120 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2121 // If this is the last window and we had requested a starting
2122 // transition window, well there is no point now.
2123 atoken.startingData = null;
2124 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2125 // If this is the last window except for a starting transition
2126 // window, we need to get rid of the starting transition.
2127 if (DEBUG_STARTING_WINDOW) {
2128 Log.v(TAG, "Schedule remove starting " + token
2129 + ": no more real windows");
2130 }
2131 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2132 mH.sendMessage(m);
2133 }
2134 }
Romain Guy06882f82009-06-10 13:36:04 -07002135
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002136 if (win.mAttrs.type == TYPE_WALLPAPER) {
2137 mLastWallpaperTimeoutTime = 0;
2138 adjustWallpaperWindowsLocked();
2139 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002140 adjustWallpaperWindowsLocked();
2141 }
2142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 if (!mInLayout) {
2144 assignLayersLocked();
2145 mLayoutNeeded = true;
2146 performLayoutAndPlaceSurfacesLocked();
2147 if (win.mAppToken != null) {
2148 win.mAppToken.updateReportedVisibilityLocked();
2149 }
2150 }
2151 }
2152
2153 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2154 long origId = Binder.clearCallingIdentity();
2155 try {
2156 synchronized (mWindowMap) {
2157 WindowState w = windowForClientLocked(session, client);
2158 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002159 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 Surface.openTransaction();
2161 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002162 if (SHOW_TRANSACTIONS) Log.i(
2163 TAG, " SURFACE " + w.mSurface
2164 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 w.mSurface.setTransparentRegionHint(region);
2166 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002167 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 Surface.closeTransaction();
2169 }
2170 }
2171 }
2172 } finally {
2173 Binder.restoreCallingIdentity(origId);
2174 }
2175 }
2176
2177 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002178 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 Rect visibleInsets) {
2180 long origId = Binder.clearCallingIdentity();
2181 try {
2182 synchronized (mWindowMap) {
2183 WindowState w = windowForClientLocked(session, client);
2184 if (w != null) {
2185 w.mGivenInsetsPending = false;
2186 w.mGivenContentInsets.set(contentInsets);
2187 w.mGivenVisibleInsets.set(visibleInsets);
2188 w.mTouchableInsets = touchableInsets;
2189 mLayoutNeeded = true;
2190 performLayoutAndPlaceSurfacesLocked();
2191 }
2192 }
2193 } finally {
2194 Binder.restoreCallingIdentity(origId);
2195 }
2196 }
Romain Guy06882f82009-06-10 13:36:04 -07002197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 public void getWindowDisplayFrame(Session session, IWindow client,
2199 Rect outDisplayFrame) {
2200 synchronized(mWindowMap) {
2201 WindowState win = windowForClientLocked(session, client);
2202 if (win == null) {
2203 outDisplayFrame.setEmpty();
2204 return;
2205 }
2206 outDisplayFrame.set(win.mDisplayFrame);
2207 }
2208 }
2209
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002210 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2211 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002212 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2213 window.mWallpaperX = x;
2214 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002215 window.mWallpaperXStep = xStep;
2216 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002217 if (updateWallpaperOffsetLocked(window, true)) {
2218 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002219 }
2220 }
2221 }
2222
Dianne Hackborn75804932009-10-20 20:15:20 -07002223 void wallpaperCommandComplete(IBinder window, Bundle result) {
2224 synchronized (mWindowMap) {
2225 if (mWaitingOnWallpaper != null &&
2226 mWaitingOnWallpaper.mClient.asBinder() == window) {
2227 mWaitingOnWallpaper = null;
2228 mWindowMap.notifyAll();
2229 }
2230 }
2231 }
2232
2233 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2234 String action, int x, int y, int z, Bundle extras, boolean sync) {
2235 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2236 || window == mUpperWallpaperTarget) {
2237 boolean doWait = sync;
2238 int curTokenIndex = mWallpaperTokens.size();
2239 while (curTokenIndex > 0) {
2240 curTokenIndex--;
2241 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2242 int curWallpaperIndex = token.windows.size();
2243 while (curWallpaperIndex > 0) {
2244 curWallpaperIndex--;
2245 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2246 try {
2247 wallpaper.mClient.dispatchWallpaperCommand(action,
2248 x, y, z, extras, sync);
2249 // We only want to be synchronous with one wallpaper.
2250 sync = false;
2251 } catch (RemoteException e) {
2252 }
2253 }
2254 }
2255
2256 if (doWait) {
2257 // XXX Need to wait for result.
2258 }
2259 }
2260
2261 return null;
2262 }
2263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 public int relayoutWindow(Session session, IWindow client,
2265 WindowManager.LayoutParams attrs, int requestedWidth,
2266 int requestedHeight, int viewVisibility, boolean insetsPending,
2267 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2268 Surface outSurface) {
2269 boolean displayed = false;
2270 boolean inTouchMode;
2271 Configuration newConfig = null;
2272 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 synchronized(mWindowMap) {
2275 WindowState win = windowForClientLocked(session, client);
2276 if (win == null) {
2277 return 0;
2278 }
2279 win.mRequestedWidth = requestedWidth;
2280 win.mRequestedHeight = requestedHeight;
2281
2282 if (attrs != null) {
2283 mPolicy.adjustWindowParamsLw(attrs);
2284 }
Romain Guy06882f82009-06-10 13:36:04 -07002285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 int attrChanges = 0;
2287 int flagChanges = 0;
2288 if (attrs != null) {
2289 flagChanges = win.mAttrs.flags ^= attrs.flags;
2290 attrChanges = win.mAttrs.copyFrom(attrs);
2291 }
2292
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002293 if (DEBUG_LAYOUT) Log.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294
2295 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2296 win.mAlpha = attrs.alpha;
2297 }
2298
2299 final boolean scaledWindow =
2300 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2301
2302 if (scaledWindow) {
2303 // requested{Width|Height} Surface's physical size
2304 // attrs.{width|height} Size on screen
2305 win.mHScale = (attrs.width != requestedWidth) ?
2306 (attrs.width / (float)requestedWidth) : 1.0f;
2307 win.mVScale = (attrs.height != requestedHeight) ?
2308 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002309 } else {
2310 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 }
2312
2313 boolean imMayMove = (flagChanges&(
2314 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2315 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 boolean focusMayChange = win.mViewVisibility != viewVisibility
2318 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2319 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002320
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002321 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2322 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 win.mRelayoutCalled = true;
2325 final int oldVisibility = win.mViewVisibility;
2326 win.mViewVisibility = viewVisibility;
2327 if (viewVisibility == View.VISIBLE &&
2328 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2329 displayed = !win.isVisibleLw();
2330 if (win.mExiting) {
2331 win.mExiting = false;
2332 win.mAnimation = null;
2333 }
2334 if (win.mDestroying) {
2335 win.mDestroying = false;
2336 mDestroySurface.remove(win);
2337 }
2338 if (oldVisibility == View.GONE) {
2339 win.mEnterAnimationPending = true;
2340 }
2341 if (displayed && win.mSurface != null && !win.mDrawPending
2342 && !win.mCommitDrawPending && !mDisplayFrozen) {
2343 applyEnterAnimationLocked(win);
2344 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002345 if (displayed && (win.mAttrs.flags
2346 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2347 win.mTurnOnScreen = true;
2348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2350 // To change the format, we need to re-build the surface.
2351 win.destroySurfaceLocked();
2352 displayed = true;
2353 }
2354 try {
2355 Surface surface = win.createSurfaceLocked();
2356 if (surface != null) {
2357 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002358 win.mReportDestroySurface = false;
2359 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002360 if (SHOW_TRANSACTIONS) Log.i(TAG,
2361 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002363 // For some reason there isn't a surface. Clear the
2364 // caller's object so they see the same state.
2365 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 }
2367 } catch (Exception e) {
2368 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002369 + client + " (" + win.mAttrs.getTitle() + ")",
2370 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 Binder.restoreCallingIdentity(origId);
2372 return 0;
2373 }
2374 if (displayed) {
2375 focusMayChange = true;
2376 }
2377 if (win.mAttrs.type == TYPE_INPUT_METHOD
2378 && mInputMethodWindow == null) {
2379 mInputMethodWindow = win;
2380 imMayMove = true;
2381 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002382 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2383 && win.mAppToken != null
2384 && win.mAppToken.startingWindow != null) {
2385 // Special handling of starting window over the base
2386 // window of the app: propagate lock screen flags to it,
2387 // to provide the correct semantics while starting.
2388 final int mask =
2389 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
2390 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
2391 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2392 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 } else {
2395 win.mEnterAnimationPending = false;
2396 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002397 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2398 + ": mExiting=" + win.mExiting
2399 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 // If we are not currently running the exit animation, we
2401 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002402 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 // Try starting an animation; if there isn't one, we
2404 // can destroy the surface right away.
2405 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2406 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2407 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2408 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002409 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002411 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 win.mExiting = true;
2413 mKeyWaiter.finishedKey(session, client, true,
2414 KeyWaiter.RETURN_NOTHING);
2415 } else if (win.isAnimating()) {
2416 // Currently in a hide animation... turn this into
2417 // an exit.
2418 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002419 } else if (win == mWallpaperTarget) {
2420 // If the wallpaper is currently behind this
2421 // window, we need to change both of them inside
2422 // of a transaction to avoid artifacts.
2423 win.mExiting = true;
2424 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 } else {
2426 if (mInputMethodWindow == win) {
2427 mInputMethodWindow = null;
2428 }
2429 win.destroySurfaceLocked();
2430 }
2431 }
2432 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002433
2434 if (win.mSurface == null || (win.getAttrs().flags
2435 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2436 || win.mSurfacePendingDestroy) {
2437 // We are being called from a local process, which
2438 // means outSurface holds its current surface. Ensure the
2439 // surface object is cleared, but we don't want it actually
2440 // destroyed at this point.
2441 win.mSurfacePendingDestroy = false;
2442 outSurface.release();
2443 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2444 } else if (win.mSurface != null) {
2445 if (DEBUG_VISIBILITY) Log.i(TAG,
2446 "Keeping surface, will report destroy: " + win);
2447 win.mReportDestroySurface = true;
2448 outSurface.copyFrom(win.mSurface);
2449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 }
2451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 if (focusMayChange) {
2453 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2454 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 imMayMove = false;
2456 }
2457 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2458 }
Romain Guy06882f82009-06-10 13:36:04 -07002459
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002460 // updateFocusedWindowLocked() already assigned layers so we only need to
2461 // reassign them at this point if the IM window state gets shuffled
2462 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002465 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2466 // Little hack here -- we -should- be able to rely on the
2467 // function to return true if the IME has moved and needs
2468 // its layer recomputed. However, if the IME was hidden
2469 // and isn't actually moved in the list, its layer may be
2470 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 assignLayers = true;
2472 }
2473 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002474 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002475 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002476 assignLayers = true;
2477 }
2478 }
Romain Guy06882f82009-06-10 13:36:04 -07002479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 mLayoutNeeded = true;
2481 win.mGivenInsetsPending = insetsPending;
2482 if (assignLayers) {
2483 assignLayersLocked();
2484 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002485 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002487 if (displayed && win.mIsWallpaper) {
2488 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002489 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 if (win.mAppToken != null) {
2492 win.mAppToken.updateReportedVisibilityLocked();
2493 }
2494 outFrame.set(win.mFrame);
2495 outContentInsets.set(win.mContentInsets);
2496 outVisibleInsets.set(win.mVisibleInsets);
2497 if (localLOGV) Log.v(
2498 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002499 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 + ", requestedHeight=" + requestedHeight
2501 + ", viewVisibility=" + viewVisibility
2502 + "\nRelayout returning frame=" + outFrame
2503 + ", surface=" + outSurface);
2504
2505 if (localLOGV || DEBUG_FOCUS) Log.v(
2506 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2507
2508 inTouchMode = mInTouchMode;
2509 }
2510
2511 if (newConfig != null) {
2512 sendNewConfiguration();
2513 }
Romain Guy06882f82009-06-10 13:36:04 -07002514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2518 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2519 }
2520
2521 public void finishDrawingWindow(Session session, IWindow client) {
2522 final long origId = Binder.clearCallingIdentity();
2523 synchronized(mWindowMap) {
2524 WindowState win = windowForClientLocked(session, client);
2525 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002526 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2527 adjustWallpaperWindowsLocked();
2528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 mLayoutNeeded = true;
2530 performLayoutAndPlaceSurfacesLocked();
2531 }
2532 }
2533 Binder.restoreCallingIdentity(origId);
2534 }
2535
2536 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2537 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2538 + (lp != null ? lp.packageName : null)
2539 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2540 if (lp != null && lp.windowAnimations != 0) {
2541 // If this is a system resource, don't try to load it from the
2542 // application resources. It is nice to avoid loading application
2543 // resources if we can.
2544 String packageName = lp.packageName != null ? lp.packageName : "android";
2545 int resId = lp.windowAnimations;
2546 if ((resId&0xFF000000) == 0x01000000) {
2547 packageName = "android";
2548 }
2549 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2550 + packageName);
2551 return AttributeCache.instance().get(packageName, resId,
2552 com.android.internal.R.styleable.WindowAnimation);
2553 }
2554 return null;
2555 }
Romain Guy06882f82009-06-10 13:36:04 -07002556
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002557 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2558 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2559 + packageName + " resId=0x" + Integer.toHexString(resId));
2560 if (packageName != null) {
2561 if ((resId&0xFF000000) == 0x01000000) {
2562 packageName = "android";
2563 }
2564 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2565 + packageName);
2566 return AttributeCache.instance().get(packageName, resId,
2567 com.android.internal.R.styleable.WindowAnimation);
2568 }
2569 return null;
2570 }
2571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 private void applyEnterAnimationLocked(WindowState win) {
2573 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2574 if (win.mEnterAnimationPending) {
2575 win.mEnterAnimationPending = false;
2576 transit = WindowManagerPolicy.TRANSIT_ENTER;
2577 }
2578
2579 applyAnimationLocked(win, transit, true);
2580 }
2581
2582 private boolean applyAnimationLocked(WindowState win,
2583 int transit, boolean isEntrance) {
2584 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2585 // If we are trying to apply an animation, but already running
2586 // an animation of the same type, then just leave that one alone.
2587 return true;
2588 }
Romain Guy06882f82009-06-10 13:36:04 -07002589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 // Only apply an animation if the display isn't frozen. If it is
2591 // frozen, there is no reason to animate and it can cause strange
2592 // artifacts when we unfreeze the display if some different animation
2593 // is running.
2594 if (!mDisplayFrozen) {
2595 int anim = mPolicy.selectAnimationLw(win, transit);
2596 int attr = -1;
2597 Animation a = null;
2598 if (anim != 0) {
2599 a = AnimationUtils.loadAnimation(mContext, anim);
2600 } else {
2601 switch (transit) {
2602 case WindowManagerPolicy.TRANSIT_ENTER:
2603 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2604 break;
2605 case WindowManagerPolicy.TRANSIT_EXIT:
2606 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2607 break;
2608 case WindowManagerPolicy.TRANSIT_SHOW:
2609 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2610 break;
2611 case WindowManagerPolicy.TRANSIT_HIDE:
2612 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2613 break;
2614 }
2615 if (attr >= 0) {
2616 a = loadAnimation(win.mAttrs, attr);
2617 }
2618 }
2619 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2620 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2621 + " mAnimation=" + win.mAnimation
2622 + " isEntrance=" + isEntrance);
2623 if (a != null) {
2624 if (DEBUG_ANIM) {
2625 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002626 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2628 }
2629 win.setAnimation(a);
2630 win.mAnimationIsEntrance = isEntrance;
2631 }
2632 } else {
2633 win.clearAnimation();
2634 }
2635
2636 return win.mAnimation != null;
2637 }
2638
2639 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2640 int anim = 0;
2641 Context context = mContext;
2642 if (animAttr >= 0) {
2643 AttributeCache.Entry ent = getCachedAnimations(lp);
2644 if (ent != null) {
2645 context = ent.context;
2646 anim = ent.array.getResourceId(animAttr, 0);
2647 }
2648 }
2649 if (anim != 0) {
2650 return AnimationUtils.loadAnimation(context, anim);
2651 }
2652 return null;
2653 }
Romain Guy06882f82009-06-10 13:36:04 -07002654
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002655 private Animation loadAnimation(String packageName, int resId) {
2656 int anim = 0;
2657 Context context = mContext;
2658 if (resId >= 0) {
2659 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2660 if (ent != null) {
2661 context = ent.context;
2662 anim = resId;
2663 }
2664 }
2665 if (anim != 0) {
2666 return AnimationUtils.loadAnimation(context, anim);
2667 }
2668 return null;
2669 }
2670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 private boolean applyAnimationLocked(AppWindowToken wtoken,
2672 WindowManager.LayoutParams lp, int transit, boolean enter) {
2673 // Only apply an animation if the display isn't frozen. If it is
2674 // frozen, there is no reason to animate and it can cause strange
2675 // artifacts when we unfreeze the display if some different animation
2676 // is running.
2677 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002678 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002679 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002680 a = new FadeInOutAnimation(enter);
2681 if (DEBUG_ANIM) Log.v(TAG,
2682 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002683 } else if (mNextAppTransitionPackage != null) {
2684 a = loadAnimation(mNextAppTransitionPackage, enter ?
2685 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002686 } else {
2687 int animAttr = 0;
2688 switch (transit) {
2689 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2690 animAttr = enter
2691 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2692 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2693 break;
2694 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2695 animAttr = enter
2696 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2697 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2698 break;
2699 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2700 animAttr = enter
2701 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2702 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2703 break;
2704 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2705 animAttr = enter
2706 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2707 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2708 break;
2709 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2710 animAttr = enter
2711 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2712 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2713 break;
2714 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2715 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002716 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002717 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2718 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002719 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002720 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002721 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2722 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002723 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002724 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002725 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002726 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2727 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2728 break;
2729 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2730 animAttr = enter
2731 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2732 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2733 break;
2734 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2735 animAttr = enter
2736 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2737 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002738 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002739 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002740 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002741 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2742 + " anim=" + a
2743 + " animAttr=0x" + Integer.toHexString(animAttr)
2744 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 if (a != null) {
2747 if (DEBUG_ANIM) {
2748 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002749 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2751 }
2752 wtoken.setAnimation(a);
2753 }
2754 } else {
2755 wtoken.clearAnimation();
2756 }
2757
2758 return wtoken.animation != null;
2759 }
2760
2761 // -------------------------------------------------------------
2762 // Application Window Tokens
2763 // -------------------------------------------------------------
2764
2765 public void validateAppTokens(List tokens) {
2766 int v = tokens.size()-1;
2767 int m = mAppTokens.size()-1;
2768 while (v >= 0 && m >= 0) {
2769 AppWindowToken wtoken = mAppTokens.get(m);
2770 if (wtoken.removed) {
2771 m--;
2772 continue;
2773 }
2774 if (tokens.get(v) != wtoken.token) {
2775 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2776 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2777 }
2778 v--;
2779 m--;
2780 }
2781 while (v >= 0) {
2782 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2783 v--;
2784 }
2785 while (m >= 0) {
2786 AppWindowToken wtoken = mAppTokens.get(m);
2787 if (!wtoken.removed) {
2788 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2789 }
2790 m--;
2791 }
2792 }
2793
2794 boolean checkCallingPermission(String permission, String func) {
2795 // Quick check: if the calling permission is me, it's all okay.
2796 if (Binder.getCallingPid() == Process.myPid()) {
2797 return true;
2798 }
Romain Guy06882f82009-06-10 13:36:04 -07002799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 if (mContext.checkCallingPermission(permission)
2801 == PackageManager.PERMISSION_GRANTED) {
2802 return true;
2803 }
2804 String msg = "Permission Denial: " + func + " from pid="
2805 + Binder.getCallingPid()
2806 + ", uid=" + Binder.getCallingUid()
2807 + " requires " + permission;
2808 Log.w(TAG, msg);
2809 return false;
2810 }
Romain Guy06882f82009-06-10 13:36:04 -07002811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 AppWindowToken findAppWindowToken(IBinder token) {
2813 WindowToken wtoken = mTokenMap.get(token);
2814 if (wtoken == null) {
2815 return null;
2816 }
2817 return wtoken.appWindowToken;
2818 }
Romain Guy06882f82009-06-10 13:36:04 -07002819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 public void addWindowToken(IBinder token, int type) {
2821 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2822 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002823 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 }
Romain Guy06882f82009-06-10 13:36:04 -07002825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 synchronized(mWindowMap) {
2827 WindowToken wtoken = mTokenMap.get(token);
2828 if (wtoken != null) {
2829 Log.w(TAG, "Attempted to add existing input method token: " + token);
2830 return;
2831 }
2832 wtoken = new WindowToken(token, type, true);
2833 mTokenMap.put(token, wtoken);
2834 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002835 if (type == TYPE_WALLPAPER) {
2836 mWallpaperTokens.add(wtoken);
2837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 }
2839 }
Romain Guy06882f82009-06-10 13:36:04 -07002840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 public void removeWindowToken(IBinder token) {
2842 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2843 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002844 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 }
2846
2847 final long origId = Binder.clearCallingIdentity();
2848 synchronized(mWindowMap) {
2849 WindowToken wtoken = mTokenMap.remove(token);
2850 mTokenList.remove(wtoken);
2851 if (wtoken != null) {
2852 boolean delayed = false;
2853 if (!wtoken.hidden) {
2854 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 final int N = wtoken.windows.size();
2857 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 for (int i=0; i<N; i++) {
2860 WindowState win = wtoken.windows.get(i);
2861
2862 if (win.isAnimating()) {
2863 delayed = true;
2864 }
Romain Guy06882f82009-06-10 13:36:04 -07002865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 if (win.isVisibleNow()) {
2867 applyAnimationLocked(win,
2868 WindowManagerPolicy.TRANSIT_EXIT, false);
2869 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2870 KeyWaiter.RETURN_NOTHING);
2871 changed = true;
2872 }
2873 }
2874
2875 if (changed) {
2876 mLayoutNeeded = true;
2877 performLayoutAndPlaceSurfacesLocked();
2878 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2879 }
Romain Guy06882f82009-06-10 13:36:04 -07002880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 if (delayed) {
2882 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002883 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2884 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 }
2886 }
Romain Guy06882f82009-06-10 13:36:04 -07002887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 } else {
2889 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2890 }
2891 }
2892 Binder.restoreCallingIdentity(origId);
2893 }
2894
2895 public void addAppToken(int addPos, IApplicationToken token,
2896 int groupId, int requestedOrientation, boolean fullscreen) {
2897 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2898 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002899 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 }
Romain Guy06882f82009-06-10 13:36:04 -07002901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 synchronized(mWindowMap) {
2903 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2904 if (wtoken != null) {
2905 Log.w(TAG, "Attempted to add existing app token: " + token);
2906 return;
2907 }
2908 wtoken = new AppWindowToken(token);
2909 wtoken.groupId = groupId;
2910 wtoken.appFullscreen = fullscreen;
2911 wtoken.requestedOrientation = requestedOrientation;
2912 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002913 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 mTokenMap.put(token.asBinder(), wtoken);
2915 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 // Application tokens start out hidden.
2918 wtoken.hidden = true;
2919 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 //dump();
2922 }
2923 }
Romain Guy06882f82009-06-10 13:36:04 -07002924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 public void setAppGroupId(IBinder token, int groupId) {
2926 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2927 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002928 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 }
2930
2931 synchronized(mWindowMap) {
2932 AppWindowToken wtoken = findAppWindowToken(token);
2933 if (wtoken == null) {
2934 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2935 return;
2936 }
2937 wtoken.groupId = groupId;
2938 }
2939 }
Romain Guy06882f82009-06-10 13:36:04 -07002940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 public int getOrientationFromWindowsLocked() {
2942 int pos = mWindows.size() - 1;
2943 while (pos >= 0) {
2944 WindowState wtoken = (WindowState) mWindows.get(pos);
2945 pos--;
2946 if (wtoken.mAppToken != null) {
2947 // We hit an application window. so the orientation will be determined by the
2948 // app window. No point in continuing further.
2949 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2950 }
2951 if (!wtoken.isVisibleLw()) {
2952 continue;
2953 }
2954 int req = wtoken.mAttrs.screenOrientation;
2955 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2956 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2957 continue;
2958 } else {
2959 return req;
2960 }
2961 }
2962 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2963 }
Romain Guy06882f82009-06-10 13:36:04 -07002964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 public int getOrientationFromAppTokensLocked() {
2966 int pos = mAppTokens.size() - 1;
2967 int curGroup = 0;
2968 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002969 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002971 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 while (pos >= 0) {
2973 AppWindowToken wtoken = mAppTokens.get(pos);
2974 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002975 // if we're about to tear down this window and not seek for
2976 // the behind activity, don't use it for orientation
2977 if (!findingBehind
2978 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002979 continue;
2980 }
2981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 if (!haveGroup) {
2983 // We ignore any hidden applications on the top.
2984 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2985 continue;
2986 }
2987 haveGroup = true;
2988 curGroup = wtoken.groupId;
2989 lastOrientation = wtoken.requestedOrientation;
2990 } else if (curGroup != wtoken.groupId) {
2991 // If we have hit a new application group, and the bottom
2992 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002993 // the orientation behind it, and the last app was
2994 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002996 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2997 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 return lastOrientation;
2999 }
3000 }
3001 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07003002 // If this application is fullscreen, and didn't explicitly say
3003 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08003005 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07003006 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07003007 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 return or;
3009 }
3010 // If this application has requested an explicit orientation,
3011 // then use it.
3012 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3013 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3014 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3015 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3016 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3017 return or;
3018 }
Owen Lin3413b892009-05-01 17:12:32 -07003019 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 }
3021 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3022 }
Romain Guy06882f82009-06-10 13:36:04 -07003023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003025 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003026 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3027 "updateOrientationFromAppTokens()")) {
3028 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3029 }
3030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 Configuration config;
3032 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003033 config = updateOrientationFromAppTokensUnchecked(currentConfig,
3034 freezeThisOneIfNeeded);
3035 Binder.restoreCallingIdentity(ident);
3036 return config;
3037 }
3038
3039 Configuration updateOrientationFromAppTokensUnchecked(
3040 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
3041 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003043 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003044 if (config != null) {
3045 mLayoutNeeded = true;
3046 performLayoutAndPlaceSurfacesLocked();
3047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 return config;
3050 }
Romain Guy06882f82009-06-10 13:36:04 -07003051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 /*
3053 * The orientation is computed from non-application windows first. If none of
3054 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003055 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3057 * android.os.IBinder)
3058 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003059 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07003060 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 long ident = Binder.clearCallingIdentity();
3063 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003064 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 if (req != mForcedAppOrientation) {
3067 changed = true;
3068 mForcedAppOrientation = req;
3069 //send a message to Policy indicating orientation change to take
3070 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003071 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 }
Romain Guy06882f82009-06-10 13:36:04 -07003073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 if (changed) {
3075 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07003076 WindowManagerPolicy.USE_LAST_ROTATION,
3077 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 if (changed) {
3079 if (freezeThisOneIfNeeded != null) {
3080 AppWindowToken wtoken = findAppWindowToken(
3081 freezeThisOneIfNeeded);
3082 if (wtoken != null) {
3083 startAppFreezingScreenLocked(wtoken,
3084 ActivityInfo.CONFIG_ORIENTATION);
3085 }
3086 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003087 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 }
3089 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003090
3091 // No obvious action we need to take, but if our current
3092 // state mismatches the activity maanager's, update it
3093 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003094 mTempConfiguration.setToDefaults();
3095 if (computeNewConfigurationLocked(mTempConfiguration)) {
3096 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003097 return new Configuration(mTempConfiguration);
3098 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003099 }
3100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 } finally {
3102 Binder.restoreCallingIdentity(ident);
3103 }
Romain Guy06882f82009-06-10 13:36:04 -07003104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 return null;
3106 }
Romain Guy06882f82009-06-10 13:36:04 -07003107
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003108 int computeForcedAppOrientationLocked() {
3109 int req = getOrientationFromWindowsLocked();
3110 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3111 req = getOrientationFromAppTokensLocked();
3112 }
3113 return req;
3114 }
Romain Guy06882f82009-06-10 13:36:04 -07003115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3117 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3118 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003119 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 }
Romain Guy06882f82009-06-10 13:36:04 -07003121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 synchronized(mWindowMap) {
3123 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3124 if (wtoken == null) {
3125 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3126 return;
3127 }
Romain Guy06882f82009-06-10 13:36:04 -07003128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 wtoken.requestedOrientation = requestedOrientation;
3130 }
3131 }
Romain Guy06882f82009-06-10 13:36:04 -07003132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 public int getAppOrientation(IApplicationToken token) {
3134 synchronized(mWindowMap) {
3135 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3136 if (wtoken == null) {
3137 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3138 }
Romain Guy06882f82009-06-10 13:36:04 -07003139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 return wtoken.requestedOrientation;
3141 }
3142 }
Romain Guy06882f82009-06-10 13:36:04 -07003143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3145 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3146 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003147 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 }
3149
3150 synchronized(mWindowMap) {
3151 boolean changed = false;
3152 if (token == null) {
3153 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3154 changed = mFocusedApp != null;
3155 mFocusedApp = null;
3156 mKeyWaiter.tickle();
3157 } else {
3158 AppWindowToken newFocus = findAppWindowToken(token);
3159 if (newFocus == null) {
3160 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3161 return;
3162 }
3163 changed = mFocusedApp != newFocus;
3164 mFocusedApp = newFocus;
3165 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3166 mKeyWaiter.tickle();
3167 }
3168
3169 if (moveFocusNow && changed) {
3170 final long origId = Binder.clearCallingIdentity();
3171 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3172 Binder.restoreCallingIdentity(origId);
3173 }
3174 }
3175 }
3176
3177 public void prepareAppTransition(int transit) {
3178 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3179 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003180 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 }
Romain Guy06882f82009-06-10 13:36:04 -07003182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 synchronized(mWindowMap) {
3184 if (DEBUG_APP_TRANSITIONS) Log.v(
3185 TAG, "Prepare app transition: transit=" + transit
3186 + " mNextAppTransition=" + mNextAppTransition);
3187 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003188 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3189 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003191 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3192 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3193 // Opening a new task always supersedes a close for the anim.
3194 mNextAppTransition = transit;
3195 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3196 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3197 // Opening a new activity always supersedes a close for the anim.
3198 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 }
3200 mAppTransitionReady = false;
3201 mAppTransitionTimeout = false;
3202 mStartingIconInTransition = false;
3203 mSkipAppTransitionAnimation = false;
3204 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3205 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3206 5000);
3207 }
3208 }
3209 }
3210
3211 public int getPendingAppTransition() {
3212 return mNextAppTransition;
3213 }
Romain Guy06882f82009-06-10 13:36:04 -07003214
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003215 public void overridePendingAppTransition(String packageName,
3216 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003217 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003218 mNextAppTransitionPackage = packageName;
3219 mNextAppTransitionEnter = enterAnim;
3220 mNextAppTransitionExit = exitAnim;
3221 }
3222 }
3223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 public void executeAppTransition() {
3225 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3226 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003227 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 }
Romain Guy06882f82009-06-10 13:36:04 -07003229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003231 if (DEBUG_APP_TRANSITIONS) {
3232 RuntimeException e = new RuntimeException("here");
3233 e.fillInStackTrace();
3234 Log.w(TAG, "Execute app transition: mNextAppTransition="
3235 + mNextAppTransition, e);
3236 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003237 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 mAppTransitionReady = true;
3239 final long origId = Binder.clearCallingIdentity();
3240 performLayoutAndPlaceSurfacesLocked();
3241 Binder.restoreCallingIdentity(origId);
3242 }
3243 }
3244 }
3245
3246 public void setAppStartingWindow(IBinder token, String pkg,
3247 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3248 IBinder transferFrom, boolean createIfNeeded) {
3249 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3250 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003251 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 }
3253
3254 synchronized(mWindowMap) {
3255 if (DEBUG_STARTING_WINDOW) Log.v(
3256 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3257 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 AppWindowToken wtoken = findAppWindowToken(token);
3260 if (wtoken == null) {
3261 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3262 return;
3263 }
3264
3265 // If the display is frozen, we won't do anything until the
3266 // actual window is displayed so there is no reason to put in
3267 // the starting window.
3268 if (mDisplayFrozen) {
3269 return;
3270 }
Romain Guy06882f82009-06-10 13:36:04 -07003271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 if (wtoken.startingData != null) {
3273 return;
3274 }
Romain Guy06882f82009-06-10 13:36:04 -07003275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 if (transferFrom != null) {
3277 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3278 if (ttoken != null) {
3279 WindowState startingWindow = ttoken.startingWindow;
3280 if (startingWindow != null) {
3281 if (mStartingIconInTransition) {
3282 // In this case, the starting icon has already
3283 // been displayed, so start letting windows get
3284 // shown immediately without any more transitions.
3285 mSkipAppTransitionAnimation = true;
3286 }
3287 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3288 "Moving existing starting from " + ttoken
3289 + " to " + wtoken);
3290 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 // Transfer the starting window over to the new
3293 // token.
3294 wtoken.startingData = ttoken.startingData;
3295 wtoken.startingView = ttoken.startingView;
3296 wtoken.startingWindow = startingWindow;
3297 ttoken.startingData = null;
3298 ttoken.startingView = null;
3299 ttoken.startingWindow = null;
3300 ttoken.startingMoved = true;
3301 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003302 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003304 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3305 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 mWindows.remove(startingWindow);
3307 ttoken.windows.remove(startingWindow);
3308 ttoken.allAppWindows.remove(startingWindow);
3309 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 // Propagate other interesting state between the
3312 // tokens. If the old token is displayed, we should
3313 // immediately force the new one to be displayed. If
3314 // it is animating, we need to move that animation to
3315 // the new one.
3316 if (ttoken.allDrawn) {
3317 wtoken.allDrawn = true;
3318 }
3319 if (ttoken.firstWindowDrawn) {
3320 wtoken.firstWindowDrawn = true;
3321 }
3322 if (!ttoken.hidden) {
3323 wtoken.hidden = false;
3324 wtoken.hiddenRequested = false;
3325 wtoken.willBeHidden = false;
3326 }
3327 if (wtoken.clientHidden != ttoken.clientHidden) {
3328 wtoken.clientHidden = ttoken.clientHidden;
3329 wtoken.sendAppVisibilityToClients();
3330 }
3331 if (ttoken.animation != null) {
3332 wtoken.animation = ttoken.animation;
3333 wtoken.animating = ttoken.animating;
3334 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3335 ttoken.animation = null;
3336 ttoken.animLayerAdjustment = 0;
3337 wtoken.updateLayers();
3338 ttoken.updateLayers();
3339 }
Romain Guy06882f82009-06-10 13:36:04 -07003340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 mLayoutNeeded = true;
3343 performLayoutAndPlaceSurfacesLocked();
3344 Binder.restoreCallingIdentity(origId);
3345 return;
3346 } else if (ttoken.startingData != null) {
3347 // The previous app was getting ready to show a
3348 // starting window, but hasn't yet done so. Steal it!
3349 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3350 "Moving pending starting from " + ttoken
3351 + " to " + wtoken);
3352 wtoken.startingData = ttoken.startingData;
3353 ttoken.startingData = null;
3354 ttoken.startingMoved = true;
3355 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3356 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3357 // want to process the message ASAP, before any other queued
3358 // messages.
3359 mH.sendMessageAtFrontOfQueue(m);
3360 return;
3361 }
3362 }
3363 }
3364
3365 // There is no existing starting window, and the caller doesn't
3366 // want us to create one, so that's it!
3367 if (!createIfNeeded) {
3368 return;
3369 }
Romain Guy06882f82009-06-10 13:36:04 -07003370
Dianne Hackborn284ac932009-08-28 10:34:25 -07003371 // If this is a translucent or wallpaper window, then don't
3372 // show a starting window -- the current effect (a full-screen
3373 // opaque starting window that fades away to the real contents
3374 // when it is ready) does not work for this.
3375 if (theme != 0) {
3376 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3377 com.android.internal.R.styleable.Window);
3378 if (ent.array.getBoolean(
3379 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3380 return;
3381 }
3382 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003383 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3384 return;
3385 }
3386 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003387 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3388 return;
3389 }
3390 }
3391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 mStartingIconInTransition = true;
3393 wtoken.startingData = new StartingData(
3394 pkg, theme, nonLocalizedLabel,
3395 labelRes, icon);
3396 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3397 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3398 // want to process the message ASAP, before any other queued
3399 // messages.
3400 mH.sendMessageAtFrontOfQueue(m);
3401 }
3402 }
3403
3404 public void setAppWillBeHidden(IBinder token) {
3405 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3406 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003407 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 }
3409
3410 AppWindowToken wtoken;
3411
3412 synchronized(mWindowMap) {
3413 wtoken = findAppWindowToken(token);
3414 if (wtoken == null) {
3415 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3416 return;
3417 }
3418 wtoken.willBeHidden = true;
3419 }
3420 }
Romain Guy06882f82009-06-10 13:36:04 -07003421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3423 boolean visible, int transit, boolean performLayout) {
3424 boolean delayed = false;
3425
3426 if (wtoken.clientHidden == visible) {
3427 wtoken.clientHidden = !visible;
3428 wtoken.sendAppVisibilityToClients();
3429 }
Romain Guy06882f82009-06-10 13:36:04 -07003430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 wtoken.willBeHidden = false;
3432 if (wtoken.hidden == visible) {
3433 final int N = wtoken.allAppWindows.size();
3434 boolean changed = false;
3435 if (DEBUG_APP_TRANSITIONS) Log.v(
3436 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3437 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003440
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003441 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 if (wtoken.animation == sDummyAnimation) {
3443 wtoken.animation = null;
3444 }
3445 applyAnimationLocked(wtoken, lp, transit, visible);
3446 changed = true;
3447 if (wtoken.animation != null) {
3448 delayed = runningAppAnimation = true;
3449 }
3450 }
Romain Guy06882f82009-06-10 13:36:04 -07003451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 for (int i=0; i<N; i++) {
3453 WindowState win = wtoken.allAppWindows.get(i);
3454 if (win == wtoken.startingWindow) {
3455 continue;
3456 }
3457
3458 if (win.isAnimating()) {
3459 delayed = true;
3460 }
Romain Guy06882f82009-06-10 13:36:04 -07003461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3463 //win.dump(" ");
3464 if (visible) {
3465 if (!win.isVisibleNow()) {
3466 if (!runningAppAnimation) {
3467 applyAnimationLocked(win,
3468 WindowManagerPolicy.TRANSIT_ENTER, true);
3469 }
3470 changed = true;
3471 }
3472 } else if (win.isVisibleNow()) {
3473 if (!runningAppAnimation) {
3474 applyAnimationLocked(win,
3475 WindowManagerPolicy.TRANSIT_EXIT, false);
3476 }
3477 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3478 KeyWaiter.RETURN_NOTHING);
3479 changed = true;
3480 }
3481 }
3482
3483 wtoken.hidden = wtoken.hiddenRequested = !visible;
3484 if (!visible) {
3485 unsetAppFreezingScreenLocked(wtoken, true, true);
3486 } else {
3487 // If we are being set visible, and the starting window is
3488 // not yet displayed, then make sure it doesn't get displayed.
3489 WindowState swin = wtoken.startingWindow;
3490 if (swin != null && (swin.mDrawPending
3491 || swin.mCommitDrawPending)) {
3492 swin.mPolicyVisibility = false;
3493 swin.mPolicyVisibilityAfterAnim = false;
3494 }
3495 }
Romain Guy06882f82009-06-10 13:36:04 -07003496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3498 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3499 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003500
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003501 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003503 if (performLayout) {
3504 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3505 performLayoutAndPlaceSurfacesLocked();
3506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 }
3508 }
3509
3510 if (wtoken.animation != null) {
3511 delayed = true;
3512 }
Romain Guy06882f82009-06-10 13:36:04 -07003513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 return delayed;
3515 }
3516
3517 public void setAppVisibility(IBinder token, boolean visible) {
3518 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3519 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003520 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 }
3522
3523 AppWindowToken wtoken;
3524
3525 synchronized(mWindowMap) {
3526 wtoken = findAppWindowToken(token);
3527 if (wtoken == null) {
3528 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3529 return;
3530 }
3531
3532 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3533 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003534 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3536 + "): mNextAppTransition=" + mNextAppTransition
3537 + " hidden=" + wtoken.hidden
3538 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3539 }
Romain Guy06882f82009-06-10 13:36:04 -07003540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 // If we are preparing an app transition, then delay changing
3542 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003543 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 // Already in requested state, don't do anything more.
3545 if (wtoken.hiddenRequested != visible) {
3546 return;
3547 }
3548 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 if (DEBUG_APP_TRANSITIONS) Log.v(
3551 TAG, "Setting dummy animation on: " + wtoken);
3552 wtoken.setDummyAnimation();
3553 mOpeningApps.remove(wtoken);
3554 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003555 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 wtoken.inPendingTransaction = true;
3557 if (visible) {
3558 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 wtoken.startingDisplayed = false;
3560 wtoken.startingMoved = false;
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003561
3562 // If the token is currently hidden (should be the
3563 // common case), then we need to set up to wait for
3564 // its windows to be ready.
3565 if (wtoken.hidden) {
3566 wtoken.allDrawn = false;
3567 wtoken.waitingToShow = true;
3568
3569 if (wtoken.clientHidden) {
3570 // In the case where we are making an app visible
3571 // but holding off for a transition, we still need
3572 // to tell the client to make its windows visible so
3573 // they get drawn. Otherwise, we will wait on
3574 // performing the transition until all windows have
3575 // been drawn, they never will be, and we are sad.
3576 wtoken.clientHidden = false;
3577 wtoken.sendAppVisibilityToClients();
3578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 }
3580 } else {
3581 mClosingApps.add(wtoken);
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003582
3583 // If the token is currently visible (should be the
3584 // common case), then set up to wait for it to be hidden.
3585 if (!wtoken.hidden) {
3586 wtoken.waitingToHide = true;
3587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 }
3589 return;
3590 }
Romain Guy06882f82009-06-10 13:36:04 -07003591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003593 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 wtoken.updateReportedVisibilityLocked();
3595 Binder.restoreCallingIdentity(origId);
3596 }
3597 }
3598
3599 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3600 boolean unfreezeSurfaceNow, boolean force) {
3601 if (wtoken.freezingScreen) {
3602 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3603 + " force=" + force);
3604 final int N = wtoken.allAppWindows.size();
3605 boolean unfrozeWindows = false;
3606 for (int i=0; i<N; i++) {
3607 WindowState w = wtoken.allAppWindows.get(i);
3608 if (w.mAppFreezing) {
3609 w.mAppFreezing = false;
3610 if (w.mSurface != null && !w.mOrientationChanging) {
3611 w.mOrientationChanging = true;
3612 }
3613 unfrozeWindows = true;
3614 }
3615 }
3616 if (force || unfrozeWindows) {
3617 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3618 wtoken.freezingScreen = false;
3619 mAppsFreezingScreen--;
3620 }
3621 if (unfreezeSurfaceNow) {
3622 if (unfrozeWindows) {
3623 mLayoutNeeded = true;
3624 performLayoutAndPlaceSurfacesLocked();
3625 }
3626 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3627 stopFreezingDisplayLocked();
3628 }
3629 }
3630 }
3631 }
Romain Guy06882f82009-06-10 13:36:04 -07003632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3634 int configChanges) {
3635 if (DEBUG_ORIENTATION) {
3636 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003637 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 Log.i(TAG, "Set freezing of " + wtoken.appToken
3639 + ": hidden=" + wtoken.hidden + " freezing="
3640 + wtoken.freezingScreen, e);
3641 }
3642 if (!wtoken.hiddenRequested) {
3643 if (!wtoken.freezingScreen) {
3644 wtoken.freezingScreen = true;
3645 mAppsFreezingScreen++;
3646 if (mAppsFreezingScreen == 1) {
3647 startFreezingDisplayLocked();
3648 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3649 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3650 5000);
3651 }
3652 }
3653 final int N = wtoken.allAppWindows.size();
3654 for (int i=0; i<N; i++) {
3655 WindowState w = wtoken.allAppWindows.get(i);
3656 w.mAppFreezing = true;
3657 }
3658 }
3659 }
Romain Guy06882f82009-06-10 13:36:04 -07003660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 public void startAppFreezingScreen(IBinder token, int configChanges) {
3662 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3663 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003664 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 }
3666
3667 synchronized(mWindowMap) {
3668 if (configChanges == 0 && !mDisplayFrozen) {
3669 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3670 return;
3671 }
Romain Guy06882f82009-06-10 13:36:04 -07003672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 AppWindowToken wtoken = findAppWindowToken(token);
3674 if (wtoken == null || wtoken.appToken == null) {
3675 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3676 return;
3677 }
3678 final long origId = Binder.clearCallingIdentity();
3679 startAppFreezingScreenLocked(wtoken, configChanges);
3680 Binder.restoreCallingIdentity(origId);
3681 }
3682 }
Romain Guy06882f82009-06-10 13:36:04 -07003683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 public void stopAppFreezingScreen(IBinder token, boolean force) {
3685 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3686 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003687 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 }
3689
3690 synchronized(mWindowMap) {
3691 AppWindowToken wtoken = findAppWindowToken(token);
3692 if (wtoken == null || wtoken.appToken == null) {
3693 return;
3694 }
3695 final long origId = Binder.clearCallingIdentity();
3696 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3697 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3698 unsetAppFreezingScreenLocked(wtoken, true, force);
3699 Binder.restoreCallingIdentity(origId);
3700 }
3701 }
Romain Guy06882f82009-06-10 13:36:04 -07003702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 public void removeAppToken(IBinder token) {
3704 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3705 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003706 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 }
3708
3709 AppWindowToken wtoken = null;
3710 AppWindowToken startingToken = null;
3711 boolean delayed = false;
3712
3713 final long origId = Binder.clearCallingIdentity();
3714 synchronized(mWindowMap) {
3715 WindowToken basewtoken = mTokenMap.remove(token);
3716 mTokenList.remove(basewtoken);
3717 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3718 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003719 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 wtoken.inPendingTransaction = false;
3721 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003722 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 if (mClosingApps.contains(wtoken)) {
3724 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003725 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003727 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 delayed = true;
3729 }
3730 if (DEBUG_APP_TRANSITIONS) Log.v(
3731 TAG, "Removing app " + wtoken + " delayed=" + delayed
3732 + " animation=" + wtoken.animation
3733 + " animating=" + wtoken.animating);
3734 if (delayed) {
3735 // set the token aside because it has an active animation to be finished
3736 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003737 } else {
3738 // Make sure there is no animation running on this token,
3739 // so any windows associated with it will be removed as
3740 // soon as their animations are complete
3741 wtoken.animation = null;
3742 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 }
3744 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003745 if (mLastEnterAnimToken == wtoken) {
3746 mLastEnterAnimToken = null;
3747 mLastEnterAnimParams = null;
3748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 wtoken.removed = true;
3750 if (wtoken.startingData != null) {
3751 startingToken = wtoken;
3752 }
3753 unsetAppFreezingScreenLocked(wtoken, true, true);
3754 if (mFocusedApp == wtoken) {
3755 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3756 mFocusedApp = null;
3757 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3758 mKeyWaiter.tickle();
3759 }
3760 } else {
3761 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3762 }
Romain Guy06882f82009-06-10 13:36:04 -07003763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 if (!delayed && wtoken != null) {
3765 wtoken.updateReportedVisibilityLocked();
3766 }
3767 }
3768 Binder.restoreCallingIdentity(origId);
3769
3770 if (startingToken != null) {
3771 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3772 + startingToken + ": app token removed");
3773 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3774 mH.sendMessage(m);
3775 }
3776 }
3777
3778 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3779 final int NW = token.windows.size();
3780 for (int i=0; i<NW; i++) {
3781 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003782 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 mWindows.remove(win);
3784 int j = win.mChildWindows.size();
3785 while (j > 0) {
3786 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003787 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3788 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3789 "Tmp removing child window " + cwin);
3790 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 }
3792 }
3793 return NW > 0;
3794 }
3795
3796 void dumpAppTokensLocked() {
3797 for (int i=mAppTokens.size()-1; i>=0; i--) {
3798 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3799 }
3800 }
Romain Guy06882f82009-06-10 13:36:04 -07003801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 void dumpWindowsLocked() {
3803 for (int i=mWindows.size()-1; i>=0; i--) {
3804 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3805 }
3806 }
Romain Guy06882f82009-06-10 13:36:04 -07003807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 private int findWindowOffsetLocked(int tokenPos) {
3809 final int NW = mWindows.size();
3810
3811 if (tokenPos >= mAppTokens.size()) {
3812 int i = NW;
3813 while (i > 0) {
3814 i--;
3815 WindowState win = (WindowState)mWindows.get(i);
3816 if (win.getAppToken() != null) {
3817 return i+1;
3818 }
3819 }
3820 }
3821
3822 while (tokenPos > 0) {
3823 // Find the first app token below the new position that has
3824 // a window displayed.
3825 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3826 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3827 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003828 if (wtoken.sendingToBottom) {
3829 if (DEBUG_REORDER) Log.v(TAG,
3830 "Skipping token -- currently sending to bottom");
3831 tokenPos--;
3832 continue;
3833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 int i = wtoken.windows.size();
3835 while (i > 0) {
3836 i--;
3837 WindowState win = wtoken.windows.get(i);
3838 int j = win.mChildWindows.size();
3839 while (j > 0) {
3840 j--;
3841 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003842 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003843 for (int pos=NW-1; pos>=0; pos--) {
3844 if (mWindows.get(pos) == cwin) {
3845 if (DEBUG_REORDER) Log.v(TAG,
3846 "Found child win @" + (pos+1));
3847 return pos+1;
3848 }
3849 }
3850 }
3851 }
3852 for (int pos=NW-1; pos>=0; pos--) {
3853 if (mWindows.get(pos) == win) {
3854 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3855 return pos+1;
3856 }
3857 }
3858 }
3859 tokenPos--;
3860 }
3861
3862 return 0;
3863 }
3864
3865 private final int reAddWindowLocked(int index, WindowState win) {
3866 final int NCW = win.mChildWindows.size();
3867 boolean added = false;
3868 for (int j=0; j<NCW; j++) {
3869 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3870 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003871 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3872 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 mWindows.add(index, win);
3874 index++;
3875 added = true;
3876 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003877 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3878 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003879 mWindows.add(index, cwin);
3880 index++;
3881 }
3882 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003883 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3884 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 mWindows.add(index, win);
3886 index++;
3887 }
3888 return index;
3889 }
Romain Guy06882f82009-06-10 13:36:04 -07003890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3892 final int NW = token.windows.size();
3893 for (int i=0; i<NW; i++) {
3894 index = reAddWindowLocked(index, token.windows.get(i));
3895 }
3896 return index;
3897 }
3898
3899 public void moveAppToken(int index, IBinder token) {
3900 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3901 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003902 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 }
3904
3905 synchronized(mWindowMap) {
3906 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3907 if (DEBUG_REORDER) dumpAppTokensLocked();
3908 final AppWindowToken wtoken = findAppWindowToken(token);
3909 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3910 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3911 + token + " (" + wtoken + ")");
3912 return;
3913 }
3914 mAppTokens.add(index, wtoken);
3915 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3916 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003918 final long origId = Binder.clearCallingIdentity();
3919 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3920 if (DEBUG_REORDER) dumpWindowsLocked();
3921 if (tmpRemoveAppWindowsLocked(wtoken)) {
3922 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3923 if (DEBUG_REORDER) dumpWindowsLocked();
3924 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3925 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3926 if (DEBUG_REORDER) dumpWindowsLocked();
3927 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 mLayoutNeeded = true;
3929 performLayoutAndPlaceSurfacesLocked();
3930 }
3931 Binder.restoreCallingIdentity(origId);
3932 }
3933 }
3934
3935 private void removeAppTokensLocked(List<IBinder> tokens) {
3936 // XXX This should be done more efficiently!
3937 // (take advantage of the fact that both lists should be
3938 // ordered in the same way.)
3939 int N = tokens.size();
3940 for (int i=0; i<N; i++) {
3941 IBinder token = tokens.get(i);
3942 final AppWindowToken wtoken = findAppWindowToken(token);
3943 if (!mAppTokens.remove(wtoken)) {
3944 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3945 + token + " (" + wtoken + ")");
3946 i--;
3947 N--;
3948 }
3949 }
3950 }
3951
Dianne Hackborna8f60182009-09-01 19:01:50 -07003952 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3953 boolean updateFocusAndLayout) {
3954 // First remove all of the windows from the list.
3955 tmpRemoveAppWindowsLocked(wtoken);
3956
3957 // Where to start adding?
3958 int pos = findWindowOffsetLocked(tokenPos);
3959
3960 // And now add them back at the correct place.
3961 pos = reAddAppWindowsLocked(pos, wtoken);
3962
3963 if (updateFocusAndLayout) {
3964 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3965 assignLayersLocked();
3966 }
3967 mLayoutNeeded = true;
3968 performLayoutAndPlaceSurfacesLocked();
3969 }
3970 }
3971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3973 // First remove all of the windows from the list.
3974 final int N = tokens.size();
3975 int i;
3976 for (i=0; i<N; i++) {
3977 WindowToken token = mTokenMap.get(tokens.get(i));
3978 if (token != null) {
3979 tmpRemoveAppWindowsLocked(token);
3980 }
3981 }
3982
3983 // Where to start adding?
3984 int pos = findWindowOffsetLocked(tokenPos);
3985
3986 // And now add them back at the correct place.
3987 for (i=0; i<N; i++) {
3988 WindowToken token = mTokenMap.get(tokens.get(i));
3989 if (token != null) {
3990 pos = reAddAppWindowsLocked(pos, token);
3991 }
3992 }
3993
Dianne Hackborna8f60182009-09-01 19:01:50 -07003994 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3995 assignLayersLocked();
3996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 mLayoutNeeded = true;
3998 performLayoutAndPlaceSurfacesLocked();
3999
4000 //dump();
4001 }
4002
4003 public void moveAppTokensToTop(List<IBinder> tokens) {
4004 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4005 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004006 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 }
4008
4009 final long origId = Binder.clearCallingIdentity();
4010 synchronized(mWindowMap) {
4011 removeAppTokensLocked(tokens);
4012 final int N = tokens.size();
4013 for (int i=0; i<N; i++) {
4014 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4015 if (wt != null) {
4016 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004017 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004018 mToTopApps.remove(wt);
4019 mToBottomApps.remove(wt);
4020 mToTopApps.add(wt);
4021 wt.sendingToBottom = false;
4022 wt.sendingToTop = true;
4023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 }
4025 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07004026
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004027 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004028 moveAppWindowsLocked(tokens, mAppTokens.size());
4029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 }
4031 Binder.restoreCallingIdentity(origId);
4032 }
4033
4034 public void moveAppTokensToBottom(List<IBinder> tokens) {
4035 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4036 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004037 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 }
4039
4040 final long origId = Binder.clearCallingIdentity();
4041 synchronized(mWindowMap) {
4042 removeAppTokensLocked(tokens);
4043 final int N = tokens.size();
4044 int pos = 0;
4045 for (int i=0; i<N; i++) {
4046 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4047 if (wt != null) {
4048 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004049 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004050 mToTopApps.remove(wt);
4051 mToBottomApps.remove(wt);
4052 mToBottomApps.add(i, wt);
4053 wt.sendingToTop = false;
4054 wt.sendingToBottom = true;
4055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 pos++;
4057 }
4058 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07004059
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004060 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004061 moveAppWindowsLocked(tokens, 0);
4062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 }
4064 Binder.restoreCallingIdentity(origId);
4065 }
4066
4067 // -------------------------------------------------------------
4068 // Misc IWindowSession methods
4069 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004072 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073 != PackageManager.PERMISSION_GRANTED) {
4074 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4075 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004076 synchronized (mKeyguardTokenWatcher) {
4077 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 }
4080
4081 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004082 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 != PackageManager.PERMISSION_GRANTED) {
4084 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4085 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004086 synchronized (mKeyguardTokenWatcher) {
4087 mKeyguardTokenWatcher.release(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088
Mike Lockwood983ee092009-11-22 01:42:24 -05004089 if (!mKeyguardTokenWatcher.isAcquired()) {
4090 // If we are the last one to reenable the keyguard wait until
4091 // we have actaully finished reenabling until returning.
4092 // It is possible that reenableKeyguard() can be called before
4093 // the previous disableKeyguard() is handled, in which case
4094 // neither mKeyguardTokenWatcher.acquired() or released() would
4095 // be called. In that case mKeyguardDisabled will be false here
4096 // and we have nothing to wait for.
4097 while (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 try {
Mike Lockwood983ee092009-11-22 01:42:24 -05004099 mKeyguardTokenWatcher.wait();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 } catch (InterruptedException e) {
4101 Thread.currentThread().interrupt();
4102 }
4103 }
4104 }
4105 }
4106 }
4107
4108 /**
4109 * @see android.app.KeyguardManager#exitKeyguardSecurely
4110 */
4111 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004112 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 != PackageManager.PERMISSION_GRANTED) {
4114 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4115 }
4116 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4117 public void onKeyguardExitResult(boolean success) {
4118 try {
4119 callback.onKeyguardExitResult(success);
4120 } catch (RemoteException e) {
4121 // Client has died, we don't care.
4122 }
4123 }
4124 });
4125 }
4126
4127 public boolean inKeyguardRestrictedInputMode() {
4128 return mPolicy.inKeyguardRestrictedKeyInputMode();
4129 }
Romain Guy06882f82009-06-10 13:36:04 -07004130
Dianne Hackbornffa42482009-09-23 22:20:11 -07004131 public void closeSystemDialogs(String reason) {
4132 synchronized(mWindowMap) {
4133 for (int i=mWindows.size()-1; i>=0; i--) {
4134 WindowState w = (WindowState)mWindows.get(i);
4135 if (w.mSurface != null) {
4136 try {
4137 w.mClient.closeSystemDialogs(reason);
4138 } catch (RemoteException e) {
4139 }
4140 }
4141 }
4142 }
4143 }
4144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 static float fixScale(float scale) {
4146 if (scale < 0) scale = 0;
4147 else if (scale > 20) scale = 20;
4148 return Math.abs(scale);
4149 }
Romain Guy06882f82009-06-10 13:36:04 -07004150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 public void setAnimationScale(int which, float scale) {
4152 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4153 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004154 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 }
4156
4157 if (scale < 0) scale = 0;
4158 else if (scale > 20) scale = 20;
4159 scale = Math.abs(scale);
4160 switch (which) {
4161 case 0: mWindowAnimationScale = fixScale(scale); break;
4162 case 1: mTransitionAnimationScale = fixScale(scale); break;
4163 }
Romain Guy06882f82009-06-10 13:36:04 -07004164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 // Persist setting
4166 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4167 }
Romain Guy06882f82009-06-10 13:36:04 -07004168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 public void setAnimationScales(float[] scales) {
4170 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4171 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004172 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 }
4174
4175 if (scales != null) {
4176 if (scales.length >= 1) {
4177 mWindowAnimationScale = fixScale(scales[0]);
4178 }
4179 if (scales.length >= 2) {
4180 mTransitionAnimationScale = fixScale(scales[1]);
4181 }
4182 }
Romain Guy06882f82009-06-10 13:36:04 -07004183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184 // Persist setting
4185 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4186 }
Romain Guy06882f82009-06-10 13:36:04 -07004187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 public float getAnimationScale(int which) {
4189 switch (which) {
4190 case 0: return mWindowAnimationScale;
4191 case 1: return mTransitionAnimationScale;
4192 }
4193 return 0;
4194 }
Romain Guy06882f82009-06-10 13:36:04 -07004195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 public float[] getAnimationScales() {
4197 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4198 }
Romain Guy06882f82009-06-10 13:36:04 -07004199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 public int getSwitchState(int sw) {
4201 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4202 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004203 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004204 }
4205 return KeyInputQueue.getSwitchState(sw);
4206 }
Romain Guy06882f82009-06-10 13:36:04 -07004207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 public int getSwitchStateForDevice(int devid, int sw) {
4209 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4210 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004211 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 }
4213 return KeyInputQueue.getSwitchState(devid, sw);
4214 }
Romain Guy06882f82009-06-10 13:36:04 -07004215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 public int getScancodeState(int sw) {
4217 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4218 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004219 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004221 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 }
Romain Guy06882f82009-06-10 13:36:04 -07004223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 public int getScancodeStateForDevice(int devid, int sw) {
4225 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4226 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004227 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004229 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 }
Romain Guy06882f82009-06-10 13:36:04 -07004231
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004232 public int getTrackballScancodeState(int sw) {
4233 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4234 "getTrackballScancodeState()")) {
4235 throw new SecurityException("Requires READ_INPUT_STATE permission");
4236 }
4237 return mQueue.getTrackballScancodeState(sw);
4238 }
4239
4240 public int getDPadScancodeState(int sw) {
4241 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4242 "getDPadScancodeState()")) {
4243 throw new SecurityException("Requires READ_INPUT_STATE permission");
4244 }
4245 return mQueue.getDPadScancodeState(sw);
4246 }
4247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 public int getKeycodeState(int sw) {
4249 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4250 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004251 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004253 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 }
Romain Guy06882f82009-06-10 13:36:04 -07004255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 public int getKeycodeStateForDevice(int devid, int sw) {
4257 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4258 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004259 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004261 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 }
Romain Guy06882f82009-06-10 13:36:04 -07004263
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004264 public int getTrackballKeycodeState(int sw) {
4265 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4266 "getTrackballKeycodeState()")) {
4267 throw new SecurityException("Requires READ_INPUT_STATE permission");
4268 }
4269 return mQueue.getTrackballKeycodeState(sw);
4270 }
4271
4272 public int getDPadKeycodeState(int sw) {
4273 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4274 "getDPadKeycodeState()")) {
4275 throw new SecurityException("Requires READ_INPUT_STATE permission");
4276 }
4277 return mQueue.getDPadKeycodeState(sw);
4278 }
4279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4281 return KeyInputQueue.hasKeys(keycodes, keyExists);
4282 }
Romain Guy06882f82009-06-10 13:36:04 -07004283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004284 public void enableScreenAfterBoot() {
4285 synchronized(mWindowMap) {
4286 if (mSystemBooted) {
4287 return;
4288 }
4289 mSystemBooted = true;
4290 }
Romain Guy06882f82009-06-10 13:36:04 -07004291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 performEnableScreen();
4293 }
Romain Guy06882f82009-06-10 13:36:04 -07004294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 public void enableScreenIfNeededLocked() {
4296 if (mDisplayEnabled) {
4297 return;
4298 }
4299 if (!mSystemBooted) {
4300 return;
4301 }
4302 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4303 }
Romain Guy06882f82009-06-10 13:36:04 -07004304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 public void performEnableScreen() {
4306 synchronized(mWindowMap) {
4307 if (mDisplayEnabled) {
4308 return;
4309 }
4310 if (!mSystemBooted) {
4311 return;
4312 }
Romain Guy06882f82009-06-10 13:36:04 -07004313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 // Don't enable the screen until all existing windows
4315 // have been drawn.
4316 final int N = mWindows.size();
4317 for (int i=0; i<N; i++) {
4318 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08004319 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 return;
4321 }
4322 }
Romain Guy06882f82009-06-10 13:36:04 -07004323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 mDisplayEnabled = true;
4325 if (false) {
4326 Log.i(TAG, "ENABLING SCREEN!");
4327 StringWriter sw = new StringWriter();
4328 PrintWriter pw = new PrintWriter(sw);
4329 this.dump(null, pw, null);
4330 Log.i(TAG, sw.toString());
4331 }
4332 try {
4333 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4334 if (surfaceFlinger != null) {
4335 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4336 Parcel data = Parcel.obtain();
4337 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4338 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4339 data, null, 0);
4340 data.recycle();
4341 }
4342 } catch (RemoteException ex) {
4343 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4344 }
4345 }
Romain Guy06882f82009-06-10 13:36:04 -07004346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004350 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4351 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352 }
Romain Guy06882f82009-06-10 13:36:04 -07004353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004354 public void setInTouchMode(boolean mode) {
4355 synchronized(mWindowMap) {
4356 mInTouchMode = mode;
4357 }
4358 }
4359
Romain Guy06882f82009-06-10 13:36:04 -07004360 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004361 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004362 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004363 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004364 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 }
4366
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004367 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 }
Romain Guy06882f82009-06-10 13:36:04 -07004369
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004370 public void setRotationUnchecked(int rotation,
4371 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 if(DEBUG_ORIENTATION) Log.v(TAG,
4373 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 long origId = Binder.clearCallingIdentity();
4376 boolean changed;
4377 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004378 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 }
Romain Guy06882f82009-06-10 13:36:04 -07004380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 if (changed) {
4382 sendNewConfiguration();
4383 synchronized(mWindowMap) {
4384 mLayoutNeeded = true;
4385 performLayoutAndPlaceSurfacesLocked();
4386 }
4387 } else if (alwaysSendConfiguration) {
4388 //update configuration ignoring orientation change
4389 sendNewConfiguration();
4390 }
Romain Guy06882f82009-06-10 13:36:04 -07004391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004392 Binder.restoreCallingIdentity(origId);
4393 }
Romain Guy06882f82009-06-10 13:36:04 -07004394
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004395 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 boolean changed;
4397 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4398 rotation = mRequestedRotation;
4399 } else {
4400 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004401 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 }
4403 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004404 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 mRotation, mDisplayEnabled);
4406 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4407 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004410 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 "Rotation changed to " + rotation
4412 + " from " + mRotation
4413 + " (forceApp=" + mForcedAppOrientation
4414 + ", req=" + mRequestedRotation + ")");
4415 mRotation = rotation;
4416 mWindowsFreezingScreen = true;
4417 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4418 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4419 2000);
4420 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004421 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 mQueue.setOrientation(rotation);
4423 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004424 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 }
4426 for (int i=mWindows.size()-1; i>=0; i--) {
4427 WindowState w = (WindowState)mWindows.get(i);
4428 if (w.mSurface != null) {
4429 w.mOrientationChanging = true;
4430 }
4431 }
4432 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4433 try {
4434 mRotationWatchers.get(i).onRotationChanged(rotation);
4435 } catch (RemoteException e) {
4436 }
4437 }
4438 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 return changed;
4441 }
Romain Guy06882f82009-06-10 13:36:04 -07004442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 public int getRotation() {
4444 return mRotation;
4445 }
4446
4447 public int watchRotation(IRotationWatcher watcher) {
4448 final IBinder watcherBinder = watcher.asBinder();
4449 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4450 public void binderDied() {
4451 synchronized (mWindowMap) {
4452 for (int i=0; i<mRotationWatchers.size(); i++) {
4453 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004454 IRotationWatcher removed = mRotationWatchers.remove(i);
4455 if (removed != null) {
4456 removed.asBinder().unlinkToDeath(this, 0);
4457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 i--;
4459 }
4460 }
4461 }
4462 }
4463 };
Romain Guy06882f82009-06-10 13:36:04 -07004464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 synchronized (mWindowMap) {
4466 try {
4467 watcher.asBinder().linkToDeath(dr, 0);
4468 mRotationWatchers.add(watcher);
4469 } catch (RemoteException e) {
4470 // Client died, no cleanup needed.
4471 }
Romain Guy06882f82009-06-10 13:36:04 -07004472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 return mRotation;
4474 }
4475 }
4476
4477 /**
4478 * Starts the view server on the specified port.
4479 *
4480 * @param port The port to listener to.
4481 *
4482 * @return True if the server was successfully started, false otherwise.
4483 *
4484 * @see com.android.server.ViewServer
4485 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4486 */
4487 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004488 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 return false;
4490 }
4491
4492 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4493 return false;
4494 }
4495
4496 if (port < 1024) {
4497 return false;
4498 }
4499
4500 if (mViewServer != null) {
4501 if (!mViewServer.isRunning()) {
4502 try {
4503 return mViewServer.start();
4504 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004505 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 }
4507 }
4508 return false;
4509 }
4510
4511 try {
4512 mViewServer = new ViewServer(this, port);
4513 return mViewServer.start();
4514 } catch (IOException e) {
4515 Log.w(TAG, "View server did not start");
4516 }
4517 return false;
4518 }
4519
Romain Guy06882f82009-06-10 13:36:04 -07004520 private boolean isSystemSecure() {
4521 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4522 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4523 }
4524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525 /**
4526 * Stops the view server if it exists.
4527 *
4528 * @return True if the server stopped, false if it wasn't started or
4529 * couldn't be stopped.
4530 *
4531 * @see com.android.server.ViewServer
4532 */
4533 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004534 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 return false;
4536 }
4537
4538 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4539 return false;
4540 }
4541
4542 if (mViewServer != null) {
4543 return mViewServer.stop();
4544 }
4545 return false;
4546 }
4547
4548 /**
4549 * Indicates whether the view server is running.
4550 *
4551 * @return True if the server is running, false otherwise.
4552 *
4553 * @see com.android.server.ViewServer
4554 */
4555 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004556 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004557 return false;
4558 }
4559
4560 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4561 return false;
4562 }
4563
4564 return mViewServer != null && mViewServer.isRunning();
4565 }
4566
4567 /**
4568 * Lists all availble windows in the system. The listing is written in the
4569 * specified Socket's output stream with the following syntax:
4570 * windowHashCodeInHexadecimal windowName
4571 * Each line of the ouput represents a different window.
4572 *
4573 * @param client The remote client to send the listing to.
4574 * @return False if an error occured, true otherwise.
4575 */
4576 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004577 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578 return false;
4579 }
4580
4581 boolean result = true;
4582
4583 Object[] windows;
4584 synchronized (mWindowMap) {
4585 windows = new Object[mWindows.size()];
4586 //noinspection unchecked
4587 windows = mWindows.toArray(windows);
4588 }
4589
4590 BufferedWriter out = null;
4591
4592 // Any uncaught exception will crash the system process
4593 try {
4594 OutputStream clientStream = client.getOutputStream();
4595 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4596
4597 final int count = windows.length;
4598 for (int i = 0; i < count; i++) {
4599 final WindowState w = (WindowState) windows[i];
4600 out.write(Integer.toHexString(System.identityHashCode(w)));
4601 out.write(' ');
4602 out.append(w.mAttrs.getTitle());
4603 out.write('\n');
4604 }
4605
4606 out.write("DONE.\n");
4607 out.flush();
4608 } catch (Exception e) {
4609 result = false;
4610 } finally {
4611 if (out != null) {
4612 try {
4613 out.close();
4614 } catch (IOException e) {
4615 result = false;
4616 }
4617 }
4618 }
4619
4620 return result;
4621 }
4622
4623 /**
4624 * Sends a command to a target window. The result of the command, if any, will be
4625 * written in the output stream of the specified socket.
4626 *
4627 * The parameters must follow this syntax:
4628 * windowHashcode extra
4629 *
4630 * Where XX is the length in characeters of the windowTitle.
4631 *
4632 * The first parameter is the target window. The window with the specified hashcode
4633 * will be the target. If no target can be found, nothing happens. The extra parameters
4634 * will be delivered to the target window and as parameters to the command itself.
4635 *
4636 * @param client The remote client to sent the result, if any, to.
4637 * @param command The command to execute.
4638 * @param parameters The command parameters.
4639 *
4640 * @return True if the command was successfully delivered, false otherwise. This does
4641 * not indicate whether the command itself was successful.
4642 */
4643 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004644 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004645 return false;
4646 }
4647
4648 boolean success = true;
4649 Parcel data = null;
4650 Parcel reply = null;
4651
4652 // Any uncaught exception will crash the system process
4653 try {
4654 // Find the hashcode of the window
4655 int index = parameters.indexOf(' ');
4656 if (index == -1) {
4657 index = parameters.length();
4658 }
4659 final String code = parameters.substring(0, index);
4660 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4661
4662 // Extract the command's parameter after the window description
4663 if (index < parameters.length()) {
4664 parameters = parameters.substring(index + 1);
4665 } else {
4666 parameters = "";
4667 }
4668
4669 final WindowManagerService.WindowState window = findWindow(hashCode);
4670 if (window == null) {
4671 return false;
4672 }
4673
4674 data = Parcel.obtain();
4675 data.writeInterfaceToken("android.view.IWindow");
4676 data.writeString(command);
4677 data.writeString(parameters);
4678 data.writeInt(1);
4679 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4680
4681 reply = Parcel.obtain();
4682
4683 final IBinder binder = window.mClient.asBinder();
4684 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4685 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4686
4687 reply.readException();
4688
4689 } catch (Exception e) {
4690 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4691 success = false;
4692 } finally {
4693 if (data != null) {
4694 data.recycle();
4695 }
4696 if (reply != null) {
4697 reply.recycle();
4698 }
4699 }
4700
4701 return success;
4702 }
4703
4704 private WindowState findWindow(int hashCode) {
4705 if (hashCode == -1) {
4706 return getFocusedWindow();
4707 }
4708
4709 synchronized (mWindowMap) {
4710 final ArrayList windows = mWindows;
4711 final int count = windows.size();
4712
4713 for (int i = 0; i < count; i++) {
4714 WindowState w = (WindowState) windows.get(i);
4715 if (System.identityHashCode(w) == hashCode) {
4716 return w;
4717 }
4718 }
4719 }
4720
4721 return null;
4722 }
4723
4724 /*
4725 * Instruct the Activity Manager to fetch the current configuration and broadcast
4726 * that to config-changed listeners if appropriate.
4727 */
4728 void sendNewConfiguration() {
4729 try {
4730 mActivityManager.updateConfiguration(null);
4731 } catch (RemoteException e) {
4732 }
4733 }
Romain Guy06882f82009-06-10 13:36:04 -07004734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004735 public Configuration computeNewConfiguration() {
4736 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004737 return computeNewConfigurationLocked();
4738 }
4739 }
Romain Guy06882f82009-06-10 13:36:04 -07004740
Dianne Hackbornc485a602009-03-24 22:39:49 -07004741 Configuration computeNewConfigurationLocked() {
4742 Configuration config = new Configuration();
4743 if (!computeNewConfigurationLocked(config)) {
4744 return null;
4745 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004746 return config;
4747 }
Romain Guy06882f82009-06-10 13:36:04 -07004748
Dianne Hackbornc485a602009-03-24 22:39:49 -07004749 boolean computeNewConfigurationLocked(Configuration config) {
4750 if (mDisplay == null) {
4751 return false;
4752 }
4753 mQueue.getInputConfiguration(config);
4754 final int dw = mDisplay.getWidth();
4755 final int dh = mDisplay.getHeight();
4756 int orientation = Configuration.ORIENTATION_SQUARE;
4757 if (dw < dh) {
4758 orientation = Configuration.ORIENTATION_PORTRAIT;
4759 } else if (dw > dh) {
4760 orientation = Configuration.ORIENTATION_LANDSCAPE;
4761 }
4762 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004763
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004764 DisplayMetrics dm = new DisplayMetrics();
4765 mDisplay.getMetrics(dm);
4766 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4767
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004768 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004769 // Note we only do this once because at this point we don't
4770 // expect the screen to change in this way at runtime, and want
4771 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004772 int longSize = dw;
4773 int shortSize = dh;
4774 if (longSize < shortSize) {
4775 int tmp = longSize;
4776 longSize = shortSize;
4777 shortSize = tmp;
4778 }
4779 longSize = (int)(longSize/dm.density);
4780 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004781
Dianne Hackborn723738c2009-06-25 19:48:04 -07004782 // These semi-magic numbers define our compatibility modes for
4783 // applications with different screens. Don't change unless you
4784 // make sure to test lots and lots of apps!
4785 if (longSize < 470) {
4786 // This is shorter than an HVGA normal density screen (which
4787 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004788 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4789 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004790 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004791 // Is this a large screen?
4792 if (longSize > 640 && shortSize >= 480) {
4793 // VGA or larger screens at medium density are the point
4794 // at which we consider it to be a large screen.
4795 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4796 } else {
4797 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4798
4799 // If this screen is wider than normal HVGA, or taller
4800 // than FWVGA, then for old apps we want to run in size
4801 // compatibility mode.
4802 if (shortSize > 321 || longSize > 570) {
4803 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4804 }
4805 }
4806
4807 // Is this a long screen?
4808 if (((longSize*3)/5) >= (shortSize-1)) {
4809 // Anything wider than WVGA (5:3) is considering to be long.
4810 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4811 } else {
4812 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4813 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004814 }
4815 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004816 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004817
Dianne Hackbornc485a602009-03-24 22:39:49 -07004818 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4819 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4820 mPolicy.adjustConfigurationLw(config);
4821 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004822 }
Romain Guy06882f82009-06-10 13:36:04 -07004823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824 // -------------------------------------------------------------
4825 // Input Events and Focus Management
4826 // -------------------------------------------------------------
4827
4828 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004829 long curTime = SystemClock.uptimeMillis();
4830
Michael Chane10de972009-05-18 11:24:50 -07004831 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004832 if (mLastTouchEventType == eventType &&
4833 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4834 return;
4835 }
4836 mLastUserActivityCallTime = curTime;
4837 mLastTouchEventType = eventType;
4838 }
4839
4840 if (targetWin == null
4841 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4842 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004843 }
4844 }
4845
4846 // tells if it's a cheek event or not -- this function is stateful
4847 private static final int EVENT_NONE = 0;
4848 private static final int EVENT_UNKNOWN = 0;
4849 private static final int EVENT_CHEEK = 0;
4850 private static final int EVENT_IGNORE_DURATION = 300; // ms
4851 private static final float CHEEK_THRESHOLD = 0.6f;
4852 private int mEventState = EVENT_NONE;
4853 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004855 private int eventType(MotionEvent ev) {
4856 float size = ev.getSize();
4857 switch (ev.getAction()) {
4858 case MotionEvent.ACTION_DOWN:
4859 mEventSize = size;
4860 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4861 case MotionEvent.ACTION_UP:
4862 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004863 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004864 case MotionEvent.ACTION_MOVE:
4865 final int N = ev.getHistorySize();
4866 if (size > mEventSize) mEventSize = size;
4867 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4868 for (int i=0; i<N; i++) {
4869 size = ev.getHistoricalSize(i);
4870 if (size > mEventSize) mEventSize = size;
4871 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4872 }
4873 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4874 return TOUCH_EVENT;
4875 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004876 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 }
4878 default:
4879 // not good
4880 return OTHER_EVENT;
4881 }
4882 }
4883
4884 /**
4885 * @return Returns true if event was dispatched, false if it was dropped for any reason
4886 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004887 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004888 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4889 "dispatchPointer " + ev);
4890
Michael Chan53071d62009-05-13 17:29:48 -07004891 if (MEASURE_LATENCY) {
4892 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4893 }
4894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004896 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004897
Michael Chan53071d62009-05-13 17:29:48 -07004898 if (MEASURE_LATENCY) {
4899 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4900 }
4901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004904 if (action == MotionEvent.ACTION_UP) {
4905 // let go of our target
4906 mKeyWaiter.mMotionTarget = null;
4907 mPowerManager.logPointerUpEvent();
4908 } else if (action == MotionEvent.ACTION_DOWN) {
4909 mPowerManager.logPointerDownEvent();
4910 }
4911
4912 if (targetObj == null) {
4913 // In this case we are either dropping the event, or have received
4914 // a move or up without a down. It is common to receive move
4915 // events in such a way, since this means the user is moving the
4916 // pointer without actually pressing down. All other cases should
4917 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004918 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004919 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4920 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08004921 synchronized (mWindowMap) {
4922 if (mSendingPointersToWallpaper) {
4923 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4924 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4925 }
4926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 if (qev != null) {
4928 mQueue.recycleEvent(qev);
4929 }
4930 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004931 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 }
4933 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004934 synchronized (mWindowMap) {
4935 if (mSendingPointersToWallpaper) {
4936 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4937 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4938 }
4939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 if (qev != null) {
4941 mQueue.recycleEvent(qev);
4942 }
4943 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004944 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945 }
Romain Guy06882f82009-06-10 13:36:04 -07004946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004949 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004950 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951
4952 //Log.i(TAG, "Sending " + ev + " to " + target);
4953
4954 if (uid != 0 && uid != target.mSession.mUid) {
4955 if (mContext.checkPermission(
4956 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4957 != PackageManager.PERMISSION_GRANTED) {
4958 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4959 + pid + " uid " + uid + " to window " + target
4960 + " owned by uid " + target.mSession.mUid);
4961 if (qev != null) {
4962 mQueue.recycleEvent(qev);
4963 }
4964 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004965 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 }
4967 }
4968
Michael Chan53071d62009-05-13 17:29:48 -07004969 if (MEASURE_LATENCY) {
4970 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4971 }
4972
Romain Guy06882f82009-06-10 13:36:04 -07004973 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004974 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4975 //target wants to ignore fat touch events
4976 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4977 //explicit flag to return without processing event further
4978 boolean returnFlag = false;
4979 if((action == MotionEvent.ACTION_DOWN)) {
4980 mFatTouch = false;
4981 if(cheekPress) {
4982 mFatTouch = true;
4983 returnFlag = true;
4984 }
4985 } else {
4986 if(action == MotionEvent.ACTION_UP) {
4987 if(mFatTouch) {
4988 //earlier even was invalid doesnt matter if current up is cheekpress or not
4989 mFatTouch = false;
4990 returnFlag = true;
4991 } else if(cheekPress) {
4992 //cancel the earlier event
4993 ev.setAction(MotionEvent.ACTION_CANCEL);
4994 action = MotionEvent.ACTION_CANCEL;
4995 }
4996 } else if(action == MotionEvent.ACTION_MOVE) {
4997 if(mFatTouch) {
4998 //two cases here
4999 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07005000 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 returnFlag = true;
5002 } else if(cheekPress) {
5003 //valid down followed by invalid moves
5004 //an invalid move have to cancel earlier action
5005 ev.setAction(MotionEvent.ACTION_CANCEL);
5006 action = MotionEvent.ACTION_CANCEL;
5007 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
5008 //note that the subsequent invalid moves will not get here
5009 mFatTouch = true;
5010 }
5011 }
5012 } //else if action
5013 if(returnFlag) {
5014 //recycle que, ev
5015 if (qev != null) {
5016 mQueue.recycleEvent(qev);
5017 }
5018 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005019 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 }
5021 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005022
Michael Chan9f028e62009-08-04 17:37:46 -07005023 // Enable this for testing the "right" value
5024 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005025 int max_events_per_sec = 35;
5026 try {
5027 max_events_per_sec = Integer.parseInt(SystemProperties
5028 .get("windowsmgr.max_events_per_sec"));
5029 if (max_events_per_sec < 1) {
5030 max_events_per_sec = 35;
5031 }
5032 } catch (NumberFormatException e) {
5033 }
5034 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5035 }
5036
5037 /*
5038 * Throttle events to minimize CPU usage when there's a flood of events
5039 * e.g. constant contact with the screen
5040 */
5041 if (action == MotionEvent.ACTION_MOVE) {
5042 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5043 long now = SystemClock.uptimeMillis();
5044 if (now < nextEventTime) {
5045 try {
5046 Thread.sleep(nextEventTime - now);
5047 } catch (InterruptedException e) {
5048 }
5049 mLastTouchEventTime = nextEventTime;
5050 } else {
5051 mLastTouchEventTime = now;
5052 }
5053 }
5054
Michael Chan53071d62009-05-13 17:29:48 -07005055 if (MEASURE_LATENCY) {
5056 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5057 }
5058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005060 if (!target.isVisibleLw()) {
5061 // During this motion dispatch, the target window has become
5062 // invisible.
5063 if (mSendingPointersToWallpaper) {
5064 sendPointerToWallpaperLocked(null, ev, eventTime);
5065 }
5066 if (qev != null) {
5067 mQueue.recycleEvent(qev);
5068 }
5069 ev.recycle();
5070 return INJECT_SUCCEEDED;
5071 }
5072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5074 mKeyWaiter.bindTargetWindowLocked(target,
5075 KeyWaiter.RETURN_PENDING_POINTER, qev);
5076 ev = null;
5077 } else {
5078 if (action == MotionEvent.ACTION_DOWN) {
5079 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5080 if (out != null) {
5081 MotionEvent oev = MotionEvent.obtain(ev);
5082 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5083 do {
5084 final Rect frame = out.mFrame;
5085 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5086 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005087 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 } catch (android.os.RemoteException e) {
5089 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
5090 }
5091 oev.offsetLocation((float)frame.left, (float)frame.top);
5092 out = out.mNextOutsideTouch;
5093 } while (out != null);
5094 mKeyWaiter.mOutsideTouchTargets = null;
5095 }
5096 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005097
5098 // If we are on top of the wallpaper, then the wallpaper also
5099 // gets to see this movement.
Dianne Hackborn9b52a212009-12-11 14:51:35 -08005100 if ((mWallpaperTarget == target &&
5101 target.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD)
5102 || mSendingPointersToWallpaper) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005103 sendPointerToWallpaperLocked(null, ev, eventTime);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005104 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005105
5106 final Rect frame = target.mFrame;
5107 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5108 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 }
5110 }
Romain Guy06882f82009-06-10 13:36:04 -07005111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 // finally offset the event to the target's coordinate system and
5113 // dispatch the event.
5114 try {
5115 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
5116 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
5117 }
Michael Chan53071d62009-05-13 17:29:48 -07005118
5119 if (MEASURE_LATENCY) {
5120 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5121 }
5122
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005123 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005124
5125 if (MEASURE_LATENCY) {
5126 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5127 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005128 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 } catch (android.os.RemoteException e) {
5130 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
5131 mKeyWaiter.mMotionTarget = null;
5132 try {
5133 removeWindow(target.mSession, target.mClient);
5134 } catch (java.util.NoSuchElementException ex) {
5135 // This will happen if the window has already been
5136 // removed.
5137 }
5138 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005139 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005140 }
Romain Guy06882f82009-06-10 13:36:04 -07005141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 /**
5143 * @return Returns true if event was dispatched, false if it was dropped for any reason
5144 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005145 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 if (DEBUG_INPUT) Log.v(
5147 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005150 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005151 if (focusObj == null) {
5152 Log.w(TAG, "No focus window, dropping trackball: " + ev);
5153 if (qev != null) {
5154 mQueue.recycleEvent(qev);
5155 }
5156 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005157 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005158 }
5159 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5160 if (qev != null) {
5161 mQueue.recycleEvent(qev);
5162 }
5163 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005164 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005165 }
Romain Guy06882f82009-06-10 13:36:04 -07005166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 if (uid != 0 && uid != focus.mSession.mUid) {
5170 if (mContext.checkPermission(
5171 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5172 != PackageManager.PERMISSION_GRANTED) {
5173 Log.w(TAG, "Permission denied: injecting key event from pid "
5174 + pid + " uid " + uid + " to window " + focus
5175 + " owned by uid " + focus.mSession.mUid);
5176 if (qev != null) {
5177 mQueue.recycleEvent(qev);
5178 }
5179 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005180 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 }
5182 }
Romain Guy06882f82009-06-10 13:36:04 -07005183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 synchronized(mWindowMap) {
5187 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5188 mKeyWaiter.bindTargetWindowLocked(focus,
5189 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5190 // We don't deliver movement events to the client, we hold
5191 // them and wait for them to call back.
5192 ev = null;
5193 } else {
5194 mKeyWaiter.bindTargetWindowLocked(focus);
5195 }
5196 }
Romain Guy06882f82009-06-10 13:36:04 -07005197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005198 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005199 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005200 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 } catch (android.os.RemoteException e) {
5202 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5203 try {
5204 removeWindow(focus.mSession, focus.mClient);
5205 } catch (java.util.NoSuchElementException ex) {
5206 // This will happen if the window has already been
5207 // removed.
5208 }
5209 }
Romain Guy06882f82009-06-10 13:36:04 -07005210
Dianne Hackborncfaef692009-06-15 14:24:44 -07005211 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 }
Romain Guy06882f82009-06-10 13:36:04 -07005213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 /**
5215 * @return Returns true if event was dispatched, false if it was dropped for any reason
5216 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005217 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5219
5220 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005221 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 if (focusObj == null) {
5223 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005224 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 }
5226 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005227 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228 }
Romain Guy06882f82009-06-10 13:36:04 -07005229
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005230 // Okay we have finished waiting for the last event to be processed.
5231 // First off, if this is a repeat event, check to see if there is
5232 // a corresponding up event in the queue. If there is, we will
5233 // just drop the repeat, because it makes no sense to repeat after
5234 // the user has released a key. (This is especially important for
5235 // long presses.)
5236 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5237 return INJECT_SUCCEEDED;
5238 }
5239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 if (DEBUG_INPUT) Log.v(
5243 TAG, "Dispatching to " + focus + ": " + event);
5244
5245 if (uid != 0 && uid != focus.mSession.mUid) {
5246 if (mContext.checkPermission(
5247 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5248 != PackageManager.PERMISSION_GRANTED) {
5249 Log.w(TAG, "Permission denied: injecting key event from pid "
5250 + pid + " uid " + uid + " to window " + focus
5251 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005252 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 }
5254 }
Romain Guy06882f82009-06-10 13:36:04 -07005255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 synchronized(mWindowMap) {
5257 mKeyWaiter.bindTargetWindowLocked(focus);
5258 }
5259
5260 // NOSHIP extra state logging
5261 mKeyWaiter.recordDispatchState(event, focus);
5262 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 try {
5265 if (DEBUG_INPUT || DEBUG_FOCUS) {
5266 Log.v(TAG, "Delivering key " + event.getKeyCode()
5267 + " to " + focus);
5268 }
5269 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005270 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 } catch (android.os.RemoteException e) {
5272 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5273 try {
5274 removeWindow(focus.mSession, focus.mClient);
5275 } catch (java.util.NoSuchElementException ex) {
5276 // This will happen if the window has already been
5277 // removed.
5278 }
5279 }
Romain Guy06882f82009-06-10 13:36:04 -07005280
Dianne Hackborncfaef692009-06-15 14:24:44 -07005281 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005282 }
Romain Guy06882f82009-06-10 13:36:04 -07005283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 public void pauseKeyDispatching(IBinder _token) {
5285 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5286 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005287 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 }
5289
5290 synchronized (mWindowMap) {
5291 WindowToken token = mTokenMap.get(_token);
5292 if (token != null) {
5293 mKeyWaiter.pauseDispatchingLocked(token);
5294 }
5295 }
5296 }
5297
5298 public void resumeKeyDispatching(IBinder _token) {
5299 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5300 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005301 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005302 }
5303
5304 synchronized (mWindowMap) {
5305 WindowToken token = mTokenMap.get(_token);
5306 if (token != null) {
5307 mKeyWaiter.resumeDispatchingLocked(token);
5308 }
5309 }
5310 }
5311
5312 public void setEventDispatching(boolean enabled) {
5313 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5314 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005315 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 }
5317
5318 synchronized (mWindowMap) {
5319 mKeyWaiter.setEventDispatchingLocked(enabled);
5320 }
5321 }
Romain Guy06882f82009-06-10 13:36:04 -07005322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 /**
5324 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005325 *
5326 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005327 * {@link SystemClock#uptimeMillis()} as the timebase.)
5328 * @param sync If true, wait for the event to be completed before returning to the caller.
5329 * @return Returns true if event was dispatched, false if it was dropped for any reason
5330 */
5331 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5332 long downTime = ev.getDownTime();
5333 long eventTime = ev.getEventTime();
5334
5335 int action = ev.getAction();
5336 int code = ev.getKeyCode();
5337 int repeatCount = ev.getRepeatCount();
5338 int metaState = ev.getMetaState();
5339 int deviceId = ev.getDeviceId();
5340 int scancode = ev.getScanCode();
5341
5342 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5343 if (downTime == 0) downTime = eventTime;
5344
5345 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005346 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005348 final int pid = Binder.getCallingPid();
5349 final int uid = Binder.getCallingUid();
5350 final long ident = Binder.clearCallingIdentity();
5351 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005353 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005355 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005356 switch (result) {
5357 case INJECT_NO_PERMISSION:
5358 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305359 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005360 case INJECT_SUCCEEDED:
5361 return true;
5362 }
5363 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 }
5365
5366 /**
5367 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005368 *
5369 * @param ev A motion event describing the pointer (touch) action. (As noted in
5370 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005371 * {@link SystemClock#uptimeMillis()} as the timebase.)
5372 * @param sync If true, wait for the event to be completed before returning to the caller.
5373 * @return Returns true if event was dispatched, false if it was dropped for any reason
5374 */
5375 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005376 final int pid = Binder.getCallingPid();
5377 final int uid = Binder.getCallingUid();
5378 final long ident = Binder.clearCallingIdentity();
5379 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005380 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005381 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005383 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005384 switch (result) {
5385 case INJECT_NO_PERMISSION:
5386 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305387 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005388 case INJECT_SUCCEEDED:
5389 return true;
5390 }
5391 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392 }
Romain Guy06882f82009-06-10 13:36:04 -07005393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 /**
5395 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005396 *
5397 * @param ev A motion event describing the trackball action. (As noted in
5398 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 * {@link SystemClock#uptimeMillis()} as the timebase.)
5400 * @param sync If true, wait for the event to be completed before returning to the caller.
5401 * @return Returns true if event was dispatched, false if it was dropped for any reason
5402 */
5403 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005404 final int pid = Binder.getCallingPid();
5405 final int uid = Binder.getCallingUid();
5406 final long ident = Binder.clearCallingIdentity();
5407 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005408 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005409 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005411 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005412 switch (result) {
5413 case INJECT_NO_PERMISSION:
5414 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305415 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005416 case INJECT_SUCCEEDED:
5417 return true;
5418 }
5419 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005420 }
Romain Guy06882f82009-06-10 13:36:04 -07005421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 private WindowState getFocusedWindow() {
5423 synchronized (mWindowMap) {
5424 return getFocusedWindowLocked();
5425 }
5426 }
5427
5428 private WindowState getFocusedWindowLocked() {
5429 return mCurrentFocus;
5430 }
Romain Guy06882f82009-06-10 13:36:04 -07005431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 /**
5433 * This class holds the state for dispatching key events. This state
5434 * is protected by the KeyWaiter instance, NOT by the window lock. You
5435 * can be holding the main window lock while acquire the KeyWaiter lock,
5436 * but not the other way around.
5437 */
5438 final class KeyWaiter {
5439 // NOSHIP debugging
5440 public class DispatchState {
5441 private KeyEvent event;
5442 private WindowState focus;
5443 private long time;
5444 private WindowState lastWin;
5445 private IBinder lastBinder;
5446 private boolean finished;
5447 private boolean gotFirstWindow;
5448 private boolean eventDispatching;
5449 private long timeToSwitch;
5450 private boolean wasFrozen;
5451 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005452 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5455 focus = theFocus;
5456 event = theEvent;
5457 time = System.currentTimeMillis();
5458 // snapshot KeyWaiter state
5459 lastWin = mLastWin;
5460 lastBinder = mLastBinder;
5461 finished = mFinished;
5462 gotFirstWindow = mGotFirstWindow;
5463 eventDispatching = mEventDispatching;
5464 timeToSwitch = mTimeToSwitch;
5465 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005466 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 // cache the paused state at ctor time as well
5468 if (theFocus == null || theFocus.mToken == null) {
5469 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5470 focusPaused = false;
5471 } else {
5472 focusPaused = theFocus.mToken.paused;
5473 }
5474 }
Romain Guy06882f82009-06-10 13:36:04 -07005475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 public String toString() {
5477 return "{{" + event + " to " + focus + " @ " + time
5478 + " lw=" + lastWin + " lb=" + lastBinder
5479 + " fin=" + finished + " gfw=" + gotFirstWindow
5480 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005481 + " wf=" + wasFrozen + " fp=" + focusPaused
Johan Alfven6c3da7f2010-05-12 09:00:28 +02005482 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 }
5484 };
5485 private DispatchState mDispatchState = null;
5486 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5487 mDispatchState = new DispatchState(theEvent, theFocus);
5488 }
5489 // END NOSHIP
5490
5491 public static final int RETURN_NOTHING = 0;
5492 public static final int RETURN_PENDING_POINTER = 1;
5493 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005495 final Object SKIP_TARGET_TOKEN = new Object();
5496 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 private WindowState mLastWin = null;
5499 private IBinder mLastBinder = null;
5500 private boolean mFinished = true;
5501 private boolean mGotFirstWindow = false;
5502 private boolean mEventDispatching = true;
5503 private long mTimeToSwitch = 0;
5504 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 // Target of Motion events
5507 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 // Windows above the target who would like to receive an "outside"
5510 // touch event for any down events outside of them.
5511 WindowState mOutsideTouchTargets;
5512
5513 /**
5514 * Wait for the last event dispatch to complete, then find the next
5515 * target that should receive the given event and wait for that one
5516 * to be ready to receive it.
5517 */
5518 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5519 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005520 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005521 long startTime = SystemClock.uptimeMillis();
5522 long keyDispatchingTimeout = 5 * 1000;
5523 long waitedFor = 0;
5524
5525 while (true) {
5526 // Figure out which window we care about. It is either the
5527 // last window we are waiting to have process the event or,
5528 // if none, then the next window we think the event should go
5529 // to. Note: we retrieve mLastWin outside of the lock, so
5530 // it may change before we lock. Thus we must check it again.
5531 WindowState targetWin = mLastWin;
5532 boolean targetIsNew = targetWin == null;
5533 if (DEBUG_INPUT) Log.v(
5534 TAG, "waitForLastKey: mFinished=" + mFinished +
5535 ", mLastWin=" + mLastWin);
5536 if (targetIsNew) {
5537 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005538 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005539 if (target == SKIP_TARGET_TOKEN) {
5540 // The user has pressed a special key, and we are
5541 // dropping all pending events before it.
5542 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5543 + " " + nextMotion);
5544 return null;
5545 }
5546 if (target == CONSUMED_EVENT_TOKEN) {
5547 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5548 + " " + nextMotion);
5549 return target;
5550 }
5551 targetWin = (WindowState)target;
5552 }
Romain Guy06882f82009-06-10 13:36:04 -07005553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005554 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005556 // Now: is it okay to send the next event to this window?
5557 synchronized (this) {
5558 // First: did we come here based on the last window not
5559 // being null, but it changed by the time we got here?
5560 // If so, try again.
5561 if (!targetIsNew && mLastWin == null) {
5562 continue;
5563 }
Romain Guy06882f82009-06-10 13:36:04 -07005564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 // We never dispatch events if not finished with the
5566 // last one, or the display is frozen.
5567 if (mFinished && !mDisplayFrozen) {
5568 // If event dispatching is disabled, then we
5569 // just consume the events.
5570 if (!mEventDispatching) {
5571 if (DEBUG_INPUT) Log.v(TAG,
5572 "Skipping event; dispatching disabled: "
5573 + nextKey + " " + nextMotion);
5574 return null;
5575 }
5576 if (targetWin != null) {
5577 // If this is a new target, and that target is not
5578 // paused or unresponsive, then all looks good to
5579 // handle the event.
5580 if (targetIsNew && !targetWin.mToken.paused) {
5581 return targetWin;
5582 }
Romain Guy06882f82009-06-10 13:36:04 -07005583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005584 // If we didn't find a target window, and there is no
5585 // focused app window, then just eat the events.
5586 } else if (mFocusedApp == null) {
5587 if (DEBUG_INPUT) Log.v(TAG,
5588 "Skipping event; no focused app: "
5589 + nextKey + " " + nextMotion);
5590 return null;
5591 }
5592 }
Romain Guy06882f82009-06-10 13:36:04 -07005593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 if (DEBUG_INPUT) Log.v(
5595 TAG, "Waiting for last key in " + mLastBinder
5596 + " target=" + targetWin
5597 + " mFinished=" + mFinished
5598 + " mDisplayFrozen=" + mDisplayFrozen
5599 + " targetIsNew=" + targetIsNew
5600 + " paused="
5601 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005602 + " mFocusedApp=" + mFocusedApp
5603 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005605 targetApp = targetWin != null
5606 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 long curTimeout = keyDispatchingTimeout;
5609 if (mTimeToSwitch != 0) {
5610 long now = SystemClock.uptimeMillis();
5611 if (mTimeToSwitch <= now) {
5612 // If an app switch key has been pressed, and we have
5613 // waited too long for the current app to finish
5614 // processing keys, then wait no more!
5615 doFinishedKeyLocked(true);
5616 continue;
5617 }
5618 long switchTimeout = mTimeToSwitch - now;
5619 if (curTimeout > switchTimeout) {
5620 curTimeout = switchTimeout;
5621 }
5622 }
Romain Guy06882f82009-06-10 13:36:04 -07005623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005624 try {
5625 // after that continue
5626 // processing keys, so we don't get stuck.
5627 if (DEBUG_INPUT) Log.v(
5628 TAG, "Waiting for key dispatch: " + curTimeout);
5629 wait(curTimeout);
5630 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5631 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005632 + startTime + " switchTime=" + mTimeToSwitch
5633 + " target=" + targetWin + " mLW=" + mLastWin
5634 + " mLB=" + mLastBinder + " fin=" + mFinished
5635 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 } catch (InterruptedException e) {
5637 }
5638 }
5639
5640 // If we were frozen during configuration change, restart the
5641 // timeout checks from now; otherwise look at whether we timed
5642 // out before awakening.
5643 if (mWasFrozen) {
5644 waitedFor = 0;
5645 mWasFrozen = false;
5646 } else {
5647 waitedFor = SystemClock.uptimeMillis() - startTime;
5648 }
5649
5650 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5651 IApplicationToken at = null;
5652 synchronized (this) {
5653 Log.w(TAG, "Key dispatching timed out sending to " +
5654 (targetWin != null ? targetWin.mAttrs.getTitle()
5655 : "<null>"));
5656 // NOSHIP debugging
5657 Log.w(TAG, "Dispatch state: " + mDispatchState);
5658 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5659 // END NOSHIP
5660 //dump();
5661 if (targetWin != null) {
5662 at = targetWin.getAppToken();
5663 } else if (targetApp != null) {
5664 at = targetApp.appToken;
5665 }
5666 }
5667
5668 boolean abort = true;
5669 if (at != null) {
5670 try {
5671 long timeout = at.getKeyDispatchingTimeout();
5672 if (timeout > waitedFor) {
5673 // we did not wait the proper amount of time for this application.
5674 // set the timeout to be the real timeout and wait again.
5675 keyDispatchingTimeout = timeout - waitedFor;
5676 continue;
5677 } else {
5678 abort = at.keyDispatchingTimedOut();
5679 }
5680 } catch (RemoteException ex) {
5681 }
5682 }
5683
5684 synchronized (this) {
5685 if (abort && (mLastWin == targetWin || targetWin == null)) {
5686 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005687 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005688 if (DEBUG_INPUT) Log.v(TAG,
5689 "Window " + mLastWin +
5690 " timed out on key input");
5691 if (mLastWin.mToken.paused) {
5692 Log.w(TAG, "Un-pausing dispatching to this window");
5693 mLastWin.mToken.paused = false;
5694 }
5695 }
5696 if (mMotionTarget == targetWin) {
5697 mMotionTarget = null;
5698 }
5699 mLastWin = null;
5700 mLastBinder = null;
5701 if (failIfTimeout || targetWin == null) {
5702 return null;
5703 }
5704 } else {
5705 Log.w(TAG, "Continuing to wait for key to be dispatched");
5706 startTime = SystemClock.uptimeMillis();
5707 }
5708 }
5709 }
5710 }
5711 }
Romain Guy06882f82009-06-10 13:36:04 -07005712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005714 MotionEvent nextMotion, boolean isPointerEvent,
5715 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005716 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 if (nextKey != null) {
5719 // Find the target window for a normal key event.
5720 final int keycode = nextKey.getKeyCode();
5721 final int repeatCount = nextKey.getRepeatCount();
5722 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5723 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005726 if (callingUid == 0 ||
5727 mContext.checkPermission(
5728 android.Manifest.permission.INJECT_EVENTS,
5729 callingPid, callingUid)
5730 == PackageManager.PERMISSION_GRANTED) {
5731 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005732 nextKey.getMetaState(), down, repeatCount,
5733 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 Log.w(TAG, "Event timeout during app switch: dropping "
5736 + nextKey);
5737 return SKIP_TARGET_TOKEN;
5738 }
Romain Guy06882f82009-06-10 13:36:04 -07005739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005740 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005742 WindowState focus = null;
5743 synchronized(mWindowMap) {
5744 focus = getFocusedWindowLocked();
5745 }
Romain Guy06882f82009-06-10 13:36:04 -07005746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005748
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005749 if (callingUid == 0 ||
5750 (focus != null && callingUid == focus.mSession.mUid) ||
5751 mContext.checkPermission(
5752 android.Manifest.permission.INJECT_EVENTS,
5753 callingPid, callingUid)
5754 == PackageManager.PERMISSION_GRANTED) {
5755 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005756 keycode, nextKey.getMetaState(), down, repeatCount,
5757 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005758 return CONSUMED_EVENT_TOKEN;
5759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 }
Romain Guy06882f82009-06-10 13:36:04 -07005761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 } else if (!isPointerEvent) {
5765 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5766 if (!dispatch) {
5767 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5768 + nextMotion);
5769 return SKIP_TARGET_TOKEN;
5770 }
Romain Guy06882f82009-06-10 13:36:04 -07005771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005772 WindowState focus = null;
5773 synchronized(mWindowMap) {
5774 focus = getFocusedWindowLocked();
5775 }
Romain Guy06882f82009-06-10 13:36:04 -07005776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5778 return focus;
5779 }
Romain Guy06882f82009-06-10 13:36:04 -07005780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 if (nextMotion == null) {
5782 return SKIP_TARGET_TOKEN;
5783 }
Romain Guy06882f82009-06-10 13:36:04 -07005784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005785 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5786 KeyEvent.KEYCODE_UNKNOWN);
5787 if (!dispatch) {
5788 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5789 + nextMotion);
5790 return SKIP_TARGET_TOKEN;
5791 }
Romain Guy06882f82009-06-10 13:36:04 -07005792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 // Find the target window for a pointer event.
5794 int action = nextMotion.getAction();
5795 final float xf = nextMotion.getX();
5796 final float yf = nextMotion.getY();
5797 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005799 final boolean screenWasOff = qev != null
5800 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804 synchronized(mWindowMap) {
5805 synchronized (this) {
5806 if (action == MotionEvent.ACTION_DOWN) {
5807 if (mMotionTarget != null) {
5808 // this is weird, we got a pen down, but we thought it was
5809 // already down!
5810 // XXX: We should probably send an ACTION_UP to the current
5811 // target.
5812 Log.w(TAG, "Pointer down received while already down in: "
5813 + mMotionTarget);
5814 mMotionTarget = null;
5815 }
Romain Guy06882f82009-06-10 13:36:04 -07005816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 // ACTION_DOWN is special, because we need to lock next events to
5818 // the window we'll land onto.
5819 final int x = (int)xf;
5820 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 final ArrayList windows = mWindows;
5823 final int N = windows.size();
5824 WindowState topErrWindow = null;
5825 final Rect tmpRect = mTempRect;
5826 for (int i=N-1; i>=0; i--) {
5827 WindowState child = (WindowState)windows.get(i);
5828 //Log.i(TAG, "Checking dispatch to: " + child);
5829 final int flags = child.mAttrs.flags;
5830 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5831 if (topErrWindow == null) {
5832 topErrWindow = child;
5833 }
5834 }
5835 if (!child.isVisibleLw()) {
5836 //Log.i(TAG, "Not visible!");
5837 continue;
5838 }
5839 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5840 //Log.i(TAG, "Not touchable!");
5841 if ((flags & WindowManager.LayoutParams
5842 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5843 child.mNextOutsideTouch = mOutsideTouchTargets;
5844 mOutsideTouchTargets = child;
5845 }
5846 continue;
5847 }
5848 tmpRect.set(child.mFrame);
5849 if (child.mTouchableInsets == ViewTreeObserver
5850 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5851 // The touch is inside of the window if it is
5852 // inside the frame, AND the content part of that
5853 // frame that was given by the application.
5854 tmpRect.left += child.mGivenContentInsets.left;
5855 tmpRect.top += child.mGivenContentInsets.top;
5856 tmpRect.right -= child.mGivenContentInsets.right;
5857 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5858 } else if (child.mTouchableInsets == ViewTreeObserver
5859 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5860 // The touch is inside of the window if it is
5861 // inside the frame, AND the visible part of that
5862 // frame that was given by the application.
5863 tmpRect.left += child.mGivenVisibleInsets.left;
5864 tmpRect.top += child.mGivenVisibleInsets.top;
5865 tmpRect.right -= child.mGivenVisibleInsets.right;
5866 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5867 }
5868 final int touchFlags = flags &
5869 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5870 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5871 if (tmpRect.contains(x, y) || touchFlags == 0) {
5872 //Log.i(TAG, "Using this target!");
5873 if (!screenWasOff || (flags &
5874 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5875 mMotionTarget = child;
5876 } else {
5877 //Log.i(TAG, "Waking, skip!");
5878 mMotionTarget = null;
5879 }
5880 break;
5881 }
Romain Guy06882f82009-06-10 13:36:04 -07005882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 if ((flags & WindowManager.LayoutParams
5884 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5885 child.mNextOutsideTouch = mOutsideTouchTargets;
5886 mOutsideTouchTargets = child;
5887 //Log.i(TAG, "Adding to outside target list: " + child);
5888 }
5889 }
5890
5891 // if there's an error window but it's not accepting
5892 // focus (typically because it is not yet visible) just
5893 // wait for it -- any other focused window may in fact
5894 // be in ANR state.
5895 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5896 mMotionTarget = null;
5897 }
5898 }
Romain Guy06882f82009-06-10 13:36:04 -07005899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 target = mMotionTarget;
5901 }
5902 }
Romain Guy06882f82009-06-10 13:36:04 -07005903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005906 // Pointer events are a little different -- if there isn't a
5907 // target found for any event, then just drop it.
5908 return target != null ? target : SKIP_TARGET_TOKEN;
5909 }
Romain Guy06882f82009-06-10 13:36:04 -07005910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 boolean checkShouldDispatchKey(int keycode) {
5912 synchronized (this) {
5913 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5914 mTimeToSwitch = 0;
5915 return true;
5916 }
5917 if (mTimeToSwitch != 0
5918 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5919 return false;
5920 }
5921 return true;
5922 }
5923 }
Romain Guy06882f82009-06-10 13:36:04 -07005924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 void bindTargetWindowLocked(WindowState win,
5926 int pendingWhat, QueuedEvent pendingMotion) {
5927 synchronized (this) {
5928 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5929 }
5930 }
Romain Guy06882f82009-06-10 13:36:04 -07005931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932 void bindTargetWindowLocked(WindowState win) {
5933 synchronized (this) {
5934 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5935 }
5936 }
5937
5938 void bindTargetWindowLockedLocked(WindowState win,
5939 int pendingWhat, QueuedEvent pendingMotion) {
5940 mLastWin = win;
5941 mLastBinder = win.mClient.asBinder();
5942 mFinished = false;
5943 if (pendingMotion != null) {
5944 final Session s = win.mSession;
5945 if (pendingWhat == RETURN_PENDING_POINTER) {
5946 releasePendingPointerLocked(s);
5947 s.mPendingPointerMove = pendingMotion;
5948 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005949 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 "bindTargetToWindow " + s.mPendingPointerMove);
5951 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5952 releasePendingTrackballLocked(s);
5953 s.mPendingTrackballMove = pendingMotion;
5954 s.mPendingTrackballWindow = win;
5955 }
5956 }
5957 }
Romain Guy06882f82009-06-10 13:36:04 -07005958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 void releasePendingPointerLocked(Session s) {
5960 if (DEBUG_INPUT) Log.v(TAG,
5961 "releasePendingPointer " + s.mPendingPointerMove);
5962 if (s.mPendingPointerMove != null) {
5963 mQueue.recycleEvent(s.mPendingPointerMove);
5964 s.mPendingPointerMove = null;
5965 }
5966 }
Romain Guy06882f82009-06-10 13:36:04 -07005967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005968 void releasePendingTrackballLocked(Session s) {
5969 if (s.mPendingTrackballMove != null) {
5970 mQueue.recycleEvent(s.mPendingTrackballMove);
5971 s.mPendingTrackballMove = null;
5972 }
5973 }
Romain Guy06882f82009-06-10 13:36:04 -07005974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5976 int returnWhat) {
5977 if (DEBUG_INPUT) Log.v(
5978 TAG, "finishedKey: client=" + client + ", force=" + force);
5979
5980 if (client == null) {
5981 return null;
5982 }
5983
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005984 MotionEvent res = null;
5985 QueuedEvent qev = null;
5986 WindowState win = null;
5987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 synchronized (this) {
5989 if (DEBUG_INPUT) Log.v(
5990 TAG, "finishedKey: client=" + client.asBinder()
5991 + ", force=" + force + ", last=" + mLastBinder
5992 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 if (returnWhat == RETURN_PENDING_POINTER) {
5995 qev = session.mPendingPointerMove;
5996 win = session.mPendingPointerWindow;
5997 session.mPendingPointerMove = null;
5998 session.mPendingPointerWindow = null;
5999 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
6000 qev = session.mPendingTrackballMove;
6001 win = session.mPendingTrackballWindow;
6002 session.mPendingTrackballMove = null;
6003 session.mPendingTrackballWindow = null;
6004 }
Romain Guy06882f82009-06-10 13:36:04 -07006005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 if (mLastBinder == client.asBinder()) {
6007 if (DEBUG_INPUT) Log.v(
6008 TAG, "finishedKey: last paused="
6009 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6010 if (mLastWin != null && (!mLastWin.mToken.paused || force
6011 || !mEventDispatching)) {
6012 doFinishedKeyLocked(false);
6013 } else {
6014 // Make sure to wake up anyone currently waiting to
6015 // dispatch a key, so they can re-evaluate their
6016 // current situation.
6017 mFinished = true;
6018 notifyAll();
6019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006020 }
Romain Guy06882f82009-06-10 13:36:04 -07006021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006023 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 if (DEBUG_INPUT) Log.v(TAG,
6025 "Returning pending motion: " + res);
6026 mQueue.recycleEvent(qev);
6027 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6028 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006031 }
6032
6033 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6034 synchronized (mWindowMap) {
6035 if ((mWallpaperTarget == win &&
6036 win.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD)
6037 || mSendingPointersToWallpaper) {
6038 sendPointerToWallpaperLocked(win, res, res.getEventTime());
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006039 }
6040 }
6041 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006042
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006043 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006044 }
6045
6046 void tickle() {
6047 synchronized (this) {
6048 notifyAll();
6049 }
6050 }
Romain Guy06882f82009-06-10 13:36:04 -07006051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052 void handleNewWindowLocked(WindowState newWindow) {
6053 if (!newWindow.canReceiveKeys()) {
6054 return;
6055 }
6056 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006057 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006058 TAG, "New key dispatch window: win="
6059 + newWindow.mClient.asBinder()
6060 + ", last=" + mLastBinder
6061 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6062 + "), finished=" + mFinished + ", paused="
6063 + newWindow.mToken.paused);
6064
6065 // Displaying a window implicitly causes dispatching to
6066 // be unpaused. (This is to protect against bugs if someone
6067 // pauses dispatching but forgets to resume.)
6068 newWindow.mToken.paused = false;
6069
6070 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006071
6072 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
6073 if (DEBUG_INPUT) Log.v(TAG,
6074 "New SYSTEM_ERROR window; resetting state");
6075 mLastWin = null;
6076 mLastBinder = null;
6077 mMotionTarget = null;
6078 mFinished = true;
6079 } else if (mLastWin != null) {
6080 // If the new window is above the window we are
6081 // waiting on, then stop waiting and let key dispatching
6082 // start on the new guy.
6083 if (DEBUG_INPUT) Log.v(
6084 TAG, "Last win layer=" + mLastWin.mLayer
6085 + ", new win layer=" + newWindow.mLayer);
6086 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006087 // The new window is above the old; finish pending input to the last
6088 // window and start directing it to the new one.
6089 mLastWin.mToken.paused = false;
6090 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006092 // Either the new window is lower, so there is no need to wake key waiters,
6093 // or we just finished key input to the previous window, which implicitly
6094 // notified the key waiters. In both cases, we don't need to issue the
6095 // notification here.
6096 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006097 }
6098
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006099 // Now that we've put a new window state in place, make the event waiter
6100 // take notice and retarget its attentions.
6101 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 }
6103 }
6104
6105 void pauseDispatchingLocked(WindowToken token) {
6106 synchronized (this)
6107 {
6108 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
6109 token.paused = true;
6110
6111 /*
6112 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6113 mPaused = true;
6114 } else {
6115 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07006116 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07006118 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006119 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07006120 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 }
6122 }
6123 */
6124 }
6125 }
6126
6127 void resumeDispatchingLocked(WindowToken token) {
6128 synchronized (this) {
6129 if (token.paused) {
6130 if (DEBUG_INPUT) Log.v(
6131 TAG, "Resuming WindowToken " + token
6132 + ", last=" + mLastBinder
6133 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6134 + "), finished=" + mFinished + ", paused="
6135 + token.paused);
6136 token.paused = false;
6137 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
6138 doFinishedKeyLocked(true);
6139 } else {
6140 notifyAll();
6141 }
6142 }
6143 }
6144 }
6145
6146 void setEventDispatchingLocked(boolean enabled) {
6147 synchronized (this) {
6148 mEventDispatching = enabled;
6149 notifyAll();
6150 }
6151 }
Romain Guy06882f82009-06-10 13:36:04 -07006152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 void appSwitchComing() {
6154 synchronized (this) {
6155 // Don't wait for more than .5 seconds for app to finish
6156 // processing the pending events.
6157 long now = SystemClock.uptimeMillis() + 500;
6158 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6159 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6160 mTimeToSwitch = now;
6161 }
6162 notifyAll();
6163 }
6164 }
Romain Guy06882f82009-06-10 13:36:04 -07006165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006166 private final void doFinishedKeyLocked(boolean doRecycle) {
6167 if (mLastWin != null) {
6168 releasePendingPointerLocked(mLastWin.mSession);
6169 releasePendingTrackballLocked(mLastWin.mSession);
6170 }
Romain Guy06882f82009-06-10 13:36:04 -07006171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 if (mLastWin == null || !mLastWin.mToken.paused
6173 || !mLastWin.isVisibleLw()) {
6174 // If the current window has been paused, we aren't -really-
6175 // finished... so let the waiters still wait.
6176 mLastWin = null;
6177 mLastBinder = null;
6178 }
6179 mFinished = true;
6180 notifyAll();
6181 }
6182 }
6183
6184 private class KeyQ extends KeyInputQueue
6185 implements KeyInputQueue.FilterCallback {
6186 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006189 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6191 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6192 "KEEP_SCREEN_ON_FLAG");
6193 mHoldingScreen.setReferenceCounted(false);
6194 }
6195
6196 @Override
6197 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6198 if (mPolicy.preprocessInputEventTq(event)) {
6199 return true;
6200 }
Romain Guy06882f82009-06-10 13:36:04 -07006201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006202 switch (event.type) {
6203 case RawInputEvent.EV_KEY: {
6204 // XXX begin hack
6205 if (DEBUG) {
6206 if (event.keycode == KeyEvent.KEYCODE_G) {
6207 if (event.value != 0) {
6208 // G down
6209 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6210 }
6211 return false;
6212 }
6213 if (event.keycode == KeyEvent.KEYCODE_D) {
6214 if (event.value != 0) {
6215 //dump();
6216 }
6217 return false;
6218 }
6219 }
6220 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006221
Charles Mendis322591c2009-10-29 11:06:59 -07006222 boolean screenIsOff = !mPowerManager.isScreenOn();
6223 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006224 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006226 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6227 mPowerManager.goToSleep(event.when);
6228 }
6229
6230 if (screenIsOff) {
6231 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6232 }
6233 if (screenIsDim) {
6234 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6235 }
6236 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6237 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006238 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 }
Romain Guy06882f82009-06-10 13:36:04 -07006240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006241 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6242 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6243 filterQueue(this);
6244 mKeyWaiter.appSwitchComing();
6245 }
6246 return true;
6247 } else {
6248 return false;
6249 }
6250 }
Romain Guy06882f82009-06-10 13:36:04 -07006251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006253 boolean screenIsOff = !mPowerManager.isScreenOn();
6254 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 if (screenIsOff) {
6256 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6257 device.classes, event)) {
6258 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6259 return false;
6260 }
6261 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6262 }
6263 if (screenIsDim) {
6264 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6265 }
6266 return true;
6267 }
Romain Guy06882f82009-06-10 13:36:04 -07006268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006270 boolean screenIsOff = !mPowerManager.isScreenOn();
6271 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 if (screenIsOff) {
6273 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6274 device.classes, event)) {
6275 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6276 return false;
6277 }
6278 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6279 }
6280 if (screenIsDim) {
6281 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6282 }
6283 return true;
6284 }
Romain Guy06882f82009-06-10 13:36:04 -07006285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 default:
6287 return true;
6288 }
6289 }
6290
6291 public int filterEvent(QueuedEvent ev) {
6292 switch (ev.classType) {
6293 case RawInputEvent.CLASS_KEYBOARD:
6294 KeyEvent ke = (KeyEvent)ev.event;
6295 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6296 Log.w(TAG, "Dropping movement key during app switch: "
6297 + ke.getKeyCode() + ", action=" + ke.getAction());
6298 return FILTER_REMOVE;
6299 }
6300 return FILTER_ABORT;
6301 default:
6302 return FILTER_KEEP;
6303 }
6304 }
Romain Guy06882f82009-06-10 13:36:04 -07006305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006306 /**
6307 * Must be called with the main window manager lock held.
6308 */
6309 void setHoldScreenLocked(boolean holding) {
6310 boolean state = mHoldingScreen.isHeld();
6311 if (holding != state) {
6312 if (holding) {
6313 mHoldingScreen.acquire();
6314 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006315 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 mHoldingScreen.release();
6317 }
6318 }
6319 }
Michael Chan53071d62009-05-13 17:29:48 -07006320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006321
6322 public boolean detectSafeMode() {
6323 mSafeMode = mPolicy.detectSafeMode();
6324 return mSafeMode;
6325 }
Romain Guy06882f82009-06-10 13:36:04 -07006326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 public void systemReady() {
6328 mPolicy.systemReady();
6329 }
Romain Guy06882f82009-06-10 13:36:04 -07006330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 private final class InputDispatcherThread extends Thread {
6332 // Time to wait when there is nothing to do: 9999 seconds.
6333 static final int LONG_WAIT=9999*1000;
6334
6335 public InputDispatcherThread() {
6336 super("InputDispatcher");
6337 }
Romain Guy06882f82009-06-10 13:36:04 -07006338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 @Override
6340 public void run() {
6341 while (true) {
6342 try {
6343 process();
6344 } catch (Exception e) {
6345 Log.e(TAG, "Exception in input dispatcher", e);
6346 }
6347 }
6348 }
Romain Guy06882f82009-06-10 13:36:04 -07006349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 private void process() {
6351 android.os.Process.setThreadPriority(
6352 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006354 // The last key event we saw
6355 KeyEvent lastKey = null;
6356
6357 // Last keydown time for auto-repeating keys
6358 long lastKeyTime = SystemClock.uptimeMillis();
6359 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006360 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361
Romain Guy06882f82009-06-10 13:36:04 -07006362 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006363 int keyRepeatCount = 0;
6364
6365 // Need to report that configuration has changed?
6366 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 while (true) {
6369 long curTime = SystemClock.uptimeMillis();
6370
6371 if (DEBUG_INPUT) Log.v(
6372 TAG, "Waiting for next key: now=" + curTime
6373 + ", repeat @ " + nextKeyTime);
6374
6375 // Retrieve next event, waiting only as long as the next
6376 // repeat timeout. If the configuration has changed, then
6377 // don't wait at all -- we'll report the change as soon as
6378 // we have processed all events.
6379 QueuedEvent ev = mQueue.getEvent(
6380 (int)((!configChanged && curTime < nextKeyTime)
6381 ? (nextKeyTime-curTime) : 0));
6382
6383 if (DEBUG_INPUT && ev != null) Log.v(
6384 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6385
Michael Chan53071d62009-05-13 17:29:48 -07006386 if (MEASURE_LATENCY) {
6387 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6388 }
6389
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006390 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6391 // cancel key repeat at the request of the policy.
6392 lastKey = null;
6393 downTime = 0;
6394 lastKeyTime = curTime;
6395 nextKeyTime = curTime + LONG_WAIT;
6396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006397 try {
6398 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006399 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006400 int eventType;
6401 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6402 eventType = eventType((MotionEvent)ev.event);
6403 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6404 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6405 eventType = LocalPowerManager.BUTTON_EVENT;
6406 } else {
6407 eventType = LocalPowerManager.OTHER_EVENT;
6408 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006409 try {
Michael Chan53071d62009-05-13 17:29:48 -07006410 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006411 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006412 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006413 mBatteryStats.noteInputEvent();
6414 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006415 } catch (RemoteException e) {
6416 // Ignore
6417 }
Michael Chane10de972009-05-18 11:24:50 -07006418
Mike Lockwood5db42402009-11-30 14:51:51 -05006419 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6420 // do not wake screen in this case
6421 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006422 && eventType != LONG_TOUCH_EVENT
6423 && eventType != CHEEK_EVENT) {
6424 mPowerManager.userActivity(curTime, false,
6425 eventType, false);
6426 } else if (mLastTouchEventType != eventType
6427 || (curTime - mLastUserActivityCallTime)
6428 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6429 mLastUserActivityCallTime = curTime;
6430 mLastTouchEventType = eventType;
6431 mPowerManager.userActivity(curTime, false,
6432 eventType, false);
6433 }
6434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006435 switch (ev.classType) {
6436 case RawInputEvent.CLASS_KEYBOARD:
6437 KeyEvent ke = (KeyEvent)ev.event;
6438 if (ke.isDown()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006439 lastKeyTime = curTime;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006440 if (lastKey != null &&
6441 ke.getKeyCode() == lastKey.getKeyCode()) {
6442 keyRepeatCount++;
6443 // Arbitrary long timeout to block
6444 // repeating here since we know that
6445 // the device driver takes care of it.
6446 nextKeyTime = lastKeyTime + LONG_WAIT;
6447 if (DEBUG_INPUT) Log.v(
6448 TAG, "Received repeated key down");
6449 } else {
6450 downTime = curTime;
6451 keyRepeatCount = 0;
6452 nextKeyTime = lastKeyTime
6453 + ViewConfiguration.getLongPressTimeout();
6454 if (DEBUG_INPUT) Log.v(
6455 TAG, "Received key down: first repeat @ "
6456 + nextKeyTime);
6457 }
6458 lastKey = ke;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 } else {
6460 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006461 downTime = 0;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006462 keyRepeatCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 // Arbitrary long timeout.
6464 lastKeyTime = curTime;
6465 nextKeyTime = curTime + LONG_WAIT;
6466 if (DEBUG_INPUT) Log.v(
6467 TAG, "Received key up: ignore repeat @ "
6468 + nextKeyTime);
6469 }
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006470 if (keyRepeatCount > 0) {
6471 dispatchKey(KeyEvent.changeTimeRepeat(ke,
6472 ke.getEventTime(), keyRepeatCount), 0, 0);
6473 } else {
6474 dispatchKey(ke, 0, 0);
6475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006476 mQueue.recycleEvent(ev);
6477 break;
6478 case RawInputEvent.CLASS_TOUCHSCREEN:
6479 //Log.i(TAG, "Read next event " + ev);
6480 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6481 break;
6482 case RawInputEvent.CLASS_TRACKBALL:
6483 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6484 break;
6485 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6486 configChanged = true;
6487 break;
6488 default:
6489 mQueue.recycleEvent(ev);
6490 break;
6491 }
Romain Guy06882f82009-06-10 13:36:04 -07006492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 } else if (configChanged) {
6494 configChanged = false;
6495 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006497 } else if (lastKey != null) {
6498 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500 // Timeout occurred while key was down. If it is at or
6501 // past the key repeat time, dispatch the repeat.
6502 if (DEBUG_INPUT) Log.v(
6503 TAG, "Key timeout: repeat=" + nextKeyTime
6504 + ", now=" + curTime);
6505 if (curTime < nextKeyTime) {
6506 continue;
6507 }
Romain Guy06882f82009-06-10 13:36:04 -07006508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 lastKeyTime = nextKeyTime;
6510 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6511 keyRepeatCount++;
6512 if (DEBUG_INPUT) Log.v(
6513 TAG, "Key repeat: count=" + keyRepeatCount
6514 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006515 KeyEvent newEvent;
6516 if (downTime != 0 && (downTime
6517 + ViewConfiguration.getLongPressTimeout())
6518 <= curTime) {
6519 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6520 curTime, keyRepeatCount,
6521 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6522 downTime = 0;
6523 } else {
6524 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6525 curTime, keyRepeatCount);
6526 }
6527 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006529 } else {
6530 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006532 lastKeyTime = curTime;
6533 nextKeyTime = curTime + LONG_WAIT;
6534 }
Romain Guy06882f82009-06-10 13:36:04 -07006535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006536 } catch (Exception e) {
6537 Log.e(TAG,
6538 "Input thread received uncaught exception: " + e, e);
6539 }
6540 }
6541 }
6542 }
6543
6544 // -------------------------------------------------------------
6545 // Client Session State
6546 // -------------------------------------------------------------
6547
6548 private final class Session extends IWindowSession.Stub
6549 implements IBinder.DeathRecipient {
6550 final IInputMethodClient mClient;
6551 final IInputContext mInputContext;
6552 final int mUid;
6553 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006554 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 SurfaceSession mSurfaceSession;
6556 int mNumWindow = 0;
6557 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 /**
6560 * Current pointer move event being dispatched to client window... must
6561 * hold key lock to access.
6562 */
6563 QueuedEvent mPendingPointerMove;
6564 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006566 /**
6567 * Current trackball move event being dispatched to client window... must
6568 * hold key lock to access.
6569 */
6570 QueuedEvent mPendingTrackballMove;
6571 WindowState mPendingTrackballWindow;
6572
6573 public Session(IInputMethodClient client, IInputContext inputContext) {
6574 mClient = client;
6575 mInputContext = inputContext;
6576 mUid = Binder.getCallingUid();
6577 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006578 StringBuilder sb = new StringBuilder();
6579 sb.append("Session{");
6580 sb.append(Integer.toHexString(System.identityHashCode(this)));
6581 sb.append(" uid ");
6582 sb.append(mUid);
6583 sb.append("}");
6584 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 synchronized (mWindowMap) {
6587 if (mInputMethodManager == null && mHaveInputMethods) {
6588 IBinder b = ServiceManager.getService(
6589 Context.INPUT_METHOD_SERVICE);
6590 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6591 }
6592 }
6593 long ident = Binder.clearCallingIdentity();
6594 try {
6595 // Note: it is safe to call in to the input method manager
6596 // here because we are not holding our lock.
6597 if (mInputMethodManager != null) {
6598 mInputMethodManager.addClient(client, inputContext,
6599 mUid, mPid);
6600 } else {
6601 client.setUsingInputMethod(false);
6602 }
6603 client.asBinder().linkToDeath(this, 0);
6604 } catch (RemoteException e) {
6605 // The caller has died, so we can just forget about this.
6606 try {
6607 if (mInputMethodManager != null) {
6608 mInputMethodManager.removeClient(client);
6609 }
6610 } catch (RemoteException ee) {
6611 }
6612 } finally {
6613 Binder.restoreCallingIdentity(ident);
6614 }
6615 }
Romain Guy06882f82009-06-10 13:36:04 -07006616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006617 @Override
6618 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6619 throws RemoteException {
6620 try {
6621 return super.onTransact(code, data, reply, flags);
6622 } catch (RuntimeException e) {
6623 // Log all 'real' exceptions thrown to the caller
6624 if (!(e instanceof SecurityException)) {
6625 Log.e(TAG, "Window Session Crash", e);
6626 }
6627 throw e;
6628 }
6629 }
6630
6631 public void binderDied() {
6632 // Note: it is safe to call in to the input method manager
6633 // here because we are not holding our lock.
6634 try {
6635 if (mInputMethodManager != null) {
6636 mInputMethodManager.removeClient(mClient);
6637 }
6638 } catch (RemoteException e) {
6639 }
6640 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006641 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006642 mClientDead = true;
6643 killSessionLocked();
6644 }
6645 }
6646
6647 public int add(IWindow window, WindowManager.LayoutParams attrs,
6648 int viewVisibility, Rect outContentInsets) {
6649 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6650 }
Romain Guy06882f82009-06-10 13:36:04 -07006651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 public void remove(IWindow window) {
6653 removeWindow(this, window);
6654 }
Romain Guy06882f82009-06-10 13:36:04 -07006655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006656 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6657 int requestedWidth, int requestedHeight, int viewFlags,
6658 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6659 Rect outVisibleInsets, Surface outSurface) {
6660 return relayoutWindow(this, window, attrs,
6661 requestedWidth, requestedHeight, viewFlags, insetsPending,
6662 outFrame, outContentInsets, outVisibleInsets, outSurface);
6663 }
Romain Guy06882f82009-06-10 13:36:04 -07006664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006665 public void setTransparentRegion(IWindow window, Region region) {
6666 setTransparentRegionWindow(this, window, region);
6667 }
Romain Guy06882f82009-06-10 13:36:04 -07006668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669 public void setInsets(IWindow window, int touchableInsets,
6670 Rect contentInsets, Rect visibleInsets) {
6671 setInsetsWindow(this, window, touchableInsets, contentInsets,
6672 visibleInsets);
6673 }
Romain Guy06882f82009-06-10 13:36:04 -07006674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006675 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6676 getWindowDisplayFrame(this, window, outDisplayFrame);
6677 }
Romain Guy06882f82009-06-10 13:36:04 -07006678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 public void finishDrawing(IWindow window) {
6680 if (localLOGV) Log.v(
6681 TAG, "IWindow finishDrawing called for " + window);
6682 finishDrawingWindow(this, window);
6683 }
6684
6685 public void finishKey(IWindow window) {
6686 if (localLOGV) Log.v(
6687 TAG, "IWindow finishKey called for " + window);
6688 mKeyWaiter.finishedKey(this, window, false,
6689 KeyWaiter.RETURN_NOTHING);
6690 }
6691
6692 public MotionEvent getPendingPointerMove(IWindow window) {
6693 if (localLOGV) Log.v(
6694 TAG, "IWindow getPendingMotionEvent called for " + window);
6695 return mKeyWaiter.finishedKey(this, window, false,
6696 KeyWaiter.RETURN_PENDING_POINTER);
6697 }
Romain Guy06882f82009-06-10 13:36:04 -07006698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006699 public MotionEvent getPendingTrackballMove(IWindow window) {
6700 if (localLOGV) Log.v(
6701 TAG, "IWindow getPendingMotionEvent called for " + window);
6702 return mKeyWaiter.finishedKey(this, window, false,
6703 KeyWaiter.RETURN_PENDING_TRACKBALL);
6704 }
6705
6706 public void setInTouchMode(boolean mode) {
6707 synchronized(mWindowMap) {
6708 mInTouchMode = mode;
6709 }
6710 }
6711
6712 public boolean getInTouchMode() {
6713 synchronized(mWindowMap) {
6714 return mInTouchMode;
6715 }
6716 }
6717
6718 public boolean performHapticFeedback(IWindow window, int effectId,
6719 boolean always) {
6720 synchronized(mWindowMap) {
6721 long ident = Binder.clearCallingIdentity();
6722 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006723 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006724 windowForClientLocked(this, window), effectId, always);
6725 } finally {
6726 Binder.restoreCallingIdentity(ident);
6727 }
6728 }
6729 }
Romain Guy06882f82009-06-10 13:36:04 -07006730
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006731 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006732 synchronized(mWindowMap) {
6733 long ident = Binder.clearCallingIdentity();
6734 try {
6735 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006736 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006737 } finally {
6738 Binder.restoreCallingIdentity(ident);
6739 }
6740 }
6741 }
6742
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006743 public void wallpaperOffsetsComplete(IBinder window) {
6744 WindowManagerService.this.wallpaperOffsetsComplete(window);
6745 }
6746
Dianne Hackborn75804932009-10-20 20:15:20 -07006747 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6748 int z, Bundle extras, boolean sync) {
6749 synchronized(mWindowMap) {
6750 long ident = Binder.clearCallingIdentity();
6751 try {
6752 return sendWindowWallpaperCommandLocked(
6753 windowForClientLocked(this, window),
6754 action, x, y, z, extras, sync);
6755 } finally {
6756 Binder.restoreCallingIdentity(ident);
6757 }
6758 }
6759 }
6760
6761 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6762 WindowManagerService.this.wallpaperCommandComplete(window, result);
6763 }
6764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 void windowAddedLocked() {
6766 if (mSurfaceSession == null) {
6767 if (localLOGV) Log.v(
6768 TAG, "First window added to " + this + ", creating SurfaceSession");
6769 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006770 if (SHOW_TRANSACTIONS) Log.i(
6771 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006772 mSessions.add(this);
6773 }
6774 mNumWindow++;
6775 }
6776
6777 void windowRemovedLocked() {
6778 mNumWindow--;
6779 killSessionLocked();
6780 }
Romain Guy06882f82009-06-10 13:36:04 -07006781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006782 void killSessionLocked() {
6783 if (mNumWindow <= 0 && mClientDead) {
6784 mSessions.remove(this);
6785 if (mSurfaceSession != null) {
6786 if (localLOGV) Log.v(
6787 TAG, "Last window removed from " + this
6788 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006789 if (SHOW_TRANSACTIONS) Log.i(
6790 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006791 try {
6792 mSurfaceSession.kill();
6793 } catch (Exception e) {
6794 Log.w(TAG, "Exception thrown when killing surface session "
6795 + mSurfaceSession + " in session " + this
6796 + ": " + e.toString());
6797 }
6798 mSurfaceSession = null;
6799 }
6800 }
6801 }
Romain Guy06882f82009-06-10 13:36:04 -07006802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006804 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6805 pw.print(" mClientDead="); pw.print(mClientDead);
6806 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6807 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6808 pw.print(prefix);
6809 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6810 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6811 }
6812 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6813 pw.print(prefix);
6814 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6815 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006817 }
6818
6819 @Override
6820 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006821 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 }
6823 }
6824
6825 // -------------------------------------------------------------
6826 // Client Window State
6827 // -------------------------------------------------------------
6828
6829 private final class WindowState implements WindowManagerPolicy.WindowState {
6830 final Session mSession;
6831 final IWindow mClient;
6832 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006833 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834 AppWindowToken mAppToken;
6835 AppWindowToken mTargetAppToken;
6836 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6837 final DeathRecipient mDeathRecipient;
6838 final WindowState mAttachedWindow;
6839 final ArrayList mChildWindows = new ArrayList();
6840 final int mBaseLayer;
6841 final int mSubLayer;
6842 final boolean mLayoutAttached;
6843 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006844 final boolean mIsWallpaper;
6845 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846 int mViewVisibility;
6847 boolean mPolicyVisibility = true;
6848 boolean mPolicyVisibilityAfterAnim = true;
6849 boolean mAppFreezing;
6850 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006851 boolean mReportDestroySurface;
6852 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853 boolean mAttachedHidden; // is our parent window hidden?
6854 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006855 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006856 int mRequestedWidth;
6857 int mRequestedHeight;
6858 int mLastRequestedWidth;
6859 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006860 int mLayer;
6861 int mAnimLayer;
6862 int mLastLayer;
6863 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006864 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006865 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866
6867 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869 // Actual frame shown on-screen (may be modified by animation)
6870 final Rect mShownFrame = new Rect();
6871 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 /**
6874 * Insets that determine the actually visible area
6875 */
6876 final Rect mVisibleInsets = new Rect();
6877 final Rect mLastVisibleInsets = new Rect();
6878 boolean mVisibleInsetsChanged;
6879
6880 /**
6881 * Insets that are covered by system windows
6882 */
6883 final Rect mContentInsets = new Rect();
6884 final Rect mLastContentInsets = new Rect();
6885 boolean mContentInsetsChanged;
6886
6887 /**
6888 * Set to true if we are waiting for this window to receive its
6889 * given internal insets before laying out other windows based on it.
6890 */
6891 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 /**
6894 * These are the content insets that were given during layout for
6895 * this window, to be applied to windows behind it.
6896 */
6897 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899 /**
6900 * These are the visible insets that were given during layout for
6901 * this window, to be applied to windows behind it.
6902 */
6903 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006905 /**
6906 * Flag indicating whether the touchable region should be adjusted by
6907 * the visible insets; if false the area outside the visible insets is
6908 * NOT touchable, so we must use those to adjust the frame during hit
6909 * tests.
6910 */
6911 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006913 // Current transformation being applied.
6914 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6915 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6916 float mHScale=1, mVScale=1;
6917 float mLastHScale=1, mLastVScale=1;
6918 final Matrix mTmpMatrix = new Matrix();
6919
6920 // "Real" frame that the application sees.
6921 final Rect mFrame = new Rect();
6922 final Rect mLastFrame = new Rect();
6923
6924 final Rect mContainingFrame = new Rect();
6925 final Rect mDisplayFrame = new Rect();
6926 final Rect mContentFrame = new Rect();
6927 final Rect mVisibleFrame = new Rect();
6928
6929 float mShownAlpha = 1;
6930 float mAlpha = 1;
6931 float mLastAlpha = 1;
6932
6933 // Set to true if, when the window gets displayed, it should perform
6934 // an enter animation.
6935 boolean mEnterAnimationPending;
6936
6937 // Currently running animation.
6938 boolean mAnimating;
6939 boolean mLocalAnimating;
6940 Animation mAnimation;
6941 boolean mAnimationIsEntrance;
6942 boolean mHasTransformation;
6943 boolean mHasLocalTransformation;
6944 final Transformation mTransformation = new Transformation();
6945
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006946 // If a window showing a wallpaper: the requested offset for the
6947 // wallpaper; if a wallpaper window: the currently applied offset.
6948 float mWallpaperX = -1;
6949 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006950
6951 // If a window showing a wallpaper: what fraction of the offset
6952 // range corresponds to a full virtual screen.
6953 float mWallpaperXStep = -1;
6954 float mWallpaperYStep = -1;
6955
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006956 // Wallpaper windows: pixels offset based on above variables.
6957 int mXOffset;
6958 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006960 // This is set after IWindowSession.relayout() has been called at
6961 // least once for the window. It allows us to detect the situation
6962 // where we don't yet have a surface, but should have one soon, so
6963 // we can give the window focus before waiting for the relayout.
6964 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006966 // This is set after the Surface has been created but before the
6967 // window has been drawn. During this time the surface is hidden.
6968 boolean mDrawPending;
6969
6970 // This is set after the window has finished drawing for the first
6971 // time but before its surface is shown. The surface will be
6972 // displayed when the next layout is run.
6973 boolean mCommitDrawPending;
6974
6975 // This is set during the time after the window's drawing has been
6976 // committed, and before its surface is actually shown. It is used
6977 // to delay showing the surface until all windows in a token are ready
6978 // to be shown.
6979 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 // Set when the window has been shown in the screen the first time.
6982 boolean mHasDrawn;
6983
6984 // Currently running an exit animation?
6985 boolean mExiting;
6986
6987 // Currently on the mDestroySurface list?
6988 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006990 // Completely remove from window manager after exit animation?
6991 boolean mRemoveOnExit;
6992
6993 // Set when the orientation is changing and this window has not yet
6994 // been updated for the new orientation.
6995 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006997 // Is this window now (or just being) removed?
6998 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007000 WindowState(Session s, IWindow c, WindowToken token,
7001 WindowState attachedWindow, WindowManager.LayoutParams a,
7002 int viewVisibility) {
7003 mSession = s;
7004 mClient = c;
7005 mToken = token;
7006 mAttrs.copyFrom(a);
7007 mViewVisibility = viewVisibility;
7008 DeathRecipient deathRecipient = new DeathRecipient();
7009 mAlpha = a.alpha;
7010 if (localLOGV) Log.v(
7011 TAG, "Window " + this + " client=" + c.asBinder()
7012 + " token=" + token + " (" + mAttrs.token + ")");
7013 try {
7014 c.asBinder().linkToDeath(deathRecipient, 0);
7015 } catch (RemoteException e) {
7016 mDeathRecipient = null;
7017 mAttachedWindow = null;
7018 mLayoutAttached = false;
7019 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007020 mIsWallpaper = false;
7021 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007022 mBaseLayer = 0;
7023 mSubLayer = 0;
7024 return;
7025 }
7026 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007028 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7029 mAttrs.type <= LAST_SUB_WINDOW)) {
7030 // The multiplier here is to reserve space for multiple
7031 // windows in the same type layer.
7032 mBaseLayer = mPolicy.windowTypeToLayerLw(
7033 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7034 + TYPE_LAYER_OFFSET;
7035 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7036 mAttachedWindow = attachedWindow;
7037 mAttachedWindow.mChildWindows.add(this);
7038 mLayoutAttached = mAttrs.type !=
7039 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7040 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7041 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007042 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7043 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007044 } else {
7045 // The multiplier here is to reserve space for multiple
7046 // windows in the same type layer.
7047 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7048 * TYPE_LAYER_MULTIPLIER
7049 + TYPE_LAYER_OFFSET;
7050 mSubLayer = 0;
7051 mAttachedWindow = null;
7052 mLayoutAttached = false;
7053 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7054 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007055 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7056 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007057 }
7058
7059 WindowState appWin = this;
7060 while (appWin.mAttachedWindow != null) {
7061 appWin = mAttachedWindow;
7062 }
7063 WindowToken appToken = appWin.mToken;
7064 while (appToken.appWindowToken == null) {
7065 WindowToken parent = mTokenMap.get(appToken.token);
7066 if (parent == null || appToken == parent) {
7067 break;
7068 }
7069 appToken = parent;
7070 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007071 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007072 mAppToken = appToken.appWindowToken;
7073
7074 mSurface = null;
7075 mRequestedWidth = 0;
7076 mRequestedHeight = 0;
7077 mLastRequestedWidth = 0;
7078 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007079 mXOffset = 0;
7080 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081 mLayer = 0;
7082 mAnimLayer = 0;
7083 mLastLayer = 0;
7084 }
7085
7086 void attach() {
7087 if (localLOGV) Log.v(
7088 TAG, "Attaching " + this + " token=" + mToken
7089 + ", list=" + mToken.windows);
7090 mSession.windowAddedLocked();
7091 }
7092
7093 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7094 mHaveFrame = true;
7095
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007096 final Rect container = mContainingFrame;
7097 container.set(pf);
7098
7099 final Rect display = mDisplayFrame;
7100 display.set(df);
7101
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007102 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007103 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007104 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7105 display.intersect(mCompatibleScreenFrame);
7106 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007107 }
7108
7109 final int pw = container.right - container.left;
7110 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111
7112 int w,h;
7113 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7114 w = mAttrs.width < 0 ? pw : mAttrs.width;
7115 h = mAttrs.height< 0 ? ph : mAttrs.height;
7116 } else {
7117 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
7118 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
7119 }
Romain Guy06882f82009-06-10 13:36:04 -07007120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 final Rect content = mContentFrame;
7122 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007124 final Rect visible = mVisibleFrame;
7125 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007128 final int fw = frame.width();
7129 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007131 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7132 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7133
7134 Gravity.apply(mAttrs.gravity, w, h, container,
7135 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7136 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7137
7138 //System.out.println("Out: " + mFrame);
7139
7140 // Now make sure the window fits in the overall display.
7141 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007143 // Make sure the content and visible frames are inside of the
7144 // final window frame.
7145 if (content.left < frame.left) content.left = frame.left;
7146 if (content.top < frame.top) content.top = frame.top;
7147 if (content.right > frame.right) content.right = frame.right;
7148 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7149 if (visible.left < frame.left) visible.left = frame.left;
7150 if (visible.top < frame.top) visible.top = frame.top;
7151 if (visible.right > frame.right) visible.right = frame.right;
7152 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007154 final Rect contentInsets = mContentInsets;
7155 contentInsets.left = content.left-frame.left;
7156 contentInsets.top = content.top-frame.top;
7157 contentInsets.right = frame.right-content.right;
7158 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007160 final Rect visibleInsets = mVisibleInsets;
7161 visibleInsets.left = visible.left-frame.left;
7162 visibleInsets.top = visible.top-frame.top;
7163 visibleInsets.right = frame.right-visible.right;
7164 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007165
Dianne Hackborn284ac932009-08-28 10:34:25 -07007166 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7167 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007168 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007169 }
7170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007171 if (localLOGV) {
7172 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7173 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7174 Log.v(TAG, "Resolving (mRequestedWidth="
7175 + mRequestedWidth + ", mRequestedheight="
7176 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7177 + "): frame=" + mFrame.toShortString()
7178 + " ci=" + contentInsets.toShortString()
7179 + " vi=" + visibleInsets.toShortString());
7180 //}
7181 }
7182 }
Romain Guy06882f82009-06-10 13:36:04 -07007183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007184 public Rect getFrameLw() {
7185 return mFrame;
7186 }
7187
7188 public Rect getShownFrameLw() {
7189 return mShownFrame;
7190 }
7191
7192 public Rect getDisplayFrameLw() {
7193 return mDisplayFrame;
7194 }
7195
7196 public Rect getContentFrameLw() {
7197 return mContentFrame;
7198 }
7199
7200 public Rect getVisibleFrameLw() {
7201 return mVisibleFrame;
7202 }
7203
7204 public boolean getGivenInsetsPendingLw() {
7205 return mGivenInsetsPending;
7206 }
7207
7208 public Rect getGivenContentInsetsLw() {
7209 return mGivenContentInsets;
7210 }
Romain Guy06882f82009-06-10 13:36:04 -07007211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007212 public Rect getGivenVisibleInsetsLw() {
7213 return mGivenVisibleInsets;
7214 }
Romain Guy06882f82009-06-10 13:36:04 -07007215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 public WindowManager.LayoutParams getAttrs() {
7217 return mAttrs;
7218 }
7219
7220 public int getSurfaceLayer() {
7221 return mLayer;
7222 }
Romain Guy06882f82009-06-10 13:36:04 -07007223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 public IApplicationToken getAppToken() {
7225 return mAppToken != null ? mAppToken.appToken : null;
7226 }
7227
7228 public boolean hasAppShownWindows() {
7229 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7230 }
7231
7232 public boolean hasAppStartingIcon() {
7233 return mAppToken != null ? (mAppToken.startingData != null) : false;
7234 }
7235
7236 public WindowManagerPolicy.WindowState getAppStartingWindow() {
7237 return mAppToken != null ? mAppToken.startingWindow : null;
7238 }
7239
7240 public void setAnimation(Animation anim) {
7241 if (localLOGV) Log.v(
7242 TAG, "Setting animation in " + this + ": " + anim);
7243 mAnimating = false;
7244 mLocalAnimating = false;
7245 mAnimation = anim;
7246 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7247 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7248 }
7249
7250 public void clearAnimation() {
7251 if (mAnimation != null) {
7252 mAnimating = true;
7253 mLocalAnimating = false;
7254 mAnimation = null;
7255 }
7256 }
Romain Guy06882f82009-06-10 13:36:04 -07007257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007258 Surface createSurfaceLocked() {
7259 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007260 mReportDestroySurface = false;
7261 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007262 mDrawPending = true;
7263 mCommitDrawPending = false;
7264 mReadyToShow = false;
7265 if (mAppToken != null) {
7266 mAppToken.allDrawn = false;
7267 }
7268
7269 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007270 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007271 flags |= Surface.PUSH_BUFFERS;
7272 }
7273
7274 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7275 flags |= Surface.SECURE;
7276 }
7277 if (DEBUG_VISIBILITY) Log.v(
7278 TAG, "Creating surface in session "
7279 + mSession.mSurfaceSession + " window " + this
7280 + " w=" + mFrame.width()
7281 + " h=" + mFrame.height() + " format="
7282 + mAttrs.format + " flags=" + flags);
7283
7284 int w = mFrame.width();
7285 int h = mFrame.height();
7286 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7287 // for a scaled surface, we always want the requested
7288 // size.
7289 w = mRequestedWidth;
7290 h = mRequestedHeight;
7291 }
7292
Romain Guy9825ec62009-10-01 00:58:09 -07007293 // Something is wrong and SurfaceFlinger will not like this,
7294 // try to revert to sane values
7295 if (w <= 0) w = 1;
7296 if (h <= 0) h = 1;
7297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007298 try {
7299 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007300 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007301 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007302 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7303 + mSurface + " IN SESSION "
7304 + mSession.mSurfaceSession
7305 + ": pid=" + mSession.mPid + " format="
7306 + mAttrs.format + " flags=0x"
7307 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007308 } catch (Surface.OutOfResourcesException e) {
7309 Log.w(TAG, "OutOfResourcesException creating surface");
7310 reclaimSomeSurfaceMemoryLocked(this, "create");
7311 return null;
7312 } catch (Exception e) {
7313 Log.e(TAG, "Exception creating surface", e);
7314 return null;
7315 }
Romain Guy06882f82009-06-10 13:36:04 -07007316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 if (localLOGV) Log.v(
7318 TAG, "Got surface: " + mSurface
7319 + ", set left=" + mFrame.left + " top=" + mFrame.top
7320 + ", animLayer=" + mAnimLayer);
7321 if (SHOW_TRANSACTIONS) {
7322 Log.i(TAG, ">>> OPEN TRANSACTION");
7323 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7324 + mAttrs.getTitle() + ") pos=(" +
7325 mFrame.left + "," + mFrame.top + ") (" +
7326 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7327 mAnimLayer + " HIDE");
7328 }
7329 Surface.openTransaction();
7330 try {
7331 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007332 mSurface.setPosition(mFrame.left + mXOffset,
7333 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007334 mSurface.setLayer(mAnimLayer);
7335 mSurface.hide();
7336 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007337 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7338 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007339 mSurface.setFlags(Surface.SURFACE_DITHER,
7340 Surface.SURFACE_DITHER);
7341 }
7342 } catch (RuntimeException e) {
7343 Log.w(TAG, "Error creating surface in " + w, e);
7344 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7345 }
7346 mLastHidden = true;
7347 } finally {
7348 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7349 Surface.closeTransaction();
7350 }
7351 if (localLOGV) Log.v(
7352 TAG, "Created surface " + this);
7353 }
7354 return mSurface;
7355 }
Romain Guy06882f82009-06-10 13:36:04 -07007356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 void destroySurfaceLocked() {
7358 // Window is no longer on-screen, so can no longer receive
7359 // key events... if we were waiting for it to finish
7360 // handling a key event, the wait is over!
7361 mKeyWaiter.finishedKey(mSession, mClient, true,
7362 KeyWaiter.RETURN_NOTHING);
7363 mKeyWaiter.releasePendingPointerLocked(mSession);
7364 mKeyWaiter.releasePendingTrackballLocked(mSession);
7365
7366 if (mAppToken != null && this == mAppToken.startingWindow) {
7367 mAppToken.startingDisplayed = false;
7368 }
Romain Guy06882f82009-06-10 13:36:04 -07007369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007371 mDrawPending = false;
7372 mCommitDrawPending = false;
7373 mReadyToShow = false;
7374
7375 int i = mChildWindows.size();
7376 while (i > 0) {
7377 i--;
7378 WindowState c = (WindowState)mChildWindows.get(i);
7379 c.mAttachedHidden = true;
7380 }
7381
7382 if (mReportDestroySurface) {
7383 mReportDestroySurface = false;
7384 mSurfacePendingDestroy = true;
7385 try {
7386 mClient.dispatchGetNewSurface();
7387 // We'll really destroy on the next time around.
7388 return;
7389 } catch (RemoteException e) {
7390 }
7391 }
7392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007393 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007394 if (DEBUG_VISIBILITY) {
7395 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007396 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007397 Log.w(TAG, "Window " + this + " destroying surface "
7398 + mSurface + ", session " + mSession, e);
7399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007400 if (SHOW_TRANSACTIONS) {
7401 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007402 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7404 + mAttrs.getTitle() + ")", ex);
7405 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007406 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007407 } catch (RuntimeException e) {
7408 Log.w(TAG, "Exception thrown when destroying Window " + this
7409 + " surface " + mSurface + " session " + mSession
7410 + ": " + e.toString());
7411 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007413 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 }
7415 }
7416
7417 boolean finishDrawingLocked() {
7418 if (mDrawPending) {
7419 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7420 TAG, "finishDrawingLocked: " + mSurface);
7421 mCommitDrawPending = true;
7422 mDrawPending = false;
7423 return true;
7424 }
7425 return false;
7426 }
7427
7428 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007429 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007430 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7431 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007432 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007433 }
7434 mCommitDrawPending = false;
7435 mReadyToShow = true;
7436 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7437 final AppWindowToken atoken = mAppToken;
7438 if (atoken == null || atoken.allDrawn || starting) {
7439 performShowLocked();
7440 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007441 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007442 }
7443
7444 // This must be called while inside a transaction.
7445 boolean performShowLocked() {
7446 if (DEBUG_VISIBILITY) {
7447 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007448 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007449 Log.v(TAG, "performShow on " + this
7450 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7451 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7452 }
7453 if (mReadyToShow && isReadyForDisplay()) {
7454 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7455 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7456 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7457 + " during animation: policyVis=" + mPolicyVisibility
7458 + " attHidden=" + mAttachedHidden
7459 + " tok.hiddenRequested="
7460 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007461 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 + (mAppToken != null ? mAppToken.hidden : false)
7463 + " animating=" + mAnimating
7464 + " tok animating="
7465 + (mAppToken != null ? mAppToken.animating : false));
7466 if (!showSurfaceRobustlyLocked(this)) {
7467 return false;
7468 }
7469 mLastAlpha = -1;
7470 mHasDrawn = true;
7471 mLastHidden = false;
7472 mReadyToShow = false;
7473 enableScreenIfNeededLocked();
7474
7475 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007477 int i = mChildWindows.size();
7478 while (i > 0) {
7479 i--;
7480 WindowState c = (WindowState)mChildWindows.get(i);
7481 if (c.mSurface != null && c.mAttachedHidden) {
7482 c.mAttachedHidden = false;
7483 c.performShowLocked();
Dianne Hackborn9b52a212009-12-11 14:51:35 -08007484 // It hadn't been shown, which means layout not
7485 // performed on it, so now we want to make sure to
7486 // do a layout. If called from within the transaction
7487 // loop, this will cause it to restart with a new
7488 // layout.
7489 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007490 }
7491 }
Romain Guy06882f82009-06-10 13:36:04 -07007492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007493 if (mAttrs.type != TYPE_APPLICATION_STARTING
7494 && mAppToken != null) {
7495 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007496
7497 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007498 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7499 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007500 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007501 // If this initial window is animating, stop it -- we
7502 // will do an animation to reveal it from behind the
7503 // starting window, so there is no need for it to also
7504 // be doing its own stuff.
7505 if (mAnimation != null) {
7506 mAnimation = null;
7507 // Make sure we clean up the animation.
7508 mAnimating = true;
7509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007510 mFinishedStarting.add(mAppToken);
7511 mH.sendEmptyMessage(H.FINISHED_STARTING);
7512 }
7513 mAppToken.updateReportedVisibilityLocked();
7514 }
7515 }
7516 return true;
7517 }
Romain Guy06882f82009-06-10 13:36:04 -07007518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519 // This must be called while inside a transaction. Returns true if
7520 // there is more animation to run.
7521 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7522 if (!mDisplayFrozen) {
7523 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007525 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7526 mHasTransformation = true;
7527 mHasLocalTransformation = true;
7528 if (!mLocalAnimating) {
7529 if (DEBUG_ANIM) Log.v(
7530 TAG, "Starting animation in " + this +
7531 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7532 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7533 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7534 mAnimation.setStartTime(currentTime);
7535 mLocalAnimating = true;
7536 mAnimating = true;
7537 }
7538 mTransformation.clear();
7539 final boolean more = mAnimation.getTransformation(
7540 currentTime, mTransformation);
7541 if (DEBUG_ANIM) Log.v(
7542 TAG, "Stepped animation in " + this +
7543 ": more=" + more + ", xform=" + mTransformation);
7544 if (more) {
7545 // we're not done!
7546 return true;
7547 }
7548 if (DEBUG_ANIM) Log.v(
7549 TAG, "Finished animation in " + this +
7550 " @ " + currentTime);
7551 mAnimation = null;
7552 //WindowManagerService.this.dump();
7553 }
7554 mHasLocalTransformation = false;
7555 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007556 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007557 // When our app token is animating, we kind-of pretend like
7558 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7559 // part of this check means that we will only do this if
7560 // our window is not currently exiting, or it is not
7561 // locally animating itself. The idea being that one that
7562 // is exiting and doing a local animation should be removed
7563 // once that animation is done.
7564 mAnimating = true;
7565 mHasTransformation = true;
7566 mTransformation.clear();
7567 return false;
7568 } else if (mHasTransformation) {
7569 // Little trick to get through the path below to act like
7570 // we have finished an animation.
7571 mAnimating = true;
7572 } else if (isAnimating()) {
7573 mAnimating = true;
7574 }
7575 } else if (mAnimation != null) {
7576 // If the display is frozen, and there is a pending animation,
7577 // clear it and make sure we run the cleanup code.
7578 mAnimating = true;
7579 mLocalAnimating = true;
7580 mAnimation = null;
7581 }
Romain Guy06882f82009-06-10 13:36:04 -07007582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 if (!mAnimating && !mLocalAnimating) {
7584 return false;
7585 }
7586
7587 if (DEBUG_ANIM) Log.v(
7588 TAG, "Animation done in " + this + ": exiting=" + mExiting
7589 + ", reportedVisible="
7590 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007592 mAnimating = false;
7593 mLocalAnimating = false;
7594 mAnimation = null;
7595 mAnimLayer = mLayer;
7596 if (mIsImWindow) {
7597 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007598 } else if (mIsWallpaper) {
7599 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007600 }
7601 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7602 + " anim layer: " + mAnimLayer);
7603 mHasTransformation = false;
7604 mHasLocalTransformation = false;
7605 mPolicyVisibility = mPolicyVisibilityAfterAnim;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007606 if (!mPolicyVisibility) {
7607 // Window is no longer visible -- make sure if we were waiting
7608 // for it to be displayed before enabling the display, that
7609 // we allow the display to be enabled now.
7610 enableScreenIfNeededLocked();
7611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 mTransformation.clear();
7613 if (mHasDrawn
7614 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7615 && mAppToken != null
7616 && mAppToken.firstWindowDrawn
7617 && mAppToken.startingData != null) {
7618 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7619 + mToken + ": first real window done animating");
7620 mFinishedStarting.add(mAppToken);
7621 mH.sendEmptyMessage(H.FINISHED_STARTING);
7622 }
Romain Guy06882f82009-06-10 13:36:04 -07007623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007624 finishExit();
7625
7626 if (mAppToken != null) {
7627 mAppToken.updateReportedVisibilityLocked();
7628 }
7629
7630 return false;
7631 }
7632
7633 void finishExit() {
7634 if (DEBUG_ANIM) Log.v(
7635 TAG, "finishExit in " + this
7636 + ": exiting=" + mExiting
7637 + " remove=" + mRemoveOnExit
7638 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007640 final int N = mChildWindows.size();
7641 for (int i=0; i<N; i++) {
7642 ((WindowState)mChildWindows.get(i)).finishExit();
7643 }
Romain Guy06882f82009-06-10 13:36:04 -07007644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007645 if (!mExiting) {
7646 return;
7647 }
Romain Guy06882f82009-06-10 13:36:04 -07007648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 if (isWindowAnimating()) {
7650 return;
7651 }
7652
7653 if (localLOGV) Log.v(
7654 TAG, "Exit animation finished in " + this
7655 + ": remove=" + mRemoveOnExit);
7656 if (mSurface != null) {
7657 mDestroySurface.add(this);
7658 mDestroying = true;
7659 if (SHOW_TRANSACTIONS) Log.i(
7660 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7661 try {
7662 mSurface.hide();
7663 } catch (RuntimeException e) {
7664 Log.w(TAG, "Error hiding surface in " + this, e);
7665 }
7666 mLastHidden = true;
7667 mKeyWaiter.releasePendingPointerLocked(mSession);
7668 }
7669 mExiting = false;
7670 if (mRemoveOnExit) {
7671 mPendingRemove.add(this);
7672 mRemoveOnExit = false;
7673 }
7674 }
Romain Guy06882f82009-06-10 13:36:04 -07007675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7677 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7678 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7679 if (dtdx < -.000001f || dtdx > .000001f) return false;
7680 if (dsdy < -.000001f || dsdy > .000001f) return false;
7681 return true;
7682 }
Romain Guy06882f82009-06-10 13:36:04 -07007683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684 void computeShownFrameLocked() {
7685 final boolean selfTransformation = mHasLocalTransformation;
7686 Transformation attachedTransformation =
7687 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7688 ? mAttachedWindow.mTransformation : null;
7689 Transformation appTransformation =
7690 (mAppToken != null && mAppToken.hasTransformation)
7691 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007692
7693 // Wallpapers are animated based on the "real" window they
7694 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007695 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007696 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007697 if (mWallpaperTarget.mHasLocalTransformation &&
7698 mWallpaperTarget.mAnimation != null &&
7699 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007700 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007701 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7702 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7703 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007704 }
7705 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007706 mWallpaperTarget.mAppToken.hasTransformation &&
7707 mWallpaperTarget.mAppToken.animation != null &&
7708 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007709 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007710 if (DEBUG_WALLPAPER && appTransformation != null) {
7711 Log.v(TAG, "WP target app xform: " + appTransformation);
7712 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007713 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007714 }
7715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716 if (selfTransformation || attachedTransformation != null
7717 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007718 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 final Rect frame = mFrame;
7720 final float tmpFloats[] = mTmpFloats;
7721 final Matrix tmpMatrix = mTmpMatrix;
7722
7723 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007724 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007726 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007727 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007728 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007729 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007730 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007731 }
7732 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007733 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734 }
7735
7736 // "convert" it into SurfaceFlinger's format
7737 // (a 2x2 matrix + an offset)
7738 // Here we must not transform the position of the surface
7739 // since it is already included in the transformation.
7740 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007742 tmpMatrix.getValues(tmpFloats);
7743 mDsDx = tmpFloats[Matrix.MSCALE_X];
7744 mDtDx = tmpFloats[Matrix.MSKEW_X];
7745 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7746 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007747 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7748 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007749 int w = frame.width();
7750 int h = frame.height();
7751 mShownFrame.set(x, y, x+w, y+h);
7752
7753 // Now set the alpha... but because our current hardware
7754 // can't do alpha transformation on a non-opaque surface,
7755 // turn it off if we are running an animation that is also
7756 // transforming since it is more important to have that
7757 // animation be smooth.
7758 mShownAlpha = mAlpha;
7759 if (!mLimitedAlphaCompositing
7760 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7761 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7762 && x == frame.left && y == frame.top))) {
7763 //Log.i(TAG, "Applying alpha transform");
7764 if (selfTransformation) {
7765 mShownAlpha *= mTransformation.getAlpha();
7766 }
7767 if (attachedTransformation != null) {
7768 mShownAlpha *= attachedTransformation.getAlpha();
7769 }
7770 if (appTransformation != null) {
7771 mShownAlpha *= appTransformation.getAlpha();
7772 }
7773 } else {
7774 //Log.i(TAG, "Not applying alpha transform");
7775 }
Romain Guy06882f82009-06-10 13:36:04 -07007776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007777 if (localLOGV) Log.v(
7778 TAG, "Continuing animation in " + this +
7779 ": " + mShownFrame +
7780 ", alpha=" + mTransformation.getAlpha());
7781 return;
7782 }
Romain Guy06882f82009-06-10 13:36:04 -07007783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007784 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007785 if (mXOffset != 0 || mYOffset != 0) {
7786 mShownFrame.offset(mXOffset, mYOffset);
7787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007788 mShownAlpha = mAlpha;
7789 mDsDx = 1;
7790 mDtDx = 0;
7791 mDsDy = 0;
7792 mDtDy = 1;
7793 }
Romain Guy06882f82009-06-10 13:36:04 -07007794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 /**
7796 * Is this window visible? It is not visible if there is no
7797 * surface, or we are in the process of running an exit animation
7798 * that will remove the surface, or its app token has been hidden.
7799 */
7800 public boolean isVisibleLw() {
7801 final AppWindowToken atoken = mAppToken;
7802 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7803 && (atoken == null || !atoken.hiddenRequested)
7804 && !mExiting && !mDestroying;
7805 }
7806
7807 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007808 * Like {@link #isVisibleLw}, but also counts a window that is currently
7809 * "hidden" behind the keyguard as visible. This allows us to apply
7810 * things like window flags that impact the keyguard.
7811 * XXX I am starting to think we need to have ANOTHER visibility flag
7812 * for this "hidden behind keyguard" state rather than overloading
7813 * mPolicyVisibility. Ungh.
7814 */
7815 public boolean isVisibleOrBehindKeyguardLw() {
7816 final AppWindowToken atoken = mAppToken;
7817 return mSurface != null && !mAttachedHidden
7818 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7819 && !mExiting && !mDestroying;
7820 }
7821
7822 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 * Is this window visible, ignoring its app token? It is not visible
7824 * if there is no surface, or we are in the process of running an exit animation
7825 * that will remove the surface.
7826 */
7827 public boolean isWinVisibleLw() {
7828 final AppWindowToken atoken = mAppToken;
7829 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7830 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7831 && !mExiting && !mDestroying;
7832 }
7833
7834 /**
7835 * The same as isVisible(), but follows the current hidden state of
7836 * the associated app token, not the pending requested hidden state.
7837 */
7838 boolean isVisibleNow() {
7839 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007840 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007841 }
7842
7843 /**
7844 * Same as isVisible(), but we also count it as visible between the
7845 * call to IWindowSession.add() and the first relayout().
7846 */
7847 boolean isVisibleOrAdding() {
7848 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007849 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007850 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7851 && mPolicyVisibility && !mAttachedHidden
7852 && (atoken == null || !atoken.hiddenRequested)
7853 && !mExiting && !mDestroying;
7854 }
7855
7856 /**
7857 * Is this window currently on-screen? It is on-screen either if it
7858 * is visible or it is currently running an animation before no longer
7859 * being visible.
7860 */
7861 boolean isOnScreen() {
7862 final AppWindowToken atoken = mAppToken;
7863 if (atoken != null) {
7864 return mSurface != null && mPolicyVisibility && !mDestroying
7865 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007866 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 } else {
7868 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007869 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007870 }
7871 }
Romain Guy06882f82009-06-10 13:36:04 -07007872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007873 /**
7874 * Like isOnScreen(), but we don't return true if the window is part
7875 * of a transition that has not yet been started.
7876 */
7877 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007878 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007879 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007880 return false;
7881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007882 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007883 final boolean animating = atoken != null
7884 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007885 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007886 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7887 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007888 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 }
7890
7891 /** Is the window or its container currently animating? */
7892 boolean isAnimating() {
7893 final WindowState attached = mAttachedWindow;
7894 final AppWindowToken atoken = mAppToken;
7895 return mAnimation != null
7896 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007897 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007898 (atoken.animation != null
7899 || atoken.inPendingTransaction));
7900 }
7901
7902 /** Is this window currently animating? */
7903 boolean isWindowAnimating() {
7904 return mAnimation != null;
7905 }
7906
7907 /**
7908 * Like isOnScreen, but returns false if the surface hasn't yet
7909 * been drawn.
7910 */
7911 public boolean isDisplayedLw() {
7912 final AppWindowToken atoken = mAppToken;
7913 return mSurface != null && mPolicyVisibility && !mDestroying
7914 && !mDrawPending && !mCommitDrawPending
7915 && ((!mAttachedHidden &&
7916 (atoken == null || !atoken.hiddenRequested))
7917 || mAnimating);
7918 }
7919
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007920 /**
7921 * Returns true if the window has a surface that it has drawn a
7922 * complete UI in to.
7923 */
7924 public boolean isDrawnLw() {
7925 final AppWindowToken atoken = mAppToken;
7926 return mSurface != null && !mDestroying
7927 && !mDrawPending && !mCommitDrawPending;
7928 }
7929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007930 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7931 boolean shownFrame, boolean onlyOpaque) {
7932 if (mSurface == null) {
7933 return false;
7934 }
7935 if (mAppToken != null && !mAppToken.appFullscreen) {
7936 return false;
7937 }
7938 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7939 return false;
7940 }
7941 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007942
7943 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7944 return frame.left <= mCompatibleScreenFrame.left &&
7945 frame.top <= mCompatibleScreenFrame.top &&
7946 frame.right >= mCompatibleScreenFrame.right &&
7947 frame.bottom >= mCompatibleScreenFrame.bottom;
7948 } else {
7949 return frame.left <= 0 && frame.top <= 0
7950 && frame.right >= screenWidth
7951 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 }
Romain Guy06882f82009-06-10 13:36:04 -07007954
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007955 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007956 * Return true if the window is opaque and fully drawn. This indicates
7957 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007958 */
7959 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007960 return (mAttrs.format == PixelFormat.OPAQUE
7961 || mAttrs.type == TYPE_WALLPAPER)
7962 && mSurface != null && mAnimation == null
7963 && (mAppToken == null || mAppToken.animation == null)
7964 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007965 }
7966
7967 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7968 return
7969 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007970 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7971 // only if it's visible
7972 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007973 // and only if the application fills the compatible screen
7974 mFrame.left <= mCompatibleScreenFrame.left &&
7975 mFrame.top <= mCompatibleScreenFrame.top &&
7976 mFrame.right >= mCompatibleScreenFrame.right &&
7977 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007978 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007979 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007980 }
7981
7982 boolean isFullscreen(int screenWidth, int screenHeight) {
7983 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007984 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007985 }
7986
7987 void removeLocked() {
7988 if (mAttachedWindow != null) {
7989 mAttachedWindow.mChildWindows.remove(this);
7990 }
7991 destroySurfaceLocked();
7992 mSession.windowRemovedLocked();
7993 try {
7994 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7995 } catch (RuntimeException e) {
7996 // Ignore if it has already been removed (usually because
7997 // we are doing this as part of processing a death note.)
7998 }
7999 }
8000
8001 private class DeathRecipient implements IBinder.DeathRecipient {
8002 public void binderDied() {
8003 try {
8004 synchronized(mWindowMap) {
8005 WindowState win = windowForClientLocked(mSession, mClient);
8006 Log.i(TAG, "WIN DEATH: " + win);
8007 if (win != null) {
8008 removeWindowLocked(mSession, win);
8009 }
8010 }
8011 } catch (IllegalArgumentException ex) {
8012 // This will happen if the window has already been
8013 // removed.
8014 }
8015 }
8016 }
8017
8018 /** Returns true if this window desires key events. */
8019 public final boolean canReceiveKeys() {
8020 return isVisibleOrAdding()
8021 && (mViewVisibility == View.VISIBLE)
8022 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8023 }
8024
8025 public boolean hasDrawnLw() {
8026 return mHasDrawn;
8027 }
8028
8029 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008030 return showLw(doAnimation, true);
8031 }
8032
8033 boolean showLw(boolean doAnimation, boolean requestAnim) {
8034 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8035 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008036 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008037 mPolicyVisibility = true;
8038 mPolicyVisibilityAfterAnim = true;
8039 if (doAnimation) {
8040 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8041 }
8042 if (requestAnim) {
8043 requestAnimationLocked(0);
8044 }
8045 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046 }
8047
8048 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008049 return hideLw(doAnimation, true);
8050 }
8051
8052 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008053 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8054 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008055 if (!current) {
8056 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008057 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008058 if (doAnimation) {
8059 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8060 if (mAnimation == null) {
8061 doAnimation = false;
8062 }
8063 }
8064 if (doAnimation) {
8065 mPolicyVisibilityAfterAnim = false;
8066 } else {
8067 mPolicyVisibilityAfterAnim = false;
8068 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008069 // Window is no longer visible -- make sure if we were waiting
8070 // for it to be displayed before enabling the display, that
8071 // we allow the display to be enabled now.
8072 enableScreenIfNeededLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008073 }
8074 if (requestAnim) {
8075 requestAnimationLocked(0);
8076 }
8077 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008078 }
8079
8080 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008081 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07008082
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008083 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8084 pw.print(" mClient="); pw.println(mClient.asBinder());
8085 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8086 if (mAttachedWindow != null || mLayoutAttached) {
8087 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8088 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8089 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008090 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8091 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8092 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008093 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8094 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008095 }
8096 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8097 pw.print(" mSubLayer="); pw.print(mSubLayer);
8098 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8099 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8100 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8101 pw.print("="); pw.print(mAnimLayer);
8102 pw.print(" mLastLayer="); pw.println(mLastLayer);
8103 if (mSurface != null) {
8104 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8105 }
8106 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8107 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8108 if (mAppToken != null) {
8109 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8110 }
8111 if (mTargetAppToken != null) {
8112 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8113 }
8114 pw.print(prefix); pw.print("mViewVisibility=0x");
8115 pw.print(Integer.toHexString(mViewVisibility));
8116 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008117 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8118 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008119 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8120 pw.print(prefix); pw.print("mPolicyVisibility=");
8121 pw.print(mPolicyVisibility);
8122 pw.print(" mPolicyVisibilityAfterAnim=");
8123 pw.print(mPolicyVisibilityAfterAnim);
8124 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8125 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008126 if (!mRelayoutCalled) {
8127 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8128 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008129 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008130 pw.print(" h="); pw.println(mRequestedHeight);
8131 if (mXOffset != 0 || mYOffset != 0) {
8132 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8133 pw.print(" y="); pw.println(mYOffset);
8134 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008135 pw.print(prefix); pw.print("mGivenContentInsets=");
8136 mGivenContentInsets.printShortString(pw);
8137 pw.print(" mGivenVisibleInsets=");
8138 mGivenVisibleInsets.printShortString(pw);
8139 pw.println();
8140 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8141 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8142 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8143 }
8144 pw.print(prefix); pw.print("mShownFrame=");
8145 mShownFrame.printShortString(pw);
8146 pw.print(" last="); mLastShownFrame.printShortString(pw);
8147 pw.println();
8148 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8149 pw.print(" last="); mLastFrame.printShortString(pw);
8150 pw.println();
8151 pw.print(prefix); pw.print("mContainingFrame=");
8152 mContainingFrame.printShortString(pw);
8153 pw.print(" mDisplayFrame=");
8154 mDisplayFrame.printShortString(pw);
8155 pw.println();
8156 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8157 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8158 pw.println();
8159 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8160 pw.print(" last="); mLastContentInsets.printShortString(pw);
8161 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8162 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8163 pw.println();
8164 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8165 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8166 pw.print(" mAlpha="); pw.print(mAlpha);
8167 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8168 }
8169 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8170 || mAnimation != null) {
8171 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8172 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8173 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8174 pw.print(" mAnimation="); pw.println(mAnimation);
8175 }
8176 if (mHasTransformation || mHasLocalTransformation) {
8177 pw.print(prefix); pw.print("XForm: has=");
8178 pw.print(mHasTransformation);
8179 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8180 pw.print(" "); mTransformation.printShortString(pw);
8181 pw.println();
8182 }
8183 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8184 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8185 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8186 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8187 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8188 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8189 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8190 pw.print(" mDestroying="); pw.print(mDestroying);
8191 pw.print(" mRemoved="); pw.println(mRemoved);
8192 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008193 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008194 pw.print(prefix); pw.print("mOrientationChanging=");
8195 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008196 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8197 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008198 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008199 if (mHScale != 1 || mVScale != 1) {
8200 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8201 pw.print(" mVScale="); pw.println(mVScale);
8202 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008203 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008204 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8205 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8206 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008207 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8208 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8209 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211 }
8212
8213 @Override
8214 public String toString() {
8215 return "Window{"
8216 + Integer.toHexString(System.identityHashCode(this))
8217 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8218 }
8219 }
Romain Guy06882f82009-06-10 13:36:04 -07008220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008221 // -------------------------------------------------------------
8222 // Window Token State
8223 // -------------------------------------------------------------
8224
8225 class WindowToken {
8226 // The actual token.
8227 final IBinder token;
8228
8229 // The type of window this token is for, as per WindowManager.LayoutParams.
8230 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008232 // Set if this token was explicitly added by a client, so should
8233 // not be removed when all windows are removed.
8234 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008235
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008236 // For printing.
8237 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008239 // If this is an AppWindowToken, this is non-null.
8240 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008242 // All of the windows associated with this token.
8243 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8244
8245 // Is key dispatching paused for this token?
8246 boolean paused = false;
8247
8248 // Should this token's windows be hidden?
8249 boolean hidden;
8250
8251 // Temporary for finding which tokens no longer have visible windows.
8252 boolean hasVisible;
8253
Dianne Hackborna8f60182009-09-01 19:01:50 -07008254 // Set to true when this token is in a pending transaction where it
8255 // will be shown.
8256 boolean waitingToShow;
8257
8258 // Set to true when this token is in a pending transaction where it
8259 // will be hidden.
8260 boolean waitingToHide;
8261
8262 // Set to true when this token is in a pending transaction where its
8263 // windows will be put to the bottom of the list.
8264 boolean sendingToBottom;
8265
8266 // Set to true when this token is in a pending transaction where its
8267 // windows will be put to the top of the list.
8268 boolean sendingToTop;
8269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008270 WindowToken(IBinder _token, int type, boolean _explicit) {
8271 token = _token;
8272 windowType = type;
8273 explicit = _explicit;
8274 }
8275
8276 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008277 pw.print(prefix); pw.print("token="); pw.println(token);
8278 pw.print(prefix); pw.print("windows="); pw.println(windows);
8279 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8280 pw.print(" hidden="); pw.print(hidden);
8281 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008282 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8283 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8284 pw.print(" waitingToHide="); pw.print(waitingToHide);
8285 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8286 pw.print(" sendingToTop="); pw.println(sendingToTop);
8287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008288 }
8289
8290 @Override
8291 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008292 if (stringName == null) {
8293 StringBuilder sb = new StringBuilder();
8294 sb.append("WindowToken{");
8295 sb.append(Integer.toHexString(System.identityHashCode(this)));
8296 sb.append(" token="); sb.append(token); sb.append('}');
8297 stringName = sb.toString();
8298 }
8299 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008300 }
8301 };
8302
8303 class AppWindowToken extends WindowToken {
8304 // Non-null only for application tokens.
8305 final IApplicationToken appToken;
8306
8307 // All of the windows and child windows that are included in this
8308 // application token. Note this list is NOT sorted!
8309 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8310
8311 int groupId = -1;
8312 boolean appFullscreen;
8313 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008315 // These are used for determining when all windows associated with
8316 // an activity have been drawn, so they can be made visible together
8317 // at the same time.
8318 int lastTransactionSequence = mTransactionSequence-1;
8319 int numInterestingWindows;
8320 int numDrawnWindows;
8321 boolean inPendingTransaction;
8322 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008324 // Is this token going to be hidden in a little while? If so, it
8325 // won't be taken into account for setting the screen orientation.
8326 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 // Is this window's surface needed? This is almost like hidden, except
8329 // it will sometimes be true a little earlier: when the token has
8330 // been shown, but is still waiting for its app transition to execute
8331 // before making its windows shown.
8332 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008334 // Have we told the window clients to hide themselves?
8335 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 // Last visibility state we reported to the app token.
8338 boolean reportedVisible;
8339
8340 // Set to true when the token has been removed from the window mgr.
8341 boolean removed;
8342
8343 // Have we been asked to have this token keep the screen frozen?
8344 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008346 boolean animating;
8347 Animation animation;
8348 boolean hasTransformation;
8349 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008351 // Offset to the window of all layers in the token, for use by
8352 // AppWindowToken animations.
8353 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 // Information about an application starting window if displayed.
8356 StartingData startingData;
8357 WindowState startingWindow;
8358 View startingView;
8359 boolean startingDisplayed;
8360 boolean startingMoved;
8361 boolean firstWindowDrawn;
8362
8363 AppWindowToken(IApplicationToken _token) {
8364 super(_token.asBinder(),
8365 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8366 appWindowToken = this;
8367 appToken = _token;
8368 }
Romain Guy06882f82009-06-10 13:36:04 -07008369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008370 public void setAnimation(Animation anim) {
8371 if (localLOGV) Log.v(
8372 TAG, "Setting animation in " + this + ": " + anim);
8373 animation = anim;
8374 animating = false;
8375 anim.restrictDuration(MAX_ANIMATION_DURATION);
8376 anim.scaleCurrentDuration(mTransitionAnimationScale);
8377 int zorder = anim.getZAdjustment();
8378 int adj = 0;
8379 if (zorder == Animation.ZORDER_TOP) {
8380 adj = TYPE_LAYER_OFFSET;
8381 } else if (zorder == Animation.ZORDER_BOTTOM) {
8382 adj = -TYPE_LAYER_OFFSET;
8383 }
Romain Guy06882f82009-06-10 13:36:04 -07008384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008385 if (animLayerAdjustment != adj) {
8386 animLayerAdjustment = adj;
8387 updateLayers();
8388 }
8389 }
Romain Guy06882f82009-06-10 13:36:04 -07008390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008391 public void setDummyAnimation() {
8392 if (animation == null) {
8393 if (localLOGV) Log.v(
8394 TAG, "Setting dummy animation in " + this);
8395 animation = sDummyAnimation;
8396 }
8397 }
8398
8399 public void clearAnimation() {
8400 if (animation != null) {
8401 animation = null;
8402 animating = true;
8403 }
8404 }
Romain Guy06882f82009-06-10 13:36:04 -07008405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008406 void updateLayers() {
8407 final int N = allAppWindows.size();
8408 final int adj = animLayerAdjustment;
8409 for (int i=0; i<N; i++) {
8410 WindowState w = allAppWindows.get(i);
8411 w.mAnimLayer = w.mLayer + adj;
8412 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8413 + w.mAnimLayer);
8414 if (w == mInputMethodTarget) {
8415 setInputMethodAnimLayerAdjustment(adj);
8416 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008417 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008418 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 }
8421 }
Romain Guy06882f82009-06-10 13:36:04 -07008422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008423 void sendAppVisibilityToClients() {
8424 final int N = allAppWindows.size();
8425 for (int i=0; i<N; i++) {
8426 WindowState win = allAppWindows.get(i);
8427 if (win == startingWindow && clientHidden) {
8428 // Don't hide the starting window.
8429 continue;
8430 }
8431 try {
8432 if (DEBUG_VISIBILITY) Log.v(TAG,
8433 "Setting visibility of " + win + ": " + (!clientHidden));
8434 win.mClient.dispatchAppVisibility(!clientHidden);
8435 } catch (RemoteException e) {
8436 }
8437 }
8438 }
Romain Guy06882f82009-06-10 13:36:04 -07008439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008440 void showAllWindowsLocked() {
8441 final int NW = allAppWindows.size();
8442 for (int i=0; i<NW; i++) {
8443 WindowState w = allAppWindows.get(i);
8444 if (DEBUG_VISIBILITY) Log.v(TAG,
8445 "performing show on: " + w);
8446 w.performShowLocked();
8447 }
8448 }
Romain Guy06882f82009-06-10 13:36:04 -07008449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008450 // This must be called while inside a transaction.
8451 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8452 if (!mDisplayFrozen) {
8453 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008455 if (animation == sDummyAnimation) {
8456 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008457 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008458 // when it is really time to animate, this will be set to
8459 // a real animation and the next call will execute normally.
8460 return false;
8461 }
Romain Guy06882f82009-06-10 13:36:04 -07008462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008463 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8464 if (!animating) {
8465 if (DEBUG_ANIM) Log.v(
8466 TAG, "Starting animation in " + this +
8467 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8468 + " scale=" + mTransitionAnimationScale
8469 + " allDrawn=" + allDrawn + " animating=" + animating);
8470 animation.initialize(dw, dh, dw, dh);
8471 animation.setStartTime(currentTime);
8472 animating = true;
8473 }
8474 transformation.clear();
8475 final boolean more = animation.getTransformation(
8476 currentTime, transformation);
8477 if (DEBUG_ANIM) Log.v(
8478 TAG, "Stepped animation in " + this +
8479 ": more=" + more + ", xform=" + transformation);
8480 if (more) {
8481 // we're done!
8482 hasTransformation = true;
8483 return true;
8484 }
8485 if (DEBUG_ANIM) Log.v(
8486 TAG, "Finished animation in " + this +
8487 " @ " + currentTime);
8488 animation = null;
8489 }
8490 } else if (animation != null) {
8491 // If the display is frozen, and there is a pending animation,
8492 // clear it and make sure we run the cleanup code.
8493 animating = true;
8494 animation = null;
8495 }
8496
8497 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 if (!animating) {
8500 return false;
8501 }
8502
8503 clearAnimation();
8504 animating = false;
8505 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8506 moveInputMethodWindowsIfNeededLocked(true);
8507 }
Romain Guy06882f82009-06-10 13:36:04 -07008508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 if (DEBUG_ANIM) Log.v(
8510 TAG, "Animation done in " + this
8511 + ": reportedVisible=" + reportedVisible);
8512
8513 transformation.clear();
8514 if (animLayerAdjustment != 0) {
8515 animLayerAdjustment = 0;
8516 updateLayers();
8517 }
Romain Guy06882f82009-06-10 13:36:04 -07008518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 final int N = windows.size();
8520 for (int i=0; i<N; i++) {
8521 ((WindowState)windows.get(i)).finishExit();
8522 }
8523 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008525 return false;
8526 }
8527
8528 void updateReportedVisibilityLocked() {
8529 if (appToken == null) {
8530 return;
8531 }
Romain Guy06882f82009-06-10 13:36:04 -07008532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008533 int numInteresting = 0;
8534 int numVisible = 0;
8535 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008537 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8538 final int N = allAppWindows.size();
8539 for (int i=0; i<N; i++) {
8540 WindowState win = allAppWindows.get(i);
Maciej Białka3779dd12010-03-24 10:20:52 +01008541 if (win == startingWindow || win.mAppFreezing
8542 || win.mAttrs.type == TYPE_APPLICATION_STARTING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 continue;
8544 }
8545 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008546 Log.v(TAG, "Win " + win + ": isDrawn="
8547 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008549 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008550 Log.v(TAG, "Not displayed: s=" + win.mSurface
8551 + " pv=" + win.mPolicyVisibility
8552 + " dp=" + win.mDrawPending
8553 + " cdp=" + win.mCommitDrawPending
8554 + " ah=" + win.mAttachedHidden
8555 + " th="
8556 + (win.mAppToken != null
8557 ? win.mAppToken.hiddenRequested : false)
8558 + " a=" + win.mAnimating);
8559 }
8560 }
8561 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008562 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008563 if (!win.isAnimating()) {
8564 numVisible++;
8565 }
8566 nowGone = false;
8567 } else if (win.isAnimating()) {
8568 nowGone = false;
8569 }
8570 }
Romain Guy06882f82009-06-10 13:36:04 -07008571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8573 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8574 + numInteresting + " visible=" + numVisible);
8575 if (nowVisible != reportedVisible) {
8576 if (DEBUG_VISIBILITY) Log.v(
8577 TAG, "Visibility changed in " + this
8578 + ": vis=" + nowVisible);
8579 reportedVisible = nowVisible;
8580 Message m = mH.obtainMessage(
8581 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8582 nowVisible ? 1 : 0,
8583 nowGone ? 1 : 0,
8584 this);
8585 mH.sendMessage(m);
8586 }
8587 }
Romain Guy06882f82009-06-10 13:36:04 -07008588
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008589 WindowState findMainWindow() {
8590 int j = windows.size();
8591 while (j > 0) {
8592 j--;
8593 WindowState win = windows.get(j);
8594 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8595 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8596 return win;
8597 }
8598 }
8599 return null;
8600 }
8601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008602 void dump(PrintWriter pw, String prefix) {
8603 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008604 if (appToken != null) {
8605 pw.print(prefix); pw.println("app=true");
8606 }
8607 if (allAppWindows.size() > 0) {
8608 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8609 }
8610 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008611 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008612 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8613 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8614 pw.print(" clientHidden="); pw.print(clientHidden);
8615 pw.print(" willBeHidden="); pw.print(willBeHidden);
8616 pw.print(" reportedVisible="); pw.println(reportedVisible);
8617 if (paused || freezingScreen) {
8618 pw.print(prefix); pw.print("paused="); pw.print(paused);
8619 pw.print(" freezingScreen="); pw.println(freezingScreen);
8620 }
8621 if (numInterestingWindows != 0 || numDrawnWindows != 0
8622 || inPendingTransaction || allDrawn) {
8623 pw.print(prefix); pw.print("numInterestingWindows=");
8624 pw.print(numInterestingWindows);
8625 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8626 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8627 pw.print(" allDrawn="); pw.println(allDrawn);
8628 }
8629 if (animating || animation != null) {
8630 pw.print(prefix); pw.print("animating="); pw.print(animating);
8631 pw.print(" animation="); pw.println(animation);
8632 }
8633 if (animLayerAdjustment != 0) {
8634 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8635 }
8636 if (hasTransformation) {
8637 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8638 pw.print(" transformation="); transformation.printShortString(pw);
8639 pw.println();
8640 }
8641 if (startingData != null || removed || firstWindowDrawn) {
8642 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8643 pw.print(" removed="); pw.print(removed);
8644 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8645 }
8646 if (startingWindow != null || startingView != null
8647 || startingDisplayed || startingMoved) {
8648 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8649 pw.print(" startingView="); pw.print(startingView);
8650 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8651 pw.print(" startingMoved"); pw.println(startingMoved);
8652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008653 }
8654
8655 @Override
8656 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008657 if (stringName == null) {
8658 StringBuilder sb = new StringBuilder();
8659 sb.append("AppWindowToken{");
8660 sb.append(Integer.toHexString(System.identityHashCode(this)));
8661 sb.append(" token="); sb.append(token); sb.append('}');
8662 stringName = sb.toString();
8663 }
8664 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008665 }
8666 }
Romain Guy06882f82009-06-10 13:36:04 -07008667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 // -------------------------------------------------------------
8669 // DummyAnimation
8670 // -------------------------------------------------------------
8671
8672 // This is an animation that does nothing: it just immediately finishes
8673 // itself every time it is called. It is used as a stub animation in cases
8674 // where we want to synchronize multiple things that may be animating.
8675 static final class DummyAnimation extends Animation {
8676 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8677 return false;
8678 }
8679 }
8680 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 // -------------------------------------------------------------
8683 // Async Handler
8684 // -------------------------------------------------------------
8685
8686 static final class StartingData {
8687 final String pkg;
8688 final int theme;
8689 final CharSequence nonLocalizedLabel;
8690 final int labelRes;
8691 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008693 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8694 int _labelRes, int _icon) {
8695 pkg = _pkg;
8696 theme = _theme;
8697 nonLocalizedLabel = _nonLocalizedLabel;
8698 labelRes = _labelRes;
8699 icon = _icon;
8700 }
8701 }
8702
8703 private final class H extends Handler {
8704 public static final int REPORT_FOCUS_CHANGE = 2;
8705 public static final int REPORT_LOSING_FOCUS = 3;
8706 public static final int ANIMATE = 4;
8707 public static final int ADD_STARTING = 5;
8708 public static final int REMOVE_STARTING = 6;
8709 public static final int FINISHED_STARTING = 7;
8710 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008711 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8712 public static final int HOLD_SCREEN_CHANGED = 12;
8713 public static final int APP_TRANSITION_TIMEOUT = 13;
8714 public static final int PERSIST_ANIMATION_SCALE = 14;
8715 public static final int FORCE_GC = 15;
8716 public static final int ENABLE_SCREEN = 16;
8717 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008718 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 public H() {
8723 }
Romain Guy06882f82009-06-10 13:36:04 -07008724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008725 @Override
8726 public void handleMessage(Message msg) {
8727 switch (msg.what) {
8728 case REPORT_FOCUS_CHANGE: {
8729 WindowState lastFocus;
8730 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008732 synchronized(mWindowMap) {
8733 lastFocus = mLastFocus;
8734 newFocus = mCurrentFocus;
8735 if (lastFocus == newFocus) {
8736 // Focus is not changing, so nothing to do.
8737 return;
8738 }
8739 mLastFocus = newFocus;
8740 //Log.i(TAG, "Focus moving from " + lastFocus
8741 // + " to " + newFocus);
8742 if (newFocus != null && lastFocus != null
8743 && !newFocus.isDisplayedLw()) {
8744 //Log.i(TAG, "Delaying loss of focus...");
8745 mLosingFocus.add(lastFocus);
8746 lastFocus = null;
8747 }
8748 }
8749
8750 if (lastFocus != newFocus) {
8751 //System.out.println("Changing focus from " + lastFocus
8752 // + " to " + newFocus);
8753 if (newFocus != null) {
8754 try {
8755 //Log.i(TAG, "Gaining focus: " + newFocus);
8756 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8757 } catch (RemoteException e) {
8758 // Ignore if process has died.
8759 }
8760 }
8761
8762 if (lastFocus != null) {
8763 try {
8764 //Log.i(TAG, "Losing focus: " + lastFocus);
8765 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8766 } catch (RemoteException e) {
8767 // Ignore if process has died.
8768 }
8769 }
8770 }
8771 } break;
8772
8773 case REPORT_LOSING_FOCUS: {
8774 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008776 synchronized(mWindowMap) {
8777 losers = mLosingFocus;
8778 mLosingFocus = new ArrayList<WindowState>();
8779 }
8780
8781 final int N = losers.size();
8782 for (int i=0; i<N; i++) {
8783 try {
8784 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8785 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8786 } catch (RemoteException e) {
8787 // Ignore if process has died.
8788 }
8789 }
8790 } break;
8791
8792 case ANIMATE: {
8793 synchronized(mWindowMap) {
8794 mAnimationPending = false;
8795 performLayoutAndPlaceSurfacesLocked();
8796 }
8797 } break;
8798
8799 case ADD_STARTING: {
8800 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8801 final StartingData sd = wtoken.startingData;
8802
8803 if (sd == null) {
8804 // Animation has been canceled... do nothing.
8805 return;
8806 }
Romain Guy06882f82009-06-10 13:36:04 -07008807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008808 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8809 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008811 View view = null;
8812 try {
8813 view = mPolicy.addStartingWindow(
8814 wtoken.token, sd.pkg,
8815 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8816 sd.icon);
8817 } catch (Exception e) {
8818 Log.w(TAG, "Exception when adding starting window", e);
8819 }
8820
8821 if (view != null) {
8822 boolean abort = false;
8823
8824 synchronized(mWindowMap) {
8825 if (wtoken.removed || wtoken.startingData == null) {
8826 // If the window was successfully added, then
8827 // we need to remove it.
8828 if (wtoken.startingWindow != null) {
8829 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8830 "Aborted starting " + wtoken
8831 + ": removed=" + wtoken.removed
8832 + " startingData=" + wtoken.startingData);
8833 wtoken.startingWindow = null;
8834 wtoken.startingData = null;
8835 abort = true;
8836 }
8837 } else {
8838 wtoken.startingView = view;
8839 }
8840 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8841 "Added starting " + wtoken
8842 + ": startingWindow="
8843 + wtoken.startingWindow + " startingView="
8844 + wtoken.startingView);
8845 }
8846
8847 if (abort) {
8848 try {
8849 mPolicy.removeStartingWindow(wtoken.token, view);
8850 } catch (Exception e) {
8851 Log.w(TAG, "Exception when removing starting window", e);
8852 }
8853 }
8854 }
8855 } break;
8856
8857 case REMOVE_STARTING: {
8858 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8859 IBinder token = null;
8860 View view = null;
8861 synchronized (mWindowMap) {
8862 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8863 + wtoken + ": startingWindow="
8864 + wtoken.startingWindow + " startingView="
8865 + wtoken.startingView);
8866 if (wtoken.startingWindow != null) {
8867 view = wtoken.startingView;
8868 token = wtoken.token;
8869 wtoken.startingData = null;
8870 wtoken.startingView = null;
8871 wtoken.startingWindow = null;
8872 }
8873 }
8874 if (view != null) {
8875 try {
8876 mPolicy.removeStartingWindow(token, view);
8877 } catch (Exception e) {
8878 Log.w(TAG, "Exception when removing starting window", e);
8879 }
8880 }
8881 } break;
8882
8883 case FINISHED_STARTING: {
8884 IBinder token = null;
8885 View view = null;
8886 while (true) {
8887 synchronized (mWindowMap) {
8888 final int N = mFinishedStarting.size();
8889 if (N <= 0) {
8890 break;
8891 }
8892 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8893
8894 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8895 "Finished starting " + wtoken
8896 + ": startingWindow=" + wtoken.startingWindow
8897 + " startingView=" + wtoken.startingView);
8898
8899 if (wtoken.startingWindow == null) {
8900 continue;
8901 }
8902
8903 view = wtoken.startingView;
8904 token = wtoken.token;
8905 wtoken.startingData = null;
8906 wtoken.startingView = null;
8907 wtoken.startingWindow = null;
8908 }
8909
8910 try {
8911 mPolicy.removeStartingWindow(token, view);
8912 } catch (Exception e) {
8913 Log.w(TAG, "Exception when removing starting window", e);
8914 }
8915 }
8916 } break;
8917
8918 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8919 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8920
8921 boolean nowVisible = msg.arg1 != 0;
8922 boolean nowGone = msg.arg2 != 0;
8923
8924 try {
8925 if (DEBUG_VISIBILITY) Log.v(
8926 TAG, "Reporting visible in " + wtoken
8927 + " visible=" + nowVisible
8928 + " gone=" + nowGone);
8929 if (nowVisible) {
8930 wtoken.appToken.windowsVisible();
8931 } else {
8932 wtoken.appToken.windowsGone();
8933 }
8934 } catch (RemoteException ex) {
8935 }
8936 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008938 case WINDOW_FREEZE_TIMEOUT: {
8939 synchronized (mWindowMap) {
8940 Log.w(TAG, "Window freeze timeout expired.");
8941 int i = mWindows.size();
8942 while (i > 0) {
8943 i--;
8944 WindowState w = (WindowState)mWindows.get(i);
8945 if (w.mOrientationChanging) {
8946 w.mOrientationChanging = false;
8947 Log.w(TAG, "Force clearing orientation change: " + w);
8948 }
8949 }
8950 performLayoutAndPlaceSurfacesLocked();
8951 }
8952 break;
8953 }
Romain Guy06882f82009-06-10 13:36:04 -07008954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008955 case HOLD_SCREEN_CHANGED: {
8956 Session oldHold;
8957 Session newHold;
8958 synchronized (mWindowMap) {
8959 oldHold = mLastReportedHold;
8960 newHold = (Session)msg.obj;
8961 mLastReportedHold = newHold;
8962 }
Romain Guy06882f82009-06-10 13:36:04 -07008963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008964 if (oldHold != newHold) {
8965 try {
8966 if (oldHold != null) {
8967 mBatteryStats.noteStopWakelock(oldHold.mUid,
8968 "window",
8969 BatteryStats.WAKE_TYPE_WINDOW);
8970 }
8971 if (newHold != null) {
8972 mBatteryStats.noteStartWakelock(newHold.mUid,
8973 "window",
8974 BatteryStats.WAKE_TYPE_WINDOW);
8975 }
8976 } catch (RemoteException e) {
8977 }
8978 }
8979 break;
8980 }
Romain Guy06882f82009-06-10 13:36:04 -07008981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008982 case APP_TRANSITION_TIMEOUT: {
8983 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008984 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008985 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8986 "*** APP TRANSITION TIMEOUT");
8987 mAppTransitionReady = true;
8988 mAppTransitionTimeout = true;
8989 performLayoutAndPlaceSurfacesLocked();
8990 }
8991 }
8992 break;
8993 }
Romain Guy06882f82009-06-10 13:36:04 -07008994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008995 case PERSIST_ANIMATION_SCALE: {
8996 Settings.System.putFloat(mContext.getContentResolver(),
8997 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8998 Settings.System.putFloat(mContext.getContentResolver(),
8999 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
9000 break;
9001 }
Romain Guy06882f82009-06-10 13:36:04 -07009002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009003 case FORCE_GC: {
9004 synchronized(mWindowMap) {
9005 if (mAnimationPending) {
9006 // If we are animating, don't do the gc now but
9007 // delay a bit so we don't interrupt the animation.
9008 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9009 2000);
9010 return;
9011 }
9012 // If we are currently rotating the display, it will
9013 // schedule a new message when done.
9014 if (mDisplayFrozen) {
9015 return;
9016 }
9017 mFreezeGcPending = 0;
9018 }
9019 Runtime.getRuntime().gc();
9020 break;
9021 }
Romain Guy06882f82009-06-10 13:36:04 -07009022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009023 case ENABLE_SCREEN: {
9024 performEnableScreen();
9025 break;
9026 }
Romain Guy06882f82009-06-10 13:36:04 -07009027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 case APP_FREEZE_TIMEOUT: {
9029 synchronized (mWindowMap) {
9030 Log.w(TAG, "App freeze timeout expired.");
9031 int i = mAppTokens.size();
9032 while (i > 0) {
9033 i--;
9034 AppWindowToken tok = mAppTokens.get(i);
9035 if (tok.freezingScreen) {
9036 Log.w(TAG, "Force clearing freeze: " + tok);
9037 unsetAppFreezingScreenLocked(tok, true, true);
9038 }
9039 }
9040 }
9041 break;
9042 }
Romain Guy06882f82009-06-10 13:36:04 -07009043
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009044 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07009045 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009046 sendNewConfiguration();
9047 }
9048 break;
9049 }
Romain Guy06882f82009-06-10 13:36:04 -07009050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009051 }
9052 }
9053 }
9054
9055 // -------------------------------------------------------------
9056 // IWindowManager API
9057 // -------------------------------------------------------------
9058
9059 public IWindowSession openSession(IInputMethodClient client,
9060 IInputContext inputContext) {
9061 if (client == null) throw new IllegalArgumentException("null client");
9062 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9063 return new Session(client, inputContext);
9064 }
9065
9066 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9067 synchronized (mWindowMap) {
9068 // The focus for the client is the window immediately below
9069 // where we would place the input method window.
9070 int idx = findDesiredInputMethodWindowIndexLocked(false);
9071 WindowState imFocus;
9072 if (idx > 0) {
9073 imFocus = (WindowState)mWindows.get(idx-1);
9074 if (imFocus != null) {
9075 if (imFocus.mSession.mClient != null &&
9076 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9077 return true;
9078 }
9079 }
9080 }
9081 }
9082 return false;
9083 }
Romain Guy06882f82009-06-10 13:36:04 -07009084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009085 // -------------------------------------------------------------
9086 // Internals
9087 // -------------------------------------------------------------
9088
9089 final WindowState windowForClientLocked(Session session, IWindow client) {
9090 return windowForClientLocked(session, client.asBinder());
9091 }
Romain Guy06882f82009-06-10 13:36:04 -07009092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009093 final WindowState windowForClientLocked(Session session, IBinder client) {
9094 WindowState win = mWindowMap.get(client);
9095 if (localLOGV) Log.v(
9096 TAG, "Looking up client " + client + ": " + win);
9097 if (win == null) {
9098 RuntimeException ex = new RuntimeException();
9099 Log.w(TAG, "Requested window " + client + " does not exist", ex);
9100 return null;
9101 }
9102 if (session != null && win.mSession != session) {
9103 RuntimeException ex = new RuntimeException();
9104 Log.w(TAG, "Requested window " + client + " is in session " +
9105 win.mSession + ", not " + session, ex);
9106 return null;
9107 }
9108
9109 return win;
9110 }
9111
Dianne Hackborna8f60182009-09-01 19:01:50 -07009112 final void rebuildAppWindowListLocked() {
9113 int NW = mWindows.size();
9114 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009115 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009116 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009117
9118 // First remove all existing app windows.
9119 i=0;
9120 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009121 WindowState w = (WindowState)mWindows.get(i);
9122 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009123 WindowState win = (WindowState)mWindows.remove(i);
9124 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
9125 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009126 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009127 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009128 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009129 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9130 && lastWallpaper == i-1) {
9131 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009132 }
9133 i++;
9134 }
9135
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009136 // The wallpaper window(s) typically live at the bottom of the stack,
9137 // so skip them before adding app tokens.
9138 lastWallpaper++;
9139 i = lastWallpaper;
9140
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009141 // First add all of the exiting app tokens... these are no longer
9142 // in the main app list, but still have windows shown. We put them
9143 // in the back because now that the animation is over we no longer
9144 // will care about them.
9145 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009146 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009147 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9148 }
9149
9150 // And add in the still active app tokens in Z order.
9151 NT = mAppTokens.size();
9152 for (int j=0; j<NT; j++) {
9153 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009154 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009155
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009156 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009157 if (i != numRemoved) {
9158 Log.w(TAG, "Rebuild removed " + numRemoved
9159 + " windows but added " + i);
9160 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009161 }
9162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009163 private final void assignLayersLocked() {
9164 int N = mWindows.size();
9165 int curBaseLayer = 0;
9166 int curLayer = 0;
9167 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009169 for (i=0; i<N; i++) {
9170 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009171 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9172 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 curLayer += WINDOW_LAYER_MULTIPLIER;
9174 w.mLayer = curLayer;
9175 } else {
9176 curBaseLayer = curLayer = w.mBaseLayer;
9177 w.mLayer = curLayer;
9178 }
9179 if (w.mTargetAppToken != null) {
9180 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9181 } else if (w.mAppToken != null) {
9182 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9183 } else {
9184 w.mAnimLayer = w.mLayer;
9185 }
9186 if (w.mIsImWindow) {
9187 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009188 } else if (w.mIsWallpaper) {
9189 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009190 }
9191 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9192 + w.mAnimLayer);
9193 //System.out.println(
9194 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9195 }
9196 }
9197
9198 private boolean mInLayout = false;
9199 private final void performLayoutAndPlaceSurfacesLocked() {
9200 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009201 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009202 throw new RuntimeException("Recursive call!");
9203 }
9204 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9205 return;
9206 }
9207
9208 boolean recoveringMemory = false;
9209 if (mForceRemoves != null) {
9210 recoveringMemory = true;
9211 // Wait a little it for things to settle down, and off we go.
9212 for (int i=0; i<mForceRemoves.size(); i++) {
9213 WindowState ws = mForceRemoves.get(i);
9214 Log.i(TAG, "Force removing: " + ws);
9215 removeWindowInnerLocked(ws.mSession, ws);
9216 }
9217 mForceRemoves = null;
9218 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9219 Object tmp = new Object();
9220 synchronized (tmp) {
9221 try {
9222 tmp.wait(250);
9223 } catch (InterruptedException e) {
9224 }
9225 }
9226 }
Romain Guy06882f82009-06-10 13:36:04 -07009227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 mInLayout = true;
9229 try {
9230 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009232 int i = mPendingRemove.size()-1;
9233 if (i >= 0) {
9234 while (i >= 0) {
9235 WindowState w = mPendingRemove.get(i);
9236 removeWindowInnerLocked(w.mSession, w);
9237 i--;
9238 }
9239 mPendingRemove.clear();
9240
9241 mInLayout = false;
9242 assignLayersLocked();
9243 mLayoutNeeded = true;
9244 performLayoutAndPlaceSurfacesLocked();
9245
9246 } else {
9247 mInLayout = false;
9248 if (mLayoutNeeded) {
9249 requestAnimationLocked(0);
9250 }
9251 }
9252 } catch (RuntimeException e) {
9253 mInLayout = false;
9254 Log.e(TAG, "Unhandled exception while layout out windows", e);
9255 }
9256 }
9257
9258 private final void performLayoutLockedInner() {
9259 final int dw = mDisplay.getWidth();
9260 final int dh = mDisplay.getHeight();
9261
9262 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009263 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009264 int i;
9265
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009266 if (DEBUG_LAYOUT) Log.v(TAG, "performLayout: needed="
9267 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009269 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009270
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009271 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009272 mPolicy.beginLayoutLw(dw, dh);
9273
9274 // First perform layout of any root windows (not attached
9275 // to another window).
9276 int topAttached = -1;
9277 for (i = N-1; i >= 0; i--) {
9278 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009279
9280 // Don't do layout of a window if it is not visible, or
9281 // soon won't be visible, to avoid wasting time and funky
9282 // changes while a window is animating away.
9283 final AppWindowToken atoken = win.mAppToken;
9284 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009285 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009286 || win.mRootToken.hidden
9287 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009288 || win.mAttachedHidden
9289 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009290
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009291 if (win.mLayoutAttached) {
9292 if (DEBUG_LAYOUT) Log.v(TAG, "First pass " + win
9293 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9294 + " mLayoutAttached=" + win.mLayoutAttached);
9295 if (DEBUG_LAYOUT && gone) Log.v(TAG, " (mViewVisibility="
9296 + win.mViewVisibility + " mRelayoutCalled="
9297 + win.mRelayoutCalled + " hidden="
9298 + win.mRootToken.hidden + " hiddenRequested="
9299 + (atoken != null && atoken.hiddenRequested)
9300 + " mAttachedHidden=" + win.mAttachedHidden);
9301 }
9302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009303 // If this view is GONE, then skip it -- keep the current
9304 // frame, and let the caller know so they can ignore it
9305 // if they want. (We do the normal layout for INVISIBLE
9306 // windows, since that means "perform layout as normal,
9307 // just don't display").
9308 if (!gone || !win.mHaveFrame) {
9309 if (!win.mLayoutAttached) {
9310 mPolicy.layoutWindowLw(win, win.mAttrs, null);
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009311 if (DEBUG_LAYOUT) Log.v(TAG, "-> mFrame="
9312 + win.mFrame + " mContainingFrame="
9313 + win.mContainingFrame + " mDisplayFrame="
9314 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009315 } else {
9316 if (topAttached < 0) topAttached = i;
9317 }
9318 }
9319 }
Romain Guy06882f82009-06-10 13:36:04 -07009320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009321 // Now perform layout of attached windows, which usually
9322 // depend on the position of the window they are attached to.
9323 // XXX does not deal with windows that are attached to windows
9324 // that are themselves attached.
9325 for (i = topAttached; i >= 0; i--) {
9326 WindowState win = (WindowState) mWindows.get(i);
9327
9328 // If this view is GONE, then skip it -- keep the current
9329 // frame, and let the caller know so they can ignore it
9330 // if they want. (We do the normal layout for INVISIBLE
9331 // windows, since that means "perform layout as normal,
9332 // just don't display").
9333 if (win.mLayoutAttached) {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009334 if (DEBUG_LAYOUT) Log.v(TAG, "Second pass " + win
9335 + " mHaveFrame=" + win.mHaveFrame
9336 + " mViewVisibility=" + win.mViewVisibility
9337 + " mRelayoutCalled=" + win.mRelayoutCalled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009338 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9339 || !win.mHaveFrame) {
9340 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009341 if (DEBUG_LAYOUT) Log.v(TAG, "-> mFrame="
9342 + win.mFrame + " mContainingFrame="
9343 + win.mContainingFrame + " mDisplayFrame="
9344 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009345 }
9346 }
9347 }
9348
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009349 int changes = mPolicy.finishLayoutLw();
9350 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9351 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9352 assignLayersLocked();
9353 }
9354 }
9355 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009356 mLayoutNeeded = false;
9357 } else if (repeats > 2) {
9358 Log.w(TAG, "Layout repeat aborted after too many iterations");
9359 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009360 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9361 Configuration newConfig = updateOrientationFromAppTokensLocked(
9362 null, null);
9363 if (newConfig != null) {
9364 mLayoutNeeded = true;
9365 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9366 }
9367 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009368 } else {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009369 if (DEBUG_LAYOUT) Log.v(TAG, "Repeating layout because changes=0x"
9370 + Integer.toHexString(changes));
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009371 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009372 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009373 if (DEBUG_LAYOUT) Log.v(TAG, "Computing new config from layout");
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009374 Configuration newConfig = updateOrientationFromAppTokensLocked(
9375 null, null);
9376 if (newConfig != null) {
9377 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9378 }
9379 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009381 }
9382 }
Romain Guy06882f82009-06-10 13:36:04 -07009383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009384 private final void performLayoutAndPlaceSurfacesLockedInner(
9385 boolean recoveringMemory) {
9386 final long currentTime = SystemClock.uptimeMillis();
9387 final int dw = mDisplay.getWidth();
9388 final int dh = mDisplay.getHeight();
9389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009390 int i;
9391
9392 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009393 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009395 if (mFxSession == null) {
9396 mFxSession = new SurfaceSession();
9397 }
Romain Guy06882f82009-06-10 13:36:04 -07009398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009399 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9400
9401 // Initialize state of exiting tokens.
9402 for (i=mExitingTokens.size()-1; i>=0; i--) {
9403 mExitingTokens.get(i).hasVisible = false;
9404 }
9405
9406 // Initialize state of exiting applications.
9407 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9408 mExitingAppTokens.get(i).hasVisible = false;
9409 }
9410
9411 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009412 boolean orientationChangeComplete = true;
9413 Session holdScreen = null;
9414 float screenBrightness = -1;
9415 boolean focusDisplayed = false;
9416 boolean animating = false;
9417
9418 Surface.openTransaction();
9419 try {
9420 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009421 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009422
9423 do {
9424 final int transactionSequence = ++mTransactionSequence;
9425
9426 // Update animations of all applications, including those
9427 // associated with exiting/removed apps
9428 boolean tokensAnimating = false;
9429 final int NAT = mAppTokens.size();
9430 for (i=0; i<NAT; i++) {
9431 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9432 tokensAnimating = true;
9433 }
9434 }
9435 final int NEAT = mExitingAppTokens.size();
9436 for (i=0; i<NEAT; i++) {
9437 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9438 tokensAnimating = true;
9439 }
9440 }
9441
9442 animating = tokensAnimating;
9443 restart = false;
9444
9445 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009446 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009447 boolean focusMayChange = false;
9448 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009449
9450 mPolicy.beginAnimationLw(dw, dh);
9451
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009452 final int N = mWindows.size();
9453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009454 for (i=N-1; i>=0; i--) {
9455 WindowState w = (WindowState)mWindows.get(i);
9456
9457 final WindowManager.LayoutParams attrs = w.mAttrs;
9458
9459 if (w.mSurface != null) {
9460 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009461 if (w.commitFinishDrawingLocked(currentTime)) {
9462 if ((w.mAttrs.flags
9463 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009464 if (DEBUG_WALLPAPER) Log.v(TAG,
9465 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009466 wallpaperMayChange = true;
9467 }
9468 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009469
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009470 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009471 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9472 animating = true;
9473 //w.dump(" ");
9474 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009475 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9476 wallpaperMayChange = true;
9477 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009478
9479 if (mPolicy.doesForceHide(w, attrs)) {
9480 if (!wasAnimating && animating) {
9481 wallpaperForceHidingChanged = true;
9482 focusMayChange = true;
9483 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9484 forceHiding = true;
9485 }
9486 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9487 boolean changed;
9488 if (forceHiding) {
9489 changed = w.hideLw(false, false);
9490 } else {
9491 changed = w.showLw(false, false);
9492 if (changed && wallpaperForceHidingChanged
9493 && w.isReadyForDisplay()) {
9494 // Assume we will need to animate. If
9495 // we don't (because the wallpaper will
9496 // stay with the lock screen), then we will
9497 // clean up later.
9498 Animation a = mPolicy.createForceHideEnterAnimation();
9499 if (a != null) {
9500 w.setAnimation(a);
9501 }
9502 }
9503 }
9504 if (changed && (attrs.flags
9505 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9506 wallpaperMayChange = true;
9507 }
Christopher Tate405d0892009-10-27 20:23:28 -07009508 if (changed && !forceHiding
9509 && (mCurrentFocus == null)
9510 && (mFocusedApp != null)) {
9511 // It's possible that the last focus recalculation left no
9512 // current focused window even though the app has come to the
9513 // foreground already. In this case, we make sure to recalculate
9514 // focus when we show a window.
9515 focusMayChange = true;
9516 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009517 }
9518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009519 mPolicy.animatingWindowLw(w, attrs);
9520 }
9521
9522 final AppWindowToken atoken = w.mAppToken;
9523 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9524 if (atoken.lastTransactionSequence != transactionSequence) {
9525 atoken.lastTransactionSequence = transactionSequence;
9526 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9527 atoken.startingDisplayed = false;
9528 }
9529 if ((w.isOnScreen() || w.mAttrs.type
9530 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9531 && !w.mExiting && !w.mDestroying) {
9532 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009533 Log.v(TAG, "Eval win " + w + ": isDrawn="
9534 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009535 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009536 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009537 Log.v(TAG, "Not displayed: s=" + w.mSurface
9538 + " pv=" + w.mPolicyVisibility
9539 + " dp=" + w.mDrawPending
9540 + " cdp=" + w.mCommitDrawPending
9541 + " ah=" + w.mAttachedHidden
9542 + " th=" + atoken.hiddenRequested
9543 + " a=" + w.mAnimating);
9544 }
9545 }
9546 if (w != atoken.startingWindow) {
9547 if (!atoken.freezingScreen || !w.mAppFreezing) {
9548 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009549 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009550 atoken.numDrawnWindows++;
9551 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9552 "tokenMayBeDrawn: " + atoken
9553 + " freezingScreen=" + atoken.freezingScreen
9554 + " mAppFreezing=" + w.mAppFreezing);
9555 tokenMayBeDrawn = true;
9556 }
9557 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009558 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009559 atoken.startingDisplayed = true;
9560 }
9561 }
9562 } else if (w.mReadyToShow) {
9563 w.performShowLocked();
9564 }
9565 }
9566
9567 if (mPolicy.finishAnimationLw()) {
9568 restart = true;
9569 }
9570
9571 if (tokenMayBeDrawn) {
9572 // See if any windows have been drawn, so they (and others
9573 // associated with them) can now be shown.
9574 final int NT = mTokenList.size();
9575 for (i=0; i<NT; i++) {
9576 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9577 if (wtoken == null) {
9578 continue;
9579 }
9580 if (wtoken.freezingScreen) {
9581 int numInteresting = wtoken.numInterestingWindows;
9582 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9583 if (DEBUG_VISIBILITY) Log.v(TAG,
9584 "allDrawn: " + wtoken
9585 + " interesting=" + numInteresting
9586 + " drawn=" + wtoken.numDrawnWindows);
9587 wtoken.showAllWindowsLocked();
9588 unsetAppFreezingScreenLocked(wtoken, false, true);
9589 orientationChangeComplete = true;
9590 }
9591 } else if (!wtoken.allDrawn) {
9592 int numInteresting = wtoken.numInterestingWindows;
9593 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9594 if (DEBUG_VISIBILITY) Log.v(TAG,
9595 "allDrawn: " + wtoken
9596 + " interesting=" + numInteresting
9597 + " drawn=" + wtoken.numDrawnWindows);
9598 wtoken.allDrawn = true;
9599 restart = true;
9600
9601 // We can now show all of the drawn windows!
9602 if (!mOpeningApps.contains(wtoken)) {
9603 wtoken.showAllWindowsLocked();
9604 }
9605 }
9606 }
9607 }
9608 }
9609
9610 // If we are ready to perform an app transition, check through
9611 // all of the app tokens to be shown and see if they are ready
9612 // to go.
9613 if (mAppTransitionReady) {
9614 int NN = mOpeningApps.size();
9615 boolean goodToGo = true;
9616 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9617 "Checking " + NN + " opening apps (frozen="
9618 + mDisplayFrozen + " timeout="
9619 + mAppTransitionTimeout + ")...");
9620 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9621 // If the display isn't frozen, wait to do anything until
9622 // all of the apps are ready. Otherwise just go because
9623 // we'll unfreeze the display when everyone is ready.
9624 for (i=0; i<NN && goodToGo; i++) {
9625 AppWindowToken wtoken = mOpeningApps.get(i);
9626 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9627 "Check opening app" + wtoken + ": allDrawn="
9628 + wtoken.allDrawn + " startingDisplayed="
9629 + wtoken.startingDisplayed);
9630 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9631 && !wtoken.startingMoved) {
9632 goodToGo = false;
9633 }
9634 }
9635 }
9636 if (goodToGo) {
9637 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9638 int transit = mNextAppTransition;
9639 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009640 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009641 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009642 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009643 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009644 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009645 mAppTransitionTimeout = false;
9646 mStartingIconInTransition = false;
9647 mSkipAppTransitionAnimation = false;
9648
9649 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9650
Dianne Hackborna8f60182009-09-01 19:01:50 -07009651 // If there are applications waiting to come to the
9652 // top of the stack, now is the time to move their windows.
9653 // (Note that we don't do apps going to the bottom
9654 // here -- we want to keep their windows in the old
9655 // Z-order until the animation completes.)
9656 if (mToTopApps.size() > 0) {
9657 NN = mAppTokens.size();
9658 for (i=0; i<NN; i++) {
9659 AppWindowToken wtoken = mAppTokens.get(i);
9660 if (wtoken.sendingToTop) {
9661 wtoken.sendingToTop = false;
9662 moveAppWindowsLocked(wtoken, NN, false);
9663 }
9664 }
9665 mToTopApps.clear();
9666 }
9667
Dianne Hackborn25994b42009-09-04 14:21:19 -07009668 WindowState oldWallpaper = mWallpaperTarget;
9669
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009670 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009671 wallpaperMayChange = false;
9672
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009673 // The top-most window will supply the layout params,
9674 // and we will determine it below.
9675 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009676 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009677 int bestAnimLayer = -1;
9678
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009679 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009680 "New wallpaper target=" + mWallpaperTarget
9681 + ", lower target=" + mLowerWallpaperTarget
9682 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009683 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009684 // Do a first pass through the tokens for two
9685 // things:
9686 // (1) Determine if both the closing and opening
9687 // app token sets are wallpaper targets, in which
9688 // case special animations are needed
9689 // (since the wallpaper needs to stay static
9690 // behind them).
9691 // (2) Find the layout params of the top-most
9692 // application window in the tokens, which is
9693 // what will control the animation theme.
9694 final int NC = mClosingApps.size();
9695 NN = NC + mOpeningApps.size();
9696 for (i=0; i<NN; i++) {
9697 AppWindowToken wtoken;
9698 int mode;
9699 if (i < NC) {
9700 wtoken = mClosingApps.get(i);
9701 mode = 1;
9702 } else {
9703 wtoken = mOpeningApps.get(i-NC);
9704 mode = 2;
9705 }
9706 if (mLowerWallpaperTarget != null) {
9707 if (mLowerWallpaperTarget.mAppToken == wtoken
9708 || mUpperWallpaperTarget.mAppToken == wtoken) {
9709 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009710 }
9711 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009712 if (wtoken.appFullscreen) {
9713 WindowState ws = wtoken.findMainWindow();
9714 if (ws != null) {
9715 // If this is a compatibility mode
9716 // window, we will always use its anim.
9717 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9718 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009719 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009720 bestAnimLayer = Integer.MAX_VALUE;
9721 } else if (ws.mLayer > bestAnimLayer) {
9722 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009723 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009724 bestAnimLayer = ws.mLayer;
9725 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009726 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009727 }
9728 }
9729
Dianne Hackborn25994b42009-09-04 14:21:19 -07009730 if (foundWallpapers == 3) {
9731 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9732 "Wallpaper animation!");
9733 switch (transit) {
9734 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9735 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9736 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9737 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9738 break;
9739 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9740 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9741 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9742 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9743 break;
9744 }
9745 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9746 "New transit: " + transit);
9747 } else if (oldWallpaper != null) {
9748 // We are transitioning from an activity with
9749 // a wallpaper to one without.
9750 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9751 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9752 "New transit away from wallpaper: " + transit);
9753 } else if (mWallpaperTarget != null) {
9754 // We are transitioning from an activity without
9755 // a wallpaper to now showing the wallpaper
9756 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9757 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9758 "New transit into wallpaper: " + transit);
9759 }
9760
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009761 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9762 mLastEnterAnimToken = animToken;
9763 mLastEnterAnimParams = animLp;
9764 } else if (mLastEnterAnimParams != null) {
9765 animLp = mLastEnterAnimParams;
9766 mLastEnterAnimToken = null;
9767 mLastEnterAnimParams = null;
9768 }
9769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009770 NN = mOpeningApps.size();
9771 for (i=0; i<NN; i++) {
9772 AppWindowToken wtoken = mOpeningApps.get(i);
9773 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9774 "Now opening app" + wtoken);
9775 wtoken.reportedVisible = false;
9776 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009777 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009778 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009779 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009780 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009781 wtoken.showAllWindowsLocked();
9782 }
9783 NN = mClosingApps.size();
9784 for (i=0; i<NN; i++) {
9785 AppWindowToken wtoken = mClosingApps.get(i);
9786 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9787 "Now closing app" + wtoken);
9788 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009789 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009790 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009791 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009792 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009793 // Force the allDrawn flag, because we want to start
9794 // this guy's animations regardless of whether it's
9795 // gotten drawn.
9796 wtoken.allDrawn = true;
9797 }
9798
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009799 mNextAppTransitionPackage = null;
9800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009801 mOpeningApps.clear();
9802 mClosingApps.clear();
9803
9804 // This has changed the visibility of windows, so perform
9805 // a new layout to get them all up-to-date.
9806 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009807 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9808 assignLayersLocked();
9809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810 performLayoutLockedInner();
9811 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009812 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009813
9814 restart = true;
9815 }
9816 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009817
Dianne Hackborna8f60182009-09-01 19:01:50 -07009818 if (!animating && mAppTransitionRunning) {
9819 // We have finished the animation of an app transition. To do
9820 // this, we have delayed a lot of operations like showing and
9821 // hiding apps, moving apps in Z-order, etc. The app token list
9822 // reflects the correct Z-order, but the window list may now
9823 // be out of sync with it. So here we will just rebuild the
9824 // entire app window list. Fun!
9825 mAppTransitionRunning = false;
9826 // Clear information about apps that were moving.
9827 mToBottomApps.clear();
9828
9829 rebuildAppWindowListLocked();
9830 restart = true;
9831 moveInputMethodWindowsIfNeededLocked(false);
9832 wallpaperMayChange = true;
9833 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009834 // Since the window list has been rebuilt, focus might
9835 // have to be recomputed since the actual order of windows
9836 // might have changed again.
9837 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009838 }
9839
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009840 int adjResult = 0;
9841
9842 if (wallpaperForceHidingChanged) {
9843 // At this point, there was a window with a wallpaper that
9844 // was force hiding other windows behind it, but now it
9845 // is going away. This may be simple -- just animate
9846 // away the wallpaper and its window -- or it may be
9847 // hard -- the wallpaper now needs to be shown behind
9848 // something that was hidden.
9849 WindowState oldWallpaper = mWallpaperTarget;
9850 adjResult = adjustWallpaperWindowsLocked();
9851 wallpaperMayChange = false;
9852 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9853 + " NEW: " + mWallpaperTarget);
9854 if (mLowerWallpaperTarget == null) {
9855 // Whoops, we don't need a special wallpaper animation.
9856 // Clear them out.
9857 forceHiding = false;
9858 for (i=N-1; i>=0; i--) {
9859 WindowState w = (WindowState)mWindows.get(i);
9860 if (w.mSurface != null) {
9861 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009862 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9863 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009864 forceHiding = true;
9865 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9866 if (!w.mAnimating) {
9867 // We set the animation above so it
9868 // is not yet running.
9869 w.clearAnimation();
9870 }
9871 }
9872 }
9873 }
9874 }
9875 }
9876
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009877 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009878 if (DEBUG_WALLPAPER) Log.v(TAG,
9879 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009880 adjResult = adjustWallpaperWindowsLocked();
9881 }
9882
9883 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9884 if (DEBUG_WALLPAPER) Log.v(TAG,
9885 "Wallpaper layer changed: assigning layers + relayout");
9886 restart = true;
9887 mLayoutNeeded = true;
9888 assignLayersLocked();
9889 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9890 if (DEBUG_WALLPAPER) Log.v(TAG,
9891 "Wallpaper visibility changed: relayout");
9892 restart = true;
9893 mLayoutNeeded = true;
9894 }
9895
9896 if (focusMayChange) {
9897 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009898 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009899 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009900 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009901 }
9902
9903 if (mLayoutNeeded) {
9904 restart = true;
9905 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009906 }
9907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009908 } while (restart);
9909
9910 // THIRD LOOP: Update the surfaces of all windows.
9911
9912 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9913
9914 boolean obscured = false;
9915 boolean blurring = false;
9916 boolean dimming = false;
9917 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009918 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009919 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009920
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009921 final int N = mWindows.size();
9922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009923 for (i=N-1; i>=0; i--) {
9924 WindowState w = (WindowState)mWindows.get(i);
9925
9926 boolean displayed = false;
9927 final WindowManager.LayoutParams attrs = w.mAttrs;
9928 final int attrFlags = attrs.flags;
9929
9930 if (w.mSurface != null) {
9931 w.computeShownFrameLocked();
9932 if (localLOGV) Log.v(
9933 TAG, "Placing surface #" + i + " " + w.mSurface
9934 + ": new=" + w.mShownFrame + ", old="
9935 + w.mLastShownFrame);
9936
9937 boolean resize;
9938 int width, height;
9939 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9940 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9941 w.mLastRequestedHeight != w.mRequestedHeight;
9942 // for a scaled surface, we just want to use
9943 // the requested size.
9944 width = w.mRequestedWidth;
9945 height = w.mRequestedHeight;
9946 w.mLastRequestedWidth = width;
9947 w.mLastRequestedHeight = height;
9948 w.mLastShownFrame.set(w.mShownFrame);
9949 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009950 if (SHOW_TRANSACTIONS) Log.i(
9951 TAG, " SURFACE " + w.mSurface
9952 + ": POS " + w.mShownFrame.left
9953 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9955 } catch (RuntimeException e) {
9956 Log.w(TAG, "Error positioning surface in " + w, e);
9957 if (!recoveringMemory) {
9958 reclaimSomeSurfaceMemoryLocked(w, "position");
9959 }
9960 }
9961 } else {
9962 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9963 width = w.mShownFrame.width();
9964 height = w.mShownFrame.height();
9965 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009966 }
9967
9968 if (resize) {
9969 if (width < 1) width = 1;
9970 if (height < 1) height = 1;
9971 if (w.mSurface != null) {
9972 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009973 if (SHOW_TRANSACTIONS) Log.i(
9974 TAG, " SURFACE " + w.mSurface + ": POS "
9975 + w.mShownFrame.left + ","
9976 + w.mShownFrame.top + " SIZE "
9977 + w.mShownFrame.width() + "x"
9978 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009979 w.mSurface.setSize(width, height);
9980 w.mSurface.setPosition(w.mShownFrame.left,
9981 w.mShownFrame.top);
9982 } catch (RuntimeException e) {
9983 // If something goes wrong with the surface (such
9984 // as running out of memory), don't take down the
9985 // entire system.
9986 Log.e(TAG, "Failure updating surface of " + w
9987 + "size=(" + width + "x" + height
9988 + "), pos=(" + w.mShownFrame.left
9989 + "," + w.mShownFrame.top + ")", e);
9990 if (!recoveringMemory) {
9991 reclaimSomeSurfaceMemoryLocked(w, "size");
9992 }
9993 }
9994 }
9995 }
9996 if (!w.mAppFreezing) {
9997 w.mContentInsetsChanged =
9998 !w.mLastContentInsets.equals(w.mContentInsets);
9999 w.mVisibleInsetsChanged =
10000 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -070010001 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002 || w.mContentInsetsChanged
10003 || w.mVisibleInsetsChanged) {
10004 w.mLastFrame.set(w.mFrame);
10005 w.mLastContentInsets.set(w.mContentInsets);
10006 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010007 // If the screen is currently frozen, then keep
10008 // it frozen until this window draws at its new
10009 // orientation.
10010 if (mDisplayFrozen) {
10011 if (DEBUG_ORIENTATION) Log.v(TAG,
10012 "Resizing while display frozen: " + w);
10013 w.mOrientationChanging = true;
10014 if (mWindowsFreezingScreen) {
10015 mWindowsFreezingScreen = true;
10016 // XXX should probably keep timeout from
10017 // when we first froze the display.
10018 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10019 mH.sendMessageDelayed(mH.obtainMessage(
10020 H.WINDOW_FREEZE_TIMEOUT), 2000);
10021 }
10022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010023 // If the orientation is changing, then we need to
10024 // hold off on unfreezing the display until this
10025 // window has been redrawn; to do that, we need
10026 // to go through the process of getting informed
10027 // by the application when it has finished drawing.
10028 if (w.mOrientationChanging) {
10029 if (DEBUG_ORIENTATION) Log.v(TAG,
10030 "Orientation start waiting for draw in "
10031 + w + ", surface " + w.mSurface);
10032 w.mDrawPending = true;
10033 w.mCommitDrawPending = false;
10034 w.mReadyToShow = false;
10035 if (w.mAppToken != null) {
10036 w.mAppToken.allDrawn = false;
10037 }
10038 }
Romain Guy06882f82009-06-10 13:36:04 -070010039 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010040 "Resizing window " + w + " to " + w.mFrame);
10041 mResizingWindows.add(w);
10042 } else if (w.mOrientationChanging) {
10043 if (!w.mDrawPending && !w.mCommitDrawPending) {
10044 if (DEBUG_ORIENTATION) Log.v(TAG,
10045 "Orientation not waiting for draw in "
10046 + w + ", surface " + w.mSurface);
10047 w.mOrientationChanging = false;
10048 }
10049 }
10050 }
10051
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010052 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010053 if (!w.mLastHidden) {
10054 //dump();
10055 w.mLastHidden = true;
10056 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010057 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010058 if (w.mSurface != null) {
10059 try {
10060 w.mSurface.hide();
10061 } catch (RuntimeException e) {
10062 Log.w(TAG, "Exception hiding surface in " + w);
10063 }
10064 }
10065 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10066 }
10067 // If we are waiting for this window to handle an
10068 // orientation change, well, it is hidden, so
10069 // doesn't really matter. Note that this does
10070 // introduce a potential glitch if the window
10071 // becomes unhidden before it has drawn for the
10072 // new orientation.
10073 if (w.mOrientationChanging) {
10074 w.mOrientationChanging = false;
10075 if (DEBUG_ORIENTATION) Log.v(TAG,
10076 "Orientation change skips hidden " + w);
10077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010078 } else if (w.mLastLayer != w.mAnimLayer
10079 || w.mLastAlpha != w.mShownAlpha
10080 || w.mLastDsDx != w.mDsDx
10081 || w.mLastDtDx != w.mDtDx
10082 || w.mLastDsDy != w.mDsDy
10083 || w.mLastDtDy != w.mDtDy
10084 || w.mLastHScale != w.mHScale
10085 || w.mLastVScale != w.mVScale
10086 || w.mLastHidden) {
10087 displayed = true;
10088 w.mLastAlpha = w.mShownAlpha;
10089 w.mLastLayer = w.mAnimLayer;
10090 w.mLastDsDx = w.mDsDx;
10091 w.mLastDtDx = w.mDtDx;
10092 w.mLastDsDy = w.mDsDy;
10093 w.mLastDtDy = w.mDtDy;
10094 w.mLastHScale = w.mHScale;
10095 w.mLastVScale = w.mVScale;
10096 if (SHOW_TRANSACTIONS) Log.i(
10097 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010098 + w.mShownAlpha + " layer=" + w.mAnimLayer
10099 + " matrix=[" + (w.mDsDx*w.mHScale)
10100 + "," + (w.mDtDx*w.mVScale)
10101 + "][" + (w.mDsDy*w.mHScale)
10102 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010103 if (w.mSurface != null) {
10104 try {
10105 w.mSurface.setAlpha(w.mShownAlpha);
10106 w.mSurface.setLayer(w.mAnimLayer);
10107 w.mSurface.setMatrix(
10108 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10109 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10110 } catch (RuntimeException e) {
10111 Log.w(TAG, "Error updating surface in " + w, e);
10112 if (!recoveringMemory) {
10113 reclaimSomeSurfaceMemoryLocked(w, "update");
10114 }
10115 }
10116 }
10117
10118 if (w.mLastHidden && !w.mDrawPending
10119 && !w.mCommitDrawPending
10120 && !w.mReadyToShow) {
10121 if (SHOW_TRANSACTIONS) Log.i(
10122 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
10123 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
10124 + " during relayout");
10125 if (showSurfaceRobustlyLocked(w)) {
10126 w.mHasDrawn = true;
10127 w.mLastHidden = false;
10128 } else {
10129 w.mOrientationChanging = false;
10130 }
10131 }
10132 if (w.mSurface != null) {
10133 w.mToken.hasVisible = true;
10134 }
10135 } else {
10136 displayed = true;
10137 }
10138
10139 if (displayed) {
10140 if (!covered) {
10141 if (attrs.width == LayoutParams.FILL_PARENT
10142 && attrs.height == LayoutParams.FILL_PARENT) {
10143 covered = true;
10144 }
10145 }
10146 if (w.mOrientationChanging) {
10147 if (w.mDrawPending || w.mCommitDrawPending) {
10148 orientationChangeComplete = false;
10149 if (DEBUG_ORIENTATION) Log.v(TAG,
10150 "Orientation continue waiting for draw in " + w);
10151 } else {
10152 w.mOrientationChanging = false;
10153 if (DEBUG_ORIENTATION) Log.v(TAG,
10154 "Orientation change complete in " + w);
10155 }
10156 }
10157 w.mToken.hasVisible = true;
10158 }
10159 } else if (w.mOrientationChanging) {
10160 if (DEBUG_ORIENTATION) Log.v(TAG,
10161 "Orientation change skips hidden " + w);
10162 w.mOrientationChanging = false;
10163 }
10164
10165 final boolean canBeSeen = w.isDisplayedLw();
10166
10167 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10168 focusDisplayed = true;
10169 }
10170
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010171 final boolean obscuredChanged = w.mObscured != obscured;
10172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010173 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010174 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010175 if (w.mSurface != null) {
10176 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10177 holdScreen = w.mSession;
10178 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010179 if (!syswin && w.mAttrs.screenBrightness >= 0
10180 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010181 screenBrightness = w.mAttrs.screenBrightness;
10182 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010183 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10184 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10185 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10186 syswin = true;
10187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010189
Dianne Hackborn25994b42009-09-04 14:21:19 -070010190 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10191 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010192 // This window completely covers everything behind it,
10193 // so we want to leave all of them as unblurred (for
10194 // performance reasons).
10195 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010196 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
10197 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010198 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010199 obscured = true;
10200 if (mBackgroundFillerSurface == null) {
10201 try {
10202 mBackgroundFillerSurface = new Surface(mFxSession, 0,
10203 0, dw, dh,
10204 PixelFormat.OPAQUE,
10205 Surface.FX_SURFACE_NORMAL);
10206 } catch (Exception e) {
10207 Log.e(TAG, "Exception creating filler surface", e);
10208 }
10209 }
10210 try {
10211 mBackgroundFillerSurface.setPosition(0, 0);
10212 mBackgroundFillerSurface.setSize(dw, dh);
10213 // Using the same layer as Dim because they will never be shown at the
10214 // same time.
10215 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10216 mBackgroundFillerSurface.show();
10217 } catch (RuntimeException e) {
10218 Log.e(TAG, "Exception showing filler surface");
10219 }
10220 backgroundFillerShown = true;
10221 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010222 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010223 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10224 if (localLOGV) Log.v(TAG, "Win " + w
10225 + ": blurring=" + blurring
10226 + " obscured=" + obscured
10227 + " displayed=" + displayed);
10228 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10229 if (!dimming) {
10230 //Log.i(TAG, "DIM BEHIND: " + w);
10231 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010232 if (mDimAnimator == null) {
10233 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010234 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010235 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010236 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010237 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010238 }
10239 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10240 if (!blurring) {
10241 //Log.i(TAG, "BLUR BEHIND: " + w);
10242 blurring = true;
10243 mBlurShown = true;
10244 if (mBlurSurface == null) {
10245 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10246 + mBlurSurface + ": CREATE");
10247 try {
Romain Guy06882f82009-06-10 13:36:04 -070010248 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010249 -1, 16, 16,
10250 PixelFormat.OPAQUE,
10251 Surface.FX_SURFACE_BLUR);
10252 } catch (Exception e) {
10253 Log.e(TAG, "Exception creating Blur surface", e);
10254 }
10255 }
10256 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10257 + mBlurSurface + ": SHOW pos=(0,0) (" +
10258 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10259 if (mBlurSurface != null) {
10260 mBlurSurface.setPosition(0, 0);
10261 mBlurSurface.setSize(dw, dh);
10262 try {
10263 mBlurSurface.show();
10264 } catch (RuntimeException e) {
10265 Log.w(TAG, "Failure showing blur surface", e);
10266 }
10267 }
10268 }
10269 mBlurSurface.setLayer(w.mAnimLayer-2);
10270 }
10271 }
10272 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010273
10274 if (obscuredChanged && mWallpaperTarget == w) {
10275 // This is the wallpaper target and its obscured state
10276 // changed... make sure the current wallaper's visibility
10277 // has been updated accordingly.
10278 updateWallpaperVisibilityLocked();
10279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010280 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010281
10282 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10283 mBackgroundFillerShown = false;
10284 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10285 try {
10286 mBackgroundFillerSurface.hide();
10287 } catch (RuntimeException e) {
10288 Log.e(TAG, "Exception hiding filler surface", e);
10289 }
10290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010291
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010292 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10293 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010294 }
Romain Guy06882f82009-06-10 13:36:04 -070010295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010296 if (!blurring && mBlurShown) {
10297 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10298 + ": HIDE");
10299 try {
10300 mBlurSurface.hide();
10301 } catch (IllegalArgumentException e) {
10302 Log.w(TAG, "Illegal argument exception hiding blur surface");
10303 }
10304 mBlurShown = false;
10305 }
10306
10307 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10308 } catch (RuntimeException e) {
10309 Log.e(TAG, "Unhandled exception in Window Manager", e);
10310 }
10311
10312 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010314 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10315 "With display frozen, orientationChangeComplete="
10316 + orientationChangeComplete);
10317 if (orientationChangeComplete) {
10318 if (mWindowsFreezingScreen) {
10319 mWindowsFreezingScreen = false;
10320 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10321 }
10322 if (mAppsFreezingScreen == 0) {
10323 stopFreezingDisplayLocked();
10324 }
10325 }
Romain Guy06882f82009-06-10 13:36:04 -070010326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010327 i = mResizingWindows.size();
10328 if (i > 0) {
10329 do {
10330 i--;
10331 WindowState win = mResizingWindows.get(i);
10332 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010333 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10334 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 win.mClient.resized(win.mFrame.width(),
10336 win.mFrame.height(), win.mLastContentInsets,
10337 win.mLastVisibleInsets, win.mDrawPending);
10338 win.mContentInsetsChanged = false;
10339 win.mVisibleInsetsChanged = false;
10340 } catch (RemoteException e) {
10341 win.mOrientationChanging = false;
10342 }
10343 } while (i > 0);
10344 mResizingWindows.clear();
10345 }
Romain Guy06882f82009-06-10 13:36:04 -070010346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010347 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010348 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010349 i = mDestroySurface.size();
10350 if (i > 0) {
10351 do {
10352 i--;
10353 WindowState win = mDestroySurface.get(i);
10354 win.mDestroying = false;
10355 if (mInputMethodWindow == win) {
10356 mInputMethodWindow = null;
10357 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010358 if (win == mWallpaperTarget) {
10359 wallpaperDestroyed = true;
10360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 win.destroySurfaceLocked();
10362 } while (i > 0);
10363 mDestroySurface.clear();
10364 }
10365
10366 // Time to remove any exiting tokens?
10367 for (i=mExitingTokens.size()-1; i>=0; i--) {
10368 WindowToken token = mExitingTokens.get(i);
10369 if (!token.hasVisible) {
10370 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010371 if (token.windowType == TYPE_WALLPAPER) {
10372 mWallpaperTokens.remove(token);
10373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010374 }
10375 }
10376
10377 // Time to remove any exiting applications?
10378 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10379 AppWindowToken token = mExitingAppTokens.get(i);
10380 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010381 // Make sure there is no animation running on this token,
10382 // so any windows associated with it will be removed as
10383 // soon as their animations are complete
10384 token.animation = null;
10385 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386 mAppTokens.remove(token);
10387 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010388 if (mLastEnterAnimToken == token) {
10389 mLastEnterAnimToken = null;
10390 mLastEnterAnimParams = null;
10391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010392 }
10393 }
10394
Dianne Hackborna8f60182009-09-01 19:01:50 -070010395 boolean needRelayout = false;
10396
10397 if (!animating && mAppTransitionRunning) {
10398 // We have finished the animation of an app transition. To do
10399 // this, we have delayed a lot of operations like showing and
10400 // hiding apps, moving apps in Z-order, etc. The app token list
10401 // reflects the correct Z-order, but the window list may now
10402 // be out of sync with it. So here we will just rebuild the
10403 // entire app window list. Fun!
10404 mAppTransitionRunning = false;
10405 needRelayout = true;
10406 rebuildAppWindowListLocked();
10407 // Clear information about apps that were moving.
10408 mToBottomApps.clear();
10409 }
10410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010411 if (focusDisplayed) {
10412 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10413 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010414 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010415 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010416 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010417 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010418 requestAnimationLocked(0);
10419 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10421 }
10422 mQueue.setHoldScreenLocked(holdScreen != null);
10423 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10424 mPowerManager.setScreenBrightnessOverride(-1);
10425 } else {
10426 mPowerManager.setScreenBrightnessOverride((int)
10427 (screenBrightness * Power.BRIGHTNESS_ON));
10428 }
10429 if (holdScreen != mHoldingScreenOn) {
10430 mHoldingScreenOn = holdScreen;
10431 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10432 mH.sendMessage(m);
10433 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010434
10435 if (mTurnOnScreen) {
10436 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10437 LocalPowerManager.BUTTON_EVENT, true);
10438 mTurnOnScreen = false;
10439 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010440
10441 // Check to see if we are now in a state where the screen should
10442 // be enabled, because the window obscured flags have changed.
10443 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 }
10445
10446 void requestAnimationLocked(long delay) {
10447 if (!mAnimationPending) {
10448 mAnimationPending = true;
10449 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10450 }
10451 }
Romain Guy06882f82009-06-10 13:36:04 -070010452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 /**
10454 * Have the surface flinger show a surface, robustly dealing with
10455 * error conditions. In particular, if there is not enough memory
10456 * to show the surface, then we will try to get rid of other surfaces
10457 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010458 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459 * @return Returns true if the surface was successfully shown.
10460 */
10461 boolean showSurfaceRobustlyLocked(WindowState win) {
10462 try {
10463 if (win.mSurface != null) {
10464 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010465 if (win.mTurnOnScreen) {
10466 win.mTurnOnScreen = false;
10467 mTurnOnScreen = true;
10468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010469 }
10470 return true;
10471 } catch (RuntimeException e) {
10472 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10473 }
Romain Guy06882f82009-06-10 13:36:04 -070010474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 return false;
10478 }
Romain Guy06882f82009-06-10 13:36:04 -070010479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10481 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010483 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
10484 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010486 if (mForceRemoves == null) {
10487 mForceRemoves = new ArrayList<WindowState>();
10488 }
Romain Guy06882f82009-06-10 13:36:04 -070010489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010490 long callingIdentity = Binder.clearCallingIdentity();
10491 try {
10492 // There was some problem... first, do a sanity check of the
10493 // window list to make sure we haven't left any dangling surfaces
10494 // around.
10495 int N = mWindows.size();
10496 boolean leakedSurface = false;
10497 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10498 for (int i=0; i<N; i++) {
10499 WindowState ws = (WindowState)mWindows.get(i);
10500 if (ws.mSurface != null) {
10501 if (!mSessions.contains(ws.mSession)) {
10502 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10503 + ws + " surface=" + ws.mSurface
10504 + " token=" + win.mToken
10505 + " pid=" + ws.mSession.mPid
10506 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010507 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010508 ws.mSurface = null;
10509 mForceRemoves.add(ws);
10510 i--;
10511 N--;
10512 leakedSurface = true;
10513 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10514 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10515 + ws + " surface=" + ws.mSurface
10516 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010517 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010518 ws.mSurface = null;
10519 leakedSurface = true;
10520 }
10521 }
10522 }
Romain Guy06882f82009-06-10 13:36:04 -070010523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010524 boolean killedApps = false;
10525 if (!leakedSurface) {
10526 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10527 SparseIntArray pidCandidates = new SparseIntArray();
10528 for (int i=0; i<N; i++) {
10529 WindowState ws = (WindowState)mWindows.get(i);
10530 if (ws.mSurface != null) {
10531 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10532 }
10533 }
10534 if (pidCandidates.size() > 0) {
10535 int[] pids = new int[pidCandidates.size()];
10536 for (int i=0; i<pids.length; i++) {
10537 pids[i] = pidCandidates.keyAt(i);
10538 }
10539 try {
10540 if (mActivityManager.killPidsForMemory(pids)) {
10541 killedApps = true;
10542 }
10543 } catch (RemoteException e) {
10544 }
10545 }
10546 }
Romain Guy06882f82009-06-10 13:36:04 -070010547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010548 if (leakedSurface || killedApps) {
10549 // We managed to reclaim some memory, so get rid of the trouble
10550 // surface and ask the app to request another one.
10551 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10552 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010553 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010554 win.mSurface = null;
10555 }
Romain Guy06882f82009-06-10 13:36:04 -070010556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 try {
10558 win.mClient.dispatchGetNewSurface();
10559 } catch (RemoteException e) {
10560 }
10561 }
10562 } finally {
10563 Binder.restoreCallingIdentity(callingIdentity);
10564 }
10565 }
Romain Guy06882f82009-06-10 13:36:04 -070010566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010567 private boolean updateFocusedWindowLocked(int mode) {
10568 WindowState newFocus = computeFocusedWindowLocked();
10569 if (mCurrentFocus != newFocus) {
10570 // This check makes sure that we don't already have the focus
10571 // change message pending.
10572 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10573 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10574 if (localLOGV) Log.v(
10575 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10576 final WindowState oldFocus = mCurrentFocus;
10577 mCurrentFocus = newFocus;
10578 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010580 final WindowState imWindow = mInputMethodWindow;
10581 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010582 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010584 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10585 mLayoutNeeded = true;
10586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010587 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10588 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010589 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10590 // Client will do the layout, but we need to assign layers
10591 // for handleNewWindowLocked() below.
10592 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010593 }
10594 }
Romain Guy06882f82009-06-10 13:36:04 -070010595
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010596 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10597 mKeyWaiter.handleNewWindowLocked(newFocus);
10598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010599 return true;
10600 }
10601 return false;
10602 }
10603
10604 private WindowState computeFocusedWindowLocked() {
10605 WindowState result = null;
10606 WindowState win;
10607
10608 int i = mWindows.size() - 1;
10609 int nextAppIndex = mAppTokens.size()-1;
10610 WindowToken nextApp = nextAppIndex >= 0
10611 ? mAppTokens.get(nextAppIndex) : null;
10612
10613 while (i >= 0) {
10614 win = (WindowState)mWindows.get(i);
10615
10616 if (localLOGV || DEBUG_FOCUS) Log.v(
10617 TAG, "Looking for focus: " + i
10618 + " = " + win
10619 + ", flags=" + win.mAttrs.flags
10620 + ", canReceive=" + win.canReceiveKeys());
10621
10622 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 // If this window's application has been removed, just skip it.
10625 if (thisApp != null && thisApp.removed) {
10626 i--;
10627 continue;
10628 }
Romain Guy06882f82009-06-10 13:36:04 -070010629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 // If there is a focused app, don't allow focus to go to any
10631 // windows below it. If this is an application window, step
10632 // through the app tokens until we find its app.
10633 if (thisApp != null && nextApp != null && thisApp != nextApp
10634 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10635 int origAppIndex = nextAppIndex;
10636 while (nextAppIndex > 0) {
10637 if (nextApp == mFocusedApp) {
10638 // Whoops, we are below the focused app... no focus
10639 // for you!
10640 if (localLOGV || DEBUG_FOCUS) Log.v(
10641 TAG, "Reached focused app: " + mFocusedApp);
10642 return null;
10643 }
10644 nextAppIndex--;
10645 nextApp = mAppTokens.get(nextAppIndex);
10646 if (nextApp == thisApp) {
10647 break;
10648 }
10649 }
10650 if (thisApp != nextApp) {
10651 // Uh oh, the app token doesn't exist! This shouldn't
10652 // happen, but if it does we can get totally hosed...
10653 // so restart at the original app.
10654 nextAppIndex = origAppIndex;
10655 nextApp = mAppTokens.get(nextAppIndex);
10656 }
10657 }
10658
10659 // Dispatch to this window if it is wants key events.
10660 if (win.canReceiveKeys()) {
10661 if (DEBUG_FOCUS) Log.v(
10662 TAG, "Found focus @ " + i + " = " + win);
10663 result = win;
10664 break;
10665 }
10666
10667 i--;
10668 }
10669
10670 return result;
10671 }
10672
10673 private void startFreezingDisplayLocked() {
10674 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010675 // Freezing the display also suspends key event delivery, to
10676 // keep events from going astray while the display is reconfigured.
10677 // If someone has changed orientation again while the screen is
10678 // still frozen, the events will continue to be blocked while the
10679 // successive orientation change is processed. To prevent spurious
10680 // ANRs, we reset the event dispatch timeout in this case.
10681 synchronized (mKeyWaiter) {
10682 mKeyWaiter.mWasFrozen = true;
10683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010684 return;
10685 }
Romain Guy06882f82009-06-10 13:36:04 -070010686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010687 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 long now = SystemClock.uptimeMillis();
10690 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10691 if (mFreezeGcPending != 0) {
10692 if (now > (mFreezeGcPending+1000)) {
10693 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10694 mH.removeMessages(H.FORCE_GC);
10695 Runtime.getRuntime().gc();
10696 mFreezeGcPending = now;
10697 }
10698 } else {
10699 mFreezeGcPending = now;
10700 }
Romain Guy06882f82009-06-10 13:36:04 -070010701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010703 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10704 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010705 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010706 mAppTransitionReady = true;
10707 }
Romain Guy06882f82009-06-10 13:36:04 -070010708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010709 if (PROFILE_ORIENTATION) {
10710 File file = new File("/data/system/frozen");
10711 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10712 }
10713 Surface.freezeDisplay(0);
10714 }
Romain Guy06882f82009-06-10 13:36:04 -070010715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010716 private void stopFreezingDisplayLocked() {
10717 if (!mDisplayFrozen) {
10718 return;
10719 }
Romain Guy06882f82009-06-10 13:36:04 -070010720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721 mDisplayFrozen = false;
10722 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10723 if (PROFILE_ORIENTATION) {
10724 Debug.stopMethodTracing();
10725 }
10726 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010727
Chris Tate2ad63a92009-03-25 17:36:48 -070010728 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10729 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010730 synchronized (mKeyWaiter) {
10731 mKeyWaiter.mWasFrozen = true;
10732 mKeyWaiter.notifyAll();
10733 }
10734
10735 // A little kludge: a lot could have happened while the
10736 // display was frozen, so now that we are coming back we
10737 // do a gc so that any remote references the system
10738 // processes holds on others can be released if they are
10739 // no longer needed.
10740 mH.removeMessages(H.FORCE_GC);
10741 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10742 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010744 mScreenFrozenLock.release();
10745 }
Romain Guy06882f82009-06-10 13:36:04 -070010746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010747 @Override
10748 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10749 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10750 != PackageManager.PERMISSION_GRANTED) {
10751 pw.println("Permission Denial: can't dump WindowManager from from pid="
10752 + Binder.getCallingPid()
10753 + ", uid=" + Binder.getCallingUid());
10754 return;
10755 }
Romain Guy06882f82009-06-10 13:36:04 -070010756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010757 synchronized(mWindowMap) {
10758 pw.println("Current Window Manager state:");
10759 for (int i=mWindows.size()-1; i>=0; i--) {
10760 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010761 pw.print(" Window #"); pw.print(i); pw.print(' ');
10762 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010763 w.dump(pw, " ");
10764 }
10765 if (mInputMethodDialogs.size() > 0) {
10766 pw.println(" ");
10767 pw.println(" Input method dialogs:");
10768 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10769 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010770 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 }
10772 }
10773 if (mPendingRemove.size() > 0) {
10774 pw.println(" ");
10775 pw.println(" Remove pending for:");
10776 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10777 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010778 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10779 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010780 w.dump(pw, " ");
10781 }
10782 }
10783 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10784 pw.println(" ");
10785 pw.println(" Windows force removing:");
10786 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10787 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010788 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10789 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010790 w.dump(pw, " ");
10791 }
10792 }
10793 if (mDestroySurface.size() > 0) {
10794 pw.println(" ");
10795 pw.println(" Windows waiting to destroy their surface:");
10796 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10797 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010798 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10799 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010800 w.dump(pw, " ");
10801 }
10802 }
10803 if (mLosingFocus.size() > 0) {
10804 pw.println(" ");
10805 pw.println(" Windows losing focus:");
10806 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10807 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010808 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10809 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010810 w.dump(pw, " ");
10811 }
10812 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010813 if (mResizingWindows.size() > 0) {
10814 pw.println(" ");
10815 pw.println(" Windows waiting to resize:");
10816 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10817 WindowState w = mResizingWindows.get(i);
10818 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10819 pw.print(w); pw.println(":");
10820 w.dump(pw, " ");
10821 }
10822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010823 if (mSessions.size() > 0) {
10824 pw.println(" ");
10825 pw.println(" All active sessions:");
10826 Iterator<Session> it = mSessions.iterator();
10827 while (it.hasNext()) {
10828 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010829 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010830 s.dump(pw, " ");
10831 }
10832 }
10833 if (mTokenMap.size() > 0) {
10834 pw.println(" ");
10835 pw.println(" All tokens:");
10836 Iterator<WindowToken> it = mTokenMap.values().iterator();
10837 while (it.hasNext()) {
10838 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010839 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010840 token.dump(pw, " ");
10841 }
10842 }
10843 if (mTokenList.size() > 0) {
10844 pw.println(" ");
10845 pw.println(" Window token list:");
10846 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010847 pw.print(" #"); pw.print(i); pw.print(": ");
10848 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010849 }
10850 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010851 if (mWallpaperTokens.size() > 0) {
10852 pw.println(" ");
10853 pw.println(" Wallpaper tokens:");
10854 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10855 WindowToken token = mWallpaperTokens.get(i);
10856 pw.print(" Wallpaper #"); pw.print(i);
10857 pw.print(' '); pw.print(token); pw.println(':');
10858 token.dump(pw, " ");
10859 }
10860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010861 if (mAppTokens.size() > 0) {
10862 pw.println(" ");
10863 pw.println(" Application tokens in Z order:");
10864 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010865 pw.print(" App #"); pw.print(i); pw.print(": ");
10866 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010867 }
10868 }
10869 if (mFinishedStarting.size() > 0) {
10870 pw.println(" ");
10871 pw.println(" Finishing start of application tokens:");
10872 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10873 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010874 pw.print(" Finished Starting #"); pw.print(i);
10875 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010876 token.dump(pw, " ");
10877 }
10878 }
10879 if (mExitingTokens.size() > 0) {
10880 pw.println(" ");
10881 pw.println(" Exiting tokens:");
10882 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10883 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010884 pw.print(" Exiting #"); pw.print(i);
10885 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010886 token.dump(pw, " ");
10887 }
10888 }
10889 if (mExitingAppTokens.size() > 0) {
10890 pw.println(" ");
10891 pw.println(" Exiting application tokens:");
10892 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10893 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010894 pw.print(" Exiting App #"); pw.print(i);
10895 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010896 token.dump(pw, " ");
10897 }
10898 }
10899 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010900 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10901 pw.print(" mLastFocus="); pw.println(mLastFocus);
10902 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10903 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10904 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010905 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010906 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10907 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10908 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10909 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010910 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10911 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10912 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10913 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10914 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010915 if (mDimAnimator != null) {
10916 mDimAnimator.printTo(pw);
10917 } else {
10918 pw.print( " no DimAnimator ");
10919 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010920 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010921 pw.print(mInputMethodAnimLayerAdjustment);
10922 pw.print(" mWallpaperAnimLayerAdjustment=");
10923 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010924 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10925 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010926 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10927 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10928 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10929 pw.print(" mRotation="); pw.print(mRotation);
10930 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10931 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10932 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10933 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10934 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10935 pw.print(" mNextAppTransition=0x");
10936 pw.print(Integer.toHexString(mNextAppTransition));
10937 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010938 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010939 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010940 if (mNextAppTransitionPackage != null) {
10941 pw.print(" mNextAppTransitionPackage=");
10942 pw.print(mNextAppTransitionPackage);
10943 pw.print(", mNextAppTransitionEnter=0x");
10944 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10945 pw.print(", mNextAppTransitionExit=0x");
10946 pw.print(Integer.toHexString(mNextAppTransitionExit));
10947 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010948 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10949 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010950 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10951 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10952 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10953 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010954 if (mOpeningApps.size() > 0) {
10955 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10956 }
10957 if (mClosingApps.size() > 0) {
10958 pw.print(" mClosingApps="); pw.println(mClosingApps);
10959 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010960 if (mToTopApps.size() > 0) {
10961 pw.print(" mToTopApps="); pw.println(mToTopApps);
10962 }
10963 if (mToBottomApps.size() > 0) {
10964 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10965 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010966 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10967 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010969 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10970 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10971 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10972 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10973 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10974 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010975 }
10976 }
10977
10978 public void monitor() {
10979 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050010980 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010981 synchronized (mKeyWaiter) { }
10982 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010983
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010984 public void virtualKeyFeedback(KeyEvent event) {
10985 mPolicy.keyFeedbackFromInput(event);
10986 }
10987
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010988 /**
10989 * DimAnimator class that controls the dim animation. This holds the surface and
10990 * all state used for dim animation.
10991 */
10992 private static class DimAnimator {
10993 Surface mDimSurface;
10994 boolean mDimShown = false;
10995 float mDimCurrentAlpha;
10996 float mDimTargetAlpha;
10997 float mDimDeltaPerMs;
10998 long mLastDimAnimTime;
10999
11000 DimAnimator (SurfaceSession session) {
11001 if (mDimSurface == null) {
11002 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11003 + mDimSurface + ": CREATE");
11004 try {
11005 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
11006 Surface.FX_SURFACE_DIM);
Maciej Białka9ee5c222010-03-24 10:25:40 +010011007 mDimSurface.setAlpha(0.0f);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011008 } catch (Exception e) {
11009 Log.e(TAG, "Exception creating Dim surface", e);
11010 }
11011 }
11012 }
11013
11014 /**
11015 * Show the dim surface.
11016 */
11017 void show(int dw, int dh) {
11018 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
11019 dw + "x" + dh + ")");
11020 mDimShown = true;
11021 try {
11022 mDimSurface.setPosition(0, 0);
11023 mDimSurface.setSize(dw, dh);
11024 mDimSurface.show();
11025 } catch (RuntimeException e) {
11026 Log.w(TAG, "Failure showing dim surface", e);
11027 }
11028 }
11029
11030 /**
11031 * Set's the dim surface's layer and update dim parameters that will be used in
11032 * {@link updateSurface} after all windows are examined.
11033 */
11034 void updateParameters(WindowState w, long currentTime) {
11035 mDimSurface.setLayer(w.mAnimLayer-1);
11036
11037 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011038 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11039 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011040 if (mDimTargetAlpha != target) {
11041 // If the desired dim level has changed, then
11042 // start an animation to it.
11043 mLastDimAnimTime = currentTime;
11044 long duration = (w.mAnimating && w.mAnimation != null)
11045 ? w.mAnimation.computeDurationHint()
11046 : DEFAULT_DIM_DURATION;
11047 if (target > mDimTargetAlpha) {
11048 // This is happening behind the activity UI,
11049 // so we can make it run a little longer to
11050 // give a stronger impression without disrupting
11051 // the user.
11052 duration *= DIM_DURATION_MULTIPLIER;
11053 }
11054 if (duration < 1) {
11055 // Don't divide by zero
11056 duration = 1;
11057 }
11058 mDimTargetAlpha = target;
11059 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11060 }
11061 }
11062
11063 /**
11064 * Updating the surface's alpha. Returns true if the animation continues, or returns
11065 * false when the animation is finished and the dim surface is hidden.
11066 */
11067 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11068 if (!dimming) {
11069 if (mDimTargetAlpha != 0) {
11070 mLastDimAnimTime = currentTime;
11071 mDimTargetAlpha = 0;
11072 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11073 }
11074 }
11075
11076 boolean animating = false;
11077 if (mLastDimAnimTime != 0) {
11078 mDimCurrentAlpha += mDimDeltaPerMs
11079 * (currentTime-mLastDimAnimTime);
11080 boolean more = true;
11081 if (displayFrozen) {
11082 // If the display is frozen, there is no reason to animate.
11083 more = false;
11084 } else if (mDimDeltaPerMs > 0) {
11085 if (mDimCurrentAlpha > mDimTargetAlpha) {
11086 more = false;
11087 }
11088 } else if (mDimDeltaPerMs < 0) {
11089 if (mDimCurrentAlpha < mDimTargetAlpha) {
11090 more = false;
11091 }
11092 } else {
11093 more = false;
11094 }
11095
11096 // Do we need to continue animating?
11097 if (more) {
11098 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11099 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11100 mLastDimAnimTime = currentTime;
11101 mDimSurface.setAlpha(mDimCurrentAlpha);
11102 animating = true;
11103 } else {
11104 mDimCurrentAlpha = mDimTargetAlpha;
11105 mLastDimAnimTime = 0;
11106 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11107 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11108 mDimSurface.setAlpha(mDimCurrentAlpha);
11109 if (!dimming) {
11110 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11111 + ": HIDE");
11112 try {
11113 mDimSurface.hide();
11114 } catch (RuntimeException e) {
11115 Log.w(TAG, "Illegal argument exception hiding dim surface");
11116 }
11117 mDimShown = false;
11118 }
11119 }
11120 }
11121 return animating;
11122 }
11123
11124 public void printTo(PrintWriter pw) {
11125 pw.print(" mDimShown="); pw.print(mDimShown);
11126 pw.print(" current="); pw.print(mDimCurrentAlpha);
11127 pw.print(" target="); pw.print(mDimTargetAlpha);
11128 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11129 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11130 }
11131 }
11132
11133 /**
11134 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11135 * This is used for opening/closing transition for apps in compatible mode.
11136 */
11137 private static class FadeInOutAnimation extends Animation {
11138 int mWidth;
11139 boolean mFadeIn;
11140
11141 public FadeInOutAnimation(boolean fadeIn) {
11142 setInterpolator(new AccelerateInterpolator());
11143 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11144 mFadeIn = fadeIn;
11145 }
11146
11147 @Override
11148 protected void applyTransformation(float interpolatedTime, Transformation t) {
11149 float x = interpolatedTime;
11150 if (!mFadeIn) {
11151 x = 1.0f - x; // reverse the interpolation for fade out
11152 }
11153 if (x < 0.5) {
11154 // move the window out of the screen.
11155 t.getMatrix().setTranslate(mWidth, 0);
11156 } else {
11157 t.getMatrix().setTranslate(0, 0);// show
11158 t.setAlpha((x - 0.5f) * 2);
11159 }
11160 }
11161
11162 @Override
11163 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11164 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11165 mWidth = width;
11166 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011167
11168 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011169 public int getZAdjustment() {
11170 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011171 }
11172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011173}