blob: 327cd72c81959518416eac84f25d65c0f94fb710 [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;
136 static final boolean DEBUG_LAYERS = false;
137 static final boolean DEBUG_INPUT = false;
138 static final boolean DEBUG_INPUT_METHOD = false;
139 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700140 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 static final boolean DEBUG_ORIENTATION = false;
142 static final boolean DEBUG_APP_TRANSITIONS = false;
143 static final boolean DEBUG_STARTING_WINDOW = false;
144 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700145 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final boolean SHOW_TRANSACTIONS = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700147 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700148 static final boolean MEASURE_LATENCY = false;
149 static private LatencyTimer lt;
150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 static final boolean PROFILE_ORIENTATION = false;
152 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700153 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 /** How long to wait for subsequent key repeats, in milliseconds */
158 static final int KEY_REPEAT_DELAY = 50;
159
160 /** How much to multiply the policy's type layer, to reserve room
161 * for multiple windows of the same type and Z-ordering adjustment
162 * with TYPE_LAYER_OFFSET. */
163 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
166 * or below others in the same layer. */
167 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /** How much to increment the layer for each window, to reserve room
170 * for effect surfaces between them.
171 */
172 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 /** The maximum length we will accept for a loaded animation duration:
175 * this is 10 seconds.
176 */
177 static final int MAX_ANIMATION_DURATION = 10*1000;
178
179 /** Amount of time (in milliseconds) to animate the dim surface from one
180 * value to another, when no window animation is driving it.
181 */
182 static final int DEFAULT_DIM_DURATION = 200;
183
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700184 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
185 * compatible windows.
186 */
187 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 /** Adjustment to time to perform a dim, to make it more dramatic.
190 */
191 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700192
Dianne Hackborncfaef692009-06-15 14:24:44 -0700193 static final int INJECT_FAILED = 0;
194 static final int INJECT_SUCCEEDED = 1;
195 static final int INJECT_NO_PERMISSION = -1;
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 static final int UPDATE_FOCUS_NORMAL = 0;
198 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
199 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
200 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700201
Michael Chane96440f2009-05-06 10:27:36 -0700202 /** The minimum time between dispatching touch events. */
203 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
204
205 // Last touch event time
206 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700207
Michael Chane96440f2009-05-06 10:27:36 -0700208 // Last touch event type
209 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700210
Michael Chane96440f2009-05-06 10:27:36 -0700211 // Time to wait before calling useractivity again. This saves CPU usage
212 // when we get a flood of touch events.
213 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
214
215 // Last time we call user activity
216 long mLastUserActivityCallTime = 0;
217
Romain Guy06882f82009-06-10 13:36:04 -0700218 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700219 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700222 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
224 /**
225 * Condition waited on by {@link #reenableKeyguard} to know the call to
226 * the window policy has finished.
227 */
228 private boolean mWaitingUntilKeyguardReenabled = false;
229
230
231 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
232 new Handler(), "WindowManagerService.mKeyguardDisabled") {
233 public void acquired() {
234 mPolicy.enableKeyguard(false);
235 }
236 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700237 mPolicy.enableKeyguard(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 synchronized (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 mWaitingUntilKeyguardReenabled = false;
240 mKeyguardDisabled.notifyAll();
241 }
242 }
243 };
244
245 final Context mContext;
246
247 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
252
253 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * All currently active sessions with clients.
259 */
260 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * Mapping from an IWindow IBinder to the server's Window object.
264 * This is also used as the lock for all of our state.
265 */
266 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
267
268 /**
269 * Mapping from a token IBinder to a WindowToken object.
270 */
271 final HashMap<IBinder, WindowToken> mTokenMap =
272 new HashMap<IBinder, WindowToken>();
273
274 /**
275 * The same tokens as mTokenMap, stored in a list for efficient iteration
276 * over them.
277 */
278 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
281 * Window tokens that are in the process of exiting, but still
282 * on screen for animations.
283 */
284 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
285
286 /**
287 * Z-ordered (bottom-most first) list of all application tokens, for
288 * controlling the ordering of windows in different applications. This
289 * contains WindowToken objects.
290 */
291 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
292
293 /**
294 * Application tokens that are in the process of exiting, but still
295 * on screen for animations.
296 */
297 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
298
299 /**
300 * List of window tokens that have finished starting their application,
301 * and now need to have the policy remove their windows.
302 */
303 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
304
305 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700306 * This was the app token that was used to retrieve the last enter
307 * animation. It will be used for the next exit animation.
308 */
309 AppWindowToken mLastEnterAnimToken;
310
311 /**
312 * These were the layout params used to retrieve the last enter animation.
313 * They will be used for the next exit animation.
314 */
315 LayoutParams mLastEnterAnimParams;
316
317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 * Z-ordered (bottom-most first) list of all Window objects.
319 */
320 final ArrayList mWindows = new ArrayList();
321
322 /**
323 * Windows that are being resized. Used so we can tell the client about
324 * the resize after closing the transaction in which we resized the
325 * underlying surface.
326 */
327 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
328
329 /**
330 * Windows whose animations have ended and now must be removed.
331 */
332 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
333
334 /**
335 * Windows whose surface should be destroyed.
336 */
337 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
338
339 /**
340 * Windows that have lost input focus and are waiting for the new
341 * focus window to be displayed before they are told about this.
342 */
343 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
344
345 /**
346 * This is set when we have run out of memory, and will either be an empty
347 * list or contain windows that need to be force removed.
348 */
349 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700354 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 Surface mBlurSurface;
356 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 final float[] mTmpFloats = new float[9];
361
362 boolean mSafeMode;
363 boolean mDisplayEnabled = false;
364 boolean mSystemBooted = false;
365 int mRotation = 0;
366 int mRequestedRotation = 0;
367 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700368 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 ArrayList<IRotationWatcher> mRotationWatchers
370 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 boolean mLayoutNeeded = true;
373 boolean mAnimationPending = false;
374 boolean mDisplayFrozen = false;
375 boolean mWindowsFreezingScreen = false;
376 long mFreezeGcPending = 0;
377 int mAppsFreezingScreen = 0;
378
379 // This is held as long as we have the screen frozen, to give us time to
380 // perform a rotation animation when turning off shows the lock screen which
381 // changes the orientation.
382 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 // State management of app transitions. When we are preparing for a
385 // transition, mNextAppTransition will be the kind of transition to
386 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
387 // mOpeningApps and mClosingApps are the lists of tokens that will be
388 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700389 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700390 String mNextAppTransitionPackage;
391 int mNextAppTransitionEnter;
392 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700394 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 boolean mAppTransitionTimeout = false;
396 boolean mStartingIconInTransition = false;
397 boolean mSkipAppTransitionAnimation = false;
398 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
399 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700400 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
401 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 //flag to detect fat touch events
404 boolean mFatTouch = false;
405 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 H mH = new H();
408
409 WindowState mCurrentFocus = null;
410 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 // This just indicates the window the input method is on top of, not
413 // necessarily the window its input is going to.
414 WindowState mInputMethodTarget = null;
415 WindowState mUpcomingInputMethodTarget = null;
416 boolean mInputMethodTargetWaitingAnim;
417 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 WindowState mInputMethodWindow = null;
420 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
421
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700422 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
423
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700424 // If non-null, this is the currently visible window that is associated
425 // with the wallpaper.
426 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700427 // If non-null, we are in the middle of animating from one wallpaper target
428 // to another, and this is the lower one in Z-order.
429 WindowState mLowerWallpaperTarget = null;
430 // If non-null, we are in the middle of animating from one wallpaper target
431 // to another, and this is the higher one in Z-order.
432 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700433 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700434 float mLastWallpaperX = -1;
435 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800436 float mLastWallpaperXStep = -1;
437 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800438 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700439 // This is set when we are waiting for a wallpaper to tell us it is done
440 // changing its scroll position.
441 WindowState mWaitingOnWallpaper;
442 // The last time we had a timeout when waiting for a wallpaper.
443 long mLastWallpaperTimeoutTime;
444 // We give a wallpaper up to 150ms to finish scrolling.
445 static final long WALLPAPER_TIMEOUT = 150;
446 // Time we wait after a timeout before trying to wait again.
447 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 AppWindowToken mFocusedApp = null;
450
451 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 float mWindowAnimationScale = 1.0f;
454 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 final KeyWaiter mKeyWaiter = new KeyWaiter();
457 final KeyQ mQueue;
458 final InputDispatcherThread mInputThread;
459
460 // Who is holding the screen on.
461 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700462
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700463 boolean mTurnOnScreen;
464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 /**
466 * Whether the UI is currently running in touch mode (not showing
467 * navigational focus because the user is directly pressing the screen).
468 */
469 boolean mInTouchMode = false;
470
471 private ViewServer mViewServer;
472
473 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700474
Dianne Hackbornc485a602009-03-24 22:39:49 -0700475 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700476 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700477
478 // The frame use to limit the size of the app running in compatibility mode.
479 Rect mCompatibleScreenFrame = new Rect();
480 // The surface used to fill the outer rim of the app running in compatibility mode.
481 Surface mBackgroundFillerSurface = null;
482 boolean mBackgroundFillerShown = false;
483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 public static WindowManagerService main(Context context,
485 PowerManagerService pm, boolean haveInputMethods) {
486 WMThread thr = new WMThread(context, pm, haveInputMethods);
487 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 synchronized (thr) {
490 while (thr.mService == null) {
491 try {
492 thr.wait();
493 } catch (InterruptedException e) {
494 }
495 }
496 }
Romain Guy06882f82009-06-10 13:36:04 -0700497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 return thr.mService;
499 }
Romain Guy06882f82009-06-10 13:36:04 -0700500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 static class WMThread extends Thread {
502 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 private final Context mContext;
505 private final PowerManagerService mPM;
506 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 public WMThread(Context context, PowerManagerService pm,
509 boolean haveInputMethods) {
510 super("WindowManager");
511 mContext = context;
512 mPM = pm;
513 mHaveInputMethods = haveInputMethods;
514 }
Romain Guy06882f82009-06-10 13:36:04 -0700515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 public void run() {
517 Looper.prepare();
518 WindowManagerService s = new WindowManagerService(mContext, mPM,
519 mHaveInputMethods);
520 android.os.Process.setThreadPriority(
521 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 synchronized (this) {
524 mService = s;
525 notifyAll();
526 }
Romain Guy06882f82009-06-10 13:36:04 -0700527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 Looper.loop();
529 }
530 }
531
532 static class PolicyThread extends Thread {
533 private final WindowManagerPolicy mPolicy;
534 private final WindowManagerService mService;
535 private final Context mContext;
536 private final PowerManagerService mPM;
537 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 public PolicyThread(WindowManagerPolicy policy,
540 WindowManagerService service, Context context,
541 PowerManagerService pm) {
542 super("WindowManagerPolicy");
543 mPolicy = policy;
544 mService = service;
545 mContext = context;
546 mPM = pm;
547 }
Romain Guy06882f82009-06-10 13:36:04 -0700548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 public void run() {
550 Looper.prepare();
551 //Looper.myLooper().setMessageLogging(new LogPrinter(
552 // Log.VERBOSE, "WindowManagerPolicy"));
553 android.os.Process.setThreadPriority(
554 android.os.Process.THREAD_PRIORITY_FOREGROUND);
555 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 synchronized (this) {
558 mRunning = true;
559 notifyAll();
560 }
Romain Guy06882f82009-06-10 13:36:04 -0700561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 Looper.loop();
563 }
564 }
565
566 private WindowManagerService(Context context, PowerManagerService pm,
567 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700568 if (MEASURE_LATENCY) {
569 lt = new LatencyTimer(100, 1000);
570 }
571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 mContext = context;
573 mHaveInputMethods = haveInputMethods;
574 mLimitedAlphaCompositing = context.getResources().getBoolean(
575 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 mPowerManager = pm;
578 mPowerManager.setPolicy(mPolicy);
579 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
580 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
581 "SCREEN_FROZEN");
582 mScreenFrozenLock.setReferenceCounted(false);
583
584 mActivityManager = ActivityManagerNative.getDefault();
585 mBatteryStats = BatteryStatsService.getService();
586
587 // Get persisted window scale setting
588 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
589 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
590 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
591 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700592
Michael Chan9f028e62009-08-04 17:37:46 -0700593 int max_events_per_sec = 35;
594 try {
595 max_events_per_sec = Integer.parseInt(SystemProperties
596 .get("windowsmgr.max_events_per_sec"));
597 if (max_events_per_sec < 1) {
598 max_events_per_sec = 35;
599 }
600 } catch (NumberFormatException e) {
601 }
602 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 mQueue = new KeyQ();
605
606 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
609 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 synchronized (thr) {
612 while (!thr.mRunning) {
613 try {
614 thr.wait();
615 } catch (InterruptedException e) {
616 }
617 }
618 }
Romain Guy06882f82009-06-10 13:36:04 -0700619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 // Add ourself to the Watchdog monitors.
623 Watchdog.getInstance().addMonitor(this);
624 }
625
626 @Override
627 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
628 throws RemoteException {
629 try {
630 return super.onTransact(code, data, reply, flags);
631 } catch (RuntimeException e) {
632 // The window manager only throws security exceptions, so let's
633 // log all others.
634 if (!(e instanceof SecurityException)) {
635 Log.e(TAG, "Window Manager Crash", e);
636 }
637 throw e;
638 }
639 }
640
641 private void placeWindowAfter(Object pos, WindowState window) {
642 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700643 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 TAG, "Adding window " + window + " at "
645 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
646 mWindows.add(i+1, window);
647 }
648
649 private void placeWindowBefore(Object pos, WindowState window) {
650 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700651 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 TAG, "Adding window " + window + " at "
653 + i + " of " + mWindows.size() + " (before " + pos + ")");
654 mWindows.add(i, window);
655 }
656
657 //This method finds out the index of a window that has the same app token as
658 //win. used for z ordering the windows in mWindows
659 private int findIdxBasedOnAppTokens(WindowState win) {
660 //use a local variable to cache mWindows
661 ArrayList localmWindows = mWindows;
662 int jmax = localmWindows.size();
663 if(jmax == 0) {
664 return -1;
665 }
666 for(int j = (jmax-1); j >= 0; j--) {
667 WindowState wentry = (WindowState)localmWindows.get(j);
668 if(wentry.mAppToken == win.mAppToken) {
669 return j;
670 }
671 }
672 return -1;
673 }
Romain Guy06882f82009-06-10 13:36:04 -0700674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
676 final IWindow client = win.mClient;
677 final WindowToken token = win.mToken;
678 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 final int N = localmWindows.size();
681 final WindowState attached = win.mAttachedWindow;
682 int i;
683 if (attached == null) {
684 int tokenWindowsPos = token.windows.size();
685 if (token.appWindowToken != null) {
686 int index = tokenWindowsPos-1;
687 if (index >= 0) {
688 // If this application has existing windows, we
689 // simply place the new window on top of them... but
690 // keep the starting window on top.
691 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
692 // Base windows go behind everything else.
693 placeWindowBefore(token.windows.get(0), win);
694 tokenWindowsPos = 0;
695 } else {
696 AppWindowToken atoken = win.mAppToken;
697 if (atoken != null &&
698 token.windows.get(index) == atoken.startingWindow) {
699 placeWindowBefore(token.windows.get(index), win);
700 tokenWindowsPos--;
701 } else {
702 int newIdx = findIdxBasedOnAppTokens(win);
703 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700704 //there is a window above this one associated with the same
705 //apptoken note that the window could be a floating window
706 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700708 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
709 TAG, "Adding window " + win + " at "
710 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714 }
715 } else {
716 if (localLOGV) Log.v(
717 TAG, "Figuring out where to add app window "
718 + client.asBinder() + " (token=" + token + ")");
719 // Figure out where the window should go, based on the
720 // order of applications.
721 final int NA = mAppTokens.size();
722 Object pos = null;
723 for (i=NA-1; i>=0; i--) {
724 AppWindowToken t = mAppTokens.get(i);
725 if (t == token) {
726 i--;
727 break;
728 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700729
730 // We haven't reached the token yet; if this token
731 // is not going to the bottom and has windows, we can
732 // use it as an anchor for when we do reach the token.
733 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 pos = t.windows.get(0);
735 }
736 }
737 // We now know the index into the apps. If we found
738 // an app window above, that gives us the position; else
739 // we need to look some more.
740 if (pos != null) {
741 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700742 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 mTokenMap.get(((WindowState)pos).mClient.asBinder());
744 if (atoken != null) {
745 final int NC = atoken.windows.size();
746 if (NC > 0) {
747 WindowState bottom = atoken.windows.get(0);
748 if (bottom.mSubLayer < 0) {
749 pos = bottom;
750 }
751 }
752 }
753 placeWindowBefore(pos, win);
754 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700755 // Continue looking down until we find the first
756 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 while (i >= 0) {
758 AppWindowToken t = mAppTokens.get(i);
759 final int NW = t.windows.size();
760 if (NW > 0) {
761 pos = t.windows.get(NW-1);
762 break;
763 }
764 i--;
765 }
766 if (pos != null) {
767 // Move in front of any windows attached to this
768 // one.
769 WindowToken atoken =
770 mTokenMap.get(((WindowState)pos).mClient.asBinder());
771 if (atoken != null) {
772 final int NC = atoken.windows.size();
773 if (NC > 0) {
774 WindowState top = atoken.windows.get(NC-1);
775 if (top.mSubLayer >= 0) {
776 pos = top;
777 }
778 }
779 }
780 placeWindowAfter(pos, win);
781 } else {
782 // Just search for the start of this layer.
783 final int myLayer = win.mBaseLayer;
784 for (i=0; i<N; i++) {
785 WindowState w = (WindowState)localmWindows.get(i);
786 if (w.mBaseLayer > myLayer) {
787 break;
788 }
789 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700790 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
791 TAG, "Adding window " + win + " at "
792 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 localmWindows.add(i, win);
794 }
795 }
796 }
797 } else {
798 // Figure out where window should go, based on layer.
799 final int myLayer = win.mBaseLayer;
800 for (i=N-1; i>=0; i--) {
801 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
802 i++;
803 break;
804 }
805 }
806 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700807 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
808 TAG, "Adding window " + win + " at "
809 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 localmWindows.add(i, win);
811 }
812 if (addToToken) {
813 token.windows.add(tokenWindowsPos, win);
814 }
815
816 } else {
817 // Figure out this window's ordering relative to the window
818 // it is attached to.
819 final int NA = token.windows.size();
820 final int sublayer = win.mSubLayer;
821 int largestSublayer = Integer.MIN_VALUE;
822 WindowState windowWithLargestSublayer = null;
823 for (i=0; i<NA; i++) {
824 WindowState w = token.windows.get(i);
825 final int wSublayer = w.mSubLayer;
826 if (wSublayer >= largestSublayer) {
827 largestSublayer = wSublayer;
828 windowWithLargestSublayer = w;
829 }
830 if (sublayer < 0) {
831 // For negative sublayers, we go below all windows
832 // in the same sublayer.
833 if (wSublayer >= sublayer) {
834 if (addToToken) {
835 token.windows.add(i, win);
836 }
837 placeWindowBefore(
838 wSublayer >= 0 ? attached : w, win);
839 break;
840 }
841 } else {
842 // For positive sublayers, we go above all windows
843 // in the same sublayer.
844 if (wSublayer > sublayer) {
845 if (addToToken) {
846 token.windows.add(i, win);
847 }
848 placeWindowBefore(w, win);
849 break;
850 }
851 }
852 }
853 if (i >= NA) {
854 if (addToToken) {
855 token.windows.add(win);
856 }
857 if (sublayer < 0) {
858 placeWindowBefore(attached, win);
859 } else {
860 placeWindowAfter(largestSublayer >= 0
861 ? windowWithLargestSublayer
862 : attached,
863 win);
864 }
865 }
866 }
Romain Guy06882f82009-06-10 13:36:04 -0700867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 if (win.mAppToken != null && addToToken) {
869 win.mAppToken.allAppWindows.add(win);
870 }
871 }
Romain Guy06882f82009-06-10 13:36:04 -0700872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 static boolean canBeImeTarget(WindowState w) {
874 final int fl = w.mAttrs.flags
875 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
876 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
877 return w.isVisibleOrAdding();
878 }
879 return false;
880 }
Romain Guy06882f82009-06-10 13:36:04 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
883 final ArrayList localmWindows = mWindows;
884 final int N = localmWindows.size();
885 WindowState w = null;
886 int i = N;
887 while (i > 0) {
888 i--;
889 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
892 // + Integer.toHexString(w.mAttrs.flags));
893 if (canBeImeTarget(w)) {
894 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 // Yet more tricksyness! If this window is a "starting"
897 // window, we do actually want to be on top of it, but
898 // it is not -really- where input will go. So if the caller
899 // is not actually looking to move the IME, look down below
900 // for a real window to target...
901 if (!willMove
902 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
903 && i > 0) {
904 WindowState wb = (WindowState)localmWindows.get(i-1);
905 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
906 i--;
907 w = wb;
908 }
909 }
910 break;
911 }
912 }
Romain Guy06882f82009-06-10 13:36:04 -0700913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
917 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 if (willMove && w != null) {
920 final WindowState curTarget = mInputMethodTarget;
921 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 // Now some fun for dealing with window animations that
924 // modify the Z order. We need to look at all windows below
925 // the current target that are in this app, finding the highest
926 // visible one in layering.
927 AppWindowToken token = curTarget.mAppToken;
928 WindowState highestTarget = null;
929 int highestPos = 0;
930 if (token.animating || token.animation != null) {
931 int pos = 0;
932 pos = localmWindows.indexOf(curTarget);
933 while (pos >= 0) {
934 WindowState win = (WindowState)localmWindows.get(pos);
935 if (win.mAppToken != token) {
936 break;
937 }
938 if (!win.mRemoved) {
939 if (highestTarget == null || win.mAnimLayer >
940 highestTarget.mAnimLayer) {
941 highestTarget = win;
942 highestPos = pos;
943 }
944 }
945 pos--;
946 }
947 }
Romain Guy06882f82009-06-10 13:36:04 -0700948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700950 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 + mNextAppTransition + " " + highestTarget
952 + " animating=" + highestTarget.isAnimating()
953 + " layer=" + highestTarget.mAnimLayer
954 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700955
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700956 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 // If we are currently setting up for an animation,
958 // hold everything until we can find out what will happen.
959 mInputMethodTargetWaitingAnim = true;
960 mInputMethodTarget = highestTarget;
961 return highestPos + 1;
962 } else if (highestTarget.isAnimating() &&
963 highestTarget.mAnimLayer > w.mAnimLayer) {
964 // If the window we are currently targeting is involved
965 // with an animation, and it is on top of the next target
966 // we will be over, then hold off on moving until
967 // that is done.
968 mInputMethodTarget = highestTarget;
969 return highestPos + 1;
970 }
971 }
972 }
973 }
Romain Guy06882f82009-06-10 13:36:04 -0700974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 //Log.i(TAG, "Placing input method @" + (i+1));
976 if (w != null) {
977 if (willMove) {
978 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700979 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
981 + mInputMethodTarget + " to " + w, e);
982 mInputMethodTarget = w;
983 if (w.mAppToken != null) {
984 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
985 } else {
986 setInputMethodAnimLayerAdjustment(0);
987 }
988 }
989 return i+1;
990 }
991 if (willMove) {
992 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700993 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
995 + mInputMethodTarget + " to null", e);
996 mInputMethodTarget = null;
997 setInputMethodAnimLayerAdjustment(0);
998 }
999 return -1;
1000 }
Romain Guy06882f82009-06-10 13:36:04 -07001001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 void addInputMethodWindowToListLocked(WindowState win) {
1003 int pos = findDesiredInputMethodWindowIndexLocked(true);
1004 if (pos >= 0) {
1005 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001006 if (DEBUG_WINDOW_MOVEMENT) Log.v(
1007 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 mWindows.add(pos, win);
1009 moveInputMethodDialogsLocked(pos+1);
1010 return;
1011 }
1012 win.mTargetAppToken = null;
1013 addWindowToListInOrderLocked(win, true);
1014 moveInputMethodDialogsLocked(pos);
1015 }
Romain Guy06882f82009-06-10 13:36:04 -07001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 void setInputMethodAnimLayerAdjustment(int adj) {
1018 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1019 mInputMethodAnimLayerAdjustment = adj;
1020 WindowState imw = mInputMethodWindow;
1021 if (imw != null) {
1022 imw.mAnimLayer = imw.mLayer + adj;
1023 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1024 + " anim layer: " + imw.mAnimLayer);
1025 int wi = imw.mChildWindows.size();
1026 while (wi > 0) {
1027 wi--;
1028 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1029 cw.mAnimLayer = cw.mLayer + adj;
1030 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1031 + " anim layer: " + cw.mAnimLayer);
1032 }
1033 }
1034 int di = mInputMethodDialogs.size();
1035 while (di > 0) {
1036 di --;
1037 imw = mInputMethodDialogs.get(di);
1038 imw.mAnimLayer = imw.mLayer + adj;
1039 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1040 + " anim layer: " + imw.mAnimLayer);
1041 }
1042 }
Romain Guy06882f82009-06-10 13:36:04 -07001043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1045 int wpos = mWindows.indexOf(win);
1046 if (wpos >= 0) {
1047 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001048 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 mWindows.remove(wpos);
1050 int NC = win.mChildWindows.size();
1051 while (NC > 0) {
1052 NC--;
1053 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1054 int cpos = mWindows.indexOf(cw);
1055 if (cpos >= 0) {
1056 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001057 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1058 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 mWindows.remove(cpos);
1060 }
1061 }
1062 }
1063 return interestingPos;
1064 }
Romain Guy06882f82009-06-10 13:36:04 -07001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 private void reAddWindowToListInOrderLocked(WindowState win) {
1067 addWindowToListInOrderLocked(win, false);
1068 // This is a hack to get all of the child windows added as well
1069 // at the right position. Child windows should be rare and
1070 // this case should be rare, so it shouldn't be that big a deal.
1071 int wpos = mWindows.indexOf(win);
1072 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001073 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1074 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 mWindows.remove(wpos);
1076 reAddWindowLocked(wpos, win);
1077 }
1078 }
Romain Guy06882f82009-06-10 13:36:04 -07001079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 void logWindowList(String prefix) {
1081 int N = mWindows.size();
1082 while (N > 0) {
1083 N--;
1084 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1085 }
1086 }
Romain Guy06882f82009-06-10 13:36:04 -07001087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 void moveInputMethodDialogsLocked(int pos) {
1089 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 final int N = dialogs.size();
1092 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1093 for (int i=0; i<N; i++) {
1094 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1095 }
1096 if (DEBUG_INPUT_METHOD) {
1097 Log.v(TAG, "Window list w/pos=" + pos);
1098 logWindowList(" ");
1099 }
Romain Guy06882f82009-06-10 13:36:04 -07001100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 if (pos >= 0) {
1102 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1103 if (pos < mWindows.size()) {
1104 WindowState wp = (WindowState)mWindows.get(pos);
1105 if (wp == mInputMethodWindow) {
1106 pos++;
1107 }
1108 }
1109 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1110 for (int i=0; i<N; i++) {
1111 WindowState win = dialogs.get(i);
1112 win.mTargetAppToken = targetAppToken;
1113 pos = reAddWindowLocked(pos, win);
1114 }
1115 if (DEBUG_INPUT_METHOD) {
1116 Log.v(TAG, "Final window list:");
1117 logWindowList(" ");
1118 }
1119 return;
1120 }
1121 for (int i=0; i<N; i++) {
1122 WindowState win = dialogs.get(i);
1123 win.mTargetAppToken = null;
1124 reAddWindowToListInOrderLocked(win);
1125 if (DEBUG_INPUT_METHOD) {
1126 Log.v(TAG, "No IM target, final list:");
1127 logWindowList(" ");
1128 }
1129 }
1130 }
Romain Guy06882f82009-06-10 13:36:04 -07001131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1133 final WindowState imWin = mInputMethodWindow;
1134 final int DN = mInputMethodDialogs.size();
1135 if (imWin == null && DN == 0) {
1136 return false;
1137 }
Romain Guy06882f82009-06-10 13:36:04 -07001138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1140 if (imPos >= 0) {
1141 // In this case, the input method windows are to be placed
1142 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 // First check to see if the input method windows are already
1145 // located here, and contiguous.
1146 final int N = mWindows.size();
1147 WindowState firstImWin = imPos < N
1148 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 // Figure out the actual input method window that should be
1151 // at the bottom of their stack.
1152 WindowState baseImWin = imWin != null
1153 ? imWin : mInputMethodDialogs.get(0);
1154 if (baseImWin.mChildWindows.size() > 0) {
1155 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1156 if (cw.mSubLayer < 0) baseImWin = cw;
1157 }
Romain Guy06882f82009-06-10 13:36:04 -07001158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 if (firstImWin == baseImWin) {
1160 // The windows haven't moved... but are they still contiguous?
1161 // First find the top IM window.
1162 int pos = imPos+1;
1163 while (pos < N) {
1164 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1165 break;
1166 }
1167 pos++;
1168 }
1169 pos++;
1170 // Now there should be no more input method windows above.
1171 while (pos < N) {
1172 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1173 break;
1174 }
1175 pos++;
1176 }
1177 if (pos >= N) {
1178 // All is good!
1179 return false;
1180 }
1181 }
Romain Guy06882f82009-06-10 13:36:04 -07001182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 if (imWin != null) {
1184 if (DEBUG_INPUT_METHOD) {
1185 Log.v(TAG, "Moving IM from " + imPos);
1186 logWindowList(" ");
1187 }
1188 imPos = tmpRemoveWindowLocked(imPos, imWin);
1189 if (DEBUG_INPUT_METHOD) {
1190 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1191 logWindowList(" ");
1192 }
1193 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1194 reAddWindowLocked(imPos, imWin);
1195 if (DEBUG_INPUT_METHOD) {
1196 Log.v(TAG, "List after moving IM to " + imPos + ":");
1197 logWindowList(" ");
1198 }
1199 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1200 } else {
1201 moveInputMethodDialogsLocked(imPos);
1202 }
Romain Guy06882f82009-06-10 13:36:04 -07001203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 } else {
1205 // In this case, the input method windows go in a fixed layer,
1206 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 if (imWin != null) {
1209 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1210 tmpRemoveWindowLocked(0, imWin);
1211 imWin.mTargetAppToken = null;
1212 reAddWindowToListInOrderLocked(imWin);
1213 if (DEBUG_INPUT_METHOD) {
1214 Log.v(TAG, "List with no IM target:");
1215 logWindowList(" ");
1216 }
1217 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1218 } else {
1219 moveInputMethodDialogsLocked(-1);;
1220 }
Romain Guy06882f82009-06-10 13:36:04 -07001221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
Romain Guy06882f82009-06-10 13:36:04 -07001223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 if (needAssignLayers) {
1225 assignLayersLocked();
1226 }
Romain Guy06882f82009-06-10 13:36:04 -07001227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 return true;
1229 }
Romain Guy06882f82009-06-10 13:36:04 -07001230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 void adjustInputMethodDialogsLocked() {
1232 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1233 }
Romain Guy06882f82009-06-10 13:36:04 -07001234
Dianne Hackborn25994b42009-09-04 14:21:19 -07001235 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1236 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1237 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1238 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1239 ? wallpaperTarget.mAppToken.animation : null)
1240 + " upper=" + mUpperWallpaperTarget
1241 + " lower=" + mLowerWallpaperTarget);
1242 return (wallpaperTarget != null
1243 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1244 && wallpaperTarget.mAppToken.animation != null)))
1245 || mUpperWallpaperTarget != null
1246 || mLowerWallpaperTarget != null;
1247 }
1248
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001249 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1250 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1251
1252 int adjustWallpaperWindowsLocked() {
1253 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001254
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001255 final int dw = mDisplay.getWidth();
1256 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001257
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001258 // First find top-most window that has asked to be on top of the
1259 // wallpaper; all wallpapers go behind it.
1260 final ArrayList localmWindows = mWindows;
1261 int N = localmWindows.size();
1262 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001263 WindowState foundW = null;
1264 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001265 WindowState topCurW = null;
1266 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001267 int i = N;
1268 while (i > 0) {
1269 i--;
1270 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001271 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1272 if (topCurW == null) {
1273 topCurW = w;
1274 topCurI = i;
1275 }
1276 continue;
1277 }
1278 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001279 if (w.mAppToken != null) {
1280 // If this window's app token is hidden and not animating,
1281 // it is of no interest to us.
1282 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1283 if (DEBUG_WALLPAPER) Log.v(TAG,
1284 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001285 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001286 continue;
1287 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001288 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001289 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1290 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1291 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001292 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001293 && (mWallpaperTarget == w
1294 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001295 if (DEBUG_WALLPAPER) Log.v(TAG,
1296 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001297 foundW = w;
1298 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001299 if (w == mWallpaperTarget && ((w.mAppToken != null
1300 && w.mAppToken.animation != null)
1301 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001302 // The current wallpaper target is animating, so we'll
1303 // look behind it for another possible target and figure
1304 // out what is going on below.
1305 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1306 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001307 continue;
1308 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001309 break;
1310 }
1311 }
1312
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001313 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001314 // If we are currently waiting for an app transition, and either
1315 // the current target or the next target are involved with it,
1316 // then hold off on doing anything with the wallpaper.
1317 // Note that we are checking here for just whether the target
1318 // is part of an app token... which is potentially overly aggressive
1319 // (the app token may not be involved in the transition), but good
1320 // enough (we'll just wait until whatever transition is pending
1321 // executes).
1322 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001323 if (DEBUG_WALLPAPER) Log.v(TAG,
1324 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001325 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001326 }
1327 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001328 if (DEBUG_WALLPAPER) Log.v(TAG,
1329 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001330 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001331 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001332 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001333
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001334 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001335 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001336 Log.v(TAG, "New wallpaper target: " + foundW
1337 + " oldTarget: " + mWallpaperTarget);
1338 }
1339
1340 mLowerWallpaperTarget = null;
1341 mUpperWallpaperTarget = null;
1342
1343 WindowState oldW = mWallpaperTarget;
1344 mWallpaperTarget = foundW;
1345
1346 // Now what is happening... if the current and new targets are
1347 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001348 if (foundW != null && oldW != null) {
1349 boolean oldAnim = oldW.mAnimation != null
1350 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1351 boolean foundAnim = foundW.mAnimation != null
1352 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001353 if (DEBUG_WALLPAPER) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001354 Log.v(TAG, "New animation: " + foundAnim
1355 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001356 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001357 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001358 int oldI = localmWindows.indexOf(oldW);
1359 if (DEBUG_WALLPAPER) {
1360 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1361 }
1362 if (oldI >= 0) {
1363 if (DEBUG_WALLPAPER) {
1364 Log.v(TAG, "Animating wallpapers: old#" + oldI
1365 + "=" + oldW + "; new#" + foundI
1366 + "=" + foundW);
1367 }
1368
1369 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001370 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001371 if (DEBUG_WALLPAPER) {
1372 Log.v(TAG, "Old wallpaper still the target.");
1373 }
1374 mWallpaperTarget = oldW;
1375 }
1376
1377 // Now set the upper and lower wallpaper targets
1378 // correctly, and make sure that we are positioning
1379 // the wallpaper below the lower.
1380 if (foundI > oldI) {
1381 // The new target is on top of the old one.
1382 if (DEBUG_WALLPAPER) {
1383 Log.v(TAG, "Found target above old target.");
1384 }
1385 mUpperWallpaperTarget = foundW;
1386 mLowerWallpaperTarget = oldW;
1387 foundW = oldW;
1388 foundI = oldI;
1389 } else {
1390 // The new target is below the old one.
1391 if (DEBUG_WALLPAPER) {
1392 Log.v(TAG, "Found target below old target.");
1393 }
1394 mUpperWallpaperTarget = oldW;
1395 mLowerWallpaperTarget = foundW;
1396 }
1397 }
1398 }
1399 }
1400
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001401 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001402 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001403 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1404 || (mLowerWallpaperTarget.mAppToken != null
1405 && mLowerWallpaperTarget.mAppToken.animation != null);
1406 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1407 || (mUpperWallpaperTarget.mAppToken != null
1408 && mUpperWallpaperTarget.mAppToken.animation != null);
1409 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001410 if (DEBUG_WALLPAPER) {
1411 Log.v(TAG, "No longer animating wallpaper targets!");
1412 }
1413 mLowerWallpaperTarget = null;
1414 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001415 }
1416 }
1417
1418 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001419 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001420 // The window is visible to the compositor... but is it visible
1421 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001422 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001423 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001424
1425 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001426 // its layer adjustment. Only do this if we are not transfering
1427 // between two wallpaper targets.
1428 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001429 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001430 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001431
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001432 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1433 * TYPE_LAYER_MULTIPLIER
1434 + TYPE_LAYER_OFFSET;
1435
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001436 // Now w is the window we are supposed to be behind... but we
1437 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001438 // AND any starting window associated with it, AND below the
1439 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001440 while (foundI > 0) {
1441 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001442 if (wb.mBaseLayer < maxLayer &&
1443 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001444 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001445 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001446 // This window is not related to the previous one in any
1447 // interesting way, so stop here.
1448 break;
1449 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001450 foundW = wb;
1451 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001452 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001453 } else {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001454 if (DEBUG_WALLPAPER) Log.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001455 }
1456
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001457 if (foundW == null && topCurW != null) {
1458 // There is no wallpaper target, so it goes at the bottom.
1459 // We will assume it is the same place as last time, if known.
1460 foundW = topCurW;
1461 foundI = topCurI+1;
1462 } else {
1463 // Okay i is the position immediately above the wallpaper. Look at
1464 // what is below it for later.
1465 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1466 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001467
Dianne Hackborn284ac932009-08-28 10:34:25 -07001468 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001469 if (mWallpaperTarget.mWallpaperX >= 0) {
1470 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001471 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001472 }
1473 if (mWallpaperTarget.mWallpaperY >= 0) {
1474 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001475 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001476 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001477 }
1478
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001479 // Start stepping backwards from here, ensuring that our wallpaper windows
1480 // are correctly placed.
1481 int curTokenIndex = mWallpaperTokens.size();
1482 while (curTokenIndex > 0) {
1483 curTokenIndex--;
1484 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001485 if (token.hidden == visible) {
1486 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1487 token.hidden = !visible;
1488 // Need to do a layout to ensure the wallpaper now has the
1489 // correct size.
1490 mLayoutNeeded = true;
1491 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001492
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001493 int curWallpaperIndex = token.windows.size();
1494 while (curWallpaperIndex > 0) {
1495 curWallpaperIndex--;
1496 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001497
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001498 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001499 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001500 }
1501
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001502 // First, make sure the client has the current visibility
1503 // state.
1504 if (wallpaper.mWallpaperVisible != visible) {
1505 wallpaper.mWallpaperVisible = visible;
1506 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001507 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001508 "Setting visibility of wallpaper " + wallpaper
1509 + ": " + visible);
1510 wallpaper.mClient.dispatchAppVisibility(visible);
1511 } catch (RemoteException e) {
1512 }
1513 }
1514
1515 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001516 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1517 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001518
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001519 // First, if this window is at the current index, then all
1520 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001521 if (wallpaper == foundW) {
1522 foundI--;
1523 foundW = foundI > 0
1524 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001525 continue;
1526 }
1527
1528 // The window didn't match... the current wallpaper window,
1529 // wherever it is, is in the wrong place, so make sure it is
1530 // not in the list.
1531 int oldIndex = localmWindows.indexOf(wallpaper);
1532 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001533 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1534 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001535 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001536 if (oldIndex < foundI) {
1537 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001538 }
1539 }
1540
1541 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001542 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1543 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001544 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001545
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001546 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001547 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001548 }
1549 }
1550
1551 return changed;
1552 }
1553
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001554 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001555 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1556 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001557 mWallpaperAnimLayerAdjustment = adj;
1558 int curTokenIndex = mWallpaperTokens.size();
1559 while (curTokenIndex > 0) {
1560 curTokenIndex--;
1561 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1562 int curWallpaperIndex = token.windows.size();
1563 while (curWallpaperIndex > 0) {
1564 curWallpaperIndex--;
1565 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1566 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001567 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1568 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001569 }
1570 }
1571 }
1572
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001573 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1574 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001575 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001576 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001577 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001578 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001579 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1580 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1581 changed = wallpaperWin.mXOffset != offset;
1582 if (changed) {
1583 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1584 + wallpaperWin + " x: " + offset);
1585 wallpaperWin.mXOffset = offset;
1586 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001587 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001588 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001589 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001590 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001591 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001592
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001593 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001594 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001595 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1596 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1597 if (wallpaperWin.mYOffset != offset) {
1598 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1599 + wallpaperWin + " y: " + offset);
1600 changed = true;
1601 wallpaperWin.mYOffset = offset;
1602 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001603 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001604 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001605 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001606 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001607 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001608
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001609 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001610 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001611 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1612 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1613 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001614 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001615 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001616 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001617 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001618 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1619 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001620 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001621 if (mWaitingOnWallpaper != null) {
1622 long start = SystemClock.uptimeMillis();
1623 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1624 < start) {
1625 try {
1626 if (DEBUG_WALLPAPER) Log.v(TAG,
1627 "Waiting for offset complete...");
1628 mWindowMap.wait(WALLPAPER_TIMEOUT);
1629 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001630 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001631 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1632 if ((start+WALLPAPER_TIMEOUT)
1633 < SystemClock.uptimeMillis()) {
1634 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1635 + wallpaperWin);
1636 mLastWallpaperTimeoutTime = start;
1637 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001638 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001639 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001640 }
1641 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001642 } catch (RemoteException e) {
1643 }
1644 }
1645
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001646 return changed;
1647 }
1648
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001649 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001650 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001651 if (mWaitingOnWallpaper != null &&
1652 mWaitingOnWallpaper.mClient.asBinder() == window) {
1653 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001654 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001655 }
1656 }
1657 }
1658
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001659 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001660 final int dw = mDisplay.getWidth();
1661 final int dh = mDisplay.getHeight();
1662
1663 boolean changed = false;
1664
1665 WindowState target = mWallpaperTarget;
1666 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001667 if (target.mWallpaperX >= 0) {
1668 mLastWallpaperX = target.mWallpaperX;
1669 } else if (changingTarget.mWallpaperX >= 0) {
1670 mLastWallpaperX = changingTarget.mWallpaperX;
1671 }
1672 if (target.mWallpaperY >= 0) {
1673 mLastWallpaperY = target.mWallpaperY;
1674 } else if (changingTarget.mWallpaperY >= 0) {
1675 mLastWallpaperY = changingTarget.mWallpaperY;
1676 }
1677 }
1678
1679 int curTokenIndex = mWallpaperTokens.size();
1680 while (curTokenIndex > 0) {
1681 curTokenIndex--;
1682 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1683 int curWallpaperIndex = token.windows.size();
1684 while (curWallpaperIndex > 0) {
1685 curWallpaperIndex--;
1686 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1687 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1688 wallpaper.computeShownFrameLocked();
1689 changed = true;
1690 // We only want to be synchronous with one wallpaper.
1691 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001692 }
1693 }
1694 }
1695
1696 return changed;
1697 }
1698
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001699 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001700 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001701 final int dw = mDisplay.getWidth();
1702 final int dh = mDisplay.getHeight();
1703
1704 int curTokenIndex = mWallpaperTokens.size();
1705 while (curTokenIndex > 0) {
1706 curTokenIndex--;
1707 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001708 if (token.hidden == visible) {
1709 token.hidden = !visible;
1710 // Need to do a layout to ensure the wallpaper now has the
1711 // correct size.
1712 mLayoutNeeded = true;
1713 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001714
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001715 int curWallpaperIndex = token.windows.size();
1716 while (curWallpaperIndex > 0) {
1717 curWallpaperIndex--;
1718 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1719 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001720 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001721 }
1722
1723 if (wallpaper.mWallpaperVisible != visible) {
1724 wallpaper.mWallpaperVisible = visible;
1725 try {
1726 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001727 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001728 + ": " + visible);
1729 wallpaper.mClient.dispatchAppVisibility(visible);
1730 } catch (RemoteException e) {
1731 }
1732 }
1733 }
1734 }
1735 }
1736
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001737 void sendPointerToWallpaperLocked(WindowState srcWin,
1738 MotionEvent pointer, long eventTime) {
1739 int curTokenIndex = mWallpaperTokens.size();
1740 while (curTokenIndex > 0) {
1741 curTokenIndex--;
1742 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1743 int curWallpaperIndex = token.windows.size();
1744 while (curWallpaperIndex > 0) {
1745 curWallpaperIndex--;
1746 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1747 if ((wallpaper.mAttrs.flags &
1748 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1749 continue;
1750 }
1751 try {
1752 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001753 if (srcWin != null) {
1754 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1755 srcWin.mFrame.top-wallpaper.mFrame.top);
1756 } else {
1757 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1758 }
1759 switch (pointer.getAction()) {
1760 case MotionEvent.ACTION_DOWN:
1761 mSendingPointersToWallpaper = true;
1762 break;
1763 case MotionEvent.ACTION_UP:
1764 mSendingPointersToWallpaper = false;
1765 break;
1766 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001767 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1768 } catch (RemoteException e) {
1769 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1770 }
1771 }
1772 }
1773 }
1774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 public int addWindow(Session session, IWindow client,
1776 WindowManager.LayoutParams attrs, int viewVisibility,
1777 Rect outContentInsets) {
1778 int res = mPolicy.checkAddPermission(attrs);
1779 if (res != WindowManagerImpl.ADD_OKAY) {
1780 return res;
1781 }
Romain Guy06882f82009-06-10 13:36:04 -07001782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 boolean reportNewConfig = false;
1784 WindowState attachedWindow = null;
1785 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 synchronized(mWindowMap) {
1788 // Instantiating a Display requires talking with the simulator,
1789 // so don't do it until we know the system is mostly up and
1790 // running.
1791 if (mDisplay == null) {
1792 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1793 mDisplay = wm.getDefaultDisplay();
1794 mQueue.setDisplay(mDisplay);
1795 reportNewConfig = true;
1796 }
Romain Guy06882f82009-06-10 13:36:04 -07001797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 if (mWindowMap.containsKey(client.asBinder())) {
1799 Log.w(TAG, "Window " + client + " is already added");
1800 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1801 }
1802
1803 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001804 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 if (attachedWindow == null) {
1806 Log.w(TAG, "Attempted to add window with token that is not a window: "
1807 + attrs.token + ". Aborting.");
1808 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1809 }
1810 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1811 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1812 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1813 + attrs.token + ". Aborting.");
1814 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1815 }
1816 }
1817
1818 boolean addToken = false;
1819 WindowToken token = mTokenMap.get(attrs.token);
1820 if (token == null) {
1821 if (attrs.type >= FIRST_APPLICATION_WINDOW
1822 && attrs.type <= LAST_APPLICATION_WINDOW) {
1823 Log.w(TAG, "Attempted to add application window with unknown token "
1824 + attrs.token + ". Aborting.");
1825 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1826 }
1827 if (attrs.type == TYPE_INPUT_METHOD) {
1828 Log.w(TAG, "Attempted to add input method window with unknown token "
1829 + attrs.token + ". Aborting.");
1830 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1831 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001832 if (attrs.type == TYPE_WALLPAPER) {
1833 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1834 + attrs.token + ". Aborting.");
1835 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 token = new WindowToken(attrs.token, -1, false);
1838 addToken = true;
1839 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1840 && attrs.type <= LAST_APPLICATION_WINDOW) {
1841 AppWindowToken atoken = token.appWindowToken;
1842 if (atoken == null) {
1843 Log.w(TAG, "Attempted to add window with non-application token "
1844 + token + ". Aborting.");
1845 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1846 } else if (atoken.removed) {
1847 Log.w(TAG, "Attempted to add window with exiting application token "
1848 + token + ". Aborting.");
1849 return WindowManagerImpl.ADD_APP_EXITING;
1850 }
1851 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1852 // No need for this guy!
1853 if (localLOGV) Log.v(
1854 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1855 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1856 }
1857 } else if (attrs.type == TYPE_INPUT_METHOD) {
1858 if (token.windowType != TYPE_INPUT_METHOD) {
1859 Log.w(TAG, "Attempted to add input method window with bad token "
1860 + attrs.token + ". Aborting.");
1861 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1862 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001863 } else if (attrs.type == TYPE_WALLPAPER) {
1864 if (token.windowType != TYPE_WALLPAPER) {
1865 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1866 + attrs.token + ". Aborting.");
1867 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
1870
1871 win = new WindowState(session, client, token,
1872 attachedWindow, attrs, viewVisibility);
1873 if (win.mDeathRecipient == null) {
1874 // Client has apparently died, so there is no reason to
1875 // continue.
1876 Log.w(TAG, "Adding window client " + client.asBinder()
1877 + " that is dead, aborting.");
1878 return WindowManagerImpl.ADD_APP_EXITING;
1879 }
1880
1881 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 res = mPolicy.prepareAddWindowLw(win, attrs);
1884 if (res != WindowManagerImpl.ADD_OKAY) {
1885 return res;
1886 }
1887
1888 // From now on, no exceptions or errors allowed!
1889
1890 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 if (addToken) {
1895 mTokenMap.put(attrs.token, token);
1896 mTokenList.add(token);
1897 }
1898 win.attach();
1899 mWindowMap.put(client.asBinder(), win);
1900
1901 if (attrs.type == TYPE_APPLICATION_STARTING &&
1902 token.appWindowToken != null) {
1903 token.appWindowToken.startingWindow = win;
1904 }
1905
1906 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 if (attrs.type == TYPE_INPUT_METHOD) {
1909 mInputMethodWindow = win;
1910 addInputMethodWindowToListLocked(win);
1911 imMayMove = false;
1912 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1913 mInputMethodDialogs.add(win);
1914 addWindowToListInOrderLocked(win, true);
1915 adjustInputMethodDialogsLocked();
1916 imMayMove = false;
1917 } else {
1918 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001919 if (attrs.type == TYPE_WALLPAPER) {
1920 mLastWallpaperTimeoutTime = 0;
1921 adjustWallpaperWindowsLocked();
1922 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001923 adjustWallpaperWindowsLocked();
1924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 }
Romain Guy06882f82009-06-10 13:36:04 -07001926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 if (mInTouchMode) {
1932 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1933 }
1934 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1935 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1936 }
Romain Guy06882f82009-06-10 13:36:04 -07001937
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001938 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001940 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1941 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 imMayMove = false;
1943 }
1944 }
Romain Guy06882f82009-06-10 13:36:04 -07001945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001947 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
Romain Guy06882f82009-06-10 13:36:04 -07001949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 assignLayersLocked();
1951 // Don't do layout here, the window must call
1952 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 //dump();
1955
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001956 if (focusChanged) {
1957 if (mCurrentFocus != null) {
1958 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1959 }
1960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 if (localLOGV) Log.v(
1962 TAG, "New client " + client.asBinder()
1963 + ": window=" + win);
1964 }
1965
1966 // sendNewConfiguration() checks caller permissions so we must call it with
1967 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1968 // identity anyway, so it's safe to just clear & restore around this whole
1969 // block.
1970 final long origId = Binder.clearCallingIdentity();
1971 if (reportNewConfig) {
1972 sendNewConfiguration();
1973 } else {
1974 // Update Orientation after adding a window, only if the window needs to be
1975 // displayed right away
1976 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001977 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 sendNewConfiguration();
1979 }
1980 }
1981 }
1982 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 return res;
1985 }
Romain Guy06882f82009-06-10 13:36:04 -07001986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 public void removeWindow(Session session, IWindow client) {
1988 synchronized(mWindowMap) {
1989 WindowState win = windowForClientLocked(session, client);
1990 if (win == null) {
1991 return;
1992 }
1993 removeWindowLocked(session, win);
1994 }
1995 }
Romain Guy06882f82009-06-10 13:36:04 -07001996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 public void removeWindowLocked(Session session, WindowState win) {
1998
1999 if (localLOGV || DEBUG_FOCUS) Log.v(
2000 TAG, "Remove " + win + " client="
2001 + Integer.toHexString(System.identityHashCode(
2002 win.mClient.asBinder()))
2003 + ", surface=" + win.mSurface);
2004
2005 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 if (DEBUG_APP_TRANSITIONS) Log.v(
2008 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2009 + " mExiting=" + win.mExiting
2010 + " isAnimating=" + win.isAnimating()
2011 + " app-animation="
2012 + (win.mAppToken != null ? win.mAppToken.animation : null)
2013 + " inPendingTransaction="
2014 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2015 + " mDisplayFrozen=" + mDisplayFrozen);
2016 // Visibility of the removed window. Will be used later to update orientation later on.
2017 boolean wasVisible = false;
2018 // First, see if we need to run an animation. If we do, we have
2019 // to hold off on removing the window until the animation is done.
2020 // If the display is frozen, just remove immediately, since the
2021 // animation wouldn't be seen.
2022 if (win.mSurface != null && !mDisplayFrozen) {
2023 // If we are not currently running the exit animation, we
2024 // need to see about starting one.
2025 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2028 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2029 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2030 }
2031 // Try starting an animation.
2032 if (applyAnimationLocked(win, transit, false)) {
2033 win.mExiting = true;
2034 }
2035 }
2036 if (win.mExiting || win.isAnimating()) {
2037 // The exit animation is running... wait for it!
2038 //Log.i(TAG, "*** Running exit animation...");
2039 win.mExiting = true;
2040 win.mRemoveOnExit = true;
2041 mLayoutNeeded = true;
2042 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2043 performLayoutAndPlaceSurfacesLocked();
2044 if (win.mAppToken != null) {
2045 win.mAppToken.updateReportedVisibilityLocked();
2046 }
2047 //dump();
2048 Binder.restoreCallingIdentity(origId);
2049 return;
2050 }
2051 }
2052
2053 removeWindowInnerLocked(session, win);
2054 // Removing a visible window will effect the computed orientation
2055 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002056 if (wasVisible && computeForcedAppOrientationLocked()
2057 != mForcedAppOrientation) {
2058 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 }
2060 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2061 Binder.restoreCallingIdentity(origId);
2062 }
Romain Guy06882f82009-06-10 13:36:04 -07002063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002065 mKeyWaiter.finishedKey(session, win.mClient, true,
2066 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2068 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 if (mInputMethodTarget == win) {
2073 moveInputMethodWindowsIfNeededLocked(false);
2074 }
Romain Guy06882f82009-06-10 13:36:04 -07002075
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002076 if (false) {
2077 RuntimeException e = new RuntimeException("here");
2078 e.fillInStackTrace();
2079 Log.w(TAG, "Removing window " + win, e);
2080 }
2081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 mPolicy.removeWindowLw(win);
2083 win.removeLocked();
2084
2085 mWindowMap.remove(win.mClient.asBinder());
2086 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002087 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088
2089 if (mInputMethodWindow == win) {
2090 mInputMethodWindow = null;
2091 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2092 mInputMethodDialogs.remove(win);
2093 }
Romain Guy06882f82009-06-10 13:36:04 -07002094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 final WindowToken token = win.mToken;
2096 final AppWindowToken atoken = win.mAppToken;
2097 token.windows.remove(win);
2098 if (atoken != null) {
2099 atoken.allAppWindows.remove(win);
2100 }
2101 if (localLOGV) Log.v(
2102 TAG, "**** Removing window " + win + ": count="
2103 + token.windows.size());
2104 if (token.windows.size() == 0) {
2105 if (!token.explicit) {
2106 mTokenMap.remove(token.token);
2107 mTokenList.remove(token);
2108 } else if (atoken != null) {
2109 atoken.firstWindowDrawn = false;
2110 }
2111 }
2112
2113 if (atoken != null) {
2114 if (atoken.startingWindow == win) {
2115 atoken.startingWindow = null;
2116 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2117 // If this is the last window and we had requested a starting
2118 // transition window, well there is no point now.
2119 atoken.startingData = null;
2120 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2121 // If this is the last window except for a starting transition
2122 // window, we need to get rid of the starting transition.
2123 if (DEBUG_STARTING_WINDOW) {
2124 Log.v(TAG, "Schedule remove starting " + token
2125 + ": no more real windows");
2126 }
2127 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2128 mH.sendMessage(m);
2129 }
2130 }
Romain Guy06882f82009-06-10 13:36:04 -07002131
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002132 if (win.mAttrs.type == TYPE_WALLPAPER) {
2133 mLastWallpaperTimeoutTime = 0;
2134 adjustWallpaperWindowsLocked();
2135 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002136 adjustWallpaperWindowsLocked();
2137 }
2138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 if (!mInLayout) {
2140 assignLayersLocked();
2141 mLayoutNeeded = true;
2142 performLayoutAndPlaceSurfacesLocked();
2143 if (win.mAppToken != null) {
2144 win.mAppToken.updateReportedVisibilityLocked();
2145 }
2146 }
2147 }
2148
2149 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2150 long origId = Binder.clearCallingIdentity();
2151 try {
2152 synchronized (mWindowMap) {
2153 WindowState w = windowForClientLocked(session, client);
2154 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002155 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 Surface.openTransaction();
2157 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002158 if (SHOW_TRANSACTIONS) Log.i(
2159 TAG, " SURFACE " + w.mSurface
2160 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 w.mSurface.setTransparentRegionHint(region);
2162 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002163 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 Surface.closeTransaction();
2165 }
2166 }
2167 }
2168 } finally {
2169 Binder.restoreCallingIdentity(origId);
2170 }
2171 }
2172
2173 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002174 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 Rect visibleInsets) {
2176 long origId = Binder.clearCallingIdentity();
2177 try {
2178 synchronized (mWindowMap) {
2179 WindowState w = windowForClientLocked(session, client);
2180 if (w != null) {
2181 w.mGivenInsetsPending = false;
2182 w.mGivenContentInsets.set(contentInsets);
2183 w.mGivenVisibleInsets.set(visibleInsets);
2184 w.mTouchableInsets = touchableInsets;
2185 mLayoutNeeded = true;
2186 performLayoutAndPlaceSurfacesLocked();
2187 }
2188 }
2189 } finally {
2190 Binder.restoreCallingIdentity(origId);
2191 }
2192 }
Romain Guy06882f82009-06-10 13:36:04 -07002193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 public void getWindowDisplayFrame(Session session, IWindow client,
2195 Rect outDisplayFrame) {
2196 synchronized(mWindowMap) {
2197 WindowState win = windowForClientLocked(session, client);
2198 if (win == null) {
2199 outDisplayFrame.setEmpty();
2200 return;
2201 }
2202 outDisplayFrame.set(win.mDisplayFrame);
2203 }
2204 }
2205
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002206 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2207 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002208 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2209 window.mWallpaperX = x;
2210 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002211 window.mWallpaperXStep = xStep;
2212 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002213 if (updateWallpaperOffsetLocked(window, true)) {
2214 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002215 }
2216 }
2217 }
2218
Dianne Hackborn75804932009-10-20 20:15:20 -07002219 void wallpaperCommandComplete(IBinder window, Bundle result) {
2220 synchronized (mWindowMap) {
2221 if (mWaitingOnWallpaper != null &&
2222 mWaitingOnWallpaper.mClient.asBinder() == window) {
2223 mWaitingOnWallpaper = null;
2224 mWindowMap.notifyAll();
2225 }
2226 }
2227 }
2228
2229 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2230 String action, int x, int y, int z, Bundle extras, boolean sync) {
2231 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2232 || window == mUpperWallpaperTarget) {
2233 boolean doWait = sync;
2234 int curTokenIndex = mWallpaperTokens.size();
2235 while (curTokenIndex > 0) {
2236 curTokenIndex--;
2237 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2238 int curWallpaperIndex = token.windows.size();
2239 while (curWallpaperIndex > 0) {
2240 curWallpaperIndex--;
2241 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2242 try {
2243 wallpaper.mClient.dispatchWallpaperCommand(action,
2244 x, y, z, extras, sync);
2245 // We only want to be synchronous with one wallpaper.
2246 sync = false;
2247 } catch (RemoteException e) {
2248 }
2249 }
2250 }
2251
2252 if (doWait) {
2253 // XXX Need to wait for result.
2254 }
2255 }
2256
2257 return null;
2258 }
2259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 public int relayoutWindow(Session session, IWindow client,
2261 WindowManager.LayoutParams attrs, int requestedWidth,
2262 int requestedHeight, int viewVisibility, boolean insetsPending,
2263 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2264 Surface outSurface) {
2265 boolean displayed = false;
2266 boolean inTouchMode;
2267 Configuration newConfig = null;
2268 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 synchronized(mWindowMap) {
2271 WindowState win = windowForClientLocked(session, client);
2272 if (win == null) {
2273 return 0;
2274 }
2275 win.mRequestedWidth = requestedWidth;
2276 win.mRequestedHeight = requestedHeight;
2277
2278 if (attrs != null) {
2279 mPolicy.adjustWindowParamsLw(attrs);
2280 }
Romain Guy06882f82009-06-10 13:36:04 -07002281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 int attrChanges = 0;
2283 int flagChanges = 0;
2284 if (attrs != null) {
2285 flagChanges = win.mAttrs.flags ^= attrs.flags;
2286 attrChanges = win.mAttrs.copyFrom(attrs);
2287 }
2288
2289 if (localLOGV) Log.v(
2290 TAG, "Relayout given client " + client.asBinder()
2291 + " (" + win.mAttrs.getTitle() + ")");
2292
2293
2294 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2295 win.mAlpha = attrs.alpha;
2296 }
2297
2298 final boolean scaledWindow =
2299 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2300
2301 if (scaledWindow) {
2302 // requested{Width|Height} Surface's physical size
2303 // attrs.{width|height} Size on screen
2304 win.mHScale = (attrs.width != requestedWidth) ?
2305 (attrs.width / (float)requestedWidth) : 1.0f;
2306 win.mVScale = (attrs.height != requestedHeight) ?
2307 (attrs.height / (float)requestedHeight) : 1.0f;
2308 }
2309
2310 boolean imMayMove = (flagChanges&(
2311 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2312 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 boolean focusMayChange = win.mViewVisibility != viewVisibility
2315 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2316 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002317
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002318 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2319 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 win.mRelayoutCalled = true;
2322 final int oldVisibility = win.mViewVisibility;
2323 win.mViewVisibility = viewVisibility;
2324 if (viewVisibility == View.VISIBLE &&
2325 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2326 displayed = !win.isVisibleLw();
2327 if (win.mExiting) {
2328 win.mExiting = false;
2329 win.mAnimation = null;
2330 }
2331 if (win.mDestroying) {
2332 win.mDestroying = false;
2333 mDestroySurface.remove(win);
2334 }
2335 if (oldVisibility == View.GONE) {
2336 win.mEnterAnimationPending = true;
2337 }
2338 if (displayed && win.mSurface != null && !win.mDrawPending
2339 && !win.mCommitDrawPending && !mDisplayFrozen) {
2340 applyEnterAnimationLocked(win);
2341 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002342 if (displayed && (win.mAttrs.flags
2343 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2344 win.mTurnOnScreen = true;
2345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2347 // To change the format, we need to re-build the surface.
2348 win.destroySurfaceLocked();
2349 displayed = true;
2350 }
2351 try {
2352 Surface surface = win.createSurfaceLocked();
2353 if (surface != null) {
2354 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002355 win.mReportDestroySurface = false;
2356 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002357 if (SHOW_TRANSACTIONS) Log.i(TAG,
2358 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002360 // For some reason there isn't a surface. Clear the
2361 // caller's object so they see the same state.
2362 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 }
2364 } catch (Exception e) {
2365 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002366 + client + " (" + win.mAttrs.getTitle() + ")",
2367 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 Binder.restoreCallingIdentity(origId);
2369 return 0;
2370 }
2371 if (displayed) {
2372 focusMayChange = true;
2373 }
2374 if (win.mAttrs.type == TYPE_INPUT_METHOD
2375 && mInputMethodWindow == null) {
2376 mInputMethodWindow = win;
2377 imMayMove = true;
2378 }
2379 } else {
2380 win.mEnterAnimationPending = false;
2381 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002382 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2383 + ": mExiting=" + win.mExiting
2384 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 // If we are not currently running the exit animation, we
2386 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002387 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 // Try starting an animation; if there isn't one, we
2389 // can destroy the surface right away.
2390 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2391 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2392 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2393 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002394 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002396 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 win.mExiting = true;
2398 mKeyWaiter.finishedKey(session, client, true,
2399 KeyWaiter.RETURN_NOTHING);
2400 } else if (win.isAnimating()) {
2401 // Currently in a hide animation... turn this into
2402 // an exit.
2403 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002404 } else if (win == mWallpaperTarget) {
2405 // If the wallpaper is currently behind this
2406 // window, we need to change both of them inside
2407 // of a transaction to avoid artifacts.
2408 win.mExiting = true;
2409 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 } else {
2411 if (mInputMethodWindow == win) {
2412 mInputMethodWindow = null;
2413 }
2414 win.destroySurfaceLocked();
2415 }
2416 }
2417 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002418
2419 if (win.mSurface == null || (win.getAttrs().flags
2420 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2421 || win.mSurfacePendingDestroy) {
2422 // We are being called from a local process, which
2423 // means outSurface holds its current surface. Ensure the
2424 // surface object is cleared, but we don't want it actually
2425 // destroyed at this point.
2426 win.mSurfacePendingDestroy = false;
2427 outSurface.release();
2428 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2429 } else if (win.mSurface != null) {
2430 if (DEBUG_VISIBILITY) Log.i(TAG,
2431 "Keeping surface, will report destroy: " + win);
2432 win.mReportDestroySurface = true;
2433 outSurface.copyFrom(win.mSurface);
2434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 }
2436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 if (focusMayChange) {
2438 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2439 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 imMayMove = false;
2441 }
2442 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2443 }
Romain Guy06882f82009-06-10 13:36:04 -07002444
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002445 // updateFocusedWindowLocked() already assigned layers so we only need to
2446 // reassign them at this point if the IM window state gets shuffled
2447 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 if (imMayMove) {
2450 if (moveInputMethodWindowsIfNeededLocked(false)) {
2451 assignLayers = true;
2452 }
2453 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002454 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002455 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002456 assignLayers = true;
2457 }
2458 }
Romain Guy06882f82009-06-10 13:36:04 -07002459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 mLayoutNeeded = true;
2461 win.mGivenInsetsPending = insetsPending;
2462 if (assignLayers) {
2463 assignLayersLocked();
2464 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002465 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002467 if (displayed && win.mIsWallpaper) {
2468 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002469 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 if (win.mAppToken != null) {
2472 win.mAppToken.updateReportedVisibilityLocked();
2473 }
2474 outFrame.set(win.mFrame);
2475 outContentInsets.set(win.mContentInsets);
2476 outVisibleInsets.set(win.mVisibleInsets);
2477 if (localLOGV) Log.v(
2478 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002479 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 + ", requestedHeight=" + requestedHeight
2481 + ", viewVisibility=" + viewVisibility
2482 + "\nRelayout returning frame=" + outFrame
2483 + ", surface=" + outSurface);
2484
2485 if (localLOGV || DEBUG_FOCUS) Log.v(
2486 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2487
2488 inTouchMode = mInTouchMode;
2489 }
2490
2491 if (newConfig != null) {
2492 sendNewConfiguration();
2493 }
Romain Guy06882f82009-06-10 13:36:04 -07002494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2498 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2499 }
2500
2501 public void finishDrawingWindow(Session session, IWindow client) {
2502 final long origId = Binder.clearCallingIdentity();
2503 synchronized(mWindowMap) {
2504 WindowState win = windowForClientLocked(session, client);
2505 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002506 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2507 adjustWallpaperWindowsLocked();
2508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 mLayoutNeeded = true;
2510 performLayoutAndPlaceSurfacesLocked();
2511 }
2512 }
2513 Binder.restoreCallingIdentity(origId);
2514 }
2515
2516 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2517 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2518 + (lp != null ? lp.packageName : null)
2519 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2520 if (lp != null && lp.windowAnimations != 0) {
2521 // If this is a system resource, don't try to load it from the
2522 // application resources. It is nice to avoid loading application
2523 // resources if we can.
2524 String packageName = lp.packageName != null ? lp.packageName : "android";
2525 int resId = lp.windowAnimations;
2526 if ((resId&0xFF000000) == 0x01000000) {
2527 packageName = "android";
2528 }
2529 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2530 + packageName);
2531 return AttributeCache.instance().get(packageName, resId,
2532 com.android.internal.R.styleable.WindowAnimation);
2533 }
2534 return null;
2535 }
Romain Guy06882f82009-06-10 13:36:04 -07002536
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002537 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2538 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2539 + packageName + " resId=0x" + Integer.toHexString(resId));
2540 if (packageName != null) {
2541 if ((resId&0xFF000000) == 0x01000000) {
2542 packageName = "android";
2543 }
2544 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2545 + packageName);
2546 return AttributeCache.instance().get(packageName, resId,
2547 com.android.internal.R.styleable.WindowAnimation);
2548 }
2549 return null;
2550 }
2551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 private void applyEnterAnimationLocked(WindowState win) {
2553 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2554 if (win.mEnterAnimationPending) {
2555 win.mEnterAnimationPending = false;
2556 transit = WindowManagerPolicy.TRANSIT_ENTER;
2557 }
2558
2559 applyAnimationLocked(win, transit, true);
2560 }
2561
2562 private boolean applyAnimationLocked(WindowState win,
2563 int transit, boolean isEntrance) {
2564 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2565 // If we are trying to apply an animation, but already running
2566 // an animation of the same type, then just leave that one alone.
2567 return true;
2568 }
Romain Guy06882f82009-06-10 13:36:04 -07002569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 // Only apply an animation if the display isn't frozen. If it is
2571 // frozen, there is no reason to animate and it can cause strange
2572 // artifacts when we unfreeze the display if some different animation
2573 // is running.
2574 if (!mDisplayFrozen) {
2575 int anim = mPolicy.selectAnimationLw(win, transit);
2576 int attr = -1;
2577 Animation a = null;
2578 if (anim != 0) {
2579 a = AnimationUtils.loadAnimation(mContext, anim);
2580 } else {
2581 switch (transit) {
2582 case WindowManagerPolicy.TRANSIT_ENTER:
2583 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2584 break;
2585 case WindowManagerPolicy.TRANSIT_EXIT:
2586 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2587 break;
2588 case WindowManagerPolicy.TRANSIT_SHOW:
2589 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2590 break;
2591 case WindowManagerPolicy.TRANSIT_HIDE:
2592 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2593 break;
2594 }
2595 if (attr >= 0) {
2596 a = loadAnimation(win.mAttrs, attr);
2597 }
2598 }
2599 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2600 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2601 + " mAnimation=" + win.mAnimation
2602 + " isEntrance=" + isEntrance);
2603 if (a != null) {
2604 if (DEBUG_ANIM) {
2605 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002606 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2608 }
2609 win.setAnimation(a);
2610 win.mAnimationIsEntrance = isEntrance;
2611 }
2612 } else {
2613 win.clearAnimation();
2614 }
2615
2616 return win.mAnimation != null;
2617 }
2618
2619 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2620 int anim = 0;
2621 Context context = mContext;
2622 if (animAttr >= 0) {
2623 AttributeCache.Entry ent = getCachedAnimations(lp);
2624 if (ent != null) {
2625 context = ent.context;
2626 anim = ent.array.getResourceId(animAttr, 0);
2627 }
2628 }
2629 if (anim != 0) {
2630 return AnimationUtils.loadAnimation(context, anim);
2631 }
2632 return null;
2633 }
Romain Guy06882f82009-06-10 13:36:04 -07002634
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002635 private Animation loadAnimation(String packageName, int resId) {
2636 int anim = 0;
2637 Context context = mContext;
2638 if (resId >= 0) {
2639 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2640 if (ent != null) {
2641 context = ent.context;
2642 anim = resId;
2643 }
2644 }
2645 if (anim != 0) {
2646 return AnimationUtils.loadAnimation(context, anim);
2647 }
2648 return null;
2649 }
2650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 private boolean applyAnimationLocked(AppWindowToken wtoken,
2652 WindowManager.LayoutParams lp, int transit, boolean enter) {
2653 // Only apply an animation if the display isn't frozen. If it is
2654 // frozen, there is no reason to animate and it can cause strange
2655 // artifacts when we unfreeze the display if some different animation
2656 // is running.
2657 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002658 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002659 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002660 a = new FadeInOutAnimation(enter);
2661 if (DEBUG_ANIM) Log.v(TAG,
2662 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002663 } else if (mNextAppTransitionPackage != null) {
2664 a = loadAnimation(mNextAppTransitionPackage, enter ?
2665 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002666 } else {
2667 int animAttr = 0;
2668 switch (transit) {
2669 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2670 animAttr = enter
2671 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2672 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2673 break;
2674 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2675 animAttr = enter
2676 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2677 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2678 break;
2679 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2680 animAttr = enter
2681 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2682 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2683 break;
2684 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2685 animAttr = enter
2686 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2687 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2688 break;
2689 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2690 animAttr = enter
2691 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2692 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2693 break;
2694 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2695 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002696 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002697 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2698 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002699 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002700 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002701 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2702 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002703 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002704 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002705 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002706 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2707 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2708 break;
2709 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2710 animAttr = enter
2711 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2712 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2713 break;
2714 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2715 animAttr = enter
2716 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2717 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002718 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002719 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002720 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002721 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2722 + " anim=" + a
2723 + " animAttr=0x" + Integer.toHexString(animAttr)
2724 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 if (a != null) {
2727 if (DEBUG_ANIM) {
2728 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002729 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2731 }
2732 wtoken.setAnimation(a);
2733 }
2734 } else {
2735 wtoken.clearAnimation();
2736 }
2737
2738 return wtoken.animation != null;
2739 }
2740
2741 // -------------------------------------------------------------
2742 // Application Window Tokens
2743 // -------------------------------------------------------------
2744
2745 public void validateAppTokens(List tokens) {
2746 int v = tokens.size()-1;
2747 int m = mAppTokens.size()-1;
2748 while (v >= 0 && m >= 0) {
2749 AppWindowToken wtoken = mAppTokens.get(m);
2750 if (wtoken.removed) {
2751 m--;
2752 continue;
2753 }
2754 if (tokens.get(v) != wtoken.token) {
2755 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2756 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2757 }
2758 v--;
2759 m--;
2760 }
2761 while (v >= 0) {
2762 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2763 v--;
2764 }
2765 while (m >= 0) {
2766 AppWindowToken wtoken = mAppTokens.get(m);
2767 if (!wtoken.removed) {
2768 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2769 }
2770 m--;
2771 }
2772 }
2773
2774 boolean checkCallingPermission(String permission, String func) {
2775 // Quick check: if the calling permission is me, it's all okay.
2776 if (Binder.getCallingPid() == Process.myPid()) {
2777 return true;
2778 }
Romain Guy06882f82009-06-10 13:36:04 -07002779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 if (mContext.checkCallingPermission(permission)
2781 == PackageManager.PERMISSION_GRANTED) {
2782 return true;
2783 }
2784 String msg = "Permission Denial: " + func + " from pid="
2785 + Binder.getCallingPid()
2786 + ", uid=" + Binder.getCallingUid()
2787 + " requires " + permission;
2788 Log.w(TAG, msg);
2789 return false;
2790 }
Romain Guy06882f82009-06-10 13:36:04 -07002791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 AppWindowToken findAppWindowToken(IBinder token) {
2793 WindowToken wtoken = mTokenMap.get(token);
2794 if (wtoken == null) {
2795 return null;
2796 }
2797 return wtoken.appWindowToken;
2798 }
Romain Guy06882f82009-06-10 13:36:04 -07002799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 public void addWindowToken(IBinder token, int type) {
2801 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2802 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002803 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 }
Romain Guy06882f82009-06-10 13:36:04 -07002805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 synchronized(mWindowMap) {
2807 WindowToken wtoken = mTokenMap.get(token);
2808 if (wtoken != null) {
2809 Log.w(TAG, "Attempted to add existing input method token: " + token);
2810 return;
2811 }
2812 wtoken = new WindowToken(token, type, true);
2813 mTokenMap.put(token, wtoken);
2814 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002815 if (type == TYPE_WALLPAPER) {
2816 mWallpaperTokens.add(wtoken);
2817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 }
2819 }
Romain Guy06882f82009-06-10 13:36:04 -07002820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 public void removeWindowToken(IBinder token) {
2822 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2823 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002824 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 }
2826
2827 final long origId = Binder.clearCallingIdentity();
2828 synchronized(mWindowMap) {
2829 WindowToken wtoken = mTokenMap.remove(token);
2830 mTokenList.remove(wtoken);
2831 if (wtoken != null) {
2832 boolean delayed = false;
2833 if (!wtoken.hidden) {
2834 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 final int N = wtoken.windows.size();
2837 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 for (int i=0; i<N; i++) {
2840 WindowState win = wtoken.windows.get(i);
2841
2842 if (win.isAnimating()) {
2843 delayed = true;
2844 }
Romain Guy06882f82009-06-10 13:36:04 -07002845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 if (win.isVisibleNow()) {
2847 applyAnimationLocked(win,
2848 WindowManagerPolicy.TRANSIT_EXIT, false);
2849 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2850 KeyWaiter.RETURN_NOTHING);
2851 changed = true;
2852 }
2853 }
2854
2855 if (changed) {
2856 mLayoutNeeded = true;
2857 performLayoutAndPlaceSurfacesLocked();
2858 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2859 }
Romain Guy06882f82009-06-10 13:36:04 -07002860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 if (delayed) {
2862 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002863 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2864 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 }
2866 }
Romain Guy06882f82009-06-10 13:36:04 -07002867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 } else {
2869 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2870 }
2871 }
2872 Binder.restoreCallingIdentity(origId);
2873 }
2874
2875 public void addAppToken(int addPos, IApplicationToken token,
2876 int groupId, int requestedOrientation, boolean fullscreen) {
2877 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2878 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002879 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 }
Romain Guy06882f82009-06-10 13:36:04 -07002881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 synchronized(mWindowMap) {
2883 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2884 if (wtoken != null) {
2885 Log.w(TAG, "Attempted to add existing app token: " + token);
2886 return;
2887 }
2888 wtoken = new AppWindowToken(token);
2889 wtoken.groupId = groupId;
2890 wtoken.appFullscreen = fullscreen;
2891 wtoken.requestedOrientation = requestedOrientation;
2892 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002893 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 mTokenMap.put(token.asBinder(), wtoken);
2895 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 // Application tokens start out hidden.
2898 wtoken.hidden = true;
2899 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 //dump();
2902 }
2903 }
Romain Guy06882f82009-06-10 13:36:04 -07002904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 public void setAppGroupId(IBinder token, int groupId) {
2906 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2907 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002908 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 }
2910
2911 synchronized(mWindowMap) {
2912 AppWindowToken wtoken = findAppWindowToken(token);
2913 if (wtoken == null) {
2914 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2915 return;
2916 }
2917 wtoken.groupId = groupId;
2918 }
2919 }
Romain Guy06882f82009-06-10 13:36:04 -07002920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 public int getOrientationFromWindowsLocked() {
2922 int pos = mWindows.size() - 1;
2923 while (pos >= 0) {
2924 WindowState wtoken = (WindowState) mWindows.get(pos);
2925 pos--;
2926 if (wtoken.mAppToken != null) {
2927 // We hit an application window. so the orientation will be determined by the
2928 // app window. No point in continuing further.
2929 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2930 }
2931 if (!wtoken.isVisibleLw()) {
2932 continue;
2933 }
2934 int req = wtoken.mAttrs.screenOrientation;
2935 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2936 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2937 continue;
2938 } else {
2939 return req;
2940 }
2941 }
2942 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2943 }
Romain Guy06882f82009-06-10 13:36:04 -07002944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 public int getOrientationFromAppTokensLocked() {
2946 int pos = mAppTokens.size() - 1;
2947 int curGroup = 0;
2948 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002949 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002951 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 while (pos >= 0) {
2953 AppWindowToken wtoken = mAppTokens.get(pos);
2954 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002955 // if we're about to tear down this window and not seek for
2956 // the behind activity, don't use it for orientation
2957 if (!findingBehind
2958 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002959 continue;
2960 }
2961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 if (!haveGroup) {
2963 // We ignore any hidden applications on the top.
2964 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2965 continue;
2966 }
2967 haveGroup = true;
2968 curGroup = wtoken.groupId;
2969 lastOrientation = wtoken.requestedOrientation;
2970 } else if (curGroup != wtoken.groupId) {
2971 // If we have hit a new application group, and the bottom
2972 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002973 // the orientation behind it, and the last app was
2974 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002976 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2977 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 return lastOrientation;
2979 }
2980 }
2981 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002982 // If this application is fullscreen, and didn't explicitly say
2983 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002985 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002986 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002987 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 return or;
2989 }
2990 // If this application has requested an explicit orientation,
2991 // then use it.
2992 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2993 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2994 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2995 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2996 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2997 return or;
2998 }
Owen Lin3413b892009-05-01 17:12:32 -07002999 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 }
3001 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3002 }
Romain Guy06882f82009-06-10 13:36:04 -07003003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003005 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003006 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3007 "updateOrientationFromAppTokens()")) {
3008 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3009 }
3010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 Configuration config;
3012 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003013 config = updateOrientationFromAppTokensUnchecked(currentConfig,
3014 freezeThisOneIfNeeded);
3015 Binder.restoreCallingIdentity(ident);
3016 return config;
3017 }
3018
3019 Configuration updateOrientationFromAppTokensUnchecked(
3020 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
3021 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003023 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003024 if (config != null) {
3025 mLayoutNeeded = true;
3026 performLayoutAndPlaceSurfacesLocked();
3027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 return config;
3030 }
Romain Guy06882f82009-06-10 13:36:04 -07003031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 /*
3033 * The orientation is computed from non-application windows first. If none of
3034 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003035 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3037 * android.os.IBinder)
3038 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003039 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07003040 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 long ident = Binder.clearCallingIdentity();
3043 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003044 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 if (req != mForcedAppOrientation) {
3047 changed = true;
3048 mForcedAppOrientation = req;
3049 //send a message to Policy indicating orientation change to take
3050 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003051 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 }
Romain Guy06882f82009-06-10 13:36:04 -07003053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 if (changed) {
3055 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07003056 WindowManagerPolicy.USE_LAST_ROTATION,
3057 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 if (changed) {
3059 if (freezeThisOneIfNeeded != null) {
3060 AppWindowToken wtoken = findAppWindowToken(
3061 freezeThisOneIfNeeded);
3062 if (wtoken != null) {
3063 startAppFreezingScreenLocked(wtoken,
3064 ActivityInfo.CONFIG_ORIENTATION);
3065 }
3066 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003067 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 }
3069 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003070
3071 // No obvious action we need to take, but if our current
3072 // state mismatches the activity maanager's, update it
3073 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003074 mTempConfiguration.setToDefaults();
3075 if (computeNewConfigurationLocked(mTempConfiguration)) {
3076 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003077 return new Configuration(mTempConfiguration);
3078 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003079 }
3080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 } finally {
3082 Binder.restoreCallingIdentity(ident);
3083 }
Romain Guy06882f82009-06-10 13:36:04 -07003084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 return null;
3086 }
Romain Guy06882f82009-06-10 13:36:04 -07003087
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003088 int computeForcedAppOrientationLocked() {
3089 int req = getOrientationFromWindowsLocked();
3090 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3091 req = getOrientationFromAppTokensLocked();
3092 }
3093 return req;
3094 }
Romain Guy06882f82009-06-10 13:36:04 -07003095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3097 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3098 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003099 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 }
Romain Guy06882f82009-06-10 13:36:04 -07003101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 synchronized(mWindowMap) {
3103 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3104 if (wtoken == null) {
3105 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3106 return;
3107 }
Romain Guy06882f82009-06-10 13:36:04 -07003108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 wtoken.requestedOrientation = requestedOrientation;
3110 }
3111 }
Romain Guy06882f82009-06-10 13:36:04 -07003112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 public int getAppOrientation(IApplicationToken token) {
3114 synchronized(mWindowMap) {
3115 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3116 if (wtoken == null) {
3117 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3118 }
Romain Guy06882f82009-06-10 13:36:04 -07003119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 return wtoken.requestedOrientation;
3121 }
3122 }
Romain Guy06882f82009-06-10 13:36:04 -07003123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3125 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3126 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003127 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 }
3129
3130 synchronized(mWindowMap) {
3131 boolean changed = false;
3132 if (token == null) {
3133 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3134 changed = mFocusedApp != null;
3135 mFocusedApp = null;
3136 mKeyWaiter.tickle();
3137 } else {
3138 AppWindowToken newFocus = findAppWindowToken(token);
3139 if (newFocus == null) {
3140 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3141 return;
3142 }
3143 changed = mFocusedApp != newFocus;
3144 mFocusedApp = newFocus;
3145 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3146 mKeyWaiter.tickle();
3147 }
3148
3149 if (moveFocusNow && changed) {
3150 final long origId = Binder.clearCallingIdentity();
3151 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3152 Binder.restoreCallingIdentity(origId);
3153 }
3154 }
3155 }
3156
3157 public void prepareAppTransition(int transit) {
3158 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3159 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003160 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 }
Romain Guy06882f82009-06-10 13:36:04 -07003162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 synchronized(mWindowMap) {
3164 if (DEBUG_APP_TRANSITIONS) Log.v(
3165 TAG, "Prepare app transition: transit=" + transit
3166 + " mNextAppTransition=" + mNextAppTransition);
3167 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003168 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3169 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003171 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3172 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3173 // Opening a new task always supersedes a close for the anim.
3174 mNextAppTransition = transit;
3175 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3176 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3177 // Opening a new activity always supersedes a close for the anim.
3178 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 }
3180 mAppTransitionReady = false;
3181 mAppTransitionTimeout = false;
3182 mStartingIconInTransition = false;
3183 mSkipAppTransitionAnimation = false;
3184 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3185 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3186 5000);
3187 }
3188 }
3189 }
3190
3191 public int getPendingAppTransition() {
3192 return mNextAppTransition;
3193 }
Romain Guy06882f82009-06-10 13:36:04 -07003194
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003195 public void overridePendingAppTransition(String packageName,
3196 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003197 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003198 mNextAppTransitionPackage = packageName;
3199 mNextAppTransitionEnter = enterAnim;
3200 mNextAppTransitionExit = exitAnim;
3201 }
3202 }
3203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 public void executeAppTransition() {
3205 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3206 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003207 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 }
Romain Guy06882f82009-06-10 13:36:04 -07003209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003211 if (DEBUG_APP_TRANSITIONS) {
3212 RuntimeException e = new RuntimeException("here");
3213 e.fillInStackTrace();
3214 Log.w(TAG, "Execute app transition: mNextAppTransition="
3215 + mNextAppTransition, e);
3216 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003217 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 mAppTransitionReady = true;
3219 final long origId = Binder.clearCallingIdentity();
3220 performLayoutAndPlaceSurfacesLocked();
3221 Binder.restoreCallingIdentity(origId);
3222 }
3223 }
3224 }
3225
3226 public void setAppStartingWindow(IBinder token, String pkg,
3227 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3228 IBinder transferFrom, boolean createIfNeeded) {
3229 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3230 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003231 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 }
3233
3234 synchronized(mWindowMap) {
3235 if (DEBUG_STARTING_WINDOW) Log.v(
3236 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3237 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 AppWindowToken wtoken = findAppWindowToken(token);
3240 if (wtoken == null) {
3241 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3242 return;
3243 }
3244
3245 // If the display is frozen, we won't do anything until the
3246 // actual window is displayed so there is no reason to put in
3247 // the starting window.
3248 if (mDisplayFrozen) {
3249 return;
3250 }
Romain Guy06882f82009-06-10 13:36:04 -07003251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 if (wtoken.startingData != null) {
3253 return;
3254 }
Romain Guy06882f82009-06-10 13:36:04 -07003255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 if (transferFrom != null) {
3257 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3258 if (ttoken != null) {
3259 WindowState startingWindow = ttoken.startingWindow;
3260 if (startingWindow != null) {
3261 if (mStartingIconInTransition) {
3262 // In this case, the starting icon has already
3263 // been displayed, so start letting windows get
3264 // shown immediately without any more transitions.
3265 mSkipAppTransitionAnimation = true;
3266 }
3267 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3268 "Moving existing starting from " + ttoken
3269 + " to " + wtoken);
3270 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 // Transfer the starting window over to the new
3273 // token.
3274 wtoken.startingData = ttoken.startingData;
3275 wtoken.startingView = ttoken.startingView;
3276 wtoken.startingWindow = startingWindow;
3277 ttoken.startingData = null;
3278 ttoken.startingView = null;
3279 ttoken.startingWindow = null;
3280 ttoken.startingMoved = true;
3281 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003282 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003284 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3285 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 mWindows.remove(startingWindow);
3287 ttoken.windows.remove(startingWindow);
3288 ttoken.allAppWindows.remove(startingWindow);
3289 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 // Propagate other interesting state between the
3292 // tokens. If the old token is displayed, we should
3293 // immediately force the new one to be displayed. If
3294 // it is animating, we need to move that animation to
3295 // the new one.
3296 if (ttoken.allDrawn) {
3297 wtoken.allDrawn = true;
3298 }
3299 if (ttoken.firstWindowDrawn) {
3300 wtoken.firstWindowDrawn = true;
3301 }
3302 if (!ttoken.hidden) {
3303 wtoken.hidden = false;
3304 wtoken.hiddenRequested = false;
3305 wtoken.willBeHidden = false;
3306 }
3307 if (wtoken.clientHidden != ttoken.clientHidden) {
3308 wtoken.clientHidden = ttoken.clientHidden;
3309 wtoken.sendAppVisibilityToClients();
3310 }
3311 if (ttoken.animation != null) {
3312 wtoken.animation = ttoken.animation;
3313 wtoken.animating = ttoken.animating;
3314 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3315 ttoken.animation = null;
3316 ttoken.animLayerAdjustment = 0;
3317 wtoken.updateLayers();
3318 ttoken.updateLayers();
3319 }
Romain Guy06882f82009-06-10 13:36:04 -07003320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 mLayoutNeeded = true;
3323 performLayoutAndPlaceSurfacesLocked();
3324 Binder.restoreCallingIdentity(origId);
3325 return;
3326 } else if (ttoken.startingData != null) {
3327 // The previous app was getting ready to show a
3328 // starting window, but hasn't yet done so. Steal it!
3329 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3330 "Moving pending starting from " + ttoken
3331 + " to " + wtoken);
3332 wtoken.startingData = ttoken.startingData;
3333 ttoken.startingData = null;
3334 ttoken.startingMoved = true;
3335 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3336 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3337 // want to process the message ASAP, before any other queued
3338 // messages.
3339 mH.sendMessageAtFrontOfQueue(m);
3340 return;
3341 }
3342 }
3343 }
3344
3345 // There is no existing starting window, and the caller doesn't
3346 // want us to create one, so that's it!
3347 if (!createIfNeeded) {
3348 return;
3349 }
Romain Guy06882f82009-06-10 13:36:04 -07003350
Dianne Hackborn284ac932009-08-28 10:34:25 -07003351 // If this is a translucent or wallpaper window, then don't
3352 // show a starting window -- the current effect (a full-screen
3353 // opaque starting window that fades away to the real contents
3354 // when it is ready) does not work for this.
3355 if (theme != 0) {
3356 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3357 com.android.internal.R.styleable.Window);
3358 if (ent.array.getBoolean(
3359 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3360 return;
3361 }
3362 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003363 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3364 return;
3365 }
3366 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003367 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3368 return;
3369 }
3370 }
3371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 mStartingIconInTransition = true;
3373 wtoken.startingData = new StartingData(
3374 pkg, theme, nonLocalizedLabel,
3375 labelRes, icon);
3376 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3377 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3378 // want to process the message ASAP, before any other queued
3379 // messages.
3380 mH.sendMessageAtFrontOfQueue(m);
3381 }
3382 }
3383
3384 public void setAppWillBeHidden(IBinder token) {
3385 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3386 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003387 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 }
3389
3390 AppWindowToken wtoken;
3391
3392 synchronized(mWindowMap) {
3393 wtoken = findAppWindowToken(token);
3394 if (wtoken == null) {
3395 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3396 return;
3397 }
3398 wtoken.willBeHidden = true;
3399 }
3400 }
Romain Guy06882f82009-06-10 13:36:04 -07003401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3403 boolean visible, int transit, boolean performLayout) {
3404 boolean delayed = false;
3405
3406 if (wtoken.clientHidden == visible) {
3407 wtoken.clientHidden = !visible;
3408 wtoken.sendAppVisibilityToClients();
3409 }
Romain Guy06882f82009-06-10 13:36:04 -07003410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 wtoken.willBeHidden = false;
3412 if (wtoken.hidden == visible) {
3413 final int N = wtoken.allAppWindows.size();
3414 boolean changed = false;
3415 if (DEBUG_APP_TRANSITIONS) Log.v(
3416 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3417 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003420
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003421 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 if (wtoken.animation == sDummyAnimation) {
3423 wtoken.animation = null;
3424 }
3425 applyAnimationLocked(wtoken, lp, transit, visible);
3426 changed = true;
3427 if (wtoken.animation != null) {
3428 delayed = runningAppAnimation = true;
3429 }
3430 }
Romain Guy06882f82009-06-10 13:36:04 -07003431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 for (int i=0; i<N; i++) {
3433 WindowState win = wtoken.allAppWindows.get(i);
3434 if (win == wtoken.startingWindow) {
3435 continue;
3436 }
3437
3438 if (win.isAnimating()) {
3439 delayed = true;
3440 }
Romain Guy06882f82009-06-10 13:36:04 -07003441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3443 //win.dump(" ");
3444 if (visible) {
3445 if (!win.isVisibleNow()) {
3446 if (!runningAppAnimation) {
3447 applyAnimationLocked(win,
3448 WindowManagerPolicy.TRANSIT_ENTER, true);
3449 }
3450 changed = true;
3451 }
3452 } else if (win.isVisibleNow()) {
3453 if (!runningAppAnimation) {
3454 applyAnimationLocked(win,
3455 WindowManagerPolicy.TRANSIT_EXIT, false);
3456 }
3457 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3458 KeyWaiter.RETURN_NOTHING);
3459 changed = true;
3460 }
3461 }
3462
3463 wtoken.hidden = wtoken.hiddenRequested = !visible;
3464 if (!visible) {
3465 unsetAppFreezingScreenLocked(wtoken, true, true);
3466 } else {
3467 // If we are being set visible, and the starting window is
3468 // not yet displayed, then make sure it doesn't get displayed.
3469 WindowState swin = wtoken.startingWindow;
3470 if (swin != null && (swin.mDrawPending
3471 || swin.mCommitDrawPending)) {
3472 swin.mPolicyVisibility = false;
3473 swin.mPolicyVisibilityAfterAnim = false;
3474 }
3475 }
Romain Guy06882f82009-06-10 13:36:04 -07003476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3478 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3479 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 if (changed && performLayout) {
3482 mLayoutNeeded = true;
3483 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 performLayoutAndPlaceSurfacesLocked();
3485 }
3486 }
3487
3488 if (wtoken.animation != null) {
3489 delayed = true;
3490 }
Romain Guy06882f82009-06-10 13:36:04 -07003491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 return delayed;
3493 }
3494
3495 public void setAppVisibility(IBinder token, boolean visible) {
3496 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3497 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003498 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 }
3500
3501 AppWindowToken wtoken;
3502
3503 synchronized(mWindowMap) {
3504 wtoken = findAppWindowToken(token);
3505 if (wtoken == null) {
3506 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3507 return;
3508 }
3509
3510 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3511 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003512 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3514 + "): mNextAppTransition=" + mNextAppTransition
3515 + " hidden=" + wtoken.hidden
3516 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3517 }
Romain Guy06882f82009-06-10 13:36:04 -07003518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 // If we are preparing an app transition, then delay changing
3520 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003521 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 // Already in requested state, don't do anything more.
3523 if (wtoken.hiddenRequested != visible) {
3524 return;
3525 }
3526 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 if (DEBUG_APP_TRANSITIONS) Log.v(
3529 TAG, "Setting dummy animation on: " + wtoken);
3530 wtoken.setDummyAnimation();
3531 mOpeningApps.remove(wtoken);
3532 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003533 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 wtoken.inPendingTransaction = true;
3535 if (visible) {
3536 mOpeningApps.add(wtoken);
3537 wtoken.allDrawn = false;
3538 wtoken.startingDisplayed = false;
3539 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003540 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 if (wtoken.clientHidden) {
3543 // In the case where we are making an app visible
3544 // but holding off for a transition, we still need
3545 // to tell the client to make its windows visible so
3546 // they get drawn. Otherwise, we will wait on
3547 // performing the transition until all windows have
3548 // been drawn, they never will be, and we are sad.
3549 wtoken.clientHidden = false;
3550 wtoken.sendAppVisibilityToClients();
3551 }
3552 } else {
3553 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003554 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 }
3556 return;
3557 }
Romain Guy06882f82009-06-10 13:36:04 -07003558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003560 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 wtoken.updateReportedVisibilityLocked();
3562 Binder.restoreCallingIdentity(origId);
3563 }
3564 }
3565
3566 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3567 boolean unfreezeSurfaceNow, boolean force) {
3568 if (wtoken.freezingScreen) {
3569 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3570 + " force=" + force);
3571 final int N = wtoken.allAppWindows.size();
3572 boolean unfrozeWindows = false;
3573 for (int i=0; i<N; i++) {
3574 WindowState w = wtoken.allAppWindows.get(i);
3575 if (w.mAppFreezing) {
3576 w.mAppFreezing = false;
3577 if (w.mSurface != null && !w.mOrientationChanging) {
3578 w.mOrientationChanging = true;
3579 }
3580 unfrozeWindows = true;
3581 }
3582 }
3583 if (force || unfrozeWindows) {
3584 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3585 wtoken.freezingScreen = false;
3586 mAppsFreezingScreen--;
3587 }
3588 if (unfreezeSurfaceNow) {
3589 if (unfrozeWindows) {
3590 mLayoutNeeded = true;
3591 performLayoutAndPlaceSurfacesLocked();
3592 }
3593 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3594 stopFreezingDisplayLocked();
3595 }
3596 }
3597 }
3598 }
Romain Guy06882f82009-06-10 13:36:04 -07003599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3601 int configChanges) {
3602 if (DEBUG_ORIENTATION) {
3603 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003604 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 Log.i(TAG, "Set freezing of " + wtoken.appToken
3606 + ": hidden=" + wtoken.hidden + " freezing="
3607 + wtoken.freezingScreen, e);
3608 }
3609 if (!wtoken.hiddenRequested) {
3610 if (!wtoken.freezingScreen) {
3611 wtoken.freezingScreen = true;
3612 mAppsFreezingScreen++;
3613 if (mAppsFreezingScreen == 1) {
3614 startFreezingDisplayLocked();
3615 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3616 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3617 5000);
3618 }
3619 }
3620 final int N = wtoken.allAppWindows.size();
3621 for (int i=0; i<N; i++) {
3622 WindowState w = wtoken.allAppWindows.get(i);
3623 w.mAppFreezing = true;
3624 }
3625 }
3626 }
Romain Guy06882f82009-06-10 13:36:04 -07003627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 public void startAppFreezingScreen(IBinder token, int configChanges) {
3629 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3630 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003631 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 }
3633
3634 synchronized(mWindowMap) {
3635 if (configChanges == 0 && !mDisplayFrozen) {
3636 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3637 return;
3638 }
Romain Guy06882f82009-06-10 13:36:04 -07003639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 AppWindowToken wtoken = findAppWindowToken(token);
3641 if (wtoken == null || wtoken.appToken == null) {
3642 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3643 return;
3644 }
3645 final long origId = Binder.clearCallingIdentity();
3646 startAppFreezingScreenLocked(wtoken, configChanges);
3647 Binder.restoreCallingIdentity(origId);
3648 }
3649 }
Romain Guy06882f82009-06-10 13:36:04 -07003650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 public void stopAppFreezingScreen(IBinder token, boolean force) {
3652 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3653 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003654 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 }
3656
3657 synchronized(mWindowMap) {
3658 AppWindowToken wtoken = findAppWindowToken(token);
3659 if (wtoken == null || wtoken.appToken == null) {
3660 return;
3661 }
3662 final long origId = Binder.clearCallingIdentity();
3663 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3664 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3665 unsetAppFreezingScreenLocked(wtoken, true, force);
3666 Binder.restoreCallingIdentity(origId);
3667 }
3668 }
Romain Guy06882f82009-06-10 13:36:04 -07003669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 public void removeAppToken(IBinder token) {
3671 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3672 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003673 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 }
3675
3676 AppWindowToken wtoken = null;
3677 AppWindowToken startingToken = null;
3678 boolean delayed = false;
3679
3680 final long origId = Binder.clearCallingIdentity();
3681 synchronized(mWindowMap) {
3682 WindowToken basewtoken = mTokenMap.remove(token);
3683 mTokenList.remove(basewtoken);
3684 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3685 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003686 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 wtoken.inPendingTransaction = false;
3688 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003689 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 if (mClosingApps.contains(wtoken)) {
3691 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003692 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003694 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 delayed = true;
3696 }
3697 if (DEBUG_APP_TRANSITIONS) Log.v(
3698 TAG, "Removing app " + wtoken + " delayed=" + delayed
3699 + " animation=" + wtoken.animation
3700 + " animating=" + wtoken.animating);
3701 if (delayed) {
3702 // set the token aside because it has an active animation to be finished
3703 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003704 } else {
3705 // Make sure there is no animation running on this token,
3706 // so any windows associated with it will be removed as
3707 // soon as their animations are complete
3708 wtoken.animation = null;
3709 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 }
3711 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003712 if (mLastEnterAnimToken == wtoken) {
3713 mLastEnterAnimToken = null;
3714 mLastEnterAnimParams = null;
3715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 wtoken.removed = true;
3717 if (wtoken.startingData != null) {
3718 startingToken = wtoken;
3719 }
3720 unsetAppFreezingScreenLocked(wtoken, true, true);
3721 if (mFocusedApp == wtoken) {
3722 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3723 mFocusedApp = null;
3724 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3725 mKeyWaiter.tickle();
3726 }
3727 } else {
3728 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3729 }
Romain Guy06882f82009-06-10 13:36:04 -07003730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 if (!delayed && wtoken != null) {
3732 wtoken.updateReportedVisibilityLocked();
3733 }
3734 }
3735 Binder.restoreCallingIdentity(origId);
3736
3737 if (startingToken != null) {
3738 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3739 + startingToken + ": app token removed");
3740 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3741 mH.sendMessage(m);
3742 }
3743 }
3744
3745 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3746 final int NW = token.windows.size();
3747 for (int i=0; i<NW; i++) {
3748 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003749 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 mWindows.remove(win);
3751 int j = win.mChildWindows.size();
3752 while (j > 0) {
3753 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003754 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3755 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3756 "Tmp removing child window " + cwin);
3757 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 }
3759 }
3760 return NW > 0;
3761 }
3762
3763 void dumpAppTokensLocked() {
3764 for (int i=mAppTokens.size()-1; i>=0; i--) {
3765 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3766 }
3767 }
Romain Guy06882f82009-06-10 13:36:04 -07003768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 void dumpWindowsLocked() {
3770 for (int i=mWindows.size()-1; i>=0; i--) {
3771 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3772 }
3773 }
Romain Guy06882f82009-06-10 13:36:04 -07003774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 private int findWindowOffsetLocked(int tokenPos) {
3776 final int NW = mWindows.size();
3777
3778 if (tokenPos >= mAppTokens.size()) {
3779 int i = NW;
3780 while (i > 0) {
3781 i--;
3782 WindowState win = (WindowState)mWindows.get(i);
3783 if (win.getAppToken() != null) {
3784 return i+1;
3785 }
3786 }
3787 }
3788
3789 while (tokenPos > 0) {
3790 // Find the first app token below the new position that has
3791 // a window displayed.
3792 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3793 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3794 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003795 if (wtoken.sendingToBottom) {
3796 if (DEBUG_REORDER) Log.v(TAG,
3797 "Skipping token -- currently sending to bottom");
3798 tokenPos--;
3799 continue;
3800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 int i = wtoken.windows.size();
3802 while (i > 0) {
3803 i--;
3804 WindowState win = wtoken.windows.get(i);
3805 int j = win.mChildWindows.size();
3806 while (j > 0) {
3807 j--;
3808 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003809 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 for (int pos=NW-1; pos>=0; pos--) {
3811 if (mWindows.get(pos) == cwin) {
3812 if (DEBUG_REORDER) Log.v(TAG,
3813 "Found child win @" + (pos+1));
3814 return pos+1;
3815 }
3816 }
3817 }
3818 }
3819 for (int pos=NW-1; pos>=0; pos--) {
3820 if (mWindows.get(pos) == win) {
3821 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3822 return pos+1;
3823 }
3824 }
3825 }
3826 tokenPos--;
3827 }
3828
3829 return 0;
3830 }
3831
3832 private final int reAddWindowLocked(int index, WindowState win) {
3833 final int NCW = win.mChildWindows.size();
3834 boolean added = false;
3835 for (int j=0; j<NCW; j++) {
3836 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3837 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003838 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3839 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 mWindows.add(index, win);
3841 index++;
3842 added = true;
3843 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003844 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3845 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 mWindows.add(index, cwin);
3847 index++;
3848 }
3849 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003850 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3851 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 mWindows.add(index, win);
3853 index++;
3854 }
3855 return index;
3856 }
Romain Guy06882f82009-06-10 13:36:04 -07003857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3859 final int NW = token.windows.size();
3860 for (int i=0; i<NW; i++) {
3861 index = reAddWindowLocked(index, token.windows.get(i));
3862 }
3863 return index;
3864 }
3865
3866 public void moveAppToken(int index, IBinder token) {
3867 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3868 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003869 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 }
3871
3872 synchronized(mWindowMap) {
3873 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3874 if (DEBUG_REORDER) dumpAppTokensLocked();
3875 final AppWindowToken wtoken = findAppWindowToken(token);
3876 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3877 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3878 + token + " (" + wtoken + ")");
3879 return;
3880 }
3881 mAppTokens.add(index, wtoken);
3882 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3883 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 final long origId = Binder.clearCallingIdentity();
3886 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3887 if (DEBUG_REORDER) dumpWindowsLocked();
3888 if (tmpRemoveAppWindowsLocked(wtoken)) {
3889 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3890 if (DEBUG_REORDER) dumpWindowsLocked();
3891 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3892 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3893 if (DEBUG_REORDER) dumpWindowsLocked();
3894 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003895 mLayoutNeeded = true;
3896 performLayoutAndPlaceSurfacesLocked();
3897 }
3898 Binder.restoreCallingIdentity(origId);
3899 }
3900 }
3901
3902 private void removeAppTokensLocked(List<IBinder> tokens) {
3903 // XXX This should be done more efficiently!
3904 // (take advantage of the fact that both lists should be
3905 // ordered in the same way.)
3906 int N = tokens.size();
3907 for (int i=0; i<N; i++) {
3908 IBinder token = tokens.get(i);
3909 final AppWindowToken wtoken = findAppWindowToken(token);
3910 if (!mAppTokens.remove(wtoken)) {
3911 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3912 + token + " (" + wtoken + ")");
3913 i--;
3914 N--;
3915 }
3916 }
3917 }
3918
Dianne Hackborna8f60182009-09-01 19:01:50 -07003919 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3920 boolean updateFocusAndLayout) {
3921 // First remove all of the windows from the list.
3922 tmpRemoveAppWindowsLocked(wtoken);
3923
3924 // Where to start adding?
3925 int pos = findWindowOffsetLocked(tokenPos);
3926
3927 // And now add them back at the correct place.
3928 pos = reAddAppWindowsLocked(pos, wtoken);
3929
3930 if (updateFocusAndLayout) {
3931 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3932 assignLayersLocked();
3933 }
3934 mLayoutNeeded = true;
3935 performLayoutAndPlaceSurfacesLocked();
3936 }
3937 }
3938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3940 // First remove all of the windows from the list.
3941 final int N = tokens.size();
3942 int i;
3943 for (i=0; i<N; i++) {
3944 WindowToken token = mTokenMap.get(tokens.get(i));
3945 if (token != null) {
3946 tmpRemoveAppWindowsLocked(token);
3947 }
3948 }
3949
3950 // Where to start adding?
3951 int pos = findWindowOffsetLocked(tokenPos);
3952
3953 // And now add them back at the correct place.
3954 for (i=0; i<N; i++) {
3955 WindowToken token = mTokenMap.get(tokens.get(i));
3956 if (token != null) {
3957 pos = reAddAppWindowsLocked(pos, token);
3958 }
3959 }
3960
Dianne Hackborna8f60182009-09-01 19:01:50 -07003961 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3962 assignLayersLocked();
3963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 mLayoutNeeded = true;
3965 performLayoutAndPlaceSurfacesLocked();
3966
3967 //dump();
3968 }
3969
3970 public void moveAppTokensToTop(List<IBinder> tokens) {
3971 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3972 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003973 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 }
3975
3976 final long origId = Binder.clearCallingIdentity();
3977 synchronized(mWindowMap) {
3978 removeAppTokensLocked(tokens);
3979 final int N = tokens.size();
3980 for (int i=0; i<N; i++) {
3981 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3982 if (wt != null) {
3983 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003984 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003985 mToTopApps.remove(wt);
3986 mToBottomApps.remove(wt);
3987 mToTopApps.add(wt);
3988 wt.sendingToBottom = false;
3989 wt.sendingToTop = true;
3990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 }
3992 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003993
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003994 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003995 moveAppWindowsLocked(tokens, mAppTokens.size());
3996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 }
3998 Binder.restoreCallingIdentity(origId);
3999 }
4000
4001 public void moveAppTokensToBottom(List<IBinder> tokens) {
4002 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4003 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004004 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 }
4006
4007 final long origId = Binder.clearCallingIdentity();
4008 synchronized(mWindowMap) {
4009 removeAppTokensLocked(tokens);
4010 final int N = tokens.size();
4011 int pos = 0;
4012 for (int i=0; i<N; i++) {
4013 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4014 if (wt != null) {
4015 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004016 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004017 mToTopApps.remove(wt);
4018 mToBottomApps.remove(wt);
4019 mToBottomApps.add(i, wt);
4020 wt.sendingToTop = false;
4021 wt.sendingToBottom = true;
4022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 pos++;
4024 }
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, 0);
4029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 }
4031 Binder.restoreCallingIdentity(origId);
4032 }
4033
4034 // -------------------------------------------------------------
4035 // Misc IWindowSession methods
4036 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004039 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 != PackageManager.PERMISSION_GRANTED) {
4041 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4042 }
Mike Lockwooddd884682009-10-11 16:57:08 -04004043 synchronized (mKeyguardDisabled) {
4044 mKeyguardDisabled.acquire(token, tag);
4045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 }
4047
4048 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004049 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 != PackageManager.PERMISSION_GRANTED) {
4051 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4052 }
4053 synchronized (mKeyguardDisabled) {
4054 mKeyguardDisabled.release(token);
4055
4056 if (!mKeyguardDisabled.isAcquired()) {
4057 // if we are the last one to reenable the keyguard wait until
4058 // we have actaully finished reenabling until returning
4059 mWaitingUntilKeyguardReenabled = true;
4060 while (mWaitingUntilKeyguardReenabled) {
4061 try {
4062 mKeyguardDisabled.wait();
4063 } catch (InterruptedException e) {
4064 Thread.currentThread().interrupt();
4065 }
4066 }
4067 }
4068 }
4069 }
4070
4071 /**
4072 * @see android.app.KeyguardManager#exitKeyguardSecurely
4073 */
4074 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004075 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076 != PackageManager.PERMISSION_GRANTED) {
4077 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4078 }
4079 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4080 public void onKeyguardExitResult(boolean success) {
4081 try {
4082 callback.onKeyguardExitResult(success);
4083 } catch (RemoteException e) {
4084 // Client has died, we don't care.
4085 }
4086 }
4087 });
4088 }
4089
4090 public boolean inKeyguardRestrictedInputMode() {
4091 return mPolicy.inKeyguardRestrictedKeyInputMode();
4092 }
Romain Guy06882f82009-06-10 13:36:04 -07004093
Dianne Hackbornffa42482009-09-23 22:20:11 -07004094 public void closeSystemDialogs(String reason) {
4095 synchronized(mWindowMap) {
4096 for (int i=mWindows.size()-1; i>=0; i--) {
4097 WindowState w = (WindowState)mWindows.get(i);
4098 if (w.mSurface != null) {
4099 try {
4100 w.mClient.closeSystemDialogs(reason);
4101 } catch (RemoteException e) {
4102 }
4103 }
4104 }
4105 }
4106 }
4107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 static float fixScale(float scale) {
4109 if (scale < 0) scale = 0;
4110 else if (scale > 20) scale = 20;
4111 return Math.abs(scale);
4112 }
Romain Guy06882f82009-06-10 13:36:04 -07004113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 public void setAnimationScale(int which, float scale) {
4115 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4116 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004117 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 }
4119
4120 if (scale < 0) scale = 0;
4121 else if (scale > 20) scale = 20;
4122 scale = Math.abs(scale);
4123 switch (which) {
4124 case 0: mWindowAnimationScale = fixScale(scale); break;
4125 case 1: mTransitionAnimationScale = fixScale(scale); break;
4126 }
Romain Guy06882f82009-06-10 13:36:04 -07004127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 // Persist setting
4129 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4130 }
Romain Guy06882f82009-06-10 13:36:04 -07004131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 public void setAnimationScales(float[] scales) {
4133 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4134 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004135 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 }
4137
4138 if (scales != null) {
4139 if (scales.length >= 1) {
4140 mWindowAnimationScale = fixScale(scales[0]);
4141 }
4142 if (scales.length >= 2) {
4143 mTransitionAnimationScale = fixScale(scales[1]);
4144 }
4145 }
Romain Guy06882f82009-06-10 13:36:04 -07004146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 // Persist setting
4148 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4149 }
Romain Guy06882f82009-06-10 13:36:04 -07004150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 public float getAnimationScale(int which) {
4152 switch (which) {
4153 case 0: return mWindowAnimationScale;
4154 case 1: return mTransitionAnimationScale;
4155 }
4156 return 0;
4157 }
Romain Guy06882f82009-06-10 13:36:04 -07004158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 public float[] getAnimationScales() {
4160 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4161 }
Romain Guy06882f82009-06-10 13:36:04 -07004162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 public int getSwitchState(int sw) {
4164 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4165 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004166 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 }
4168 return KeyInputQueue.getSwitchState(sw);
4169 }
Romain Guy06882f82009-06-10 13:36:04 -07004170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 public int getSwitchStateForDevice(int devid, int sw) {
4172 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4173 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004174 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 }
4176 return KeyInputQueue.getSwitchState(devid, sw);
4177 }
Romain Guy06882f82009-06-10 13:36:04 -07004178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 public int getScancodeState(int sw) {
4180 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4181 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004182 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004184 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 }
Romain Guy06882f82009-06-10 13:36:04 -07004186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 public int getScancodeStateForDevice(int devid, int sw) {
4188 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4189 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004190 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004192 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 }
Romain Guy06882f82009-06-10 13:36:04 -07004194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 public int getKeycodeState(int sw) {
4196 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4197 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004198 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004200 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 }
Romain Guy06882f82009-06-10 13:36:04 -07004202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203 public int getKeycodeStateForDevice(int devid, int sw) {
4204 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4205 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004206 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004208 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 }
Romain Guy06882f82009-06-10 13:36:04 -07004210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4212 return KeyInputQueue.hasKeys(keycodes, keyExists);
4213 }
Romain Guy06882f82009-06-10 13:36:04 -07004214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215 public void enableScreenAfterBoot() {
4216 synchronized(mWindowMap) {
4217 if (mSystemBooted) {
4218 return;
4219 }
4220 mSystemBooted = true;
4221 }
Romain Guy06882f82009-06-10 13:36:04 -07004222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 performEnableScreen();
4224 }
Romain Guy06882f82009-06-10 13:36:04 -07004225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 public void enableScreenIfNeededLocked() {
4227 if (mDisplayEnabled) {
4228 return;
4229 }
4230 if (!mSystemBooted) {
4231 return;
4232 }
4233 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4234 }
Romain Guy06882f82009-06-10 13:36:04 -07004235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 public void performEnableScreen() {
4237 synchronized(mWindowMap) {
4238 if (mDisplayEnabled) {
4239 return;
4240 }
4241 if (!mSystemBooted) {
4242 return;
4243 }
Romain Guy06882f82009-06-10 13:36:04 -07004244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 // Don't enable the screen until all existing windows
4246 // have been drawn.
4247 final int N = mWindows.size();
4248 for (int i=0; i<N; i++) {
4249 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07004250 if (w.isVisibleLw() && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 return;
4252 }
4253 }
Romain Guy06882f82009-06-10 13:36:04 -07004254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 mDisplayEnabled = true;
4256 if (false) {
4257 Log.i(TAG, "ENABLING SCREEN!");
4258 StringWriter sw = new StringWriter();
4259 PrintWriter pw = new PrintWriter(sw);
4260 this.dump(null, pw, null);
4261 Log.i(TAG, sw.toString());
4262 }
4263 try {
4264 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4265 if (surfaceFlinger != null) {
4266 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4267 Parcel data = Parcel.obtain();
4268 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4269 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4270 data, null, 0);
4271 data.recycle();
4272 }
4273 } catch (RemoteException ex) {
4274 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4275 }
4276 }
Romain Guy06882f82009-06-10 13:36:04 -07004277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004281 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4282 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 }
Romain Guy06882f82009-06-10 13:36:04 -07004284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 public void setInTouchMode(boolean mode) {
4286 synchronized(mWindowMap) {
4287 mInTouchMode = mode;
4288 }
4289 }
4290
Romain Guy06882f82009-06-10 13:36:04 -07004291 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004292 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004294 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004295 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296 }
4297
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004298 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 }
Romain Guy06882f82009-06-10 13:36:04 -07004300
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004301 public void setRotationUnchecked(int rotation,
4302 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 if(DEBUG_ORIENTATION) Log.v(TAG,
4304 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 long origId = Binder.clearCallingIdentity();
4307 boolean changed;
4308 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004309 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 }
Romain Guy06882f82009-06-10 13:36:04 -07004311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 if (changed) {
4313 sendNewConfiguration();
4314 synchronized(mWindowMap) {
4315 mLayoutNeeded = true;
4316 performLayoutAndPlaceSurfacesLocked();
4317 }
4318 } else if (alwaysSendConfiguration) {
4319 //update configuration ignoring orientation change
4320 sendNewConfiguration();
4321 }
Romain Guy06882f82009-06-10 13:36:04 -07004322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 Binder.restoreCallingIdentity(origId);
4324 }
Romain Guy06882f82009-06-10 13:36:04 -07004325
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004326 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 boolean changed;
4328 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4329 rotation = mRequestedRotation;
4330 } else {
4331 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004332 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 }
4334 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004335 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 mRotation, mDisplayEnabled);
4337 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4338 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004341 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 "Rotation changed to " + rotation
4343 + " from " + mRotation
4344 + " (forceApp=" + mForcedAppOrientation
4345 + ", req=" + mRequestedRotation + ")");
4346 mRotation = rotation;
4347 mWindowsFreezingScreen = true;
4348 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4349 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4350 2000);
4351 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004352 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004353 mQueue.setOrientation(rotation);
4354 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004355 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 }
4357 for (int i=mWindows.size()-1; i>=0; i--) {
4358 WindowState w = (WindowState)mWindows.get(i);
4359 if (w.mSurface != null) {
4360 w.mOrientationChanging = true;
4361 }
4362 }
4363 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4364 try {
4365 mRotationWatchers.get(i).onRotationChanged(rotation);
4366 } catch (RemoteException e) {
4367 }
4368 }
4369 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 return changed;
4372 }
Romain Guy06882f82009-06-10 13:36:04 -07004373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 public int getRotation() {
4375 return mRotation;
4376 }
4377
4378 public int watchRotation(IRotationWatcher watcher) {
4379 final IBinder watcherBinder = watcher.asBinder();
4380 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4381 public void binderDied() {
4382 synchronized (mWindowMap) {
4383 for (int i=0; i<mRotationWatchers.size(); i++) {
4384 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004385 IRotationWatcher removed = mRotationWatchers.remove(i);
4386 if (removed != null) {
4387 removed.asBinder().unlinkToDeath(this, 0);
4388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004389 i--;
4390 }
4391 }
4392 }
4393 }
4394 };
Romain Guy06882f82009-06-10 13:36:04 -07004395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 synchronized (mWindowMap) {
4397 try {
4398 watcher.asBinder().linkToDeath(dr, 0);
4399 mRotationWatchers.add(watcher);
4400 } catch (RemoteException e) {
4401 // Client died, no cleanup needed.
4402 }
Romain Guy06882f82009-06-10 13:36:04 -07004403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 return mRotation;
4405 }
4406 }
4407
4408 /**
4409 * Starts the view server on the specified port.
4410 *
4411 * @param port The port to listener to.
4412 *
4413 * @return True if the server was successfully started, false otherwise.
4414 *
4415 * @see com.android.server.ViewServer
4416 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4417 */
4418 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004419 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 return false;
4421 }
4422
4423 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4424 return false;
4425 }
4426
4427 if (port < 1024) {
4428 return false;
4429 }
4430
4431 if (mViewServer != null) {
4432 if (!mViewServer.isRunning()) {
4433 try {
4434 return mViewServer.start();
4435 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004436 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437 }
4438 }
4439 return false;
4440 }
4441
4442 try {
4443 mViewServer = new ViewServer(this, port);
4444 return mViewServer.start();
4445 } catch (IOException e) {
4446 Log.w(TAG, "View server did not start");
4447 }
4448 return false;
4449 }
4450
Romain Guy06882f82009-06-10 13:36:04 -07004451 private boolean isSystemSecure() {
4452 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4453 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4454 }
4455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 /**
4457 * Stops the view server if it exists.
4458 *
4459 * @return True if the server stopped, false if it wasn't started or
4460 * couldn't be stopped.
4461 *
4462 * @see com.android.server.ViewServer
4463 */
4464 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004465 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 return false;
4467 }
4468
4469 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4470 return false;
4471 }
4472
4473 if (mViewServer != null) {
4474 return mViewServer.stop();
4475 }
4476 return false;
4477 }
4478
4479 /**
4480 * Indicates whether the view server is running.
4481 *
4482 * @return True if the server is running, false otherwise.
4483 *
4484 * @see com.android.server.ViewServer
4485 */
4486 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004487 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004488 return false;
4489 }
4490
4491 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4492 return false;
4493 }
4494
4495 return mViewServer != null && mViewServer.isRunning();
4496 }
4497
4498 /**
4499 * Lists all availble windows in the system. The listing is written in the
4500 * specified Socket's output stream with the following syntax:
4501 * windowHashCodeInHexadecimal windowName
4502 * Each line of the ouput represents a different window.
4503 *
4504 * @param client The remote client to send the listing to.
4505 * @return False if an error occured, true otherwise.
4506 */
4507 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004508 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 return false;
4510 }
4511
4512 boolean result = true;
4513
4514 Object[] windows;
4515 synchronized (mWindowMap) {
4516 windows = new Object[mWindows.size()];
4517 //noinspection unchecked
4518 windows = mWindows.toArray(windows);
4519 }
4520
4521 BufferedWriter out = null;
4522
4523 // Any uncaught exception will crash the system process
4524 try {
4525 OutputStream clientStream = client.getOutputStream();
4526 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4527
4528 final int count = windows.length;
4529 for (int i = 0; i < count; i++) {
4530 final WindowState w = (WindowState) windows[i];
4531 out.write(Integer.toHexString(System.identityHashCode(w)));
4532 out.write(' ');
4533 out.append(w.mAttrs.getTitle());
4534 out.write('\n');
4535 }
4536
4537 out.write("DONE.\n");
4538 out.flush();
4539 } catch (Exception e) {
4540 result = false;
4541 } finally {
4542 if (out != null) {
4543 try {
4544 out.close();
4545 } catch (IOException e) {
4546 result = false;
4547 }
4548 }
4549 }
4550
4551 return result;
4552 }
4553
4554 /**
4555 * Sends a command to a target window. The result of the command, if any, will be
4556 * written in the output stream of the specified socket.
4557 *
4558 * The parameters must follow this syntax:
4559 * windowHashcode extra
4560 *
4561 * Where XX is the length in characeters of the windowTitle.
4562 *
4563 * The first parameter is the target window. The window with the specified hashcode
4564 * will be the target. If no target can be found, nothing happens. The extra parameters
4565 * will be delivered to the target window and as parameters to the command itself.
4566 *
4567 * @param client The remote client to sent the result, if any, to.
4568 * @param command The command to execute.
4569 * @param parameters The command parameters.
4570 *
4571 * @return True if the command was successfully delivered, false otherwise. This does
4572 * not indicate whether the command itself was successful.
4573 */
4574 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004575 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 return false;
4577 }
4578
4579 boolean success = true;
4580 Parcel data = null;
4581 Parcel reply = null;
4582
4583 // Any uncaught exception will crash the system process
4584 try {
4585 // Find the hashcode of the window
4586 int index = parameters.indexOf(' ');
4587 if (index == -1) {
4588 index = parameters.length();
4589 }
4590 final String code = parameters.substring(0, index);
4591 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4592
4593 // Extract the command's parameter after the window description
4594 if (index < parameters.length()) {
4595 parameters = parameters.substring(index + 1);
4596 } else {
4597 parameters = "";
4598 }
4599
4600 final WindowManagerService.WindowState window = findWindow(hashCode);
4601 if (window == null) {
4602 return false;
4603 }
4604
4605 data = Parcel.obtain();
4606 data.writeInterfaceToken("android.view.IWindow");
4607 data.writeString(command);
4608 data.writeString(parameters);
4609 data.writeInt(1);
4610 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4611
4612 reply = Parcel.obtain();
4613
4614 final IBinder binder = window.mClient.asBinder();
4615 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4616 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4617
4618 reply.readException();
4619
4620 } catch (Exception e) {
4621 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4622 success = false;
4623 } finally {
4624 if (data != null) {
4625 data.recycle();
4626 }
4627 if (reply != null) {
4628 reply.recycle();
4629 }
4630 }
4631
4632 return success;
4633 }
4634
4635 private WindowState findWindow(int hashCode) {
4636 if (hashCode == -1) {
4637 return getFocusedWindow();
4638 }
4639
4640 synchronized (mWindowMap) {
4641 final ArrayList windows = mWindows;
4642 final int count = windows.size();
4643
4644 for (int i = 0; i < count; i++) {
4645 WindowState w = (WindowState) windows.get(i);
4646 if (System.identityHashCode(w) == hashCode) {
4647 return w;
4648 }
4649 }
4650 }
4651
4652 return null;
4653 }
4654
4655 /*
4656 * Instruct the Activity Manager to fetch the current configuration and broadcast
4657 * that to config-changed listeners if appropriate.
4658 */
4659 void sendNewConfiguration() {
4660 try {
4661 mActivityManager.updateConfiguration(null);
4662 } catch (RemoteException e) {
4663 }
4664 }
Romain Guy06882f82009-06-10 13:36:04 -07004665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 public Configuration computeNewConfiguration() {
4667 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004668 return computeNewConfigurationLocked();
4669 }
4670 }
Romain Guy06882f82009-06-10 13:36:04 -07004671
Dianne Hackbornc485a602009-03-24 22:39:49 -07004672 Configuration computeNewConfigurationLocked() {
4673 Configuration config = new Configuration();
4674 if (!computeNewConfigurationLocked(config)) {
4675 return null;
4676 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004677 return config;
4678 }
Romain Guy06882f82009-06-10 13:36:04 -07004679
Dianne Hackbornc485a602009-03-24 22:39:49 -07004680 boolean computeNewConfigurationLocked(Configuration config) {
4681 if (mDisplay == null) {
4682 return false;
4683 }
4684 mQueue.getInputConfiguration(config);
4685 final int dw = mDisplay.getWidth();
4686 final int dh = mDisplay.getHeight();
4687 int orientation = Configuration.ORIENTATION_SQUARE;
4688 if (dw < dh) {
4689 orientation = Configuration.ORIENTATION_PORTRAIT;
4690 } else if (dw > dh) {
4691 orientation = Configuration.ORIENTATION_LANDSCAPE;
4692 }
4693 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004694
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004695 DisplayMetrics dm = new DisplayMetrics();
4696 mDisplay.getMetrics(dm);
4697 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4698
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004699 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004700 // Note we only do this once because at this point we don't
4701 // expect the screen to change in this way at runtime, and want
4702 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004703 int longSize = dw;
4704 int shortSize = dh;
4705 if (longSize < shortSize) {
4706 int tmp = longSize;
4707 longSize = shortSize;
4708 shortSize = tmp;
4709 }
4710 longSize = (int)(longSize/dm.density);
4711 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004712
Dianne Hackborn723738c2009-06-25 19:48:04 -07004713 // These semi-magic numbers define our compatibility modes for
4714 // applications with different screens. Don't change unless you
4715 // make sure to test lots and lots of apps!
4716 if (longSize < 470) {
4717 // This is shorter than an HVGA normal density screen (which
4718 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004719 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4720 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004721 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004722 // Is this a large screen?
4723 if (longSize > 640 && shortSize >= 480) {
4724 // VGA or larger screens at medium density are the point
4725 // at which we consider it to be a large screen.
4726 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4727 } else {
4728 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4729
4730 // If this screen is wider than normal HVGA, or taller
4731 // than FWVGA, then for old apps we want to run in size
4732 // compatibility mode.
4733 if (shortSize > 321 || longSize > 570) {
4734 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4735 }
4736 }
4737
4738 // Is this a long screen?
4739 if (((longSize*3)/5) >= (shortSize-1)) {
4740 // Anything wider than WVGA (5:3) is considering to be long.
4741 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4742 } else {
4743 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4744 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004745 }
4746 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004747 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004748
Dianne Hackbornc485a602009-03-24 22:39:49 -07004749 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4750 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4751 mPolicy.adjustConfigurationLw(config);
4752 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004753 }
Romain Guy06882f82009-06-10 13:36:04 -07004754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755 // -------------------------------------------------------------
4756 // Input Events and Focus Management
4757 // -------------------------------------------------------------
4758
4759 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004760 long curTime = SystemClock.uptimeMillis();
4761
Michael Chane10de972009-05-18 11:24:50 -07004762 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004763 if (mLastTouchEventType == eventType &&
4764 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4765 return;
4766 }
4767 mLastUserActivityCallTime = curTime;
4768 mLastTouchEventType = eventType;
4769 }
4770
4771 if (targetWin == null
4772 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4773 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004774 }
4775 }
4776
4777 // tells if it's a cheek event or not -- this function is stateful
4778 private static final int EVENT_NONE = 0;
4779 private static final int EVENT_UNKNOWN = 0;
4780 private static final int EVENT_CHEEK = 0;
4781 private static final int EVENT_IGNORE_DURATION = 300; // ms
4782 private static final float CHEEK_THRESHOLD = 0.6f;
4783 private int mEventState = EVENT_NONE;
4784 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004786 private int eventType(MotionEvent ev) {
4787 float size = ev.getSize();
4788 switch (ev.getAction()) {
4789 case MotionEvent.ACTION_DOWN:
4790 mEventSize = size;
4791 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4792 case MotionEvent.ACTION_UP:
4793 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004794 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004795 case MotionEvent.ACTION_MOVE:
4796 final int N = ev.getHistorySize();
4797 if (size > mEventSize) mEventSize = size;
4798 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4799 for (int i=0; i<N; i++) {
4800 size = ev.getHistoricalSize(i);
4801 if (size > mEventSize) mEventSize = size;
4802 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4803 }
4804 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4805 return TOUCH_EVENT;
4806 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004807 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004808 }
4809 default:
4810 // not good
4811 return OTHER_EVENT;
4812 }
4813 }
4814
4815 /**
4816 * @return Returns true if event was dispatched, false if it was dropped for any reason
4817 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004818 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004819 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4820 "dispatchPointer " + ev);
4821
Michael Chan53071d62009-05-13 17:29:48 -07004822 if (MEASURE_LATENCY) {
4823 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4824 }
4825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004827 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828
Michael Chan53071d62009-05-13 17:29:48 -07004829 if (MEASURE_LATENCY) {
4830 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4831 }
4832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004835 if (action == MotionEvent.ACTION_UP) {
4836 // let go of our target
4837 mKeyWaiter.mMotionTarget = null;
4838 mPowerManager.logPointerUpEvent();
4839 } else if (action == MotionEvent.ACTION_DOWN) {
4840 mPowerManager.logPointerDownEvent();
4841 }
4842
4843 if (targetObj == null) {
4844 // In this case we are either dropping the event, or have received
4845 // a move or up without a down. It is common to receive move
4846 // events in such a way, since this means the user is moving the
4847 // pointer without actually pressing down. All other cases should
4848 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004849 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004850 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4851 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08004852 synchronized (mWindowMap) {
4853 if (mSendingPointersToWallpaper) {
4854 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4855 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4856 }
4857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004858 if (qev != null) {
4859 mQueue.recycleEvent(qev);
4860 }
4861 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004862 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004863 }
4864 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004865 synchronized (mWindowMap) {
4866 if (mSendingPointersToWallpaper) {
4867 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4868 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4869 }
4870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 if (qev != null) {
4872 mQueue.recycleEvent(qev);
4873 }
4874 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004875 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 }
Romain Guy06882f82009-06-10 13:36:04 -07004877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004881 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882
4883 //Log.i(TAG, "Sending " + ev + " to " + target);
4884
4885 if (uid != 0 && uid != target.mSession.mUid) {
4886 if (mContext.checkPermission(
4887 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4888 != PackageManager.PERMISSION_GRANTED) {
4889 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4890 + pid + " uid " + uid + " to window " + target
4891 + " owned by uid " + target.mSession.mUid);
4892 if (qev != null) {
4893 mQueue.recycleEvent(qev);
4894 }
4895 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004896 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004897 }
4898 }
4899
Michael Chan53071d62009-05-13 17:29:48 -07004900 if (MEASURE_LATENCY) {
4901 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4902 }
4903
Romain Guy06882f82009-06-10 13:36:04 -07004904 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4906 //target wants to ignore fat touch events
4907 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4908 //explicit flag to return without processing event further
4909 boolean returnFlag = false;
4910 if((action == MotionEvent.ACTION_DOWN)) {
4911 mFatTouch = false;
4912 if(cheekPress) {
4913 mFatTouch = true;
4914 returnFlag = true;
4915 }
4916 } else {
4917 if(action == MotionEvent.ACTION_UP) {
4918 if(mFatTouch) {
4919 //earlier even was invalid doesnt matter if current up is cheekpress or not
4920 mFatTouch = false;
4921 returnFlag = true;
4922 } else if(cheekPress) {
4923 //cancel the earlier event
4924 ev.setAction(MotionEvent.ACTION_CANCEL);
4925 action = MotionEvent.ACTION_CANCEL;
4926 }
4927 } else if(action == MotionEvent.ACTION_MOVE) {
4928 if(mFatTouch) {
4929 //two cases here
4930 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004931 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 returnFlag = true;
4933 } else if(cheekPress) {
4934 //valid down followed by invalid moves
4935 //an invalid move have to cancel earlier action
4936 ev.setAction(MotionEvent.ACTION_CANCEL);
4937 action = MotionEvent.ACTION_CANCEL;
4938 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4939 //note that the subsequent invalid moves will not get here
4940 mFatTouch = true;
4941 }
4942 }
4943 } //else if action
4944 if(returnFlag) {
4945 //recycle que, ev
4946 if (qev != null) {
4947 mQueue.recycleEvent(qev);
4948 }
4949 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004950 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 }
4952 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004953
Michael Chan9f028e62009-08-04 17:37:46 -07004954 // Enable this for testing the "right" value
4955 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004956 int max_events_per_sec = 35;
4957 try {
4958 max_events_per_sec = Integer.parseInt(SystemProperties
4959 .get("windowsmgr.max_events_per_sec"));
4960 if (max_events_per_sec < 1) {
4961 max_events_per_sec = 35;
4962 }
4963 } catch (NumberFormatException e) {
4964 }
4965 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4966 }
4967
4968 /*
4969 * Throttle events to minimize CPU usage when there's a flood of events
4970 * e.g. constant contact with the screen
4971 */
4972 if (action == MotionEvent.ACTION_MOVE) {
4973 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4974 long now = SystemClock.uptimeMillis();
4975 if (now < nextEventTime) {
4976 try {
4977 Thread.sleep(nextEventTime - now);
4978 } catch (InterruptedException e) {
4979 }
4980 mLastTouchEventTime = nextEventTime;
4981 } else {
4982 mLastTouchEventTime = now;
4983 }
4984 }
4985
Michael Chan53071d62009-05-13 17:29:48 -07004986 if (MEASURE_LATENCY) {
4987 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4988 }
4989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004990 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004991 if (!target.isVisibleLw()) {
4992 // During this motion dispatch, the target window has become
4993 // invisible.
4994 if (mSendingPointersToWallpaper) {
4995 sendPointerToWallpaperLocked(null, ev, eventTime);
4996 }
4997 if (qev != null) {
4998 mQueue.recycleEvent(qev);
4999 }
5000 ev.recycle();
5001 return INJECT_SUCCEEDED;
5002 }
5003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5005 mKeyWaiter.bindTargetWindowLocked(target,
5006 KeyWaiter.RETURN_PENDING_POINTER, qev);
5007 ev = null;
5008 } else {
5009 if (action == MotionEvent.ACTION_DOWN) {
5010 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5011 if (out != null) {
5012 MotionEvent oev = MotionEvent.obtain(ev);
5013 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5014 do {
5015 final Rect frame = out.mFrame;
5016 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5017 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005018 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005019 } catch (android.os.RemoteException e) {
5020 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
5021 }
5022 oev.offsetLocation((float)frame.left, (float)frame.top);
5023 out = out.mNextOutsideTouch;
5024 } while (out != null);
5025 mKeyWaiter.mOutsideTouchTargets = null;
5026 }
5027 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005028
5029 // If we are on top of the wallpaper, then the wallpaper also
5030 // gets to see this movement.
Dianne Hackborn6adba242009-11-10 11:10:09 -08005031 if (mWallpaperTarget == target || mSendingPointersToWallpaper) {
5032 sendPointerToWallpaperLocked(null, ev, eventTime);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005033 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005034
5035 final Rect frame = target.mFrame;
5036 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5037 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 }
5039 }
Romain Guy06882f82009-06-10 13:36:04 -07005040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005041 // finally offset the event to the target's coordinate system and
5042 // dispatch the event.
5043 try {
5044 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
5045 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
5046 }
Michael Chan53071d62009-05-13 17:29:48 -07005047
5048 if (MEASURE_LATENCY) {
5049 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5050 }
5051
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005052 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005053
5054 if (MEASURE_LATENCY) {
5055 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5056 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005057 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 } catch (android.os.RemoteException e) {
5059 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
5060 mKeyWaiter.mMotionTarget = null;
5061 try {
5062 removeWindow(target.mSession, target.mClient);
5063 } catch (java.util.NoSuchElementException ex) {
5064 // This will happen if the window has already been
5065 // removed.
5066 }
5067 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005068 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 }
Romain Guy06882f82009-06-10 13:36:04 -07005070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 /**
5072 * @return Returns true if event was dispatched, false if it was dropped for any reason
5073 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005074 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005075 if (DEBUG_INPUT) Log.v(
5076 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005078 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005079 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 if (focusObj == null) {
5081 Log.w(TAG, "No focus window, dropping trackball: " + ev);
5082 if (qev != null) {
5083 mQueue.recycleEvent(qev);
5084 }
5085 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005086 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 }
5088 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5089 if (qev != null) {
5090 mQueue.recycleEvent(qev);
5091 }
5092 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005093 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 }
Romain Guy06882f82009-06-10 13:36:04 -07005095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005098 if (uid != 0 && uid != focus.mSession.mUid) {
5099 if (mContext.checkPermission(
5100 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5101 != PackageManager.PERMISSION_GRANTED) {
5102 Log.w(TAG, "Permission denied: injecting key event from pid "
5103 + pid + " uid " + uid + " to window " + focus
5104 + " owned by uid " + focus.mSession.mUid);
5105 if (qev != null) {
5106 mQueue.recycleEvent(qev);
5107 }
5108 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005109 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 }
5111 }
Romain Guy06882f82009-06-10 13:36:04 -07005112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 synchronized(mWindowMap) {
5116 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5117 mKeyWaiter.bindTargetWindowLocked(focus,
5118 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5119 // We don't deliver movement events to the client, we hold
5120 // them and wait for them to call back.
5121 ev = null;
5122 } else {
5123 mKeyWaiter.bindTargetWindowLocked(focus);
5124 }
5125 }
Romain Guy06882f82009-06-10 13:36:04 -07005126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005128 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005129 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 } catch (android.os.RemoteException e) {
5131 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5132 try {
5133 removeWindow(focus.mSession, focus.mClient);
5134 } catch (java.util.NoSuchElementException ex) {
5135 // This will happen if the window has already been
5136 // removed.
5137 }
5138 }
Romain Guy06882f82009-06-10 13:36:04 -07005139
Dianne Hackborncfaef692009-06-15 14:24:44 -07005140 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005141 }
Romain Guy06882f82009-06-10 13:36:04 -07005142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143 /**
5144 * @return Returns true if event was dispatched, false if it was dropped for any reason
5145 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005146 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5148
5149 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005150 null, 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: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005153 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005154 }
5155 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005156 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 }
Romain Guy06882f82009-06-10 13:36:04 -07005158
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005159 // Okay we have finished waiting for the last event to be processed.
5160 // First off, if this is a repeat event, check to see if there is
5161 // a corresponding up event in the queue. If there is, we will
5162 // just drop the repeat, because it makes no sense to repeat after
5163 // the user has released a key. (This is especially important for
5164 // long presses.)
5165 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5166 return INJECT_SUCCEEDED;
5167 }
5168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171 if (DEBUG_INPUT) Log.v(
5172 TAG, "Dispatching to " + focus + ": " + event);
5173
5174 if (uid != 0 && uid != focus.mSession.mUid) {
5175 if (mContext.checkPermission(
5176 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5177 != PackageManager.PERMISSION_GRANTED) {
5178 Log.w(TAG, "Permission denied: injecting key event from pid "
5179 + pid + " uid " + uid + " to window " + focus
5180 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005181 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005182 }
5183 }
Romain Guy06882f82009-06-10 13:36:04 -07005184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 synchronized(mWindowMap) {
5186 mKeyWaiter.bindTargetWindowLocked(focus);
5187 }
5188
5189 // NOSHIP extra state logging
5190 mKeyWaiter.recordDispatchState(event, focus);
5191 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 try {
5194 if (DEBUG_INPUT || DEBUG_FOCUS) {
5195 Log.v(TAG, "Delivering key " + event.getKeyCode()
5196 + " to " + focus);
5197 }
5198 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005199 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 } catch (android.os.RemoteException e) {
5201 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5202 try {
5203 removeWindow(focus.mSession, focus.mClient);
5204 } catch (java.util.NoSuchElementException ex) {
5205 // This will happen if the window has already been
5206 // removed.
5207 }
5208 }
Romain Guy06882f82009-06-10 13:36:04 -07005209
Dianne Hackborncfaef692009-06-15 14:24:44 -07005210 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 }
Romain Guy06882f82009-06-10 13:36:04 -07005212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 public void pauseKeyDispatching(IBinder _token) {
5214 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5215 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005216 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 }
5218
5219 synchronized (mWindowMap) {
5220 WindowToken token = mTokenMap.get(_token);
5221 if (token != null) {
5222 mKeyWaiter.pauseDispatchingLocked(token);
5223 }
5224 }
5225 }
5226
5227 public void resumeKeyDispatching(IBinder _token) {
5228 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5229 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005230 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 }
5232
5233 synchronized (mWindowMap) {
5234 WindowToken token = mTokenMap.get(_token);
5235 if (token != null) {
5236 mKeyWaiter.resumeDispatchingLocked(token);
5237 }
5238 }
5239 }
5240
5241 public void setEventDispatching(boolean enabled) {
5242 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5243 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005244 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 }
5246
5247 synchronized (mWindowMap) {
5248 mKeyWaiter.setEventDispatchingLocked(enabled);
5249 }
5250 }
Romain Guy06882f82009-06-10 13:36:04 -07005251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 /**
5253 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005254 *
5255 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 * {@link SystemClock#uptimeMillis()} as the timebase.)
5257 * @param sync If true, wait for the event to be completed before returning to the caller.
5258 * @return Returns true if event was dispatched, false if it was dropped for any reason
5259 */
5260 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5261 long downTime = ev.getDownTime();
5262 long eventTime = ev.getEventTime();
5263
5264 int action = ev.getAction();
5265 int code = ev.getKeyCode();
5266 int repeatCount = ev.getRepeatCount();
5267 int metaState = ev.getMetaState();
5268 int deviceId = ev.getDeviceId();
5269 int scancode = ev.getScanCode();
5270
5271 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5272 if (downTime == 0) downTime = eventTime;
5273
5274 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005275 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005277 final int pid = Binder.getCallingPid();
5278 final int uid = Binder.getCallingUid();
5279 final long ident = Binder.clearCallingIdentity();
5280 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005282 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005284 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005285 switch (result) {
5286 case INJECT_NO_PERMISSION:
5287 throw new SecurityException(
5288 "Injecting to another application requires INJECT_EVENT permission");
5289 case INJECT_SUCCEEDED:
5290 return true;
5291 }
5292 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005293 }
5294
5295 /**
5296 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005297 *
5298 * @param ev A motion event describing the pointer (touch) action. (As noted in
5299 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 * {@link SystemClock#uptimeMillis()} as the timebase.)
5301 * @param sync If true, wait for the event to be completed before returning to the caller.
5302 * @return Returns true if event was dispatched, false if it was dropped for any reason
5303 */
5304 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005305 final int pid = Binder.getCallingPid();
5306 final int uid = Binder.getCallingUid();
5307 final long ident = Binder.clearCallingIdentity();
5308 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005309 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005310 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005312 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005313 switch (result) {
5314 case INJECT_NO_PERMISSION:
5315 throw new SecurityException(
5316 "Injecting to another application requires INJECT_EVENT permission");
5317 case INJECT_SUCCEEDED:
5318 return true;
5319 }
5320 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 }
Romain Guy06882f82009-06-10 13:36:04 -07005322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 /**
5324 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005325 *
5326 * @param ev A motion event describing the trackball action. (As noted in
5327 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005328 * {@link SystemClock#uptimeMillis()} as the timebase.)
5329 * @param sync If true, wait for the event to be completed before returning to the caller.
5330 * @return Returns true if event was dispatched, false if it was dropped for any reason
5331 */
5332 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005333 final int pid = Binder.getCallingPid();
5334 final int uid = Binder.getCallingUid();
5335 final long ident = Binder.clearCallingIdentity();
5336 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005338 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005340 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005341 switch (result) {
5342 case INJECT_NO_PERMISSION:
5343 throw new SecurityException(
5344 "Injecting to another application requires INJECT_EVENT permission");
5345 case INJECT_SUCCEEDED:
5346 return true;
5347 }
5348 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 }
Romain Guy06882f82009-06-10 13:36:04 -07005350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 private WindowState getFocusedWindow() {
5352 synchronized (mWindowMap) {
5353 return getFocusedWindowLocked();
5354 }
5355 }
5356
5357 private WindowState getFocusedWindowLocked() {
5358 return mCurrentFocus;
5359 }
Romain Guy06882f82009-06-10 13:36:04 -07005360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 /**
5362 * This class holds the state for dispatching key events. This state
5363 * is protected by the KeyWaiter instance, NOT by the window lock. You
5364 * can be holding the main window lock while acquire the KeyWaiter lock,
5365 * but not the other way around.
5366 */
5367 final class KeyWaiter {
5368 // NOSHIP debugging
5369 public class DispatchState {
5370 private KeyEvent event;
5371 private WindowState focus;
5372 private long time;
5373 private WindowState lastWin;
5374 private IBinder lastBinder;
5375 private boolean finished;
5376 private boolean gotFirstWindow;
5377 private boolean eventDispatching;
5378 private long timeToSwitch;
5379 private boolean wasFrozen;
5380 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005381 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5384 focus = theFocus;
5385 event = theEvent;
5386 time = System.currentTimeMillis();
5387 // snapshot KeyWaiter state
5388 lastWin = mLastWin;
5389 lastBinder = mLastBinder;
5390 finished = mFinished;
5391 gotFirstWindow = mGotFirstWindow;
5392 eventDispatching = mEventDispatching;
5393 timeToSwitch = mTimeToSwitch;
5394 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005395 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 // cache the paused state at ctor time as well
5397 if (theFocus == null || theFocus.mToken == null) {
5398 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5399 focusPaused = false;
5400 } else {
5401 focusPaused = theFocus.mToken.paused;
5402 }
5403 }
Romain Guy06882f82009-06-10 13:36:04 -07005404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 public String toString() {
5406 return "{{" + event + " to " + focus + " @ " + time
5407 + " lw=" + lastWin + " lb=" + lastBinder
5408 + " fin=" + finished + " gfw=" + gotFirstWindow
5409 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005410 + " wf=" + wasFrozen + " fp=" + focusPaused
5411 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 }
5413 };
5414 private DispatchState mDispatchState = null;
5415 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5416 mDispatchState = new DispatchState(theEvent, theFocus);
5417 }
5418 // END NOSHIP
5419
5420 public static final int RETURN_NOTHING = 0;
5421 public static final int RETURN_PENDING_POINTER = 1;
5422 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424 final Object SKIP_TARGET_TOKEN = new Object();
5425 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005427 private WindowState mLastWin = null;
5428 private IBinder mLastBinder = null;
5429 private boolean mFinished = true;
5430 private boolean mGotFirstWindow = false;
5431 private boolean mEventDispatching = true;
5432 private long mTimeToSwitch = 0;
5433 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 // Target of Motion events
5436 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005438 // Windows above the target who would like to receive an "outside"
5439 // touch event for any down events outside of them.
5440 WindowState mOutsideTouchTargets;
5441
5442 /**
5443 * Wait for the last event dispatch to complete, then find the next
5444 * target that should receive the given event and wait for that one
5445 * to be ready to receive it.
5446 */
5447 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5448 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005449 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 long startTime = SystemClock.uptimeMillis();
5451 long keyDispatchingTimeout = 5 * 1000;
5452 long waitedFor = 0;
5453
5454 while (true) {
5455 // Figure out which window we care about. It is either the
5456 // last window we are waiting to have process the event or,
5457 // if none, then the next window we think the event should go
5458 // to. Note: we retrieve mLastWin outside of the lock, so
5459 // it may change before we lock. Thus we must check it again.
5460 WindowState targetWin = mLastWin;
5461 boolean targetIsNew = targetWin == null;
5462 if (DEBUG_INPUT) Log.v(
5463 TAG, "waitForLastKey: mFinished=" + mFinished +
5464 ", mLastWin=" + mLastWin);
5465 if (targetIsNew) {
5466 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005467 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 if (target == SKIP_TARGET_TOKEN) {
5469 // The user has pressed a special key, and we are
5470 // dropping all pending events before it.
5471 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5472 + " " + nextMotion);
5473 return null;
5474 }
5475 if (target == CONSUMED_EVENT_TOKEN) {
5476 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5477 + " " + nextMotion);
5478 return target;
5479 }
5480 targetWin = (WindowState)target;
5481 }
Romain Guy06882f82009-06-10 13:36:04 -07005482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 // Now: is it okay to send the next event to this window?
5486 synchronized (this) {
5487 // First: did we come here based on the last window not
5488 // being null, but it changed by the time we got here?
5489 // If so, try again.
5490 if (!targetIsNew && mLastWin == null) {
5491 continue;
5492 }
Romain Guy06882f82009-06-10 13:36:04 -07005493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005494 // We never dispatch events if not finished with the
5495 // last one, or the display is frozen.
5496 if (mFinished && !mDisplayFrozen) {
5497 // If event dispatching is disabled, then we
5498 // just consume the events.
5499 if (!mEventDispatching) {
5500 if (DEBUG_INPUT) Log.v(TAG,
5501 "Skipping event; dispatching disabled: "
5502 + nextKey + " " + nextMotion);
5503 return null;
5504 }
5505 if (targetWin != null) {
5506 // If this is a new target, and that target is not
5507 // paused or unresponsive, then all looks good to
5508 // handle the event.
5509 if (targetIsNew && !targetWin.mToken.paused) {
5510 return targetWin;
5511 }
Romain Guy06882f82009-06-10 13:36:04 -07005512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005513 // If we didn't find a target window, and there is no
5514 // focused app window, then just eat the events.
5515 } else if (mFocusedApp == null) {
5516 if (DEBUG_INPUT) Log.v(TAG,
5517 "Skipping event; no focused app: "
5518 + nextKey + " " + nextMotion);
5519 return null;
5520 }
5521 }
Romain Guy06882f82009-06-10 13:36:04 -07005522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005523 if (DEBUG_INPUT) Log.v(
5524 TAG, "Waiting for last key in " + mLastBinder
5525 + " target=" + targetWin
5526 + " mFinished=" + mFinished
5527 + " mDisplayFrozen=" + mDisplayFrozen
5528 + " targetIsNew=" + targetIsNew
5529 + " paused="
5530 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005531 + " mFocusedApp=" + mFocusedApp
5532 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 targetApp = targetWin != null
5535 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 long curTimeout = keyDispatchingTimeout;
5538 if (mTimeToSwitch != 0) {
5539 long now = SystemClock.uptimeMillis();
5540 if (mTimeToSwitch <= now) {
5541 // If an app switch key has been pressed, and we have
5542 // waited too long for the current app to finish
5543 // processing keys, then wait no more!
5544 doFinishedKeyLocked(true);
5545 continue;
5546 }
5547 long switchTimeout = mTimeToSwitch - now;
5548 if (curTimeout > switchTimeout) {
5549 curTimeout = switchTimeout;
5550 }
5551 }
Romain Guy06882f82009-06-10 13:36:04 -07005552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005553 try {
5554 // after that continue
5555 // processing keys, so we don't get stuck.
5556 if (DEBUG_INPUT) Log.v(
5557 TAG, "Waiting for key dispatch: " + curTimeout);
5558 wait(curTimeout);
5559 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5560 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005561 + startTime + " switchTime=" + mTimeToSwitch
5562 + " target=" + targetWin + " mLW=" + mLastWin
5563 + " mLB=" + mLastBinder + " fin=" + mFinished
5564 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 } catch (InterruptedException e) {
5566 }
5567 }
5568
5569 // If we were frozen during configuration change, restart the
5570 // timeout checks from now; otherwise look at whether we timed
5571 // out before awakening.
5572 if (mWasFrozen) {
5573 waitedFor = 0;
5574 mWasFrozen = false;
5575 } else {
5576 waitedFor = SystemClock.uptimeMillis() - startTime;
5577 }
5578
5579 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5580 IApplicationToken at = null;
5581 synchronized (this) {
5582 Log.w(TAG, "Key dispatching timed out sending to " +
5583 (targetWin != null ? targetWin.mAttrs.getTitle()
5584 : "<null>"));
5585 // NOSHIP debugging
5586 Log.w(TAG, "Dispatch state: " + mDispatchState);
5587 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5588 // END NOSHIP
5589 //dump();
5590 if (targetWin != null) {
5591 at = targetWin.getAppToken();
5592 } else if (targetApp != null) {
5593 at = targetApp.appToken;
5594 }
5595 }
5596
5597 boolean abort = true;
5598 if (at != null) {
5599 try {
5600 long timeout = at.getKeyDispatchingTimeout();
5601 if (timeout > waitedFor) {
5602 // we did not wait the proper amount of time for this application.
5603 // set the timeout to be the real timeout and wait again.
5604 keyDispatchingTimeout = timeout - waitedFor;
5605 continue;
5606 } else {
5607 abort = at.keyDispatchingTimedOut();
5608 }
5609 } catch (RemoteException ex) {
5610 }
5611 }
5612
5613 synchronized (this) {
5614 if (abort && (mLastWin == targetWin || targetWin == null)) {
5615 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005616 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617 if (DEBUG_INPUT) Log.v(TAG,
5618 "Window " + mLastWin +
5619 " timed out on key input");
5620 if (mLastWin.mToken.paused) {
5621 Log.w(TAG, "Un-pausing dispatching to this window");
5622 mLastWin.mToken.paused = false;
5623 }
5624 }
5625 if (mMotionTarget == targetWin) {
5626 mMotionTarget = null;
5627 }
5628 mLastWin = null;
5629 mLastBinder = null;
5630 if (failIfTimeout || targetWin == null) {
5631 return null;
5632 }
5633 } else {
5634 Log.w(TAG, "Continuing to wait for key to be dispatched");
5635 startTime = SystemClock.uptimeMillis();
5636 }
5637 }
5638 }
5639 }
5640 }
Romain Guy06882f82009-06-10 13:36:04 -07005641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005642 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005643 MotionEvent nextMotion, boolean isPointerEvent,
5644 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 if (nextKey != null) {
5648 // Find the target window for a normal key event.
5649 final int keycode = nextKey.getKeyCode();
5650 final int repeatCount = nextKey.getRepeatCount();
5651 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5652 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005655 if (callingUid == 0 ||
5656 mContext.checkPermission(
5657 android.Manifest.permission.INJECT_EVENTS,
5658 callingPid, callingUid)
5659 == PackageManager.PERMISSION_GRANTED) {
5660 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005661 nextKey.getMetaState(), down, repeatCount,
5662 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 Log.w(TAG, "Event timeout during app switch: dropping "
5665 + nextKey);
5666 return SKIP_TARGET_TOKEN;
5667 }
Romain Guy06882f82009-06-10 13:36:04 -07005668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 WindowState focus = null;
5672 synchronized(mWindowMap) {
5673 focus = getFocusedWindowLocked();
5674 }
Romain Guy06882f82009-06-10 13:36:04 -07005675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005677
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005678 if (callingUid == 0 ||
5679 (focus != null && callingUid == focus.mSession.mUid) ||
5680 mContext.checkPermission(
5681 android.Manifest.permission.INJECT_EVENTS,
5682 callingPid, callingUid)
5683 == PackageManager.PERMISSION_GRANTED) {
5684 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005685 keycode, nextKey.getMetaState(), down, repeatCount,
5686 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005687 return CONSUMED_EVENT_TOKEN;
5688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 }
Romain Guy06882f82009-06-10 13:36:04 -07005690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 } else if (!isPointerEvent) {
5694 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5695 if (!dispatch) {
5696 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5697 + nextMotion);
5698 return SKIP_TARGET_TOKEN;
5699 }
Romain Guy06882f82009-06-10 13:36:04 -07005700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 WindowState focus = null;
5702 synchronized(mWindowMap) {
5703 focus = getFocusedWindowLocked();
5704 }
Romain Guy06882f82009-06-10 13:36:04 -07005705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5707 return focus;
5708 }
Romain Guy06882f82009-06-10 13:36:04 -07005709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 if (nextMotion == null) {
5711 return SKIP_TARGET_TOKEN;
5712 }
Romain Guy06882f82009-06-10 13:36:04 -07005713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5715 KeyEvent.KEYCODE_UNKNOWN);
5716 if (!dispatch) {
5717 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5718 + nextMotion);
5719 return SKIP_TARGET_TOKEN;
5720 }
Romain Guy06882f82009-06-10 13:36:04 -07005721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 // Find the target window for a pointer event.
5723 int action = nextMotion.getAction();
5724 final float xf = nextMotion.getX();
5725 final float yf = nextMotion.getY();
5726 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728 final boolean screenWasOff = qev != null
5729 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005731 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005733 synchronized(mWindowMap) {
5734 synchronized (this) {
5735 if (action == MotionEvent.ACTION_DOWN) {
5736 if (mMotionTarget != null) {
5737 // this is weird, we got a pen down, but we thought it was
5738 // already down!
5739 // XXX: We should probably send an ACTION_UP to the current
5740 // target.
5741 Log.w(TAG, "Pointer down received while already down in: "
5742 + mMotionTarget);
5743 mMotionTarget = null;
5744 }
Romain Guy06882f82009-06-10 13:36:04 -07005745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 // ACTION_DOWN is special, because we need to lock next events to
5747 // the window we'll land onto.
5748 final int x = (int)xf;
5749 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 final ArrayList windows = mWindows;
5752 final int N = windows.size();
5753 WindowState topErrWindow = null;
5754 final Rect tmpRect = mTempRect;
5755 for (int i=N-1; i>=0; i--) {
5756 WindowState child = (WindowState)windows.get(i);
5757 //Log.i(TAG, "Checking dispatch to: " + child);
5758 final int flags = child.mAttrs.flags;
5759 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5760 if (topErrWindow == null) {
5761 topErrWindow = child;
5762 }
5763 }
5764 if (!child.isVisibleLw()) {
5765 //Log.i(TAG, "Not visible!");
5766 continue;
5767 }
5768 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5769 //Log.i(TAG, "Not touchable!");
5770 if ((flags & WindowManager.LayoutParams
5771 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5772 child.mNextOutsideTouch = mOutsideTouchTargets;
5773 mOutsideTouchTargets = child;
5774 }
5775 continue;
5776 }
5777 tmpRect.set(child.mFrame);
5778 if (child.mTouchableInsets == ViewTreeObserver
5779 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5780 // The touch is inside of the window if it is
5781 // inside the frame, AND the content part of that
5782 // frame that was given by the application.
5783 tmpRect.left += child.mGivenContentInsets.left;
5784 tmpRect.top += child.mGivenContentInsets.top;
5785 tmpRect.right -= child.mGivenContentInsets.right;
5786 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5787 } else if (child.mTouchableInsets == ViewTreeObserver
5788 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5789 // The touch is inside of the window if it is
5790 // inside the frame, AND the visible part of that
5791 // frame that was given by the application.
5792 tmpRect.left += child.mGivenVisibleInsets.left;
5793 tmpRect.top += child.mGivenVisibleInsets.top;
5794 tmpRect.right -= child.mGivenVisibleInsets.right;
5795 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5796 }
5797 final int touchFlags = flags &
5798 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5799 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5800 if (tmpRect.contains(x, y) || touchFlags == 0) {
5801 //Log.i(TAG, "Using this target!");
5802 if (!screenWasOff || (flags &
5803 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5804 mMotionTarget = child;
5805 } else {
5806 //Log.i(TAG, "Waking, skip!");
5807 mMotionTarget = null;
5808 }
5809 break;
5810 }
Romain Guy06882f82009-06-10 13:36:04 -07005811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005812 if ((flags & WindowManager.LayoutParams
5813 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5814 child.mNextOutsideTouch = mOutsideTouchTargets;
5815 mOutsideTouchTargets = child;
5816 //Log.i(TAG, "Adding to outside target list: " + child);
5817 }
5818 }
5819
5820 // if there's an error window but it's not accepting
5821 // focus (typically because it is not yet visible) just
5822 // wait for it -- any other focused window may in fact
5823 // be in ANR state.
5824 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5825 mMotionTarget = null;
5826 }
5827 }
Romain Guy06882f82009-06-10 13:36:04 -07005828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005829 target = mMotionTarget;
5830 }
5831 }
Romain Guy06882f82009-06-10 13:36:04 -07005832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005833 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835 // Pointer events are a little different -- if there isn't a
5836 // target found for any event, then just drop it.
5837 return target != null ? target : SKIP_TARGET_TOKEN;
5838 }
Romain Guy06882f82009-06-10 13:36:04 -07005839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005840 boolean checkShouldDispatchKey(int keycode) {
5841 synchronized (this) {
5842 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5843 mTimeToSwitch = 0;
5844 return true;
5845 }
5846 if (mTimeToSwitch != 0
5847 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5848 return false;
5849 }
5850 return true;
5851 }
5852 }
Romain Guy06882f82009-06-10 13:36:04 -07005853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 void bindTargetWindowLocked(WindowState win,
5855 int pendingWhat, QueuedEvent pendingMotion) {
5856 synchronized (this) {
5857 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5858 }
5859 }
Romain Guy06882f82009-06-10 13:36:04 -07005860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 void bindTargetWindowLocked(WindowState win) {
5862 synchronized (this) {
5863 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5864 }
5865 }
5866
5867 void bindTargetWindowLockedLocked(WindowState win,
5868 int pendingWhat, QueuedEvent pendingMotion) {
5869 mLastWin = win;
5870 mLastBinder = win.mClient.asBinder();
5871 mFinished = false;
5872 if (pendingMotion != null) {
5873 final Session s = win.mSession;
5874 if (pendingWhat == RETURN_PENDING_POINTER) {
5875 releasePendingPointerLocked(s);
5876 s.mPendingPointerMove = pendingMotion;
5877 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005878 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 "bindTargetToWindow " + s.mPendingPointerMove);
5880 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5881 releasePendingTrackballLocked(s);
5882 s.mPendingTrackballMove = pendingMotion;
5883 s.mPendingTrackballWindow = win;
5884 }
5885 }
5886 }
Romain Guy06882f82009-06-10 13:36:04 -07005887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 void releasePendingPointerLocked(Session s) {
5889 if (DEBUG_INPUT) Log.v(TAG,
5890 "releasePendingPointer " + s.mPendingPointerMove);
5891 if (s.mPendingPointerMove != null) {
5892 mQueue.recycleEvent(s.mPendingPointerMove);
5893 s.mPendingPointerMove = null;
5894 }
5895 }
Romain Guy06882f82009-06-10 13:36:04 -07005896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 void releasePendingTrackballLocked(Session s) {
5898 if (s.mPendingTrackballMove != null) {
5899 mQueue.recycleEvent(s.mPendingTrackballMove);
5900 s.mPendingTrackballMove = null;
5901 }
5902 }
Romain Guy06882f82009-06-10 13:36:04 -07005903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5905 int returnWhat) {
5906 if (DEBUG_INPUT) Log.v(
5907 TAG, "finishedKey: client=" + client + ", force=" + force);
5908
5909 if (client == null) {
5910 return null;
5911 }
5912
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005913 MotionEvent res = null;
5914 QueuedEvent qev = null;
5915 WindowState win = null;
5916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 synchronized (this) {
5918 if (DEBUG_INPUT) Log.v(
5919 TAG, "finishedKey: client=" + client.asBinder()
5920 + ", force=" + force + ", last=" + mLastBinder
5921 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 if (returnWhat == RETURN_PENDING_POINTER) {
5924 qev = session.mPendingPointerMove;
5925 win = session.mPendingPointerWindow;
5926 session.mPendingPointerMove = null;
5927 session.mPendingPointerWindow = null;
5928 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5929 qev = session.mPendingTrackballMove;
5930 win = session.mPendingTrackballWindow;
5931 session.mPendingTrackballMove = null;
5932 session.mPendingTrackballWindow = null;
5933 }
Romain Guy06882f82009-06-10 13:36:04 -07005934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 if (mLastBinder == client.asBinder()) {
5936 if (DEBUG_INPUT) Log.v(
5937 TAG, "finishedKey: last paused="
5938 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5939 if (mLastWin != null && (!mLastWin.mToken.paused || force
5940 || !mEventDispatching)) {
5941 doFinishedKeyLocked(false);
5942 } else {
5943 // Make sure to wake up anyone currently waiting to
5944 // dispatch a key, so they can re-evaluate their
5945 // current situation.
5946 mFinished = true;
5947 notifyAll();
5948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 }
Romain Guy06882f82009-06-10 13:36:04 -07005950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005952 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 if (DEBUG_INPUT) Log.v(TAG,
5954 "Returning pending motion: " + res);
5955 mQueue.recycleEvent(qev);
5956 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5957 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005960
5961 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5962 synchronized (mWindowMap) {
5963 if (mWallpaperTarget == win || mSendingPointersToWallpaper) {
5964 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5965 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005966 }
5967 }
5968 }
5969
5970 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 }
5972
5973 void tickle() {
5974 synchronized (this) {
5975 notifyAll();
5976 }
5977 }
Romain Guy06882f82009-06-10 13:36:04 -07005978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005979 void handleNewWindowLocked(WindowState newWindow) {
5980 if (!newWindow.canReceiveKeys()) {
5981 return;
5982 }
5983 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005984 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 TAG, "New key dispatch window: win="
5986 + newWindow.mClient.asBinder()
5987 + ", last=" + mLastBinder
5988 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5989 + "), finished=" + mFinished + ", paused="
5990 + newWindow.mToken.paused);
5991
5992 // Displaying a window implicitly causes dispatching to
5993 // be unpaused. (This is to protect against bugs if someone
5994 // pauses dispatching but forgets to resume.)
5995 newWindow.mToken.paused = false;
5996
5997 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005998
5999 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
6000 if (DEBUG_INPUT) Log.v(TAG,
6001 "New SYSTEM_ERROR window; resetting state");
6002 mLastWin = null;
6003 mLastBinder = null;
6004 mMotionTarget = null;
6005 mFinished = true;
6006 } else if (mLastWin != null) {
6007 // If the new window is above the window we are
6008 // waiting on, then stop waiting and let key dispatching
6009 // start on the new guy.
6010 if (DEBUG_INPUT) Log.v(
6011 TAG, "Last win layer=" + mLastWin.mLayer
6012 + ", new win layer=" + newWindow.mLayer);
6013 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006014 // The new window is above the old; finish pending input to the last
6015 // window and start directing it to the new one.
6016 mLastWin.mToken.paused = false;
6017 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006018 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006019 // Either the new window is lower, so there is no need to wake key waiters,
6020 // or we just finished key input to the previous window, which implicitly
6021 // notified the key waiters. In both cases, we don't need to issue the
6022 // notification here.
6023 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 }
6025
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006026 // Now that we've put a new window state in place, make the event waiter
6027 // take notice and retarget its attentions.
6028 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 }
6030 }
6031
6032 void pauseDispatchingLocked(WindowToken token) {
6033 synchronized (this)
6034 {
6035 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
6036 token.paused = true;
6037
6038 /*
6039 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6040 mPaused = true;
6041 } else {
6042 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07006043 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006044 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07006045 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006046 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07006047 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 }
6049 }
6050 */
6051 }
6052 }
6053
6054 void resumeDispatchingLocked(WindowToken token) {
6055 synchronized (this) {
6056 if (token.paused) {
6057 if (DEBUG_INPUT) Log.v(
6058 TAG, "Resuming WindowToken " + token
6059 + ", last=" + mLastBinder
6060 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6061 + "), finished=" + mFinished + ", paused="
6062 + token.paused);
6063 token.paused = false;
6064 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
6065 doFinishedKeyLocked(true);
6066 } else {
6067 notifyAll();
6068 }
6069 }
6070 }
6071 }
6072
6073 void setEventDispatchingLocked(boolean enabled) {
6074 synchronized (this) {
6075 mEventDispatching = enabled;
6076 notifyAll();
6077 }
6078 }
Romain Guy06882f82009-06-10 13:36:04 -07006079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 void appSwitchComing() {
6081 synchronized (this) {
6082 // Don't wait for more than .5 seconds for app to finish
6083 // processing the pending events.
6084 long now = SystemClock.uptimeMillis() + 500;
6085 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6086 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6087 mTimeToSwitch = now;
6088 }
6089 notifyAll();
6090 }
6091 }
Romain Guy06882f82009-06-10 13:36:04 -07006092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 private final void doFinishedKeyLocked(boolean doRecycle) {
6094 if (mLastWin != null) {
6095 releasePendingPointerLocked(mLastWin.mSession);
6096 releasePendingTrackballLocked(mLastWin.mSession);
6097 }
Romain Guy06882f82009-06-10 13:36:04 -07006098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 if (mLastWin == null || !mLastWin.mToken.paused
6100 || !mLastWin.isVisibleLw()) {
6101 // If the current window has been paused, we aren't -really-
6102 // finished... so let the waiters still wait.
6103 mLastWin = null;
6104 mLastBinder = null;
6105 }
6106 mFinished = true;
6107 notifyAll();
6108 }
6109 }
6110
6111 private class KeyQ extends KeyInputQueue
6112 implements KeyInputQueue.FilterCallback {
6113 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006115 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006116 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6118 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6119 "KEEP_SCREEN_ON_FLAG");
6120 mHoldingScreen.setReferenceCounted(false);
6121 }
6122
6123 @Override
6124 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6125 if (mPolicy.preprocessInputEventTq(event)) {
6126 return true;
6127 }
Romain Guy06882f82009-06-10 13:36:04 -07006128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006129 switch (event.type) {
6130 case RawInputEvent.EV_KEY: {
6131 // XXX begin hack
6132 if (DEBUG) {
6133 if (event.keycode == KeyEvent.KEYCODE_G) {
6134 if (event.value != 0) {
6135 // G down
6136 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6137 }
6138 return false;
6139 }
6140 if (event.keycode == KeyEvent.KEYCODE_D) {
6141 if (event.value != 0) {
6142 //dump();
6143 }
6144 return false;
6145 }
6146 }
6147 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006148
Charles Mendis322591c2009-10-29 11:06:59 -07006149 boolean screenIsOff = !mPowerManager.isScreenOn();
6150 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6154 mPowerManager.goToSleep(event.when);
6155 }
6156
6157 if (screenIsOff) {
6158 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6159 }
6160 if (screenIsDim) {
6161 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6162 }
6163 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6164 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006165 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006166 }
Romain Guy06882f82009-06-10 13:36:04 -07006167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006168 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6169 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6170 filterQueue(this);
6171 mKeyWaiter.appSwitchComing();
6172 }
6173 return true;
6174 } else {
6175 return false;
6176 }
6177 }
Romain Guy06882f82009-06-10 13:36:04 -07006178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006180 boolean screenIsOff = !mPowerManager.isScreenOn();
6181 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006182 if (screenIsOff) {
6183 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6184 device.classes, event)) {
6185 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6186 return false;
6187 }
6188 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6189 }
6190 if (screenIsDim) {
6191 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6192 }
6193 return true;
6194 }
Romain Guy06882f82009-06-10 13:36:04 -07006195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006197 boolean screenIsOff = !mPowerManager.isScreenOn();
6198 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006199 if (screenIsOff) {
6200 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6201 device.classes, event)) {
6202 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6203 return false;
6204 }
6205 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6206 }
6207 if (screenIsDim) {
6208 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6209 }
6210 return true;
6211 }
Romain Guy06882f82009-06-10 13:36:04 -07006212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006213 default:
6214 return true;
6215 }
6216 }
6217
6218 public int filterEvent(QueuedEvent ev) {
6219 switch (ev.classType) {
6220 case RawInputEvent.CLASS_KEYBOARD:
6221 KeyEvent ke = (KeyEvent)ev.event;
6222 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6223 Log.w(TAG, "Dropping movement key during app switch: "
6224 + ke.getKeyCode() + ", action=" + ke.getAction());
6225 return FILTER_REMOVE;
6226 }
6227 return FILTER_ABORT;
6228 default:
6229 return FILTER_KEEP;
6230 }
6231 }
Romain Guy06882f82009-06-10 13:36:04 -07006232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 /**
6234 * Must be called with the main window manager lock held.
6235 */
6236 void setHoldScreenLocked(boolean holding) {
6237 boolean state = mHoldingScreen.isHeld();
6238 if (holding != state) {
6239 if (holding) {
6240 mHoldingScreen.acquire();
6241 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006242 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006243 mHoldingScreen.release();
6244 }
6245 }
6246 }
Michael Chan53071d62009-05-13 17:29:48 -07006247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248
6249 public boolean detectSafeMode() {
6250 mSafeMode = mPolicy.detectSafeMode();
6251 return mSafeMode;
6252 }
Romain Guy06882f82009-06-10 13:36:04 -07006253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 public void systemReady() {
6255 mPolicy.systemReady();
6256 }
Romain Guy06882f82009-06-10 13:36:04 -07006257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 private final class InputDispatcherThread extends Thread {
6259 // Time to wait when there is nothing to do: 9999 seconds.
6260 static final int LONG_WAIT=9999*1000;
6261
6262 public InputDispatcherThread() {
6263 super("InputDispatcher");
6264 }
Romain Guy06882f82009-06-10 13:36:04 -07006265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 @Override
6267 public void run() {
6268 while (true) {
6269 try {
6270 process();
6271 } catch (Exception e) {
6272 Log.e(TAG, "Exception in input dispatcher", e);
6273 }
6274 }
6275 }
Romain Guy06882f82009-06-10 13:36:04 -07006276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006277 private void process() {
6278 android.os.Process.setThreadPriority(
6279 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006281 // The last key event we saw
6282 KeyEvent lastKey = null;
6283
6284 // Last keydown time for auto-repeating keys
6285 long lastKeyTime = SystemClock.uptimeMillis();
6286 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006287 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288
Romain Guy06882f82009-06-10 13:36:04 -07006289 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 int keyRepeatCount = 0;
6291
6292 // Need to report that configuration has changed?
6293 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006295 while (true) {
6296 long curTime = SystemClock.uptimeMillis();
6297
6298 if (DEBUG_INPUT) Log.v(
6299 TAG, "Waiting for next key: now=" + curTime
6300 + ", repeat @ " + nextKeyTime);
6301
6302 // Retrieve next event, waiting only as long as the next
6303 // repeat timeout. If the configuration has changed, then
6304 // don't wait at all -- we'll report the change as soon as
6305 // we have processed all events.
6306 QueuedEvent ev = mQueue.getEvent(
6307 (int)((!configChanged && curTime < nextKeyTime)
6308 ? (nextKeyTime-curTime) : 0));
6309
6310 if (DEBUG_INPUT && ev != null) Log.v(
6311 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6312
Michael Chan53071d62009-05-13 17:29:48 -07006313 if (MEASURE_LATENCY) {
6314 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6315 }
6316
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006317 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6318 // cancel key repeat at the request of the policy.
6319 lastKey = null;
6320 downTime = 0;
6321 lastKeyTime = curTime;
6322 nextKeyTime = curTime + LONG_WAIT;
6323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324 try {
6325 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006326 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 int eventType;
6328 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6329 eventType = eventType((MotionEvent)ev.event);
6330 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6331 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6332 eventType = LocalPowerManager.BUTTON_EVENT;
6333 } else {
6334 eventType = LocalPowerManager.OTHER_EVENT;
6335 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006336 try {
Michael Chan53071d62009-05-13 17:29:48 -07006337 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006338 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006339 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006340 mBatteryStats.noteInputEvent();
6341 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006342 } catch (RemoteException e) {
6343 // Ignore
6344 }
Michael Chane10de972009-05-18 11:24:50 -07006345
6346 if (eventType != TOUCH_EVENT
6347 && eventType != LONG_TOUCH_EVENT
6348 && eventType != CHEEK_EVENT) {
6349 mPowerManager.userActivity(curTime, false,
6350 eventType, false);
6351 } else if (mLastTouchEventType != eventType
6352 || (curTime - mLastUserActivityCallTime)
6353 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6354 mLastUserActivityCallTime = curTime;
6355 mLastTouchEventType = eventType;
6356 mPowerManager.userActivity(curTime, false,
6357 eventType, false);
6358 }
6359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 switch (ev.classType) {
6361 case RawInputEvent.CLASS_KEYBOARD:
6362 KeyEvent ke = (KeyEvent)ev.event;
6363 if (ke.isDown()) {
6364 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006365 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 keyRepeatCount = 0;
6367 lastKeyTime = curTime;
6368 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006369 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006370 if (DEBUG_INPUT) Log.v(
6371 TAG, "Received key down: first repeat @ "
6372 + nextKeyTime);
6373 } else {
6374 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006375 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376 // Arbitrary long timeout.
6377 lastKeyTime = curTime;
6378 nextKeyTime = curTime + LONG_WAIT;
6379 if (DEBUG_INPUT) Log.v(
6380 TAG, "Received key up: ignore repeat @ "
6381 + nextKeyTime);
6382 }
6383 dispatchKey((KeyEvent)ev.event, 0, 0);
6384 mQueue.recycleEvent(ev);
6385 break;
6386 case RawInputEvent.CLASS_TOUCHSCREEN:
6387 //Log.i(TAG, "Read next event " + ev);
6388 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6389 break;
6390 case RawInputEvent.CLASS_TRACKBALL:
6391 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6392 break;
6393 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6394 configChanged = true;
6395 break;
6396 default:
6397 mQueue.recycleEvent(ev);
6398 break;
6399 }
Romain Guy06882f82009-06-10 13:36:04 -07006400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006401 } else if (configChanged) {
6402 configChanged = false;
6403 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006405 } else if (lastKey != null) {
6406 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006408 // Timeout occurred while key was down. If it is at or
6409 // past the key repeat time, dispatch the repeat.
6410 if (DEBUG_INPUT) Log.v(
6411 TAG, "Key timeout: repeat=" + nextKeyTime
6412 + ", now=" + curTime);
6413 if (curTime < nextKeyTime) {
6414 continue;
6415 }
Romain Guy06882f82009-06-10 13:36:04 -07006416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006417 lastKeyTime = nextKeyTime;
6418 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6419 keyRepeatCount++;
6420 if (DEBUG_INPUT) Log.v(
6421 TAG, "Key repeat: count=" + keyRepeatCount
6422 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006423 KeyEvent newEvent;
6424 if (downTime != 0 && (downTime
6425 + ViewConfiguration.getLongPressTimeout())
6426 <= curTime) {
6427 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6428 curTime, keyRepeatCount,
6429 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6430 downTime = 0;
6431 } else {
6432 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6433 curTime, keyRepeatCount);
6434 }
6435 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006437 } else {
6438 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006440 lastKeyTime = curTime;
6441 nextKeyTime = curTime + LONG_WAIT;
6442 }
Romain Guy06882f82009-06-10 13:36:04 -07006443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 } catch (Exception e) {
6445 Log.e(TAG,
6446 "Input thread received uncaught exception: " + e, e);
6447 }
6448 }
6449 }
6450 }
6451
6452 // -------------------------------------------------------------
6453 // Client Session State
6454 // -------------------------------------------------------------
6455
6456 private final class Session extends IWindowSession.Stub
6457 implements IBinder.DeathRecipient {
6458 final IInputMethodClient mClient;
6459 final IInputContext mInputContext;
6460 final int mUid;
6461 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006462 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 SurfaceSession mSurfaceSession;
6464 int mNumWindow = 0;
6465 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006467 /**
6468 * Current pointer move event being dispatched to client window... must
6469 * hold key lock to access.
6470 */
6471 QueuedEvent mPendingPointerMove;
6472 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006474 /**
6475 * Current trackball move event being dispatched to client window... must
6476 * hold key lock to access.
6477 */
6478 QueuedEvent mPendingTrackballMove;
6479 WindowState mPendingTrackballWindow;
6480
6481 public Session(IInputMethodClient client, IInputContext inputContext) {
6482 mClient = client;
6483 mInputContext = inputContext;
6484 mUid = Binder.getCallingUid();
6485 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006486 StringBuilder sb = new StringBuilder();
6487 sb.append("Session{");
6488 sb.append(Integer.toHexString(System.identityHashCode(this)));
6489 sb.append(" uid ");
6490 sb.append(mUid);
6491 sb.append("}");
6492 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 synchronized (mWindowMap) {
6495 if (mInputMethodManager == null && mHaveInputMethods) {
6496 IBinder b = ServiceManager.getService(
6497 Context.INPUT_METHOD_SERVICE);
6498 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6499 }
6500 }
6501 long ident = Binder.clearCallingIdentity();
6502 try {
6503 // Note: it is safe to call in to the input method manager
6504 // here because we are not holding our lock.
6505 if (mInputMethodManager != null) {
6506 mInputMethodManager.addClient(client, inputContext,
6507 mUid, mPid);
6508 } else {
6509 client.setUsingInputMethod(false);
6510 }
6511 client.asBinder().linkToDeath(this, 0);
6512 } catch (RemoteException e) {
6513 // The caller has died, so we can just forget about this.
6514 try {
6515 if (mInputMethodManager != null) {
6516 mInputMethodManager.removeClient(client);
6517 }
6518 } catch (RemoteException ee) {
6519 }
6520 } finally {
6521 Binder.restoreCallingIdentity(ident);
6522 }
6523 }
Romain Guy06882f82009-06-10 13:36:04 -07006524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006525 @Override
6526 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6527 throws RemoteException {
6528 try {
6529 return super.onTransact(code, data, reply, flags);
6530 } catch (RuntimeException e) {
6531 // Log all 'real' exceptions thrown to the caller
6532 if (!(e instanceof SecurityException)) {
6533 Log.e(TAG, "Window Session Crash", e);
6534 }
6535 throw e;
6536 }
6537 }
6538
6539 public void binderDied() {
6540 // Note: it is safe to call in to the input method manager
6541 // here because we are not holding our lock.
6542 try {
6543 if (mInputMethodManager != null) {
6544 mInputMethodManager.removeClient(mClient);
6545 }
6546 } catch (RemoteException e) {
6547 }
6548 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006549 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 mClientDead = true;
6551 killSessionLocked();
6552 }
6553 }
6554
6555 public int add(IWindow window, WindowManager.LayoutParams attrs,
6556 int viewVisibility, Rect outContentInsets) {
6557 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6558 }
Romain Guy06882f82009-06-10 13:36:04 -07006559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 public void remove(IWindow window) {
6561 removeWindow(this, window);
6562 }
Romain Guy06882f82009-06-10 13:36:04 -07006563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006564 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6565 int requestedWidth, int requestedHeight, int viewFlags,
6566 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6567 Rect outVisibleInsets, Surface outSurface) {
6568 return relayoutWindow(this, window, attrs,
6569 requestedWidth, requestedHeight, viewFlags, insetsPending,
6570 outFrame, outContentInsets, outVisibleInsets, outSurface);
6571 }
Romain Guy06882f82009-06-10 13:36:04 -07006572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006573 public void setTransparentRegion(IWindow window, Region region) {
6574 setTransparentRegionWindow(this, window, region);
6575 }
Romain Guy06882f82009-06-10 13:36:04 -07006576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006577 public void setInsets(IWindow window, int touchableInsets,
6578 Rect contentInsets, Rect visibleInsets) {
6579 setInsetsWindow(this, window, touchableInsets, contentInsets,
6580 visibleInsets);
6581 }
Romain Guy06882f82009-06-10 13:36:04 -07006582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006583 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6584 getWindowDisplayFrame(this, window, outDisplayFrame);
6585 }
Romain Guy06882f82009-06-10 13:36:04 -07006586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006587 public void finishDrawing(IWindow window) {
6588 if (localLOGV) Log.v(
6589 TAG, "IWindow finishDrawing called for " + window);
6590 finishDrawingWindow(this, window);
6591 }
6592
6593 public void finishKey(IWindow window) {
6594 if (localLOGV) Log.v(
6595 TAG, "IWindow finishKey called for " + window);
6596 mKeyWaiter.finishedKey(this, window, false,
6597 KeyWaiter.RETURN_NOTHING);
6598 }
6599
6600 public MotionEvent getPendingPointerMove(IWindow window) {
6601 if (localLOGV) Log.v(
6602 TAG, "IWindow getPendingMotionEvent called for " + window);
6603 return mKeyWaiter.finishedKey(this, window, false,
6604 KeyWaiter.RETURN_PENDING_POINTER);
6605 }
Romain Guy06882f82009-06-10 13:36:04 -07006606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006607 public MotionEvent getPendingTrackballMove(IWindow window) {
6608 if (localLOGV) Log.v(
6609 TAG, "IWindow getPendingMotionEvent called for " + window);
6610 return mKeyWaiter.finishedKey(this, window, false,
6611 KeyWaiter.RETURN_PENDING_TRACKBALL);
6612 }
6613
6614 public void setInTouchMode(boolean mode) {
6615 synchronized(mWindowMap) {
6616 mInTouchMode = mode;
6617 }
6618 }
6619
6620 public boolean getInTouchMode() {
6621 synchronized(mWindowMap) {
6622 return mInTouchMode;
6623 }
6624 }
6625
6626 public boolean performHapticFeedback(IWindow window, int effectId,
6627 boolean always) {
6628 synchronized(mWindowMap) {
6629 long ident = Binder.clearCallingIdentity();
6630 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006631 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 windowForClientLocked(this, window), effectId, always);
6633 } finally {
6634 Binder.restoreCallingIdentity(ident);
6635 }
6636 }
6637 }
Romain Guy06882f82009-06-10 13:36:04 -07006638
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006639 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006640 synchronized(mWindowMap) {
6641 long ident = Binder.clearCallingIdentity();
6642 try {
6643 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006644 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006645 } finally {
6646 Binder.restoreCallingIdentity(ident);
6647 }
6648 }
6649 }
6650
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006651 public void wallpaperOffsetsComplete(IBinder window) {
6652 WindowManagerService.this.wallpaperOffsetsComplete(window);
6653 }
6654
Dianne Hackborn75804932009-10-20 20:15:20 -07006655 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6656 int z, Bundle extras, boolean sync) {
6657 synchronized(mWindowMap) {
6658 long ident = Binder.clearCallingIdentity();
6659 try {
6660 return sendWindowWallpaperCommandLocked(
6661 windowForClientLocked(this, window),
6662 action, x, y, z, extras, sync);
6663 } finally {
6664 Binder.restoreCallingIdentity(ident);
6665 }
6666 }
6667 }
6668
6669 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6670 WindowManagerService.this.wallpaperCommandComplete(window, result);
6671 }
6672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 void windowAddedLocked() {
6674 if (mSurfaceSession == null) {
6675 if (localLOGV) Log.v(
6676 TAG, "First window added to " + this + ", creating SurfaceSession");
6677 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006678 if (SHOW_TRANSACTIONS) Log.i(
6679 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006680 mSessions.add(this);
6681 }
6682 mNumWindow++;
6683 }
6684
6685 void windowRemovedLocked() {
6686 mNumWindow--;
6687 killSessionLocked();
6688 }
Romain Guy06882f82009-06-10 13:36:04 -07006689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 void killSessionLocked() {
6691 if (mNumWindow <= 0 && mClientDead) {
6692 mSessions.remove(this);
6693 if (mSurfaceSession != null) {
6694 if (localLOGV) Log.v(
6695 TAG, "Last window removed from " + this
6696 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006697 if (SHOW_TRANSACTIONS) Log.i(
6698 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006699 try {
6700 mSurfaceSession.kill();
6701 } catch (Exception e) {
6702 Log.w(TAG, "Exception thrown when killing surface session "
6703 + mSurfaceSession + " in session " + this
6704 + ": " + e.toString());
6705 }
6706 mSurfaceSession = null;
6707 }
6708 }
6709 }
Romain Guy06882f82009-06-10 13:36:04 -07006710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006711 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006712 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6713 pw.print(" mClientDead="); pw.print(mClientDead);
6714 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6715 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6716 pw.print(prefix);
6717 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6718 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6719 }
6720 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6721 pw.print(prefix);
6722 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6723 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006725 }
6726
6727 @Override
6728 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006729 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006730 }
6731 }
6732
6733 // -------------------------------------------------------------
6734 // Client Window State
6735 // -------------------------------------------------------------
6736
6737 private final class WindowState implements WindowManagerPolicy.WindowState {
6738 final Session mSession;
6739 final IWindow mClient;
6740 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006741 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 AppWindowToken mAppToken;
6743 AppWindowToken mTargetAppToken;
6744 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6745 final DeathRecipient mDeathRecipient;
6746 final WindowState mAttachedWindow;
6747 final ArrayList mChildWindows = new ArrayList();
6748 final int mBaseLayer;
6749 final int mSubLayer;
6750 final boolean mLayoutAttached;
6751 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006752 final boolean mIsWallpaper;
6753 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 int mViewVisibility;
6755 boolean mPolicyVisibility = true;
6756 boolean mPolicyVisibilityAfterAnim = true;
6757 boolean mAppFreezing;
6758 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006759 boolean mReportDestroySurface;
6760 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006761 boolean mAttachedHidden; // is our parent window hidden?
6762 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006763 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 int mRequestedWidth;
6765 int mRequestedHeight;
6766 int mLastRequestedWidth;
6767 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 int mLayer;
6769 int mAnimLayer;
6770 int mLastLayer;
6771 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006772 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006773 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006774
6775 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777 // Actual frame shown on-screen (may be modified by animation)
6778 final Rect mShownFrame = new Rect();
6779 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 /**
6782 * Insets that determine the actually visible area
6783 */
6784 final Rect mVisibleInsets = new Rect();
6785 final Rect mLastVisibleInsets = new Rect();
6786 boolean mVisibleInsetsChanged;
6787
6788 /**
6789 * Insets that are covered by system windows
6790 */
6791 final Rect mContentInsets = new Rect();
6792 final Rect mLastContentInsets = new Rect();
6793 boolean mContentInsetsChanged;
6794
6795 /**
6796 * Set to true if we are waiting for this window to receive its
6797 * given internal insets before laying out other windows based on it.
6798 */
6799 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801 /**
6802 * These are the content insets that were given during layout for
6803 * this window, to be applied to windows behind it.
6804 */
6805 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006807 /**
6808 * These are the visible insets that were given during layout for
6809 * this window, to be applied to windows behind it.
6810 */
6811 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006813 /**
6814 * Flag indicating whether the touchable region should be adjusted by
6815 * the visible insets; if false the area outside the visible insets is
6816 * NOT touchable, so we must use those to adjust the frame during hit
6817 * tests.
6818 */
6819 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006821 // Current transformation being applied.
6822 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6823 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6824 float mHScale=1, mVScale=1;
6825 float mLastHScale=1, mLastVScale=1;
6826 final Matrix mTmpMatrix = new Matrix();
6827
6828 // "Real" frame that the application sees.
6829 final Rect mFrame = new Rect();
6830 final Rect mLastFrame = new Rect();
6831
6832 final Rect mContainingFrame = new Rect();
6833 final Rect mDisplayFrame = new Rect();
6834 final Rect mContentFrame = new Rect();
6835 final Rect mVisibleFrame = new Rect();
6836
6837 float mShownAlpha = 1;
6838 float mAlpha = 1;
6839 float mLastAlpha = 1;
6840
6841 // Set to true if, when the window gets displayed, it should perform
6842 // an enter animation.
6843 boolean mEnterAnimationPending;
6844
6845 // Currently running animation.
6846 boolean mAnimating;
6847 boolean mLocalAnimating;
6848 Animation mAnimation;
6849 boolean mAnimationIsEntrance;
6850 boolean mHasTransformation;
6851 boolean mHasLocalTransformation;
6852 final Transformation mTransformation = new Transformation();
6853
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006854 // If a window showing a wallpaper: the requested offset for the
6855 // wallpaper; if a wallpaper window: the currently applied offset.
6856 float mWallpaperX = -1;
6857 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006858
6859 // If a window showing a wallpaper: what fraction of the offset
6860 // range corresponds to a full virtual screen.
6861 float mWallpaperXStep = -1;
6862 float mWallpaperYStep = -1;
6863
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006864 // Wallpaper windows: pixels offset based on above variables.
6865 int mXOffset;
6866 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006868 // This is set after IWindowSession.relayout() has been called at
6869 // least once for the window. It allows us to detect the situation
6870 // where we don't yet have a surface, but should have one soon, so
6871 // we can give the window focus before waiting for the relayout.
6872 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 // This is set after the Surface has been created but before the
6875 // window has been drawn. During this time the surface is hidden.
6876 boolean mDrawPending;
6877
6878 // This is set after the window has finished drawing for the first
6879 // time but before its surface is shown. The surface will be
6880 // displayed when the next layout is run.
6881 boolean mCommitDrawPending;
6882
6883 // This is set during the time after the window's drawing has been
6884 // committed, and before its surface is actually shown. It is used
6885 // to delay showing the surface until all windows in a token are ready
6886 // to be shown.
6887 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006889 // Set when the window has been shown in the screen the first time.
6890 boolean mHasDrawn;
6891
6892 // Currently running an exit animation?
6893 boolean mExiting;
6894
6895 // Currently on the mDestroySurface list?
6896 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006898 // Completely remove from window manager after exit animation?
6899 boolean mRemoveOnExit;
6900
6901 // Set when the orientation is changing and this window has not yet
6902 // been updated for the new orientation.
6903 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006905 // Is this window now (or just being) removed?
6906 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006908 WindowState(Session s, IWindow c, WindowToken token,
6909 WindowState attachedWindow, WindowManager.LayoutParams a,
6910 int viewVisibility) {
6911 mSession = s;
6912 mClient = c;
6913 mToken = token;
6914 mAttrs.copyFrom(a);
6915 mViewVisibility = viewVisibility;
6916 DeathRecipient deathRecipient = new DeathRecipient();
6917 mAlpha = a.alpha;
6918 if (localLOGV) Log.v(
6919 TAG, "Window " + this + " client=" + c.asBinder()
6920 + " token=" + token + " (" + mAttrs.token + ")");
6921 try {
6922 c.asBinder().linkToDeath(deathRecipient, 0);
6923 } catch (RemoteException e) {
6924 mDeathRecipient = null;
6925 mAttachedWindow = null;
6926 mLayoutAttached = false;
6927 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006928 mIsWallpaper = false;
6929 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006930 mBaseLayer = 0;
6931 mSubLayer = 0;
6932 return;
6933 }
6934 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6937 mAttrs.type <= LAST_SUB_WINDOW)) {
6938 // The multiplier here is to reserve space for multiple
6939 // windows in the same type layer.
6940 mBaseLayer = mPolicy.windowTypeToLayerLw(
6941 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6942 + TYPE_LAYER_OFFSET;
6943 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6944 mAttachedWindow = attachedWindow;
6945 mAttachedWindow.mChildWindows.add(this);
6946 mLayoutAttached = mAttrs.type !=
6947 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6948 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6949 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006950 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6951 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 } else {
6953 // The multiplier here is to reserve space for multiple
6954 // windows in the same type layer.
6955 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6956 * TYPE_LAYER_MULTIPLIER
6957 + TYPE_LAYER_OFFSET;
6958 mSubLayer = 0;
6959 mAttachedWindow = null;
6960 mLayoutAttached = false;
6961 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6962 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006963 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6964 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006965 }
6966
6967 WindowState appWin = this;
6968 while (appWin.mAttachedWindow != null) {
6969 appWin = mAttachedWindow;
6970 }
6971 WindowToken appToken = appWin.mToken;
6972 while (appToken.appWindowToken == null) {
6973 WindowToken parent = mTokenMap.get(appToken.token);
6974 if (parent == null || appToken == parent) {
6975 break;
6976 }
6977 appToken = parent;
6978 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006979 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006980 mAppToken = appToken.appWindowToken;
6981
6982 mSurface = null;
6983 mRequestedWidth = 0;
6984 mRequestedHeight = 0;
6985 mLastRequestedWidth = 0;
6986 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006987 mXOffset = 0;
6988 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 mLayer = 0;
6990 mAnimLayer = 0;
6991 mLastLayer = 0;
6992 }
6993
6994 void attach() {
6995 if (localLOGV) Log.v(
6996 TAG, "Attaching " + this + " token=" + mToken
6997 + ", list=" + mToken.windows);
6998 mSession.windowAddedLocked();
6999 }
7000
7001 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7002 mHaveFrame = true;
7003
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007004 final Rect container = mContainingFrame;
7005 container.set(pf);
7006
7007 final Rect display = mDisplayFrame;
7008 display.set(df);
7009
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007010 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007011 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007012 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7013 display.intersect(mCompatibleScreenFrame);
7014 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007015 }
7016
7017 final int pw = container.right - container.left;
7018 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019
7020 int w,h;
7021 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7022 w = mAttrs.width < 0 ? pw : mAttrs.width;
7023 h = mAttrs.height< 0 ? ph : mAttrs.height;
7024 } else {
7025 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
7026 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
7027 }
Romain Guy06882f82009-06-10 13:36:04 -07007028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007029 final Rect content = mContentFrame;
7030 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007032 final Rect visible = mVisibleFrame;
7033 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007035 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007036 final int fw = frame.width();
7037 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007039 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7040 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7041
7042 Gravity.apply(mAttrs.gravity, w, h, container,
7043 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7044 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7045
7046 //System.out.println("Out: " + mFrame);
7047
7048 // Now make sure the window fits in the overall display.
7049 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007051 // Make sure the content and visible frames are inside of the
7052 // final window frame.
7053 if (content.left < frame.left) content.left = frame.left;
7054 if (content.top < frame.top) content.top = frame.top;
7055 if (content.right > frame.right) content.right = frame.right;
7056 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7057 if (visible.left < frame.left) visible.left = frame.left;
7058 if (visible.top < frame.top) visible.top = frame.top;
7059 if (visible.right > frame.right) visible.right = frame.right;
7060 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007062 final Rect contentInsets = mContentInsets;
7063 contentInsets.left = content.left-frame.left;
7064 contentInsets.top = content.top-frame.top;
7065 contentInsets.right = frame.right-content.right;
7066 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068 final Rect visibleInsets = mVisibleInsets;
7069 visibleInsets.left = visible.left-frame.left;
7070 visibleInsets.top = visible.top-frame.top;
7071 visibleInsets.right = frame.right-visible.right;
7072 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007073
Dianne Hackborn284ac932009-08-28 10:34:25 -07007074 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7075 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007076 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007077 }
7078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007079 if (localLOGV) {
7080 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7081 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7082 Log.v(TAG, "Resolving (mRequestedWidth="
7083 + mRequestedWidth + ", mRequestedheight="
7084 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7085 + "): frame=" + mFrame.toShortString()
7086 + " ci=" + contentInsets.toShortString()
7087 + " vi=" + visibleInsets.toShortString());
7088 //}
7089 }
7090 }
Romain Guy06882f82009-06-10 13:36:04 -07007091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007092 public Rect getFrameLw() {
7093 return mFrame;
7094 }
7095
7096 public Rect getShownFrameLw() {
7097 return mShownFrame;
7098 }
7099
7100 public Rect getDisplayFrameLw() {
7101 return mDisplayFrame;
7102 }
7103
7104 public Rect getContentFrameLw() {
7105 return mContentFrame;
7106 }
7107
7108 public Rect getVisibleFrameLw() {
7109 return mVisibleFrame;
7110 }
7111
7112 public boolean getGivenInsetsPendingLw() {
7113 return mGivenInsetsPending;
7114 }
7115
7116 public Rect getGivenContentInsetsLw() {
7117 return mGivenContentInsets;
7118 }
Romain Guy06882f82009-06-10 13:36:04 -07007119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 public Rect getGivenVisibleInsetsLw() {
7121 return mGivenVisibleInsets;
7122 }
Romain Guy06882f82009-06-10 13:36:04 -07007123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007124 public WindowManager.LayoutParams getAttrs() {
7125 return mAttrs;
7126 }
7127
7128 public int getSurfaceLayer() {
7129 return mLayer;
7130 }
Romain Guy06882f82009-06-10 13:36:04 -07007131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 public IApplicationToken getAppToken() {
7133 return mAppToken != null ? mAppToken.appToken : null;
7134 }
7135
7136 public boolean hasAppShownWindows() {
7137 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7138 }
7139
7140 public boolean hasAppStartingIcon() {
7141 return mAppToken != null ? (mAppToken.startingData != null) : false;
7142 }
7143
7144 public WindowManagerPolicy.WindowState getAppStartingWindow() {
7145 return mAppToken != null ? mAppToken.startingWindow : null;
7146 }
7147
7148 public void setAnimation(Animation anim) {
7149 if (localLOGV) Log.v(
7150 TAG, "Setting animation in " + this + ": " + anim);
7151 mAnimating = false;
7152 mLocalAnimating = false;
7153 mAnimation = anim;
7154 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7155 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7156 }
7157
7158 public void clearAnimation() {
7159 if (mAnimation != null) {
7160 mAnimating = true;
7161 mLocalAnimating = false;
7162 mAnimation = null;
7163 }
7164 }
Romain Guy06882f82009-06-10 13:36:04 -07007165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007166 Surface createSurfaceLocked() {
7167 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007168 mReportDestroySurface = false;
7169 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007170 mDrawPending = true;
7171 mCommitDrawPending = false;
7172 mReadyToShow = false;
7173 if (mAppToken != null) {
7174 mAppToken.allDrawn = false;
7175 }
7176
7177 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007178 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 flags |= Surface.PUSH_BUFFERS;
7180 }
7181
7182 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7183 flags |= Surface.SECURE;
7184 }
7185 if (DEBUG_VISIBILITY) Log.v(
7186 TAG, "Creating surface in session "
7187 + mSession.mSurfaceSession + " window " + this
7188 + " w=" + mFrame.width()
7189 + " h=" + mFrame.height() + " format="
7190 + mAttrs.format + " flags=" + flags);
7191
7192 int w = mFrame.width();
7193 int h = mFrame.height();
7194 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7195 // for a scaled surface, we always want the requested
7196 // size.
7197 w = mRequestedWidth;
7198 h = mRequestedHeight;
7199 }
7200
Romain Guy9825ec62009-10-01 00:58:09 -07007201 // Something is wrong and SurfaceFlinger will not like this,
7202 // try to revert to sane values
7203 if (w <= 0) w = 1;
7204 if (h <= 0) h = 1;
7205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007206 try {
7207 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007208 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007210 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7211 + mSurface + " IN SESSION "
7212 + mSession.mSurfaceSession
7213 + ": pid=" + mSession.mPid + " format="
7214 + mAttrs.format + " flags=0x"
7215 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 } catch (Surface.OutOfResourcesException e) {
7217 Log.w(TAG, "OutOfResourcesException creating surface");
7218 reclaimSomeSurfaceMemoryLocked(this, "create");
7219 return null;
7220 } catch (Exception e) {
7221 Log.e(TAG, "Exception creating surface", e);
7222 return null;
7223 }
Romain Guy06882f82009-06-10 13:36:04 -07007224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007225 if (localLOGV) Log.v(
7226 TAG, "Got surface: " + mSurface
7227 + ", set left=" + mFrame.left + " top=" + mFrame.top
7228 + ", animLayer=" + mAnimLayer);
7229 if (SHOW_TRANSACTIONS) {
7230 Log.i(TAG, ">>> OPEN TRANSACTION");
7231 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7232 + mAttrs.getTitle() + ") pos=(" +
7233 mFrame.left + "," + mFrame.top + ") (" +
7234 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7235 mAnimLayer + " HIDE");
7236 }
7237 Surface.openTransaction();
7238 try {
7239 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007240 mSurface.setPosition(mFrame.left + mXOffset,
7241 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007242 mSurface.setLayer(mAnimLayer);
7243 mSurface.hide();
7244 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007245 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7246 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007247 mSurface.setFlags(Surface.SURFACE_DITHER,
7248 Surface.SURFACE_DITHER);
7249 }
7250 } catch (RuntimeException e) {
7251 Log.w(TAG, "Error creating surface in " + w, e);
7252 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7253 }
7254 mLastHidden = true;
7255 } finally {
7256 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7257 Surface.closeTransaction();
7258 }
7259 if (localLOGV) Log.v(
7260 TAG, "Created surface " + this);
7261 }
7262 return mSurface;
7263 }
Romain Guy06882f82009-06-10 13:36:04 -07007264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007265 void destroySurfaceLocked() {
7266 // Window is no longer on-screen, so can no longer receive
7267 // key events... if we were waiting for it to finish
7268 // handling a key event, the wait is over!
7269 mKeyWaiter.finishedKey(mSession, mClient, true,
7270 KeyWaiter.RETURN_NOTHING);
7271 mKeyWaiter.releasePendingPointerLocked(mSession);
7272 mKeyWaiter.releasePendingTrackballLocked(mSession);
7273
7274 if (mAppToken != null && this == mAppToken.startingWindow) {
7275 mAppToken.startingDisplayed = false;
7276 }
Romain Guy06882f82009-06-10 13:36:04 -07007277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007279 mDrawPending = false;
7280 mCommitDrawPending = false;
7281 mReadyToShow = false;
7282
7283 int i = mChildWindows.size();
7284 while (i > 0) {
7285 i--;
7286 WindowState c = (WindowState)mChildWindows.get(i);
7287 c.mAttachedHidden = true;
7288 }
7289
7290 if (mReportDestroySurface) {
7291 mReportDestroySurface = false;
7292 mSurfacePendingDestroy = true;
7293 try {
7294 mClient.dispatchGetNewSurface();
7295 // We'll really destroy on the next time around.
7296 return;
7297 } catch (RemoteException e) {
7298 }
7299 }
7300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007301 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007302 if (DEBUG_VISIBILITY) {
7303 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007304 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007305 Log.w(TAG, "Window " + this + " destroying surface "
7306 + mSurface + ", session " + mSession, e);
7307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007308 if (SHOW_TRANSACTIONS) {
7309 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007310 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7312 + mAttrs.getTitle() + ")", ex);
7313 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007314 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 } catch (RuntimeException e) {
7316 Log.w(TAG, "Exception thrown when destroying Window " + this
7317 + " surface " + mSurface + " session " + mSession
7318 + ": " + e.toString());
7319 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007322 }
7323 }
7324
7325 boolean finishDrawingLocked() {
7326 if (mDrawPending) {
7327 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7328 TAG, "finishDrawingLocked: " + mSurface);
7329 mCommitDrawPending = true;
7330 mDrawPending = false;
7331 return true;
7332 }
7333 return false;
7334 }
7335
7336 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007337 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007338 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7339 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007340 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007341 }
7342 mCommitDrawPending = false;
7343 mReadyToShow = true;
7344 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7345 final AppWindowToken atoken = mAppToken;
7346 if (atoken == null || atoken.allDrawn || starting) {
7347 performShowLocked();
7348 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007349 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007350 }
7351
7352 // This must be called while inside a transaction.
7353 boolean performShowLocked() {
7354 if (DEBUG_VISIBILITY) {
7355 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007356 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 Log.v(TAG, "performShow on " + this
7358 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7359 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7360 }
7361 if (mReadyToShow && isReadyForDisplay()) {
7362 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7363 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7364 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7365 + " during animation: policyVis=" + mPolicyVisibility
7366 + " attHidden=" + mAttachedHidden
7367 + " tok.hiddenRequested="
7368 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007369 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370 + (mAppToken != null ? mAppToken.hidden : false)
7371 + " animating=" + mAnimating
7372 + " tok animating="
7373 + (mAppToken != null ? mAppToken.animating : false));
7374 if (!showSurfaceRobustlyLocked(this)) {
7375 return false;
7376 }
7377 mLastAlpha = -1;
7378 mHasDrawn = true;
7379 mLastHidden = false;
7380 mReadyToShow = false;
7381 enableScreenIfNeededLocked();
7382
7383 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 int i = mChildWindows.size();
7386 while (i > 0) {
7387 i--;
7388 WindowState c = (WindowState)mChildWindows.get(i);
7389 if (c.mSurface != null && c.mAttachedHidden) {
7390 c.mAttachedHidden = false;
7391 c.performShowLocked();
7392 }
7393 }
Romain Guy06882f82009-06-10 13:36:04 -07007394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007395 if (mAttrs.type != TYPE_APPLICATION_STARTING
7396 && mAppToken != null) {
7397 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007398
7399 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007400 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7401 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007403 // If this initial window is animating, stop it -- we
7404 // will do an animation to reveal it from behind the
7405 // starting window, so there is no need for it to also
7406 // be doing its own stuff.
7407 if (mAnimation != null) {
7408 mAnimation = null;
7409 // Make sure we clean up the animation.
7410 mAnimating = true;
7411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412 mFinishedStarting.add(mAppToken);
7413 mH.sendEmptyMessage(H.FINISHED_STARTING);
7414 }
7415 mAppToken.updateReportedVisibilityLocked();
7416 }
7417 }
7418 return true;
7419 }
Romain Guy06882f82009-06-10 13:36:04 -07007420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007421 // This must be called while inside a transaction. Returns true if
7422 // there is more animation to run.
7423 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7424 if (!mDisplayFrozen) {
7425 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007427 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7428 mHasTransformation = true;
7429 mHasLocalTransformation = true;
7430 if (!mLocalAnimating) {
7431 if (DEBUG_ANIM) Log.v(
7432 TAG, "Starting animation in " + this +
7433 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7434 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7435 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7436 mAnimation.setStartTime(currentTime);
7437 mLocalAnimating = true;
7438 mAnimating = true;
7439 }
7440 mTransformation.clear();
7441 final boolean more = mAnimation.getTransformation(
7442 currentTime, mTransformation);
7443 if (DEBUG_ANIM) Log.v(
7444 TAG, "Stepped animation in " + this +
7445 ": more=" + more + ", xform=" + mTransformation);
7446 if (more) {
7447 // we're not done!
7448 return true;
7449 }
7450 if (DEBUG_ANIM) Log.v(
7451 TAG, "Finished animation in " + this +
7452 " @ " + currentTime);
7453 mAnimation = null;
7454 //WindowManagerService.this.dump();
7455 }
7456 mHasLocalTransformation = false;
7457 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007458 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007459 // When our app token is animating, we kind-of pretend like
7460 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7461 // part of this check means that we will only do this if
7462 // our window is not currently exiting, or it is not
7463 // locally animating itself. The idea being that one that
7464 // is exiting and doing a local animation should be removed
7465 // once that animation is done.
7466 mAnimating = true;
7467 mHasTransformation = true;
7468 mTransformation.clear();
7469 return false;
7470 } else if (mHasTransformation) {
7471 // Little trick to get through the path below to act like
7472 // we have finished an animation.
7473 mAnimating = true;
7474 } else if (isAnimating()) {
7475 mAnimating = true;
7476 }
7477 } else if (mAnimation != null) {
7478 // If the display is frozen, and there is a pending animation,
7479 // clear it and make sure we run the cleanup code.
7480 mAnimating = true;
7481 mLocalAnimating = true;
7482 mAnimation = null;
7483 }
Romain Guy06882f82009-06-10 13:36:04 -07007484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007485 if (!mAnimating && !mLocalAnimating) {
7486 return false;
7487 }
7488
7489 if (DEBUG_ANIM) Log.v(
7490 TAG, "Animation done in " + this + ": exiting=" + mExiting
7491 + ", reportedVisible="
7492 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007494 mAnimating = false;
7495 mLocalAnimating = false;
7496 mAnimation = null;
7497 mAnimLayer = mLayer;
7498 if (mIsImWindow) {
7499 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007500 } else if (mIsWallpaper) {
7501 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 }
7503 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7504 + " anim layer: " + mAnimLayer);
7505 mHasTransformation = false;
7506 mHasLocalTransformation = false;
7507 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7508 mTransformation.clear();
7509 if (mHasDrawn
7510 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7511 && mAppToken != null
7512 && mAppToken.firstWindowDrawn
7513 && mAppToken.startingData != null) {
7514 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7515 + mToken + ": first real window done animating");
7516 mFinishedStarting.add(mAppToken);
7517 mH.sendEmptyMessage(H.FINISHED_STARTING);
7518 }
Romain Guy06882f82009-06-10 13:36:04 -07007519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 finishExit();
7521
7522 if (mAppToken != null) {
7523 mAppToken.updateReportedVisibilityLocked();
7524 }
7525
7526 return false;
7527 }
7528
7529 void finishExit() {
7530 if (DEBUG_ANIM) Log.v(
7531 TAG, "finishExit in " + this
7532 + ": exiting=" + mExiting
7533 + " remove=" + mRemoveOnExit
7534 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007536 final int N = mChildWindows.size();
7537 for (int i=0; i<N; i++) {
7538 ((WindowState)mChildWindows.get(i)).finishExit();
7539 }
Romain Guy06882f82009-06-10 13:36:04 -07007540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007541 if (!mExiting) {
7542 return;
7543 }
Romain Guy06882f82009-06-10 13:36:04 -07007544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007545 if (isWindowAnimating()) {
7546 return;
7547 }
7548
7549 if (localLOGV) Log.v(
7550 TAG, "Exit animation finished in " + this
7551 + ": remove=" + mRemoveOnExit);
7552 if (mSurface != null) {
7553 mDestroySurface.add(this);
7554 mDestroying = true;
7555 if (SHOW_TRANSACTIONS) Log.i(
7556 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7557 try {
7558 mSurface.hide();
7559 } catch (RuntimeException e) {
7560 Log.w(TAG, "Error hiding surface in " + this, e);
7561 }
7562 mLastHidden = true;
7563 mKeyWaiter.releasePendingPointerLocked(mSession);
7564 }
7565 mExiting = false;
7566 if (mRemoveOnExit) {
7567 mPendingRemove.add(this);
7568 mRemoveOnExit = false;
7569 }
7570 }
Romain Guy06882f82009-06-10 13:36:04 -07007571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007572 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7573 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7574 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7575 if (dtdx < -.000001f || dtdx > .000001f) return false;
7576 if (dsdy < -.000001f || dsdy > .000001f) return false;
7577 return true;
7578 }
Romain Guy06882f82009-06-10 13:36:04 -07007579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007580 void computeShownFrameLocked() {
7581 final boolean selfTransformation = mHasLocalTransformation;
7582 Transformation attachedTransformation =
7583 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7584 ? mAttachedWindow.mTransformation : null;
7585 Transformation appTransformation =
7586 (mAppToken != null && mAppToken.hasTransformation)
7587 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007588
7589 // Wallpapers are animated based on the "real" window they
7590 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007591 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007592 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007593 if (mWallpaperTarget.mHasLocalTransformation &&
7594 mWallpaperTarget.mAnimation != null &&
7595 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007596 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007597 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7598 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7599 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007600 }
7601 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007602 mWallpaperTarget.mAppToken.hasTransformation &&
7603 mWallpaperTarget.mAppToken.animation != null &&
7604 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007605 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007606 if (DEBUG_WALLPAPER && appTransformation != null) {
7607 Log.v(TAG, "WP target app xform: " + appTransformation);
7608 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007609 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007610 }
7611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 if (selfTransformation || attachedTransformation != null
7613 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007614 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007615 final Rect frame = mFrame;
7616 final float tmpFloats[] = mTmpFloats;
7617 final Matrix tmpMatrix = mTmpMatrix;
7618
7619 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007620 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007621 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007622 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007623 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007624 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007625 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007626 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007627 }
7628 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007629 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007630 }
7631
7632 // "convert" it into SurfaceFlinger's format
7633 // (a 2x2 matrix + an offset)
7634 // Here we must not transform the position of the surface
7635 // since it is already included in the transformation.
7636 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007638 tmpMatrix.getValues(tmpFloats);
7639 mDsDx = tmpFloats[Matrix.MSCALE_X];
7640 mDtDx = tmpFloats[Matrix.MSKEW_X];
7641 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7642 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007643 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7644 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007645 int w = frame.width();
7646 int h = frame.height();
7647 mShownFrame.set(x, y, x+w, y+h);
7648
7649 // Now set the alpha... but because our current hardware
7650 // can't do alpha transformation on a non-opaque surface,
7651 // turn it off if we are running an animation that is also
7652 // transforming since it is more important to have that
7653 // animation be smooth.
7654 mShownAlpha = mAlpha;
7655 if (!mLimitedAlphaCompositing
7656 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7657 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7658 && x == frame.left && y == frame.top))) {
7659 //Log.i(TAG, "Applying alpha transform");
7660 if (selfTransformation) {
7661 mShownAlpha *= mTransformation.getAlpha();
7662 }
7663 if (attachedTransformation != null) {
7664 mShownAlpha *= attachedTransformation.getAlpha();
7665 }
7666 if (appTransformation != null) {
7667 mShownAlpha *= appTransformation.getAlpha();
7668 }
7669 } else {
7670 //Log.i(TAG, "Not applying alpha transform");
7671 }
Romain Guy06882f82009-06-10 13:36:04 -07007672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007673 if (localLOGV) Log.v(
7674 TAG, "Continuing animation in " + this +
7675 ": " + mShownFrame +
7676 ", alpha=" + mTransformation.getAlpha());
7677 return;
7678 }
Romain Guy06882f82009-06-10 13:36:04 -07007679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007680 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007681 if (mXOffset != 0 || mYOffset != 0) {
7682 mShownFrame.offset(mXOffset, mYOffset);
7683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684 mShownAlpha = mAlpha;
7685 mDsDx = 1;
7686 mDtDx = 0;
7687 mDsDy = 0;
7688 mDtDy = 1;
7689 }
Romain Guy06882f82009-06-10 13:36:04 -07007690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007691 /**
7692 * Is this window visible? It is not visible if there is no
7693 * surface, or we are in the process of running an exit animation
7694 * that will remove the surface, or its app token has been hidden.
7695 */
7696 public boolean isVisibleLw() {
7697 final AppWindowToken atoken = mAppToken;
7698 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7699 && (atoken == null || !atoken.hiddenRequested)
7700 && !mExiting && !mDestroying;
7701 }
7702
7703 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007704 * Like {@link #isVisibleLw}, but also counts a window that is currently
7705 * "hidden" behind the keyguard as visible. This allows us to apply
7706 * things like window flags that impact the keyguard.
7707 * XXX I am starting to think we need to have ANOTHER visibility flag
7708 * for this "hidden behind keyguard" state rather than overloading
7709 * mPolicyVisibility. Ungh.
7710 */
7711 public boolean isVisibleOrBehindKeyguardLw() {
7712 final AppWindowToken atoken = mAppToken;
7713 return mSurface != null && !mAttachedHidden
7714 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7715 && !mExiting && !mDestroying;
7716 }
7717
7718 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 * Is this window visible, ignoring its app token? It is not visible
7720 * if there is no surface, or we are in the process of running an exit animation
7721 * that will remove the surface.
7722 */
7723 public boolean isWinVisibleLw() {
7724 final AppWindowToken atoken = mAppToken;
7725 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7726 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7727 && !mExiting && !mDestroying;
7728 }
7729
7730 /**
7731 * The same as isVisible(), but follows the current hidden state of
7732 * the associated app token, not the pending requested hidden state.
7733 */
7734 boolean isVisibleNow() {
7735 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007736 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737 }
7738
7739 /**
7740 * Same as isVisible(), but we also count it as visible between the
7741 * call to IWindowSession.add() and the first relayout().
7742 */
7743 boolean isVisibleOrAdding() {
7744 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007745 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007746 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7747 && mPolicyVisibility && !mAttachedHidden
7748 && (atoken == null || !atoken.hiddenRequested)
7749 && !mExiting && !mDestroying;
7750 }
7751
7752 /**
7753 * Is this window currently on-screen? It is on-screen either if it
7754 * is visible or it is currently running an animation before no longer
7755 * being visible.
7756 */
7757 boolean isOnScreen() {
7758 final AppWindowToken atoken = mAppToken;
7759 if (atoken != null) {
7760 return mSurface != null && mPolicyVisibility && !mDestroying
7761 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007762 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007763 } else {
7764 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007765 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 }
7767 }
Romain Guy06882f82009-06-10 13:36:04 -07007768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 /**
7770 * Like isOnScreen(), but we don't return true if the window is part
7771 * of a transition that has not yet been started.
7772 */
7773 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007774 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007775 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007776 return false;
7777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007778 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007779 final boolean animating = atoken != null
7780 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007781 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007782 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7783 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007784 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007785 }
7786
7787 /** Is the window or its container currently animating? */
7788 boolean isAnimating() {
7789 final WindowState attached = mAttachedWindow;
7790 final AppWindowToken atoken = mAppToken;
7791 return mAnimation != null
7792 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007793 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007794 (atoken.animation != null
7795 || atoken.inPendingTransaction));
7796 }
7797
7798 /** Is this window currently animating? */
7799 boolean isWindowAnimating() {
7800 return mAnimation != null;
7801 }
7802
7803 /**
7804 * Like isOnScreen, but returns false if the surface hasn't yet
7805 * been drawn.
7806 */
7807 public boolean isDisplayedLw() {
7808 final AppWindowToken atoken = mAppToken;
7809 return mSurface != null && mPolicyVisibility && !mDestroying
7810 && !mDrawPending && !mCommitDrawPending
7811 && ((!mAttachedHidden &&
7812 (atoken == null || !atoken.hiddenRequested))
7813 || mAnimating);
7814 }
7815
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007816 /**
7817 * Returns true if the window has a surface that it has drawn a
7818 * complete UI in to.
7819 */
7820 public boolean isDrawnLw() {
7821 final AppWindowToken atoken = mAppToken;
7822 return mSurface != null && !mDestroying
7823 && !mDrawPending && !mCommitDrawPending;
7824 }
7825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7827 boolean shownFrame, boolean onlyOpaque) {
7828 if (mSurface == null) {
7829 return false;
7830 }
7831 if (mAppToken != null && !mAppToken.appFullscreen) {
7832 return false;
7833 }
7834 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7835 return false;
7836 }
7837 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007838
7839 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7840 return frame.left <= mCompatibleScreenFrame.left &&
7841 frame.top <= mCompatibleScreenFrame.top &&
7842 frame.right >= mCompatibleScreenFrame.right &&
7843 frame.bottom >= mCompatibleScreenFrame.bottom;
7844 } else {
7845 return frame.left <= 0 && frame.top <= 0
7846 && frame.right >= screenWidth
7847 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 }
Romain Guy06882f82009-06-10 13:36:04 -07007850
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007851 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007852 * Return true if the window is opaque and fully drawn. This indicates
7853 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007854 */
7855 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007856 return (mAttrs.format == PixelFormat.OPAQUE
7857 || mAttrs.type == TYPE_WALLPAPER)
7858 && mSurface != null && mAnimation == null
7859 && (mAppToken == null || mAppToken.animation == null)
7860 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007861 }
7862
7863 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7864 return
7865 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007866 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7867 // only if it's visible
7868 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007869 // and only if the application fills the compatible screen
7870 mFrame.left <= mCompatibleScreenFrame.left &&
7871 mFrame.top <= mCompatibleScreenFrame.top &&
7872 mFrame.right >= mCompatibleScreenFrame.right &&
7873 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007874 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007875 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007876 }
7877
7878 boolean isFullscreen(int screenWidth, int screenHeight) {
7879 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007880 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007881 }
7882
7883 void removeLocked() {
7884 if (mAttachedWindow != null) {
7885 mAttachedWindow.mChildWindows.remove(this);
7886 }
7887 destroySurfaceLocked();
7888 mSession.windowRemovedLocked();
7889 try {
7890 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7891 } catch (RuntimeException e) {
7892 // Ignore if it has already been removed (usually because
7893 // we are doing this as part of processing a death note.)
7894 }
7895 }
7896
7897 private class DeathRecipient implements IBinder.DeathRecipient {
7898 public void binderDied() {
7899 try {
7900 synchronized(mWindowMap) {
7901 WindowState win = windowForClientLocked(mSession, mClient);
7902 Log.i(TAG, "WIN DEATH: " + win);
7903 if (win != null) {
7904 removeWindowLocked(mSession, win);
7905 }
7906 }
7907 } catch (IllegalArgumentException ex) {
7908 // This will happen if the window has already been
7909 // removed.
7910 }
7911 }
7912 }
7913
7914 /** Returns true if this window desires key events. */
7915 public final boolean canReceiveKeys() {
7916 return isVisibleOrAdding()
7917 && (mViewVisibility == View.VISIBLE)
7918 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7919 }
7920
7921 public boolean hasDrawnLw() {
7922 return mHasDrawn;
7923 }
7924
7925 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007926 return showLw(doAnimation, true);
7927 }
7928
7929 boolean showLw(boolean doAnimation, boolean requestAnim) {
7930 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
7931 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007932 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007933 mPolicyVisibility = true;
7934 mPolicyVisibilityAfterAnim = true;
7935 if (doAnimation) {
7936 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7937 }
7938 if (requestAnim) {
7939 requestAnimationLocked(0);
7940 }
7941 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007942 }
7943
7944 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007945 return hideLw(doAnimation, true);
7946 }
7947
7948 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007949 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7950 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007951 if (!current) {
7952 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007954 if (doAnimation) {
7955 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7956 if (mAnimation == null) {
7957 doAnimation = false;
7958 }
7959 }
7960 if (doAnimation) {
7961 mPolicyVisibilityAfterAnim = false;
7962 } else {
7963 mPolicyVisibilityAfterAnim = false;
7964 mPolicyVisibility = false;
7965 }
7966 if (requestAnim) {
7967 requestAnimationLocked(0);
7968 }
7969 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007970 }
7971
7972 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007973 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007974
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007975 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7976 pw.print(" mClient="); pw.println(mClient.asBinder());
7977 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7978 if (mAttachedWindow != null || mLayoutAttached) {
7979 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7980 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7981 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007982 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7983 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7984 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007985 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7986 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007987 }
7988 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7989 pw.print(" mSubLayer="); pw.print(mSubLayer);
7990 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7991 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7992 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7993 pw.print("="); pw.print(mAnimLayer);
7994 pw.print(" mLastLayer="); pw.println(mLastLayer);
7995 if (mSurface != null) {
7996 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7997 }
7998 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7999 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8000 if (mAppToken != null) {
8001 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8002 }
8003 if (mTargetAppToken != null) {
8004 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8005 }
8006 pw.print(prefix); pw.print("mViewVisibility=0x");
8007 pw.print(Integer.toHexString(mViewVisibility));
8008 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008009 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8010 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008011 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8012 pw.print(prefix); pw.print("mPolicyVisibility=");
8013 pw.print(mPolicyVisibility);
8014 pw.print(" mPolicyVisibilityAfterAnim=");
8015 pw.print(mPolicyVisibilityAfterAnim);
8016 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8017 }
8018 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008019 pw.print(" h="); pw.println(mRequestedHeight);
8020 if (mXOffset != 0 || mYOffset != 0) {
8021 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8022 pw.print(" y="); pw.println(mYOffset);
8023 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008024 pw.print(prefix); pw.print("mGivenContentInsets=");
8025 mGivenContentInsets.printShortString(pw);
8026 pw.print(" mGivenVisibleInsets=");
8027 mGivenVisibleInsets.printShortString(pw);
8028 pw.println();
8029 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8030 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8031 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8032 }
8033 pw.print(prefix); pw.print("mShownFrame=");
8034 mShownFrame.printShortString(pw);
8035 pw.print(" last="); mLastShownFrame.printShortString(pw);
8036 pw.println();
8037 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8038 pw.print(" last="); mLastFrame.printShortString(pw);
8039 pw.println();
8040 pw.print(prefix); pw.print("mContainingFrame=");
8041 mContainingFrame.printShortString(pw);
8042 pw.print(" mDisplayFrame=");
8043 mDisplayFrame.printShortString(pw);
8044 pw.println();
8045 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8046 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8047 pw.println();
8048 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8049 pw.print(" last="); mLastContentInsets.printShortString(pw);
8050 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8051 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8052 pw.println();
8053 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8054 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8055 pw.print(" mAlpha="); pw.print(mAlpha);
8056 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8057 }
8058 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8059 || mAnimation != null) {
8060 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8061 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8062 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8063 pw.print(" mAnimation="); pw.println(mAnimation);
8064 }
8065 if (mHasTransformation || mHasLocalTransformation) {
8066 pw.print(prefix); pw.print("XForm: has=");
8067 pw.print(mHasTransformation);
8068 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8069 pw.print(" "); mTransformation.printShortString(pw);
8070 pw.println();
8071 }
8072 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8073 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8074 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8075 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8076 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8077 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8078 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8079 pw.print(" mDestroying="); pw.print(mDestroying);
8080 pw.print(" mRemoved="); pw.println(mRemoved);
8081 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008082 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008083 pw.print(prefix); pw.print("mOrientationChanging=");
8084 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008085 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8086 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008087 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008088 if (mHScale != 1 || mVScale != 1) {
8089 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8090 pw.print(" mVScale="); pw.println(mVScale);
8091 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008092 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008093 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8094 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8095 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008096 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8097 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8098 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100 }
8101
8102 @Override
8103 public String toString() {
8104 return "Window{"
8105 + Integer.toHexString(System.identityHashCode(this))
8106 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8107 }
8108 }
Romain Guy06882f82009-06-10 13:36:04 -07008109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008110 // -------------------------------------------------------------
8111 // Window Token State
8112 // -------------------------------------------------------------
8113
8114 class WindowToken {
8115 // The actual token.
8116 final IBinder token;
8117
8118 // The type of window this token is for, as per WindowManager.LayoutParams.
8119 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 // Set if this token was explicitly added by a client, so should
8122 // not be removed when all windows are removed.
8123 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008124
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008125 // For printing.
8126 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008128 // If this is an AppWindowToken, this is non-null.
8129 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008131 // All of the windows associated with this token.
8132 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8133
8134 // Is key dispatching paused for this token?
8135 boolean paused = false;
8136
8137 // Should this token's windows be hidden?
8138 boolean hidden;
8139
8140 // Temporary for finding which tokens no longer have visible windows.
8141 boolean hasVisible;
8142
Dianne Hackborna8f60182009-09-01 19:01:50 -07008143 // Set to true when this token is in a pending transaction where it
8144 // will be shown.
8145 boolean waitingToShow;
8146
8147 // Set to true when this token is in a pending transaction where it
8148 // will be hidden.
8149 boolean waitingToHide;
8150
8151 // Set to true when this token is in a pending transaction where its
8152 // windows will be put to the bottom of the list.
8153 boolean sendingToBottom;
8154
8155 // Set to true when this token is in a pending transaction where its
8156 // windows will be put to the top of the list.
8157 boolean sendingToTop;
8158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008159 WindowToken(IBinder _token, int type, boolean _explicit) {
8160 token = _token;
8161 windowType = type;
8162 explicit = _explicit;
8163 }
8164
8165 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008166 pw.print(prefix); pw.print("token="); pw.println(token);
8167 pw.print(prefix); pw.print("windows="); pw.println(windows);
8168 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8169 pw.print(" hidden="); pw.print(hidden);
8170 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008171 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8172 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8173 pw.print(" waitingToHide="); pw.print(waitingToHide);
8174 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8175 pw.print(" sendingToTop="); pw.println(sendingToTop);
8176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008177 }
8178
8179 @Override
8180 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008181 if (stringName == null) {
8182 StringBuilder sb = new StringBuilder();
8183 sb.append("WindowToken{");
8184 sb.append(Integer.toHexString(System.identityHashCode(this)));
8185 sb.append(" token="); sb.append(token); sb.append('}');
8186 stringName = sb.toString();
8187 }
8188 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008189 }
8190 };
8191
8192 class AppWindowToken extends WindowToken {
8193 // Non-null only for application tokens.
8194 final IApplicationToken appToken;
8195
8196 // All of the windows and child windows that are included in this
8197 // application token. Note this list is NOT sorted!
8198 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8199
8200 int groupId = -1;
8201 boolean appFullscreen;
8202 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008204 // These are used for determining when all windows associated with
8205 // an activity have been drawn, so they can be made visible together
8206 // at the same time.
8207 int lastTransactionSequence = mTransactionSequence-1;
8208 int numInterestingWindows;
8209 int numDrawnWindows;
8210 boolean inPendingTransaction;
8211 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008213 // Is this token going to be hidden in a little while? If so, it
8214 // won't be taken into account for setting the screen orientation.
8215 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008217 // Is this window's surface needed? This is almost like hidden, except
8218 // it will sometimes be true a little earlier: when the token has
8219 // been shown, but is still waiting for its app transition to execute
8220 // before making its windows shown.
8221 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223 // Have we told the window clients to hide themselves?
8224 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008226 // Last visibility state we reported to the app token.
8227 boolean reportedVisible;
8228
8229 // Set to true when the token has been removed from the window mgr.
8230 boolean removed;
8231
8232 // Have we been asked to have this token keep the screen frozen?
8233 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 boolean animating;
8236 Animation animation;
8237 boolean hasTransformation;
8238 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008240 // Offset to the window of all layers in the token, for use by
8241 // AppWindowToken animations.
8242 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008244 // Information about an application starting window if displayed.
8245 StartingData startingData;
8246 WindowState startingWindow;
8247 View startingView;
8248 boolean startingDisplayed;
8249 boolean startingMoved;
8250 boolean firstWindowDrawn;
8251
8252 AppWindowToken(IApplicationToken _token) {
8253 super(_token.asBinder(),
8254 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8255 appWindowToken = this;
8256 appToken = _token;
8257 }
Romain Guy06882f82009-06-10 13:36:04 -07008258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 public void setAnimation(Animation anim) {
8260 if (localLOGV) Log.v(
8261 TAG, "Setting animation in " + this + ": " + anim);
8262 animation = anim;
8263 animating = false;
8264 anim.restrictDuration(MAX_ANIMATION_DURATION);
8265 anim.scaleCurrentDuration(mTransitionAnimationScale);
8266 int zorder = anim.getZAdjustment();
8267 int adj = 0;
8268 if (zorder == Animation.ZORDER_TOP) {
8269 adj = TYPE_LAYER_OFFSET;
8270 } else if (zorder == Animation.ZORDER_BOTTOM) {
8271 adj = -TYPE_LAYER_OFFSET;
8272 }
Romain Guy06882f82009-06-10 13:36:04 -07008273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274 if (animLayerAdjustment != adj) {
8275 animLayerAdjustment = adj;
8276 updateLayers();
8277 }
8278 }
Romain Guy06882f82009-06-10 13:36:04 -07008279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 public void setDummyAnimation() {
8281 if (animation == null) {
8282 if (localLOGV) Log.v(
8283 TAG, "Setting dummy animation in " + this);
8284 animation = sDummyAnimation;
8285 }
8286 }
8287
8288 public void clearAnimation() {
8289 if (animation != null) {
8290 animation = null;
8291 animating = true;
8292 }
8293 }
Romain Guy06882f82009-06-10 13:36:04 -07008294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008295 void updateLayers() {
8296 final int N = allAppWindows.size();
8297 final int adj = animLayerAdjustment;
8298 for (int i=0; i<N; i++) {
8299 WindowState w = allAppWindows.get(i);
8300 w.mAnimLayer = w.mLayer + adj;
8301 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8302 + w.mAnimLayer);
8303 if (w == mInputMethodTarget) {
8304 setInputMethodAnimLayerAdjustment(adj);
8305 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008306 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008307 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008309 }
8310 }
Romain Guy06882f82009-06-10 13:36:04 -07008311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008312 void sendAppVisibilityToClients() {
8313 final int N = allAppWindows.size();
8314 for (int i=0; i<N; i++) {
8315 WindowState win = allAppWindows.get(i);
8316 if (win == startingWindow && clientHidden) {
8317 // Don't hide the starting window.
8318 continue;
8319 }
8320 try {
8321 if (DEBUG_VISIBILITY) Log.v(TAG,
8322 "Setting visibility of " + win + ": " + (!clientHidden));
8323 win.mClient.dispatchAppVisibility(!clientHidden);
8324 } catch (RemoteException e) {
8325 }
8326 }
8327 }
Romain Guy06882f82009-06-10 13:36:04 -07008328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008329 void showAllWindowsLocked() {
8330 final int NW = allAppWindows.size();
8331 for (int i=0; i<NW; i++) {
8332 WindowState w = allAppWindows.get(i);
8333 if (DEBUG_VISIBILITY) Log.v(TAG,
8334 "performing show on: " + w);
8335 w.performShowLocked();
8336 }
8337 }
Romain Guy06882f82009-06-10 13:36:04 -07008338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008339 // This must be called while inside a transaction.
8340 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8341 if (!mDisplayFrozen) {
8342 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008344 if (animation == sDummyAnimation) {
8345 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008346 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008347 // when it is really time to animate, this will be set to
8348 // a real animation and the next call will execute normally.
8349 return false;
8350 }
Romain Guy06882f82009-06-10 13:36:04 -07008351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008352 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8353 if (!animating) {
8354 if (DEBUG_ANIM) Log.v(
8355 TAG, "Starting animation in " + this +
8356 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8357 + " scale=" + mTransitionAnimationScale
8358 + " allDrawn=" + allDrawn + " animating=" + animating);
8359 animation.initialize(dw, dh, dw, dh);
8360 animation.setStartTime(currentTime);
8361 animating = true;
8362 }
8363 transformation.clear();
8364 final boolean more = animation.getTransformation(
8365 currentTime, transformation);
8366 if (DEBUG_ANIM) Log.v(
8367 TAG, "Stepped animation in " + this +
8368 ": more=" + more + ", xform=" + transformation);
8369 if (more) {
8370 // we're done!
8371 hasTransformation = true;
8372 return true;
8373 }
8374 if (DEBUG_ANIM) Log.v(
8375 TAG, "Finished animation in " + this +
8376 " @ " + currentTime);
8377 animation = null;
8378 }
8379 } else if (animation != null) {
8380 // If the display is frozen, and there is a pending animation,
8381 // clear it and make sure we run the cleanup code.
8382 animating = true;
8383 animation = null;
8384 }
8385
8386 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008388 if (!animating) {
8389 return false;
8390 }
8391
8392 clearAnimation();
8393 animating = false;
8394 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8395 moveInputMethodWindowsIfNeededLocked(true);
8396 }
Romain Guy06882f82009-06-10 13:36:04 -07008397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 if (DEBUG_ANIM) Log.v(
8399 TAG, "Animation done in " + this
8400 + ": reportedVisible=" + reportedVisible);
8401
8402 transformation.clear();
8403 if (animLayerAdjustment != 0) {
8404 animLayerAdjustment = 0;
8405 updateLayers();
8406 }
Romain Guy06882f82009-06-10 13:36:04 -07008407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 final int N = windows.size();
8409 for (int i=0; i<N; i++) {
8410 ((WindowState)windows.get(i)).finishExit();
8411 }
8412 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008414 return false;
8415 }
8416
8417 void updateReportedVisibilityLocked() {
8418 if (appToken == null) {
8419 return;
8420 }
Romain Guy06882f82009-06-10 13:36:04 -07008421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008422 int numInteresting = 0;
8423 int numVisible = 0;
8424 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008426 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8427 final int N = allAppWindows.size();
8428 for (int i=0; i<N; i++) {
8429 WindowState win = allAppWindows.get(i);
8430 if (win == startingWindow || win.mAppFreezing) {
8431 continue;
8432 }
8433 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008434 Log.v(TAG, "Win " + win + ": isDrawn="
8435 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008436 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008437 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008438 Log.v(TAG, "Not displayed: s=" + win.mSurface
8439 + " pv=" + win.mPolicyVisibility
8440 + " dp=" + win.mDrawPending
8441 + " cdp=" + win.mCommitDrawPending
8442 + " ah=" + win.mAttachedHidden
8443 + " th="
8444 + (win.mAppToken != null
8445 ? win.mAppToken.hiddenRequested : false)
8446 + " a=" + win.mAnimating);
8447 }
8448 }
8449 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008450 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008451 if (!win.isAnimating()) {
8452 numVisible++;
8453 }
8454 nowGone = false;
8455 } else if (win.isAnimating()) {
8456 nowGone = false;
8457 }
8458 }
Romain Guy06882f82009-06-10 13:36:04 -07008459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8461 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8462 + numInteresting + " visible=" + numVisible);
8463 if (nowVisible != reportedVisible) {
8464 if (DEBUG_VISIBILITY) Log.v(
8465 TAG, "Visibility changed in " + this
8466 + ": vis=" + nowVisible);
8467 reportedVisible = nowVisible;
8468 Message m = mH.obtainMessage(
8469 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8470 nowVisible ? 1 : 0,
8471 nowGone ? 1 : 0,
8472 this);
8473 mH.sendMessage(m);
8474 }
8475 }
Romain Guy06882f82009-06-10 13:36:04 -07008476
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008477 WindowState findMainWindow() {
8478 int j = windows.size();
8479 while (j > 0) {
8480 j--;
8481 WindowState win = windows.get(j);
8482 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8483 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8484 return win;
8485 }
8486 }
8487 return null;
8488 }
8489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 void dump(PrintWriter pw, String prefix) {
8491 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008492 if (appToken != null) {
8493 pw.print(prefix); pw.println("app=true");
8494 }
8495 if (allAppWindows.size() > 0) {
8496 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8497 }
8498 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008499 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008500 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8501 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8502 pw.print(" clientHidden="); pw.print(clientHidden);
8503 pw.print(" willBeHidden="); pw.print(willBeHidden);
8504 pw.print(" reportedVisible="); pw.println(reportedVisible);
8505 if (paused || freezingScreen) {
8506 pw.print(prefix); pw.print("paused="); pw.print(paused);
8507 pw.print(" freezingScreen="); pw.println(freezingScreen);
8508 }
8509 if (numInterestingWindows != 0 || numDrawnWindows != 0
8510 || inPendingTransaction || allDrawn) {
8511 pw.print(prefix); pw.print("numInterestingWindows=");
8512 pw.print(numInterestingWindows);
8513 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8514 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8515 pw.print(" allDrawn="); pw.println(allDrawn);
8516 }
8517 if (animating || animation != null) {
8518 pw.print(prefix); pw.print("animating="); pw.print(animating);
8519 pw.print(" animation="); pw.println(animation);
8520 }
8521 if (animLayerAdjustment != 0) {
8522 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8523 }
8524 if (hasTransformation) {
8525 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8526 pw.print(" transformation="); transformation.printShortString(pw);
8527 pw.println();
8528 }
8529 if (startingData != null || removed || firstWindowDrawn) {
8530 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8531 pw.print(" removed="); pw.print(removed);
8532 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8533 }
8534 if (startingWindow != null || startingView != null
8535 || startingDisplayed || startingMoved) {
8536 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8537 pw.print(" startingView="); pw.print(startingView);
8538 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8539 pw.print(" startingMoved"); pw.println(startingMoved);
8540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008541 }
8542
8543 @Override
8544 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008545 if (stringName == null) {
8546 StringBuilder sb = new StringBuilder();
8547 sb.append("AppWindowToken{");
8548 sb.append(Integer.toHexString(System.identityHashCode(this)));
8549 sb.append(" token="); sb.append(token); sb.append('}');
8550 stringName = sb.toString();
8551 }
8552 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 }
8554 }
Romain Guy06882f82009-06-10 13:36:04 -07008555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008556 // -------------------------------------------------------------
8557 // DummyAnimation
8558 // -------------------------------------------------------------
8559
8560 // This is an animation that does nothing: it just immediately finishes
8561 // itself every time it is called. It is used as a stub animation in cases
8562 // where we want to synchronize multiple things that may be animating.
8563 static final class DummyAnimation extends Animation {
8564 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8565 return false;
8566 }
8567 }
8568 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 // -------------------------------------------------------------
8571 // Async Handler
8572 // -------------------------------------------------------------
8573
8574 static final class StartingData {
8575 final String pkg;
8576 final int theme;
8577 final CharSequence nonLocalizedLabel;
8578 final int labelRes;
8579 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008581 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8582 int _labelRes, int _icon) {
8583 pkg = _pkg;
8584 theme = _theme;
8585 nonLocalizedLabel = _nonLocalizedLabel;
8586 labelRes = _labelRes;
8587 icon = _icon;
8588 }
8589 }
8590
8591 private final class H extends Handler {
8592 public static final int REPORT_FOCUS_CHANGE = 2;
8593 public static final int REPORT_LOSING_FOCUS = 3;
8594 public static final int ANIMATE = 4;
8595 public static final int ADD_STARTING = 5;
8596 public static final int REMOVE_STARTING = 6;
8597 public static final int FINISHED_STARTING = 7;
8598 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008599 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8600 public static final int HOLD_SCREEN_CHANGED = 12;
8601 public static final int APP_TRANSITION_TIMEOUT = 13;
8602 public static final int PERSIST_ANIMATION_SCALE = 14;
8603 public static final int FORCE_GC = 15;
8604 public static final int ENABLE_SCREEN = 16;
8605 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008606 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008608 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008610 public H() {
8611 }
Romain Guy06882f82009-06-10 13:36:04 -07008612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008613 @Override
8614 public void handleMessage(Message msg) {
8615 switch (msg.what) {
8616 case REPORT_FOCUS_CHANGE: {
8617 WindowState lastFocus;
8618 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008620 synchronized(mWindowMap) {
8621 lastFocus = mLastFocus;
8622 newFocus = mCurrentFocus;
8623 if (lastFocus == newFocus) {
8624 // Focus is not changing, so nothing to do.
8625 return;
8626 }
8627 mLastFocus = newFocus;
8628 //Log.i(TAG, "Focus moving from " + lastFocus
8629 // + " to " + newFocus);
8630 if (newFocus != null && lastFocus != null
8631 && !newFocus.isDisplayedLw()) {
8632 //Log.i(TAG, "Delaying loss of focus...");
8633 mLosingFocus.add(lastFocus);
8634 lastFocus = null;
8635 }
8636 }
8637
8638 if (lastFocus != newFocus) {
8639 //System.out.println("Changing focus from " + lastFocus
8640 // + " to " + newFocus);
8641 if (newFocus != null) {
8642 try {
8643 //Log.i(TAG, "Gaining focus: " + newFocus);
8644 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8645 } catch (RemoteException e) {
8646 // Ignore if process has died.
8647 }
8648 }
8649
8650 if (lastFocus != null) {
8651 try {
8652 //Log.i(TAG, "Losing focus: " + lastFocus);
8653 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8654 } catch (RemoteException e) {
8655 // Ignore if process has died.
8656 }
8657 }
8658 }
8659 } break;
8660
8661 case REPORT_LOSING_FOCUS: {
8662 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 synchronized(mWindowMap) {
8665 losers = mLosingFocus;
8666 mLosingFocus = new ArrayList<WindowState>();
8667 }
8668
8669 final int N = losers.size();
8670 for (int i=0; i<N; i++) {
8671 try {
8672 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8673 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8674 } catch (RemoteException e) {
8675 // Ignore if process has died.
8676 }
8677 }
8678 } break;
8679
8680 case ANIMATE: {
8681 synchronized(mWindowMap) {
8682 mAnimationPending = false;
8683 performLayoutAndPlaceSurfacesLocked();
8684 }
8685 } break;
8686
8687 case ADD_STARTING: {
8688 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8689 final StartingData sd = wtoken.startingData;
8690
8691 if (sd == null) {
8692 // Animation has been canceled... do nothing.
8693 return;
8694 }
Romain Guy06882f82009-06-10 13:36:04 -07008695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008696 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8697 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008699 View view = null;
8700 try {
8701 view = mPolicy.addStartingWindow(
8702 wtoken.token, sd.pkg,
8703 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8704 sd.icon);
8705 } catch (Exception e) {
8706 Log.w(TAG, "Exception when adding starting window", e);
8707 }
8708
8709 if (view != null) {
8710 boolean abort = false;
8711
8712 synchronized(mWindowMap) {
8713 if (wtoken.removed || wtoken.startingData == null) {
8714 // If the window was successfully added, then
8715 // we need to remove it.
8716 if (wtoken.startingWindow != null) {
8717 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8718 "Aborted starting " + wtoken
8719 + ": removed=" + wtoken.removed
8720 + " startingData=" + wtoken.startingData);
8721 wtoken.startingWindow = null;
8722 wtoken.startingData = null;
8723 abort = true;
8724 }
8725 } else {
8726 wtoken.startingView = view;
8727 }
8728 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8729 "Added starting " + wtoken
8730 + ": startingWindow="
8731 + wtoken.startingWindow + " startingView="
8732 + wtoken.startingView);
8733 }
8734
8735 if (abort) {
8736 try {
8737 mPolicy.removeStartingWindow(wtoken.token, view);
8738 } catch (Exception e) {
8739 Log.w(TAG, "Exception when removing starting window", e);
8740 }
8741 }
8742 }
8743 } break;
8744
8745 case REMOVE_STARTING: {
8746 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8747 IBinder token = null;
8748 View view = null;
8749 synchronized (mWindowMap) {
8750 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8751 + wtoken + ": startingWindow="
8752 + wtoken.startingWindow + " startingView="
8753 + wtoken.startingView);
8754 if (wtoken.startingWindow != null) {
8755 view = wtoken.startingView;
8756 token = wtoken.token;
8757 wtoken.startingData = null;
8758 wtoken.startingView = null;
8759 wtoken.startingWindow = null;
8760 }
8761 }
8762 if (view != null) {
8763 try {
8764 mPolicy.removeStartingWindow(token, view);
8765 } catch (Exception e) {
8766 Log.w(TAG, "Exception when removing starting window", e);
8767 }
8768 }
8769 } break;
8770
8771 case FINISHED_STARTING: {
8772 IBinder token = null;
8773 View view = null;
8774 while (true) {
8775 synchronized (mWindowMap) {
8776 final int N = mFinishedStarting.size();
8777 if (N <= 0) {
8778 break;
8779 }
8780 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8781
8782 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8783 "Finished starting " + wtoken
8784 + ": startingWindow=" + wtoken.startingWindow
8785 + " startingView=" + wtoken.startingView);
8786
8787 if (wtoken.startingWindow == null) {
8788 continue;
8789 }
8790
8791 view = wtoken.startingView;
8792 token = wtoken.token;
8793 wtoken.startingData = null;
8794 wtoken.startingView = null;
8795 wtoken.startingWindow = null;
8796 }
8797
8798 try {
8799 mPolicy.removeStartingWindow(token, view);
8800 } catch (Exception e) {
8801 Log.w(TAG, "Exception when removing starting window", e);
8802 }
8803 }
8804 } break;
8805
8806 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8807 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8808
8809 boolean nowVisible = msg.arg1 != 0;
8810 boolean nowGone = msg.arg2 != 0;
8811
8812 try {
8813 if (DEBUG_VISIBILITY) Log.v(
8814 TAG, "Reporting visible in " + wtoken
8815 + " visible=" + nowVisible
8816 + " gone=" + nowGone);
8817 if (nowVisible) {
8818 wtoken.appToken.windowsVisible();
8819 } else {
8820 wtoken.appToken.windowsGone();
8821 }
8822 } catch (RemoteException ex) {
8823 }
8824 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008826 case WINDOW_FREEZE_TIMEOUT: {
8827 synchronized (mWindowMap) {
8828 Log.w(TAG, "Window freeze timeout expired.");
8829 int i = mWindows.size();
8830 while (i > 0) {
8831 i--;
8832 WindowState w = (WindowState)mWindows.get(i);
8833 if (w.mOrientationChanging) {
8834 w.mOrientationChanging = false;
8835 Log.w(TAG, "Force clearing orientation change: " + w);
8836 }
8837 }
8838 performLayoutAndPlaceSurfacesLocked();
8839 }
8840 break;
8841 }
Romain Guy06882f82009-06-10 13:36:04 -07008842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008843 case HOLD_SCREEN_CHANGED: {
8844 Session oldHold;
8845 Session newHold;
8846 synchronized (mWindowMap) {
8847 oldHold = mLastReportedHold;
8848 newHold = (Session)msg.obj;
8849 mLastReportedHold = newHold;
8850 }
Romain Guy06882f82009-06-10 13:36:04 -07008851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008852 if (oldHold != newHold) {
8853 try {
8854 if (oldHold != null) {
8855 mBatteryStats.noteStopWakelock(oldHold.mUid,
8856 "window",
8857 BatteryStats.WAKE_TYPE_WINDOW);
8858 }
8859 if (newHold != null) {
8860 mBatteryStats.noteStartWakelock(newHold.mUid,
8861 "window",
8862 BatteryStats.WAKE_TYPE_WINDOW);
8863 }
8864 } catch (RemoteException e) {
8865 }
8866 }
8867 break;
8868 }
Romain Guy06882f82009-06-10 13:36:04 -07008869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008870 case APP_TRANSITION_TIMEOUT: {
8871 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008872 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008873 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8874 "*** APP TRANSITION TIMEOUT");
8875 mAppTransitionReady = true;
8876 mAppTransitionTimeout = true;
8877 performLayoutAndPlaceSurfacesLocked();
8878 }
8879 }
8880 break;
8881 }
Romain Guy06882f82009-06-10 13:36:04 -07008882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008883 case PERSIST_ANIMATION_SCALE: {
8884 Settings.System.putFloat(mContext.getContentResolver(),
8885 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8886 Settings.System.putFloat(mContext.getContentResolver(),
8887 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8888 break;
8889 }
Romain Guy06882f82009-06-10 13:36:04 -07008890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008891 case FORCE_GC: {
8892 synchronized(mWindowMap) {
8893 if (mAnimationPending) {
8894 // If we are animating, don't do the gc now but
8895 // delay a bit so we don't interrupt the animation.
8896 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8897 2000);
8898 return;
8899 }
8900 // If we are currently rotating the display, it will
8901 // schedule a new message when done.
8902 if (mDisplayFrozen) {
8903 return;
8904 }
8905 mFreezeGcPending = 0;
8906 }
8907 Runtime.getRuntime().gc();
8908 break;
8909 }
Romain Guy06882f82009-06-10 13:36:04 -07008910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008911 case ENABLE_SCREEN: {
8912 performEnableScreen();
8913 break;
8914 }
Romain Guy06882f82009-06-10 13:36:04 -07008915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 case APP_FREEZE_TIMEOUT: {
8917 synchronized (mWindowMap) {
8918 Log.w(TAG, "App freeze timeout expired.");
8919 int i = mAppTokens.size();
8920 while (i > 0) {
8921 i--;
8922 AppWindowToken tok = mAppTokens.get(i);
8923 if (tok.freezingScreen) {
8924 Log.w(TAG, "Force clearing freeze: " + tok);
8925 unsetAppFreezingScreenLocked(tok, true, true);
8926 }
8927 }
8928 }
8929 break;
8930 }
Romain Guy06882f82009-06-10 13:36:04 -07008931
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008932 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008933 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008934 sendNewConfiguration();
8935 }
8936 break;
8937 }
Romain Guy06882f82009-06-10 13:36:04 -07008938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008939 }
8940 }
8941 }
8942
8943 // -------------------------------------------------------------
8944 // IWindowManager API
8945 // -------------------------------------------------------------
8946
8947 public IWindowSession openSession(IInputMethodClient client,
8948 IInputContext inputContext) {
8949 if (client == null) throw new IllegalArgumentException("null client");
8950 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8951 return new Session(client, inputContext);
8952 }
8953
8954 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8955 synchronized (mWindowMap) {
8956 // The focus for the client is the window immediately below
8957 // where we would place the input method window.
8958 int idx = findDesiredInputMethodWindowIndexLocked(false);
8959 WindowState imFocus;
8960 if (idx > 0) {
8961 imFocus = (WindowState)mWindows.get(idx-1);
8962 if (imFocus != null) {
8963 if (imFocus.mSession.mClient != null &&
8964 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8965 return true;
8966 }
8967 }
8968 }
8969 }
8970 return false;
8971 }
Romain Guy06882f82009-06-10 13:36:04 -07008972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008973 // -------------------------------------------------------------
8974 // Internals
8975 // -------------------------------------------------------------
8976
8977 final WindowState windowForClientLocked(Session session, IWindow client) {
8978 return windowForClientLocked(session, client.asBinder());
8979 }
Romain Guy06882f82009-06-10 13:36:04 -07008980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008981 final WindowState windowForClientLocked(Session session, IBinder client) {
8982 WindowState win = mWindowMap.get(client);
8983 if (localLOGV) Log.v(
8984 TAG, "Looking up client " + client + ": " + win);
8985 if (win == null) {
8986 RuntimeException ex = new RuntimeException();
8987 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8988 return null;
8989 }
8990 if (session != null && win.mSession != session) {
8991 RuntimeException ex = new RuntimeException();
8992 Log.w(TAG, "Requested window " + client + " is in session " +
8993 win.mSession + ", not " + session, ex);
8994 return null;
8995 }
8996
8997 return win;
8998 }
8999
Dianne Hackborna8f60182009-09-01 19:01:50 -07009000 final void rebuildAppWindowListLocked() {
9001 int NW = mWindows.size();
9002 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009003 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009004 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009005
9006 // First remove all existing app windows.
9007 i=0;
9008 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009009 WindowState w = (WindowState)mWindows.get(i);
9010 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009011 WindowState win = (WindowState)mWindows.remove(i);
9012 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
9013 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009014 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009015 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009016 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009017 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9018 && lastWallpaper == i-1) {
9019 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009020 }
9021 i++;
9022 }
9023
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009024 // The wallpaper window(s) typically live at the bottom of the stack,
9025 // so skip them before adding app tokens.
9026 lastWallpaper++;
9027 i = lastWallpaper;
9028
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009029 // First add all of the exiting app tokens... these are no longer
9030 // in the main app list, but still have windows shown. We put them
9031 // in the back because now that the animation is over we no longer
9032 // will care about them.
9033 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009034 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009035 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9036 }
9037
9038 // And add in the still active app tokens in Z order.
9039 NT = mAppTokens.size();
9040 for (int j=0; j<NT; j++) {
9041 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009042 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009043
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009044 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009045 if (i != numRemoved) {
9046 Log.w(TAG, "Rebuild removed " + numRemoved
9047 + " windows but added " + i);
9048 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009049 }
9050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009051 private final void assignLayersLocked() {
9052 int N = mWindows.size();
9053 int curBaseLayer = 0;
9054 int curLayer = 0;
9055 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009057 for (i=0; i<N; i++) {
9058 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009059 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9060 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009061 curLayer += WINDOW_LAYER_MULTIPLIER;
9062 w.mLayer = curLayer;
9063 } else {
9064 curBaseLayer = curLayer = w.mBaseLayer;
9065 w.mLayer = curLayer;
9066 }
9067 if (w.mTargetAppToken != null) {
9068 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9069 } else if (w.mAppToken != null) {
9070 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9071 } else {
9072 w.mAnimLayer = w.mLayer;
9073 }
9074 if (w.mIsImWindow) {
9075 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009076 } else if (w.mIsWallpaper) {
9077 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009078 }
9079 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9080 + w.mAnimLayer);
9081 //System.out.println(
9082 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9083 }
9084 }
9085
9086 private boolean mInLayout = false;
9087 private final void performLayoutAndPlaceSurfacesLocked() {
9088 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009089 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009090 throw new RuntimeException("Recursive call!");
9091 }
9092 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9093 return;
9094 }
9095
9096 boolean recoveringMemory = false;
9097 if (mForceRemoves != null) {
9098 recoveringMemory = true;
9099 // Wait a little it for things to settle down, and off we go.
9100 for (int i=0; i<mForceRemoves.size(); i++) {
9101 WindowState ws = mForceRemoves.get(i);
9102 Log.i(TAG, "Force removing: " + ws);
9103 removeWindowInnerLocked(ws.mSession, ws);
9104 }
9105 mForceRemoves = null;
9106 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9107 Object tmp = new Object();
9108 synchronized (tmp) {
9109 try {
9110 tmp.wait(250);
9111 } catch (InterruptedException e) {
9112 }
9113 }
9114 }
Romain Guy06882f82009-06-10 13:36:04 -07009115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009116 mInLayout = true;
9117 try {
9118 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009120 int i = mPendingRemove.size()-1;
9121 if (i >= 0) {
9122 while (i >= 0) {
9123 WindowState w = mPendingRemove.get(i);
9124 removeWindowInnerLocked(w.mSession, w);
9125 i--;
9126 }
9127 mPendingRemove.clear();
9128
9129 mInLayout = false;
9130 assignLayersLocked();
9131 mLayoutNeeded = true;
9132 performLayoutAndPlaceSurfacesLocked();
9133
9134 } else {
9135 mInLayout = false;
9136 if (mLayoutNeeded) {
9137 requestAnimationLocked(0);
9138 }
9139 }
9140 } catch (RuntimeException e) {
9141 mInLayout = false;
9142 Log.e(TAG, "Unhandled exception while layout out windows", e);
9143 }
9144 }
9145
9146 private final void performLayoutLockedInner() {
9147 final int dw = mDisplay.getWidth();
9148 final int dh = mDisplay.getHeight();
9149
9150 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009151 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009152 int i;
9153
9154 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009155
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009156 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157 mPolicy.beginLayoutLw(dw, dh);
9158
9159 // First perform layout of any root windows (not attached
9160 // to another window).
9161 int topAttached = -1;
9162 for (i = N-1; i >= 0; i--) {
9163 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009164
9165 // Don't do layout of a window if it is not visible, or
9166 // soon won't be visible, to avoid wasting time and funky
9167 // changes while a window is animating away.
9168 final AppWindowToken atoken = win.mAppToken;
9169 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009170 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009171 || win.mRootToken.hidden
9172 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009173 || win.mAttachedHidden
9174 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009175
9176 // If this view is GONE, then skip it -- keep the current
9177 // frame, and let the caller know so they can ignore it
9178 // if they want. (We do the normal layout for INVISIBLE
9179 // windows, since that means "perform layout as normal,
9180 // just don't display").
9181 if (!gone || !win.mHaveFrame) {
9182 if (!win.mLayoutAttached) {
9183 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9184 } else {
9185 if (topAttached < 0) topAttached = i;
9186 }
9187 }
9188 }
Romain Guy06882f82009-06-10 13:36:04 -07009189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009190 // Now perform layout of attached windows, which usually
9191 // depend on the position of the window they are attached to.
9192 // XXX does not deal with windows that are attached to windows
9193 // that are themselves attached.
9194 for (i = topAttached; i >= 0; i--) {
9195 WindowState win = (WindowState) mWindows.get(i);
9196
9197 // If this view is GONE, then skip it -- keep the current
9198 // frame, and let the caller know so they can ignore it
9199 // if they want. (We do the normal layout for INVISIBLE
9200 // windows, since that means "perform layout as normal,
9201 // just don't display").
9202 if (win.mLayoutAttached) {
9203 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9204 || !win.mHaveFrame) {
9205 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9206 }
9207 }
9208 }
9209
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009210 int changes = mPolicy.finishLayoutLw();
9211 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9212 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9213 assignLayersLocked();
9214 }
9215 }
9216 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009217 mLayoutNeeded = false;
9218 } else if (repeats > 2) {
9219 Log.w(TAG, "Layout repeat aborted after too many iterations");
9220 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009221 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9222 Configuration newConfig = updateOrientationFromAppTokensLocked(
9223 null, null);
9224 if (newConfig != null) {
9225 mLayoutNeeded = true;
9226 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9227 }
9228 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009229 } else {
9230 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009231 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9232 Configuration newConfig = updateOrientationFromAppTokensLocked(
9233 null, null);
9234 if (newConfig != null) {
9235 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9236 }
9237 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 }
9240 }
Romain Guy06882f82009-06-10 13:36:04 -07009241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009242 private final void performLayoutAndPlaceSurfacesLockedInner(
9243 boolean recoveringMemory) {
9244 final long currentTime = SystemClock.uptimeMillis();
9245 final int dw = mDisplay.getWidth();
9246 final int dh = mDisplay.getHeight();
9247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009248 int i;
9249
9250 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009251 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009253 if (mFxSession == null) {
9254 mFxSession = new SurfaceSession();
9255 }
Romain Guy06882f82009-06-10 13:36:04 -07009256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009257 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9258
9259 // Initialize state of exiting tokens.
9260 for (i=mExitingTokens.size()-1; i>=0; i--) {
9261 mExitingTokens.get(i).hasVisible = false;
9262 }
9263
9264 // Initialize state of exiting applications.
9265 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9266 mExitingAppTokens.get(i).hasVisible = false;
9267 }
9268
9269 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009270 boolean orientationChangeComplete = true;
9271 Session holdScreen = null;
9272 float screenBrightness = -1;
9273 boolean focusDisplayed = false;
9274 boolean animating = false;
9275
9276 Surface.openTransaction();
9277 try {
9278 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009279 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280
9281 do {
9282 final int transactionSequence = ++mTransactionSequence;
9283
9284 // Update animations of all applications, including those
9285 // associated with exiting/removed apps
9286 boolean tokensAnimating = false;
9287 final int NAT = mAppTokens.size();
9288 for (i=0; i<NAT; i++) {
9289 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9290 tokensAnimating = true;
9291 }
9292 }
9293 final int NEAT = mExitingAppTokens.size();
9294 for (i=0; i<NEAT; i++) {
9295 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9296 tokensAnimating = true;
9297 }
9298 }
9299
9300 animating = tokensAnimating;
9301 restart = false;
9302
9303 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009304 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009305 boolean focusMayChange = false;
9306 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009307
9308 mPolicy.beginAnimationLw(dw, dh);
9309
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009310 final int N = mWindows.size();
9311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009312 for (i=N-1; i>=0; i--) {
9313 WindowState w = (WindowState)mWindows.get(i);
9314
9315 final WindowManager.LayoutParams attrs = w.mAttrs;
9316
9317 if (w.mSurface != null) {
9318 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009319 if (w.commitFinishDrawingLocked(currentTime)) {
9320 if ((w.mAttrs.flags
9321 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009322 if (DEBUG_WALLPAPER) Log.v(TAG,
9323 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009324 wallpaperMayChange = true;
9325 }
9326 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009327
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009328 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009329 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9330 animating = true;
9331 //w.dump(" ");
9332 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009333 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9334 wallpaperMayChange = true;
9335 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009336
9337 if (mPolicy.doesForceHide(w, attrs)) {
9338 if (!wasAnimating && animating) {
9339 wallpaperForceHidingChanged = true;
9340 focusMayChange = true;
9341 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9342 forceHiding = true;
9343 }
9344 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9345 boolean changed;
9346 if (forceHiding) {
9347 changed = w.hideLw(false, false);
9348 } else {
9349 changed = w.showLw(false, false);
9350 if (changed && wallpaperForceHidingChanged
9351 && w.isReadyForDisplay()) {
9352 // Assume we will need to animate. If
9353 // we don't (because the wallpaper will
9354 // stay with the lock screen), then we will
9355 // clean up later.
9356 Animation a = mPolicy.createForceHideEnterAnimation();
9357 if (a != null) {
9358 w.setAnimation(a);
9359 }
9360 }
9361 }
9362 if (changed && (attrs.flags
9363 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9364 wallpaperMayChange = true;
9365 }
Christopher Tate405d0892009-10-27 20:23:28 -07009366 if (changed && !forceHiding
9367 && (mCurrentFocus == null)
9368 && (mFocusedApp != null)) {
9369 // It's possible that the last focus recalculation left no
9370 // current focused window even though the app has come to the
9371 // foreground already. In this case, we make sure to recalculate
9372 // focus when we show a window.
9373 focusMayChange = true;
9374 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009375 }
9376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009377 mPolicy.animatingWindowLw(w, attrs);
9378 }
9379
9380 final AppWindowToken atoken = w.mAppToken;
9381 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9382 if (atoken.lastTransactionSequence != transactionSequence) {
9383 atoken.lastTransactionSequence = transactionSequence;
9384 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9385 atoken.startingDisplayed = false;
9386 }
9387 if ((w.isOnScreen() || w.mAttrs.type
9388 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9389 && !w.mExiting && !w.mDestroying) {
9390 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009391 Log.v(TAG, "Eval win " + w + ": isDrawn="
9392 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009393 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009394 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009395 Log.v(TAG, "Not displayed: s=" + w.mSurface
9396 + " pv=" + w.mPolicyVisibility
9397 + " dp=" + w.mDrawPending
9398 + " cdp=" + w.mCommitDrawPending
9399 + " ah=" + w.mAttachedHidden
9400 + " th=" + atoken.hiddenRequested
9401 + " a=" + w.mAnimating);
9402 }
9403 }
9404 if (w != atoken.startingWindow) {
9405 if (!atoken.freezingScreen || !w.mAppFreezing) {
9406 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009407 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009408 atoken.numDrawnWindows++;
9409 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9410 "tokenMayBeDrawn: " + atoken
9411 + " freezingScreen=" + atoken.freezingScreen
9412 + " mAppFreezing=" + w.mAppFreezing);
9413 tokenMayBeDrawn = true;
9414 }
9415 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009416 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009417 atoken.startingDisplayed = true;
9418 }
9419 }
9420 } else if (w.mReadyToShow) {
9421 w.performShowLocked();
9422 }
9423 }
9424
9425 if (mPolicy.finishAnimationLw()) {
9426 restart = true;
9427 }
9428
9429 if (tokenMayBeDrawn) {
9430 // See if any windows have been drawn, so they (and others
9431 // associated with them) can now be shown.
9432 final int NT = mTokenList.size();
9433 for (i=0; i<NT; i++) {
9434 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9435 if (wtoken == null) {
9436 continue;
9437 }
9438 if (wtoken.freezingScreen) {
9439 int numInteresting = wtoken.numInterestingWindows;
9440 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9441 if (DEBUG_VISIBILITY) Log.v(TAG,
9442 "allDrawn: " + wtoken
9443 + " interesting=" + numInteresting
9444 + " drawn=" + wtoken.numDrawnWindows);
9445 wtoken.showAllWindowsLocked();
9446 unsetAppFreezingScreenLocked(wtoken, false, true);
9447 orientationChangeComplete = true;
9448 }
9449 } else if (!wtoken.allDrawn) {
9450 int numInteresting = wtoken.numInterestingWindows;
9451 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9452 if (DEBUG_VISIBILITY) Log.v(TAG,
9453 "allDrawn: " + wtoken
9454 + " interesting=" + numInteresting
9455 + " drawn=" + wtoken.numDrawnWindows);
9456 wtoken.allDrawn = true;
9457 restart = true;
9458
9459 // We can now show all of the drawn windows!
9460 if (!mOpeningApps.contains(wtoken)) {
9461 wtoken.showAllWindowsLocked();
9462 }
9463 }
9464 }
9465 }
9466 }
9467
9468 // If we are ready to perform an app transition, check through
9469 // all of the app tokens to be shown and see if they are ready
9470 // to go.
9471 if (mAppTransitionReady) {
9472 int NN = mOpeningApps.size();
9473 boolean goodToGo = true;
9474 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9475 "Checking " + NN + " opening apps (frozen="
9476 + mDisplayFrozen + " timeout="
9477 + mAppTransitionTimeout + ")...");
9478 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9479 // If the display isn't frozen, wait to do anything until
9480 // all of the apps are ready. Otherwise just go because
9481 // we'll unfreeze the display when everyone is ready.
9482 for (i=0; i<NN && goodToGo; i++) {
9483 AppWindowToken wtoken = mOpeningApps.get(i);
9484 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9485 "Check opening app" + wtoken + ": allDrawn="
9486 + wtoken.allDrawn + " startingDisplayed="
9487 + wtoken.startingDisplayed);
9488 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9489 && !wtoken.startingMoved) {
9490 goodToGo = false;
9491 }
9492 }
9493 }
9494 if (goodToGo) {
9495 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9496 int transit = mNextAppTransition;
9497 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009498 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009499 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009500 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009502 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009503 mAppTransitionTimeout = false;
9504 mStartingIconInTransition = false;
9505 mSkipAppTransitionAnimation = false;
9506
9507 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9508
Dianne Hackborna8f60182009-09-01 19:01:50 -07009509 // If there are applications waiting to come to the
9510 // top of the stack, now is the time to move their windows.
9511 // (Note that we don't do apps going to the bottom
9512 // here -- we want to keep their windows in the old
9513 // Z-order until the animation completes.)
9514 if (mToTopApps.size() > 0) {
9515 NN = mAppTokens.size();
9516 for (i=0; i<NN; i++) {
9517 AppWindowToken wtoken = mAppTokens.get(i);
9518 if (wtoken.sendingToTop) {
9519 wtoken.sendingToTop = false;
9520 moveAppWindowsLocked(wtoken, NN, false);
9521 }
9522 }
9523 mToTopApps.clear();
9524 }
9525
Dianne Hackborn25994b42009-09-04 14:21:19 -07009526 WindowState oldWallpaper = mWallpaperTarget;
9527
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009528 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009529 wallpaperMayChange = false;
9530
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009531 // The top-most window will supply the layout params,
9532 // and we will determine it below.
9533 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009534 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009535 int bestAnimLayer = -1;
9536
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009537 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009538 "New wallpaper target=" + mWallpaperTarget
9539 + ", lower target=" + mLowerWallpaperTarget
9540 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009541 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009542 // Do a first pass through the tokens for two
9543 // things:
9544 // (1) Determine if both the closing and opening
9545 // app token sets are wallpaper targets, in which
9546 // case special animations are needed
9547 // (since the wallpaper needs to stay static
9548 // behind them).
9549 // (2) Find the layout params of the top-most
9550 // application window in the tokens, which is
9551 // what will control the animation theme.
9552 final int NC = mClosingApps.size();
9553 NN = NC + mOpeningApps.size();
9554 for (i=0; i<NN; i++) {
9555 AppWindowToken wtoken;
9556 int mode;
9557 if (i < NC) {
9558 wtoken = mClosingApps.get(i);
9559 mode = 1;
9560 } else {
9561 wtoken = mOpeningApps.get(i-NC);
9562 mode = 2;
9563 }
9564 if (mLowerWallpaperTarget != null) {
9565 if (mLowerWallpaperTarget.mAppToken == wtoken
9566 || mUpperWallpaperTarget.mAppToken == wtoken) {
9567 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009568 }
9569 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009570 if (wtoken.appFullscreen) {
9571 WindowState ws = wtoken.findMainWindow();
9572 if (ws != null) {
9573 // If this is a compatibility mode
9574 // window, we will always use its anim.
9575 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9576 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009577 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009578 bestAnimLayer = Integer.MAX_VALUE;
9579 } else if (ws.mLayer > bestAnimLayer) {
9580 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009581 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009582 bestAnimLayer = ws.mLayer;
9583 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009584 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009585 }
9586 }
9587
Dianne Hackborn25994b42009-09-04 14:21:19 -07009588 if (foundWallpapers == 3) {
9589 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9590 "Wallpaper animation!");
9591 switch (transit) {
9592 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9593 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9594 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9595 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9596 break;
9597 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9598 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9599 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9600 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9601 break;
9602 }
9603 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9604 "New transit: " + transit);
9605 } else if (oldWallpaper != null) {
9606 // We are transitioning from an activity with
9607 // a wallpaper to one without.
9608 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9609 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9610 "New transit away from wallpaper: " + transit);
9611 } else if (mWallpaperTarget != null) {
9612 // We are transitioning from an activity without
9613 // a wallpaper to now showing the wallpaper
9614 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9615 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9616 "New transit into wallpaper: " + transit);
9617 }
9618
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009619 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9620 mLastEnterAnimToken = animToken;
9621 mLastEnterAnimParams = animLp;
9622 } else if (mLastEnterAnimParams != null) {
9623 animLp = mLastEnterAnimParams;
9624 mLastEnterAnimToken = null;
9625 mLastEnterAnimParams = null;
9626 }
9627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009628 NN = mOpeningApps.size();
9629 for (i=0; i<NN; i++) {
9630 AppWindowToken wtoken = mOpeningApps.get(i);
9631 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9632 "Now opening app" + wtoken);
9633 wtoken.reportedVisible = false;
9634 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009635 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009636 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009637 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009638 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009639 wtoken.showAllWindowsLocked();
9640 }
9641 NN = mClosingApps.size();
9642 for (i=0; i<NN; i++) {
9643 AppWindowToken wtoken = mClosingApps.get(i);
9644 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9645 "Now closing app" + wtoken);
9646 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009647 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009648 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009649 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009650 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009651 // Force the allDrawn flag, because we want to start
9652 // this guy's animations regardless of whether it's
9653 // gotten drawn.
9654 wtoken.allDrawn = true;
9655 }
9656
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009657 mNextAppTransitionPackage = null;
9658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009659 mOpeningApps.clear();
9660 mClosingApps.clear();
9661
9662 // This has changed the visibility of windows, so perform
9663 // a new layout to get them all up-to-date.
9664 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009665 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9666 assignLayersLocked();
9667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009668 performLayoutLockedInner();
9669 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009670 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009671
9672 restart = true;
9673 }
9674 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009675
Dianne Hackborna8f60182009-09-01 19:01:50 -07009676 if (!animating && mAppTransitionRunning) {
9677 // We have finished the animation of an app transition. To do
9678 // this, we have delayed a lot of operations like showing and
9679 // hiding apps, moving apps in Z-order, etc. The app token list
9680 // reflects the correct Z-order, but the window list may now
9681 // be out of sync with it. So here we will just rebuild the
9682 // entire app window list. Fun!
9683 mAppTransitionRunning = false;
9684 // Clear information about apps that were moving.
9685 mToBottomApps.clear();
9686
9687 rebuildAppWindowListLocked();
9688 restart = true;
9689 moveInputMethodWindowsIfNeededLocked(false);
9690 wallpaperMayChange = true;
9691 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009692 // Since the window list has been rebuilt, focus might
9693 // have to be recomputed since the actual order of windows
9694 // might have changed again.
9695 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009696 }
9697
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009698 int adjResult = 0;
9699
9700 if (wallpaperForceHidingChanged) {
9701 // At this point, there was a window with a wallpaper that
9702 // was force hiding other windows behind it, but now it
9703 // is going away. This may be simple -- just animate
9704 // away the wallpaper and its window -- or it may be
9705 // hard -- the wallpaper now needs to be shown behind
9706 // something that was hidden.
9707 WindowState oldWallpaper = mWallpaperTarget;
9708 adjResult = adjustWallpaperWindowsLocked();
9709 wallpaperMayChange = false;
9710 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9711 + " NEW: " + mWallpaperTarget);
9712 if (mLowerWallpaperTarget == null) {
9713 // Whoops, we don't need a special wallpaper animation.
9714 // Clear them out.
9715 forceHiding = false;
9716 for (i=N-1; i>=0; i--) {
9717 WindowState w = (WindowState)mWindows.get(i);
9718 if (w.mSurface != null) {
9719 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009720 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9721 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009722 forceHiding = true;
9723 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9724 if (!w.mAnimating) {
9725 // We set the animation above so it
9726 // is not yet running.
9727 w.clearAnimation();
9728 }
9729 }
9730 }
9731 }
9732 }
9733 }
9734
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009735 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009736 if (DEBUG_WALLPAPER) Log.v(TAG,
9737 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009738 adjResult = adjustWallpaperWindowsLocked();
9739 }
9740
9741 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9742 if (DEBUG_WALLPAPER) Log.v(TAG,
9743 "Wallpaper layer changed: assigning layers + relayout");
9744 restart = true;
9745 mLayoutNeeded = true;
9746 assignLayersLocked();
9747 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9748 if (DEBUG_WALLPAPER) Log.v(TAG,
9749 "Wallpaper visibility changed: relayout");
9750 restart = true;
9751 mLayoutNeeded = true;
9752 }
9753
9754 if (focusMayChange) {
9755 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009756 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009757 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009758 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009759 }
9760
9761 if (mLayoutNeeded) {
9762 restart = true;
9763 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009764 }
9765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009766 } while (restart);
9767
9768 // THIRD LOOP: Update the surfaces of all windows.
9769
9770 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9771
9772 boolean obscured = false;
9773 boolean blurring = false;
9774 boolean dimming = false;
9775 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009776 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009777 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009778
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009779 final int N = mWindows.size();
9780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009781 for (i=N-1; i>=0; i--) {
9782 WindowState w = (WindowState)mWindows.get(i);
9783
9784 boolean displayed = false;
9785 final WindowManager.LayoutParams attrs = w.mAttrs;
9786 final int attrFlags = attrs.flags;
9787
9788 if (w.mSurface != null) {
9789 w.computeShownFrameLocked();
9790 if (localLOGV) Log.v(
9791 TAG, "Placing surface #" + i + " " + w.mSurface
9792 + ": new=" + w.mShownFrame + ", old="
9793 + w.mLastShownFrame);
9794
9795 boolean resize;
9796 int width, height;
9797 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9798 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9799 w.mLastRequestedHeight != w.mRequestedHeight;
9800 // for a scaled surface, we just want to use
9801 // the requested size.
9802 width = w.mRequestedWidth;
9803 height = w.mRequestedHeight;
9804 w.mLastRequestedWidth = width;
9805 w.mLastRequestedHeight = height;
9806 w.mLastShownFrame.set(w.mShownFrame);
9807 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009808 if (SHOW_TRANSACTIONS) Log.i(
9809 TAG, " SURFACE " + w.mSurface
9810 + ": POS " + w.mShownFrame.left
9811 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009812 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9813 } catch (RuntimeException e) {
9814 Log.w(TAG, "Error positioning surface in " + w, e);
9815 if (!recoveringMemory) {
9816 reclaimSomeSurfaceMemoryLocked(w, "position");
9817 }
9818 }
9819 } else {
9820 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9821 width = w.mShownFrame.width();
9822 height = w.mShownFrame.height();
9823 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009824 }
9825
9826 if (resize) {
9827 if (width < 1) width = 1;
9828 if (height < 1) height = 1;
9829 if (w.mSurface != null) {
9830 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009831 if (SHOW_TRANSACTIONS) Log.i(
9832 TAG, " SURFACE " + w.mSurface + ": POS "
9833 + w.mShownFrame.left + ","
9834 + w.mShownFrame.top + " SIZE "
9835 + w.mShownFrame.width() + "x"
9836 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009837 w.mSurface.setSize(width, height);
9838 w.mSurface.setPosition(w.mShownFrame.left,
9839 w.mShownFrame.top);
9840 } catch (RuntimeException e) {
9841 // If something goes wrong with the surface (such
9842 // as running out of memory), don't take down the
9843 // entire system.
9844 Log.e(TAG, "Failure updating surface of " + w
9845 + "size=(" + width + "x" + height
9846 + "), pos=(" + w.mShownFrame.left
9847 + "," + w.mShownFrame.top + ")", e);
9848 if (!recoveringMemory) {
9849 reclaimSomeSurfaceMemoryLocked(w, "size");
9850 }
9851 }
9852 }
9853 }
9854 if (!w.mAppFreezing) {
9855 w.mContentInsetsChanged =
9856 !w.mLastContentInsets.equals(w.mContentInsets);
9857 w.mVisibleInsetsChanged =
9858 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009859 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009860 || w.mContentInsetsChanged
9861 || w.mVisibleInsetsChanged) {
9862 w.mLastFrame.set(w.mFrame);
9863 w.mLastContentInsets.set(w.mContentInsets);
9864 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009865 // If the screen is currently frozen, then keep
9866 // it frozen until this window draws at its new
9867 // orientation.
9868 if (mDisplayFrozen) {
9869 if (DEBUG_ORIENTATION) Log.v(TAG,
9870 "Resizing while display frozen: " + w);
9871 w.mOrientationChanging = true;
9872 if (mWindowsFreezingScreen) {
9873 mWindowsFreezingScreen = true;
9874 // XXX should probably keep timeout from
9875 // when we first froze the display.
9876 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9877 mH.sendMessageDelayed(mH.obtainMessage(
9878 H.WINDOW_FREEZE_TIMEOUT), 2000);
9879 }
9880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009881 // If the orientation is changing, then we need to
9882 // hold off on unfreezing the display until this
9883 // window has been redrawn; to do that, we need
9884 // to go through the process of getting informed
9885 // by the application when it has finished drawing.
9886 if (w.mOrientationChanging) {
9887 if (DEBUG_ORIENTATION) Log.v(TAG,
9888 "Orientation start waiting for draw in "
9889 + w + ", surface " + w.mSurface);
9890 w.mDrawPending = true;
9891 w.mCommitDrawPending = false;
9892 w.mReadyToShow = false;
9893 if (w.mAppToken != null) {
9894 w.mAppToken.allDrawn = false;
9895 }
9896 }
Romain Guy06882f82009-06-10 13:36:04 -07009897 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009898 "Resizing window " + w + " to " + w.mFrame);
9899 mResizingWindows.add(w);
9900 } else if (w.mOrientationChanging) {
9901 if (!w.mDrawPending && !w.mCommitDrawPending) {
9902 if (DEBUG_ORIENTATION) Log.v(TAG,
9903 "Orientation not waiting for draw in "
9904 + w + ", surface " + w.mSurface);
9905 w.mOrientationChanging = false;
9906 }
9907 }
9908 }
9909
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009910 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 if (!w.mLastHidden) {
9912 //dump();
9913 w.mLastHidden = true;
9914 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009915 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009916 if (w.mSurface != null) {
9917 try {
9918 w.mSurface.hide();
9919 } catch (RuntimeException e) {
9920 Log.w(TAG, "Exception hiding surface in " + w);
9921 }
9922 }
9923 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9924 }
9925 // If we are waiting for this window to handle an
9926 // orientation change, well, it is hidden, so
9927 // doesn't really matter. Note that this does
9928 // introduce a potential glitch if the window
9929 // becomes unhidden before it has drawn for the
9930 // new orientation.
9931 if (w.mOrientationChanging) {
9932 w.mOrientationChanging = false;
9933 if (DEBUG_ORIENTATION) Log.v(TAG,
9934 "Orientation change skips hidden " + w);
9935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 } else if (w.mLastLayer != w.mAnimLayer
9937 || w.mLastAlpha != w.mShownAlpha
9938 || w.mLastDsDx != w.mDsDx
9939 || w.mLastDtDx != w.mDtDx
9940 || w.mLastDsDy != w.mDsDy
9941 || w.mLastDtDy != w.mDtDy
9942 || w.mLastHScale != w.mHScale
9943 || w.mLastVScale != w.mVScale
9944 || w.mLastHidden) {
9945 displayed = true;
9946 w.mLastAlpha = w.mShownAlpha;
9947 w.mLastLayer = w.mAnimLayer;
9948 w.mLastDsDx = w.mDsDx;
9949 w.mLastDtDx = w.mDtDx;
9950 w.mLastDsDy = w.mDsDy;
9951 w.mLastDtDy = w.mDtDy;
9952 w.mLastHScale = w.mHScale;
9953 w.mLastVScale = w.mVScale;
9954 if (SHOW_TRANSACTIONS) Log.i(
9955 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009956 + w.mShownAlpha + " layer=" + w.mAnimLayer
9957 + " matrix=[" + (w.mDsDx*w.mHScale)
9958 + "," + (w.mDtDx*w.mVScale)
9959 + "][" + (w.mDsDy*w.mHScale)
9960 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009961 if (w.mSurface != null) {
9962 try {
9963 w.mSurface.setAlpha(w.mShownAlpha);
9964 w.mSurface.setLayer(w.mAnimLayer);
9965 w.mSurface.setMatrix(
9966 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9967 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9968 } catch (RuntimeException e) {
9969 Log.w(TAG, "Error updating surface in " + w, e);
9970 if (!recoveringMemory) {
9971 reclaimSomeSurfaceMemoryLocked(w, "update");
9972 }
9973 }
9974 }
9975
9976 if (w.mLastHidden && !w.mDrawPending
9977 && !w.mCommitDrawPending
9978 && !w.mReadyToShow) {
9979 if (SHOW_TRANSACTIONS) Log.i(
9980 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9981 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9982 + " during relayout");
9983 if (showSurfaceRobustlyLocked(w)) {
9984 w.mHasDrawn = true;
9985 w.mLastHidden = false;
9986 } else {
9987 w.mOrientationChanging = false;
9988 }
9989 }
9990 if (w.mSurface != null) {
9991 w.mToken.hasVisible = true;
9992 }
9993 } else {
9994 displayed = true;
9995 }
9996
9997 if (displayed) {
9998 if (!covered) {
9999 if (attrs.width == LayoutParams.FILL_PARENT
10000 && attrs.height == LayoutParams.FILL_PARENT) {
10001 covered = true;
10002 }
10003 }
10004 if (w.mOrientationChanging) {
10005 if (w.mDrawPending || w.mCommitDrawPending) {
10006 orientationChangeComplete = false;
10007 if (DEBUG_ORIENTATION) Log.v(TAG,
10008 "Orientation continue waiting for draw in " + w);
10009 } else {
10010 w.mOrientationChanging = false;
10011 if (DEBUG_ORIENTATION) Log.v(TAG,
10012 "Orientation change complete in " + w);
10013 }
10014 }
10015 w.mToken.hasVisible = true;
10016 }
10017 } else if (w.mOrientationChanging) {
10018 if (DEBUG_ORIENTATION) Log.v(TAG,
10019 "Orientation change skips hidden " + w);
10020 w.mOrientationChanging = false;
10021 }
10022
10023 final boolean canBeSeen = w.isDisplayedLw();
10024
10025 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10026 focusDisplayed = true;
10027 }
10028
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010029 final boolean obscuredChanged = w.mObscured != obscured;
10030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010031 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010032 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010033 if (w.mSurface != null) {
10034 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10035 holdScreen = w.mSession;
10036 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010037 if (!syswin && w.mAttrs.screenBrightness >= 0
10038 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010039 screenBrightness = w.mAttrs.screenBrightness;
10040 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010041 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10042 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10043 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10044 syswin = true;
10045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010046 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010047
Dianne Hackborn25994b42009-09-04 14:21:19 -070010048 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10049 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010050 // This window completely covers everything behind it,
10051 // so we want to leave all of them as unblurred (for
10052 // performance reasons).
10053 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010054 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
10055 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010056 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010057 obscured = true;
10058 if (mBackgroundFillerSurface == null) {
10059 try {
10060 mBackgroundFillerSurface = new Surface(mFxSession, 0,
10061 0, dw, dh,
10062 PixelFormat.OPAQUE,
10063 Surface.FX_SURFACE_NORMAL);
10064 } catch (Exception e) {
10065 Log.e(TAG, "Exception creating filler surface", e);
10066 }
10067 }
10068 try {
10069 mBackgroundFillerSurface.setPosition(0, 0);
10070 mBackgroundFillerSurface.setSize(dw, dh);
10071 // Using the same layer as Dim because they will never be shown at the
10072 // same time.
10073 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10074 mBackgroundFillerSurface.show();
10075 } catch (RuntimeException e) {
10076 Log.e(TAG, "Exception showing filler surface");
10077 }
10078 backgroundFillerShown = true;
10079 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010080 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10082 if (localLOGV) Log.v(TAG, "Win " + w
10083 + ": blurring=" + blurring
10084 + " obscured=" + obscured
10085 + " displayed=" + displayed);
10086 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10087 if (!dimming) {
10088 //Log.i(TAG, "DIM BEHIND: " + w);
10089 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010090 if (mDimAnimator == null) {
10091 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010092 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010093 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010094 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010095 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010096 }
10097 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10098 if (!blurring) {
10099 //Log.i(TAG, "BLUR BEHIND: " + w);
10100 blurring = true;
10101 mBlurShown = true;
10102 if (mBlurSurface == null) {
10103 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10104 + mBlurSurface + ": CREATE");
10105 try {
Romain Guy06882f82009-06-10 13:36:04 -070010106 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 -1, 16, 16,
10108 PixelFormat.OPAQUE,
10109 Surface.FX_SURFACE_BLUR);
10110 } catch (Exception e) {
10111 Log.e(TAG, "Exception creating Blur surface", e);
10112 }
10113 }
10114 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10115 + mBlurSurface + ": SHOW pos=(0,0) (" +
10116 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10117 if (mBlurSurface != null) {
10118 mBlurSurface.setPosition(0, 0);
10119 mBlurSurface.setSize(dw, dh);
10120 try {
10121 mBlurSurface.show();
10122 } catch (RuntimeException e) {
10123 Log.w(TAG, "Failure showing blur surface", e);
10124 }
10125 }
10126 }
10127 mBlurSurface.setLayer(w.mAnimLayer-2);
10128 }
10129 }
10130 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010131
10132 if (obscuredChanged && mWallpaperTarget == w) {
10133 // This is the wallpaper target and its obscured state
10134 // changed... make sure the current wallaper's visibility
10135 // has been updated accordingly.
10136 updateWallpaperVisibilityLocked();
10137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010138 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010139
10140 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10141 mBackgroundFillerShown = false;
10142 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10143 try {
10144 mBackgroundFillerSurface.hide();
10145 } catch (RuntimeException e) {
10146 Log.e(TAG, "Exception hiding filler surface", e);
10147 }
10148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010149
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010150 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10151 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 }
Romain Guy06882f82009-06-10 13:36:04 -070010153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010154 if (!blurring && mBlurShown) {
10155 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10156 + ": HIDE");
10157 try {
10158 mBlurSurface.hide();
10159 } catch (IllegalArgumentException e) {
10160 Log.w(TAG, "Illegal argument exception hiding blur surface");
10161 }
10162 mBlurShown = false;
10163 }
10164
10165 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10166 } catch (RuntimeException e) {
10167 Log.e(TAG, "Unhandled exception in Window Manager", e);
10168 }
10169
10170 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010172 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10173 "With display frozen, orientationChangeComplete="
10174 + orientationChangeComplete);
10175 if (orientationChangeComplete) {
10176 if (mWindowsFreezingScreen) {
10177 mWindowsFreezingScreen = false;
10178 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10179 }
10180 if (mAppsFreezingScreen == 0) {
10181 stopFreezingDisplayLocked();
10182 }
10183 }
Romain Guy06882f82009-06-10 13:36:04 -070010184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010185 i = mResizingWindows.size();
10186 if (i > 0) {
10187 do {
10188 i--;
10189 WindowState win = mResizingWindows.get(i);
10190 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010191 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10192 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010193 win.mClient.resized(win.mFrame.width(),
10194 win.mFrame.height(), win.mLastContentInsets,
10195 win.mLastVisibleInsets, win.mDrawPending);
10196 win.mContentInsetsChanged = false;
10197 win.mVisibleInsetsChanged = false;
10198 } catch (RemoteException e) {
10199 win.mOrientationChanging = false;
10200 }
10201 } while (i > 0);
10202 mResizingWindows.clear();
10203 }
Romain Guy06882f82009-06-10 13:36:04 -070010204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010205 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010206 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 i = mDestroySurface.size();
10208 if (i > 0) {
10209 do {
10210 i--;
10211 WindowState win = mDestroySurface.get(i);
10212 win.mDestroying = false;
10213 if (mInputMethodWindow == win) {
10214 mInputMethodWindow = null;
10215 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010216 if (win == mWallpaperTarget) {
10217 wallpaperDestroyed = true;
10218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219 win.destroySurfaceLocked();
10220 } while (i > 0);
10221 mDestroySurface.clear();
10222 }
10223
10224 // Time to remove any exiting tokens?
10225 for (i=mExitingTokens.size()-1; i>=0; i--) {
10226 WindowToken token = mExitingTokens.get(i);
10227 if (!token.hasVisible) {
10228 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010229 if (token.windowType == TYPE_WALLPAPER) {
10230 mWallpaperTokens.remove(token);
10231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010232 }
10233 }
10234
10235 // Time to remove any exiting applications?
10236 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10237 AppWindowToken token = mExitingAppTokens.get(i);
10238 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010239 // Make sure there is no animation running on this token,
10240 // so any windows associated with it will be removed as
10241 // soon as their animations are complete
10242 token.animation = null;
10243 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010244 mAppTokens.remove(token);
10245 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010246 if (mLastEnterAnimToken == token) {
10247 mLastEnterAnimToken = null;
10248 mLastEnterAnimParams = null;
10249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010250 }
10251 }
10252
Dianne Hackborna8f60182009-09-01 19:01:50 -070010253 boolean needRelayout = false;
10254
10255 if (!animating && mAppTransitionRunning) {
10256 // We have finished the animation of an app transition. To do
10257 // this, we have delayed a lot of operations like showing and
10258 // hiding apps, moving apps in Z-order, etc. The app token list
10259 // reflects the correct Z-order, but the window list may now
10260 // be out of sync with it. So here we will just rebuild the
10261 // entire app window list. Fun!
10262 mAppTransitionRunning = false;
10263 needRelayout = true;
10264 rebuildAppWindowListLocked();
10265 // Clear information about apps that were moving.
10266 mToBottomApps.clear();
10267 }
10268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010269 if (focusDisplayed) {
10270 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10271 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010272 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010273 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010274 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010275 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010276 requestAnimationLocked(0);
10277 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010278 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10279 }
10280 mQueue.setHoldScreenLocked(holdScreen != null);
10281 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10282 mPowerManager.setScreenBrightnessOverride(-1);
10283 } else {
10284 mPowerManager.setScreenBrightnessOverride((int)
10285 (screenBrightness * Power.BRIGHTNESS_ON));
10286 }
10287 if (holdScreen != mHoldingScreenOn) {
10288 mHoldingScreenOn = holdScreen;
10289 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10290 mH.sendMessage(m);
10291 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010292
10293 if (mTurnOnScreen) {
10294 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10295 LocalPowerManager.BUTTON_EVENT, true);
10296 mTurnOnScreen = false;
10297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 }
10299
10300 void requestAnimationLocked(long delay) {
10301 if (!mAnimationPending) {
10302 mAnimationPending = true;
10303 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10304 }
10305 }
Romain Guy06882f82009-06-10 13:36:04 -070010306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 /**
10308 * Have the surface flinger show a surface, robustly dealing with
10309 * error conditions. In particular, if there is not enough memory
10310 * to show the surface, then we will try to get rid of other surfaces
10311 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010313 * @return Returns true if the surface was successfully shown.
10314 */
10315 boolean showSurfaceRobustlyLocked(WindowState win) {
10316 try {
10317 if (win.mSurface != null) {
10318 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010319 if (win.mTurnOnScreen) {
10320 win.mTurnOnScreen = false;
10321 mTurnOnScreen = true;
10322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 }
10324 return true;
10325 } catch (RuntimeException e) {
10326 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10327 }
Romain Guy06882f82009-06-10 13:36:04 -070010328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010329 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010331 return false;
10332 }
Romain Guy06882f82009-06-10 13:36:04 -070010333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010334 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10335 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010337 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
10338 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010340 if (mForceRemoves == null) {
10341 mForceRemoves = new ArrayList<WindowState>();
10342 }
Romain Guy06882f82009-06-10 13:36:04 -070010343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344 long callingIdentity = Binder.clearCallingIdentity();
10345 try {
10346 // There was some problem... first, do a sanity check of the
10347 // window list to make sure we haven't left any dangling surfaces
10348 // around.
10349 int N = mWindows.size();
10350 boolean leakedSurface = false;
10351 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10352 for (int i=0; i<N; i++) {
10353 WindowState ws = (WindowState)mWindows.get(i);
10354 if (ws.mSurface != null) {
10355 if (!mSessions.contains(ws.mSession)) {
10356 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10357 + ws + " surface=" + ws.mSurface
10358 + " token=" + win.mToken
10359 + " pid=" + ws.mSession.mPid
10360 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010361 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010362 ws.mSurface = null;
10363 mForceRemoves.add(ws);
10364 i--;
10365 N--;
10366 leakedSurface = true;
10367 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10368 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10369 + ws + " surface=" + ws.mSurface
10370 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010371 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 ws.mSurface = null;
10373 leakedSurface = true;
10374 }
10375 }
10376 }
Romain Guy06882f82009-06-10 13:36:04 -070010377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010378 boolean killedApps = false;
10379 if (!leakedSurface) {
10380 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10381 SparseIntArray pidCandidates = new SparseIntArray();
10382 for (int i=0; i<N; i++) {
10383 WindowState ws = (WindowState)mWindows.get(i);
10384 if (ws.mSurface != null) {
10385 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10386 }
10387 }
10388 if (pidCandidates.size() > 0) {
10389 int[] pids = new int[pidCandidates.size()];
10390 for (int i=0; i<pids.length; i++) {
10391 pids[i] = pidCandidates.keyAt(i);
10392 }
10393 try {
10394 if (mActivityManager.killPidsForMemory(pids)) {
10395 killedApps = true;
10396 }
10397 } catch (RemoteException e) {
10398 }
10399 }
10400 }
Romain Guy06882f82009-06-10 13:36:04 -070010401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010402 if (leakedSurface || killedApps) {
10403 // We managed to reclaim some memory, so get rid of the trouble
10404 // surface and ask the app to request another one.
10405 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10406 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010407 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010408 win.mSurface = null;
10409 }
Romain Guy06882f82009-06-10 13:36:04 -070010410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010411 try {
10412 win.mClient.dispatchGetNewSurface();
10413 } catch (RemoteException e) {
10414 }
10415 }
10416 } finally {
10417 Binder.restoreCallingIdentity(callingIdentity);
10418 }
10419 }
Romain Guy06882f82009-06-10 13:36:04 -070010420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010421 private boolean updateFocusedWindowLocked(int mode) {
10422 WindowState newFocus = computeFocusedWindowLocked();
10423 if (mCurrentFocus != newFocus) {
10424 // This check makes sure that we don't already have the focus
10425 // change message pending.
10426 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10427 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10428 if (localLOGV) Log.v(
10429 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10430 final WindowState oldFocus = mCurrentFocus;
10431 mCurrentFocus = newFocus;
10432 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 final WindowState imWindow = mInputMethodWindow;
10435 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010436 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010437 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010438 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10439 mLayoutNeeded = true;
10440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010441 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10442 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010443 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10444 // Client will do the layout, but we need to assign layers
10445 // for handleNewWindowLocked() below.
10446 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010447 }
10448 }
Romain Guy06882f82009-06-10 13:36:04 -070010449
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010450 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10451 mKeyWaiter.handleNewWindowLocked(newFocus);
10452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 return true;
10454 }
10455 return false;
10456 }
10457
10458 private WindowState computeFocusedWindowLocked() {
10459 WindowState result = null;
10460 WindowState win;
10461
10462 int i = mWindows.size() - 1;
10463 int nextAppIndex = mAppTokens.size()-1;
10464 WindowToken nextApp = nextAppIndex >= 0
10465 ? mAppTokens.get(nextAppIndex) : null;
10466
10467 while (i >= 0) {
10468 win = (WindowState)mWindows.get(i);
10469
10470 if (localLOGV || DEBUG_FOCUS) Log.v(
10471 TAG, "Looking for focus: " + i
10472 + " = " + win
10473 + ", flags=" + win.mAttrs.flags
10474 + ", canReceive=" + win.canReceiveKeys());
10475
10476 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010478 // If this window's application has been removed, just skip it.
10479 if (thisApp != null && thisApp.removed) {
10480 i--;
10481 continue;
10482 }
Romain Guy06882f82009-06-10 13:36:04 -070010483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010484 // If there is a focused app, don't allow focus to go to any
10485 // windows below it. If this is an application window, step
10486 // through the app tokens until we find its app.
10487 if (thisApp != null && nextApp != null && thisApp != nextApp
10488 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10489 int origAppIndex = nextAppIndex;
10490 while (nextAppIndex > 0) {
10491 if (nextApp == mFocusedApp) {
10492 // Whoops, we are below the focused app... no focus
10493 // for you!
10494 if (localLOGV || DEBUG_FOCUS) Log.v(
10495 TAG, "Reached focused app: " + mFocusedApp);
10496 return null;
10497 }
10498 nextAppIndex--;
10499 nextApp = mAppTokens.get(nextAppIndex);
10500 if (nextApp == thisApp) {
10501 break;
10502 }
10503 }
10504 if (thisApp != nextApp) {
10505 // Uh oh, the app token doesn't exist! This shouldn't
10506 // happen, but if it does we can get totally hosed...
10507 // so restart at the original app.
10508 nextAppIndex = origAppIndex;
10509 nextApp = mAppTokens.get(nextAppIndex);
10510 }
10511 }
10512
10513 // Dispatch to this window if it is wants key events.
10514 if (win.canReceiveKeys()) {
10515 if (DEBUG_FOCUS) Log.v(
10516 TAG, "Found focus @ " + i + " = " + win);
10517 result = win;
10518 break;
10519 }
10520
10521 i--;
10522 }
10523
10524 return result;
10525 }
10526
10527 private void startFreezingDisplayLocked() {
10528 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010529 // Freezing the display also suspends key event delivery, to
10530 // keep events from going astray while the display is reconfigured.
10531 // If someone has changed orientation again while the screen is
10532 // still frozen, the events will continue to be blocked while the
10533 // successive orientation change is processed. To prevent spurious
10534 // ANRs, we reset the event dispatch timeout in this case.
10535 synchronized (mKeyWaiter) {
10536 mKeyWaiter.mWasFrozen = true;
10537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010538 return;
10539 }
Romain Guy06882f82009-06-10 13:36:04 -070010540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 long now = SystemClock.uptimeMillis();
10544 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10545 if (mFreezeGcPending != 0) {
10546 if (now > (mFreezeGcPending+1000)) {
10547 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10548 mH.removeMessages(H.FORCE_GC);
10549 Runtime.getRuntime().gc();
10550 mFreezeGcPending = now;
10551 }
10552 } else {
10553 mFreezeGcPending = now;
10554 }
Romain Guy06882f82009-06-10 13:36:04 -070010555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010557 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10558 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010559 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 mAppTransitionReady = true;
10561 }
Romain Guy06882f82009-06-10 13:36:04 -070010562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010563 if (PROFILE_ORIENTATION) {
10564 File file = new File("/data/system/frozen");
10565 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10566 }
10567 Surface.freezeDisplay(0);
10568 }
Romain Guy06882f82009-06-10 13:36:04 -070010569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 private void stopFreezingDisplayLocked() {
10571 if (!mDisplayFrozen) {
10572 return;
10573 }
Romain Guy06882f82009-06-10 13:36:04 -070010574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010575 mDisplayFrozen = false;
10576 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10577 if (PROFILE_ORIENTATION) {
10578 Debug.stopMethodTracing();
10579 }
10580 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010581
Chris Tate2ad63a92009-03-25 17:36:48 -070010582 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10583 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010584 synchronized (mKeyWaiter) {
10585 mKeyWaiter.mWasFrozen = true;
10586 mKeyWaiter.notifyAll();
10587 }
10588
10589 // A little kludge: a lot could have happened while the
10590 // display was frozen, so now that we are coming back we
10591 // do a gc so that any remote references the system
10592 // processes holds on others can be released if they are
10593 // no longer needed.
10594 mH.removeMessages(H.FORCE_GC);
10595 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10596 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010598 mScreenFrozenLock.release();
10599 }
Romain Guy06882f82009-06-10 13:36:04 -070010600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010601 @Override
10602 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10603 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10604 != PackageManager.PERMISSION_GRANTED) {
10605 pw.println("Permission Denial: can't dump WindowManager from from pid="
10606 + Binder.getCallingPid()
10607 + ", uid=" + Binder.getCallingUid());
10608 return;
10609 }
Romain Guy06882f82009-06-10 13:36:04 -070010610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010611 synchronized(mWindowMap) {
10612 pw.println("Current Window Manager state:");
10613 for (int i=mWindows.size()-1; i>=0; i--) {
10614 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010615 pw.print(" Window #"); pw.print(i); pw.print(' ');
10616 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010617 w.dump(pw, " ");
10618 }
10619 if (mInputMethodDialogs.size() > 0) {
10620 pw.println(" ");
10621 pw.println(" Input method dialogs:");
10622 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10623 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010624 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010625 }
10626 }
10627 if (mPendingRemove.size() > 0) {
10628 pw.println(" ");
10629 pw.println(" Remove pending for:");
10630 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10631 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010632 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10633 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010634 w.dump(pw, " ");
10635 }
10636 }
10637 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10638 pw.println(" ");
10639 pw.println(" Windows force removing:");
10640 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10641 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010642 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10643 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 w.dump(pw, " ");
10645 }
10646 }
10647 if (mDestroySurface.size() > 0) {
10648 pw.println(" ");
10649 pw.println(" Windows waiting to destroy their surface:");
10650 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10651 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010652 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10653 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010654 w.dump(pw, " ");
10655 }
10656 }
10657 if (mLosingFocus.size() > 0) {
10658 pw.println(" ");
10659 pw.println(" Windows losing focus:");
10660 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10661 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010662 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10663 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664 w.dump(pw, " ");
10665 }
10666 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010667 if (mResizingWindows.size() > 0) {
10668 pw.println(" ");
10669 pw.println(" Windows waiting to resize:");
10670 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10671 WindowState w = mResizingWindows.get(i);
10672 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10673 pw.print(w); pw.println(":");
10674 w.dump(pw, " ");
10675 }
10676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010677 if (mSessions.size() > 0) {
10678 pw.println(" ");
10679 pw.println(" All active sessions:");
10680 Iterator<Session> it = mSessions.iterator();
10681 while (it.hasNext()) {
10682 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010683 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010684 s.dump(pw, " ");
10685 }
10686 }
10687 if (mTokenMap.size() > 0) {
10688 pw.println(" ");
10689 pw.println(" All tokens:");
10690 Iterator<WindowToken> it = mTokenMap.values().iterator();
10691 while (it.hasNext()) {
10692 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010693 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010694 token.dump(pw, " ");
10695 }
10696 }
10697 if (mTokenList.size() > 0) {
10698 pw.println(" ");
10699 pw.println(" Window token list:");
10700 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010701 pw.print(" #"); pw.print(i); pw.print(": ");
10702 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010703 }
10704 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010705 if (mWallpaperTokens.size() > 0) {
10706 pw.println(" ");
10707 pw.println(" Wallpaper tokens:");
10708 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10709 WindowToken token = mWallpaperTokens.get(i);
10710 pw.print(" Wallpaper #"); pw.print(i);
10711 pw.print(' '); pw.print(token); pw.println(':');
10712 token.dump(pw, " ");
10713 }
10714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 if (mAppTokens.size() > 0) {
10716 pw.println(" ");
10717 pw.println(" Application tokens in Z order:");
10718 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010719 pw.print(" App #"); pw.print(i); pw.print(": ");
10720 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721 }
10722 }
10723 if (mFinishedStarting.size() > 0) {
10724 pw.println(" ");
10725 pw.println(" Finishing start of application tokens:");
10726 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10727 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010728 pw.print(" Finished Starting #"); pw.print(i);
10729 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010730 token.dump(pw, " ");
10731 }
10732 }
10733 if (mExitingTokens.size() > 0) {
10734 pw.println(" ");
10735 pw.println(" Exiting tokens:");
10736 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10737 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010738 pw.print(" Exiting #"); pw.print(i);
10739 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740 token.dump(pw, " ");
10741 }
10742 }
10743 if (mExitingAppTokens.size() > 0) {
10744 pw.println(" ");
10745 pw.println(" Exiting application tokens:");
10746 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10747 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010748 pw.print(" Exiting App #"); pw.print(i);
10749 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 token.dump(pw, " ");
10751 }
10752 }
10753 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010754 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10755 pw.print(" mLastFocus="); pw.println(mLastFocus);
10756 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10757 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10758 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010759 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010760 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10761 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10762 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10763 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010764 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10765 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10766 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10767 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10768 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010769 if (mDimAnimator != null) {
10770 mDimAnimator.printTo(pw);
10771 } else {
10772 pw.print( " no DimAnimator ");
10773 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010774 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010775 pw.print(mInputMethodAnimLayerAdjustment);
10776 pw.print(" mWallpaperAnimLayerAdjustment=");
10777 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010778 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10779 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010780 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10781 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10782 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10783 pw.print(" mRotation="); pw.print(mRotation);
10784 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10785 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10786 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10787 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10788 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10789 pw.print(" mNextAppTransition=0x");
10790 pw.print(Integer.toHexString(mNextAppTransition));
10791 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010792 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010793 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010794 if (mNextAppTransitionPackage != null) {
10795 pw.print(" mNextAppTransitionPackage=");
10796 pw.print(mNextAppTransitionPackage);
10797 pw.print(", mNextAppTransitionEnter=0x");
10798 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10799 pw.print(", mNextAppTransitionExit=0x");
10800 pw.print(Integer.toHexString(mNextAppTransitionExit));
10801 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010802 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10803 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010804 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10805 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10806 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10807 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010808 if (mOpeningApps.size() > 0) {
10809 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10810 }
10811 if (mClosingApps.size() > 0) {
10812 pw.print(" mClosingApps="); pw.println(mClosingApps);
10813 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010814 if (mToTopApps.size() > 0) {
10815 pw.print(" mToTopApps="); pw.println(mToTopApps);
10816 }
10817 if (mToBottomApps.size() > 0) {
10818 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10819 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010820 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10821 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010822 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010823 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10824 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10825 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10826 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10827 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10828 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010829 }
10830 }
10831
10832 public void monitor() {
10833 synchronized (mWindowMap) { }
10834 synchronized (mKeyguardDisabled) { }
10835 synchronized (mKeyWaiter) { }
10836 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010837
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010838 public void virtualKeyFeedback(KeyEvent event) {
10839 mPolicy.keyFeedbackFromInput(event);
10840 }
10841
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010842 /**
10843 * DimAnimator class that controls the dim animation. This holds the surface and
10844 * all state used for dim animation.
10845 */
10846 private static class DimAnimator {
10847 Surface mDimSurface;
10848 boolean mDimShown = false;
10849 float mDimCurrentAlpha;
10850 float mDimTargetAlpha;
10851 float mDimDeltaPerMs;
10852 long mLastDimAnimTime;
10853
10854 DimAnimator (SurfaceSession session) {
10855 if (mDimSurface == null) {
10856 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10857 + mDimSurface + ": CREATE");
10858 try {
10859 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10860 Surface.FX_SURFACE_DIM);
10861 } catch (Exception e) {
10862 Log.e(TAG, "Exception creating Dim surface", e);
10863 }
10864 }
10865 }
10866
10867 /**
10868 * Show the dim surface.
10869 */
10870 void show(int dw, int dh) {
10871 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10872 dw + "x" + dh + ")");
10873 mDimShown = true;
10874 try {
10875 mDimSurface.setPosition(0, 0);
10876 mDimSurface.setSize(dw, dh);
10877 mDimSurface.show();
10878 } catch (RuntimeException e) {
10879 Log.w(TAG, "Failure showing dim surface", e);
10880 }
10881 }
10882
10883 /**
10884 * Set's the dim surface's layer and update dim parameters that will be used in
10885 * {@link updateSurface} after all windows are examined.
10886 */
10887 void updateParameters(WindowState w, long currentTime) {
10888 mDimSurface.setLayer(w.mAnimLayer-1);
10889
10890 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010891 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10892 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010893 if (mDimTargetAlpha != target) {
10894 // If the desired dim level has changed, then
10895 // start an animation to it.
10896 mLastDimAnimTime = currentTime;
10897 long duration = (w.mAnimating && w.mAnimation != null)
10898 ? w.mAnimation.computeDurationHint()
10899 : DEFAULT_DIM_DURATION;
10900 if (target > mDimTargetAlpha) {
10901 // This is happening behind the activity UI,
10902 // so we can make it run a little longer to
10903 // give a stronger impression without disrupting
10904 // the user.
10905 duration *= DIM_DURATION_MULTIPLIER;
10906 }
10907 if (duration < 1) {
10908 // Don't divide by zero
10909 duration = 1;
10910 }
10911 mDimTargetAlpha = target;
10912 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10913 }
10914 }
10915
10916 /**
10917 * Updating the surface's alpha. Returns true if the animation continues, or returns
10918 * false when the animation is finished and the dim surface is hidden.
10919 */
10920 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10921 if (!dimming) {
10922 if (mDimTargetAlpha != 0) {
10923 mLastDimAnimTime = currentTime;
10924 mDimTargetAlpha = 0;
10925 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10926 }
10927 }
10928
10929 boolean animating = false;
10930 if (mLastDimAnimTime != 0) {
10931 mDimCurrentAlpha += mDimDeltaPerMs
10932 * (currentTime-mLastDimAnimTime);
10933 boolean more = true;
10934 if (displayFrozen) {
10935 // If the display is frozen, there is no reason to animate.
10936 more = false;
10937 } else if (mDimDeltaPerMs > 0) {
10938 if (mDimCurrentAlpha > mDimTargetAlpha) {
10939 more = false;
10940 }
10941 } else if (mDimDeltaPerMs < 0) {
10942 if (mDimCurrentAlpha < mDimTargetAlpha) {
10943 more = false;
10944 }
10945 } else {
10946 more = false;
10947 }
10948
10949 // Do we need to continue animating?
10950 if (more) {
10951 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10952 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10953 mLastDimAnimTime = currentTime;
10954 mDimSurface.setAlpha(mDimCurrentAlpha);
10955 animating = true;
10956 } else {
10957 mDimCurrentAlpha = mDimTargetAlpha;
10958 mLastDimAnimTime = 0;
10959 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10960 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10961 mDimSurface.setAlpha(mDimCurrentAlpha);
10962 if (!dimming) {
10963 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10964 + ": HIDE");
10965 try {
10966 mDimSurface.hide();
10967 } catch (RuntimeException e) {
10968 Log.w(TAG, "Illegal argument exception hiding dim surface");
10969 }
10970 mDimShown = false;
10971 }
10972 }
10973 }
10974 return animating;
10975 }
10976
10977 public void printTo(PrintWriter pw) {
10978 pw.print(" mDimShown="); pw.print(mDimShown);
10979 pw.print(" current="); pw.print(mDimCurrentAlpha);
10980 pw.print(" target="); pw.print(mDimTargetAlpha);
10981 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10982 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10983 }
10984 }
10985
10986 /**
10987 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10988 * This is used for opening/closing transition for apps in compatible mode.
10989 */
10990 private static class FadeInOutAnimation extends Animation {
10991 int mWidth;
10992 boolean mFadeIn;
10993
10994 public FadeInOutAnimation(boolean fadeIn) {
10995 setInterpolator(new AccelerateInterpolator());
10996 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10997 mFadeIn = fadeIn;
10998 }
10999
11000 @Override
11001 protected void applyTransformation(float interpolatedTime, Transformation t) {
11002 float x = interpolatedTime;
11003 if (!mFadeIn) {
11004 x = 1.0f - x; // reverse the interpolation for fade out
11005 }
11006 if (x < 0.5) {
11007 // move the window out of the screen.
11008 t.getMatrix().setTranslate(mWidth, 0);
11009 } else {
11010 t.getMatrix().setTranslate(0, 0);// show
11011 t.setAlpha((x - 0.5f) * 2);
11012 }
11013 }
11014
11015 @Override
11016 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11017 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11018 mWidth = width;
11019 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011020
11021 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011022 public int getZAdjustment() {
11023 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011024 }
11025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011026}