blob: 887c46dfd9102a606206e53dae2e640cefb29708 [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
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004195 public int getTrackballScancodeState(int sw) {
4196 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4197 "getTrackballScancodeState()")) {
4198 throw new SecurityException("Requires READ_INPUT_STATE permission");
4199 }
4200 return mQueue.getTrackballScancodeState(sw);
4201 }
4202
4203 public int getDPadScancodeState(int sw) {
4204 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4205 "getDPadScancodeState()")) {
4206 throw new SecurityException("Requires READ_INPUT_STATE permission");
4207 }
4208 return mQueue.getDPadScancodeState(sw);
4209 }
4210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 public int getKeycodeState(int sw) {
4212 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4213 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004214 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004216 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 }
Romain Guy06882f82009-06-10 13:36:04 -07004218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 public int getKeycodeStateForDevice(int devid, int sw) {
4220 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4221 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004222 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004224 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 }
Romain Guy06882f82009-06-10 13:36:04 -07004226
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004227 public int getTrackballKeycodeState(int sw) {
4228 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4229 "getTrackballKeycodeState()")) {
4230 throw new SecurityException("Requires READ_INPUT_STATE permission");
4231 }
4232 return mQueue.getTrackballKeycodeState(sw);
4233 }
4234
4235 public int getDPadKeycodeState(int sw) {
4236 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4237 "getDPadKeycodeState()")) {
4238 throw new SecurityException("Requires READ_INPUT_STATE permission");
4239 }
4240 return mQueue.getDPadKeycodeState(sw);
4241 }
4242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4244 return KeyInputQueue.hasKeys(keycodes, keyExists);
4245 }
Romain Guy06882f82009-06-10 13:36:04 -07004246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 public void enableScreenAfterBoot() {
4248 synchronized(mWindowMap) {
4249 if (mSystemBooted) {
4250 return;
4251 }
4252 mSystemBooted = true;
4253 }
Romain Guy06882f82009-06-10 13:36:04 -07004254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 performEnableScreen();
4256 }
Romain Guy06882f82009-06-10 13:36:04 -07004257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 public void enableScreenIfNeededLocked() {
4259 if (mDisplayEnabled) {
4260 return;
4261 }
4262 if (!mSystemBooted) {
4263 return;
4264 }
4265 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4266 }
Romain Guy06882f82009-06-10 13:36:04 -07004267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004268 public void performEnableScreen() {
4269 synchronized(mWindowMap) {
4270 if (mDisplayEnabled) {
4271 return;
4272 }
4273 if (!mSystemBooted) {
4274 return;
4275 }
Romain Guy06882f82009-06-10 13:36:04 -07004276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 // Don't enable the screen until all existing windows
4278 // have been drawn.
4279 final int N = mWindows.size();
4280 for (int i=0; i<N; i++) {
4281 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07004282 if (w.isVisibleLw() && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 return;
4284 }
4285 }
Romain Guy06882f82009-06-10 13:36:04 -07004286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 mDisplayEnabled = true;
4288 if (false) {
4289 Log.i(TAG, "ENABLING SCREEN!");
4290 StringWriter sw = new StringWriter();
4291 PrintWriter pw = new PrintWriter(sw);
4292 this.dump(null, pw, null);
4293 Log.i(TAG, sw.toString());
4294 }
4295 try {
4296 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4297 if (surfaceFlinger != null) {
4298 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4299 Parcel data = Parcel.obtain();
4300 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4301 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4302 data, null, 0);
4303 data.recycle();
4304 }
4305 } catch (RemoteException ex) {
4306 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4307 }
4308 }
Romain Guy06882f82009-06-10 13:36:04 -07004309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004313 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4314 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 }
Romain Guy06882f82009-06-10 13:36:04 -07004316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 public void setInTouchMode(boolean mode) {
4318 synchronized(mWindowMap) {
4319 mInTouchMode = mode;
4320 }
4321 }
4322
Romain Guy06882f82009-06-10 13:36:04 -07004323 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004324 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004326 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004327 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004328 }
4329
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004330 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 }
Romain Guy06882f82009-06-10 13:36:04 -07004332
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004333 public void setRotationUnchecked(int rotation,
4334 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335 if(DEBUG_ORIENTATION) Log.v(TAG,
4336 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 long origId = Binder.clearCallingIdentity();
4339 boolean changed;
4340 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004341 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 }
Romain Guy06882f82009-06-10 13:36:04 -07004343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 if (changed) {
4345 sendNewConfiguration();
4346 synchronized(mWindowMap) {
4347 mLayoutNeeded = true;
4348 performLayoutAndPlaceSurfacesLocked();
4349 }
4350 } else if (alwaysSendConfiguration) {
4351 //update configuration ignoring orientation change
4352 sendNewConfiguration();
4353 }
Romain Guy06882f82009-06-10 13:36:04 -07004354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 Binder.restoreCallingIdentity(origId);
4356 }
Romain Guy06882f82009-06-10 13:36:04 -07004357
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004358 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 boolean changed;
4360 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4361 rotation = mRequestedRotation;
4362 } else {
4363 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004364 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 }
4366 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004367 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 mRotation, mDisplayEnabled);
4369 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4370 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004373 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 "Rotation changed to " + rotation
4375 + " from " + mRotation
4376 + " (forceApp=" + mForcedAppOrientation
4377 + ", req=" + mRequestedRotation + ")");
4378 mRotation = rotation;
4379 mWindowsFreezingScreen = true;
4380 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4381 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4382 2000);
4383 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004384 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004385 mQueue.setOrientation(rotation);
4386 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004387 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 }
4389 for (int i=mWindows.size()-1; i>=0; i--) {
4390 WindowState w = (WindowState)mWindows.get(i);
4391 if (w.mSurface != null) {
4392 w.mOrientationChanging = true;
4393 }
4394 }
4395 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4396 try {
4397 mRotationWatchers.get(i).onRotationChanged(rotation);
4398 } catch (RemoteException e) {
4399 }
4400 }
4401 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 return changed;
4404 }
Romain Guy06882f82009-06-10 13:36:04 -07004405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 public int getRotation() {
4407 return mRotation;
4408 }
4409
4410 public int watchRotation(IRotationWatcher watcher) {
4411 final IBinder watcherBinder = watcher.asBinder();
4412 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4413 public void binderDied() {
4414 synchronized (mWindowMap) {
4415 for (int i=0; i<mRotationWatchers.size(); i++) {
4416 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004417 IRotationWatcher removed = mRotationWatchers.remove(i);
4418 if (removed != null) {
4419 removed.asBinder().unlinkToDeath(this, 0);
4420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 i--;
4422 }
4423 }
4424 }
4425 }
4426 };
Romain Guy06882f82009-06-10 13:36:04 -07004427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 synchronized (mWindowMap) {
4429 try {
4430 watcher.asBinder().linkToDeath(dr, 0);
4431 mRotationWatchers.add(watcher);
4432 } catch (RemoteException e) {
4433 // Client died, no cleanup needed.
4434 }
Romain Guy06882f82009-06-10 13:36:04 -07004435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 return mRotation;
4437 }
4438 }
4439
4440 /**
4441 * Starts the view server on the specified port.
4442 *
4443 * @param port The port to listener to.
4444 *
4445 * @return True if the server was successfully started, false otherwise.
4446 *
4447 * @see com.android.server.ViewServer
4448 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4449 */
4450 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004451 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 return false;
4453 }
4454
4455 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4456 return false;
4457 }
4458
4459 if (port < 1024) {
4460 return false;
4461 }
4462
4463 if (mViewServer != null) {
4464 if (!mViewServer.isRunning()) {
4465 try {
4466 return mViewServer.start();
4467 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004468 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 }
4470 }
4471 return false;
4472 }
4473
4474 try {
4475 mViewServer = new ViewServer(this, port);
4476 return mViewServer.start();
4477 } catch (IOException e) {
4478 Log.w(TAG, "View server did not start");
4479 }
4480 return false;
4481 }
4482
Romain Guy06882f82009-06-10 13:36:04 -07004483 private boolean isSystemSecure() {
4484 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4485 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4486 }
4487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004488 /**
4489 * Stops the view server if it exists.
4490 *
4491 * @return True if the server stopped, false if it wasn't started or
4492 * couldn't be stopped.
4493 *
4494 * @see com.android.server.ViewServer
4495 */
4496 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004497 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 return false;
4499 }
4500
4501 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4502 return false;
4503 }
4504
4505 if (mViewServer != null) {
4506 return mViewServer.stop();
4507 }
4508 return false;
4509 }
4510
4511 /**
4512 * Indicates whether the view server is running.
4513 *
4514 * @return True if the server is running, false otherwise.
4515 *
4516 * @see com.android.server.ViewServer
4517 */
4518 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004519 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 return false;
4521 }
4522
4523 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4524 return false;
4525 }
4526
4527 return mViewServer != null && mViewServer.isRunning();
4528 }
4529
4530 /**
4531 * Lists all availble windows in the system. The listing is written in the
4532 * specified Socket's output stream with the following syntax:
4533 * windowHashCodeInHexadecimal windowName
4534 * Each line of the ouput represents a different window.
4535 *
4536 * @param client The remote client to send the listing to.
4537 * @return False if an error occured, true otherwise.
4538 */
4539 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004540 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 return false;
4542 }
4543
4544 boolean result = true;
4545
4546 Object[] windows;
4547 synchronized (mWindowMap) {
4548 windows = new Object[mWindows.size()];
4549 //noinspection unchecked
4550 windows = mWindows.toArray(windows);
4551 }
4552
4553 BufferedWriter out = null;
4554
4555 // Any uncaught exception will crash the system process
4556 try {
4557 OutputStream clientStream = client.getOutputStream();
4558 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4559
4560 final int count = windows.length;
4561 for (int i = 0; i < count; i++) {
4562 final WindowState w = (WindowState) windows[i];
4563 out.write(Integer.toHexString(System.identityHashCode(w)));
4564 out.write(' ');
4565 out.append(w.mAttrs.getTitle());
4566 out.write('\n');
4567 }
4568
4569 out.write("DONE.\n");
4570 out.flush();
4571 } catch (Exception e) {
4572 result = false;
4573 } finally {
4574 if (out != null) {
4575 try {
4576 out.close();
4577 } catch (IOException e) {
4578 result = false;
4579 }
4580 }
4581 }
4582
4583 return result;
4584 }
4585
4586 /**
4587 * Sends a command to a target window. The result of the command, if any, will be
4588 * written in the output stream of the specified socket.
4589 *
4590 * The parameters must follow this syntax:
4591 * windowHashcode extra
4592 *
4593 * Where XX is the length in characeters of the windowTitle.
4594 *
4595 * The first parameter is the target window. The window with the specified hashcode
4596 * will be the target. If no target can be found, nothing happens. The extra parameters
4597 * will be delivered to the target window and as parameters to the command itself.
4598 *
4599 * @param client The remote client to sent the result, if any, to.
4600 * @param command The command to execute.
4601 * @param parameters The command parameters.
4602 *
4603 * @return True if the command was successfully delivered, false otherwise. This does
4604 * not indicate whether the command itself was successful.
4605 */
4606 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004607 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 return false;
4609 }
4610
4611 boolean success = true;
4612 Parcel data = null;
4613 Parcel reply = null;
4614
4615 // Any uncaught exception will crash the system process
4616 try {
4617 // Find the hashcode of the window
4618 int index = parameters.indexOf(' ');
4619 if (index == -1) {
4620 index = parameters.length();
4621 }
4622 final String code = parameters.substring(0, index);
4623 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4624
4625 // Extract the command's parameter after the window description
4626 if (index < parameters.length()) {
4627 parameters = parameters.substring(index + 1);
4628 } else {
4629 parameters = "";
4630 }
4631
4632 final WindowManagerService.WindowState window = findWindow(hashCode);
4633 if (window == null) {
4634 return false;
4635 }
4636
4637 data = Parcel.obtain();
4638 data.writeInterfaceToken("android.view.IWindow");
4639 data.writeString(command);
4640 data.writeString(parameters);
4641 data.writeInt(1);
4642 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4643
4644 reply = Parcel.obtain();
4645
4646 final IBinder binder = window.mClient.asBinder();
4647 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4648 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4649
4650 reply.readException();
4651
4652 } catch (Exception e) {
4653 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4654 success = false;
4655 } finally {
4656 if (data != null) {
4657 data.recycle();
4658 }
4659 if (reply != null) {
4660 reply.recycle();
4661 }
4662 }
4663
4664 return success;
4665 }
4666
4667 private WindowState findWindow(int hashCode) {
4668 if (hashCode == -1) {
4669 return getFocusedWindow();
4670 }
4671
4672 synchronized (mWindowMap) {
4673 final ArrayList windows = mWindows;
4674 final int count = windows.size();
4675
4676 for (int i = 0; i < count; i++) {
4677 WindowState w = (WindowState) windows.get(i);
4678 if (System.identityHashCode(w) == hashCode) {
4679 return w;
4680 }
4681 }
4682 }
4683
4684 return null;
4685 }
4686
4687 /*
4688 * Instruct the Activity Manager to fetch the current configuration and broadcast
4689 * that to config-changed listeners if appropriate.
4690 */
4691 void sendNewConfiguration() {
4692 try {
4693 mActivityManager.updateConfiguration(null);
4694 } catch (RemoteException e) {
4695 }
4696 }
Romain Guy06882f82009-06-10 13:36:04 -07004697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 public Configuration computeNewConfiguration() {
4699 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004700 return computeNewConfigurationLocked();
4701 }
4702 }
Romain Guy06882f82009-06-10 13:36:04 -07004703
Dianne Hackbornc485a602009-03-24 22:39:49 -07004704 Configuration computeNewConfigurationLocked() {
4705 Configuration config = new Configuration();
4706 if (!computeNewConfigurationLocked(config)) {
4707 return null;
4708 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004709 return config;
4710 }
Romain Guy06882f82009-06-10 13:36:04 -07004711
Dianne Hackbornc485a602009-03-24 22:39:49 -07004712 boolean computeNewConfigurationLocked(Configuration config) {
4713 if (mDisplay == null) {
4714 return false;
4715 }
4716 mQueue.getInputConfiguration(config);
4717 final int dw = mDisplay.getWidth();
4718 final int dh = mDisplay.getHeight();
4719 int orientation = Configuration.ORIENTATION_SQUARE;
4720 if (dw < dh) {
4721 orientation = Configuration.ORIENTATION_PORTRAIT;
4722 } else if (dw > dh) {
4723 orientation = Configuration.ORIENTATION_LANDSCAPE;
4724 }
4725 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004726
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004727 DisplayMetrics dm = new DisplayMetrics();
4728 mDisplay.getMetrics(dm);
4729 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4730
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004731 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004732 // Note we only do this once because at this point we don't
4733 // expect the screen to change in this way at runtime, and want
4734 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004735 int longSize = dw;
4736 int shortSize = dh;
4737 if (longSize < shortSize) {
4738 int tmp = longSize;
4739 longSize = shortSize;
4740 shortSize = tmp;
4741 }
4742 longSize = (int)(longSize/dm.density);
4743 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004744
Dianne Hackborn723738c2009-06-25 19:48:04 -07004745 // These semi-magic numbers define our compatibility modes for
4746 // applications with different screens. Don't change unless you
4747 // make sure to test lots and lots of apps!
4748 if (longSize < 470) {
4749 // This is shorter than an HVGA normal density screen (which
4750 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004751 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4752 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004753 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004754 // Is this a large screen?
4755 if (longSize > 640 && shortSize >= 480) {
4756 // VGA or larger screens at medium density are the point
4757 // at which we consider it to be a large screen.
4758 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4759 } else {
4760 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4761
4762 // If this screen is wider than normal HVGA, or taller
4763 // than FWVGA, then for old apps we want to run in size
4764 // compatibility mode.
4765 if (shortSize > 321 || longSize > 570) {
4766 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4767 }
4768 }
4769
4770 // Is this a long screen?
4771 if (((longSize*3)/5) >= (shortSize-1)) {
4772 // Anything wider than WVGA (5:3) is considering to be long.
4773 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4774 } else {
4775 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4776 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004777 }
4778 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004779 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004780
Dianne Hackbornc485a602009-03-24 22:39:49 -07004781 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4782 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4783 mPolicy.adjustConfigurationLw(config);
4784 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004785 }
Romain Guy06882f82009-06-10 13:36:04 -07004786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004787 // -------------------------------------------------------------
4788 // Input Events and Focus Management
4789 // -------------------------------------------------------------
4790
4791 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004792 long curTime = SystemClock.uptimeMillis();
4793
Michael Chane10de972009-05-18 11:24:50 -07004794 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004795 if (mLastTouchEventType == eventType &&
4796 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4797 return;
4798 }
4799 mLastUserActivityCallTime = curTime;
4800 mLastTouchEventType = eventType;
4801 }
4802
4803 if (targetWin == null
4804 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4805 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004806 }
4807 }
4808
4809 // tells if it's a cheek event or not -- this function is stateful
4810 private static final int EVENT_NONE = 0;
4811 private static final int EVENT_UNKNOWN = 0;
4812 private static final int EVENT_CHEEK = 0;
4813 private static final int EVENT_IGNORE_DURATION = 300; // ms
4814 private static final float CHEEK_THRESHOLD = 0.6f;
4815 private int mEventState = EVENT_NONE;
4816 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 private int eventType(MotionEvent ev) {
4819 float size = ev.getSize();
4820 switch (ev.getAction()) {
4821 case MotionEvent.ACTION_DOWN:
4822 mEventSize = size;
4823 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4824 case MotionEvent.ACTION_UP:
4825 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004826 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004827 case MotionEvent.ACTION_MOVE:
4828 final int N = ev.getHistorySize();
4829 if (size > mEventSize) mEventSize = size;
4830 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4831 for (int i=0; i<N; i++) {
4832 size = ev.getHistoricalSize(i);
4833 if (size > mEventSize) mEventSize = size;
4834 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4835 }
4836 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4837 return TOUCH_EVENT;
4838 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004839 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 }
4841 default:
4842 // not good
4843 return OTHER_EVENT;
4844 }
4845 }
4846
4847 /**
4848 * @return Returns true if event was dispatched, false if it was dropped for any reason
4849 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004850 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004851 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4852 "dispatchPointer " + ev);
4853
Michael Chan53071d62009-05-13 17:29:48 -07004854 if (MEASURE_LATENCY) {
4855 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4856 }
4857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004858 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004859 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860
Michael Chan53071d62009-05-13 17:29:48 -07004861 if (MEASURE_LATENCY) {
4862 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4863 }
4864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004865 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867 if (action == MotionEvent.ACTION_UP) {
4868 // let go of our target
4869 mKeyWaiter.mMotionTarget = null;
4870 mPowerManager.logPointerUpEvent();
4871 } else if (action == MotionEvent.ACTION_DOWN) {
4872 mPowerManager.logPointerDownEvent();
4873 }
4874
4875 if (targetObj == null) {
4876 // In this case we are either dropping the event, or have received
4877 // a move or up without a down. It is common to receive move
4878 // events in such a way, since this means the user is moving the
4879 // pointer without actually pressing down. All other cases should
4880 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004881 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4883 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08004884 synchronized (mWindowMap) {
4885 if (mSendingPointersToWallpaper) {
4886 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4887 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4888 }
4889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 if (qev != null) {
4891 mQueue.recycleEvent(qev);
4892 }
4893 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004894 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 }
4896 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004897 synchronized (mWindowMap) {
4898 if (mSendingPointersToWallpaper) {
4899 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4900 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4901 }
4902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903 if (qev != null) {
4904 mQueue.recycleEvent(qev);
4905 }
4906 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004907 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004908 }
Romain Guy06882f82009-06-10 13:36:04 -07004909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004910 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004913 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914
4915 //Log.i(TAG, "Sending " + ev + " to " + target);
4916
4917 if (uid != 0 && uid != target.mSession.mUid) {
4918 if (mContext.checkPermission(
4919 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4920 != PackageManager.PERMISSION_GRANTED) {
4921 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4922 + pid + " uid " + uid + " to window " + target
4923 + " owned by uid " + target.mSession.mUid);
4924 if (qev != null) {
4925 mQueue.recycleEvent(qev);
4926 }
4927 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004928 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 }
4930 }
4931
Michael Chan53071d62009-05-13 17:29:48 -07004932 if (MEASURE_LATENCY) {
4933 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4934 }
4935
Romain Guy06882f82009-06-10 13:36:04 -07004936 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004937 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4938 //target wants to ignore fat touch events
4939 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4940 //explicit flag to return without processing event further
4941 boolean returnFlag = false;
4942 if((action == MotionEvent.ACTION_DOWN)) {
4943 mFatTouch = false;
4944 if(cheekPress) {
4945 mFatTouch = true;
4946 returnFlag = true;
4947 }
4948 } else {
4949 if(action == MotionEvent.ACTION_UP) {
4950 if(mFatTouch) {
4951 //earlier even was invalid doesnt matter if current up is cheekpress or not
4952 mFatTouch = false;
4953 returnFlag = true;
4954 } else if(cheekPress) {
4955 //cancel the earlier event
4956 ev.setAction(MotionEvent.ACTION_CANCEL);
4957 action = MotionEvent.ACTION_CANCEL;
4958 }
4959 } else if(action == MotionEvent.ACTION_MOVE) {
4960 if(mFatTouch) {
4961 //two cases here
4962 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004963 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004964 returnFlag = true;
4965 } else if(cheekPress) {
4966 //valid down followed by invalid moves
4967 //an invalid move have to cancel earlier action
4968 ev.setAction(MotionEvent.ACTION_CANCEL);
4969 action = MotionEvent.ACTION_CANCEL;
4970 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4971 //note that the subsequent invalid moves will not get here
4972 mFatTouch = true;
4973 }
4974 }
4975 } //else if action
4976 if(returnFlag) {
4977 //recycle que, ev
4978 if (qev != null) {
4979 mQueue.recycleEvent(qev);
4980 }
4981 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004982 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004983 }
4984 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004985
Michael Chan9f028e62009-08-04 17:37:46 -07004986 // Enable this for testing the "right" value
4987 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004988 int max_events_per_sec = 35;
4989 try {
4990 max_events_per_sec = Integer.parseInt(SystemProperties
4991 .get("windowsmgr.max_events_per_sec"));
4992 if (max_events_per_sec < 1) {
4993 max_events_per_sec = 35;
4994 }
4995 } catch (NumberFormatException e) {
4996 }
4997 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4998 }
4999
5000 /*
5001 * Throttle events to minimize CPU usage when there's a flood of events
5002 * e.g. constant contact with the screen
5003 */
5004 if (action == MotionEvent.ACTION_MOVE) {
5005 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5006 long now = SystemClock.uptimeMillis();
5007 if (now < nextEventTime) {
5008 try {
5009 Thread.sleep(nextEventTime - now);
5010 } catch (InterruptedException e) {
5011 }
5012 mLastTouchEventTime = nextEventTime;
5013 } else {
5014 mLastTouchEventTime = now;
5015 }
5016 }
5017
Michael Chan53071d62009-05-13 17:29:48 -07005018 if (MEASURE_LATENCY) {
5019 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5020 }
5021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005022 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005023 if (!target.isVisibleLw()) {
5024 // During this motion dispatch, the target window has become
5025 // invisible.
5026 if (mSendingPointersToWallpaper) {
5027 sendPointerToWallpaperLocked(null, ev, eventTime);
5028 }
5029 if (qev != null) {
5030 mQueue.recycleEvent(qev);
5031 }
5032 ev.recycle();
5033 return INJECT_SUCCEEDED;
5034 }
5035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5037 mKeyWaiter.bindTargetWindowLocked(target,
5038 KeyWaiter.RETURN_PENDING_POINTER, qev);
5039 ev = null;
5040 } else {
5041 if (action == MotionEvent.ACTION_DOWN) {
5042 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5043 if (out != null) {
5044 MotionEvent oev = MotionEvent.obtain(ev);
5045 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5046 do {
5047 final Rect frame = out.mFrame;
5048 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5049 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005050 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 } catch (android.os.RemoteException e) {
5052 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
5053 }
5054 oev.offsetLocation((float)frame.left, (float)frame.top);
5055 out = out.mNextOutsideTouch;
5056 } while (out != null);
5057 mKeyWaiter.mOutsideTouchTargets = null;
5058 }
5059 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005060
5061 // If we are on top of the wallpaper, then the wallpaper also
5062 // gets to see this movement.
Dianne Hackborn6adba242009-11-10 11:10:09 -08005063 if (mWallpaperTarget == target || mSendingPointersToWallpaper) {
5064 sendPointerToWallpaperLocked(null, ev, eventTime);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005065 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005066
5067 final Rect frame = target.mFrame;
5068 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5069 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 }
5071 }
Romain Guy06882f82009-06-10 13:36:04 -07005072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 // finally offset the event to the target's coordinate system and
5074 // dispatch the event.
5075 try {
5076 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
5077 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
5078 }
Michael Chan53071d62009-05-13 17:29:48 -07005079
5080 if (MEASURE_LATENCY) {
5081 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5082 }
5083
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005084 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005085
5086 if (MEASURE_LATENCY) {
5087 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5088 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005089 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 } catch (android.os.RemoteException e) {
5091 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
5092 mKeyWaiter.mMotionTarget = null;
5093 try {
5094 removeWindow(target.mSession, target.mClient);
5095 } catch (java.util.NoSuchElementException ex) {
5096 // This will happen if the window has already been
5097 // removed.
5098 }
5099 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005100 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005101 }
Romain Guy06882f82009-06-10 13:36:04 -07005102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 /**
5104 * @return Returns true if event was dispatched, false if it was dropped for any reason
5105 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005106 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005107 if (DEBUG_INPUT) Log.v(
5108 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005111 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 if (focusObj == null) {
5113 Log.w(TAG, "No focus window, dropping trackball: " + ev);
5114 if (qev != null) {
5115 mQueue.recycleEvent(qev);
5116 }
5117 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005118 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005119 }
5120 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5121 if (qev != null) {
5122 mQueue.recycleEvent(qev);
5123 }
5124 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005125 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 }
Romain Guy06882f82009-06-10 13:36:04 -07005127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 if (uid != 0 && uid != focus.mSession.mUid) {
5131 if (mContext.checkPermission(
5132 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5133 != PackageManager.PERMISSION_GRANTED) {
5134 Log.w(TAG, "Permission denied: injecting key event from pid "
5135 + pid + " uid " + uid + " to window " + focus
5136 + " owned by uid " + focus.mSession.mUid);
5137 if (qev != null) {
5138 mQueue.recycleEvent(qev);
5139 }
5140 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005141 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 }
5143 }
Romain Guy06882f82009-06-10 13:36:04 -07005144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 synchronized(mWindowMap) {
5148 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5149 mKeyWaiter.bindTargetWindowLocked(focus,
5150 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5151 // We don't deliver movement events to the client, we hold
5152 // them and wait for them to call back.
5153 ev = null;
5154 } else {
5155 mKeyWaiter.bindTargetWindowLocked(focus);
5156 }
5157 }
Romain Guy06882f82009-06-10 13:36:04 -07005158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005159 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005160 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005161 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 } catch (android.os.RemoteException e) {
5163 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5164 try {
5165 removeWindow(focus.mSession, focus.mClient);
5166 } catch (java.util.NoSuchElementException ex) {
5167 // This will happen if the window has already been
5168 // removed.
5169 }
5170 }
Romain Guy06882f82009-06-10 13:36:04 -07005171
Dianne Hackborncfaef692009-06-15 14:24:44 -07005172 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 }
Romain Guy06882f82009-06-10 13:36:04 -07005174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 /**
5176 * @return Returns true if event was dispatched, false if it was dropped for any reason
5177 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005178 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5180
5181 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005182 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005183 if (focusObj == null) {
5184 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005185 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 }
5187 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005188 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005189 }
Romain Guy06882f82009-06-10 13:36:04 -07005190
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005191 // Okay we have finished waiting for the last event to be processed.
5192 // First off, if this is a repeat event, check to see if there is
5193 // a corresponding up event in the queue. If there is, we will
5194 // just drop the repeat, because it makes no sense to repeat after
5195 // the user has released a key. (This is especially important for
5196 // long presses.)
5197 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5198 return INJECT_SUCCEEDED;
5199 }
5200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 if (DEBUG_INPUT) Log.v(
5204 TAG, "Dispatching to " + focus + ": " + event);
5205
5206 if (uid != 0 && uid != focus.mSession.mUid) {
5207 if (mContext.checkPermission(
5208 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5209 != PackageManager.PERMISSION_GRANTED) {
5210 Log.w(TAG, "Permission denied: injecting key event from pid "
5211 + pid + " uid " + uid + " to window " + focus
5212 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005213 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 }
5215 }
Romain Guy06882f82009-06-10 13:36:04 -07005216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 synchronized(mWindowMap) {
5218 mKeyWaiter.bindTargetWindowLocked(focus);
5219 }
5220
5221 // NOSHIP extra state logging
5222 mKeyWaiter.recordDispatchState(event, focus);
5223 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 try {
5226 if (DEBUG_INPUT || DEBUG_FOCUS) {
5227 Log.v(TAG, "Delivering key " + event.getKeyCode()
5228 + " to " + focus);
5229 }
5230 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005231 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005232 } catch (android.os.RemoteException e) {
5233 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5234 try {
5235 removeWindow(focus.mSession, focus.mClient);
5236 } catch (java.util.NoSuchElementException ex) {
5237 // This will happen if the window has already been
5238 // removed.
5239 }
5240 }
Romain Guy06882f82009-06-10 13:36:04 -07005241
Dianne Hackborncfaef692009-06-15 14:24:44 -07005242 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 }
Romain Guy06882f82009-06-10 13:36:04 -07005244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 public void pauseKeyDispatching(IBinder _token) {
5246 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5247 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005248 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005249 }
5250
5251 synchronized (mWindowMap) {
5252 WindowToken token = mTokenMap.get(_token);
5253 if (token != null) {
5254 mKeyWaiter.pauseDispatchingLocked(token);
5255 }
5256 }
5257 }
5258
5259 public void resumeKeyDispatching(IBinder _token) {
5260 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5261 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005262 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 }
5264
5265 synchronized (mWindowMap) {
5266 WindowToken token = mTokenMap.get(_token);
5267 if (token != null) {
5268 mKeyWaiter.resumeDispatchingLocked(token);
5269 }
5270 }
5271 }
5272
5273 public void setEventDispatching(boolean enabled) {
5274 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5275 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005276 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 }
5278
5279 synchronized (mWindowMap) {
5280 mKeyWaiter.setEventDispatchingLocked(enabled);
5281 }
5282 }
Romain Guy06882f82009-06-10 13:36:04 -07005283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 /**
5285 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005286 *
5287 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 * {@link SystemClock#uptimeMillis()} as the timebase.)
5289 * @param sync If true, wait for the event to be completed before returning to the caller.
5290 * @return Returns true if event was dispatched, false if it was dropped for any reason
5291 */
5292 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5293 long downTime = ev.getDownTime();
5294 long eventTime = ev.getEventTime();
5295
5296 int action = ev.getAction();
5297 int code = ev.getKeyCode();
5298 int repeatCount = ev.getRepeatCount();
5299 int metaState = ev.getMetaState();
5300 int deviceId = ev.getDeviceId();
5301 int scancode = ev.getScanCode();
5302
5303 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5304 if (downTime == 0) downTime = eventTime;
5305
5306 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005307 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005309 final int pid = Binder.getCallingPid();
5310 final int uid = Binder.getCallingUid();
5311 final long ident = Binder.clearCallingIdentity();
5312 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005314 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005315 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005316 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005317 switch (result) {
5318 case INJECT_NO_PERMISSION:
5319 throw new SecurityException(
5320 "Injecting to another application requires INJECT_EVENT permission");
5321 case INJECT_SUCCEEDED:
5322 return true;
5323 }
5324 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 }
5326
5327 /**
5328 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005329 *
5330 * @param ev A motion event describing the pointer (touch) action. (As noted in
5331 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 * {@link SystemClock#uptimeMillis()} as the timebase.)
5333 * @param sync If true, wait for the event to be completed before returning to the caller.
5334 * @return Returns true if event was dispatched, false if it was dropped for any reason
5335 */
5336 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005337 final int pid = Binder.getCallingPid();
5338 final int uid = Binder.getCallingUid();
5339 final long ident = Binder.clearCallingIdentity();
5340 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005341 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005342 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005344 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005345 switch (result) {
5346 case INJECT_NO_PERMISSION:
5347 throw new SecurityException(
5348 "Injecting to another application requires INJECT_EVENT permission");
5349 case INJECT_SUCCEEDED:
5350 return true;
5351 }
5352 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005353 }
Romain Guy06882f82009-06-10 13:36:04 -07005354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355 /**
5356 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005357 *
5358 * @param ev A motion event describing the trackball action. (As noted in
5359 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005360 * {@link SystemClock#uptimeMillis()} as the timebase.)
5361 * @param sync If true, wait for the event to be completed before returning to the caller.
5362 * @return Returns true if event was dispatched, false if it was dropped for any reason
5363 */
5364 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005365 final int pid = Binder.getCallingPid();
5366 final int uid = Binder.getCallingUid();
5367 final long ident = Binder.clearCallingIdentity();
5368 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005369 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005370 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005371 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005372 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005373 switch (result) {
5374 case INJECT_NO_PERMISSION:
5375 throw new SecurityException(
5376 "Injecting to another application requires INJECT_EVENT permission");
5377 case INJECT_SUCCEEDED:
5378 return true;
5379 }
5380 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 }
Romain Guy06882f82009-06-10 13:36:04 -07005382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 private WindowState getFocusedWindow() {
5384 synchronized (mWindowMap) {
5385 return getFocusedWindowLocked();
5386 }
5387 }
5388
5389 private WindowState getFocusedWindowLocked() {
5390 return mCurrentFocus;
5391 }
Romain Guy06882f82009-06-10 13:36:04 -07005392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 /**
5394 * This class holds the state for dispatching key events. This state
5395 * is protected by the KeyWaiter instance, NOT by the window lock. You
5396 * can be holding the main window lock while acquire the KeyWaiter lock,
5397 * but not the other way around.
5398 */
5399 final class KeyWaiter {
5400 // NOSHIP debugging
5401 public class DispatchState {
5402 private KeyEvent event;
5403 private WindowState focus;
5404 private long time;
5405 private WindowState lastWin;
5406 private IBinder lastBinder;
5407 private boolean finished;
5408 private boolean gotFirstWindow;
5409 private boolean eventDispatching;
5410 private long timeToSwitch;
5411 private boolean wasFrozen;
5412 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005413 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5416 focus = theFocus;
5417 event = theEvent;
5418 time = System.currentTimeMillis();
5419 // snapshot KeyWaiter state
5420 lastWin = mLastWin;
5421 lastBinder = mLastBinder;
5422 finished = mFinished;
5423 gotFirstWindow = mGotFirstWindow;
5424 eventDispatching = mEventDispatching;
5425 timeToSwitch = mTimeToSwitch;
5426 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005427 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 // cache the paused state at ctor time as well
5429 if (theFocus == null || theFocus.mToken == null) {
5430 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5431 focusPaused = false;
5432 } else {
5433 focusPaused = theFocus.mToken.paused;
5434 }
5435 }
Romain Guy06882f82009-06-10 13:36:04 -07005436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 public String toString() {
5438 return "{{" + event + " to " + focus + " @ " + time
5439 + " lw=" + lastWin + " lb=" + lastBinder
5440 + " fin=" + finished + " gfw=" + gotFirstWindow
5441 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005442 + " wf=" + wasFrozen + " fp=" + focusPaused
5443 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005444 }
5445 };
5446 private DispatchState mDispatchState = null;
5447 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5448 mDispatchState = new DispatchState(theEvent, theFocus);
5449 }
5450 // END NOSHIP
5451
5452 public static final int RETURN_NOTHING = 0;
5453 public static final int RETURN_PENDING_POINTER = 1;
5454 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 final Object SKIP_TARGET_TOKEN = new Object();
5457 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 private WindowState mLastWin = null;
5460 private IBinder mLastBinder = null;
5461 private boolean mFinished = true;
5462 private boolean mGotFirstWindow = false;
5463 private boolean mEventDispatching = true;
5464 private long mTimeToSwitch = 0;
5465 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 // Target of Motion events
5468 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005470 // Windows above the target who would like to receive an "outside"
5471 // touch event for any down events outside of them.
5472 WindowState mOutsideTouchTargets;
5473
5474 /**
5475 * Wait for the last event dispatch to complete, then find the next
5476 * target that should receive the given event and wait for that one
5477 * to be ready to receive it.
5478 */
5479 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5480 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005481 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 long startTime = SystemClock.uptimeMillis();
5483 long keyDispatchingTimeout = 5 * 1000;
5484 long waitedFor = 0;
5485
5486 while (true) {
5487 // Figure out which window we care about. It is either the
5488 // last window we are waiting to have process the event or,
5489 // if none, then the next window we think the event should go
5490 // to. Note: we retrieve mLastWin outside of the lock, so
5491 // it may change before we lock. Thus we must check it again.
5492 WindowState targetWin = mLastWin;
5493 boolean targetIsNew = targetWin == null;
5494 if (DEBUG_INPUT) Log.v(
5495 TAG, "waitForLastKey: mFinished=" + mFinished +
5496 ", mLastWin=" + mLastWin);
5497 if (targetIsNew) {
5498 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005499 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 if (target == SKIP_TARGET_TOKEN) {
5501 // The user has pressed a special key, and we are
5502 // dropping all pending events before it.
5503 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5504 + " " + nextMotion);
5505 return null;
5506 }
5507 if (target == CONSUMED_EVENT_TOKEN) {
5508 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5509 + " " + nextMotion);
5510 return target;
5511 }
5512 targetWin = (WindowState)target;
5513 }
Romain Guy06882f82009-06-10 13:36:04 -07005514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005515 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 // Now: is it okay to send the next event to this window?
5518 synchronized (this) {
5519 // First: did we come here based on the last window not
5520 // being null, but it changed by the time we got here?
5521 // If so, try again.
5522 if (!targetIsNew && mLastWin == null) {
5523 continue;
5524 }
Romain Guy06882f82009-06-10 13:36:04 -07005525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005526 // We never dispatch events if not finished with the
5527 // last one, or the display is frozen.
5528 if (mFinished && !mDisplayFrozen) {
5529 // If event dispatching is disabled, then we
5530 // just consume the events.
5531 if (!mEventDispatching) {
5532 if (DEBUG_INPUT) Log.v(TAG,
5533 "Skipping event; dispatching disabled: "
5534 + nextKey + " " + nextMotion);
5535 return null;
5536 }
5537 if (targetWin != null) {
5538 // If this is a new target, and that target is not
5539 // paused or unresponsive, then all looks good to
5540 // handle the event.
5541 if (targetIsNew && !targetWin.mToken.paused) {
5542 return targetWin;
5543 }
Romain Guy06882f82009-06-10 13:36:04 -07005544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 // If we didn't find a target window, and there is no
5546 // focused app window, then just eat the events.
5547 } else if (mFocusedApp == null) {
5548 if (DEBUG_INPUT) Log.v(TAG,
5549 "Skipping event; no focused app: "
5550 + nextKey + " " + nextMotion);
5551 return null;
5552 }
5553 }
Romain Guy06882f82009-06-10 13:36:04 -07005554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005555 if (DEBUG_INPUT) Log.v(
5556 TAG, "Waiting for last key in " + mLastBinder
5557 + " target=" + targetWin
5558 + " mFinished=" + mFinished
5559 + " mDisplayFrozen=" + mDisplayFrozen
5560 + " targetIsNew=" + targetIsNew
5561 + " paused="
5562 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005563 + " mFocusedApp=" + mFocusedApp
5564 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 targetApp = targetWin != null
5567 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 long curTimeout = keyDispatchingTimeout;
5570 if (mTimeToSwitch != 0) {
5571 long now = SystemClock.uptimeMillis();
5572 if (mTimeToSwitch <= now) {
5573 // If an app switch key has been pressed, and we have
5574 // waited too long for the current app to finish
5575 // processing keys, then wait no more!
5576 doFinishedKeyLocked(true);
5577 continue;
5578 }
5579 long switchTimeout = mTimeToSwitch - now;
5580 if (curTimeout > switchTimeout) {
5581 curTimeout = switchTimeout;
5582 }
5583 }
Romain Guy06882f82009-06-10 13:36:04 -07005584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 try {
5586 // after that continue
5587 // processing keys, so we don't get stuck.
5588 if (DEBUG_INPUT) Log.v(
5589 TAG, "Waiting for key dispatch: " + curTimeout);
5590 wait(curTimeout);
5591 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5592 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005593 + startTime + " switchTime=" + mTimeToSwitch
5594 + " target=" + targetWin + " mLW=" + mLastWin
5595 + " mLB=" + mLastBinder + " fin=" + mFinished
5596 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005597 } catch (InterruptedException e) {
5598 }
5599 }
5600
5601 // If we were frozen during configuration change, restart the
5602 // timeout checks from now; otherwise look at whether we timed
5603 // out before awakening.
5604 if (mWasFrozen) {
5605 waitedFor = 0;
5606 mWasFrozen = false;
5607 } else {
5608 waitedFor = SystemClock.uptimeMillis() - startTime;
5609 }
5610
5611 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5612 IApplicationToken at = null;
5613 synchronized (this) {
5614 Log.w(TAG, "Key dispatching timed out sending to " +
5615 (targetWin != null ? targetWin.mAttrs.getTitle()
5616 : "<null>"));
5617 // NOSHIP debugging
5618 Log.w(TAG, "Dispatch state: " + mDispatchState);
5619 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5620 // END NOSHIP
5621 //dump();
5622 if (targetWin != null) {
5623 at = targetWin.getAppToken();
5624 } else if (targetApp != null) {
5625 at = targetApp.appToken;
5626 }
5627 }
5628
5629 boolean abort = true;
5630 if (at != null) {
5631 try {
5632 long timeout = at.getKeyDispatchingTimeout();
5633 if (timeout > waitedFor) {
5634 // we did not wait the proper amount of time for this application.
5635 // set the timeout to be the real timeout and wait again.
5636 keyDispatchingTimeout = timeout - waitedFor;
5637 continue;
5638 } else {
5639 abort = at.keyDispatchingTimedOut();
5640 }
5641 } catch (RemoteException ex) {
5642 }
5643 }
5644
5645 synchronized (this) {
5646 if (abort && (mLastWin == targetWin || targetWin == null)) {
5647 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005648 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649 if (DEBUG_INPUT) Log.v(TAG,
5650 "Window " + mLastWin +
5651 " timed out on key input");
5652 if (mLastWin.mToken.paused) {
5653 Log.w(TAG, "Un-pausing dispatching to this window");
5654 mLastWin.mToken.paused = false;
5655 }
5656 }
5657 if (mMotionTarget == targetWin) {
5658 mMotionTarget = null;
5659 }
5660 mLastWin = null;
5661 mLastBinder = null;
5662 if (failIfTimeout || targetWin == null) {
5663 return null;
5664 }
5665 } else {
5666 Log.w(TAG, "Continuing to wait for key to be dispatched");
5667 startTime = SystemClock.uptimeMillis();
5668 }
5669 }
5670 }
5671 }
5672 }
Romain Guy06882f82009-06-10 13:36:04 -07005673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005675 MotionEvent nextMotion, boolean isPointerEvent,
5676 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005679 if (nextKey != null) {
5680 // Find the target window for a normal key event.
5681 final int keycode = nextKey.getKeyCode();
5682 final int repeatCount = nextKey.getRepeatCount();
5683 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5684 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005687 if (callingUid == 0 ||
5688 mContext.checkPermission(
5689 android.Manifest.permission.INJECT_EVENTS,
5690 callingPid, callingUid)
5691 == PackageManager.PERMISSION_GRANTED) {
5692 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005693 nextKey.getMetaState(), down, repeatCount,
5694 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 Log.w(TAG, "Event timeout during app switch: dropping "
5697 + nextKey);
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 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 WindowState focus = null;
5704 synchronized(mWindowMap) {
5705 focus = getFocusedWindowLocked();
5706 }
Romain Guy06882f82009-06-10 13:36:04 -07005707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005709
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005710 if (callingUid == 0 ||
5711 (focus != null && callingUid == focus.mSession.mUid) ||
5712 mContext.checkPermission(
5713 android.Manifest.permission.INJECT_EVENTS,
5714 callingPid, callingUid)
5715 == PackageManager.PERMISSION_GRANTED) {
5716 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005717 keycode, nextKey.getMetaState(), down, repeatCount,
5718 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005719 return CONSUMED_EVENT_TOKEN;
5720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005721 }
Romain Guy06882f82009-06-10 13:36:04 -07005722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005723 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 } else if (!isPointerEvent) {
5726 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5727 if (!dispatch) {
5728 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5729 + nextMotion);
5730 return SKIP_TARGET_TOKEN;
5731 }
Romain Guy06882f82009-06-10 13:36:04 -07005732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005733 WindowState focus = null;
5734 synchronized(mWindowMap) {
5735 focus = getFocusedWindowLocked();
5736 }
Romain Guy06882f82009-06-10 13:36:04 -07005737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5739 return focus;
5740 }
Romain Guy06882f82009-06-10 13:36:04 -07005741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005742 if (nextMotion == null) {
5743 return SKIP_TARGET_TOKEN;
5744 }
Romain Guy06882f82009-06-10 13:36:04 -07005745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5747 KeyEvent.KEYCODE_UNKNOWN);
5748 if (!dispatch) {
5749 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5750 + nextMotion);
5751 return SKIP_TARGET_TOKEN;
5752 }
Romain Guy06882f82009-06-10 13:36:04 -07005753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 // Find the target window for a pointer event.
5755 int action = nextMotion.getAction();
5756 final float xf = nextMotion.getX();
5757 final float yf = nextMotion.getY();
5758 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 final boolean screenWasOff = qev != null
5761 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005765 synchronized(mWindowMap) {
5766 synchronized (this) {
5767 if (action == MotionEvent.ACTION_DOWN) {
5768 if (mMotionTarget != null) {
5769 // this is weird, we got a pen down, but we thought it was
5770 // already down!
5771 // XXX: We should probably send an ACTION_UP to the current
5772 // target.
5773 Log.w(TAG, "Pointer down received while already down in: "
5774 + mMotionTarget);
5775 mMotionTarget = null;
5776 }
Romain Guy06882f82009-06-10 13:36:04 -07005777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005778 // ACTION_DOWN is special, because we need to lock next events to
5779 // the window we'll land onto.
5780 final int x = (int)xf;
5781 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 final ArrayList windows = mWindows;
5784 final int N = windows.size();
5785 WindowState topErrWindow = null;
5786 final Rect tmpRect = mTempRect;
5787 for (int i=N-1; i>=0; i--) {
5788 WindowState child = (WindowState)windows.get(i);
5789 //Log.i(TAG, "Checking dispatch to: " + child);
5790 final int flags = child.mAttrs.flags;
5791 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5792 if (topErrWindow == null) {
5793 topErrWindow = child;
5794 }
5795 }
5796 if (!child.isVisibleLw()) {
5797 //Log.i(TAG, "Not visible!");
5798 continue;
5799 }
5800 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5801 //Log.i(TAG, "Not touchable!");
5802 if ((flags & WindowManager.LayoutParams
5803 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5804 child.mNextOutsideTouch = mOutsideTouchTargets;
5805 mOutsideTouchTargets = child;
5806 }
5807 continue;
5808 }
5809 tmpRect.set(child.mFrame);
5810 if (child.mTouchableInsets == ViewTreeObserver
5811 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5812 // The touch is inside of the window if it is
5813 // inside the frame, AND the content part of that
5814 // frame that was given by the application.
5815 tmpRect.left += child.mGivenContentInsets.left;
5816 tmpRect.top += child.mGivenContentInsets.top;
5817 tmpRect.right -= child.mGivenContentInsets.right;
5818 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5819 } else if (child.mTouchableInsets == ViewTreeObserver
5820 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5821 // The touch is inside of the window if it is
5822 // inside the frame, AND the visible part of that
5823 // frame that was given by the application.
5824 tmpRect.left += child.mGivenVisibleInsets.left;
5825 tmpRect.top += child.mGivenVisibleInsets.top;
5826 tmpRect.right -= child.mGivenVisibleInsets.right;
5827 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5828 }
5829 final int touchFlags = flags &
5830 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5831 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5832 if (tmpRect.contains(x, y) || touchFlags == 0) {
5833 //Log.i(TAG, "Using this target!");
5834 if (!screenWasOff || (flags &
5835 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5836 mMotionTarget = child;
5837 } else {
5838 //Log.i(TAG, "Waking, skip!");
5839 mMotionTarget = null;
5840 }
5841 break;
5842 }
Romain Guy06882f82009-06-10 13:36:04 -07005843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 if ((flags & WindowManager.LayoutParams
5845 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5846 child.mNextOutsideTouch = mOutsideTouchTargets;
5847 mOutsideTouchTargets = child;
5848 //Log.i(TAG, "Adding to outside target list: " + child);
5849 }
5850 }
5851
5852 // if there's an error window but it's not accepting
5853 // focus (typically because it is not yet visible) just
5854 // wait for it -- any other focused window may in fact
5855 // be in ANR state.
5856 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5857 mMotionTarget = null;
5858 }
5859 }
Romain Guy06882f82009-06-10 13:36:04 -07005860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 target = mMotionTarget;
5862 }
5863 }
Romain Guy06882f82009-06-10 13:36:04 -07005864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005867 // Pointer events are a little different -- if there isn't a
5868 // target found for any event, then just drop it.
5869 return target != null ? target : SKIP_TARGET_TOKEN;
5870 }
Romain Guy06882f82009-06-10 13:36:04 -07005871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005872 boolean checkShouldDispatchKey(int keycode) {
5873 synchronized (this) {
5874 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5875 mTimeToSwitch = 0;
5876 return true;
5877 }
5878 if (mTimeToSwitch != 0
5879 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5880 return false;
5881 }
5882 return true;
5883 }
5884 }
Romain Guy06882f82009-06-10 13:36:04 -07005885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 void bindTargetWindowLocked(WindowState win,
5887 int pendingWhat, QueuedEvent pendingMotion) {
5888 synchronized (this) {
5889 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5890 }
5891 }
Romain Guy06882f82009-06-10 13:36:04 -07005892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 void bindTargetWindowLocked(WindowState win) {
5894 synchronized (this) {
5895 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5896 }
5897 }
5898
5899 void bindTargetWindowLockedLocked(WindowState win,
5900 int pendingWhat, QueuedEvent pendingMotion) {
5901 mLastWin = win;
5902 mLastBinder = win.mClient.asBinder();
5903 mFinished = false;
5904 if (pendingMotion != null) {
5905 final Session s = win.mSession;
5906 if (pendingWhat == RETURN_PENDING_POINTER) {
5907 releasePendingPointerLocked(s);
5908 s.mPendingPointerMove = pendingMotion;
5909 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005910 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 "bindTargetToWindow " + s.mPendingPointerMove);
5912 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5913 releasePendingTrackballLocked(s);
5914 s.mPendingTrackballMove = pendingMotion;
5915 s.mPendingTrackballWindow = win;
5916 }
5917 }
5918 }
Romain Guy06882f82009-06-10 13:36:04 -07005919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005920 void releasePendingPointerLocked(Session s) {
5921 if (DEBUG_INPUT) Log.v(TAG,
5922 "releasePendingPointer " + s.mPendingPointerMove);
5923 if (s.mPendingPointerMove != null) {
5924 mQueue.recycleEvent(s.mPendingPointerMove);
5925 s.mPendingPointerMove = null;
5926 }
5927 }
Romain Guy06882f82009-06-10 13:36:04 -07005928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005929 void releasePendingTrackballLocked(Session s) {
5930 if (s.mPendingTrackballMove != null) {
5931 mQueue.recycleEvent(s.mPendingTrackballMove);
5932 s.mPendingTrackballMove = null;
5933 }
5934 }
Romain Guy06882f82009-06-10 13:36:04 -07005935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5937 int returnWhat) {
5938 if (DEBUG_INPUT) Log.v(
5939 TAG, "finishedKey: client=" + client + ", force=" + force);
5940
5941 if (client == null) {
5942 return null;
5943 }
5944
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005945 MotionEvent res = null;
5946 QueuedEvent qev = null;
5947 WindowState win = null;
5948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 synchronized (this) {
5950 if (DEBUG_INPUT) Log.v(
5951 TAG, "finishedKey: client=" + client.asBinder()
5952 + ", force=" + force + ", last=" + mLastBinder
5953 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 if (returnWhat == RETURN_PENDING_POINTER) {
5956 qev = session.mPendingPointerMove;
5957 win = session.mPendingPointerWindow;
5958 session.mPendingPointerMove = null;
5959 session.mPendingPointerWindow = null;
5960 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5961 qev = session.mPendingTrackballMove;
5962 win = session.mPendingTrackballWindow;
5963 session.mPendingTrackballMove = null;
5964 session.mPendingTrackballWindow = null;
5965 }
Romain Guy06882f82009-06-10 13:36:04 -07005966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 if (mLastBinder == client.asBinder()) {
5968 if (DEBUG_INPUT) Log.v(
5969 TAG, "finishedKey: last paused="
5970 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5971 if (mLastWin != null && (!mLastWin.mToken.paused || force
5972 || !mEventDispatching)) {
5973 doFinishedKeyLocked(false);
5974 } else {
5975 // Make sure to wake up anyone currently waiting to
5976 // dispatch a key, so they can re-evaluate their
5977 // current situation.
5978 mFinished = true;
5979 notifyAll();
5980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005981 }
Romain Guy06882f82009-06-10 13:36:04 -07005982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005984 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 if (DEBUG_INPUT) Log.v(TAG,
5986 "Returning pending motion: " + res);
5987 mQueue.recycleEvent(qev);
5988 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5989 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005992
5993 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5994 synchronized (mWindowMap) {
5995 if (mWallpaperTarget == win || mSendingPointersToWallpaper) {
5996 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5997 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005998 }
5999 }
6000 }
6001
6002 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 }
6004
6005 void tickle() {
6006 synchronized (this) {
6007 notifyAll();
6008 }
6009 }
Romain Guy06882f82009-06-10 13:36:04 -07006010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 void handleNewWindowLocked(WindowState newWindow) {
6012 if (!newWindow.canReceiveKeys()) {
6013 return;
6014 }
6015 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006016 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 TAG, "New key dispatch window: win="
6018 + newWindow.mClient.asBinder()
6019 + ", last=" + mLastBinder
6020 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6021 + "), finished=" + mFinished + ", paused="
6022 + newWindow.mToken.paused);
6023
6024 // Displaying a window implicitly causes dispatching to
6025 // be unpaused. (This is to protect against bugs if someone
6026 // pauses dispatching but forgets to resume.)
6027 newWindow.mToken.paused = false;
6028
6029 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030
6031 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
6032 if (DEBUG_INPUT) Log.v(TAG,
6033 "New SYSTEM_ERROR window; resetting state");
6034 mLastWin = null;
6035 mLastBinder = null;
6036 mMotionTarget = null;
6037 mFinished = true;
6038 } else if (mLastWin != null) {
6039 // If the new window is above the window we are
6040 // waiting on, then stop waiting and let key dispatching
6041 // start on the new guy.
6042 if (DEBUG_INPUT) Log.v(
6043 TAG, "Last win layer=" + mLastWin.mLayer
6044 + ", new win layer=" + newWindow.mLayer);
6045 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006046 // The new window is above the old; finish pending input to the last
6047 // window and start directing it to the new one.
6048 mLastWin.mToken.paused = false;
6049 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006051 // Either the new window is lower, so there is no need to wake key waiters,
6052 // or we just finished key input to the previous window, which implicitly
6053 // notified the key waiters. In both cases, we don't need to issue the
6054 // notification here.
6055 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 }
6057
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006058 // Now that we've put a new window state in place, make the event waiter
6059 // take notice and retarget its attentions.
6060 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061 }
6062 }
6063
6064 void pauseDispatchingLocked(WindowToken token) {
6065 synchronized (this)
6066 {
6067 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
6068 token.paused = true;
6069
6070 /*
6071 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6072 mPaused = true;
6073 } else {
6074 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07006075 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07006077 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07006079 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 }
6081 }
6082 */
6083 }
6084 }
6085
6086 void resumeDispatchingLocked(WindowToken token) {
6087 synchronized (this) {
6088 if (token.paused) {
6089 if (DEBUG_INPUT) Log.v(
6090 TAG, "Resuming WindowToken " + token
6091 + ", last=" + mLastBinder
6092 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6093 + "), finished=" + mFinished + ", paused="
6094 + token.paused);
6095 token.paused = false;
6096 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
6097 doFinishedKeyLocked(true);
6098 } else {
6099 notifyAll();
6100 }
6101 }
6102 }
6103 }
6104
6105 void setEventDispatchingLocked(boolean enabled) {
6106 synchronized (this) {
6107 mEventDispatching = enabled;
6108 notifyAll();
6109 }
6110 }
Romain Guy06882f82009-06-10 13:36:04 -07006111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 void appSwitchComing() {
6113 synchronized (this) {
6114 // Don't wait for more than .5 seconds for app to finish
6115 // processing the pending events.
6116 long now = SystemClock.uptimeMillis() + 500;
6117 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6118 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6119 mTimeToSwitch = now;
6120 }
6121 notifyAll();
6122 }
6123 }
Romain Guy06882f82009-06-10 13:36:04 -07006124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006125 private final void doFinishedKeyLocked(boolean doRecycle) {
6126 if (mLastWin != null) {
6127 releasePendingPointerLocked(mLastWin.mSession);
6128 releasePendingTrackballLocked(mLastWin.mSession);
6129 }
Romain Guy06882f82009-06-10 13:36:04 -07006130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131 if (mLastWin == null || !mLastWin.mToken.paused
6132 || !mLastWin.isVisibleLw()) {
6133 // If the current window has been paused, we aren't -really-
6134 // finished... so let the waiters still wait.
6135 mLastWin = null;
6136 mLastBinder = null;
6137 }
6138 mFinished = true;
6139 notifyAll();
6140 }
6141 }
6142
6143 private class KeyQ extends KeyInputQueue
6144 implements KeyInputQueue.FilterCallback {
6145 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006147 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006148 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6150 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6151 "KEEP_SCREEN_ON_FLAG");
6152 mHoldingScreen.setReferenceCounted(false);
6153 }
6154
6155 @Override
6156 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6157 if (mPolicy.preprocessInputEventTq(event)) {
6158 return true;
6159 }
Romain Guy06882f82009-06-10 13:36:04 -07006160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006161 switch (event.type) {
6162 case RawInputEvent.EV_KEY: {
6163 // XXX begin hack
6164 if (DEBUG) {
6165 if (event.keycode == KeyEvent.KEYCODE_G) {
6166 if (event.value != 0) {
6167 // G down
6168 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6169 }
6170 return false;
6171 }
6172 if (event.keycode == KeyEvent.KEYCODE_D) {
6173 if (event.value != 0) {
6174 //dump();
6175 }
6176 return false;
6177 }
6178 }
6179 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006180
Charles Mendis322591c2009-10-29 11:06:59 -07006181 boolean screenIsOff = !mPowerManager.isScreenOn();
6182 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6186 mPowerManager.goToSleep(event.when);
6187 }
6188
6189 if (screenIsOff) {
6190 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6191 }
6192 if (screenIsDim) {
6193 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6194 }
6195 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6196 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006197 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006198 }
Romain Guy06882f82009-06-10 13:36:04 -07006199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6201 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6202 filterQueue(this);
6203 mKeyWaiter.appSwitchComing();
6204 }
6205 return true;
6206 } else {
6207 return false;
6208 }
6209 }
Romain Guy06882f82009-06-10 13:36:04 -07006210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006211 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006212 boolean screenIsOff = !mPowerManager.isScreenOn();
6213 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006214 if (screenIsOff) {
6215 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6216 device.classes, event)) {
6217 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6218 return false;
6219 }
6220 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6221 }
6222 if (screenIsDim) {
6223 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6224 }
6225 return true;
6226 }
Romain Guy06882f82009-06-10 13:36:04 -07006227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006228 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006229 boolean screenIsOff = !mPowerManager.isScreenOn();
6230 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 if (screenIsOff) {
6232 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6233 device.classes, event)) {
6234 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6235 return false;
6236 }
6237 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6238 }
6239 if (screenIsDim) {
6240 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6241 }
6242 return true;
6243 }
Romain Guy06882f82009-06-10 13:36:04 -07006244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006245 default:
6246 return true;
6247 }
6248 }
6249
6250 public int filterEvent(QueuedEvent ev) {
6251 switch (ev.classType) {
6252 case RawInputEvent.CLASS_KEYBOARD:
6253 KeyEvent ke = (KeyEvent)ev.event;
6254 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6255 Log.w(TAG, "Dropping movement key during app switch: "
6256 + ke.getKeyCode() + ", action=" + ke.getAction());
6257 return FILTER_REMOVE;
6258 }
6259 return FILTER_ABORT;
6260 default:
6261 return FILTER_KEEP;
6262 }
6263 }
Romain Guy06882f82009-06-10 13:36:04 -07006264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006265 /**
6266 * Must be called with the main window manager lock held.
6267 */
6268 void setHoldScreenLocked(boolean holding) {
6269 boolean state = mHoldingScreen.isHeld();
6270 if (holding != state) {
6271 if (holding) {
6272 mHoldingScreen.acquire();
6273 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006274 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006275 mHoldingScreen.release();
6276 }
6277 }
6278 }
Michael Chan53071d62009-05-13 17:29:48 -07006279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006280
6281 public boolean detectSafeMode() {
6282 mSafeMode = mPolicy.detectSafeMode();
6283 return mSafeMode;
6284 }
Romain Guy06882f82009-06-10 13:36:04 -07006285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 public void systemReady() {
6287 mPolicy.systemReady();
6288 }
Romain Guy06882f82009-06-10 13:36:04 -07006289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 private final class InputDispatcherThread extends Thread {
6291 // Time to wait when there is nothing to do: 9999 seconds.
6292 static final int LONG_WAIT=9999*1000;
6293
6294 public InputDispatcherThread() {
6295 super("InputDispatcher");
6296 }
Romain Guy06882f82009-06-10 13:36:04 -07006297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 @Override
6299 public void run() {
6300 while (true) {
6301 try {
6302 process();
6303 } catch (Exception e) {
6304 Log.e(TAG, "Exception in input dispatcher", e);
6305 }
6306 }
6307 }
Romain Guy06882f82009-06-10 13:36:04 -07006308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309 private void process() {
6310 android.os.Process.setThreadPriority(
6311 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 // The last key event we saw
6314 KeyEvent lastKey = null;
6315
6316 // Last keydown time for auto-repeating keys
6317 long lastKeyTime = SystemClock.uptimeMillis();
6318 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006319 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006320
Romain Guy06882f82009-06-10 13:36:04 -07006321 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 int keyRepeatCount = 0;
6323
6324 // Need to report that configuration has changed?
6325 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 while (true) {
6328 long curTime = SystemClock.uptimeMillis();
6329
6330 if (DEBUG_INPUT) Log.v(
6331 TAG, "Waiting for next key: now=" + curTime
6332 + ", repeat @ " + nextKeyTime);
6333
6334 // Retrieve next event, waiting only as long as the next
6335 // repeat timeout. If the configuration has changed, then
6336 // don't wait at all -- we'll report the change as soon as
6337 // we have processed all events.
6338 QueuedEvent ev = mQueue.getEvent(
6339 (int)((!configChanged && curTime < nextKeyTime)
6340 ? (nextKeyTime-curTime) : 0));
6341
6342 if (DEBUG_INPUT && ev != null) Log.v(
6343 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6344
Michael Chan53071d62009-05-13 17:29:48 -07006345 if (MEASURE_LATENCY) {
6346 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6347 }
6348
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006349 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6350 // cancel key repeat at the request of the policy.
6351 lastKey = null;
6352 downTime = 0;
6353 lastKeyTime = curTime;
6354 nextKeyTime = curTime + LONG_WAIT;
6355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 try {
6357 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006358 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006359 int eventType;
6360 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6361 eventType = eventType((MotionEvent)ev.event);
6362 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6363 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6364 eventType = LocalPowerManager.BUTTON_EVENT;
6365 } else {
6366 eventType = LocalPowerManager.OTHER_EVENT;
6367 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006368 try {
Michael Chan53071d62009-05-13 17:29:48 -07006369 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006370 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006371 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006372 mBatteryStats.noteInputEvent();
6373 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006374 } catch (RemoteException e) {
6375 // Ignore
6376 }
Michael Chane10de972009-05-18 11:24:50 -07006377
6378 if (eventType != TOUCH_EVENT
6379 && eventType != LONG_TOUCH_EVENT
6380 && eventType != CHEEK_EVENT) {
6381 mPowerManager.userActivity(curTime, false,
6382 eventType, false);
6383 } else if (mLastTouchEventType != eventType
6384 || (curTime - mLastUserActivityCallTime)
6385 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6386 mLastUserActivityCallTime = curTime;
6387 mLastTouchEventType = eventType;
6388 mPowerManager.userActivity(curTime, false,
6389 eventType, false);
6390 }
6391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006392 switch (ev.classType) {
6393 case RawInputEvent.CLASS_KEYBOARD:
6394 KeyEvent ke = (KeyEvent)ev.event;
6395 if (ke.isDown()) {
6396 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006397 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006398 keyRepeatCount = 0;
6399 lastKeyTime = curTime;
6400 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006401 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 if (DEBUG_INPUT) Log.v(
6403 TAG, "Received key down: first repeat @ "
6404 + nextKeyTime);
6405 } else {
6406 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006407 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006408 // Arbitrary long timeout.
6409 lastKeyTime = curTime;
6410 nextKeyTime = curTime + LONG_WAIT;
6411 if (DEBUG_INPUT) Log.v(
6412 TAG, "Received key up: ignore repeat @ "
6413 + nextKeyTime);
6414 }
6415 dispatchKey((KeyEvent)ev.event, 0, 0);
6416 mQueue.recycleEvent(ev);
6417 break;
6418 case RawInputEvent.CLASS_TOUCHSCREEN:
6419 //Log.i(TAG, "Read next event " + ev);
6420 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6421 break;
6422 case RawInputEvent.CLASS_TRACKBALL:
6423 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6424 break;
6425 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6426 configChanged = true;
6427 break;
6428 default:
6429 mQueue.recycleEvent(ev);
6430 break;
6431 }
Romain Guy06882f82009-06-10 13:36:04 -07006432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 } else if (configChanged) {
6434 configChanged = false;
6435 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006437 } else if (lastKey != null) {
6438 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006440 // Timeout occurred while key was down. If it is at or
6441 // past the key repeat time, dispatch the repeat.
6442 if (DEBUG_INPUT) Log.v(
6443 TAG, "Key timeout: repeat=" + nextKeyTime
6444 + ", now=" + curTime);
6445 if (curTime < nextKeyTime) {
6446 continue;
6447 }
Romain Guy06882f82009-06-10 13:36:04 -07006448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006449 lastKeyTime = nextKeyTime;
6450 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6451 keyRepeatCount++;
6452 if (DEBUG_INPUT) Log.v(
6453 TAG, "Key repeat: count=" + keyRepeatCount
6454 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006455 KeyEvent newEvent;
6456 if (downTime != 0 && (downTime
6457 + ViewConfiguration.getLongPressTimeout())
6458 <= curTime) {
6459 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6460 curTime, keyRepeatCount,
6461 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6462 downTime = 0;
6463 } else {
6464 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6465 curTime, keyRepeatCount);
6466 }
6467 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006469 } else {
6470 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006472 lastKeyTime = curTime;
6473 nextKeyTime = curTime + LONG_WAIT;
6474 }
Romain Guy06882f82009-06-10 13:36:04 -07006475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006476 } catch (Exception e) {
6477 Log.e(TAG,
6478 "Input thread received uncaught exception: " + e, e);
6479 }
6480 }
6481 }
6482 }
6483
6484 // -------------------------------------------------------------
6485 // Client Session State
6486 // -------------------------------------------------------------
6487
6488 private final class Session extends IWindowSession.Stub
6489 implements IBinder.DeathRecipient {
6490 final IInputMethodClient mClient;
6491 final IInputContext mInputContext;
6492 final int mUid;
6493 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006494 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 SurfaceSession mSurfaceSession;
6496 int mNumWindow = 0;
6497 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 /**
6500 * Current pointer move event being dispatched to client window... must
6501 * hold key lock to access.
6502 */
6503 QueuedEvent mPendingPointerMove;
6504 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006506 /**
6507 * Current trackball move event being dispatched to client window... must
6508 * hold key lock to access.
6509 */
6510 QueuedEvent mPendingTrackballMove;
6511 WindowState mPendingTrackballWindow;
6512
6513 public Session(IInputMethodClient client, IInputContext inputContext) {
6514 mClient = client;
6515 mInputContext = inputContext;
6516 mUid = Binder.getCallingUid();
6517 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006518 StringBuilder sb = new StringBuilder();
6519 sb.append("Session{");
6520 sb.append(Integer.toHexString(System.identityHashCode(this)));
6521 sb.append(" uid ");
6522 sb.append(mUid);
6523 sb.append("}");
6524 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006526 synchronized (mWindowMap) {
6527 if (mInputMethodManager == null && mHaveInputMethods) {
6528 IBinder b = ServiceManager.getService(
6529 Context.INPUT_METHOD_SERVICE);
6530 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6531 }
6532 }
6533 long ident = Binder.clearCallingIdentity();
6534 try {
6535 // Note: it is safe to call in to the input method manager
6536 // here because we are not holding our lock.
6537 if (mInputMethodManager != null) {
6538 mInputMethodManager.addClient(client, inputContext,
6539 mUid, mPid);
6540 } else {
6541 client.setUsingInputMethod(false);
6542 }
6543 client.asBinder().linkToDeath(this, 0);
6544 } catch (RemoteException e) {
6545 // The caller has died, so we can just forget about this.
6546 try {
6547 if (mInputMethodManager != null) {
6548 mInputMethodManager.removeClient(client);
6549 }
6550 } catch (RemoteException ee) {
6551 }
6552 } finally {
6553 Binder.restoreCallingIdentity(ident);
6554 }
6555 }
Romain Guy06882f82009-06-10 13:36:04 -07006556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006557 @Override
6558 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6559 throws RemoteException {
6560 try {
6561 return super.onTransact(code, data, reply, flags);
6562 } catch (RuntimeException e) {
6563 // Log all 'real' exceptions thrown to the caller
6564 if (!(e instanceof SecurityException)) {
6565 Log.e(TAG, "Window Session Crash", e);
6566 }
6567 throw e;
6568 }
6569 }
6570
6571 public void binderDied() {
6572 // Note: it is safe to call in to the input method manager
6573 // here because we are not holding our lock.
6574 try {
6575 if (mInputMethodManager != null) {
6576 mInputMethodManager.removeClient(mClient);
6577 }
6578 } catch (RemoteException e) {
6579 }
6580 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006581 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 mClientDead = true;
6583 killSessionLocked();
6584 }
6585 }
6586
6587 public int add(IWindow window, WindowManager.LayoutParams attrs,
6588 int viewVisibility, Rect outContentInsets) {
6589 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6590 }
Romain Guy06882f82009-06-10 13:36:04 -07006591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006592 public void remove(IWindow window) {
6593 removeWindow(this, window);
6594 }
Romain Guy06882f82009-06-10 13:36:04 -07006595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006596 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6597 int requestedWidth, int requestedHeight, int viewFlags,
6598 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6599 Rect outVisibleInsets, Surface outSurface) {
6600 return relayoutWindow(this, window, attrs,
6601 requestedWidth, requestedHeight, viewFlags, insetsPending,
6602 outFrame, outContentInsets, outVisibleInsets, outSurface);
6603 }
Romain Guy06882f82009-06-10 13:36:04 -07006604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006605 public void setTransparentRegion(IWindow window, Region region) {
6606 setTransparentRegionWindow(this, window, region);
6607 }
Romain Guy06882f82009-06-10 13:36:04 -07006608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006609 public void setInsets(IWindow window, int touchableInsets,
6610 Rect contentInsets, Rect visibleInsets) {
6611 setInsetsWindow(this, window, touchableInsets, contentInsets,
6612 visibleInsets);
6613 }
Romain Guy06882f82009-06-10 13:36:04 -07006614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6616 getWindowDisplayFrame(this, window, outDisplayFrame);
6617 }
Romain Guy06882f82009-06-10 13:36:04 -07006618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006619 public void finishDrawing(IWindow window) {
6620 if (localLOGV) Log.v(
6621 TAG, "IWindow finishDrawing called for " + window);
6622 finishDrawingWindow(this, window);
6623 }
6624
6625 public void finishKey(IWindow window) {
6626 if (localLOGV) Log.v(
6627 TAG, "IWindow finishKey called for " + window);
6628 mKeyWaiter.finishedKey(this, window, false,
6629 KeyWaiter.RETURN_NOTHING);
6630 }
6631
6632 public MotionEvent getPendingPointerMove(IWindow window) {
6633 if (localLOGV) Log.v(
6634 TAG, "IWindow getPendingMotionEvent called for " + window);
6635 return mKeyWaiter.finishedKey(this, window, false,
6636 KeyWaiter.RETURN_PENDING_POINTER);
6637 }
Romain Guy06882f82009-06-10 13:36:04 -07006638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006639 public MotionEvent getPendingTrackballMove(IWindow window) {
6640 if (localLOGV) Log.v(
6641 TAG, "IWindow getPendingMotionEvent called for " + window);
6642 return mKeyWaiter.finishedKey(this, window, false,
6643 KeyWaiter.RETURN_PENDING_TRACKBALL);
6644 }
6645
6646 public void setInTouchMode(boolean mode) {
6647 synchronized(mWindowMap) {
6648 mInTouchMode = mode;
6649 }
6650 }
6651
6652 public boolean getInTouchMode() {
6653 synchronized(mWindowMap) {
6654 return mInTouchMode;
6655 }
6656 }
6657
6658 public boolean performHapticFeedback(IWindow window, int effectId,
6659 boolean always) {
6660 synchronized(mWindowMap) {
6661 long ident = Binder.clearCallingIdentity();
6662 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006663 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 windowForClientLocked(this, window), effectId, always);
6665 } finally {
6666 Binder.restoreCallingIdentity(ident);
6667 }
6668 }
6669 }
Romain Guy06882f82009-06-10 13:36:04 -07006670
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006671 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006672 synchronized(mWindowMap) {
6673 long ident = Binder.clearCallingIdentity();
6674 try {
6675 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006676 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006677 } finally {
6678 Binder.restoreCallingIdentity(ident);
6679 }
6680 }
6681 }
6682
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006683 public void wallpaperOffsetsComplete(IBinder window) {
6684 WindowManagerService.this.wallpaperOffsetsComplete(window);
6685 }
6686
Dianne Hackborn75804932009-10-20 20:15:20 -07006687 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6688 int z, Bundle extras, boolean sync) {
6689 synchronized(mWindowMap) {
6690 long ident = Binder.clearCallingIdentity();
6691 try {
6692 return sendWindowWallpaperCommandLocked(
6693 windowForClientLocked(this, window),
6694 action, x, y, z, extras, sync);
6695 } finally {
6696 Binder.restoreCallingIdentity(ident);
6697 }
6698 }
6699 }
6700
6701 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6702 WindowManagerService.this.wallpaperCommandComplete(window, result);
6703 }
6704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006705 void windowAddedLocked() {
6706 if (mSurfaceSession == null) {
6707 if (localLOGV) Log.v(
6708 TAG, "First window added to " + this + ", creating SurfaceSession");
6709 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006710 if (SHOW_TRANSACTIONS) Log.i(
6711 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006712 mSessions.add(this);
6713 }
6714 mNumWindow++;
6715 }
6716
6717 void windowRemovedLocked() {
6718 mNumWindow--;
6719 killSessionLocked();
6720 }
Romain Guy06882f82009-06-10 13:36:04 -07006721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006722 void killSessionLocked() {
6723 if (mNumWindow <= 0 && mClientDead) {
6724 mSessions.remove(this);
6725 if (mSurfaceSession != null) {
6726 if (localLOGV) Log.v(
6727 TAG, "Last window removed from " + this
6728 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006729 if (SHOW_TRANSACTIONS) Log.i(
6730 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006731 try {
6732 mSurfaceSession.kill();
6733 } catch (Exception e) {
6734 Log.w(TAG, "Exception thrown when killing surface session "
6735 + mSurfaceSession + " in session " + this
6736 + ": " + e.toString());
6737 }
6738 mSurfaceSession = null;
6739 }
6740 }
6741 }
Romain Guy06882f82009-06-10 13:36:04 -07006742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006743 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006744 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6745 pw.print(" mClientDead="); pw.print(mClientDead);
6746 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6747 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6748 pw.print(prefix);
6749 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6750 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6751 }
6752 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6753 pw.print(prefix);
6754 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6755 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006757 }
6758
6759 @Override
6760 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006761 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 }
6763 }
6764
6765 // -------------------------------------------------------------
6766 // Client Window State
6767 // -------------------------------------------------------------
6768
6769 private final class WindowState implements WindowManagerPolicy.WindowState {
6770 final Session mSession;
6771 final IWindow mClient;
6772 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006773 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006774 AppWindowToken mAppToken;
6775 AppWindowToken mTargetAppToken;
6776 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6777 final DeathRecipient mDeathRecipient;
6778 final WindowState mAttachedWindow;
6779 final ArrayList mChildWindows = new ArrayList();
6780 final int mBaseLayer;
6781 final int mSubLayer;
6782 final boolean mLayoutAttached;
6783 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006784 final boolean mIsWallpaper;
6785 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006786 int mViewVisibility;
6787 boolean mPolicyVisibility = true;
6788 boolean mPolicyVisibilityAfterAnim = true;
6789 boolean mAppFreezing;
6790 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006791 boolean mReportDestroySurface;
6792 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 boolean mAttachedHidden; // is our parent window hidden?
6794 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006795 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 int mRequestedWidth;
6797 int mRequestedHeight;
6798 int mLastRequestedWidth;
6799 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006800 int mLayer;
6801 int mAnimLayer;
6802 int mLastLayer;
6803 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006804 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006805 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006806
6807 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006809 // Actual frame shown on-screen (may be modified by animation)
6810 final Rect mShownFrame = new Rect();
6811 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006813 /**
6814 * Insets that determine the actually visible area
6815 */
6816 final Rect mVisibleInsets = new Rect();
6817 final Rect mLastVisibleInsets = new Rect();
6818 boolean mVisibleInsetsChanged;
6819
6820 /**
6821 * Insets that are covered by system windows
6822 */
6823 final Rect mContentInsets = new Rect();
6824 final Rect mLastContentInsets = new Rect();
6825 boolean mContentInsetsChanged;
6826
6827 /**
6828 * Set to true if we are waiting for this window to receive its
6829 * given internal insets before laying out other windows based on it.
6830 */
6831 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 /**
6834 * These are the content insets that were given during layout for
6835 * this window, to be applied to windows behind it.
6836 */
6837 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839 /**
6840 * These are the visible insets that were given during layout for
6841 * this window, to be applied to windows behind it.
6842 */
6843 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006845 /**
6846 * Flag indicating whether the touchable region should be adjusted by
6847 * the visible insets; if false the area outside the visible insets is
6848 * NOT touchable, so we must use those to adjust the frame during hit
6849 * tests.
6850 */
6851 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853 // Current transformation being applied.
6854 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6855 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6856 float mHScale=1, mVScale=1;
6857 float mLastHScale=1, mLastVScale=1;
6858 final Matrix mTmpMatrix = new Matrix();
6859
6860 // "Real" frame that the application sees.
6861 final Rect mFrame = new Rect();
6862 final Rect mLastFrame = new Rect();
6863
6864 final Rect mContainingFrame = new Rect();
6865 final Rect mDisplayFrame = new Rect();
6866 final Rect mContentFrame = new Rect();
6867 final Rect mVisibleFrame = new Rect();
6868
6869 float mShownAlpha = 1;
6870 float mAlpha = 1;
6871 float mLastAlpha = 1;
6872
6873 // Set to true if, when the window gets displayed, it should perform
6874 // an enter animation.
6875 boolean mEnterAnimationPending;
6876
6877 // Currently running animation.
6878 boolean mAnimating;
6879 boolean mLocalAnimating;
6880 Animation mAnimation;
6881 boolean mAnimationIsEntrance;
6882 boolean mHasTransformation;
6883 boolean mHasLocalTransformation;
6884 final Transformation mTransformation = new Transformation();
6885
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006886 // If a window showing a wallpaper: the requested offset for the
6887 // wallpaper; if a wallpaper window: the currently applied offset.
6888 float mWallpaperX = -1;
6889 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006890
6891 // If a window showing a wallpaper: what fraction of the offset
6892 // range corresponds to a full virtual screen.
6893 float mWallpaperXStep = -1;
6894 float mWallpaperYStep = -1;
6895
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006896 // Wallpaper windows: pixels offset based on above variables.
6897 int mXOffset;
6898 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006900 // This is set after IWindowSession.relayout() has been called at
6901 // least once for the window. It allows us to detect the situation
6902 // where we don't yet have a surface, but should have one soon, so
6903 // we can give the window focus before waiting for the relayout.
6904 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 // This is set after the Surface has been created but before the
6907 // window has been drawn. During this time the surface is hidden.
6908 boolean mDrawPending;
6909
6910 // This is set after the window has finished drawing for the first
6911 // time but before its surface is shown. The surface will be
6912 // displayed when the next layout is run.
6913 boolean mCommitDrawPending;
6914
6915 // This is set during the time after the window's drawing has been
6916 // committed, and before its surface is actually shown. It is used
6917 // to delay showing the surface until all windows in a token are ready
6918 // to be shown.
6919 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006921 // Set when the window has been shown in the screen the first time.
6922 boolean mHasDrawn;
6923
6924 // Currently running an exit animation?
6925 boolean mExiting;
6926
6927 // Currently on the mDestroySurface list?
6928 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006930 // Completely remove from window manager after exit animation?
6931 boolean mRemoveOnExit;
6932
6933 // Set when the orientation is changing and this window has not yet
6934 // been updated for the new orientation.
6935 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006937 // Is this window now (or just being) removed?
6938 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006940 WindowState(Session s, IWindow c, WindowToken token,
6941 WindowState attachedWindow, WindowManager.LayoutParams a,
6942 int viewVisibility) {
6943 mSession = s;
6944 mClient = c;
6945 mToken = token;
6946 mAttrs.copyFrom(a);
6947 mViewVisibility = viewVisibility;
6948 DeathRecipient deathRecipient = new DeathRecipient();
6949 mAlpha = a.alpha;
6950 if (localLOGV) Log.v(
6951 TAG, "Window " + this + " client=" + c.asBinder()
6952 + " token=" + token + " (" + mAttrs.token + ")");
6953 try {
6954 c.asBinder().linkToDeath(deathRecipient, 0);
6955 } catch (RemoteException e) {
6956 mDeathRecipient = null;
6957 mAttachedWindow = null;
6958 mLayoutAttached = false;
6959 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006960 mIsWallpaper = false;
6961 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 mBaseLayer = 0;
6963 mSubLayer = 0;
6964 return;
6965 }
6966 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6969 mAttrs.type <= LAST_SUB_WINDOW)) {
6970 // The multiplier here is to reserve space for multiple
6971 // windows in the same type layer.
6972 mBaseLayer = mPolicy.windowTypeToLayerLw(
6973 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6974 + TYPE_LAYER_OFFSET;
6975 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6976 mAttachedWindow = attachedWindow;
6977 mAttachedWindow.mChildWindows.add(this);
6978 mLayoutAttached = mAttrs.type !=
6979 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6980 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6981 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006982 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6983 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 } else {
6985 // The multiplier here is to reserve space for multiple
6986 // windows in the same type layer.
6987 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6988 * TYPE_LAYER_MULTIPLIER
6989 + TYPE_LAYER_OFFSET;
6990 mSubLayer = 0;
6991 mAttachedWindow = null;
6992 mLayoutAttached = false;
6993 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6994 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006995 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6996 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006997 }
6998
6999 WindowState appWin = this;
7000 while (appWin.mAttachedWindow != null) {
7001 appWin = mAttachedWindow;
7002 }
7003 WindowToken appToken = appWin.mToken;
7004 while (appToken.appWindowToken == null) {
7005 WindowToken parent = mTokenMap.get(appToken.token);
7006 if (parent == null || appToken == parent) {
7007 break;
7008 }
7009 appToken = parent;
7010 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007011 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 mAppToken = appToken.appWindowToken;
7013
7014 mSurface = null;
7015 mRequestedWidth = 0;
7016 mRequestedHeight = 0;
7017 mLastRequestedWidth = 0;
7018 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007019 mXOffset = 0;
7020 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007021 mLayer = 0;
7022 mAnimLayer = 0;
7023 mLastLayer = 0;
7024 }
7025
7026 void attach() {
7027 if (localLOGV) Log.v(
7028 TAG, "Attaching " + this + " token=" + mToken
7029 + ", list=" + mToken.windows);
7030 mSession.windowAddedLocked();
7031 }
7032
7033 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7034 mHaveFrame = true;
7035
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007036 final Rect container = mContainingFrame;
7037 container.set(pf);
7038
7039 final Rect display = mDisplayFrame;
7040 display.set(df);
7041
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007042 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007043 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007044 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7045 display.intersect(mCompatibleScreenFrame);
7046 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007047 }
7048
7049 final int pw = container.right - container.left;
7050 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007051
7052 int w,h;
7053 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7054 w = mAttrs.width < 0 ? pw : mAttrs.width;
7055 h = mAttrs.height< 0 ? ph : mAttrs.height;
7056 } else {
7057 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
7058 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
7059 }
Romain Guy06882f82009-06-10 13:36:04 -07007060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007061 final Rect content = mContentFrame;
7062 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 final Rect visible = mVisibleFrame;
7065 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007068 final int fw = frame.width();
7069 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007071 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7072 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7073
7074 Gravity.apply(mAttrs.gravity, w, h, container,
7075 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7076 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7077
7078 //System.out.println("Out: " + mFrame);
7079
7080 // Now make sure the window fits in the overall display.
7081 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 // Make sure the content and visible frames are inside of the
7084 // final window frame.
7085 if (content.left < frame.left) content.left = frame.left;
7086 if (content.top < frame.top) content.top = frame.top;
7087 if (content.right > frame.right) content.right = frame.right;
7088 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7089 if (visible.left < frame.left) visible.left = frame.left;
7090 if (visible.top < frame.top) visible.top = frame.top;
7091 if (visible.right > frame.right) visible.right = frame.right;
7092 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007094 final Rect contentInsets = mContentInsets;
7095 contentInsets.left = content.left-frame.left;
7096 contentInsets.top = content.top-frame.top;
7097 contentInsets.right = frame.right-content.right;
7098 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 final Rect visibleInsets = mVisibleInsets;
7101 visibleInsets.left = visible.left-frame.left;
7102 visibleInsets.top = visible.top-frame.top;
7103 visibleInsets.right = frame.right-visible.right;
7104 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007105
Dianne Hackborn284ac932009-08-28 10:34:25 -07007106 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7107 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007108 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007109 }
7110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 if (localLOGV) {
7112 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7113 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7114 Log.v(TAG, "Resolving (mRequestedWidth="
7115 + mRequestedWidth + ", mRequestedheight="
7116 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7117 + "): frame=" + mFrame.toShortString()
7118 + " ci=" + contentInsets.toShortString()
7119 + " vi=" + visibleInsets.toShortString());
7120 //}
7121 }
7122 }
Romain Guy06882f82009-06-10 13:36:04 -07007123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007124 public Rect getFrameLw() {
7125 return mFrame;
7126 }
7127
7128 public Rect getShownFrameLw() {
7129 return mShownFrame;
7130 }
7131
7132 public Rect getDisplayFrameLw() {
7133 return mDisplayFrame;
7134 }
7135
7136 public Rect getContentFrameLw() {
7137 return mContentFrame;
7138 }
7139
7140 public Rect getVisibleFrameLw() {
7141 return mVisibleFrame;
7142 }
7143
7144 public boolean getGivenInsetsPendingLw() {
7145 return mGivenInsetsPending;
7146 }
7147
7148 public Rect getGivenContentInsetsLw() {
7149 return mGivenContentInsets;
7150 }
Romain Guy06882f82009-06-10 13:36:04 -07007151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007152 public Rect getGivenVisibleInsetsLw() {
7153 return mGivenVisibleInsets;
7154 }
Romain Guy06882f82009-06-10 13:36:04 -07007155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 public WindowManager.LayoutParams getAttrs() {
7157 return mAttrs;
7158 }
7159
7160 public int getSurfaceLayer() {
7161 return mLayer;
7162 }
Romain Guy06882f82009-06-10 13:36:04 -07007163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007164 public IApplicationToken getAppToken() {
7165 return mAppToken != null ? mAppToken.appToken : null;
7166 }
7167
7168 public boolean hasAppShownWindows() {
7169 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7170 }
7171
7172 public boolean hasAppStartingIcon() {
7173 return mAppToken != null ? (mAppToken.startingData != null) : false;
7174 }
7175
7176 public WindowManagerPolicy.WindowState getAppStartingWindow() {
7177 return mAppToken != null ? mAppToken.startingWindow : null;
7178 }
7179
7180 public void setAnimation(Animation anim) {
7181 if (localLOGV) Log.v(
7182 TAG, "Setting animation in " + this + ": " + anim);
7183 mAnimating = false;
7184 mLocalAnimating = false;
7185 mAnimation = anim;
7186 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7187 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7188 }
7189
7190 public void clearAnimation() {
7191 if (mAnimation != null) {
7192 mAnimating = true;
7193 mLocalAnimating = false;
7194 mAnimation = null;
7195 }
7196 }
Romain Guy06882f82009-06-10 13:36:04 -07007197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007198 Surface createSurfaceLocked() {
7199 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007200 mReportDestroySurface = false;
7201 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007202 mDrawPending = true;
7203 mCommitDrawPending = false;
7204 mReadyToShow = false;
7205 if (mAppToken != null) {
7206 mAppToken.allDrawn = false;
7207 }
7208
7209 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007210 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007211 flags |= Surface.PUSH_BUFFERS;
7212 }
7213
7214 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7215 flags |= Surface.SECURE;
7216 }
7217 if (DEBUG_VISIBILITY) Log.v(
7218 TAG, "Creating surface in session "
7219 + mSession.mSurfaceSession + " window " + this
7220 + " w=" + mFrame.width()
7221 + " h=" + mFrame.height() + " format="
7222 + mAttrs.format + " flags=" + flags);
7223
7224 int w = mFrame.width();
7225 int h = mFrame.height();
7226 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7227 // for a scaled surface, we always want the requested
7228 // size.
7229 w = mRequestedWidth;
7230 h = mRequestedHeight;
7231 }
7232
Romain Guy9825ec62009-10-01 00:58:09 -07007233 // Something is wrong and SurfaceFlinger will not like this,
7234 // try to revert to sane values
7235 if (w <= 0) w = 1;
7236 if (h <= 0) h = 1;
7237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007238 try {
7239 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007240 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007242 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7243 + mSurface + " IN SESSION "
7244 + mSession.mSurfaceSession
7245 + ": pid=" + mSession.mPid + " format="
7246 + mAttrs.format + " flags=0x"
7247 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 } catch (Surface.OutOfResourcesException e) {
7249 Log.w(TAG, "OutOfResourcesException creating surface");
7250 reclaimSomeSurfaceMemoryLocked(this, "create");
7251 return null;
7252 } catch (Exception e) {
7253 Log.e(TAG, "Exception creating surface", e);
7254 return null;
7255 }
Romain Guy06882f82009-06-10 13:36:04 -07007256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 if (localLOGV) Log.v(
7258 TAG, "Got surface: " + mSurface
7259 + ", set left=" + mFrame.left + " top=" + mFrame.top
7260 + ", animLayer=" + mAnimLayer);
7261 if (SHOW_TRANSACTIONS) {
7262 Log.i(TAG, ">>> OPEN TRANSACTION");
7263 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7264 + mAttrs.getTitle() + ") pos=(" +
7265 mFrame.left + "," + mFrame.top + ") (" +
7266 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7267 mAnimLayer + " HIDE");
7268 }
7269 Surface.openTransaction();
7270 try {
7271 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007272 mSurface.setPosition(mFrame.left + mXOffset,
7273 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007274 mSurface.setLayer(mAnimLayer);
7275 mSurface.hide();
7276 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007277 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7278 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 mSurface.setFlags(Surface.SURFACE_DITHER,
7280 Surface.SURFACE_DITHER);
7281 }
7282 } catch (RuntimeException e) {
7283 Log.w(TAG, "Error creating surface in " + w, e);
7284 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7285 }
7286 mLastHidden = true;
7287 } finally {
7288 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7289 Surface.closeTransaction();
7290 }
7291 if (localLOGV) Log.v(
7292 TAG, "Created surface " + this);
7293 }
7294 return mSurface;
7295 }
Romain Guy06882f82009-06-10 13:36:04 -07007296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007297 void destroySurfaceLocked() {
7298 // Window is no longer on-screen, so can no longer receive
7299 // key events... if we were waiting for it to finish
7300 // handling a key event, the wait is over!
7301 mKeyWaiter.finishedKey(mSession, mClient, true,
7302 KeyWaiter.RETURN_NOTHING);
7303 mKeyWaiter.releasePendingPointerLocked(mSession);
7304 mKeyWaiter.releasePendingTrackballLocked(mSession);
7305
7306 if (mAppToken != null && this == mAppToken.startingWindow) {
7307 mAppToken.startingDisplayed = false;
7308 }
Romain Guy06882f82009-06-10 13:36:04 -07007309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007310 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007311 mDrawPending = false;
7312 mCommitDrawPending = false;
7313 mReadyToShow = false;
7314
7315 int i = mChildWindows.size();
7316 while (i > 0) {
7317 i--;
7318 WindowState c = (WindowState)mChildWindows.get(i);
7319 c.mAttachedHidden = true;
7320 }
7321
7322 if (mReportDestroySurface) {
7323 mReportDestroySurface = false;
7324 mSurfacePendingDestroy = true;
7325 try {
7326 mClient.dispatchGetNewSurface();
7327 // We'll really destroy on the next time around.
7328 return;
7329 } catch (RemoteException e) {
7330 }
7331 }
7332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007333 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007334 if (DEBUG_VISIBILITY) {
7335 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007336 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007337 Log.w(TAG, "Window " + this + " destroying surface "
7338 + mSurface + ", session " + mSession, e);
7339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007340 if (SHOW_TRANSACTIONS) {
7341 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007342 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007343 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7344 + mAttrs.getTitle() + ")", ex);
7345 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007346 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007347 } catch (RuntimeException e) {
7348 Log.w(TAG, "Exception thrown when destroying Window " + this
7349 + " surface " + mSurface + " session " + mSession
7350 + ": " + e.toString());
7351 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007353 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007354 }
7355 }
7356
7357 boolean finishDrawingLocked() {
7358 if (mDrawPending) {
7359 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7360 TAG, "finishDrawingLocked: " + mSurface);
7361 mCommitDrawPending = true;
7362 mDrawPending = false;
7363 return true;
7364 }
7365 return false;
7366 }
7367
7368 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007369 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7371 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007372 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007373 }
7374 mCommitDrawPending = false;
7375 mReadyToShow = true;
7376 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7377 final AppWindowToken atoken = mAppToken;
7378 if (atoken == null || atoken.allDrawn || starting) {
7379 performShowLocked();
7380 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007381 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007382 }
7383
7384 // This must be called while inside a transaction.
7385 boolean performShowLocked() {
7386 if (DEBUG_VISIBILITY) {
7387 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007388 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389 Log.v(TAG, "performShow on " + this
7390 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7391 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7392 }
7393 if (mReadyToShow && isReadyForDisplay()) {
7394 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7395 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7396 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7397 + " during animation: policyVis=" + mPolicyVisibility
7398 + " attHidden=" + mAttachedHidden
7399 + " tok.hiddenRequested="
7400 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007401 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 + (mAppToken != null ? mAppToken.hidden : false)
7403 + " animating=" + mAnimating
7404 + " tok animating="
7405 + (mAppToken != null ? mAppToken.animating : false));
7406 if (!showSurfaceRobustlyLocked(this)) {
7407 return false;
7408 }
7409 mLastAlpha = -1;
7410 mHasDrawn = true;
7411 mLastHidden = false;
7412 mReadyToShow = false;
7413 enableScreenIfNeededLocked();
7414
7415 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007417 int i = mChildWindows.size();
7418 while (i > 0) {
7419 i--;
7420 WindowState c = (WindowState)mChildWindows.get(i);
7421 if (c.mSurface != null && c.mAttachedHidden) {
7422 c.mAttachedHidden = false;
7423 c.performShowLocked();
7424 }
7425 }
Romain Guy06882f82009-06-10 13:36:04 -07007426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007427 if (mAttrs.type != TYPE_APPLICATION_STARTING
7428 && mAppToken != null) {
7429 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007430
7431 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007432 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7433 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007434 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007435 // If this initial window is animating, stop it -- we
7436 // will do an animation to reveal it from behind the
7437 // starting window, so there is no need for it to also
7438 // be doing its own stuff.
7439 if (mAnimation != null) {
7440 mAnimation = null;
7441 // Make sure we clean up the animation.
7442 mAnimating = true;
7443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007444 mFinishedStarting.add(mAppToken);
7445 mH.sendEmptyMessage(H.FINISHED_STARTING);
7446 }
7447 mAppToken.updateReportedVisibilityLocked();
7448 }
7449 }
7450 return true;
7451 }
Romain Guy06882f82009-06-10 13:36:04 -07007452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007453 // This must be called while inside a transaction. Returns true if
7454 // there is more animation to run.
7455 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7456 if (!mDisplayFrozen) {
7457 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007459 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7460 mHasTransformation = true;
7461 mHasLocalTransformation = true;
7462 if (!mLocalAnimating) {
7463 if (DEBUG_ANIM) Log.v(
7464 TAG, "Starting animation in " + this +
7465 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7466 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7467 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7468 mAnimation.setStartTime(currentTime);
7469 mLocalAnimating = true;
7470 mAnimating = true;
7471 }
7472 mTransformation.clear();
7473 final boolean more = mAnimation.getTransformation(
7474 currentTime, mTransformation);
7475 if (DEBUG_ANIM) Log.v(
7476 TAG, "Stepped animation in " + this +
7477 ": more=" + more + ", xform=" + mTransformation);
7478 if (more) {
7479 // we're not done!
7480 return true;
7481 }
7482 if (DEBUG_ANIM) Log.v(
7483 TAG, "Finished animation in " + this +
7484 " @ " + currentTime);
7485 mAnimation = null;
7486 //WindowManagerService.this.dump();
7487 }
7488 mHasLocalTransformation = false;
7489 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007490 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007491 // When our app token is animating, we kind-of pretend like
7492 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7493 // part of this check means that we will only do this if
7494 // our window is not currently exiting, or it is not
7495 // locally animating itself. The idea being that one that
7496 // is exiting and doing a local animation should be removed
7497 // once that animation is done.
7498 mAnimating = true;
7499 mHasTransformation = true;
7500 mTransformation.clear();
7501 return false;
7502 } else if (mHasTransformation) {
7503 // Little trick to get through the path below to act like
7504 // we have finished an animation.
7505 mAnimating = true;
7506 } else if (isAnimating()) {
7507 mAnimating = true;
7508 }
7509 } else if (mAnimation != null) {
7510 // If the display is frozen, and there is a pending animation,
7511 // clear it and make sure we run the cleanup code.
7512 mAnimating = true;
7513 mLocalAnimating = true;
7514 mAnimation = null;
7515 }
Romain Guy06882f82009-06-10 13:36:04 -07007516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007517 if (!mAnimating && !mLocalAnimating) {
7518 return false;
7519 }
7520
7521 if (DEBUG_ANIM) Log.v(
7522 TAG, "Animation done in " + this + ": exiting=" + mExiting
7523 + ", reportedVisible="
7524 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007526 mAnimating = false;
7527 mLocalAnimating = false;
7528 mAnimation = null;
7529 mAnimLayer = mLayer;
7530 if (mIsImWindow) {
7531 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007532 } else if (mIsWallpaper) {
7533 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 }
7535 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7536 + " anim layer: " + mAnimLayer);
7537 mHasTransformation = false;
7538 mHasLocalTransformation = false;
7539 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7540 mTransformation.clear();
7541 if (mHasDrawn
7542 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7543 && mAppToken != null
7544 && mAppToken.firstWindowDrawn
7545 && mAppToken.startingData != null) {
7546 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7547 + mToken + ": first real window done animating");
7548 mFinishedStarting.add(mAppToken);
7549 mH.sendEmptyMessage(H.FINISHED_STARTING);
7550 }
Romain Guy06882f82009-06-10 13:36:04 -07007551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007552 finishExit();
7553
7554 if (mAppToken != null) {
7555 mAppToken.updateReportedVisibilityLocked();
7556 }
7557
7558 return false;
7559 }
7560
7561 void finishExit() {
7562 if (DEBUG_ANIM) Log.v(
7563 TAG, "finishExit in " + this
7564 + ": exiting=" + mExiting
7565 + " remove=" + mRemoveOnExit
7566 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 final int N = mChildWindows.size();
7569 for (int i=0; i<N; i++) {
7570 ((WindowState)mChildWindows.get(i)).finishExit();
7571 }
Romain Guy06882f82009-06-10 13:36:04 -07007572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007573 if (!mExiting) {
7574 return;
7575 }
Romain Guy06882f82009-06-10 13:36:04 -07007576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 if (isWindowAnimating()) {
7578 return;
7579 }
7580
7581 if (localLOGV) Log.v(
7582 TAG, "Exit animation finished in " + this
7583 + ": remove=" + mRemoveOnExit);
7584 if (mSurface != null) {
7585 mDestroySurface.add(this);
7586 mDestroying = true;
7587 if (SHOW_TRANSACTIONS) Log.i(
7588 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7589 try {
7590 mSurface.hide();
7591 } catch (RuntimeException e) {
7592 Log.w(TAG, "Error hiding surface in " + this, e);
7593 }
7594 mLastHidden = true;
7595 mKeyWaiter.releasePendingPointerLocked(mSession);
7596 }
7597 mExiting = false;
7598 if (mRemoveOnExit) {
7599 mPendingRemove.add(this);
7600 mRemoveOnExit = false;
7601 }
7602 }
Romain Guy06882f82009-06-10 13:36:04 -07007603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007604 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7605 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7606 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7607 if (dtdx < -.000001f || dtdx > .000001f) return false;
7608 if (dsdy < -.000001f || dsdy > .000001f) return false;
7609 return true;
7610 }
Romain Guy06882f82009-06-10 13:36:04 -07007611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 void computeShownFrameLocked() {
7613 final boolean selfTransformation = mHasLocalTransformation;
7614 Transformation attachedTransformation =
7615 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7616 ? mAttachedWindow.mTransformation : null;
7617 Transformation appTransformation =
7618 (mAppToken != null && mAppToken.hasTransformation)
7619 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007620
7621 // Wallpapers are animated based on the "real" window they
7622 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007623 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007624 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007625 if (mWallpaperTarget.mHasLocalTransformation &&
7626 mWallpaperTarget.mAnimation != null &&
7627 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007628 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007629 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7630 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7631 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007632 }
7633 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007634 mWallpaperTarget.mAppToken.hasTransformation &&
7635 mWallpaperTarget.mAppToken.animation != null &&
7636 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007637 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007638 if (DEBUG_WALLPAPER && appTransformation != null) {
7639 Log.v(TAG, "WP target app xform: " + appTransformation);
7640 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007641 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007642 }
7643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007644 if (selfTransformation || attachedTransformation != null
7645 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007646 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007647 final Rect frame = mFrame;
7648 final float tmpFloats[] = mTmpFloats;
7649 final Matrix tmpMatrix = mTmpMatrix;
7650
7651 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007652 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007654 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007655 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007656 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007658 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007659 }
7660 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007661 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007662 }
7663
7664 // "convert" it into SurfaceFlinger's format
7665 // (a 2x2 matrix + an offset)
7666 // Here we must not transform the position of the surface
7667 // since it is already included in the transformation.
7668 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007670 tmpMatrix.getValues(tmpFloats);
7671 mDsDx = tmpFloats[Matrix.MSCALE_X];
7672 mDtDx = tmpFloats[Matrix.MSKEW_X];
7673 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7674 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007675 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7676 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 int w = frame.width();
7678 int h = frame.height();
7679 mShownFrame.set(x, y, x+w, y+h);
7680
7681 // Now set the alpha... but because our current hardware
7682 // can't do alpha transformation on a non-opaque surface,
7683 // turn it off if we are running an animation that is also
7684 // transforming since it is more important to have that
7685 // animation be smooth.
7686 mShownAlpha = mAlpha;
7687 if (!mLimitedAlphaCompositing
7688 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7689 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7690 && x == frame.left && y == frame.top))) {
7691 //Log.i(TAG, "Applying alpha transform");
7692 if (selfTransformation) {
7693 mShownAlpha *= mTransformation.getAlpha();
7694 }
7695 if (attachedTransformation != null) {
7696 mShownAlpha *= attachedTransformation.getAlpha();
7697 }
7698 if (appTransformation != null) {
7699 mShownAlpha *= appTransformation.getAlpha();
7700 }
7701 } else {
7702 //Log.i(TAG, "Not applying alpha transform");
7703 }
Romain Guy06882f82009-06-10 13:36:04 -07007704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007705 if (localLOGV) Log.v(
7706 TAG, "Continuing animation in " + this +
7707 ": " + mShownFrame +
7708 ", alpha=" + mTransformation.getAlpha());
7709 return;
7710 }
Romain Guy06882f82009-06-10 13:36:04 -07007711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007712 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007713 if (mXOffset != 0 || mYOffset != 0) {
7714 mShownFrame.offset(mXOffset, mYOffset);
7715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716 mShownAlpha = mAlpha;
7717 mDsDx = 1;
7718 mDtDx = 0;
7719 mDsDy = 0;
7720 mDtDy = 1;
7721 }
Romain Guy06882f82009-06-10 13:36:04 -07007722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 /**
7724 * Is this window visible? It is not visible if there is no
7725 * surface, or we are in the process of running an exit animation
7726 * that will remove the surface, or its app token has been hidden.
7727 */
7728 public boolean isVisibleLw() {
7729 final AppWindowToken atoken = mAppToken;
7730 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7731 && (atoken == null || !atoken.hiddenRequested)
7732 && !mExiting && !mDestroying;
7733 }
7734
7735 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007736 * Like {@link #isVisibleLw}, but also counts a window that is currently
7737 * "hidden" behind the keyguard as visible. This allows us to apply
7738 * things like window flags that impact the keyguard.
7739 * XXX I am starting to think we need to have ANOTHER visibility flag
7740 * for this "hidden behind keyguard" state rather than overloading
7741 * mPolicyVisibility. Ungh.
7742 */
7743 public boolean isVisibleOrBehindKeyguardLw() {
7744 final AppWindowToken atoken = mAppToken;
7745 return mSurface != null && !mAttachedHidden
7746 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7747 && !mExiting && !mDestroying;
7748 }
7749
7750 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007751 * Is this window visible, ignoring its app token? It is not visible
7752 * if there is no surface, or we are in the process of running an exit animation
7753 * that will remove the surface.
7754 */
7755 public boolean isWinVisibleLw() {
7756 final AppWindowToken atoken = mAppToken;
7757 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7758 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7759 && !mExiting && !mDestroying;
7760 }
7761
7762 /**
7763 * The same as isVisible(), but follows the current hidden state of
7764 * the associated app token, not the pending requested hidden state.
7765 */
7766 boolean isVisibleNow() {
7767 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007768 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 }
7770
7771 /**
7772 * Same as isVisible(), but we also count it as visible between the
7773 * call to IWindowSession.add() and the first relayout().
7774 */
7775 boolean isVisibleOrAdding() {
7776 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007777 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007778 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7779 && mPolicyVisibility && !mAttachedHidden
7780 && (atoken == null || !atoken.hiddenRequested)
7781 && !mExiting && !mDestroying;
7782 }
7783
7784 /**
7785 * Is this window currently on-screen? It is on-screen either if it
7786 * is visible or it is currently running an animation before no longer
7787 * being visible.
7788 */
7789 boolean isOnScreen() {
7790 final AppWindowToken atoken = mAppToken;
7791 if (atoken != null) {
7792 return mSurface != null && mPolicyVisibility && !mDestroying
7793 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007794 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 } else {
7796 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007797 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007798 }
7799 }
Romain Guy06882f82009-06-10 13:36:04 -07007800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 /**
7802 * Like isOnScreen(), but we don't return true if the window is part
7803 * of a transition that has not yet been started.
7804 */
7805 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007806 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007807 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007808 return false;
7809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007810 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007811 final boolean animating = atoken != null
7812 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007814 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7815 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007816 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 }
7818
7819 /** Is the window or its container currently animating? */
7820 boolean isAnimating() {
7821 final WindowState attached = mAttachedWindow;
7822 final AppWindowToken atoken = mAppToken;
7823 return mAnimation != null
7824 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007825 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 (atoken.animation != null
7827 || atoken.inPendingTransaction));
7828 }
7829
7830 /** Is this window currently animating? */
7831 boolean isWindowAnimating() {
7832 return mAnimation != null;
7833 }
7834
7835 /**
7836 * Like isOnScreen, but returns false if the surface hasn't yet
7837 * been drawn.
7838 */
7839 public boolean isDisplayedLw() {
7840 final AppWindowToken atoken = mAppToken;
7841 return mSurface != null && mPolicyVisibility && !mDestroying
7842 && !mDrawPending && !mCommitDrawPending
7843 && ((!mAttachedHidden &&
7844 (atoken == null || !atoken.hiddenRequested))
7845 || mAnimating);
7846 }
7847
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007848 /**
7849 * Returns true if the window has a surface that it has drawn a
7850 * complete UI in to.
7851 */
7852 public boolean isDrawnLw() {
7853 final AppWindowToken atoken = mAppToken;
7854 return mSurface != null && !mDestroying
7855 && !mDrawPending && !mCommitDrawPending;
7856 }
7857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007858 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7859 boolean shownFrame, boolean onlyOpaque) {
7860 if (mSurface == null) {
7861 return false;
7862 }
7863 if (mAppToken != null && !mAppToken.appFullscreen) {
7864 return false;
7865 }
7866 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7867 return false;
7868 }
7869 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007870
7871 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7872 return frame.left <= mCompatibleScreenFrame.left &&
7873 frame.top <= mCompatibleScreenFrame.top &&
7874 frame.right >= mCompatibleScreenFrame.right &&
7875 frame.bottom >= mCompatibleScreenFrame.bottom;
7876 } else {
7877 return frame.left <= 0 && frame.top <= 0
7878 && frame.right >= screenWidth
7879 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007881 }
Romain Guy06882f82009-06-10 13:36:04 -07007882
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007883 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007884 * Return true if the window is opaque and fully drawn. This indicates
7885 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007886 */
7887 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007888 return (mAttrs.format == PixelFormat.OPAQUE
7889 || mAttrs.type == TYPE_WALLPAPER)
7890 && mSurface != null && mAnimation == null
7891 && (mAppToken == null || mAppToken.animation == null)
7892 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007893 }
7894
7895 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7896 return
7897 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007898 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7899 // only if it's visible
7900 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007901 // and only if the application fills the compatible screen
7902 mFrame.left <= mCompatibleScreenFrame.left &&
7903 mFrame.top <= mCompatibleScreenFrame.top &&
7904 mFrame.right >= mCompatibleScreenFrame.right &&
7905 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007906 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007907 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007908 }
7909
7910 boolean isFullscreen(int screenWidth, int screenHeight) {
7911 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007912 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007913 }
7914
7915 void removeLocked() {
7916 if (mAttachedWindow != null) {
7917 mAttachedWindow.mChildWindows.remove(this);
7918 }
7919 destroySurfaceLocked();
7920 mSession.windowRemovedLocked();
7921 try {
7922 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7923 } catch (RuntimeException e) {
7924 // Ignore if it has already been removed (usually because
7925 // we are doing this as part of processing a death note.)
7926 }
7927 }
7928
7929 private class DeathRecipient implements IBinder.DeathRecipient {
7930 public void binderDied() {
7931 try {
7932 synchronized(mWindowMap) {
7933 WindowState win = windowForClientLocked(mSession, mClient);
7934 Log.i(TAG, "WIN DEATH: " + win);
7935 if (win != null) {
7936 removeWindowLocked(mSession, win);
7937 }
7938 }
7939 } catch (IllegalArgumentException ex) {
7940 // This will happen if the window has already been
7941 // removed.
7942 }
7943 }
7944 }
7945
7946 /** Returns true if this window desires key events. */
7947 public final boolean canReceiveKeys() {
7948 return isVisibleOrAdding()
7949 && (mViewVisibility == View.VISIBLE)
7950 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7951 }
7952
7953 public boolean hasDrawnLw() {
7954 return mHasDrawn;
7955 }
7956
7957 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007958 return showLw(doAnimation, true);
7959 }
7960
7961 boolean showLw(boolean doAnimation, boolean requestAnim) {
7962 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
7963 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007964 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007965 mPolicyVisibility = true;
7966 mPolicyVisibilityAfterAnim = true;
7967 if (doAnimation) {
7968 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7969 }
7970 if (requestAnim) {
7971 requestAnimationLocked(0);
7972 }
7973 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007974 }
7975
7976 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007977 return hideLw(doAnimation, true);
7978 }
7979
7980 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007981 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7982 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007983 if (!current) {
7984 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007985 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007986 if (doAnimation) {
7987 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7988 if (mAnimation == null) {
7989 doAnimation = false;
7990 }
7991 }
7992 if (doAnimation) {
7993 mPolicyVisibilityAfterAnim = false;
7994 } else {
7995 mPolicyVisibilityAfterAnim = false;
7996 mPolicyVisibility = false;
7997 }
7998 if (requestAnim) {
7999 requestAnimationLocked(0);
8000 }
8001 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008002 }
8003
8004 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008005 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07008006
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008007 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8008 pw.print(" mClient="); pw.println(mClient.asBinder());
8009 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8010 if (mAttachedWindow != null || mLayoutAttached) {
8011 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8012 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8013 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008014 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8015 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8016 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008017 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8018 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008019 }
8020 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8021 pw.print(" mSubLayer="); pw.print(mSubLayer);
8022 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8023 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8024 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8025 pw.print("="); pw.print(mAnimLayer);
8026 pw.print(" mLastLayer="); pw.println(mLastLayer);
8027 if (mSurface != null) {
8028 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8029 }
8030 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8031 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8032 if (mAppToken != null) {
8033 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8034 }
8035 if (mTargetAppToken != null) {
8036 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8037 }
8038 pw.print(prefix); pw.print("mViewVisibility=0x");
8039 pw.print(Integer.toHexString(mViewVisibility));
8040 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008041 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8042 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008043 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8044 pw.print(prefix); pw.print("mPolicyVisibility=");
8045 pw.print(mPolicyVisibility);
8046 pw.print(" mPolicyVisibilityAfterAnim=");
8047 pw.print(mPolicyVisibilityAfterAnim);
8048 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8049 }
8050 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008051 pw.print(" h="); pw.println(mRequestedHeight);
8052 if (mXOffset != 0 || mYOffset != 0) {
8053 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8054 pw.print(" y="); pw.println(mYOffset);
8055 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008056 pw.print(prefix); pw.print("mGivenContentInsets=");
8057 mGivenContentInsets.printShortString(pw);
8058 pw.print(" mGivenVisibleInsets=");
8059 mGivenVisibleInsets.printShortString(pw);
8060 pw.println();
8061 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8062 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8063 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8064 }
8065 pw.print(prefix); pw.print("mShownFrame=");
8066 mShownFrame.printShortString(pw);
8067 pw.print(" last="); mLastShownFrame.printShortString(pw);
8068 pw.println();
8069 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8070 pw.print(" last="); mLastFrame.printShortString(pw);
8071 pw.println();
8072 pw.print(prefix); pw.print("mContainingFrame=");
8073 mContainingFrame.printShortString(pw);
8074 pw.print(" mDisplayFrame=");
8075 mDisplayFrame.printShortString(pw);
8076 pw.println();
8077 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8078 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8079 pw.println();
8080 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8081 pw.print(" last="); mLastContentInsets.printShortString(pw);
8082 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8083 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8084 pw.println();
8085 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8086 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8087 pw.print(" mAlpha="); pw.print(mAlpha);
8088 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8089 }
8090 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8091 || mAnimation != null) {
8092 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8093 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8094 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8095 pw.print(" mAnimation="); pw.println(mAnimation);
8096 }
8097 if (mHasTransformation || mHasLocalTransformation) {
8098 pw.print(prefix); pw.print("XForm: has=");
8099 pw.print(mHasTransformation);
8100 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8101 pw.print(" "); mTransformation.printShortString(pw);
8102 pw.println();
8103 }
8104 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8105 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8106 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8107 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8108 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8109 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8110 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8111 pw.print(" mDestroying="); pw.print(mDestroying);
8112 pw.print(" mRemoved="); pw.println(mRemoved);
8113 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008114 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008115 pw.print(prefix); pw.print("mOrientationChanging=");
8116 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008117 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8118 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008119 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008120 if (mHScale != 1 || mVScale != 1) {
8121 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8122 pw.print(" mVScale="); pw.println(mVScale);
8123 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008124 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008125 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8126 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8127 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008128 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8129 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8130 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008132 }
8133
8134 @Override
8135 public String toString() {
8136 return "Window{"
8137 + Integer.toHexString(System.identityHashCode(this))
8138 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8139 }
8140 }
Romain Guy06882f82009-06-10 13:36:04 -07008141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008142 // -------------------------------------------------------------
8143 // Window Token State
8144 // -------------------------------------------------------------
8145
8146 class WindowToken {
8147 // The actual token.
8148 final IBinder token;
8149
8150 // The type of window this token is for, as per WindowManager.LayoutParams.
8151 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008153 // Set if this token was explicitly added by a client, so should
8154 // not be removed when all windows are removed.
8155 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008156
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008157 // For printing.
8158 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008160 // If this is an AppWindowToken, this is non-null.
8161 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008163 // All of the windows associated with this token.
8164 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8165
8166 // Is key dispatching paused for this token?
8167 boolean paused = false;
8168
8169 // Should this token's windows be hidden?
8170 boolean hidden;
8171
8172 // Temporary for finding which tokens no longer have visible windows.
8173 boolean hasVisible;
8174
Dianne Hackborna8f60182009-09-01 19:01:50 -07008175 // Set to true when this token is in a pending transaction where it
8176 // will be shown.
8177 boolean waitingToShow;
8178
8179 // Set to true when this token is in a pending transaction where it
8180 // will be hidden.
8181 boolean waitingToHide;
8182
8183 // Set to true when this token is in a pending transaction where its
8184 // windows will be put to the bottom of the list.
8185 boolean sendingToBottom;
8186
8187 // Set to true when this token is in a pending transaction where its
8188 // windows will be put to the top of the list.
8189 boolean sendingToTop;
8190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008191 WindowToken(IBinder _token, int type, boolean _explicit) {
8192 token = _token;
8193 windowType = type;
8194 explicit = _explicit;
8195 }
8196
8197 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008198 pw.print(prefix); pw.print("token="); pw.println(token);
8199 pw.print(prefix); pw.print("windows="); pw.println(windows);
8200 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8201 pw.print(" hidden="); pw.print(hidden);
8202 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008203 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8204 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8205 pw.print(" waitingToHide="); pw.print(waitingToHide);
8206 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8207 pw.print(" sendingToTop="); pw.println(sendingToTop);
8208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008209 }
8210
8211 @Override
8212 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008213 if (stringName == null) {
8214 StringBuilder sb = new StringBuilder();
8215 sb.append("WindowToken{");
8216 sb.append(Integer.toHexString(System.identityHashCode(this)));
8217 sb.append(" token="); sb.append(token); sb.append('}');
8218 stringName = sb.toString();
8219 }
8220 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008221 }
8222 };
8223
8224 class AppWindowToken extends WindowToken {
8225 // Non-null only for application tokens.
8226 final IApplicationToken appToken;
8227
8228 // All of the windows and child windows that are included in this
8229 // application token. Note this list is NOT sorted!
8230 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8231
8232 int groupId = -1;
8233 boolean appFullscreen;
8234 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008236 // These are used for determining when all windows associated with
8237 // an activity have been drawn, so they can be made visible together
8238 // at the same time.
8239 int lastTransactionSequence = mTransactionSequence-1;
8240 int numInterestingWindows;
8241 int numDrawnWindows;
8242 boolean inPendingTransaction;
8243 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008245 // Is this token going to be hidden in a little while? If so, it
8246 // won't be taken into account for setting the screen orientation.
8247 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008249 // Is this window's surface needed? This is almost like hidden, except
8250 // it will sometimes be true a little earlier: when the token has
8251 // been shown, but is still waiting for its app transition to execute
8252 // before making its windows shown.
8253 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 // Have we told the window clients to hide themselves?
8256 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 // Last visibility state we reported to the app token.
8259 boolean reportedVisible;
8260
8261 // Set to true when the token has been removed from the window mgr.
8262 boolean removed;
8263
8264 // Have we been asked to have this token keep the screen frozen?
8265 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 boolean animating;
8268 Animation animation;
8269 boolean hasTransformation;
8270 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008272 // Offset to the window of all layers in the token, for use by
8273 // AppWindowToken animations.
8274 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008276 // Information about an application starting window if displayed.
8277 StartingData startingData;
8278 WindowState startingWindow;
8279 View startingView;
8280 boolean startingDisplayed;
8281 boolean startingMoved;
8282 boolean firstWindowDrawn;
8283
8284 AppWindowToken(IApplicationToken _token) {
8285 super(_token.asBinder(),
8286 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8287 appWindowToken = this;
8288 appToken = _token;
8289 }
Romain Guy06882f82009-06-10 13:36:04 -07008290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008291 public void setAnimation(Animation anim) {
8292 if (localLOGV) Log.v(
8293 TAG, "Setting animation in " + this + ": " + anim);
8294 animation = anim;
8295 animating = false;
8296 anim.restrictDuration(MAX_ANIMATION_DURATION);
8297 anim.scaleCurrentDuration(mTransitionAnimationScale);
8298 int zorder = anim.getZAdjustment();
8299 int adj = 0;
8300 if (zorder == Animation.ZORDER_TOP) {
8301 adj = TYPE_LAYER_OFFSET;
8302 } else if (zorder == Animation.ZORDER_BOTTOM) {
8303 adj = -TYPE_LAYER_OFFSET;
8304 }
Romain Guy06882f82009-06-10 13:36:04 -07008305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008306 if (animLayerAdjustment != adj) {
8307 animLayerAdjustment = adj;
8308 updateLayers();
8309 }
8310 }
Romain Guy06882f82009-06-10 13:36:04 -07008311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008312 public void setDummyAnimation() {
8313 if (animation == null) {
8314 if (localLOGV) Log.v(
8315 TAG, "Setting dummy animation in " + this);
8316 animation = sDummyAnimation;
8317 }
8318 }
8319
8320 public void clearAnimation() {
8321 if (animation != null) {
8322 animation = null;
8323 animating = true;
8324 }
8325 }
Romain Guy06882f82009-06-10 13:36:04 -07008326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008327 void updateLayers() {
8328 final int N = allAppWindows.size();
8329 final int adj = animLayerAdjustment;
8330 for (int i=0; i<N; i++) {
8331 WindowState w = allAppWindows.get(i);
8332 w.mAnimLayer = w.mLayer + adj;
8333 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8334 + w.mAnimLayer);
8335 if (w == mInputMethodTarget) {
8336 setInputMethodAnimLayerAdjustment(adj);
8337 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008338 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008339 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008341 }
8342 }
Romain Guy06882f82009-06-10 13:36:04 -07008343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008344 void sendAppVisibilityToClients() {
8345 final int N = allAppWindows.size();
8346 for (int i=0; i<N; i++) {
8347 WindowState win = allAppWindows.get(i);
8348 if (win == startingWindow && clientHidden) {
8349 // Don't hide the starting window.
8350 continue;
8351 }
8352 try {
8353 if (DEBUG_VISIBILITY) Log.v(TAG,
8354 "Setting visibility of " + win + ": " + (!clientHidden));
8355 win.mClient.dispatchAppVisibility(!clientHidden);
8356 } catch (RemoteException e) {
8357 }
8358 }
8359 }
Romain Guy06882f82009-06-10 13:36:04 -07008360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008361 void showAllWindowsLocked() {
8362 final int NW = allAppWindows.size();
8363 for (int i=0; i<NW; i++) {
8364 WindowState w = allAppWindows.get(i);
8365 if (DEBUG_VISIBILITY) Log.v(TAG,
8366 "performing show on: " + w);
8367 w.performShowLocked();
8368 }
8369 }
Romain Guy06882f82009-06-10 13:36:04 -07008370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008371 // This must be called while inside a transaction.
8372 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8373 if (!mDisplayFrozen) {
8374 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 if (animation == sDummyAnimation) {
8377 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008378 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008379 // when it is really time to animate, this will be set to
8380 // a real animation and the next call will execute normally.
8381 return false;
8382 }
Romain Guy06882f82009-06-10 13:36:04 -07008383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8385 if (!animating) {
8386 if (DEBUG_ANIM) Log.v(
8387 TAG, "Starting animation in " + this +
8388 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8389 + " scale=" + mTransitionAnimationScale
8390 + " allDrawn=" + allDrawn + " animating=" + animating);
8391 animation.initialize(dw, dh, dw, dh);
8392 animation.setStartTime(currentTime);
8393 animating = true;
8394 }
8395 transformation.clear();
8396 final boolean more = animation.getTransformation(
8397 currentTime, transformation);
8398 if (DEBUG_ANIM) Log.v(
8399 TAG, "Stepped animation in " + this +
8400 ": more=" + more + ", xform=" + transformation);
8401 if (more) {
8402 // we're done!
8403 hasTransformation = true;
8404 return true;
8405 }
8406 if (DEBUG_ANIM) Log.v(
8407 TAG, "Finished animation in " + this +
8408 " @ " + currentTime);
8409 animation = null;
8410 }
8411 } else if (animation != null) {
8412 // If the display is frozen, and there is a pending animation,
8413 // clear it and make sure we run the cleanup code.
8414 animating = true;
8415 animation = null;
8416 }
8417
8418 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 if (!animating) {
8421 return false;
8422 }
8423
8424 clearAnimation();
8425 animating = false;
8426 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8427 moveInputMethodWindowsIfNeededLocked(true);
8428 }
Romain Guy06882f82009-06-10 13:36:04 -07008429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 if (DEBUG_ANIM) Log.v(
8431 TAG, "Animation done in " + this
8432 + ": reportedVisible=" + reportedVisible);
8433
8434 transformation.clear();
8435 if (animLayerAdjustment != 0) {
8436 animLayerAdjustment = 0;
8437 updateLayers();
8438 }
Romain Guy06882f82009-06-10 13:36:04 -07008439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008440 final int N = windows.size();
8441 for (int i=0; i<N; i++) {
8442 ((WindowState)windows.get(i)).finishExit();
8443 }
8444 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 return false;
8447 }
8448
8449 void updateReportedVisibilityLocked() {
8450 if (appToken == null) {
8451 return;
8452 }
Romain Guy06882f82009-06-10 13:36:04 -07008453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008454 int numInteresting = 0;
8455 int numVisible = 0;
8456 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008458 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8459 final int N = allAppWindows.size();
8460 for (int i=0; i<N; i++) {
8461 WindowState win = allAppWindows.get(i);
8462 if (win == startingWindow || win.mAppFreezing) {
8463 continue;
8464 }
8465 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008466 Log.v(TAG, "Win " + win + ": isDrawn="
8467 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008468 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008469 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008470 Log.v(TAG, "Not displayed: s=" + win.mSurface
8471 + " pv=" + win.mPolicyVisibility
8472 + " dp=" + win.mDrawPending
8473 + " cdp=" + win.mCommitDrawPending
8474 + " ah=" + win.mAttachedHidden
8475 + " th="
8476 + (win.mAppToken != null
8477 ? win.mAppToken.hiddenRequested : false)
8478 + " a=" + win.mAnimating);
8479 }
8480 }
8481 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008482 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008483 if (!win.isAnimating()) {
8484 numVisible++;
8485 }
8486 nowGone = false;
8487 } else if (win.isAnimating()) {
8488 nowGone = false;
8489 }
8490 }
Romain Guy06882f82009-06-10 13:36:04 -07008491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8493 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8494 + numInteresting + " visible=" + numVisible);
8495 if (nowVisible != reportedVisible) {
8496 if (DEBUG_VISIBILITY) Log.v(
8497 TAG, "Visibility changed in " + this
8498 + ": vis=" + nowVisible);
8499 reportedVisible = nowVisible;
8500 Message m = mH.obtainMessage(
8501 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8502 nowVisible ? 1 : 0,
8503 nowGone ? 1 : 0,
8504 this);
8505 mH.sendMessage(m);
8506 }
8507 }
Romain Guy06882f82009-06-10 13:36:04 -07008508
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008509 WindowState findMainWindow() {
8510 int j = windows.size();
8511 while (j > 0) {
8512 j--;
8513 WindowState win = windows.get(j);
8514 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8515 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8516 return win;
8517 }
8518 }
8519 return null;
8520 }
8521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008522 void dump(PrintWriter pw, String prefix) {
8523 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008524 if (appToken != null) {
8525 pw.print(prefix); pw.println("app=true");
8526 }
8527 if (allAppWindows.size() > 0) {
8528 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8529 }
8530 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008531 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008532 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8533 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8534 pw.print(" clientHidden="); pw.print(clientHidden);
8535 pw.print(" willBeHidden="); pw.print(willBeHidden);
8536 pw.print(" reportedVisible="); pw.println(reportedVisible);
8537 if (paused || freezingScreen) {
8538 pw.print(prefix); pw.print("paused="); pw.print(paused);
8539 pw.print(" freezingScreen="); pw.println(freezingScreen);
8540 }
8541 if (numInterestingWindows != 0 || numDrawnWindows != 0
8542 || inPendingTransaction || allDrawn) {
8543 pw.print(prefix); pw.print("numInterestingWindows=");
8544 pw.print(numInterestingWindows);
8545 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8546 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8547 pw.print(" allDrawn="); pw.println(allDrawn);
8548 }
8549 if (animating || animation != null) {
8550 pw.print(prefix); pw.print("animating="); pw.print(animating);
8551 pw.print(" animation="); pw.println(animation);
8552 }
8553 if (animLayerAdjustment != 0) {
8554 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8555 }
8556 if (hasTransformation) {
8557 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8558 pw.print(" transformation="); transformation.printShortString(pw);
8559 pw.println();
8560 }
8561 if (startingData != null || removed || firstWindowDrawn) {
8562 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8563 pw.print(" removed="); pw.print(removed);
8564 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8565 }
8566 if (startingWindow != null || startingView != null
8567 || startingDisplayed || startingMoved) {
8568 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8569 pw.print(" startingView="); pw.print(startingView);
8570 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8571 pw.print(" startingMoved"); pw.println(startingMoved);
8572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 }
8574
8575 @Override
8576 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008577 if (stringName == null) {
8578 StringBuilder sb = new StringBuilder();
8579 sb.append("AppWindowToken{");
8580 sb.append(Integer.toHexString(System.identityHashCode(this)));
8581 sb.append(" token="); sb.append(token); sb.append('}');
8582 stringName = sb.toString();
8583 }
8584 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008585 }
8586 }
Romain Guy06882f82009-06-10 13:36:04 -07008587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588 // -------------------------------------------------------------
8589 // DummyAnimation
8590 // -------------------------------------------------------------
8591
8592 // This is an animation that does nothing: it just immediately finishes
8593 // itself every time it is called. It is used as a stub animation in cases
8594 // where we want to synchronize multiple things that may be animating.
8595 static final class DummyAnimation extends Animation {
8596 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8597 return false;
8598 }
8599 }
8600 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008602 // -------------------------------------------------------------
8603 // Async Handler
8604 // -------------------------------------------------------------
8605
8606 static final class StartingData {
8607 final String pkg;
8608 final int theme;
8609 final CharSequence nonLocalizedLabel;
8610 final int labelRes;
8611 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008613 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8614 int _labelRes, int _icon) {
8615 pkg = _pkg;
8616 theme = _theme;
8617 nonLocalizedLabel = _nonLocalizedLabel;
8618 labelRes = _labelRes;
8619 icon = _icon;
8620 }
8621 }
8622
8623 private final class H extends Handler {
8624 public static final int REPORT_FOCUS_CHANGE = 2;
8625 public static final int REPORT_LOSING_FOCUS = 3;
8626 public static final int ANIMATE = 4;
8627 public static final int ADD_STARTING = 5;
8628 public static final int REMOVE_STARTING = 6;
8629 public static final int FINISHED_STARTING = 7;
8630 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008631 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8632 public static final int HOLD_SCREEN_CHANGED = 12;
8633 public static final int APP_TRANSITION_TIMEOUT = 13;
8634 public static final int PERSIST_ANIMATION_SCALE = 14;
8635 public static final int FORCE_GC = 15;
8636 public static final int ENABLE_SCREEN = 16;
8637 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008638 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008640 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008642 public H() {
8643 }
Romain Guy06882f82009-06-10 13:36:04 -07008644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008645 @Override
8646 public void handleMessage(Message msg) {
8647 switch (msg.what) {
8648 case REPORT_FOCUS_CHANGE: {
8649 WindowState lastFocus;
8650 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 synchronized(mWindowMap) {
8653 lastFocus = mLastFocus;
8654 newFocus = mCurrentFocus;
8655 if (lastFocus == newFocus) {
8656 // Focus is not changing, so nothing to do.
8657 return;
8658 }
8659 mLastFocus = newFocus;
8660 //Log.i(TAG, "Focus moving from " + lastFocus
8661 // + " to " + newFocus);
8662 if (newFocus != null && lastFocus != null
8663 && !newFocus.isDisplayedLw()) {
8664 //Log.i(TAG, "Delaying loss of focus...");
8665 mLosingFocus.add(lastFocus);
8666 lastFocus = null;
8667 }
8668 }
8669
8670 if (lastFocus != newFocus) {
8671 //System.out.println("Changing focus from " + lastFocus
8672 // + " to " + newFocus);
8673 if (newFocus != null) {
8674 try {
8675 //Log.i(TAG, "Gaining focus: " + newFocus);
8676 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8677 } catch (RemoteException e) {
8678 // Ignore if process has died.
8679 }
8680 }
8681
8682 if (lastFocus != null) {
8683 try {
8684 //Log.i(TAG, "Losing focus: " + lastFocus);
8685 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8686 } catch (RemoteException e) {
8687 // Ignore if process has died.
8688 }
8689 }
8690 }
8691 } break;
8692
8693 case REPORT_LOSING_FOCUS: {
8694 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008696 synchronized(mWindowMap) {
8697 losers = mLosingFocus;
8698 mLosingFocus = new ArrayList<WindowState>();
8699 }
8700
8701 final int N = losers.size();
8702 for (int i=0; i<N; i++) {
8703 try {
8704 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8705 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8706 } catch (RemoteException e) {
8707 // Ignore if process has died.
8708 }
8709 }
8710 } break;
8711
8712 case ANIMATE: {
8713 synchronized(mWindowMap) {
8714 mAnimationPending = false;
8715 performLayoutAndPlaceSurfacesLocked();
8716 }
8717 } break;
8718
8719 case ADD_STARTING: {
8720 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8721 final StartingData sd = wtoken.startingData;
8722
8723 if (sd == null) {
8724 // Animation has been canceled... do nothing.
8725 return;
8726 }
Romain Guy06882f82009-06-10 13:36:04 -07008727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008728 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8729 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 View view = null;
8732 try {
8733 view = mPolicy.addStartingWindow(
8734 wtoken.token, sd.pkg,
8735 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8736 sd.icon);
8737 } catch (Exception e) {
8738 Log.w(TAG, "Exception when adding starting window", e);
8739 }
8740
8741 if (view != null) {
8742 boolean abort = false;
8743
8744 synchronized(mWindowMap) {
8745 if (wtoken.removed || wtoken.startingData == null) {
8746 // If the window was successfully added, then
8747 // we need to remove it.
8748 if (wtoken.startingWindow != null) {
8749 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8750 "Aborted starting " + wtoken
8751 + ": removed=" + wtoken.removed
8752 + " startingData=" + wtoken.startingData);
8753 wtoken.startingWindow = null;
8754 wtoken.startingData = null;
8755 abort = true;
8756 }
8757 } else {
8758 wtoken.startingView = view;
8759 }
8760 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8761 "Added starting " + wtoken
8762 + ": startingWindow="
8763 + wtoken.startingWindow + " startingView="
8764 + wtoken.startingView);
8765 }
8766
8767 if (abort) {
8768 try {
8769 mPolicy.removeStartingWindow(wtoken.token, view);
8770 } catch (Exception e) {
8771 Log.w(TAG, "Exception when removing starting window", e);
8772 }
8773 }
8774 }
8775 } break;
8776
8777 case REMOVE_STARTING: {
8778 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8779 IBinder token = null;
8780 View view = null;
8781 synchronized (mWindowMap) {
8782 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8783 + wtoken + ": startingWindow="
8784 + wtoken.startingWindow + " startingView="
8785 + wtoken.startingView);
8786 if (wtoken.startingWindow != null) {
8787 view = wtoken.startingView;
8788 token = wtoken.token;
8789 wtoken.startingData = null;
8790 wtoken.startingView = null;
8791 wtoken.startingWindow = null;
8792 }
8793 }
8794 if (view != null) {
8795 try {
8796 mPolicy.removeStartingWindow(token, view);
8797 } catch (Exception e) {
8798 Log.w(TAG, "Exception when removing starting window", e);
8799 }
8800 }
8801 } break;
8802
8803 case FINISHED_STARTING: {
8804 IBinder token = null;
8805 View view = null;
8806 while (true) {
8807 synchronized (mWindowMap) {
8808 final int N = mFinishedStarting.size();
8809 if (N <= 0) {
8810 break;
8811 }
8812 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8813
8814 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8815 "Finished starting " + wtoken
8816 + ": startingWindow=" + wtoken.startingWindow
8817 + " startingView=" + wtoken.startingView);
8818
8819 if (wtoken.startingWindow == null) {
8820 continue;
8821 }
8822
8823 view = wtoken.startingView;
8824 token = wtoken.token;
8825 wtoken.startingData = null;
8826 wtoken.startingView = null;
8827 wtoken.startingWindow = null;
8828 }
8829
8830 try {
8831 mPolicy.removeStartingWindow(token, view);
8832 } catch (Exception e) {
8833 Log.w(TAG, "Exception when removing starting window", e);
8834 }
8835 }
8836 } break;
8837
8838 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8839 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8840
8841 boolean nowVisible = msg.arg1 != 0;
8842 boolean nowGone = msg.arg2 != 0;
8843
8844 try {
8845 if (DEBUG_VISIBILITY) Log.v(
8846 TAG, "Reporting visible in " + wtoken
8847 + " visible=" + nowVisible
8848 + " gone=" + nowGone);
8849 if (nowVisible) {
8850 wtoken.appToken.windowsVisible();
8851 } else {
8852 wtoken.appToken.windowsGone();
8853 }
8854 } catch (RemoteException ex) {
8855 }
8856 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008858 case WINDOW_FREEZE_TIMEOUT: {
8859 synchronized (mWindowMap) {
8860 Log.w(TAG, "Window freeze timeout expired.");
8861 int i = mWindows.size();
8862 while (i > 0) {
8863 i--;
8864 WindowState w = (WindowState)mWindows.get(i);
8865 if (w.mOrientationChanging) {
8866 w.mOrientationChanging = false;
8867 Log.w(TAG, "Force clearing orientation change: " + w);
8868 }
8869 }
8870 performLayoutAndPlaceSurfacesLocked();
8871 }
8872 break;
8873 }
Romain Guy06882f82009-06-10 13:36:04 -07008874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008875 case HOLD_SCREEN_CHANGED: {
8876 Session oldHold;
8877 Session newHold;
8878 synchronized (mWindowMap) {
8879 oldHold = mLastReportedHold;
8880 newHold = (Session)msg.obj;
8881 mLastReportedHold = newHold;
8882 }
Romain Guy06882f82009-06-10 13:36:04 -07008883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008884 if (oldHold != newHold) {
8885 try {
8886 if (oldHold != null) {
8887 mBatteryStats.noteStopWakelock(oldHold.mUid,
8888 "window",
8889 BatteryStats.WAKE_TYPE_WINDOW);
8890 }
8891 if (newHold != null) {
8892 mBatteryStats.noteStartWakelock(newHold.mUid,
8893 "window",
8894 BatteryStats.WAKE_TYPE_WINDOW);
8895 }
8896 } catch (RemoteException e) {
8897 }
8898 }
8899 break;
8900 }
Romain Guy06882f82009-06-10 13:36:04 -07008901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008902 case APP_TRANSITION_TIMEOUT: {
8903 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008904 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008905 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8906 "*** APP TRANSITION TIMEOUT");
8907 mAppTransitionReady = true;
8908 mAppTransitionTimeout = true;
8909 performLayoutAndPlaceSurfacesLocked();
8910 }
8911 }
8912 break;
8913 }
Romain Guy06882f82009-06-10 13:36:04 -07008914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008915 case PERSIST_ANIMATION_SCALE: {
8916 Settings.System.putFloat(mContext.getContentResolver(),
8917 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8918 Settings.System.putFloat(mContext.getContentResolver(),
8919 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8920 break;
8921 }
Romain Guy06882f82009-06-10 13:36:04 -07008922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 case FORCE_GC: {
8924 synchronized(mWindowMap) {
8925 if (mAnimationPending) {
8926 // If we are animating, don't do the gc now but
8927 // delay a bit so we don't interrupt the animation.
8928 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8929 2000);
8930 return;
8931 }
8932 // If we are currently rotating the display, it will
8933 // schedule a new message when done.
8934 if (mDisplayFrozen) {
8935 return;
8936 }
8937 mFreezeGcPending = 0;
8938 }
8939 Runtime.getRuntime().gc();
8940 break;
8941 }
Romain Guy06882f82009-06-10 13:36:04 -07008942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 case ENABLE_SCREEN: {
8944 performEnableScreen();
8945 break;
8946 }
Romain Guy06882f82009-06-10 13:36:04 -07008947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008948 case APP_FREEZE_TIMEOUT: {
8949 synchronized (mWindowMap) {
8950 Log.w(TAG, "App freeze timeout expired.");
8951 int i = mAppTokens.size();
8952 while (i > 0) {
8953 i--;
8954 AppWindowToken tok = mAppTokens.get(i);
8955 if (tok.freezingScreen) {
8956 Log.w(TAG, "Force clearing freeze: " + tok);
8957 unsetAppFreezingScreenLocked(tok, true, true);
8958 }
8959 }
8960 }
8961 break;
8962 }
Romain Guy06882f82009-06-10 13:36:04 -07008963
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008964 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008965 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008966 sendNewConfiguration();
8967 }
8968 break;
8969 }
Romain Guy06882f82009-06-10 13:36:04 -07008970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008971 }
8972 }
8973 }
8974
8975 // -------------------------------------------------------------
8976 // IWindowManager API
8977 // -------------------------------------------------------------
8978
8979 public IWindowSession openSession(IInputMethodClient client,
8980 IInputContext inputContext) {
8981 if (client == null) throw new IllegalArgumentException("null client");
8982 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8983 return new Session(client, inputContext);
8984 }
8985
8986 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8987 synchronized (mWindowMap) {
8988 // The focus for the client is the window immediately below
8989 // where we would place the input method window.
8990 int idx = findDesiredInputMethodWindowIndexLocked(false);
8991 WindowState imFocus;
8992 if (idx > 0) {
8993 imFocus = (WindowState)mWindows.get(idx-1);
8994 if (imFocus != null) {
8995 if (imFocus.mSession.mClient != null &&
8996 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8997 return true;
8998 }
8999 }
9000 }
9001 }
9002 return false;
9003 }
Romain Guy06882f82009-06-10 13:36:04 -07009004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009005 // -------------------------------------------------------------
9006 // Internals
9007 // -------------------------------------------------------------
9008
9009 final WindowState windowForClientLocked(Session session, IWindow client) {
9010 return windowForClientLocked(session, client.asBinder());
9011 }
Romain Guy06882f82009-06-10 13:36:04 -07009012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009013 final WindowState windowForClientLocked(Session session, IBinder client) {
9014 WindowState win = mWindowMap.get(client);
9015 if (localLOGV) Log.v(
9016 TAG, "Looking up client " + client + ": " + win);
9017 if (win == null) {
9018 RuntimeException ex = new RuntimeException();
9019 Log.w(TAG, "Requested window " + client + " does not exist", ex);
9020 return null;
9021 }
9022 if (session != null && win.mSession != session) {
9023 RuntimeException ex = new RuntimeException();
9024 Log.w(TAG, "Requested window " + client + " is in session " +
9025 win.mSession + ", not " + session, ex);
9026 return null;
9027 }
9028
9029 return win;
9030 }
9031
Dianne Hackborna8f60182009-09-01 19:01:50 -07009032 final void rebuildAppWindowListLocked() {
9033 int NW = mWindows.size();
9034 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009035 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009036 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009037
9038 // First remove all existing app windows.
9039 i=0;
9040 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009041 WindowState w = (WindowState)mWindows.get(i);
9042 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009043 WindowState win = (WindowState)mWindows.remove(i);
9044 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
9045 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009046 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009047 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009048 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009049 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9050 && lastWallpaper == i-1) {
9051 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009052 }
9053 i++;
9054 }
9055
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009056 // The wallpaper window(s) typically live at the bottom of the stack,
9057 // so skip them before adding app tokens.
9058 lastWallpaper++;
9059 i = lastWallpaper;
9060
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009061 // First add all of the exiting app tokens... these are no longer
9062 // in the main app list, but still have windows shown. We put them
9063 // in the back because now that the animation is over we no longer
9064 // will care about them.
9065 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009066 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009067 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9068 }
9069
9070 // And add in the still active app tokens in Z order.
9071 NT = mAppTokens.size();
9072 for (int j=0; j<NT; j++) {
9073 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009074 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009075
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009076 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009077 if (i != numRemoved) {
9078 Log.w(TAG, "Rebuild removed " + numRemoved
9079 + " windows but added " + i);
9080 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009081 }
9082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009083 private final void assignLayersLocked() {
9084 int N = mWindows.size();
9085 int curBaseLayer = 0;
9086 int curLayer = 0;
9087 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009089 for (i=0; i<N; i++) {
9090 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009091 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9092 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009093 curLayer += WINDOW_LAYER_MULTIPLIER;
9094 w.mLayer = curLayer;
9095 } else {
9096 curBaseLayer = curLayer = w.mBaseLayer;
9097 w.mLayer = curLayer;
9098 }
9099 if (w.mTargetAppToken != null) {
9100 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9101 } else if (w.mAppToken != null) {
9102 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9103 } else {
9104 w.mAnimLayer = w.mLayer;
9105 }
9106 if (w.mIsImWindow) {
9107 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009108 } else if (w.mIsWallpaper) {
9109 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009110 }
9111 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9112 + w.mAnimLayer);
9113 //System.out.println(
9114 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9115 }
9116 }
9117
9118 private boolean mInLayout = false;
9119 private final void performLayoutAndPlaceSurfacesLocked() {
9120 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009121 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009122 throw new RuntimeException("Recursive call!");
9123 }
9124 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9125 return;
9126 }
9127
9128 boolean recoveringMemory = false;
9129 if (mForceRemoves != null) {
9130 recoveringMemory = true;
9131 // Wait a little it for things to settle down, and off we go.
9132 for (int i=0; i<mForceRemoves.size(); i++) {
9133 WindowState ws = mForceRemoves.get(i);
9134 Log.i(TAG, "Force removing: " + ws);
9135 removeWindowInnerLocked(ws.mSession, ws);
9136 }
9137 mForceRemoves = null;
9138 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9139 Object tmp = new Object();
9140 synchronized (tmp) {
9141 try {
9142 tmp.wait(250);
9143 } catch (InterruptedException e) {
9144 }
9145 }
9146 }
Romain Guy06882f82009-06-10 13:36:04 -07009147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009148 mInLayout = true;
9149 try {
9150 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009152 int i = mPendingRemove.size()-1;
9153 if (i >= 0) {
9154 while (i >= 0) {
9155 WindowState w = mPendingRemove.get(i);
9156 removeWindowInnerLocked(w.mSession, w);
9157 i--;
9158 }
9159 mPendingRemove.clear();
9160
9161 mInLayout = false;
9162 assignLayersLocked();
9163 mLayoutNeeded = true;
9164 performLayoutAndPlaceSurfacesLocked();
9165
9166 } else {
9167 mInLayout = false;
9168 if (mLayoutNeeded) {
9169 requestAnimationLocked(0);
9170 }
9171 }
9172 } catch (RuntimeException e) {
9173 mInLayout = false;
9174 Log.e(TAG, "Unhandled exception while layout out windows", e);
9175 }
9176 }
9177
9178 private final void performLayoutLockedInner() {
9179 final int dw = mDisplay.getWidth();
9180 final int dh = mDisplay.getHeight();
9181
9182 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009183 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009184 int i;
9185
9186 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009187
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009188 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009189 mPolicy.beginLayoutLw(dw, dh);
9190
9191 // First perform layout of any root windows (not attached
9192 // to another window).
9193 int topAttached = -1;
9194 for (i = N-1; i >= 0; i--) {
9195 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009196
9197 // Don't do layout of a window if it is not visible, or
9198 // soon won't be visible, to avoid wasting time and funky
9199 // changes while a window is animating away.
9200 final AppWindowToken atoken = win.mAppToken;
9201 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009202 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009203 || win.mRootToken.hidden
9204 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009205 || win.mAttachedHidden
9206 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009207
9208 // If this view is GONE, then skip it -- keep the current
9209 // frame, and let the caller know so they can ignore it
9210 // if they want. (We do the normal layout for INVISIBLE
9211 // windows, since that means "perform layout as normal,
9212 // just don't display").
9213 if (!gone || !win.mHaveFrame) {
9214 if (!win.mLayoutAttached) {
9215 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9216 } else {
9217 if (topAttached < 0) topAttached = i;
9218 }
9219 }
9220 }
Romain Guy06882f82009-06-10 13:36:04 -07009221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009222 // Now perform layout of attached windows, which usually
9223 // depend on the position of the window they are attached to.
9224 // XXX does not deal with windows that are attached to windows
9225 // that are themselves attached.
9226 for (i = topAttached; i >= 0; i--) {
9227 WindowState win = (WindowState) mWindows.get(i);
9228
9229 // If this view is GONE, then skip it -- keep the current
9230 // frame, and let the caller know so they can ignore it
9231 // if they want. (We do the normal layout for INVISIBLE
9232 // windows, since that means "perform layout as normal,
9233 // just don't display").
9234 if (win.mLayoutAttached) {
9235 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9236 || !win.mHaveFrame) {
9237 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9238 }
9239 }
9240 }
9241
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009242 int changes = mPolicy.finishLayoutLw();
9243 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9244 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9245 assignLayersLocked();
9246 }
9247 }
9248 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009249 mLayoutNeeded = false;
9250 } else if (repeats > 2) {
9251 Log.w(TAG, "Layout repeat aborted after too many iterations");
9252 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009253 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9254 Configuration newConfig = updateOrientationFromAppTokensLocked(
9255 null, null);
9256 if (newConfig != null) {
9257 mLayoutNeeded = true;
9258 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9259 }
9260 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009261 } else {
9262 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009263 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9264 Configuration newConfig = updateOrientationFromAppTokensLocked(
9265 null, null);
9266 if (newConfig != null) {
9267 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9268 }
9269 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009271 }
9272 }
Romain Guy06882f82009-06-10 13:36:04 -07009273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009274 private final void performLayoutAndPlaceSurfacesLockedInner(
9275 boolean recoveringMemory) {
9276 final long currentTime = SystemClock.uptimeMillis();
9277 final int dw = mDisplay.getWidth();
9278 final int dh = mDisplay.getHeight();
9279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 int i;
9281
9282 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009283 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009285 if (mFxSession == null) {
9286 mFxSession = new SurfaceSession();
9287 }
Romain Guy06882f82009-06-10 13:36:04 -07009288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009289 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9290
9291 // Initialize state of exiting tokens.
9292 for (i=mExitingTokens.size()-1; i>=0; i--) {
9293 mExitingTokens.get(i).hasVisible = false;
9294 }
9295
9296 // Initialize state of exiting applications.
9297 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9298 mExitingAppTokens.get(i).hasVisible = false;
9299 }
9300
9301 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009302 boolean orientationChangeComplete = true;
9303 Session holdScreen = null;
9304 float screenBrightness = -1;
9305 boolean focusDisplayed = false;
9306 boolean animating = false;
9307
9308 Surface.openTransaction();
9309 try {
9310 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009311 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009312
9313 do {
9314 final int transactionSequence = ++mTransactionSequence;
9315
9316 // Update animations of all applications, including those
9317 // associated with exiting/removed apps
9318 boolean tokensAnimating = false;
9319 final int NAT = mAppTokens.size();
9320 for (i=0; i<NAT; i++) {
9321 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9322 tokensAnimating = true;
9323 }
9324 }
9325 final int NEAT = mExitingAppTokens.size();
9326 for (i=0; i<NEAT; i++) {
9327 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9328 tokensAnimating = true;
9329 }
9330 }
9331
9332 animating = tokensAnimating;
9333 restart = false;
9334
9335 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009336 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009337 boolean focusMayChange = false;
9338 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339
9340 mPolicy.beginAnimationLw(dw, dh);
9341
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009342 final int N = mWindows.size();
9343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009344 for (i=N-1; i>=0; i--) {
9345 WindowState w = (WindowState)mWindows.get(i);
9346
9347 final WindowManager.LayoutParams attrs = w.mAttrs;
9348
9349 if (w.mSurface != null) {
9350 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009351 if (w.commitFinishDrawingLocked(currentTime)) {
9352 if ((w.mAttrs.flags
9353 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009354 if (DEBUG_WALLPAPER) Log.v(TAG,
9355 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009356 wallpaperMayChange = true;
9357 }
9358 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009359
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009360 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009361 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9362 animating = true;
9363 //w.dump(" ");
9364 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009365 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9366 wallpaperMayChange = true;
9367 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009368
9369 if (mPolicy.doesForceHide(w, attrs)) {
9370 if (!wasAnimating && animating) {
9371 wallpaperForceHidingChanged = true;
9372 focusMayChange = true;
9373 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9374 forceHiding = true;
9375 }
9376 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9377 boolean changed;
9378 if (forceHiding) {
9379 changed = w.hideLw(false, false);
9380 } else {
9381 changed = w.showLw(false, false);
9382 if (changed && wallpaperForceHidingChanged
9383 && w.isReadyForDisplay()) {
9384 // Assume we will need to animate. If
9385 // we don't (because the wallpaper will
9386 // stay with the lock screen), then we will
9387 // clean up later.
9388 Animation a = mPolicy.createForceHideEnterAnimation();
9389 if (a != null) {
9390 w.setAnimation(a);
9391 }
9392 }
9393 }
9394 if (changed && (attrs.flags
9395 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9396 wallpaperMayChange = true;
9397 }
Christopher Tate405d0892009-10-27 20:23:28 -07009398 if (changed && !forceHiding
9399 && (mCurrentFocus == null)
9400 && (mFocusedApp != null)) {
9401 // It's possible that the last focus recalculation left no
9402 // current focused window even though the app has come to the
9403 // foreground already. In this case, we make sure to recalculate
9404 // focus when we show a window.
9405 focusMayChange = true;
9406 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009407 }
9408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009409 mPolicy.animatingWindowLw(w, attrs);
9410 }
9411
9412 final AppWindowToken atoken = w.mAppToken;
9413 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9414 if (atoken.lastTransactionSequence != transactionSequence) {
9415 atoken.lastTransactionSequence = transactionSequence;
9416 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9417 atoken.startingDisplayed = false;
9418 }
9419 if ((w.isOnScreen() || w.mAttrs.type
9420 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9421 && !w.mExiting && !w.mDestroying) {
9422 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009423 Log.v(TAG, "Eval win " + w + ": isDrawn="
9424 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009425 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009426 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 Log.v(TAG, "Not displayed: s=" + w.mSurface
9428 + " pv=" + w.mPolicyVisibility
9429 + " dp=" + w.mDrawPending
9430 + " cdp=" + w.mCommitDrawPending
9431 + " ah=" + w.mAttachedHidden
9432 + " th=" + atoken.hiddenRequested
9433 + " a=" + w.mAnimating);
9434 }
9435 }
9436 if (w != atoken.startingWindow) {
9437 if (!atoken.freezingScreen || !w.mAppFreezing) {
9438 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009439 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440 atoken.numDrawnWindows++;
9441 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9442 "tokenMayBeDrawn: " + atoken
9443 + " freezingScreen=" + atoken.freezingScreen
9444 + " mAppFreezing=" + w.mAppFreezing);
9445 tokenMayBeDrawn = true;
9446 }
9447 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009448 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009449 atoken.startingDisplayed = true;
9450 }
9451 }
9452 } else if (w.mReadyToShow) {
9453 w.performShowLocked();
9454 }
9455 }
9456
9457 if (mPolicy.finishAnimationLw()) {
9458 restart = true;
9459 }
9460
9461 if (tokenMayBeDrawn) {
9462 // See if any windows have been drawn, so they (and others
9463 // associated with them) can now be shown.
9464 final int NT = mTokenList.size();
9465 for (i=0; i<NT; i++) {
9466 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9467 if (wtoken == null) {
9468 continue;
9469 }
9470 if (wtoken.freezingScreen) {
9471 int numInteresting = wtoken.numInterestingWindows;
9472 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9473 if (DEBUG_VISIBILITY) Log.v(TAG,
9474 "allDrawn: " + wtoken
9475 + " interesting=" + numInteresting
9476 + " drawn=" + wtoken.numDrawnWindows);
9477 wtoken.showAllWindowsLocked();
9478 unsetAppFreezingScreenLocked(wtoken, false, true);
9479 orientationChangeComplete = true;
9480 }
9481 } else if (!wtoken.allDrawn) {
9482 int numInteresting = wtoken.numInterestingWindows;
9483 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9484 if (DEBUG_VISIBILITY) Log.v(TAG,
9485 "allDrawn: " + wtoken
9486 + " interesting=" + numInteresting
9487 + " drawn=" + wtoken.numDrawnWindows);
9488 wtoken.allDrawn = true;
9489 restart = true;
9490
9491 // We can now show all of the drawn windows!
9492 if (!mOpeningApps.contains(wtoken)) {
9493 wtoken.showAllWindowsLocked();
9494 }
9495 }
9496 }
9497 }
9498 }
9499
9500 // If we are ready to perform an app transition, check through
9501 // all of the app tokens to be shown and see if they are ready
9502 // to go.
9503 if (mAppTransitionReady) {
9504 int NN = mOpeningApps.size();
9505 boolean goodToGo = true;
9506 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9507 "Checking " + NN + " opening apps (frozen="
9508 + mDisplayFrozen + " timeout="
9509 + mAppTransitionTimeout + ")...");
9510 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9511 // If the display isn't frozen, wait to do anything until
9512 // all of the apps are ready. Otherwise just go because
9513 // we'll unfreeze the display when everyone is ready.
9514 for (i=0; i<NN && goodToGo; i++) {
9515 AppWindowToken wtoken = mOpeningApps.get(i);
9516 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9517 "Check opening app" + wtoken + ": allDrawn="
9518 + wtoken.allDrawn + " startingDisplayed="
9519 + wtoken.startingDisplayed);
9520 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9521 && !wtoken.startingMoved) {
9522 goodToGo = false;
9523 }
9524 }
9525 }
9526 if (goodToGo) {
9527 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9528 int transit = mNextAppTransition;
9529 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009530 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009531 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009532 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009533 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009534 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009535 mAppTransitionTimeout = false;
9536 mStartingIconInTransition = false;
9537 mSkipAppTransitionAnimation = false;
9538
9539 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9540
Dianne Hackborna8f60182009-09-01 19:01:50 -07009541 // If there are applications waiting to come to the
9542 // top of the stack, now is the time to move their windows.
9543 // (Note that we don't do apps going to the bottom
9544 // here -- we want to keep their windows in the old
9545 // Z-order until the animation completes.)
9546 if (mToTopApps.size() > 0) {
9547 NN = mAppTokens.size();
9548 for (i=0; i<NN; i++) {
9549 AppWindowToken wtoken = mAppTokens.get(i);
9550 if (wtoken.sendingToTop) {
9551 wtoken.sendingToTop = false;
9552 moveAppWindowsLocked(wtoken, NN, false);
9553 }
9554 }
9555 mToTopApps.clear();
9556 }
9557
Dianne Hackborn25994b42009-09-04 14:21:19 -07009558 WindowState oldWallpaper = mWallpaperTarget;
9559
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009560 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009561 wallpaperMayChange = false;
9562
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009563 // The top-most window will supply the layout params,
9564 // and we will determine it below.
9565 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009566 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009567 int bestAnimLayer = -1;
9568
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009569 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009570 "New wallpaper target=" + mWallpaperTarget
9571 + ", lower target=" + mLowerWallpaperTarget
9572 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009573 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009574 // Do a first pass through the tokens for two
9575 // things:
9576 // (1) Determine if both the closing and opening
9577 // app token sets are wallpaper targets, in which
9578 // case special animations are needed
9579 // (since the wallpaper needs to stay static
9580 // behind them).
9581 // (2) Find the layout params of the top-most
9582 // application window in the tokens, which is
9583 // what will control the animation theme.
9584 final int NC = mClosingApps.size();
9585 NN = NC + mOpeningApps.size();
9586 for (i=0; i<NN; i++) {
9587 AppWindowToken wtoken;
9588 int mode;
9589 if (i < NC) {
9590 wtoken = mClosingApps.get(i);
9591 mode = 1;
9592 } else {
9593 wtoken = mOpeningApps.get(i-NC);
9594 mode = 2;
9595 }
9596 if (mLowerWallpaperTarget != null) {
9597 if (mLowerWallpaperTarget.mAppToken == wtoken
9598 || mUpperWallpaperTarget.mAppToken == wtoken) {
9599 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009600 }
9601 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009602 if (wtoken.appFullscreen) {
9603 WindowState ws = wtoken.findMainWindow();
9604 if (ws != null) {
9605 // If this is a compatibility mode
9606 // window, we will always use its anim.
9607 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9608 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009609 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009610 bestAnimLayer = Integer.MAX_VALUE;
9611 } else if (ws.mLayer > bestAnimLayer) {
9612 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009613 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009614 bestAnimLayer = ws.mLayer;
9615 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009616 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009617 }
9618 }
9619
Dianne Hackborn25994b42009-09-04 14:21:19 -07009620 if (foundWallpapers == 3) {
9621 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9622 "Wallpaper animation!");
9623 switch (transit) {
9624 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9625 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9626 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9627 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9628 break;
9629 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9630 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9631 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9632 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9633 break;
9634 }
9635 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9636 "New transit: " + transit);
9637 } else if (oldWallpaper != null) {
9638 // We are transitioning from an activity with
9639 // a wallpaper to one without.
9640 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9641 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9642 "New transit away from wallpaper: " + transit);
9643 } else if (mWallpaperTarget != null) {
9644 // We are transitioning from an activity without
9645 // a wallpaper to now showing the wallpaper
9646 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9647 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9648 "New transit into wallpaper: " + transit);
9649 }
9650
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009651 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9652 mLastEnterAnimToken = animToken;
9653 mLastEnterAnimParams = animLp;
9654 } else if (mLastEnterAnimParams != null) {
9655 animLp = mLastEnterAnimParams;
9656 mLastEnterAnimToken = null;
9657 mLastEnterAnimParams = null;
9658 }
9659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 NN = mOpeningApps.size();
9661 for (i=0; i<NN; i++) {
9662 AppWindowToken wtoken = mOpeningApps.get(i);
9663 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9664 "Now opening app" + wtoken);
9665 wtoken.reportedVisible = false;
9666 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009667 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009668 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009669 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009670 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009671 wtoken.showAllWindowsLocked();
9672 }
9673 NN = mClosingApps.size();
9674 for (i=0; i<NN; i++) {
9675 AppWindowToken wtoken = mClosingApps.get(i);
9676 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9677 "Now closing app" + wtoken);
9678 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009679 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009680 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009681 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009682 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009683 // Force the allDrawn flag, because we want to start
9684 // this guy's animations regardless of whether it's
9685 // gotten drawn.
9686 wtoken.allDrawn = true;
9687 }
9688
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009689 mNextAppTransitionPackage = null;
9690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009691 mOpeningApps.clear();
9692 mClosingApps.clear();
9693
9694 // This has changed the visibility of windows, so perform
9695 // a new layout to get them all up-to-date.
9696 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009697 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9698 assignLayersLocked();
9699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009700 performLayoutLockedInner();
9701 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009702 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703
9704 restart = true;
9705 }
9706 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009707
Dianne Hackborna8f60182009-09-01 19:01:50 -07009708 if (!animating && mAppTransitionRunning) {
9709 // We have finished the animation of an app transition. To do
9710 // this, we have delayed a lot of operations like showing and
9711 // hiding apps, moving apps in Z-order, etc. The app token list
9712 // reflects the correct Z-order, but the window list may now
9713 // be out of sync with it. So here we will just rebuild the
9714 // entire app window list. Fun!
9715 mAppTransitionRunning = false;
9716 // Clear information about apps that were moving.
9717 mToBottomApps.clear();
9718
9719 rebuildAppWindowListLocked();
9720 restart = true;
9721 moveInputMethodWindowsIfNeededLocked(false);
9722 wallpaperMayChange = true;
9723 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009724 // Since the window list has been rebuilt, focus might
9725 // have to be recomputed since the actual order of windows
9726 // might have changed again.
9727 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009728 }
9729
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009730 int adjResult = 0;
9731
9732 if (wallpaperForceHidingChanged) {
9733 // At this point, there was a window with a wallpaper that
9734 // was force hiding other windows behind it, but now it
9735 // is going away. This may be simple -- just animate
9736 // away the wallpaper and its window -- or it may be
9737 // hard -- the wallpaper now needs to be shown behind
9738 // something that was hidden.
9739 WindowState oldWallpaper = mWallpaperTarget;
9740 adjResult = adjustWallpaperWindowsLocked();
9741 wallpaperMayChange = false;
9742 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9743 + " NEW: " + mWallpaperTarget);
9744 if (mLowerWallpaperTarget == null) {
9745 // Whoops, we don't need a special wallpaper animation.
9746 // Clear them out.
9747 forceHiding = false;
9748 for (i=N-1; i>=0; i--) {
9749 WindowState w = (WindowState)mWindows.get(i);
9750 if (w.mSurface != null) {
9751 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009752 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9753 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009754 forceHiding = true;
9755 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9756 if (!w.mAnimating) {
9757 // We set the animation above so it
9758 // is not yet running.
9759 w.clearAnimation();
9760 }
9761 }
9762 }
9763 }
9764 }
9765 }
9766
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009767 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009768 if (DEBUG_WALLPAPER) Log.v(TAG,
9769 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009770 adjResult = adjustWallpaperWindowsLocked();
9771 }
9772
9773 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9774 if (DEBUG_WALLPAPER) Log.v(TAG,
9775 "Wallpaper layer changed: assigning layers + relayout");
9776 restart = true;
9777 mLayoutNeeded = true;
9778 assignLayersLocked();
9779 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9780 if (DEBUG_WALLPAPER) Log.v(TAG,
9781 "Wallpaper visibility changed: relayout");
9782 restart = true;
9783 mLayoutNeeded = true;
9784 }
9785
9786 if (focusMayChange) {
9787 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009788 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009789 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009790 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009791 }
9792
9793 if (mLayoutNeeded) {
9794 restart = true;
9795 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009796 }
9797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009798 } while (restart);
9799
9800 // THIRD LOOP: Update the surfaces of all windows.
9801
9802 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9803
9804 boolean obscured = false;
9805 boolean blurring = false;
9806 boolean dimming = false;
9807 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009808 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009809 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009811 final int N = mWindows.size();
9812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009813 for (i=N-1; i>=0; i--) {
9814 WindowState w = (WindowState)mWindows.get(i);
9815
9816 boolean displayed = false;
9817 final WindowManager.LayoutParams attrs = w.mAttrs;
9818 final int attrFlags = attrs.flags;
9819
9820 if (w.mSurface != null) {
9821 w.computeShownFrameLocked();
9822 if (localLOGV) Log.v(
9823 TAG, "Placing surface #" + i + " " + w.mSurface
9824 + ": new=" + w.mShownFrame + ", old="
9825 + w.mLastShownFrame);
9826
9827 boolean resize;
9828 int width, height;
9829 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9830 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9831 w.mLastRequestedHeight != w.mRequestedHeight;
9832 // for a scaled surface, we just want to use
9833 // the requested size.
9834 width = w.mRequestedWidth;
9835 height = w.mRequestedHeight;
9836 w.mLastRequestedWidth = width;
9837 w.mLastRequestedHeight = height;
9838 w.mLastShownFrame.set(w.mShownFrame);
9839 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009840 if (SHOW_TRANSACTIONS) Log.i(
9841 TAG, " SURFACE " + w.mSurface
9842 + ": POS " + w.mShownFrame.left
9843 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009844 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9845 } catch (RuntimeException e) {
9846 Log.w(TAG, "Error positioning surface in " + w, e);
9847 if (!recoveringMemory) {
9848 reclaimSomeSurfaceMemoryLocked(w, "position");
9849 }
9850 }
9851 } else {
9852 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9853 width = w.mShownFrame.width();
9854 height = w.mShownFrame.height();
9855 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009856 }
9857
9858 if (resize) {
9859 if (width < 1) width = 1;
9860 if (height < 1) height = 1;
9861 if (w.mSurface != null) {
9862 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009863 if (SHOW_TRANSACTIONS) Log.i(
9864 TAG, " SURFACE " + w.mSurface + ": POS "
9865 + w.mShownFrame.left + ","
9866 + w.mShownFrame.top + " SIZE "
9867 + w.mShownFrame.width() + "x"
9868 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009869 w.mSurface.setSize(width, height);
9870 w.mSurface.setPosition(w.mShownFrame.left,
9871 w.mShownFrame.top);
9872 } catch (RuntimeException e) {
9873 // If something goes wrong with the surface (such
9874 // as running out of memory), don't take down the
9875 // entire system.
9876 Log.e(TAG, "Failure updating surface of " + w
9877 + "size=(" + width + "x" + height
9878 + "), pos=(" + w.mShownFrame.left
9879 + "," + w.mShownFrame.top + ")", e);
9880 if (!recoveringMemory) {
9881 reclaimSomeSurfaceMemoryLocked(w, "size");
9882 }
9883 }
9884 }
9885 }
9886 if (!w.mAppFreezing) {
9887 w.mContentInsetsChanged =
9888 !w.mLastContentInsets.equals(w.mContentInsets);
9889 w.mVisibleInsetsChanged =
9890 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009891 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009892 || w.mContentInsetsChanged
9893 || w.mVisibleInsetsChanged) {
9894 w.mLastFrame.set(w.mFrame);
9895 w.mLastContentInsets.set(w.mContentInsets);
9896 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009897 // If the screen is currently frozen, then keep
9898 // it frozen until this window draws at its new
9899 // orientation.
9900 if (mDisplayFrozen) {
9901 if (DEBUG_ORIENTATION) Log.v(TAG,
9902 "Resizing while display frozen: " + w);
9903 w.mOrientationChanging = true;
9904 if (mWindowsFreezingScreen) {
9905 mWindowsFreezingScreen = true;
9906 // XXX should probably keep timeout from
9907 // when we first froze the display.
9908 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9909 mH.sendMessageDelayed(mH.obtainMessage(
9910 H.WINDOW_FREEZE_TIMEOUT), 2000);
9911 }
9912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009913 // If the orientation is changing, then we need to
9914 // hold off on unfreezing the display until this
9915 // window has been redrawn; to do that, we need
9916 // to go through the process of getting informed
9917 // by the application when it has finished drawing.
9918 if (w.mOrientationChanging) {
9919 if (DEBUG_ORIENTATION) Log.v(TAG,
9920 "Orientation start waiting for draw in "
9921 + w + ", surface " + w.mSurface);
9922 w.mDrawPending = true;
9923 w.mCommitDrawPending = false;
9924 w.mReadyToShow = false;
9925 if (w.mAppToken != null) {
9926 w.mAppToken.allDrawn = false;
9927 }
9928 }
Romain Guy06882f82009-06-10 13:36:04 -07009929 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930 "Resizing window " + w + " to " + w.mFrame);
9931 mResizingWindows.add(w);
9932 } else if (w.mOrientationChanging) {
9933 if (!w.mDrawPending && !w.mCommitDrawPending) {
9934 if (DEBUG_ORIENTATION) Log.v(TAG,
9935 "Orientation not waiting for draw in "
9936 + w + ", surface " + w.mSurface);
9937 w.mOrientationChanging = false;
9938 }
9939 }
9940 }
9941
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009942 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 if (!w.mLastHidden) {
9944 //dump();
9945 w.mLastHidden = true;
9946 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009947 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948 if (w.mSurface != null) {
9949 try {
9950 w.mSurface.hide();
9951 } catch (RuntimeException e) {
9952 Log.w(TAG, "Exception hiding surface in " + w);
9953 }
9954 }
9955 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9956 }
9957 // If we are waiting for this window to handle an
9958 // orientation change, well, it is hidden, so
9959 // doesn't really matter. Note that this does
9960 // introduce a potential glitch if the window
9961 // becomes unhidden before it has drawn for the
9962 // new orientation.
9963 if (w.mOrientationChanging) {
9964 w.mOrientationChanging = false;
9965 if (DEBUG_ORIENTATION) Log.v(TAG,
9966 "Orientation change skips hidden " + w);
9967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009968 } else if (w.mLastLayer != w.mAnimLayer
9969 || w.mLastAlpha != w.mShownAlpha
9970 || w.mLastDsDx != w.mDsDx
9971 || w.mLastDtDx != w.mDtDx
9972 || w.mLastDsDy != w.mDsDy
9973 || w.mLastDtDy != w.mDtDy
9974 || w.mLastHScale != w.mHScale
9975 || w.mLastVScale != w.mVScale
9976 || w.mLastHidden) {
9977 displayed = true;
9978 w.mLastAlpha = w.mShownAlpha;
9979 w.mLastLayer = w.mAnimLayer;
9980 w.mLastDsDx = w.mDsDx;
9981 w.mLastDtDx = w.mDtDx;
9982 w.mLastDsDy = w.mDsDy;
9983 w.mLastDtDy = w.mDtDy;
9984 w.mLastHScale = w.mHScale;
9985 w.mLastVScale = w.mVScale;
9986 if (SHOW_TRANSACTIONS) Log.i(
9987 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009988 + w.mShownAlpha + " layer=" + w.mAnimLayer
9989 + " matrix=[" + (w.mDsDx*w.mHScale)
9990 + "," + (w.mDtDx*w.mVScale)
9991 + "][" + (w.mDsDy*w.mHScale)
9992 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009993 if (w.mSurface != null) {
9994 try {
9995 w.mSurface.setAlpha(w.mShownAlpha);
9996 w.mSurface.setLayer(w.mAnimLayer);
9997 w.mSurface.setMatrix(
9998 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9999 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10000 } catch (RuntimeException e) {
10001 Log.w(TAG, "Error updating surface in " + w, e);
10002 if (!recoveringMemory) {
10003 reclaimSomeSurfaceMemoryLocked(w, "update");
10004 }
10005 }
10006 }
10007
10008 if (w.mLastHidden && !w.mDrawPending
10009 && !w.mCommitDrawPending
10010 && !w.mReadyToShow) {
10011 if (SHOW_TRANSACTIONS) Log.i(
10012 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
10013 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
10014 + " during relayout");
10015 if (showSurfaceRobustlyLocked(w)) {
10016 w.mHasDrawn = true;
10017 w.mLastHidden = false;
10018 } else {
10019 w.mOrientationChanging = false;
10020 }
10021 }
10022 if (w.mSurface != null) {
10023 w.mToken.hasVisible = true;
10024 }
10025 } else {
10026 displayed = true;
10027 }
10028
10029 if (displayed) {
10030 if (!covered) {
10031 if (attrs.width == LayoutParams.FILL_PARENT
10032 && attrs.height == LayoutParams.FILL_PARENT) {
10033 covered = true;
10034 }
10035 }
10036 if (w.mOrientationChanging) {
10037 if (w.mDrawPending || w.mCommitDrawPending) {
10038 orientationChangeComplete = false;
10039 if (DEBUG_ORIENTATION) Log.v(TAG,
10040 "Orientation continue waiting for draw in " + w);
10041 } else {
10042 w.mOrientationChanging = false;
10043 if (DEBUG_ORIENTATION) Log.v(TAG,
10044 "Orientation change complete in " + w);
10045 }
10046 }
10047 w.mToken.hasVisible = true;
10048 }
10049 } else if (w.mOrientationChanging) {
10050 if (DEBUG_ORIENTATION) Log.v(TAG,
10051 "Orientation change skips hidden " + w);
10052 w.mOrientationChanging = false;
10053 }
10054
10055 final boolean canBeSeen = w.isDisplayedLw();
10056
10057 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10058 focusDisplayed = true;
10059 }
10060
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010061 final boolean obscuredChanged = w.mObscured != obscured;
10062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010064 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 if (w.mSurface != null) {
10066 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10067 holdScreen = w.mSession;
10068 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010069 if (!syswin && w.mAttrs.screenBrightness >= 0
10070 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010071 screenBrightness = w.mAttrs.screenBrightness;
10072 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010073 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10074 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10075 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10076 syswin = true;
10077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010078 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010079
Dianne Hackborn25994b42009-09-04 14:21:19 -070010080 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10081 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010082 // This window completely covers everything behind it,
10083 // so we want to leave all of them as unblurred (for
10084 // performance reasons).
10085 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010086 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
10087 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010088 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010089 obscured = true;
10090 if (mBackgroundFillerSurface == null) {
10091 try {
10092 mBackgroundFillerSurface = new Surface(mFxSession, 0,
10093 0, dw, dh,
10094 PixelFormat.OPAQUE,
10095 Surface.FX_SURFACE_NORMAL);
10096 } catch (Exception e) {
10097 Log.e(TAG, "Exception creating filler surface", e);
10098 }
10099 }
10100 try {
10101 mBackgroundFillerSurface.setPosition(0, 0);
10102 mBackgroundFillerSurface.setSize(dw, dh);
10103 // Using the same layer as Dim because they will never be shown at the
10104 // same time.
10105 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10106 mBackgroundFillerSurface.show();
10107 } catch (RuntimeException e) {
10108 Log.e(TAG, "Exception showing filler surface");
10109 }
10110 backgroundFillerShown = true;
10111 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010112 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010113 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10114 if (localLOGV) Log.v(TAG, "Win " + w
10115 + ": blurring=" + blurring
10116 + " obscured=" + obscured
10117 + " displayed=" + displayed);
10118 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10119 if (!dimming) {
10120 //Log.i(TAG, "DIM BEHIND: " + w);
10121 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010122 if (mDimAnimator == null) {
10123 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010124 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010125 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010126 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010127 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010128 }
10129 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10130 if (!blurring) {
10131 //Log.i(TAG, "BLUR BEHIND: " + w);
10132 blurring = true;
10133 mBlurShown = true;
10134 if (mBlurSurface == null) {
10135 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10136 + mBlurSurface + ": CREATE");
10137 try {
Romain Guy06882f82009-06-10 13:36:04 -070010138 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010139 -1, 16, 16,
10140 PixelFormat.OPAQUE,
10141 Surface.FX_SURFACE_BLUR);
10142 } catch (Exception e) {
10143 Log.e(TAG, "Exception creating Blur surface", e);
10144 }
10145 }
10146 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10147 + mBlurSurface + ": SHOW pos=(0,0) (" +
10148 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10149 if (mBlurSurface != null) {
10150 mBlurSurface.setPosition(0, 0);
10151 mBlurSurface.setSize(dw, dh);
10152 try {
10153 mBlurSurface.show();
10154 } catch (RuntimeException e) {
10155 Log.w(TAG, "Failure showing blur surface", e);
10156 }
10157 }
10158 }
10159 mBlurSurface.setLayer(w.mAnimLayer-2);
10160 }
10161 }
10162 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010163
10164 if (obscuredChanged && mWallpaperTarget == w) {
10165 // This is the wallpaper target and its obscured state
10166 // changed... make sure the current wallaper's visibility
10167 // has been updated accordingly.
10168 updateWallpaperVisibilityLocked();
10169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010170 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010171
10172 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10173 mBackgroundFillerShown = false;
10174 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10175 try {
10176 mBackgroundFillerSurface.hide();
10177 } catch (RuntimeException e) {
10178 Log.e(TAG, "Exception hiding filler surface", e);
10179 }
10180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010181
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010182 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10183 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010184 }
Romain Guy06882f82009-06-10 13:36:04 -070010185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010186 if (!blurring && mBlurShown) {
10187 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10188 + ": HIDE");
10189 try {
10190 mBlurSurface.hide();
10191 } catch (IllegalArgumentException e) {
10192 Log.w(TAG, "Illegal argument exception hiding blur surface");
10193 }
10194 mBlurShown = false;
10195 }
10196
10197 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10198 } catch (RuntimeException e) {
10199 Log.e(TAG, "Unhandled exception in Window Manager", e);
10200 }
10201
10202 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010204 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10205 "With display frozen, orientationChangeComplete="
10206 + orientationChangeComplete);
10207 if (orientationChangeComplete) {
10208 if (mWindowsFreezingScreen) {
10209 mWindowsFreezingScreen = false;
10210 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10211 }
10212 if (mAppsFreezingScreen == 0) {
10213 stopFreezingDisplayLocked();
10214 }
10215 }
Romain Guy06882f82009-06-10 13:36:04 -070010216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010217 i = mResizingWindows.size();
10218 if (i > 0) {
10219 do {
10220 i--;
10221 WindowState win = mResizingWindows.get(i);
10222 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010223 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10224 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010225 win.mClient.resized(win.mFrame.width(),
10226 win.mFrame.height(), win.mLastContentInsets,
10227 win.mLastVisibleInsets, win.mDrawPending);
10228 win.mContentInsetsChanged = false;
10229 win.mVisibleInsetsChanged = false;
10230 } catch (RemoteException e) {
10231 win.mOrientationChanging = false;
10232 }
10233 } while (i > 0);
10234 mResizingWindows.clear();
10235 }
Romain Guy06882f82009-06-10 13:36:04 -070010236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010237 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010238 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010239 i = mDestroySurface.size();
10240 if (i > 0) {
10241 do {
10242 i--;
10243 WindowState win = mDestroySurface.get(i);
10244 win.mDestroying = false;
10245 if (mInputMethodWindow == win) {
10246 mInputMethodWindow = null;
10247 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010248 if (win == mWallpaperTarget) {
10249 wallpaperDestroyed = true;
10250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010251 win.destroySurfaceLocked();
10252 } while (i > 0);
10253 mDestroySurface.clear();
10254 }
10255
10256 // Time to remove any exiting tokens?
10257 for (i=mExitingTokens.size()-1; i>=0; i--) {
10258 WindowToken token = mExitingTokens.get(i);
10259 if (!token.hasVisible) {
10260 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010261 if (token.windowType == TYPE_WALLPAPER) {
10262 mWallpaperTokens.remove(token);
10263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010264 }
10265 }
10266
10267 // Time to remove any exiting applications?
10268 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10269 AppWindowToken token = mExitingAppTokens.get(i);
10270 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010271 // Make sure there is no animation running on this token,
10272 // so any windows associated with it will be removed as
10273 // soon as their animations are complete
10274 token.animation = null;
10275 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276 mAppTokens.remove(token);
10277 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010278 if (mLastEnterAnimToken == token) {
10279 mLastEnterAnimToken = null;
10280 mLastEnterAnimParams = null;
10281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010282 }
10283 }
10284
Dianne Hackborna8f60182009-09-01 19:01:50 -070010285 boolean needRelayout = false;
10286
10287 if (!animating && mAppTransitionRunning) {
10288 // We have finished the animation of an app transition. To do
10289 // this, we have delayed a lot of operations like showing and
10290 // hiding apps, moving apps in Z-order, etc. The app token list
10291 // reflects the correct Z-order, but the window list may now
10292 // be out of sync with it. So here we will just rebuild the
10293 // entire app window list. Fun!
10294 mAppTransitionRunning = false;
10295 needRelayout = true;
10296 rebuildAppWindowListLocked();
10297 // Clear information about apps that were moving.
10298 mToBottomApps.clear();
10299 }
10300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010301 if (focusDisplayed) {
10302 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10303 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010304 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010305 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010306 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010307 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010308 requestAnimationLocked(0);
10309 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10311 }
10312 mQueue.setHoldScreenLocked(holdScreen != null);
10313 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10314 mPowerManager.setScreenBrightnessOverride(-1);
10315 } else {
10316 mPowerManager.setScreenBrightnessOverride((int)
10317 (screenBrightness * Power.BRIGHTNESS_ON));
10318 }
10319 if (holdScreen != mHoldingScreenOn) {
10320 mHoldingScreenOn = holdScreen;
10321 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10322 mH.sendMessage(m);
10323 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010324
10325 if (mTurnOnScreen) {
10326 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10327 LocalPowerManager.BUTTON_EVENT, true);
10328 mTurnOnScreen = false;
10329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330 }
10331
10332 void requestAnimationLocked(long delay) {
10333 if (!mAnimationPending) {
10334 mAnimationPending = true;
10335 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10336 }
10337 }
Romain Guy06882f82009-06-10 13:36:04 -070010338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010339 /**
10340 * Have the surface flinger show a surface, robustly dealing with
10341 * error conditions. In particular, if there is not enough memory
10342 * to show the surface, then we will try to get rid of other surfaces
10343 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010344 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 * @return Returns true if the surface was successfully shown.
10346 */
10347 boolean showSurfaceRobustlyLocked(WindowState win) {
10348 try {
10349 if (win.mSurface != null) {
10350 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010351 if (win.mTurnOnScreen) {
10352 win.mTurnOnScreen = false;
10353 mTurnOnScreen = true;
10354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010355 }
10356 return true;
10357 } catch (RuntimeException e) {
10358 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10359 }
Romain Guy06882f82009-06-10 13:36:04 -070010360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 return false;
10364 }
Romain Guy06882f82009-06-10 13:36:04 -070010365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010366 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10367 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010369 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
10370 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 if (mForceRemoves == null) {
10373 mForceRemoves = new ArrayList<WindowState>();
10374 }
Romain Guy06882f82009-06-10 13:36:04 -070010375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010376 long callingIdentity = Binder.clearCallingIdentity();
10377 try {
10378 // There was some problem... first, do a sanity check of the
10379 // window list to make sure we haven't left any dangling surfaces
10380 // around.
10381 int N = mWindows.size();
10382 boolean leakedSurface = false;
10383 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10384 for (int i=0; i<N; i++) {
10385 WindowState ws = (WindowState)mWindows.get(i);
10386 if (ws.mSurface != null) {
10387 if (!mSessions.contains(ws.mSession)) {
10388 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10389 + ws + " surface=" + ws.mSurface
10390 + " token=" + win.mToken
10391 + " pid=" + ws.mSession.mPid
10392 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010393 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 ws.mSurface = null;
10395 mForceRemoves.add(ws);
10396 i--;
10397 N--;
10398 leakedSurface = true;
10399 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10400 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10401 + ws + " surface=" + ws.mSurface
10402 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010403 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010404 ws.mSurface = null;
10405 leakedSurface = true;
10406 }
10407 }
10408 }
Romain Guy06882f82009-06-10 13:36:04 -070010409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 boolean killedApps = false;
10411 if (!leakedSurface) {
10412 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10413 SparseIntArray pidCandidates = new SparseIntArray();
10414 for (int i=0; i<N; i++) {
10415 WindowState ws = (WindowState)mWindows.get(i);
10416 if (ws.mSurface != null) {
10417 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10418 }
10419 }
10420 if (pidCandidates.size() > 0) {
10421 int[] pids = new int[pidCandidates.size()];
10422 for (int i=0; i<pids.length; i++) {
10423 pids[i] = pidCandidates.keyAt(i);
10424 }
10425 try {
10426 if (mActivityManager.killPidsForMemory(pids)) {
10427 killedApps = true;
10428 }
10429 } catch (RemoteException e) {
10430 }
10431 }
10432 }
Romain Guy06882f82009-06-10 13:36:04 -070010433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 if (leakedSurface || killedApps) {
10435 // We managed to reclaim some memory, so get rid of the trouble
10436 // surface and ask the app to request another one.
10437 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10438 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010439 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010440 win.mSurface = null;
10441 }
Romain Guy06882f82009-06-10 13:36:04 -070010442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010443 try {
10444 win.mClient.dispatchGetNewSurface();
10445 } catch (RemoteException e) {
10446 }
10447 }
10448 } finally {
10449 Binder.restoreCallingIdentity(callingIdentity);
10450 }
10451 }
Romain Guy06882f82009-06-10 13:36:04 -070010452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 private boolean updateFocusedWindowLocked(int mode) {
10454 WindowState newFocus = computeFocusedWindowLocked();
10455 if (mCurrentFocus != newFocus) {
10456 // This check makes sure that we don't already have the focus
10457 // change message pending.
10458 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10459 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10460 if (localLOGV) Log.v(
10461 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10462 final WindowState oldFocus = mCurrentFocus;
10463 mCurrentFocus = newFocus;
10464 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010466 final WindowState imWindow = mInputMethodWindow;
10467 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010468 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010469 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010470 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10471 mLayoutNeeded = true;
10472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010473 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10474 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010475 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10476 // Client will do the layout, but we need to assign layers
10477 // for handleNewWindowLocked() below.
10478 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479 }
10480 }
Romain Guy06882f82009-06-10 13:36:04 -070010481
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010482 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10483 mKeyWaiter.handleNewWindowLocked(newFocus);
10484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010485 return true;
10486 }
10487 return false;
10488 }
10489
10490 private WindowState computeFocusedWindowLocked() {
10491 WindowState result = null;
10492 WindowState win;
10493
10494 int i = mWindows.size() - 1;
10495 int nextAppIndex = mAppTokens.size()-1;
10496 WindowToken nextApp = nextAppIndex >= 0
10497 ? mAppTokens.get(nextAppIndex) : null;
10498
10499 while (i >= 0) {
10500 win = (WindowState)mWindows.get(i);
10501
10502 if (localLOGV || DEBUG_FOCUS) Log.v(
10503 TAG, "Looking for focus: " + i
10504 + " = " + win
10505 + ", flags=" + win.mAttrs.flags
10506 + ", canReceive=" + win.canReceiveKeys());
10507
10508 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010510 // If this window's application has been removed, just skip it.
10511 if (thisApp != null && thisApp.removed) {
10512 i--;
10513 continue;
10514 }
Romain Guy06882f82009-06-10 13:36:04 -070010515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 // If there is a focused app, don't allow focus to go to any
10517 // windows below it. If this is an application window, step
10518 // through the app tokens until we find its app.
10519 if (thisApp != null && nextApp != null && thisApp != nextApp
10520 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10521 int origAppIndex = nextAppIndex;
10522 while (nextAppIndex > 0) {
10523 if (nextApp == mFocusedApp) {
10524 // Whoops, we are below the focused app... no focus
10525 // for you!
10526 if (localLOGV || DEBUG_FOCUS) Log.v(
10527 TAG, "Reached focused app: " + mFocusedApp);
10528 return null;
10529 }
10530 nextAppIndex--;
10531 nextApp = mAppTokens.get(nextAppIndex);
10532 if (nextApp == thisApp) {
10533 break;
10534 }
10535 }
10536 if (thisApp != nextApp) {
10537 // Uh oh, the app token doesn't exist! This shouldn't
10538 // happen, but if it does we can get totally hosed...
10539 // so restart at the original app.
10540 nextAppIndex = origAppIndex;
10541 nextApp = mAppTokens.get(nextAppIndex);
10542 }
10543 }
10544
10545 // Dispatch to this window if it is wants key events.
10546 if (win.canReceiveKeys()) {
10547 if (DEBUG_FOCUS) Log.v(
10548 TAG, "Found focus @ " + i + " = " + win);
10549 result = win;
10550 break;
10551 }
10552
10553 i--;
10554 }
10555
10556 return result;
10557 }
10558
10559 private void startFreezingDisplayLocked() {
10560 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010561 // Freezing the display also suspends key event delivery, to
10562 // keep events from going astray while the display is reconfigured.
10563 // If someone has changed orientation again while the screen is
10564 // still frozen, the events will continue to be blocked while the
10565 // successive orientation change is processed. To prevent spurious
10566 // ANRs, we reset the event dispatch timeout in this case.
10567 synchronized (mKeyWaiter) {
10568 mKeyWaiter.mWasFrozen = true;
10569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 return;
10571 }
Romain Guy06882f82009-06-10 13:36:04 -070010572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010575 long now = SystemClock.uptimeMillis();
10576 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10577 if (mFreezeGcPending != 0) {
10578 if (now > (mFreezeGcPending+1000)) {
10579 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10580 mH.removeMessages(H.FORCE_GC);
10581 Runtime.getRuntime().gc();
10582 mFreezeGcPending = now;
10583 }
10584 } else {
10585 mFreezeGcPending = now;
10586 }
Romain Guy06882f82009-06-10 13:36:04 -070010587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010588 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010589 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10590 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010591 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010592 mAppTransitionReady = true;
10593 }
Romain Guy06882f82009-06-10 13:36:04 -070010594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 if (PROFILE_ORIENTATION) {
10596 File file = new File("/data/system/frozen");
10597 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10598 }
10599 Surface.freezeDisplay(0);
10600 }
Romain Guy06882f82009-06-10 13:36:04 -070010601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010602 private void stopFreezingDisplayLocked() {
10603 if (!mDisplayFrozen) {
10604 return;
10605 }
Romain Guy06882f82009-06-10 13:36:04 -070010606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 mDisplayFrozen = false;
10608 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10609 if (PROFILE_ORIENTATION) {
10610 Debug.stopMethodTracing();
10611 }
10612 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010613
Chris Tate2ad63a92009-03-25 17:36:48 -070010614 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10615 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010616 synchronized (mKeyWaiter) {
10617 mKeyWaiter.mWasFrozen = true;
10618 mKeyWaiter.notifyAll();
10619 }
10620
10621 // A little kludge: a lot could have happened while the
10622 // display was frozen, so now that we are coming back we
10623 // do a gc so that any remote references the system
10624 // processes holds on others can be released if they are
10625 // no longer needed.
10626 mH.removeMessages(H.FORCE_GC);
10627 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10628 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 mScreenFrozenLock.release();
10631 }
Romain Guy06882f82009-06-10 13:36:04 -070010632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010633 @Override
10634 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10635 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10636 != PackageManager.PERMISSION_GRANTED) {
10637 pw.println("Permission Denial: can't dump WindowManager from from pid="
10638 + Binder.getCallingPid()
10639 + ", uid=" + Binder.getCallingUid());
10640 return;
10641 }
Romain Guy06882f82009-06-10 13:36:04 -070010642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010643 synchronized(mWindowMap) {
10644 pw.println("Current Window Manager state:");
10645 for (int i=mWindows.size()-1; i>=0; i--) {
10646 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010647 pw.print(" Window #"); pw.print(i); pw.print(' ');
10648 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649 w.dump(pw, " ");
10650 }
10651 if (mInputMethodDialogs.size() > 0) {
10652 pw.println(" ");
10653 pw.println(" Input method dialogs:");
10654 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10655 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010656 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010657 }
10658 }
10659 if (mPendingRemove.size() > 0) {
10660 pw.println(" ");
10661 pw.println(" Remove pending for:");
10662 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10663 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010664 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10665 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 w.dump(pw, " ");
10667 }
10668 }
10669 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10670 pw.println(" ");
10671 pw.println(" Windows force removing:");
10672 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10673 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010674 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10675 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010676 w.dump(pw, " ");
10677 }
10678 }
10679 if (mDestroySurface.size() > 0) {
10680 pw.println(" ");
10681 pw.println(" Windows waiting to destroy their surface:");
10682 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10683 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010684 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10685 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 w.dump(pw, " ");
10687 }
10688 }
10689 if (mLosingFocus.size() > 0) {
10690 pw.println(" ");
10691 pw.println(" Windows losing focus:");
10692 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10693 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010694 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10695 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010696 w.dump(pw, " ");
10697 }
10698 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010699 if (mResizingWindows.size() > 0) {
10700 pw.println(" ");
10701 pw.println(" Windows waiting to resize:");
10702 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10703 WindowState w = mResizingWindows.get(i);
10704 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10705 pw.print(w); pw.println(":");
10706 w.dump(pw, " ");
10707 }
10708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010709 if (mSessions.size() > 0) {
10710 pw.println(" ");
10711 pw.println(" All active sessions:");
10712 Iterator<Session> it = mSessions.iterator();
10713 while (it.hasNext()) {
10714 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010715 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010716 s.dump(pw, " ");
10717 }
10718 }
10719 if (mTokenMap.size() > 0) {
10720 pw.println(" ");
10721 pw.println(" All tokens:");
10722 Iterator<WindowToken> it = mTokenMap.values().iterator();
10723 while (it.hasNext()) {
10724 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010725 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010726 token.dump(pw, " ");
10727 }
10728 }
10729 if (mTokenList.size() > 0) {
10730 pw.println(" ");
10731 pw.println(" Window token list:");
10732 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010733 pw.print(" #"); pw.print(i); pw.print(": ");
10734 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 }
10736 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010737 if (mWallpaperTokens.size() > 0) {
10738 pw.println(" ");
10739 pw.println(" Wallpaper tokens:");
10740 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10741 WindowToken token = mWallpaperTokens.get(i);
10742 pw.print(" Wallpaper #"); pw.print(i);
10743 pw.print(' '); pw.print(token); pw.println(':');
10744 token.dump(pw, " ");
10745 }
10746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010747 if (mAppTokens.size() > 0) {
10748 pw.println(" ");
10749 pw.println(" Application tokens in Z order:");
10750 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010751 pw.print(" App #"); pw.print(i); pw.print(": ");
10752 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010753 }
10754 }
10755 if (mFinishedStarting.size() > 0) {
10756 pw.println(" ");
10757 pw.println(" Finishing start of application tokens:");
10758 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10759 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010760 pw.print(" Finished Starting #"); pw.print(i);
10761 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 token.dump(pw, " ");
10763 }
10764 }
10765 if (mExitingTokens.size() > 0) {
10766 pw.println(" ");
10767 pw.println(" Exiting tokens:");
10768 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10769 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010770 pw.print(" Exiting #"); pw.print(i);
10771 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010772 token.dump(pw, " ");
10773 }
10774 }
10775 if (mExitingAppTokens.size() > 0) {
10776 pw.println(" ");
10777 pw.println(" Exiting application tokens:");
10778 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10779 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010780 pw.print(" Exiting App #"); pw.print(i);
10781 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010782 token.dump(pw, " ");
10783 }
10784 }
10785 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010786 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10787 pw.print(" mLastFocus="); pw.println(mLastFocus);
10788 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10789 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10790 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010791 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010792 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10793 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10794 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10795 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010796 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10797 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10798 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10799 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10800 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010801 if (mDimAnimator != null) {
10802 mDimAnimator.printTo(pw);
10803 } else {
10804 pw.print( " no DimAnimator ");
10805 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010806 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010807 pw.print(mInputMethodAnimLayerAdjustment);
10808 pw.print(" mWallpaperAnimLayerAdjustment=");
10809 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010810 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10811 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010812 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10813 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10814 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10815 pw.print(" mRotation="); pw.print(mRotation);
10816 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10817 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10818 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10819 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10820 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10821 pw.print(" mNextAppTransition=0x");
10822 pw.print(Integer.toHexString(mNextAppTransition));
10823 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010824 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010825 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010826 if (mNextAppTransitionPackage != null) {
10827 pw.print(" mNextAppTransitionPackage=");
10828 pw.print(mNextAppTransitionPackage);
10829 pw.print(", mNextAppTransitionEnter=0x");
10830 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10831 pw.print(", mNextAppTransitionExit=0x");
10832 pw.print(Integer.toHexString(mNextAppTransitionExit));
10833 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010834 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10835 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010836 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10837 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10838 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10839 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010840 if (mOpeningApps.size() > 0) {
10841 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10842 }
10843 if (mClosingApps.size() > 0) {
10844 pw.print(" mClosingApps="); pw.println(mClosingApps);
10845 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010846 if (mToTopApps.size() > 0) {
10847 pw.print(" mToTopApps="); pw.println(mToTopApps);
10848 }
10849 if (mToBottomApps.size() > 0) {
10850 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10851 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010852 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10853 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010854 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010855 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10856 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10857 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10858 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10859 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10860 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010861 }
10862 }
10863
10864 public void monitor() {
10865 synchronized (mWindowMap) { }
10866 synchronized (mKeyguardDisabled) { }
10867 synchronized (mKeyWaiter) { }
10868 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010869
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010870 public void virtualKeyFeedback(KeyEvent event) {
10871 mPolicy.keyFeedbackFromInput(event);
10872 }
10873
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010874 /**
10875 * DimAnimator class that controls the dim animation. This holds the surface and
10876 * all state used for dim animation.
10877 */
10878 private static class DimAnimator {
10879 Surface mDimSurface;
10880 boolean mDimShown = false;
10881 float mDimCurrentAlpha;
10882 float mDimTargetAlpha;
10883 float mDimDeltaPerMs;
10884 long mLastDimAnimTime;
10885
10886 DimAnimator (SurfaceSession session) {
10887 if (mDimSurface == null) {
10888 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10889 + mDimSurface + ": CREATE");
10890 try {
10891 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10892 Surface.FX_SURFACE_DIM);
10893 } catch (Exception e) {
10894 Log.e(TAG, "Exception creating Dim surface", e);
10895 }
10896 }
10897 }
10898
10899 /**
10900 * Show the dim surface.
10901 */
10902 void show(int dw, int dh) {
10903 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10904 dw + "x" + dh + ")");
10905 mDimShown = true;
10906 try {
10907 mDimSurface.setPosition(0, 0);
10908 mDimSurface.setSize(dw, dh);
10909 mDimSurface.show();
10910 } catch (RuntimeException e) {
10911 Log.w(TAG, "Failure showing dim surface", e);
10912 }
10913 }
10914
10915 /**
10916 * Set's the dim surface's layer and update dim parameters that will be used in
10917 * {@link updateSurface} after all windows are examined.
10918 */
10919 void updateParameters(WindowState w, long currentTime) {
10920 mDimSurface.setLayer(w.mAnimLayer-1);
10921
10922 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010923 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10924 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010925 if (mDimTargetAlpha != target) {
10926 // If the desired dim level has changed, then
10927 // start an animation to it.
10928 mLastDimAnimTime = currentTime;
10929 long duration = (w.mAnimating && w.mAnimation != null)
10930 ? w.mAnimation.computeDurationHint()
10931 : DEFAULT_DIM_DURATION;
10932 if (target > mDimTargetAlpha) {
10933 // This is happening behind the activity UI,
10934 // so we can make it run a little longer to
10935 // give a stronger impression without disrupting
10936 // the user.
10937 duration *= DIM_DURATION_MULTIPLIER;
10938 }
10939 if (duration < 1) {
10940 // Don't divide by zero
10941 duration = 1;
10942 }
10943 mDimTargetAlpha = target;
10944 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10945 }
10946 }
10947
10948 /**
10949 * Updating the surface's alpha. Returns true if the animation continues, or returns
10950 * false when the animation is finished and the dim surface is hidden.
10951 */
10952 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10953 if (!dimming) {
10954 if (mDimTargetAlpha != 0) {
10955 mLastDimAnimTime = currentTime;
10956 mDimTargetAlpha = 0;
10957 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10958 }
10959 }
10960
10961 boolean animating = false;
10962 if (mLastDimAnimTime != 0) {
10963 mDimCurrentAlpha += mDimDeltaPerMs
10964 * (currentTime-mLastDimAnimTime);
10965 boolean more = true;
10966 if (displayFrozen) {
10967 // If the display is frozen, there is no reason to animate.
10968 more = false;
10969 } else if (mDimDeltaPerMs > 0) {
10970 if (mDimCurrentAlpha > mDimTargetAlpha) {
10971 more = false;
10972 }
10973 } else if (mDimDeltaPerMs < 0) {
10974 if (mDimCurrentAlpha < mDimTargetAlpha) {
10975 more = false;
10976 }
10977 } else {
10978 more = false;
10979 }
10980
10981 // Do we need to continue animating?
10982 if (more) {
10983 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10984 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10985 mLastDimAnimTime = currentTime;
10986 mDimSurface.setAlpha(mDimCurrentAlpha);
10987 animating = true;
10988 } else {
10989 mDimCurrentAlpha = mDimTargetAlpha;
10990 mLastDimAnimTime = 0;
10991 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10992 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10993 mDimSurface.setAlpha(mDimCurrentAlpha);
10994 if (!dimming) {
10995 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10996 + ": HIDE");
10997 try {
10998 mDimSurface.hide();
10999 } catch (RuntimeException e) {
11000 Log.w(TAG, "Illegal argument exception hiding dim surface");
11001 }
11002 mDimShown = false;
11003 }
11004 }
11005 }
11006 return animating;
11007 }
11008
11009 public void printTo(PrintWriter pw) {
11010 pw.print(" mDimShown="); pw.print(mDimShown);
11011 pw.print(" current="); pw.print(mDimCurrentAlpha);
11012 pw.print(" target="); pw.print(mDimTargetAlpha);
11013 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11014 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11015 }
11016 }
11017
11018 /**
11019 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11020 * This is used for opening/closing transition for apps in compatible mode.
11021 */
11022 private static class FadeInOutAnimation extends Animation {
11023 int mWidth;
11024 boolean mFadeIn;
11025
11026 public FadeInOutAnimation(boolean fadeIn) {
11027 setInterpolator(new AccelerateInterpolator());
11028 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11029 mFadeIn = fadeIn;
11030 }
11031
11032 @Override
11033 protected void applyTransformation(float interpolatedTime, Transformation t) {
11034 float x = interpolatedTime;
11035 if (!mFadeIn) {
11036 x = 1.0f - x; // reverse the interpolation for fade out
11037 }
11038 if (x < 0.5) {
11039 // move the window out of the screen.
11040 t.getMatrix().setTranslate(mWidth, 0);
11041 } else {
11042 t.getMatrix().setTranslate(0, 0);// show
11043 t.setAlpha((x - 0.5f) * 2);
11044 }
11045 }
11046
11047 @Override
11048 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11049 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11050 mWidth = width;
11051 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011052
11053 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011054 public int getZAdjustment() {
11055 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011056 }
11057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011058}