blob: 94667ebbac3ef9f7937ee78ea004f181721f63b5 [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;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700436 // This is set when we are waiting for a wallpaper to tell us it is done
437 // changing its scroll position.
438 WindowState mWaitingOnWallpaper;
439 // The last time we had a timeout when waiting for a wallpaper.
440 long mLastWallpaperTimeoutTime;
441 // We give a wallpaper up to 150ms to finish scrolling.
442 static final long WALLPAPER_TIMEOUT = 150;
443 // Time we wait after a timeout before trying to wait again.
444 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 AppWindowToken mFocusedApp = null;
447
448 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 float mWindowAnimationScale = 1.0f;
451 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 final KeyWaiter mKeyWaiter = new KeyWaiter();
454 final KeyQ mQueue;
455 final InputDispatcherThread mInputThread;
456
457 // Who is holding the screen on.
458 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700459
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700460 boolean mTurnOnScreen;
461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 /**
463 * Whether the UI is currently running in touch mode (not showing
464 * navigational focus because the user is directly pressing the screen).
465 */
466 boolean mInTouchMode = false;
467
468 private ViewServer mViewServer;
469
470 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700471
Dianne Hackbornc485a602009-03-24 22:39:49 -0700472 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700473 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700474
475 // The frame use to limit the size of the app running in compatibility mode.
476 Rect mCompatibleScreenFrame = new Rect();
477 // The surface used to fill the outer rim of the app running in compatibility mode.
478 Surface mBackgroundFillerSurface = null;
479 boolean mBackgroundFillerShown = false;
480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 public static WindowManagerService main(Context context,
482 PowerManagerService pm, boolean haveInputMethods) {
483 WMThread thr = new WMThread(context, pm, haveInputMethods);
484 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 synchronized (thr) {
487 while (thr.mService == null) {
488 try {
489 thr.wait();
490 } catch (InterruptedException e) {
491 }
492 }
493 }
Romain Guy06882f82009-06-10 13:36:04 -0700494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 return thr.mService;
496 }
Romain Guy06882f82009-06-10 13:36:04 -0700497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 static class WMThread extends Thread {
499 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 private final Context mContext;
502 private final PowerManagerService mPM;
503 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 public WMThread(Context context, PowerManagerService pm,
506 boolean haveInputMethods) {
507 super("WindowManager");
508 mContext = context;
509 mPM = pm;
510 mHaveInputMethods = haveInputMethods;
511 }
Romain Guy06882f82009-06-10 13:36:04 -0700512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 public void run() {
514 Looper.prepare();
515 WindowManagerService s = new WindowManagerService(mContext, mPM,
516 mHaveInputMethods);
517 android.os.Process.setThreadPriority(
518 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 synchronized (this) {
521 mService = s;
522 notifyAll();
523 }
Romain Guy06882f82009-06-10 13:36:04 -0700524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 Looper.loop();
526 }
527 }
528
529 static class PolicyThread extends Thread {
530 private final WindowManagerPolicy mPolicy;
531 private final WindowManagerService mService;
532 private final Context mContext;
533 private final PowerManagerService mPM;
534 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 public PolicyThread(WindowManagerPolicy policy,
537 WindowManagerService service, Context context,
538 PowerManagerService pm) {
539 super("WindowManagerPolicy");
540 mPolicy = policy;
541 mService = service;
542 mContext = context;
543 mPM = pm;
544 }
Romain Guy06882f82009-06-10 13:36:04 -0700545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 public void run() {
547 Looper.prepare();
548 //Looper.myLooper().setMessageLogging(new LogPrinter(
549 // Log.VERBOSE, "WindowManagerPolicy"));
550 android.os.Process.setThreadPriority(
551 android.os.Process.THREAD_PRIORITY_FOREGROUND);
552 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 synchronized (this) {
555 mRunning = true;
556 notifyAll();
557 }
Romain Guy06882f82009-06-10 13:36:04 -0700558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 Looper.loop();
560 }
561 }
562
563 private WindowManagerService(Context context, PowerManagerService pm,
564 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700565 if (MEASURE_LATENCY) {
566 lt = new LatencyTimer(100, 1000);
567 }
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 mContext = context;
570 mHaveInputMethods = haveInputMethods;
571 mLimitedAlphaCompositing = context.getResources().getBoolean(
572 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 mPowerManager = pm;
575 mPowerManager.setPolicy(mPolicy);
576 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
577 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
578 "SCREEN_FROZEN");
579 mScreenFrozenLock.setReferenceCounted(false);
580
581 mActivityManager = ActivityManagerNative.getDefault();
582 mBatteryStats = BatteryStatsService.getService();
583
584 // Get persisted window scale setting
585 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
586 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
587 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
588 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700589
Michael Chan9f028e62009-08-04 17:37:46 -0700590 int max_events_per_sec = 35;
591 try {
592 max_events_per_sec = Integer.parseInt(SystemProperties
593 .get("windowsmgr.max_events_per_sec"));
594 if (max_events_per_sec < 1) {
595 max_events_per_sec = 35;
596 }
597 } catch (NumberFormatException e) {
598 }
599 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 mQueue = new KeyQ();
602
603 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
606 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 synchronized (thr) {
609 while (!thr.mRunning) {
610 try {
611 thr.wait();
612 } catch (InterruptedException e) {
613 }
614 }
615 }
Romain Guy06882f82009-06-10 13:36:04 -0700616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 // Add ourself to the Watchdog monitors.
620 Watchdog.getInstance().addMonitor(this);
621 }
622
623 @Override
624 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
625 throws RemoteException {
626 try {
627 return super.onTransact(code, data, reply, flags);
628 } catch (RuntimeException e) {
629 // The window manager only throws security exceptions, so let's
630 // log all others.
631 if (!(e instanceof SecurityException)) {
632 Log.e(TAG, "Window Manager Crash", e);
633 }
634 throw e;
635 }
636 }
637
638 private void placeWindowAfter(Object pos, WindowState window) {
639 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700640 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 TAG, "Adding window " + window + " at "
642 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
643 mWindows.add(i+1, window);
644 }
645
646 private void placeWindowBefore(Object pos, WindowState window) {
647 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700648 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 TAG, "Adding window " + window + " at "
650 + i + " of " + mWindows.size() + " (before " + pos + ")");
651 mWindows.add(i, window);
652 }
653
654 //This method finds out the index of a window that has the same app token as
655 //win. used for z ordering the windows in mWindows
656 private int findIdxBasedOnAppTokens(WindowState win) {
657 //use a local variable to cache mWindows
658 ArrayList localmWindows = mWindows;
659 int jmax = localmWindows.size();
660 if(jmax == 0) {
661 return -1;
662 }
663 for(int j = (jmax-1); j >= 0; j--) {
664 WindowState wentry = (WindowState)localmWindows.get(j);
665 if(wentry.mAppToken == win.mAppToken) {
666 return j;
667 }
668 }
669 return -1;
670 }
Romain Guy06882f82009-06-10 13:36:04 -0700671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
673 final IWindow client = win.mClient;
674 final WindowToken token = win.mToken;
675 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 final int N = localmWindows.size();
678 final WindowState attached = win.mAttachedWindow;
679 int i;
680 if (attached == null) {
681 int tokenWindowsPos = token.windows.size();
682 if (token.appWindowToken != null) {
683 int index = tokenWindowsPos-1;
684 if (index >= 0) {
685 // If this application has existing windows, we
686 // simply place the new window on top of them... but
687 // keep the starting window on top.
688 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
689 // Base windows go behind everything else.
690 placeWindowBefore(token.windows.get(0), win);
691 tokenWindowsPos = 0;
692 } else {
693 AppWindowToken atoken = win.mAppToken;
694 if (atoken != null &&
695 token.windows.get(index) == atoken.startingWindow) {
696 placeWindowBefore(token.windows.get(index), win);
697 tokenWindowsPos--;
698 } else {
699 int newIdx = findIdxBasedOnAppTokens(win);
700 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700701 //there is a window above this one associated with the same
702 //apptoken note that the window could be a floating window
703 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700705 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
706 TAG, "Adding window " + win + " at "
707 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711 }
712 } else {
713 if (localLOGV) Log.v(
714 TAG, "Figuring out where to add app window "
715 + client.asBinder() + " (token=" + token + ")");
716 // Figure out where the window should go, based on the
717 // order of applications.
718 final int NA = mAppTokens.size();
719 Object pos = null;
720 for (i=NA-1; i>=0; i--) {
721 AppWindowToken t = mAppTokens.get(i);
722 if (t == token) {
723 i--;
724 break;
725 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700726
727 // We haven't reached the token yet; if this token
728 // is not going to the bottom and has windows, we can
729 // use it as an anchor for when we do reach the token.
730 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 pos = t.windows.get(0);
732 }
733 }
734 // We now know the index into the apps. If we found
735 // an app window above, that gives us the position; else
736 // we need to look some more.
737 if (pos != null) {
738 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700739 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 mTokenMap.get(((WindowState)pos).mClient.asBinder());
741 if (atoken != null) {
742 final int NC = atoken.windows.size();
743 if (NC > 0) {
744 WindowState bottom = atoken.windows.get(0);
745 if (bottom.mSubLayer < 0) {
746 pos = bottom;
747 }
748 }
749 }
750 placeWindowBefore(pos, win);
751 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700752 // Continue looking down until we find the first
753 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 while (i >= 0) {
755 AppWindowToken t = mAppTokens.get(i);
756 final int NW = t.windows.size();
757 if (NW > 0) {
758 pos = t.windows.get(NW-1);
759 break;
760 }
761 i--;
762 }
763 if (pos != null) {
764 // Move in front of any windows attached to this
765 // one.
766 WindowToken atoken =
767 mTokenMap.get(((WindowState)pos).mClient.asBinder());
768 if (atoken != null) {
769 final int NC = atoken.windows.size();
770 if (NC > 0) {
771 WindowState top = atoken.windows.get(NC-1);
772 if (top.mSubLayer >= 0) {
773 pos = top;
774 }
775 }
776 }
777 placeWindowAfter(pos, win);
778 } else {
779 // Just search for the start of this layer.
780 final int myLayer = win.mBaseLayer;
781 for (i=0; i<N; i++) {
782 WindowState w = (WindowState)localmWindows.get(i);
783 if (w.mBaseLayer > myLayer) {
784 break;
785 }
786 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700787 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
788 TAG, "Adding window " + win + " at "
789 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 localmWindows.add(i, win);
791 }
792 }
793 }
794 } else {
795 // Figure out where window should go, based on layer.
796 final int myLayer = win.mBaseLayer;
797 for (i=N-1; i>=0; i--) {
798 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
799 i++;
800 break;
801 }
802 }
803 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700804 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
805 TAG, "Adding window " + win + " at "
806 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 localmWindows.add(i, win);
808 }
809 if (addToToken) {
810 token.windows.add(tokenWindowsPos, win);
811 }
812
813 } else {
814 // Figure out this window's ordering relative to the window
815 // it is attached to.
816 final int NA = token.windows.size();
817 final int sublayer = win.mSubLayer;
818 int largestSublayer = Integer.MIN_VALUE;
819 WindowState windowWithLargestSublayer = null;
820 for (i=0; i<NA; i++) {
821 WindowState w = token.windows.get(i);
822 final int wSublayer = w.mSubLayer;
823 if (wSublayer >= largestSublayer) {
824 largestSublayer = wSublayer;
825 windowWithLargestSublayer = w;
826 }
827 if (sublayer < 0) {
828 // For negative sublayers, we go below all windows
829 // in the same sublayer.
830 if (wSublayer >= sublayer) {
831 if (addToToken) {
832 token.windows.add(i, win);
833 }
834 placeWindowBefore(
835 wSublayer >= 0 ? attached : w, win);
836 break;
837 }
838 } else {
839 // For positive sublayers, we go above all windows
840 // in the same sublayer.
841 if (wSublayer > sublayer) {
842 if (addToToken) {
843 token.windows.add(i, win);
844 }
845 placeWindowBefore(w, win);
846 break;
847 }
848 }
849 }
850 if (i >= NA) {
851 if (addToToken) {
852 token.windows.add(win);
853 }
854 if (sublayer < 0) {
855 placeWindowBefore(attached, win);
856 } else {
857 placeWindowAfter(largestSublayer >= 0
858 ? windowWithLargestSublayer
859 : attached,
860 win);
861 }
862 }
863 }
Romain Guy06882f82009-06-10 13:36:04 -0700864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 if (win.mAppToken != null && addToToken) {
866 win.mAppToken.allAppWindows.add(win);
867 }
868 }
Romain Guy06882f82009-06-10 13:36:04 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 static boolean canBeImeTarget(WindowState w) {
871 final int fl = w.mAttrs.flags
872 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
873 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
874 return w.isVisibleOrAdding();
875 }
876 return false;
877 }
Romain Guy06882f82009-06-10 13:36:04 -0700878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
880 final ArrayList localmWindows = mWindows;
881 final int N = localmWindows.size();
882 WindowState w = null;
883 int i = N;
884 while (i > 0) {
885 i--;
886 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
889 // + Integer.toHexString(w.mAttrs.flags));
890 if (canBeImeTarget(w)) {
891 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 // Yet more tricksyness! If this window is a "starting"
894 // window, we do actually want to be on top of it, but
895 // it is not -really- where input will go. So if the caller
896 // is not actually looking to move the IME, look down below
897 // for a real window to target...
898 if (!willMove
899 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
900 && i > 0) {
901 WindowState wb = (WindowState)localmWindows.get(i-1);
902 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
903 i--;
904 w = wb;
905 }
906 }
907 break;
908 }
909 }
Romain Guy06882f82009-06-10 13:36:04 -0700910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
914 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 if (willMove && w != null) {
917 final WindowState curTarget = mInputMethodTarget;
918 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 // Now some fun for dealing with window animations that
921 // modify the Z order. We need to look at all windows below
922 // the current target that are in this app, finding the highest
923 // visible one in layering.
924 AppWindowToken token = curTarget.mAppToken;
925 WindowState highestTarget = null;
926 int highestPos = 0;
927 if (token.animating || token.animation != null) {
928 int pos = 0;
929 pos = localmWindows.indexOf(curTarget);
930 while (pos >= 0) {
931 WindowState win = (WindowState)localmWindows.get(pos);
932 if (win.mAppToken != token) {
933 break;
934 }
935 if (!win.mRemoved) {
936 if (highestTarget == null || win.mAnimLayer >
937 highestTarget.mAnimLayer) {
938 highestTarget = win;
939 highestPos = pos;
940 }
941 }
942 pos--;
943 }
944 }
Romain Guy06882f82009-06-10 13:36:04 -0700945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700947 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 + mNextAppTransition + " " + highestTarget
949 + " animating=" + highestTarget.isAnimating()
950 + " layer=" + highestTarget.mAnimLayer
951 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700952
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700953 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 // If we are currently setting up for an animation,
955 // hold everything until we can find out what will happen.
956 mInputMethodTargetWaitingAnim = true;
957 mInputMethodTarget = highestTarget;
958 return highestPos + 1;
959 } else if (highestTarget.isAnimating() &&
960 highestTarget.mAnimLayer > w.mAnimLayer) {
961 // If the window we are currently targeting is involved
962 // with an animation, and it is on top of the next target
963 // we will be over, then hold off on moving until
964 // that is done.
965 mInputMethodTarget = highestTarget;
966 return highestPos + 1;
967 }
968 }
969 }
970 }
Romain Guy06882f82009-06-10 13:36:04 -0700971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 //Log.i(TAG, "Placing input method @" + (i+1));
973 if (w != null) {
974 if (willMove) {
975 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700976 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
978 + mInputMethodTarget + " to " + w, e);
979 mInputMethodTarget = w;
980 if (w.mAppToken != null) {
981 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
982 } else {
983 setInputMethodAnimLayerAdjustment(0);
984 }
985 }
986 return i+1;
987 }
988 if (willMove) {
989 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700990 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
992 + mInputMethodTarget + " to null", e);
993 mInputMethodTarget = null;
994 setInputMethodAnimLayerAdjustment(0);
995 }
996 return -1;
997 }
Romain Guy06882f82009-06-10 13:36:04 -0700998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 void addInputMethodWindowToListLocked(WindowState win) {
1000 int pos = findDesiredInputMethodWindowIndexLocked(true);
1001 if (pos >= 0) {
1002 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001003 if (DEBUG_WINDOW_MOVEMENT) Log.v(
1004 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 mWindows.add(pos, win);
1006 moveInputMethodDialogsLocked(pos+1);
1007 return;
1008 }
1009 win.mTargetAppToken = null;
1010 addWindowToListInOrderLocked(win, true);
1011 moveInputMethodDialogsLocked(pos);
1012 }
Romain Guy06882f82009-06-10 13:36:04 -07001013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 void setInputMethodAnimLayerAdjustment(int adj) {
1015 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1016 mInputMethodAnimLayerAdjustment = adj;
1017 WindowState imw = mInputMethodWindow;
1018 if (imw != null) {
1019 imw.mAnimLayer = imw.mLayer + adj;
1020 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1021 + " anim layer: " + imw.mAnimLayer);
1022 int wi = imw.mChildWindows.size();
1023 while (wi > 0) {
1024 wi--;
1025 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1026 cw.mAnimLayer = cw.mLayer + adj;
1027 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1028 + " anim layer: " + cw.mAnimLayer);
1029 }
1030 }
1031 int di = mInputMethodDialogs.size();
1032 while (di > 0) {
1033 di --;
1034 imw = mInputMethodDialogs.get(di);
1035 imw.mAnimLayer = imw.mLayer + adj;
1036 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1037 + " anim layer: " + imw.mAnimLayer);
1038 }
1039 }
Romain Guy06882f82009-06-10 13:36:04 -07001040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1042 int wpos = mWindows.indexOf(win);
1043 if (wpos >= 0) {
1044 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001045 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 mWindows.remove(wpos);
1047 int NC = win.mChildWindows.size();
1048 while (NC > 0) {
1049 NC--;
1050 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1051 int cpos = mWindows.indexOf(cw);
1052 if (cpos >= 0) {
1053 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001054 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1055 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 mWindows.remove(cpos);
1057 }
1058 }
1059 }
1060 return interestingPos;
1061 }
Romain Guy06882f82009-06-10 13:36:04 -07001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 private void reAddWindowToListInOrderLocked(WindowState win) {
1064 addWindowToListInOrderLocked(win, false);
1065 // This is a hack to get all of the child windows added as well
1066 // at the right position. Child windows should be rare and
1067 // this case should be rare, so it shouldn't be that big a deal.
1068 int wpos = mWindows.indexOf(win);
1069 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001070 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1071 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 mWindows.remove(wpos);
1073 reAddWindowLocked(wpos, win);
1074 }
1075 }
Romain Guy06882f82009-06-10 13:36:04 -07001076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 void logWindowList(String prefix) {
1078 int N = mWindows.size();
1079 while (N > 0) {
1080 N--;
1081 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1082 }
1083 }
Romain Guy06882f82009-06-10 13:36:04 -07001084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 void moveInputMethodDialogsLocked(int pos) {
1086 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 final int N = dialogs.size();
1089 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1090 for (int i=0; i<N; i++) {
1091 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1092 }
1093 if (DEBUG_INPUT_METHOD) {
1094 Log.v(TAG, "Window list w/pos=" + pos);
1095 logWindowList(" ");
1096 }
Romain Guy06882f82009-06-10 13:36:04 -07001097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 if (pos >= 0) {
1099 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1100 if (pos < mWindows.size()) {
1101 WindowState wp = (WindowState)mWindows.get(pos);
1102 if (wp == mInputMethodWindow) {
1103 pos++;
1104 }
1105 }
1106 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1107 for (int i=0; i<N; i++) {
1108 WindowState win = dialogs.get(i);
1109 win.mTargetAppToken = targetAppToken;
1110 pos = reAddWindowLocked(pos, win);
1111 }
1112 if (DEBUG_INPUT_METHOD) {
1113 Log.v(TAG, "Final window list:");
1114 logWindowList(" ");
1115 }
1116 return;
1117 }
1118 for (int i=0; i<N; i++) {
1119 WindowState win = dialogs.get(i);
1120 win.mTargetAppToken = null;
1121 reAddWindowToListInOrderLocked(win);
1122 if (DEBUG_INPUT_METHOD) {
1123 Log.v(TAG, "No IM target, final list:");
1124 logWindowList(" ");
1125 }
1126 }
1127 }
Romain Guy06882f82009-06-10 13:36:04 -07001128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1130 final WindowState imWin = mInputMethodWindow;
1131 final int DN = mInputMethodDialogs.size();
1132 if (imWin == null && DN == 0) {
1133 return false;
1134 }
Romain Guy06882f82009-06-10 13:36:04 -07001135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1137 if (imPos >= 0) {
1138 // In this case, the input method windows are to be placed
1139 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 // First check to see if the input method windows are already
1142 // located here, and contiguous.
1143 final int N = mWindows.size();
1144 WindowState firstImWin = imPos < N
1145 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 // Figure out the actual input method window that should be
1148 // at the bottom of their stack.
1149 WindowState baseImWin = imWin != null
1150 ? imWin : mInputMethodDialogs.get(0);
1151 if (baseImWin.mChildWindows.size() > 0) {
1152 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1153 if (cw.mSubLayer < 0) baseImWin = cw;
1154 }
Romain Guy06882f82009-06-10 13:36:04 -07001155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 if (firstImWin == baseImWin) {
1157 // The windows haven't moved... but are they still contiguous?
1158 // First find the top IM window.
1159 int pos = imPos+1;
1160 while (pos < N) {
1161 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1162 break;
1163 }
1164 pos++;
1165 }
1166 pos++;
1167 // Now there should be no more input method windows above.
1168 while (pos < N) {
1169 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1170 break;
1171 }
1172 pos++;
1173 }
1174 if (pos >= N) {
1175 // All is good!
1176 return false;
1177 }
1178 }
Romain Guy06882f82009-06-10 13:36:04 -07001179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 if (imWin != null) {
1181 if (DEBUG_INPUT_METHOD) {
1182 Log.v(TAG, "Moving IM from " + imPos);
1183 logWindowList(" ");
1184 }
1185 imPos = tmpRemoveWindowLocked(imPos, imWin);
1186 if (DEBUG_INPUT_METHOD) {
1187 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1188 logWindowList(" ");
1189 }
1190 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1191 reAddWindowLocked(imPos, imWin);
1192 if (DEBUG_INPUT_METHOD) {
1193 Log.v(TAG, "List after moving IM to " + imPos + ":");
1194 logWindowList(" ");
1195 }
1196 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1197 } else {
1198 moveInputMethodDialogsLocked(imPos);
1199 }
Romain Guy06882f82009-06-10 13:36:04 -07001200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 } else {
1202 // In this case, the input method windows go in a fixed layer,
1203 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 if (imWin != null) {
1206 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1207 tmpRemoveWindowLocked(0, imWin);
1208 imWin.mTargetAppToken = null;
1209 reAddWindowToListInOrderLocked(imWin);
1210 if (DEBUG_INPUT_METHOD) {
1211 Log.v(TAG, "List with no IM target:");
1212 logWindowList(" ");
1213 }
1214 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1215 } else {
1216 moveInputMethodDialogsLocked(-1);;
1217 }
Romain Guy06882f82009-06-10 13:36:04 -07001218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
Romain Guy06882f82009-06-10 13:36:04 -07001220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 if (needAssignLayers) {
1222 assignLayersLocked();
1223 }
Romain Guy06882f82009-06-10 13:36:04 -07001224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 return true;
1226 }
Romain Guy06882f82009-06-10 13:36:04 -07001227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 void adjustInputMethodDialogsLocked() {
1229 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1230 }
Romain Guy06882f82009-06-10 13:36:04 -07001231
Dianne Hackborn25994b42009-09-04 14:21:19 -07001232 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1233 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1234 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1235 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1236 ? wallpaperTarget.mAppToken.animation : null)
1237 + " upper=" + mUpperWallpaperTarget
1238 + " lower=" + mLowerWallpaperTarget);
1239 return (wallpaperTarget != null
1240 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1241 && wallpaperTarget.mAppToken.animation != null)))
1242 || mUpperWallpaperTarget != null
1243 || mLowerWallpaperTarget != null;
1244 }
1245
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001246 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1247 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1248
1249 int adjustWallpaperWindowsLocked() {
1250 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001251
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001252 final int dw = mDisplay.getWidth();
1253 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001254
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001255 // First find top-most window that has asked to be on top of the
1256 // wallpaper; all wallpapers go behind it.
1257 final ArrayList localmWindows = mWindows;
1258 int N = localmWindows.size();
1259 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001260 WindowState foundW = null;
1261 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001262 WindowState topCurW = null;
1263 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001264 int i = N;
1265 while (i > 0) {
1266 i--;
1267 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001268 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1269 if (topCurW == null) {
1270 topCurW = w;
1271 topCurI = i;
1272 }
1273 continue;
1274 }
1275 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001276 if (w.mAppToken != null) {
1277 // If this window's app token is hidden and not animating,
1278 // it is of no interest to us.
1279 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1280 if (DEBUG_WALLPAPER) Log.v(TAG,
1281 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001282 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001283 continue;
1284 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001285 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001286 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1287 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1288 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001289 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001290 && (mWallpaperTarget == w
1291 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001292 if (DEBUG_WALLPAPER) Log.v(TAG,
1293 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001294 foundW = w;
1295 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001296 if (w == mWallpaperTarget && ((w.mAppToken != null
1297 && w.mAppToken.animation != null)
1298 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001299 // The current wallpaper target is animating, so we'll
1300 // look behind it for another possible target and figure
1301 // out what is going on below.
1302 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1303 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001304 continue;
1305 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001306 break;
1307 }
1308 }
1309
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001310 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001311 // If we are currently waiting for an app transition, and either
1312 // the current target or the next target are involved with it,
1313 // then hold off on doing anything with the wallpaper.
1314 // Note that we are checking here for just whether the target
1315 // is part of an app token... which is potentially overly aggressive
1316 // (the app token may not be involved in the transition), but good
1317 // enough (we'll just wait until whatever transition is pending
1318 // executes).
1319 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001320 if (DEBUG_WALLPAPER) Log.v(TAG,
1321 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001322 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001323 }
1324 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001325 if (DEBUG_WALLPAPER) Log.v(TAG,
1326 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001327 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001328 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001329 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001330
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001331 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001332 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001333 Log.v(TAG, "New wallpaper target: " + foundW
1334 + " oldTarget: " + mWallpaperTarget);
1335 }
1336
1337 mLowerWallpaperTarget = null;
1338 mUpperWallpaperTarget = null;
1339
1340 WindowState oldW = mWallpaperTarget;
1341 mWallpaperTarget = foundW;
1342
1343 // Now what is happening... if the current and new targets are
1344 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001345 if (foundW != null && oldW != null) {
1346 boolean oldAnim = oldW.mAnimation != null
1347 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1348 boolean foundAnim = foundW.mAnimation != null
1349 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001350 if (DEBUG_WALLPAPER) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001351 Log.v(TAG, "New animation: " + foundAnim
1352 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001353 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001354 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001355 int oldI = localmWindows.indexOf(oldW);
1356 if (DEBUG_WALLPAPER) {
1357 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1358 }
1359 if (oldI >= 0) {
1360 if (DEBUG_WALLPAPER) {
1361 Log.v(TAG, "Animating wallpapers: old#" + oldI
1362 + "=" + oldW + "; new#" + foundI
1363 + "=" + foundW);
1364 }
1365
1366 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001367 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001368 if (DEBUG_WALLPAPER) {
1369 Log.v(TAG, "Old wallpaper still the target.");
1370 }
1371 mWallpaperTarget = oldW;
1372 }
1373
1374 // Now set the upper and lower wallpaper targets
1375 // correctly, and make sure that we are positioning
1376 // the wallpaper below the lower.
1377 if (foundI > oldI) {
1378 // The new target is on top of the old one.
1379 if (DEBUG_WALLPAPER) {
1380 Log.v(TAG, "Found target above old target.");
1381 }
1382 mUpperWallpaperTarget = foundW;
1383 mLowerWallpaperTarget = oldW;
1384 foundW = oldW;
1385 foundI = oldI;
1386 } else {
1387 // The new target is below the old one.
1388 if (DEBUG_WALLPAPER) {
1389 Log.v(TAG, "Found target below old target.");
1390 }
1391 mUpperWallpaperTarget = oldW;
1392 mLowerWallpaperTarget = foundW;
1393 }
1394 }
1395 }
1396 }
1397
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001398 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001399 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001400 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1401 || (mLowerWallpaperTarget.mAppToken != null
1402 && mLowerWallpaperTarget.mAppToken.animation != null);
1403 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1404 || (mUpperWallpaperTarget.mAppToken != null
1405 && mUpperWallpaperTarget.mAppToken.animation != null);
1406 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001407 if (DEBUG_WALLPAPER) {
1408 Log.v(TAG, "No longer animating wallpaper targets!");
1409 }
1410 mLowerWallpaperTarget = null;
1411 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001412 }
1413 }
1414
1415 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001416 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001417 // The window is visible to the compositor... but is it visible
1418 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001419 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001420 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001421
1422 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001423 // its layer adjustment. Only do this if we are not transfering
1424 // between two wallpaper targets.
1425 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001426 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001427 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001428
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001429 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1430 * TYPE_LAYER_MULTIPLIER
1431 + TYPE_LAYER_OFFSET;
1432
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001433 // Now w is the window we are supposed to be behind... but we
1434 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001435 // AND any starting window associated with it, AND below the
1436 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001437 while (foundI > 0) {
1438 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001439 if (wb.mBaseLayer < maxLayer &&
1440 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001441 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001442 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001443 // This window is not related to the previous one in any
1444 // interesting way, so stop here.
1445 break;
1446 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001447 foundW = wb;
1448 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001449 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001450 } else {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001451 if (DEBUG_WALLPAPER) Log.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001452 }
1453
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001454 if (foundW == null && topCurW != null) {
1455 // There is no wallpaper target, so it goes at the bottom.
1456 // We will assume it is the same place as last time, if known.
1457 foundW = topCurW;
1458 foundI = topCurI+1;
1459 } else {
1460 // Okay i is the position immediately above the wallpaper. Look at
1461 // what is below it for later.
1462 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1463 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001464
Dianne Hackborn284ac932009-08-28 10:34:25 -07001465 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001466 if (mWallpaperTarget.mWallpaperX >= 0) {
1467 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1468 }
1469 if (mWallpaperTarget.mWallpaperY >= 0) {
1470 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1471 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001472 }
1473
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001474 // Start stepping backwards from here, ensuring that our wallpaper windows
1475 // are correctly placed.
1476 int curTokenIndex = mWallpaperTokens.size();
1477 while (curTokenIndex > 0) {
1478 curTokenIndex--;
1479 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001480 if (token.hidden == visible) {
1481 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1482 token.hidden = !visible;
1483 // Need to do a layout to ensure the wallpaper now has the
1484 // correct size.
1485 mLayoutNeeded = true;
1486 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001487
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001488 int curWallpaperIndex = token.windows.size();
1489 while (curWallpaperIndex > 0) {
1490 curWallpaperIndex--;
1491 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001492
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001493 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001494 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001495 }
1496
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001497 // First, make sure the client has the current visibility
1498 // state.
1499 if (wallpaper.mWallpaperVisible != visible) {
1500 wallpaper.mWallpaperVisible = visible;
1501 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001502 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001503 "Setting visibility of wallpaper " + wallpaper
1504 + ": " + visible);
1505 wallpaper.mClient.dispatchAppVisibility(visible);
1506 } catch (RemoteException e) {
1507 }
1508 }
1509
1510 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001511 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1512 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001513
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001514 // First, if this window is at the current index, then all
1515 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001516 if (wallpaper == foundW) {
1517 foundI--;
1518 foundW = foundI > 0
1519 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001520 continue;
1521 }
1522
1523 // The window didn't match... the current wallpaper window,
1524 // wherever it is, is in the wrong place, so make sure it is
1525 // not in the list.
1526 int oldIndex = localmWindows.indexOf(wallpaper);
1527 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001528 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1529 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001530 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001531 if (oldIndex < foundI) {
1532 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001533 }
1534 }
1535
1536 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001537 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1538 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001539 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001540
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001541 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001542 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001543 }
1544 }
1545
1546 return changed;
1547 }
1548
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001549 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001550 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1551 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001552 mWallpaperAnimLayerAdjustment = adj;
1553 int curTokenIndex = mWallpaperTokens.size();
1554 while (curTokenIndex > 0) {
1555 curTokenIndex--;
1556 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1557 int curWallpaperIndex = token.windows.size();
1558 while (curWallpaperIndex > 0) {
1559 curWallpaperIndex--;
1560 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1561 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001562 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1563 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001564 }
1565 }
1566 }
1567
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001568 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1569 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001570 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001571 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001572 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
1573 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1574 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1575 changed = wallpaperWin.mXOffset != offset;
1576 if (changed) {
1577 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1578 + wallpaperWin + " x: " + offset);
1579 wallpaperWin.mXOffset = offset;
1580 }
1581 if (wallpaperWin.mWallpaperX != wpx) {
1582 wallpaperWin.mWallpaperX = wpx;
1583 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001584 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001585
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001586 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
1587 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1588 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1589 if (wallpaperWin.mYOffset != offset) {
1590 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1591 + wallpaperWin + " y: " + offset);
1592 changed = true;
1593 wallpaperWin.mYOffset = offset;
1594 }
1595 if (wallpaperWin.mWallpaperY != wpy) {
1596 wallpaperWin.mWallpaperY = wpy;
1597 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001598 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001599
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001600 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001601 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001602 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1603 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1604 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001605 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001606 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001607 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001608 wallpaperWin.mClient.dispatchWallpaperOffsets(
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001609 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY, sync);
1610 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001611 if (mWaitingOnWallpaper != null) {
1612 long start = SystemClock.uptimeMillis();
1613 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1614 < start) {
1615 try {
1616 if (DEBUG_WALLPAPER) Log.v(TAG,
1617 "Waiting for offset complete...");
1618 mWindowMap.wait(WALLPAPER_TIMEOUT);
1619 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001620 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001621 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1622 if ((start+WALLPAPER_TIMEOUT)
1623 < SystemClock.uptimeMillis()) {
1624 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1625 + wallpaperWin);
1626 mLastWallpaperTimeoutTime = start;
1627 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001628 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001629 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001630 }
1631 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001632 } catch (RemoteException e) {
1633 }
1634 }
1635
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001636 return changed;
1637 }
1638
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001639 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001640 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001641 if (mWaitingOnWallpaper != null &&
1642 mWaitingOnWallpaper.mClient.asBinder() == window) {
1643 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001644 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001645 }
1646 }
1647 }
1648
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001649 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001650 final int dw = mDisplay.getWidth();
1651 final int dh = mDisplay.getHeight();
1652
1653 boolean changed = false;
1654
1655 WindowState target = mWallpaperTarget;
1656 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001657 if (target.mWallpaperX >= 0) {
1658 mLastWallpaperX = target.mWallpaperX;
1659 } else if (changingTarget.mWallpaperX >= 0) {
1660 mLastWallpaperX = changingTarget.mWallpaperX;
1661 }
1662 if (target.mWallpaperY >= 0) {
1663 mLastWallpaperY = target.mWallpaperY;
1664 } else if (changingTarget.mWallpaperY >= 0) {
1665 mLastWallpaperY = changingTarget.mWallpaperY;
1666 }
1667 }
1668
1669 int curTokenIndex = mWallpaperTokens.size();
1670 while (curTokenIndex > 0) {
1671 curTokenIndex--;
1672 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1673 int curWallpaperIndex = token.windows.size();
1674 while (curWallpaperIndex > 0) {
1675 curWallpaperIndex--;
1676 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1677 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1678 wallpaper.computeShownFrameLocked();
1679 changed = true;
1680 // We only want to be synchronous with one wallpaper.
1681 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001682 }
1683 }
1684 }
1685
1686 return changed;
1687 }
1688
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001689 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001690 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001691 final int dw = mDisplay.getWidth();
1692 final int dh = mDisplay.getHeight();
1693
1694 int curTokenIndex = mWallpaperTokens.size();
1695 while (curTokenIndex > 0) {
1696 curTokenIndex--;
1697 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001698 if (token.hidden == visible) {
1699 token.hidden = !visible;
1700 // Need to do a layout to ensure the wallpaper now has the
1701 // correct size.
1702 mLayoutNeeded = true;
1703 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001704
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001705 int curWallpaperIndex = token.windows.size();
1706 while (curWallpaperIndex > 0) {
1707 curWallpaperIndex--;
1708 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1709 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001710 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001711 }
1712
1713 if (wallpaper.mWallpaperVisible != visible) {
1714 wallpaper.mWallpaperVisible = visible;
1715 try {
1716 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001717 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001718 + ": " + visible);
1719 wallpaper.mClient.dispatchAppVisibility(visible);
1720 } catch (RemoteException e) {
1721 }
1722 }
1723 }
1724 }
1725 }
1726
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001727 void sendPointerToWallpaperLocked(WindowState srcWin,
1728 MotionEvent pointer, long eventTime) {
1729 int curTokenIndex = mWallpaperTokens.size();
1730 while (curTokenIndex > 0) {
1731 curTokenIndex--;
1732 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1733 int curWallpaperIndex = token.windows.size();
1734 while (curWallpaperIndex > 0) {
1735 curWallpaperIndex--;
1736 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1737 if ((wallpaper.mAttrs.flags &
1738 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1739 continue;
1740 }
1741 try {
1742 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1743 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1744 srcWin.mFrame.top-wallpaper.mFrame.top);
1745 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1746 } catch (RemoteException e) {
1747 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1748 }
1749 }
1750 }
1751 }
1752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 public int addWindow(Session session, IWindow client,
1754 WindowManager.LayoutParams attrs, int viewVisibility,
1755 Rect outContentInsets) {
1756 int res = mPolicy.checkAddPermission(attrs);
1757 if (res != WindowManagerImpl.ADD_OKAY) {
1758 return res;
1759 }
Romain Guy06882f82009-06-10 13:36:04 -07001760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 boolean reportNewConfig = false;
1762 WindowState attachedWindow = null;
1763 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 synchronized(mWindowMap) {
1766 // Instantiating a Display requires talking with the simulator,
1767 // so don't do it until we know the system is mostly up and
1768 // running.
1769 if (mDisplay == null) {
1770 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1771 mDisplay = wm.getDefaultDisplay();
1772 mQueue.setDisplay(mDisplay);
1773 reportNewConfig = true;
1774 }
Romain Guy06882f82009-06-10 13:36:04 -07001775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 if (mWindowMap.containsKey(client.asBinder())) {
1777 Log.w(TAG, "Window " + client + " is already added");
1778 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1779 }
1780
1781 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001782 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 if (attachedWindow == null) {
1784 Log.w(TAG, "Attempted to add window with token that is not a window: "
1785 + attrs.token + ". Aborting.");
1786 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1787 }
1788 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1789 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1790 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1791 + attrs.token + ". Aborting.");
1792 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1793 }
1794 }
1795
1796 boolean addToken = false;
1797 WindowToken token = mTokenMap.get(attrs.token);
1798 if (token == null) {
1799 if (attrs.type >= FIRST_APPLICATION_WINDOW
1800 && attrs.type <= LAST_APPLICATION_WINDOW) {
1801 Log.w(TAG, "Attempted to add application window with unknown token "
1802 + attrs.token + ". Aborting.");
1803 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1804 }
1805 if (attrs.type == TYPE_INPUT_METHOD) {
1806 Log.w(TAG, "Attempted to add input method window with unknown token "
1807 + attrs.token + ". Aborting.");
1808 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1809 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001810 if (attrs.type == TYPE_WALLPAPER) {
1811 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1812 + attrs.token + ". Aborting.");
1813 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 token = new WindowToken(attrs.token, -1, false);
1816 addToken = true;
1817 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1818 && attrs.type <= LAST_APPLICATION_WINDOW) {
1819 AppWindowToken atoken = token.appWindowToken;
1820 if (atoken == null) {
1821 Log.w(TAG, "Attempted to add window with non-application token "
1822 + token + ". Aborting.");
1823 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1824 } else if (atoken.removed) {
1825 Log.w(TAG, "Attempted to add window with exiting application token "
1826 + token + ". Aborting.");
1827 return WindowManagerImpl.ADD_APP_EXITING;
1828 }
1829 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1830 // No need for this guy!
1831 if (localLOGV) Log.v(
1832 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1833 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1834 }
1835 } else if (attrs.type == TYPE_INPUT_METHOD) {
1836 if (token.windowType != TYPE_INPUT_METHOD) {
1837 Log.w(TAG, "Attempted to add input method window with bad token "
1838 + attrs.token + ". Aborting.");
1839 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1840 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001841 } else if (attrs.type == TYPE_WALLPAPER) {
1842 if (token.windowType != TYPE_WALLPAPER) {
1843 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1844 + attrs.token + ". Aborting.");
1845 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
1848
1849 win = new WindowState(session, client, token,
1850 attachedWindow, attrs, viewVisibility);
1851 if (win.mDeathRecipient == null) {
1852 // Client has apparently died, so there is no reason to
1853 // continue.
1854 Log.w(TAG, "Adding window client " + client.asBinder()
1855 + " that is dead, aborting.");
1856 return WindowManagerImpl.ADD_APP_EXITING;
1857 }
1858
1859 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 res = mPolicy.prepareAddWindowLw(win, attrs);
1862 if (res != WindowManagerImpl.ADD_OKAY) {
1863 return res;
1864 }
1865
1866 // From now on, no exceptions or errors allowed!
1867
1868 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 if (addToken) {
1873 mTokenMap.put(attrs.token, token);
1874 mTokenList.add(token);
1875 }
1876 win.attach();
1877 mWindowMap.put(client.asBinder(), win);
1878
1879 if (attrs.type == TYPE_APPLICATION_STARTING &&
1880 token.appWindowToken != null) {
1881 token.appWindowToken.startingWindow = win;
1882 }
1883
1884 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 if (attrs.type == TYPE_INPUT_METHOD) {
1887 mInputMethodWindow = win;
1888 addInputMethodWindowToListLocked(win);
1889 imMayMove = false;
1890 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1891 mInputMethodDialogs.add(win);
1892 addWindowToListInOrderLocked(win, true);
1893 adjustInputMethodDialogsLocked();
1894 imMayMove = false;
1895 } else {
1896 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001897 if (attrs.type == TYPE_WALLPAPER) {
1898 mLastWallpaperTimeoutTime = 0;
1899 adjustWallpaperWindowsLocked();
1900 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001901 adjustWallpaperWindowsLocked();
1902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 }
Romain Guy06882f82009-06-10 13:36:04 -07001904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 if (mInTouchMode) {
1910 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1911 }
1912 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1913 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1914 }
Romain Guy06882f82009-06-10 13:36:04 -07001915
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001916 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001918 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1919 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 imMayMove = false;
1921 }
1922 }
Romain Guy06882f82009-06-10 13:36:04 -07001923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001925 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 }
Romain Guy06882f82009-06-10 13:36:04 -07001927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 assignLayersLocked();
1929 // Don't do layout here, the window must call
1930 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 //dump();
1933
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001934 if (focusChanged) {
1935 if (mCurrentFocus != null) {
1936 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1937 }
1938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 if (localLOGV) Log.v(
1940 TAG, "New client " + client.asBinder()
1941 + ": window=" + win);
1942 }
1943
1944 // sendNewConfiguration() checks caller permissions so we must call it with
1945 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1946 // identity anyway, so it's safe to just clear & restore around this whole
1947 // block.
1948 final long origId = Binder.clearCallingIdentity();
1949 if (reportNewConfig) {
1950 sendNewConfiguration();
1951 } else {
1952 // Update Orientation after adding a window, only if the window needs to be
1953 // displayed right away
1954 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001955 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 sendNewConfiguration();
1957 }
1958 }
1959 }
1960 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 return res;
1963 }
Romain Guy06882f82009-06-10 13:36:04 -07001964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 public void removeWindow(Session session, IWindow client) {
1966 synchronized(mWindowMap) {
1967 WindowState win = windowForClientLocked(session, client);
1968 if (win == null) {
1969 return;
1970 }
1971 removeWindowLocked(session, win);
1972 }
1973 }
Romain Guy06882f82009-06-10 13:36:04 -07001974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 public void removeWindowLocked(Session session, WindowState win) {
1976
1977 if (localLOGV || DEBUG_FOCUS) Log.v(
1978 TAG, "Remove " + win + " client="
1979 + Integer.toHexString(System.identityHashCode(
1980 win.mClient.asBinder()))
1981 + ", surface=" + win.mSurface);
1982
1983 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 if (DEBUG_APP_TRANSITIONS) Log.v(
1986 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1987 + " mExiting=" + win.mExiting
1988 + " isAnimating=" + win.isAnimating()
1989 + " app-animation="
1990 + (win.mAppToken != null ? win.mAppToken.animation : null)
1991 + " inPendingTransaction="
1992 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1993 + " mDisplayFrozen=" + mDisplayFrozen);
1994 // Visibility of the removed window. Will be used later to update orientation later on.
1995 boolean wasVisible = false;
1996 // First, see if we need to run an animation. If we do, we have
1997 // to hold off on removing the window until the animation is done.
1998 // If the display is frozen, just remove immediately, since the
1999 // animation wouldn't be seen.
2000 if (win.mSurface != null && !mDisplayFrozen) {
2001 // If we are not currently running the exit animation, we
2002 // need to see about starting one.
2003 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2006 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2007 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2008 }
2009 // Try starting an animation.
2010 if (applyAnimationLocked(win, transit, false)) {
2011 win.mExiting = true;
2012 }
2013 }
2014 if (win.mExiting || win.isAnimating()) {
2015 // The exit animation is running... wait for it!
2016 //Log.i(TAG, "*** Running exit animation...");
2017 win.mExiting = true;
2018 win.mRemoveOnExit = true;
2019 mLayoutNeeded = true;
2020 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2021 performLayoutAndPlaceSurfacesLocked();
2022 if (win.mAppToken != null) {
2023 win.mAppToken.updateReportedVisibilityLocked();
2024 }
2025 //dump();
2026 Binder.restoreCallingIdentity(origId);
2027 return;
2028 }
2029 }
2030
2031 removeWindowInnerLocked(session, win);
2032 // Removing a visible window will effect the computed orientation
2033 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002034 if (wasVisible && computeForcedAppOrientationLocked()
2035 != mForcedAppOrientation) {
2036 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
2038 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2039 Binder.restoreCallingIdentity(origId);
2040 }
Romain Guy06882f82009-06-10 13:36:04 -07002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002043 mKeyWaiter.finishedKey(session, win.mClient, true,
2044 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2046 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 if (mInputMethodTarget == win) {
2051 moveInputMethodWindowsIfNeededLocked(false);
2052 }
Romain Guy06882f82009-06-10 13:36:04 -07002053
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002054 if (false) {
2055 RuntimeException e = new RuntimeException("here");
2056 e.fillInStackTrace();
2057 Log.w(TAG, "Removing window " + win, e);
2058 }
2059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 mPolicy.removeWindowLw(win);
2061 win.removeLocked();
2062
2063 mWindowMap.remove(win.mClient.asBinder());
2064 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002065 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066
2067 if (mInputMethodWindow == win) {
2068 mInputMethodWindow = null;
2069 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2070 mInputMethodDialogs.remove(win);
2071 }
Romain Guy06882f82009-06-10 13:36:04 -07002072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 final WindowToken token = win.mToken;
2074 final AppWindowToken atoken = win.mAppToken;
2075 token.windows.remove(win);
2076 if (atoken != null) {
2077 atoken.allAppWindows.remove(win);
2078 }
2079 if (localLOGV) Log.v(
2080 TAG, "**** Removing window " + win + ": count="
2081 + token.windows.size());
2082 if (token.windows.size() == 0) {
2083 if (!token.explicit) {
2084 mTokenMap.remove(token.token);
2085 mTokenList.remove(token);
2086 } else if (atoken != null) {
2087 atoken.firstWindowDrawn = false;
2088 }
2089 }
2090
2091 if (atoken != null) {
2092 if (atoken.startingWindow == win) {
2093 atoken.startingWindow = null;
2094 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2095 // If this is the last window and we had requested a starting
2096 // transition window, well there is no point now.
2097 atoken.startingData = null;
2098 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2099 // If this is the last window except for a starting transition
2100 // window, we need to get rid of the starting transition.
2101 if (DEBUG_STARTING_WINDOW) {
2102 Log.v(TAG, "Schedule remove starting " + token
2103 + ": no more real windows");
2104 }
2105 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2106 mH.sendMessage(m);
2107 }
2108 }
Romain Guy06882f82009-06-10 13:36:04 -07002109
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002110 if (win.mAttrs.type == TYPE_WALLPAPER) {
2111 mLastWallpaperTimeoutTime = 0;
2112 adjustWallpaperWindowsLocked();
2113 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002114 adjustWallpaperWindowsLocked();
2115 }
2116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 if (!mInLayout) {
2118 assignLayersLocked();
2119 mLayoutNeeded = true;
2120 performLayoutAndPlaceSurfacesLocked();
2121 if (win.mAppToken != null) {
2122 win.mAppToken.updateReportedVisibilityLocked();
2123 }
2124 }
2125 }
2126
2127 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2128 long origId = Binder.clearCallingIdentity();
2129 try {
2130 synchronized (mWindowMap) {
2131 WindowState w = windowForClientLocked(session, client);
2132 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002133 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 Surface.openTransaction();
2135 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002136 if (SHOW_TRANSACTIONS) Log.i(
2137 TAG, " SURFACE " + w.mSurface
2138 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 w.mSurface.setTransparentRegionHint(region);
2140 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002141 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 Surface.closeTransaction();
2143 }
2144 }
2145 }
2146 } finally {
2147 Binder.restoreCallingIdentity(origId);
2148 }
2149 }
2150
2151 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002152 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 Rect visibleInsets) {
2154 long origId = Binder.clearCallingIdentity();
2155 try {
2156 synchronized (mWindowMap) {
2157 WindowState w = windowForClientLocked(session, client);
2158 if (w != null) {
2159 w.mGivenInsetsPending = false;
2160 w.mGivenContentInsets.set(contentInsets);
2161 w.mGivenVisibleInsets.set(visibleInsets);
2162 w.mTouchableInsets = touchableInsets;
2163 mLayoutNeeded = true;
2164 performLayoutAndPlaceSurfacesLocked();
2165 }
2166 }
2167 } finally {
2168 Binder.restoreCallingIdentity(origId);
2169 }
2170 }
Romain Guy06882f82009-06-10 13:36:04 -07002171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 public void getWindowDisplayFrame(Session session, IWindow client,
2173 Rect outDisplayFrame) {
2174 synchronized(mWindowMap) {
2175 WindowState win = windowForClientLocked(session, client);
2176 if (win == null) {
2177 outDisplayFrame.setEmpty();
2178 return;
2179 }
2180 outDisplayFrame.set(win.mDisplayFrame);
2181 }
2182 }
2183
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002184 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2185 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2186 window.mWallpaperX = x;
2187 window.mWallpaperY = y;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002188 if (updateWallpaperOffsetLocked(window, true)) {
2189 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002190 }
2191 }
2192 }
2193
Dianne Hackborn75804932009-10-20 20:15:20 -07002194 void wallpaperCommandComplete(IBinder window, Bundle result) {
2195 synchronized (mWindowMap) {
2196 if (mWaitingOnWallpaper != null &&
2197 mWaitingOnWallpaper.mClient.asBinder() == window) {
2198 mWaitingOnWallpaper = null;
2199 mWindowMap.notifyAll();
2200 }
2201 }
2202 }
2203
2204 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2205 String action, int x, int y, int z, Bundle extras, boolean sync) {
2206 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2207 || window == mUpperWallpaperTarget) {
2208 boolean doWait = sync;
2209 int curTokenIndex = mWallpaperTokens.size();
2210 while (curTokenIndex > 0) {
2211 curTokenIndex--;
2212 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2213 int curWallpaperIndex = token.windows.size();
2214 while (curWallpaperIndex > 0) {
2215 curWallpaperIndex--;
2216 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2217 try {
2218 wallpaper.mClient.dispatchWallpaperCommand(action,
2219 x, y, z, extras, sync);
2220 // We only want to be synchronous with one wallpaper.
2221 sync = false;
2222 } catch (RemoteException e) {
2223 }
2224 }
2225 }
2226
2227 if (doWait) {
2228 // XXX Need to wait for result.
2229 }
2230 }
2231
2232 return null;
2233 }
2234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 public int relayoutWindow(Session session, IWindow client,
2236 WindowManager.LayoutParams attrs, int requestedWidth,
2237 int requestedHeight, int viewVisibility, boolean insetsPending,
2238 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2239 Surface outSurface) {
2240 boolean displayed = false;
2241 boolean inTouchMode;
2242 Configuration newConfig = null;
2243 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 synchronized(mWindowMap) {
2246 WindowState win = windowForClientLocked(session, client);
2247 if (win == null) {
2248 return 0;
2249 }
2250 win.mRequestedWidth = requestedWidth;
2251 win.mRequestedHeight = requestedHeight;
2252
2253 if (attrs != null) {
2254 mPolicy.adjustWindowParamsLw(attrs);
2255 }
Romain Guy06882f82009-06-10 13:36:04 -07002256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 int attrChanges = 0;
2258 int flagChanges = 0;
2259 if (attrs != null) {
2260 flagChanges = win.mAttrs.flags ^= attrs.flags;
2261 attrChanges = win.mAttrs.copyFrom(attrs);
2262 }
2263
2264 if (localLOGV) Log.v(
2265 TAG, "Relayout given client " + client.asBinder()
2266 + " (" + win.mAttrs.getTitle() + ")");
2267
2268
2269 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2270 win.mAlpha = attrs.alpha;
2271 }
2272
2273 final boolean scaledWindow =
2274 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2275
2276 if (scaledWindow) {
2277 // requested{Width|Height} Surface's physical size
2278 // attrs.{width|height} Size on screen
2279 win.mHScale = (attrs.width != requestedWidth) ?
2280 (attrs.width / (float)requestedWidth) : 1.0f;
2281 win.mVScale = (attrs.height != requestedHeight) ?
2282 (attrs.height / (float)requestedHeight) : 1.0f;
2283 }
2284
2285 boolean imMayMove = (flagChanges&(
2286 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2287 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 boolean focusMayChange = win.mViewVisibility != viewVisibility
2290 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2291 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002292
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002293 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2294 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 win.mRelayoutCalled = true;
2297 final int oldVisibility = win.mViewVisibility;
2298 win.mViewVisibility = viewVisibility;
2299 if (viewVisibility == View.VISIBLE &&
2300 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2301 displayed = !win.isVisibleLw();
2302 if (win.mExiting) {
2303 win.mExiting = false;
2304 win.mAnimation = null;
2305 }
2306 if (win.mDestroying) {
2307 win.mDestroying = false;
2308 mDestroySurface.remove(win);
2309 }
2310 if (oldVisibility == View.GONE) {
2311 win.mEnterAnimationPending = true;
2312 }
2313 if (displayed && win.mSurface != null && !win.mDrawPending
2314 && !win.mCommitDrawPending && !mDisplayFrozen) {
2315 applyEnterAnimationLocked(win);
2316 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002317 if (displayed && (win.mAttrs.flags
2318 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2319 win.mTurnOnScreen = true;
2320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2322 // To change the format, we need to re-build the surface.
2323 win.destroySurfaceLocked();
2324 displayed = true;
2325 }
2326 try {
2327 Surface surface = win.createSurfaceLocked();
2328 if (surface != null) {
2329 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002330 win.mReportDestroySurface = false;
2331 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002332 if (SHOW_TRANSACTIONS) Log.i(TAG,
2333 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002335 // For some reason there isn't a surface. Clear the
2336 // caller's object so they see the same state.
2337 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 }
2339 } catch (Exception e) {
2340 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002341 + client + " (" + win.mAttrs.getTitle() + ")",
2342 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 Binder.restoreCallingIdentity(origId);
2344 return 0;
2345 }
2346 if (displayed) {
2347 focusMayChange = true;
2348 }
2349 if (win.mAttrs.type == TYPE_INPUT_METHOD
2350 && mInputMethodWindow == null) {
2351 mInputMethodWindow = win;
2352 imMayMove = true;
2353 }
2354 } else {
2355 win.mEnterAnimationPending = false;
2356 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002357 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2358 + ": mExiting=" + win.mExiting
2359 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 // If we are not currently running the exit animation, we
2361 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002362 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 // Try starting an animation; if there isn't one, we
2364 // can destroy the surface right away.
2365 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2366 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2367 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2368 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002369 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002371 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 win.mExiting = true;
2373 mKeyWaiter.finishedKey(session, client, true,
2374 KeyWaiter.RETURN_NOTHING);
2375 } else if (win.isAnimating()) {
2376 // Currently in a hide animation... turn this into
2377 // an exit.
2378 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002379 } else if (win == mWallpaperTarget) {
2380 // If the wallpaper is currently behind this
2381 // window, we need to change both of them inside
2382 // of a transaction to avoid artifacts.
2383 win.mExiting = true;
2384 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 } else {
2386 if (mInputMethodWindow == win) {
2387 mInputMethodWindow = null;
2388 }
2389 win.destroySurfaceLocked();
2390 }
2391 }
2392 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002393
2394 if (win.mSurface == null || (win.getAttrs().flags
2395 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2396 || win.mSurfacePendingDestroy) {
2397 // We are being called from a local process, which
2398 // means outSurface holds its current surface. Ensure the
2399 // surface object is cleared, but we don't want it actually
2400 // destroyed at this point.
2401 win.mSurfacePendingDestroy = false;
2402 outSurface.release();
2403 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2404 } else if (win.mSurface != null) {
2405 if (DEBUG_VISIBILITY) Log.i(TAG,
2406 "Keeping surface, will report destroy: " + win);
2407 win.mReportDestroySurface = true;
2408 outSurface.copyFrom(win.mSurface);
2409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 }
2411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 if (focusMayChange) {
2413 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2414 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 imMayMove = false;
2416 }
2417 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2418 }
Romain Guy06882f82009-06-10 13:36:04 -07002419
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002420 // updateFocusedWindowLocked() already assigned layers so we only need to
2421 // reassign them at this point if the IM window state gets shuffled
2422 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 if (imMayMove) {
2425 if (moveInputMethodWindowsIfNeededLocked(false)) {
2426 assignLayers = true;
2427 }
2428 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002429 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002430 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002431 assignLayers = true;
2432 }
2433 }
Romain Guy06882f82009-06-10 13:36:04 -07002434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 mLayoutNeeded = true;
2436 win.mGivenInsetsPending = insetsPending;
2437 if (assignLayers) {
2438 assignLayersLocked();
2439 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002440 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002442 if (displayed && win.mIsWallpaper) {
2443 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002444 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 if (win.mAppToken != null) {
2447 win.mAppToken.updateReportedVisibilityLocked();
2448 }
2449 outFrame.set(win.mFrame);
2450 outContentInsets.set(win.mContentInsets);
2451 outVisibleInsets.set(win.mVisibleInsets);
2452 if (localLOGV) Log.v(
2453 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002454 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 + ", requestedHeight=" + requestedHeight
2456 + ", viewVisibility=" + viewVisibility
2457 + "\nRelayout returning frame=" + outFrame
2458 + ", surface=" + outSurface);
2459
2460 if (localLOGV || DEBUG_FOCUS) Log.v(
2461 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2462
2463 inTouchMode = mInTouchMode;
2464 }
2465
2466 if (newConfig != null) {
2467 sendNewConfiguration();
2468 }
Romain Guy06882f82009-06-10 13:36:04 -07002469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2473 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2474 }
2475
2476 public void finishDrawingWindow(Session session, IWindow client) {
2477 final long origId = Binder.clearCallingIdentity();
2478 synchronized(mWindowMap) {
2479 WindowState win = windowForClientLocked(session, client);
2480 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002481 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2482 adjustWallpaperWindowsLocked();
2483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 mLayoutNeeded = true;
2485 performLayoutAndPlaceSurfacesLocked();
2486 }
2487 }
2488 Binder.restoreCallingIdentity(origId);
2489 }
2490
2491 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2492 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2493 + (lp != null ? lp.packageName : null)
2494 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2495 if (lp != null && lp.windowAnimations != 0) {
2496 // If this is a system resource, don't try to load it from the
2497 // application resources. It is nice to avoid loading application
2498 // resources if we can.
2499 String packageName = lp.packageName != null ? lp.packageName : "android";
2500 int resId = lp.windowAnimations;
2501 if ((resId&0xFF000000) == 0x01000000) {
2502 packageName = "android";
2503 }
2504 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2505 + packageName);
2506 return AttributeCache.instance().get(packageName, resId,
2507 com.android.internal.R.styleable.WindowAnimation);
2508 }
2509 return null;
2510 }
Romain Guy06882f82009-06-10 13:36:04 -07002511
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002512 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2513 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2514 + packageName + " resId=0x" + Integer.toHexString(resId));
2515 if (packageName != null) {
2516 if ((resId&0xFF000000) == 0x01000000) {
2517 packageName = "android";
2518 }
2519 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2520 + packageName);
2521 return AttributeCache.instance().get(packageName, resId,
2522 com.android.internal.R.styleable.WindowAnimation);
2523 }
2524 return null;
2525 }
2526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 private void applyEnterAnimationLocked(WindowState win) {
2528 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2529 if (win.mEnterAnimationPending) {
2530 win.mEnterAnimationPending = false;
2531 transit = WindowManagerPolicy.TRANSIT_ENTER;
2532 }
2533
2534 applyAnimationLocked(win, transit, true);
2535 }
2536
2537 private boolean applyAnimationLocked(WindowState win,
2538 int transit, boolean isEntrance) {
2539 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2540 // If we are trying to apply an animation, but already running
2541 // an animation of the same type, then just leave that one alone.
2542 return true;
2543 }
Romain Guy06882f82009-06-10 13:36:04 -07002544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 // Only apply an animation if the display isn't frozen. If it is
2546 // frozen, there is no reason to animate and it can cause strange
2547 // artifacts when we unfreeze the display if some different animation
2548 // is running.
2549 if (!mDisplayFrozen) {
2550 int anim = mPolicy.selectAnimationLw(win, transit);
2551 int attr = -1;
2552 Animation a = null;
2553 if (anim != 0) {
2554 a = AnimationUtils.loadAnimation(mContext, anim);
2555 } else {
2556 switch (transit) {
2557 case WindowManagerPolicy.TRANSIT_ENTER:
2558 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2559 break;
2560 case WindowManagerPolicy.TRANSIT_EXIT:
2561 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2562 break;
2563 case WindowManagerPolicy.TRANSIT_SHOW:
2564 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2565 break;
2566 case WindowManagerPolicy.TRANSIT_HIDE:
2567 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2568 break;
2569 }
2570 if (attr >= 0) {
2571 a = loadAnimation(win.mAttrs, attr);
2572 }
2573 }
2574 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2575 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2576 + " mAnimation=" + win.mAnimation
2577 + " isEntrance=" + isEntrance);
2578 if (a != null) {
2579 if (DEBUG_ANIM) {
2580 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002581 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2583 }
2584 win.setAnimation(a);
2585 win.mAnimationIsEntrance = isEntrance;
2586 }
2587 } else {
2588 win.clearAnimation();
2589 }
2590
2591 return win.mAnimation != null;
2592 }
2593
2594 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2595 int anim = 0;
2596 Context context = mContext;
2597 if (animAttr >= 0) {
2598 AttributeCache.Entry ent = getCachedAnimations(lp);
2599 if (ent != null) {
2600 context = ent.context;
2601 anim = ent.array.getResourceId(animAttr, 0);
2602 }
2603 }
2604 if (anim != 0) {
2605 return AnimationUtils.loadAnimation(context, anim);
2606 }
2607 return null;
2608 }
Romain Guy06882f82009-06-10 13:36:04 -07002609
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002610 private Animation loadAnimation(String packageName, int resId) {
2611 int anim = 0;
2612 Context context = mContext;
2613 if (resId >= 0) {
2614 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2615 if (ent != null) {
2616 context = ent.context;
2617 anim = resId;
2618 }
2619 }
2620 if (anim != 0) {
2621 return AnimationUtils.loadAnimation(context, anim);
2622 }
2623 return null;
2624 }
2625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 private boolean applyAnimationLocked(AppWindowToken wtoken,
2627 WindowManager.LayoutParams lp, int transit, boolean enter) {
2628 // Only apply an animation if the display isn't frozen. If it is
2629 // frozen, there is no reason to animate and it can cause strange
2630 // artifacts when we unfreeze the display if some different animation
2631 // is running.
2632 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002633 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002634 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002635 a = new FadeInOutAnimation(enter);
2636 if (DEBUG_ANIM) Log.v(TAG,
2637 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002638 } else if (mNextAppTransitionPackage != null) {
2639 a = loadAnimation(mNextAppTransitionPackage, enter ?
2640 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002641 } else {
2642 int animAttr = 0;
2643 switch (transit) {
2644 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2645 animAttr = enter
2646 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2647 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2648 break;
2649 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2650 animAttr = enter
2651 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2652 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2653 break;
2654 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2655 animAttr = enter
2656 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2657 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2658 break;
2659 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2660 animAttr = enter
2661 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2662 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2663 break;
2664 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2665 animAttr = enter
2666 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2667 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2668 break;
2669 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2670 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002671 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002672 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2673 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002674 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002675 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002676 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2677 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002678 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002679 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002680 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002681 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2682 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2683 break;
2684 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2685 animAttr = enter
2686 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2687 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2688 break;
2689 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2690 animAttr = enter
2691 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2692 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002693 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002694 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002695 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002696 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2697 + " anim=" + a
2698 + " animAttr=0x" + Integer.toHexString(animAttr)
2699 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 if (a != null) {
2702 if (DEBUG_ANIM) {
2703 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002704 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2706 }
2707 wtoken.setAnimation(a);
2708 }
2709 } else {
2710 wtoken.clearAnimation();
2711 }
2712
2713 return wtoken.animation != null;
2714 }
2715
2716 // -------------------------------------------------------------
2717 // Application Window Tokens
2718 // -------------------------------------------------------------
2719
2720 public void validateAppTokens(List tokens) {
2721 int v = tokens.size()-1;
2722 int m = mAppTokens.size()-1;
2723 while (v >= 0 && m >= 0) {
2724 AppWindowToken wtoken = mAppTokens.get(m);
2725 if (wtoken.removed) {
2726 m--;
2727 continue;
2728 }
2729 if (tokens.get(v) != wtoken.token) {
2730 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2731 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2732 }
2733 v--;
2734 m--;
2735 }
2736 while (v >= 0) {
2737 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2738 v--;
2739 }
2740 while (m >= 0) {
2741 AppWindowToken wtoken = mAppTokens.get(m);
2742 if (!wtoken.removed) {
2743 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2744 }
2745 m--;
2746 }
2747 }
2748
2749 boolean checkCallingPermission(String permission, String func) {
2750 // Quick check: if the calling permission is me, it's all okay.
2751 if (Binder.getCallingPid() == Process.myPid()) {
2752 return true;
2753 }
Romain Guy06882f82009-06-10 13:36:04 -07002754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 if (mContext.checkCallingPermission(permission)
2756 == PackageManager.PERMISSION_GRANTED) {
2757 return true;
2758 }
2759 String msg = "Permission Denial: " + func + " from pid="
2760 + Binder.getCallingPid()
2761 + ", uid=" + Binder.getCallingUid()
2762 + " requires " + permission;
2763 Log.w(TAG, msg);
2764 return false;
2765 }
Romain Guy06882f82009-06-10 13:36:04 -07002766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 AppWindowToken findAppWindowToken(IBinder token) {
2768 WindowToken wtoken = mTokenMap.get(token);
2769 if (wtoken == null) {
2770 return null;
2771 }
2772 return wtoken.appWindowToken;
2773 }
Romain Guy06882f82009-06-10 13:36:04 -07002774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 public void addWindowToken(IBinder token, int type) {
2776 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2777 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002778 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 }
Romain Guy06882f82009-06-10 13:36:04 -07002780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 synchronized(mWindowMap) {
2782 WindowToken wtoken = mTokenMap.get(token);
2783 if (wtoken != null) {
2784 Log.w(TAG, "Attempted to add existing input method token: " + token);
2785 return;
2786 }
2787 wtoken = new WindowToken(token, type, true);
2788 mTokenMap.put(token, wtoken);
2789 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002790 if (type == TYPE_WALLPAPER) {
2791 mWallpaperTokens.add(wtoken);
2792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 }
2794 }
Romain Guy06882f82009-06-10 13:36:04 -07002795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 public void removeWindowToken(IBinder token) {
2797 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2798 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002799 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 }
2801
2802 final long origId = Binder.clearCallingIdentity();
2803 synchronized(mWindowMap) {
2804 WindowToken wtoken = mTokenMap.remove(token);
2805 mTokenList.remove(wtoken);
2806 if (wtoken != null) {
2807 boolean delayed = false;
2808 if (!wtoken.hidden) {
2809 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 final int N = wtoken.windows.size();
2812 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 for (int i=0; i<N; i++) {
2815 WindowState win = wtoken.windows.get(i);
2816
2817 if (win.isAnimating()) {
2818 delayed = true;
2819 }
Romain Guy06882f82009-06-10 13:36:04 -07002820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 if (win.isVisibleNow()) {
2822 applyAnimationLocked(win,
2823 WindowManagerPolicy.TRANSIT_EXIT, false);
2824 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2825 KeyWaiter.RETURN_NOTHING);
2826 changed = true;
2827 }
2828 }
2829
2830 if (changed) {
2831 mLayoutNeeded = true;
2832 performLayoutAndPlaceSurfacesLocked();
2833 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2834 }
Romain Guy06882f82009-06-10 13:36:04 -07002835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 if (delayed) {
2837 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002838 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2839 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 }
2841 }
Romain Guy06882f82009-06-10 13:36:04 -07002842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 } else {
2844 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2845 }
2846 }
2847 Binder.restoreCallingIdentity(origId);
2848 }
2849
2850 public void addAppToken(int addPos, IApplicationToken token,
2851 int groupId, int requestedOrientation, boolean fullscreen) {
2852 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2853 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002854 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
Romain Guy06882f82009-06-10 13:36:04 -07002856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 synchronized(mWindowMap) {
2858 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2859 if (wtoken != null) {
2860 Log.w(TAG, "Attempted to add existing app token: " + token);
2861 return;
2862 }
2863 wtoken = new AppWindowToken(token);
2864 wtoken.groupId = groupId;
2865 wtoken.appFullscreen = fullscreen;
2866 wtoken.requestedOrientation = requestedOrientation;
2867 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002868 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 mTokenMap.put(token.asBinder(), wtoken);
2870 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 // Application tokens start out hidden.
2873 wtoken.hidden = true;
2874 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 //dump();
2877 }
2878 }
Romain Guy06882f82009-06-10 13:36:04 -07002879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 public void setAppGroupId(IBinder token, int groupId) {
2881 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2882 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002883 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 }
2885
2886 synchronized(mWindowMap) {
2887 AppWindowToken wtoken = findAppWindowToken(token);
2888 if (wtoken == null) {
2889 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2890 return;
2891 }
2892 wtoken.groupId = groupId;
2893 }
2894 }
Romain Guy06882f82009-06-10 13:36:04 -07002895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 public int getOrientationFromWindowsLocked() {
2897 int pos = mWindows.size() - 1;
2898 while (pos >= 0) {
2899 WindowState wtoken = (WindowState) mWindows.get(pos);
2900 pos--;
2901 if (wtoken.mAppToken != null) {
2902 // We hit an application window. so the orientation will be determined by the
2903 // app window. No point in continuing further.
2904 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2905 }
2906 if (!wtoken.isVisibleLw()) {
2907 continue;
2908 }
2909 int req = wtoken.mAttrs.screenOrientation;
2910 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2911 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2912 continue;
2913 } else {
2914 return req;
2915 }
2916 }
2917 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2918 }
Romain Guy06882f82009-06-10 13:36:04 -07002919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 public int getOrientationFromAppTokensLocked() {
2921 int pos = mAppTokens.size() - 1;
2922 int curGroup = 0;
2923 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002924 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002926 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 while (pos >= 0) {
2928 AppWindowToken wtoken = mAppTokens.get(pos);
2929 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002930 // if we're about to tear down this window and not seek for
2931 // the behind activity, don't use it for orientation
2932 if (!findingBehind
2933 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002934 continue;
2935 }
2936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 if (!haveGroup) {
2938 // We ignore any hidden applications on the top.
2939 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2940 continue;
2941 }
2942 haveGroup = true;
2943 curGroup = wtoken.groupId;
2944 lastOrientation = wtoken.requestedOrientation;
2945 } else if (curGroup != wtoken.groupId) {
2946 // If we have hit a new application group, and the bottom
2947 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002948 // the orientation behind it, and the last app was
2949 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002951 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2952 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 return lastOrientation;
2954 }
2955 }
2956 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002957 // If this application is fullscreen, and didn't explicitly say
2958 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002960 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002961 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002962 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 return or;
2964 }
2965 // If this application has requested an explicit orientation,
2966 // then use it.
2967 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2968 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2969 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2970 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2971 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2972 return or;
2973 }
Owen Lin3413b892009-05-01 17:12:32 -07002974 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 }
2976 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2977 }
Romain Guy06882f82009-06-10 13:36:04 -07002978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002980 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002981 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2982 "updateOrientationFromAppTokens()")) {
2983 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2984 }
2985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 Configuration config;
2987 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002988 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2989 freezeThisOneIfNeeded);
2990 Binder.restoreCallingIdentity(ident);
2991 return config;
2992 }
2993
2994 Configuration updateOrientationFromAppTokensUnchecked(
2995 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2996 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002998 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002999 if (config != null) {
3000 mLayoutNeeded = true;
3001 performLayoutAndPlaceSurfacesLocked();
3002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 return config;
3005 }
Romain Guy06882f82009-06-10 13:36:04 -07003006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 /*
3008 * The orientation is computed from non-application windows first. If none of
3009 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003010 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3012 * android.os.IBinder)
3013 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003014 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07003015 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 long ident = Binder.clearCallingIdentity();
3018 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003019 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 if (req != mForcedAppOrientation) {
3022 changed = true;
3023 mForcedAppOrientation = req;
3024 //send a message to Policy indicating orientation change to take
3025 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003026 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 }
Romain Guy06882f82009-06-10 13:36:04 -07003028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 if (changed) {
3030 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07003031 WindowManagerPolicy.USE_LAST_ROTATION,
3032 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 if (changed) {
3034 if (freezeThisOneIfNeeded != null) {
3035 AppWindowToken wtoken = findAppWindowToken(
3036 freezeThisOneIfNeeded);
3037 if (wtoken != null) {
3038 startAppFreezingScreenLocked(wtoken,
3039 ActivityInfo.CONFIG_ORIENTATION);
3040 }
3041 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003042 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 }
3044 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003045
3046 // No obvious action we need to take, but if our current
3047 // state mismatches the activity maanager's, update it
3048 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003049 mTempConfiguration.setToDefaults();
3050 if (computeNewConfigurationLocked(mTempConfiguration)) {
3051 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003052 return new Configuration(mTempConfiguration);
3053 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003054 }
3055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 } finally {
3057 Binder.restoreCallingIdentity(ident);
3058 }
Romain Guy06882f82009-06-10 13:36:04 -07003059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 return null;
3061 }
Romain Guy06882f82009-06-10 13:36:04 -07003062
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003063 int computeForcedAppOrientationLocked() {
3064 int req = getOrientationFromWindowsLocked();
3065 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3066 req = getOrientationFromAppTokensLocked();
3067 }
3068 return req;
3069 }
Romain Guy06882f82009-06-10 13:36:04 -07003070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3072 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3073 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003074 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 }
Romain Guy06882f82009-06-10 13:36:04 -07003076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 synchronized(mWindowMap) {
3078 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3079 if (wtoken == null) {
3080 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3081 return;
3082 }
Romain Guy06882f82009-06-10 13:36:04 -07003083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 wtoken.requestedOrientation = requestedOrientation;
3085 }
3086 }
Romain Guy06882f82009-06-10 13:36:04 -07003087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 public int getAppOrientation(IApplicationToken token) {
3089 synchronized(mWindowMap) {
3090 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3091 if (wtoken == null) {
3092 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3093 }
Romain Guy06882f82009-06-10 13:36:04 -07003094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 return wtoken.requestedOrientation;
3096 }
3097 }
Romain Guy06882f82009-06-10 13:36:04 -07003098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3100 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3101 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003102 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 }
3104
3105 synchronized(mWindowMap) {
3106 boolean changed = false;
3107 if (token == null) {
3108 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3109 changed = mFocusedApp != null;
3110 mFocusedApp = null;
3111 mKeyWaiter.tickle();
3112 } else {
3113 AppWindowToken newFocus = findAppWindowToken(token);
3114 if (newFocus == null) {
3115 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3116 return;
3117 }
3118 changed = mFocusedApp != newFocus;
3119 mFocusedApp = newFocus;
3120 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3121 mKeyWaiter.tickle();
3122 }
3123
3124 if (moveFocusNow && changed) {
3125 final long origId = Binder.clearCallingIdentity();
3126 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3127 Binder.restoreCallingIdentity(origId);
3128 }
3129 }
3130 }
3131
3132 public void prepareAppTransition(int transit) {
3133 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3134 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003135 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 }
Romain Guy06882f82009-06-10 13:36:04 -07003137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 synchronized(mWindowMap) {
3139 if (DEBUG_APP_TRANSITIONS) Log.v(
3140 TAG, "Prepare app transition: transit=" + transit
3141 + " mNextAppTransition=" + mNextAppTransition);
3142 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003143 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3144 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003146 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3147 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3148 // Opening a new task always supersedes a close for the anim.
3149 mNextAppTransition = transit;
3150 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3151 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3152 // Opening a new activity always supersedes a close for the anim.
3153 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 }
3155 mAppTransitionReady = false;
3156 mAppTransitionTimeout = false;
3157 mStartingIconInTransition = false;
3158 mSkipAppTransitionAnimation = false;
3159 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3160 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3161 5000);
3162 }
3163 }
3164 }
3165
3166 public int getPendingAppTransition() {
3167 return mNextAppTransition;
3168 }
Romain Guy06882f82009-06-10 13:36:04 -07003169
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003170 public void overridePendingAppTransition(String packageName,
3171 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003172 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003173 mNextAppTransitionPackage = packageName;
3174 mNextAppTransitionEnter = enterAnim;
3175 mNextAppTransitionExit = exitAnim;
3176 }
3177 }
3178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 public void executeAppTransition() {
3180 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3181 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003182 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 }
Romain Guy06882f82009-06-10 13:36:04 -07003184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003186 if (DEBUG_APP_TRANSITIONS) {
3187 RuntimeException e = new RuntimeException("here");
3188 e.fillInStackTrace();
3189 Log.w(TAG, "Execute app transition: mNextAppTransition="
3190 + mNextAppTransition, e);
3191 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003192 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 mAppTransitionReady = true;
3194 final long origId = Binder.clearCallingIdentity();
3195 performLayoutAndPlaceSurfacesLocked();
3196 Binder.restoreCallingIdentity(origId);
3197 }
3198 }
3199 }
3200
3201 public void setAppStartingWindow(IBinder token, String pkg,
3202 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3203 IBinder transferFrom, boolean createIfNeeded) {
3204 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3205 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003206 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 }
3208
3209 synchronized(mWindowMap) {
3210 if (DEBUG_STARTING_WINDOW) Log.v(
3211 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3212 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 AppWindowToken wtoken = findAppWindowToken(token);
3215 if (wtoken == null) {
3216 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3217 return;
3218 }
3219
3220 // If the display is frozen, we won't do anything until the
3221 // actual window is displayed so there is no reason to put in
3222 // the starting window.
3223 if (mDisplayFrozen) {
3224 return;
3225 }
Romain Guy06882f82009-06-10 13:36:04 -07003226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 if (wtoken.startingData != null) {
3228 return;
3229 }
Romain Guy06882f82009-06-10 13:36:04 -07003230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 if (transferFrom != null) {
3232 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3233 if (ttoken != null) {
3234 WindowState startingWindow = ttoken.startingWindow;
3235 if (startingWindow != null) {
3236 if (mStartingIconInTransition) {
3237 // In this case, the starting icon has already
3238 // been displayed, so start letting windows get
3239 // shown immediately without any more transitions.
3240 mSkipAppTransitionAnimation = true;
3241 }
3242 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3243 "Moving existing starting from " + ttoken
3244 + " to " + wtoken);
3245 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 // Transfer the starting window over to the new
3248 // token.
3249 wtoken.startingData = ttoken.startingData;
3250 wtoken.startingView = ttoken.startingView;
3251 wtoken.startingWindow = startingWindow;
3252 ttoken.startingData = null;
3253 ttoken.startingView = null;
3254 ttoken.startingWindow = null;
3255 ttoken.startingMoved = true;
3256 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003257 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003259 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3260 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 mWindows.remove(startingWindow);
3262 ttoken.windows.remove(startingWindow);
3263 ttoken.allAppWindows.remove(startingWindow);
3264 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 // Propagate other interesting state between the
3267 // tokens. If the old token is displayed, we should
3268 // immediately force the new one to be displayed. If
3269 // it is animating, we need to move that animation to
3270 // the new one.
3271 if (ttoken.allDrawn) {
3272 wtoken.allDrawn = true;
3273 }
3274 if (ttoken.firstWindowDrawn) {
3275 wtoken.firstWindowDrawn = true;
3276 }
3277 if (!ttoken.hidden) {
3278 wtoken.hidden = false;
3279 wtoken.hiddenRequested = false;
3280 wtoken.willBeHidden = false;
3281 }
3282 if (wtoken.clientHidden != ttoken.clientHidden) {
3283 wtoken.clientHidden = ttoken.clientHidden;
3284 wtoken.sendAppVisibilityToClients();
3285 }
3286 if (ttoken.animation != null) {
3287 wtoken.animation = ttoken.animation;
3288 wtoken.animating = ttoken.animating;
3289 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3290 ttoken.animation = null;
3291 ttoken.animLayerAdjustment = 0;
3292 wtoken.updateLayers();
3293 ttoken.updateLayers();
3294 }
Romain Guy06882f82009-06-10 13:36:04 -07003295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 mLayoutNeeded = true;
3298 performLayoutAndPlaceSurfacesLocked();
3299 Binder.restoreCallingIdentity(origId);
3300 return;
3301 } else if (ttoken.startingData != null) {
3302 // The previous app was getting ready to show a
3303 // starting window, but hasn't yet done so. Steal it!
3304 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3305 "Moving pending starting from " + ttoken
3306 + " to " + wtoken);
3307 wtoken.startingData = ttoken.startingData;
3308 ttoken.startingData = null;
3309 ttoken.startingMoved = true;
3310 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3311 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3312 // want to process the message ASAP, before any other queued
3313 // messages.
3314 mH.sendMessageAtFrontOfQueue(m);
3315 return;
3316 }
3317 }
3318 }
3319
3320 // There is no existing starting window, and the caller doesn't
3321 // want us to create one, so that's it!
3322 if (!createIfNeeded) {
3323 return;
3324 }
Romain Guy06882f82009-06-10 13:36:04 -07003325
Dianne Hackborn284ac932009-08-28 10:34:25 -07003326 // If this is a translucent or wallpaper window, then don't
3327 // show a starting window -- the current effect (a full-screen
3328 // opaque starting window that fades away to the real contents
3329 // when it is ready) does not work for this.
3330 if (theme != 0) {
3331 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3332 com.android.internal.R.styleable.Window);
3333 if (ent.array.getBoolean(
3334 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3335 return;
3336 }
3337 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003338 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3339 return;
3340 }
3341 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003342 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3343 return;
3344 }
3345 }
3346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 mStartingIconInTransition = true;
3348 wtoken.startingData = new StartingData(
3349 pkg, theme, nonLocalizedLabel,
3350 labelRes, icon);
3351 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3352 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3353 // want to process the message ASAP, before any other queued
3354 // messages.
3355 mH.sendMessageAtFrontOfQueue(m);
3356 }
3357 }
3358
3359 public void setAppWillBeHidden(IBinder token) {
3360 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3361 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003362 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 }
3364
3365 AppWindowToken wtoken;
3366
3367 synchronized(mWindowMap) {
3368 wtoken = findAppWindowToken(token);
3369 if (wtoken == null) {
3370 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3371 return;
3372 }
3373 wtoken.willBeHidden = true;
3374 }
3375 }
Romain Guy06882f82009-06-10 13:36:04 -07003376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3378 boolean visible, int transit, boolean performLayout) {
3379 boolean delayed = false;
3380
3381 if (wtoken.clientHidden == visible) {
3382 wtoken.clientHidden = !visible;
3383 wtoken.sendAppVisibilityToClients();
3384 }
Romain Guy06882f82009-06-10 13:36:04 -07003385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 wtoken.willBeHidden = false;
3387 if (wtoken.hidden == visible) {
3388 final int N = wtoken.allAppWindows.size();
3389 boolean changed = false;
3390 if (DEBUG_APP_TRANSITIONS) Log.v(
3391 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3392 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003395
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003396 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 if (wtoken.animation == sDummyAnimation) {
3398 wtoken.animation = null;
3399 }
3400 applyAnimationLocked(wtoken, lp, transit, visible);
3401 changed = true;
3402 if (wtoken.animation != null) {
3403 delayed = runningAppAnimation = true;
3404 }
3405 }
Romain Guy06882f82009-06-10 13:36:04 -07003406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 for (int i=0; i<N; i++) {
3408 WindowState win = wtoken.allAppWindows.get(i);
3409 if (win == wtoken.startingWindow) {
3410 continue;
3411 }
3412
3413 if (win.isAnimating()) {
3414 delayed = true;
3415 }
Romain Guy06882f82009-06-10 13:36:04 -07003416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3418 //win.dump(" ");
3419 if (visible) {
3420 if (!win.isVisibleNow()) {
3421 if (!runningAppAnimation) {
3422 applyAnimationLocked(win,
3423 WindowManagerPolicy.TRANSIT_ENTER, true);
3424 }
3425 changed = true;
3426 }
3427 } else if (win.isVisibleNow()) {
3428 if (!runningAppAnimation) {
3429 applyAnimationLocked(win,
3430 WindowManagerPolicy.TRANSIT_EXIT, false);
3431 }
3432 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3433 KeyWaiter.RETURN_NOTHING);
3434 changed = true;
3435 }
3436 }
3437
3438 wtoken.hidden = wtoken.hiddenRequested = !visible;
3439 if (!visible) {
3440 unsetAppFreezingScreenLocked(wtoken, true, true);
3441 } else {
3442 // If we are being set visible, and the starting window is
3443 // not yet displayed, then make sure it doesn't get displayed.
3444 WindowState swin = wtoken.startingWindow;
3445 if (swin != null && (swin.mDrawPending
3446 || swin.mCommitDrawPending)) {
3447 swin.mPolicyVisibility = false;
3448 swin.mPolicyVisibilityAfterAnim = false;
3449 }
3450 }
Romain Guy06882f82009-06-10 13:36:04 -07003451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3453 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3454 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 if (changed && performLayout) {
3457 mLayoutNeeded = true;
3458 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 performLayoutAndPlaceSurfacesLocked();
3460 }
3461 }
3462
3463 if (wtoken.animation != null) {
3464 delayed = true;
3465 }
Romain Guy06882f82009-06-10 13:36:04 -07003466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 return delayed;
3468 }
3469
3470 public void setAppVisibility(IBinder token, boolean visible) {
3471 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3472 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003473 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 }
3475
3476 AppWindowToken wtoken;
3477
3478 synchronized(mWindowMap) {
3479 wtoken = findAppWindowToken(token);
3480 if (wtoken == null) {
3481 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3482 return;
3483 }
3484
3485 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3486 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003487 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3489 + "): mNextAppTransition=" + mNextAppTransition
3490 + " hidden=" + wtoken.hidden
3491 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3492 }
Romain Guy06882f82009-06-10 13:36:04 -07003493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 // If we are preparing an app transition, then delay changing
3495 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003496 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 // Already in requested state, don't do anything more.
3498 if (wtoken.hiddenRequested != visible) {
3499 return;
3500 }
3501 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 if (DEBUG_APP_TRANSITIONS) Log.v(
3504 TAG, "Setting dummy animation on: " + wtoken);
3505 wtoken.setDummyAnimation();
3506 mOpeningApps.remove(wtoken);
3507 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003508 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 wtoken.inPendingTransaction = true;
3510 if (visible) {
3511 mOpeningApps.add(wtoken);
3512 wtoken.allDrawn = false;
3513 wtoken.startingDisplayed = false;
3514 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003515 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 if (wtoken.clientHidden) {
3518 // In the case where we are making an app visible
3519 // but holding off for a transition, we still need
3520 // to tell the client to make its windows visible so
3521 // they get drawn. Otherwise, we will wait on
3522 // performing the transition until all windows have
3523 // been drawn, they never will be, and we are sad.
3524 wtoken.clientHidden = false;
3525 wtoken.sendAppVisibilityToClients();
3526 }
3527 } else {
3528 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003529 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 }
3531 return;
3532 }
Romain Guy06882f82009-06-10 13:36:04 -07003533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003535 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 wtoken.updateReportedVisibilityLocked();
3537 Binder.restoreCallingIdentity(origId);
3538 }
3539 }
3540
3541 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3542 boolean unfreezeSurfaceNow, boolean force) {
3543 if (wtoken.freezingScreen) {
3544 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3545 + " force=" + force);
3546 final int N = wtoken.allAppWindows.size();
3547 boolean unfrozeWindows = false;
3548 for (int i=0; i<N; i++) {
3549 WindowState w = wtoken.allAppWindows.get(i);
3550 if (w.mAppFreezing) {
3551 w.mAppFreezing = false;
3552 if (w.mSurface != null && !w.mOrientationChanging) {
3553 w.mOrientationChanging = true;
3554 }
3555 unfrozeWindows = true;
3556 }
3557 }
3558 if (force || unfrozeWindows) {
3559 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3560 wtoken.freezingScreen = false;
3561 mAppsFreezingScreen--;
3562 }
3563 if (unfreezeSurfaceNow) {
3564 if (unfrozeWindows) {
3565 mLayoutNeeded = true;
3566 performLayoutAndPlaceSurfacesLocked();
3567 }
3568 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3569 stopFreezingDisplayLocked();
3570 }
3571 }
3572 }
3573 }
Romain Guy06882f82009-06-10 13:36:04 -07003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3576 int configChanges) {
3577 if (DEBUG_ORIENTATION) {
3578 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003579 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 Log.i(TAG, "Set freezing of " + wtoken.appToken
3581 + ": hidden=" + wtoken.hidden + " freezing="
3582 + wtoken.freezingScreen, e);
3583 }
3584 if (!wtoken.hiddenRequested) {
3585 if (!wtoken.freezingScreen) {
3586 wtoken.freezingScreen = true;
3587 mAppsFreezingScreen++;
3588 if (mAppsFreezingScreen == 1) {
3589 startFreezingDisplayLocked();
3590 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3591 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3592 5000);
3593 }
3594 }
3595 final int N = wtoken.allAppWindows.size();
3596 for (int i=0; i<N; i++) {
3597 WindowState w = wtoken.allAppWindows.get(i);
3598 w.mAppFreezing = true;
3599 }
3600 }
3601 }
Romain Guy06882f82009-06-10 13:36:04 -07003602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003603 public void startAppFreezingScreen(IBinder token, int configChanges) {
3604 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3605 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003606 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 }
3608
3609 synchronized(mWindowMap) {
3610 if (configChanges == 0 && !mDisplayFrozen) {
3611 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3612 return;
3613 }
Romain Guy06882f82009-06-10 13:36:04 -07003614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 AppWindowToken wtoken = findAppWindowToken(token);
3616 if (wtoken == null || wtoken.appToken == null) {
3617 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3618 return;
3619 }
3620 final long origId = Binder.clearCallingIdentity();
3621 startAppFreezingScreenLocked(wtoken, configChanges);
3622 Binder.restoreCallingIdentity(origId);
3623 }
3624 }
Romain Guy06882f82009-06-10 13:36:04 -07003625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 public void stopAppFreezingScreen(IBinder token, boolean force) {
3627 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3628 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003629 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 }
3631
3632 synchronized(mWindowMap) {
3633 AppWindowToken wtoken = findAppWindowToken(token);
3634 if (wtoken == null || wtoken.appToken == null) {
3635 return;
3636 }
3637 final long origId = Binder.clearCallingIdentity();
3638 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3639 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3640 unsetAppFreezingScreenLocked(wtoken, true, force);
3641 Binder.restoreCallingIdentity(origId);
3642 }
3643 }
Romain Guy06882f82009-06-10 13:36:04 -07003644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 public void removeAppToken(IBinder token) {
3646 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3647 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003648 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 }
3650
3651 AppWindowToken wtoken = null;
3652 AppWindowToken startingToken = null;
3653 boolean delayed = false;
3654
3655 final long origId = Binder.clearCallingIdentity();
3656 synchronized(mWindowMap) {
3657 WindowToken basewtoken = mTokenMap.remove(token);
3658 mTokenList.remove(basewtoken);
3659 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3660 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003661 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 wtoken.inPendingTransaction = false;
3663 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003664 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 if (mClosingApps.contains(wtoken)) {
3666 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003667 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003669 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 delayed = true;
3671 }
3672 if (DEBUG_APP_TRANSITIONS) Log.v(
3673 TAG, "Removing app " + wtoken + " delayed=" + delayed
3674 + " animation=" + wtoken.animation
3675 + " animating=" + wtoken.animating);
3676 if (delayed) {
3677 // set the token aside because it has an active animation to be finished
3678 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003679 } else {
3680 // Make sure there is no animation running on this token,
3681 // so any windows associated with it will be removed as
3682 // soon as their animations are complete
3683 wtoken.animation = null;
3684 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 }
3686 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003687 if (mLastEnterAnimToken == wtoken) {
3688 mLastEnterAnimToken = null;
3689 mLastEnterAnimParams = null;
3690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 wtoken.removed = true;
3692 if (wtoken.startingData != null) {
3693 startingToken = wtoken;
3694 }
3695 unsetAppFreezingScreenLocked(wtoken, true, true);
3696 if (mFocusedApp == wtoken) {
3697 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3698 mFocusedApp = null;
3699 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3700 mKeyWaiter.tickle();
3701 }
3702 } else {
3703 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3704 }
Romain Guy06882f82009-06-10 13:36:04 -07003705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 if (!delayed && wtoken != null) {
3707 wtoken.updateReportedVisibilityLocked();
3708 }
3709 }
3710 Binder.restoreCallingIdentity(origId);
3711
3712 if (startingToken != null) {
3713 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3714 + startingToken + ": app token removed");
3715 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3716 mH.sendMessage(m);
3717 }
3718 }
3719
3720 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3721 final int NW = token.windows.size();
3722 for (int i=0; i<NW; i++) {
3723 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003724 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 mWindows.remove(win);
3726 int j = win.mChildWindows.size();
3727 while (j > 0) {
3728 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003729 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3730 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3731 "Tmp removing child window " + cwin);
3732 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 }
3734 }
3735 return NW > 0;
3736 }
3737
3738 void dumpAppTokensLocked() {
3739 for (int i=mAppTokens.size()-1; i>=0; i--) {
3740 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3741 }
3742 }
Romain Guy06882f82009-06-10 13:36:04 -07003743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 void dumpWindowsLocked() {
3745 for (int i=mWindows.size()-1; i>=0; i--) {
3746 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3747 }
3748 }
Romain Guy06882f82009-06-10 13:36:04 -07003749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 private int findWindowOffsetLocked(int tokenPos) {
3751 final int NW = mWindows.size();
3752
3753 if (tokenPos >= mAppTokens.size()) {
3754 int i = NW;
3755 while (i > 0) {
3756 i--;
3757 WindowState win = (WindowState)mWindows.get(i);
3758 if (win.getAppToken() != null) {
3759 return i+1;
3760 }
3761 }
3762 }
3763
3764 while (tokenPos > 0) {
3765 // Find the first app token below the new position that has
3766 // a window displayed.
3767 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3768 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3769 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003770 if (wtoken.sendingToBottom) {
3771 if (DEBUG_REORDER) Log.v(TAG,
3772 "Skipping token -- currently sending to bottom");
3773 tokenPos--;
3774 continue;
3775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 int i = wtoken.windows.size();
3777 while (i > 0) {
3778 i--;
3779 WindowState win = wtoken.windows.get(i);
3780 int j = win.mChildWindows.size();
3781 while (j > 0) {
3782 j--;
3783 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003784 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 for (int pos=NW-1; pos>=0; pos--) {
3786 if (mWindows.get(pos) == cwin) {
3787 if (DEBUG_REORDER) Log.v(TAG,
3788 "Found child win @" + (pos+1));
3789 return pos+1;
3790 }
3791 }
3792 }
3793 }
3794 for (int pos=NW-1; pos>=0; pos--) {
3795 if (mWindows.get(pos) == win) {
3796 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3797 return pos+1;
3798 }
3799 }
3800 }
3801 tokenPos--;
3802 }
3803
3804 return 0;
3805 }
3806
3807 private final int reAddWindowLocked(int index, WindowState win) {
3808 final int NCW = win.mChildWindows.size();
3809 boolean added = false;
3810 for (int j=0; j<NCW; j++) {
3811 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3812 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003813 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3814 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 mWindows.add(index, win);
3816 index++;
3817 added = true;
3818 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003819 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3820 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 mWindows.add(index, cwin);
3822 index++;
3823 }
3824 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003825 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3826 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 mWindows.add(index, win);
3828 index++;
3829 }
3830 return index;
3831 }
Romain Guy06882f82009-06-10 13:36:04 -07003832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3834 final int NW = token.windows.size();
3835 for (int i=0; i<NW; i++) {
3836 index = reAddWindowLocked(index, token.windows.get(i));
3837 }
3838 return index;
3839 }
3840
3841 public void moveAppToken(int index, IBinder token) {
3842 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3843 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003844 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003845 }
3846
3847 synchronized(mWindowMap) {
3848 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3849 if (DEBUG_REORDER) dumpAppTokensLocked();
3850 final AppWindowToken wtoken = findAppWindowToken(token);
3851 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3852 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3853 + token + " (" + wtoken + ")");
3854 return;
3855 }
3856 mAppTokens.add(index, wtoken);
3857 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3858 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 final long origId = Binder.clearCallingIdentity();
3861 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3862 if (DEBUG_REORDER) dumpWindowsLocked();
3863 if (tmpRemoveAppWindowsLocked(wtoken)) {
3864 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3865 if (DEBUG_REORDER) dumpWindowsLocked();
3866 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3867 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3868 if (DEBUG_REORDER) dumpWindowsLocked();
3869 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 mLayoutNeeded = true;
3871 performLayoutAndPlaceSurfacesLocked();
3872 }
3873 Binder.restoreCallingIdentity(origId);
3874 }
3875 }
3876
3877 private void removeAppTokensLocked(List<IBinder> tokens) {
3878 // XXX This should be done more efficiently!
3879 // (take advantage of the fact that both lists should be
3880 // ordered in the same way.)
3881 int N = tokens.size();
3882 for (int i=0; i<N; i++) {
3883 IBinder token = tokens.get(i);
3884 final AppWindowToken wtoken = findAppWindowToken(token);
3885 if (!mAppTokens.remove(wtoken)) {
3886 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3887 + token + " (" + wtoken + ")");
3888 i--;
3889 N--;
3890 }
3891 }
3892 }
3893
Dianne Hackborna8f60182009-09-01 19:01:50 -07003894 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3895 boolean updateFocusAndLayout) {
3896 // First remove all of the windows from the list.
3897 tmpRemoveAppWindowsLocked(wtoken);
3898
3899 // Where to start adding?
3900 int pos = findWindowOffsetLocked(tokenPos);
3901
3902 // And now add them back at the correct place.
3903 pos = reAddAppWindowsLocked(pos, wtoken);
3904
3905 if (updateFocusAndLayout) {
3906 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3907 assignLayersLocked();
3908 }
3909 mLayoutNeeded = true;
3910 performLayoutAndPlaceSurfacesLocked();
3911 }
3912 }
3913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3915 // First remove all of the windows from the list.
3916 final int N = tokens.size();
3917 int i;
3918 for (i=0; i<N; i++) {
3919 WindowToken token = mTokenMap.get(tokens.get(i));
3920 if (token != null) {
3921 tmpRemoveAppWindowsLocked(token);
3922 }
3923 }
3924
3925 // Where to start adding?
3926 int pos = findWindowOffsetLocked(tokenPos);
3927
3928 // And now add them back at the correct place.
3929 for (i=0; i<N; i++) {
3930 WindowToken token = mTokenMap.get(tokens.get(i));
3931 if (token != null) {
3932 pos = reAddAppWindowsLocked(pos, token);
3933 }
3934 }
3935
Dianne Hackborna8f60182009-09-01 19:01:50 -07003936 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3937 assignLayersLocked();
3938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 mLayoutNeeded = true;
3940 performLayoutAndPlaceSurfacesLocked();
3941
3942 //dump();
3943 }
3944
3945 public void moveAppTokensToTop(List<IBinder> tokens) {
3946 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3947 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003948 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 }
3950
3951 final long origId = Binder.clearCallingIdentity();
3952 synchronized(mWindowMap) {
3953 removeAppTokensLocked(tokens);
3954 final int N = tokens.size();
3955 for (int i=0; i<N; i++) {
3956 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3957 if (wt != null) {
3958 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003959 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003960 mToTopApps.remove(wt);
3961 mToBottomApps.remove(wt);
3962 mToTopApps.add(wt);
3963 wt.sendingToBottom = false;
3964 wt.sendingToTop = true;
3965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 }
3967 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003968
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003969 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003970 moveAppWindowsLocked(tokens, mAppTokens.size());
3971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 }
3973 Binder.restoreCallingIdentity(origId);
3974 }
3975
3976 public void moveAppTokensToBottom(List<IBinder> tokens) {
3977 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3978 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003979 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 }
3981
3982 final long origId = Binder.clearCallingIdentity();
3983 synchronized(mWindowMap) {
3984 removeAppTokensLocked(tokens);
3985 final int N = tokens.size();
3986 int pos = 0;
3987 for (int i=0; i<N; i++) {
3988 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3989 if (wt != null) {
3990 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003991 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07003992 mToTopApps.remove(wt);
3993 mToBottomApps.remove(wt);
3994 mToBottomApps.add(i, wt);
3995 wt.sendingToTop = false;
3996 wt.sendingToBottom = true;
3997 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998 pos++;
3999 }
4000 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07004001
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004002 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004003 moveAppWindowsLocked(tokens, 0);
4004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 }
4006 Binder.restoreCallingIdentity(origId);
4007 }
4008
4009 // -------------------------------------------------------------
4010 // Misc IWindowSession methods
4011 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004014 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 != PackageManager.PERMISSION_GRANTED) {
4016 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4017 }
Mike Lockwooddd884682009-10-11 16:57:08 -04004018 synchronized (mKeyguardDisabled) {
4019 mKeyguardDisabled.acquire(token, tag);
4020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 }
4022
4023 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004024 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 != PackageManager.PERMISSION_GRANTED) {
4026 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4027 }
4028 synchronized (mKeyguardDisabled) {
4029 mKeyguardDisabled.release(token);
4030
4031 if (!mKeyguardDisabled.isAcquired()) {
4032 // if we are the last one to reenable the keyguard wait until
4033 // we have actaully finished reenabling until returning
4034 mWaitingUntilKeyguardReenabled = true;
4035 while (mWaitingUntilKeyguardReenabled) {
4036 try {
4037 mKeyguardDisabled.wait();
4038 } catch (InterruptedException e) {
4039 Thread.currentThread().interrupt();
4040 }
4041 }
4042 }
4043 }
4044 }
4045
4046 /**
4047 * @see android.app.KeyguardManager#exitKeyguardSecurely
4048 */
4049 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004050 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051 != PackageManager.PERMISSION_GRANTED) {
4052 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4053 }
4054 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4055 public void onKeyguardExitResult(boolean success) {
4056 try {
4057 callback.onKeyguardExitResult(success);
4058 } catch (RemoteException e) {
4059 // Client has died, we don't care.
4060 }
4061 }
4062 });
4063 }
4064
4065 public boolean inKeyguardRestrictedInputMode() {
4066 return mPolicy.inKeyguardRestrictedKeyInputMode();
4067 }
Romain Guy06882f82009-06-10 13:36:04 -07004068
Dianne Hackbornffa42482009-09-23 22:20:11 -07004069 public void closeSystemDialogs(String reason) {
4070 synchronized(mWindowMap) {
4071 for (int i=mWindows.size()-1; i>=0; i--) {
4072 WindowState w = (WindowState)mWindows.get(i);
4073 if (w.mSurface != null) {
4074 try {
4075 w.mClient.closeSystemDialogs(reason);
4076 } catch (RemoteException e) {
4077 }
4078 }
4079 }
4080 }
4081 }
4082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 static float fixScale(float scale) {
4084 if (scale < 0) scale = 0;
4085 else if (scale > 20) scale = 20;
4086 return Math.abs(scale);
4087 }
Romain Guy06882f82009-06-10 13:36:04 -07004088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 public void setAnimationScale(int which, float scale) {
4090 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4091 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004092 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 }
4094
4095 if (scale < 0) scale = 0;
4096 else if (scale > 20) scale = 20;
4097 scale = Math.abs(scale);
4098 switch (which) {
4099 case 0: mWindowAnimationScale = fixScale(scale); break;
4100 case 1: mTransitionAnimationScale = fixScale(scale); break;
4101 }
Romain Guy06882f82009-06-10 13:36:04 -07004102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 // Persist setting
4104 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4105 }
Romain Guy06882f82009-06-10 13:36:04 -07004106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 public void setAnimationScales(float[] scales) {
4108 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4109 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004110 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 }
4112
4113 if (scales != null) {
4114 if (scales.length >= 1) {
4115 mWindowAnimationScale = fixScale(scales[0]);
4116 }
4117 if (scales.length >= 2) {
4118 mTransitionAnimationScale = fixScale(scales[1]);
4119 }
4120 }
Romain Guy06882f82009-06-10 13:36:04 -07004121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 // Persist setting
4123 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4124 }
Romain Guy06882f82009-06-10 13:36:04 -07004125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 public float getAnimationScale(int which) {
4127 switch (which) {
4128 case 0: return mWindowAnimationScale;
4129 case 1: return mTransitionAnimationScale;
4130 }
4131 return 0;
4132 }
Romain Guy06882f82009-06-10 13:36:04 -07004133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 public float[] getAnimationScales() {
4135 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4136 }
Romain Guy06882f82009-06-10 13:36:04 -07004137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 public int getSwitchState(int sw) {
4139 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4140 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004141 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 }
4143 return KeyInputQueue.getSwitchState(sw);
4144 }
Romain Guy06882f82009-06-10 13:36:04 -07004145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 public int getSwitchStateForDevice(int devid, int sw) {
4147 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4148 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004149 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 }
4151 return KeyInputQueue.getSwitchState(devid, sw);
4152 }
Romain Guy06882f82009-06-10 13:36:04 -07004153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 public int getScancodeState(int sw) {
4155 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4156 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004157 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004159 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 }
Romain Guy06882f82009-06-10 13:36:04 -07004161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 public int getScancodeStateForDevice(int devid, int sw) {
4163 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4164 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004165 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004167 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 }
Romain Guy06882f82009-06-10 13:36:04 -07004169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 public int getKeycodeState(int sw) {
4171 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4172 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004173 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004175 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 }
Romain Guy06882f82009-06-10 13:36:04 -07004177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 public int getKeycodeStateForDevice(int devid, int sw) {
4179 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4180 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004181 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004183 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184 }
Romain Guy06882f82009-06-10 13:36:04 -07004185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4187 return KeyInputQueue.hasKeys(keycodes, keyExists);
4188 }
Romain Guy06882f82009-06-10 13:36:04 -07004189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 public void enableScreenAfterBoot() {
4191 synchronized(mWindowMap) {
4192 if (mSystemBooted) {
4193 return;
4194 }
4195 mSystemBooted = true;
4196 }
Romain Guy06882f82009-06-10 13:36:04 -07004197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 performEnableScreen();
4199 }
Romain Guy06882f82009-06-10 13:36:04 -07004200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 public void enableScreenIfNeededLocked() {
4202 if (mDisplayEnabled) {
4203 return;
4204 }
4205 if (!mSystemBooted) {
4206 return;
4207 }
4208 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4209 }
Romain Guy06882f82009-06-10 13:36:04 -07004210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 public void performEnableScreen() {
4212 synchronized(mWindowMap) {
4213 if (mDisplayEnabled) {
4214 return;
4215 }
4216 if (!mSystemBooted) {
4217 return;
4218 }
Romain Guy06882f82009-06-10 13:36:04 -07004219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 // Don't enable the screen until all existing windows
4221 // have been drawn.
4222 final int N = mWindows.size();
4223 for (int i=0; i<N; i++) {
4224 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07004225 if (w.isVisibleLw() && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 return;
4227 }
4228 }
Romain Guy06882f82009-06-10 13:36:04 -07004229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 mDisplayEnabled = true;
4231 if (false) {
4232 Log.i(TAG, "ENABLING SCREEN!");
4233 StringWriter sw = new StringWriter();
4234 PrintWriter pw = new PrintWriter(sw);
4235 this.dump(null, pw, null);
4236 Log.i(TAG, sw.toString());
4237 }
4238 try {
4239 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4240 if (surfaceFlinger != null) {
4241 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4242 Parcel data = Parcel.obtain();
4243 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4244 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4245 data, null, 0);
4246 data.recycle();
4247 }
4248 } catch (RemoteException ex) {
4249 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4250 }
4251 }
Romain Guy06882f82009-06-10 13:36:04 -07004252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004256 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4257 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 }
Romain Guy06882f82009-06-10 13:36:04 -07004259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 public void setInTouchMode(boolean mode) {
4261 synchronized(mWindowMap) {
4262 mInTouchMode = mode;
4263 }
4264 }
4265
Romain Guy06882f82009-06-10 13:36:04 -07004266 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004267 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004268 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004269 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004270 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004271 }
4272
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004273 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 }
Romain Guy06882f82009-06-10 13:36:04 -07004275
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004276 public void setRotationUnchecked(int rotation,
4277 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 if(DEBUG_ORIENTATION) Log.v(TAG,
4279 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 long origId = Binder.clearCallingIdentity();
4282 boolean changed;
4283 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004284 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 }
Romain Guy06882f82009-06-10 13:36:04 -07004286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 if (changed) {
4288 sendNewConfiguration();
4289 synchronized(mWindowMap) {
4290 mLayoutNeeded = true;
4291 performLayoutAndPlaceSurfacesLocked();
4292 }
4293 } else if (alwaysSendConfiguration) {
4294 //update configuration ignoring orientation change
4295 sendNewConfiguration();
4296 }
Romain Guy06882f82009-06-10 13:36:04 -07004297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 Binder.restoreCallingIdentity(origId);
4299 }
Romain Guy06882f82009-06-10 13:36:04 -07004300
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004301 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 boolean changed;
4303 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4304 rotation = mRequestedRotation;
4305 } else {
4306 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004307 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004308 }
4309 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004310 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 mRotation, mDisplayEnabled);
4312 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4313 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004316 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 "Rotation changed to " + rotation
4318 + " from " + mRotation
4319 + " (forceApp=" + mForcedAppOrientation
4320 + ", req=" + mRequestedRotation + ")");
4321 mRotation = rotation;
4322 mWindowsFreezingScreen = true;
4323 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4324 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4325 2000);
4326 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004327 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004328 mQueue.setOrientation(rotation);
4329 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004330 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 }
4332 for (int i=mWindows.size()-1; i>=0; i--) {
4333 WindowState w = (WindowState)mWindows.get(i);
4334 if (w.mSurface != null) {
4335 w.mOrientationChanging = true;
4336 }
4337 }
4338 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4339 try {
4340 mRotationWatchers.get(i).onRotationChanged(rotation);
4341 } catch (RemoteException e) {
4342 }
4343 }
4344 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 return changed;
4347 }
Romain Guy06882f82009-06-10 13:36:04 -07004348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 public int getRotation() {
4350 return mRotation;
4351 }
4352
4353 public int watchRotation(IRotationWatcher watcher) {
4354 final IBinder watcherBinder = watcher.asBinder();
4355 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4356 public void binderDied() {
4357 synchronized (mWindowMap) {
4358 for (int i=0; i<mRotationWatchers.size(); i++) {
4359 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004360 IRotationWatcher removed = mRotationWatchers.remove(i);
4361 if (removed != null) {
4362 removed.asBinder().unlinkToDeath(this, 0);
4363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 i--;
4365 }
4366 }
4367 }
4368 }
4369 };
Romain Guy06882f82009-06-10 13:36:04 -07004370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 synchronized (mWindowMap) {
4372 try {
4373 watcher.asBinder().linkToDeath(dr, 0);
4374 mRotationWatchers.add(watcher);
4375 } catch (RemoteException e) {
4376 // Client died, no cleanup needed.
4377 }
Romain Guy06882f82009-06-10 13:36:04 -07004378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 return mRotation;
4380 }
4381 }
4382
4383 /**
4384 * Starts the view server on the specified port.
4385 *
4386 * @param port The port to listener to.
4387 *
4388 * @return True if the server was successfully started, false otherwise.
4389 *
4390 * @see com.android.server.ViewServer
4391 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4392 */
4393 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004394 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 return false;
4396 }
4397
4398 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4399 return false;
4400 }
4401
4402 if (port < 1024) {
4403 return false;
4404 }
4405
4406 if (mViewServer != null) {
4407 if (!mViewServer.isRunning()) {
4408 try {
4409 return mViewServer.start();
4410 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004411 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 }
4413 }
4414 return false;
4415 }
4416
4417 try {
4418 mViewServer = new ViewServer(this, port);
4419 return mViewServer.start();
4420 } catch (IOException e) {
4421 Log.w(TAG, "View server did not start");
4422 }
4423 return false;
4424 }
4425
Romain Guy06882f82009-06-10 13:36:04 -07004426 private boolean isSystemSecure() {
4427 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4428 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4429 }
4430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 /**
4432 * Stops the view server if it exists.
4433 *
4434 * @return True if the server stopped, false if it wasn't started or
4435 * couldn't be stopped.
4436 *
4437 * @see com.android.server.ViewServer
4438 */
4439 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004440 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 return false;
4442 }
4443
4444 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4445 return false;
4446 }
4447
4448 if (mViewServer != null) {
4449 return mViewServer.stop();
4450 }
4451 return false;
4452 }
4453
4454 /**
4455 * Indicates whether the view server is running.
4456 *
4457 * @return True if the server is running, false otherwise.
4458 *
4459 * @see com.android.server.ViewServer
4460 */
4461 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004462 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 return false;
4464 }
4465
4466 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4467 return false;
4468 }
4469
4470 return mViewServer != null && mViewServer.isRunning();
4471 }
4472
4473 /**
4474 * Lists all availble windows in the system. The listing is written in the
4475 * specified Socket's output stream with the following syntax:
4476 * windowHashCodeInHexadecimal windowName
4477 * Each line of the ouput represents a different window.
4478 *
4479 * @param client The remote client to send the listing to.
4480 * @return False if an error occured, true otherwise.
4481 */
4482 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004483 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004484 return false;
4485 }
4486
4487 boolean result = true;
4488
4489 Object[] windows;
4490 synchronized (mWindowMap) {
4491 windows = new Object[mWindows.size()];
4492 //noinspection unchecked
4493 windows = mWindows.toArray(windows);
4494 }
4495
4496 BufferedWriter out = null;
4497
4498 // Any uncaught exception will crash the system process
4499 try {
4500 OutputStream clientStream = client.getOutputStream();
4501 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4502
4503 final int count = windows.length;
4504 for (int i = 0; i < count; i++) {
4505 final WindowState w = (WindowState) windows[i];
4506 out.write(Integer.toHexString(System.identityHashCode(w)));
4507 out.write(' ');
4508 out.append(w.mAttrs.getTitle());
4509 out.write('\n');
4510 }
4511
4512 out.write("DONE.\n");
4513 out.flush();
4514 } catch (Exception e) {
4515 result = false;
4516 } finally {
4517 if (out != null) {
4518 try {
4519 out.close();
4520 } catch (IOException e) {
4521 result = false;
4522 }
4523 }
4524 }
4525
4526 return result;
4527 }
4528
4529 /**
4530 * Sends a command to a target window. The result of the command, if any, will be
4531 * written in the output stream of the specified socket.
4532 *
4533 * The parameters must follow this syntax:
4534 * windowHashcode extra
4535 *
4536 * Where XX is the length in characeters of the windowTitle.
4537 *
4538 * The first parameter is the target window. The window with the specified hashcode
4539 * will be the target. If no target can be found, nothing happens. The extra parameters
4540 * will be delivered to the target window and as parameters to the command itself.
4541 *
4542 * @param client The remote client to sent the result, if any, to.
4543 * @param command The command to execute.
4544 * @param parameters The command parameters.
4545 *
4546 * @return True if the command was successfully delivered, false otherwise. This does
4547 * not indicate whether the command itself was successful.
4548 */
4549 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004550 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 return false;
4552 }
4553
4554 boolean success = true;
4555 Parcel data = null;
4556 Parcel reply = null;
4557
4558 // Any uncaught exception will crash the system process
4559 try {
4560 // Find the hashcode of the window
4561 int index = parameters.indexOf(' ');
4562 if (index == -1) {
4563 index = parameters.length();
4564 }
4565 final String code = parameters.substring(0, index);
4566 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4567
4568 // Extract the command's parameter after the window description
4569 if (index < parameters.length()) {
4570 parameters = parameters.substring(index + 1);
4571 } else {
4572 parameters = "";
4573 }
4574
4575 final WindowManagerService.WindowState window = findWindow(hashCode);
4576 if (window == null) {
4577 return false;
4578 }
4579
4580 data = Parcel.obtain();
4581 data.writeInterfaceToken("android.view.IWindow");
4582 data.writeString(command);
4583 data.writeString(parameters);
4584 data.writeInt(1);
4585 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4586
4587 reply = Parcel.obtain();
4588
4589 final IBinder binder = window.mClient.asBinder();
4590 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4591 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4592
4593 reply.readException();
4594
4595 } catch (Exception e) {
4596 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4597 success = false;
4598 } finally {
4599 if (data != null) {
4600 data.recycle();
4601 }
4602 if (reply != null) {
4603 reply.recycle();
4604 }
4605 }
4606
4607 return success;
4608 }
4609
4610 private WindowState findWindow(int hashCode) {
4611 if (hashCode == -1) {
4612 return getFocusedWindow();
4613 }
4614
4615 synchronized (mWindowMap) {
4616 final ArrayList windows = mWindows;
4617 final int count = windows.size();
4618
4619 for (int i = 0; i < count; i++) {
4620 WindowState w = (WindowState) windows.get(i);
4621 if (System.identityHashCode(w) == hashCode) {
4622 return w;
4623 }
4624 }
4625 }
4626
4627 return null;
4628 }
4629
4630 /*
4631 * Instruct the Activity Manager to fetch the current configuration and broadcast
4632 * that to config-changed listeners if appropriate.
4633 */
4634 void sendNewConfiguration() {
4635 try {
4636 mActivityManager.updateConfiguration(null);
4637 } catch (RemoteException e) {
4638 }
4639 }
Romain Guy06882f82009-06-10 13:36:04 -07004640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 public Configuration computeNewConfiguration() {
4642 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004643 return computeNewConfigurationLocked();
4644 }
4645 }
Romain Guy06882f82009-06-10 13:36:04 -07004646
Dianne Hackbornc485a602009-03-24 22:39:49 -07004647 Configuration computeNewConfigurationLocked() {
4648 Configuration config = new Configuration();
4649 if (!computeNewConfigurationLocked(config)) {
4650 return null;
4651 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004652 return config;
4653 }
Romain Guy06882f82009-06-10 13:36:04 -07004654
Dianne Hackbornc485a602009-03-24 22:39:49 -07004655 boolean computeNewConfigurationLocked(Configuration config) {
4656 if (mDisplay == null) {
4657 return false;
4658 }
4659 mQueue.getInputConfiguration(config);
4660 final int dw = mDisplay.getWidth();
4661 final int dh = mDisplay.getHeight();
4662 int orientation = Configuration.ORIENTATION_SQUARE;
4663 if (dw < dh) {
4664 orientation = Configuration.ORIENTATION_PORTRAIT;
4665 } else if (dw > dh) {
4666 orientation = Configuration.ORIENTATION_LANDSCAPE;
4667 }
4668 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004669
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004670 DisplayMetrics dm = new DisplayMetrics();
4671 mDisplay.getMetrics(dm);
4672 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4673
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004674 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004675 // Note we only do this once because at this point we don't
4676 // expect the screen to change in this way at runtime, and want
4677 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004678 int longSize = dw;
4679 int shortSize = dh;
4680 if (longSize < shortSize) {
4681 int tmp = longSize;
4682 longSize = shortSize;
4683 shortSize = tmp;
4684 }
4685 longSize = (int)(longSize/dm.density);
4686 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004687
Dianne Hackborn723738c2009-06-25 19:48:04 -07004688 // These semi-magic numbers define our compatibility modes for
4689 // applications with different screens. Don't change unless you
4690 // make sure to test lots and lots of apps!
4691 if (longSize < 470) {
4692 // This is shorter than an HVGA normal density screen (which
4693 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004694 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4695 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004696 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004697 // Is this a large screen?
4698 if (longSize > 640 && shortSize >= 480) {
4699 // VGA or larger screens at medium density are the point
4700 // at which we consider it to be a large screen.
4701 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4702 } else {
4703 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4704
4705 // If this screen is wider than normal HVGA, or taller
4706 // than FWVGA, then for old apps we want to run in size
4707 // compatibility mode.
4708 if (shortSize > 321 || longSize > 570) {
4709 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4710 }
4711 }
4712
4713 // Is this a long screen?
4714 if (((longSize*3)/5) >= (shortSize-1)) {
4715 // Anything wider than WVGA (5:3) is considering to be long.
4716 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4717 } else {
4718 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4719 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004720 }
4721 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004722 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004723
Dianne Hackbornc485a602009-03-24 22:39:49 -07004724 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4725 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4726 mPolicy.adjustConfigurationLw(config);
4727 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004728 }
Romain Guy06882f82009-06-10 13:36:04 -07004729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004730 // -------------------------------------------------------------
4731 // Input Events and Focus Management
4732 // -------------------------------------------------------------
4733
4734 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004735 long curTime = SystemClock.uptimeMillis();
4736
Michael Chane10de972009-05-18 11:24:50 -07004737 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004738 if (mLastTouchEventType == eventType &&
4739 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4740 return;
4741 }
4742 mLastUserActivityCallTime = curTime;
4743 mLastTouchEventType = eventType;
4744 }
4745
4746 if (targetWin == null
4747 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4748 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004749 }
4750 }
4751
4752 // tells if it's a cheek event or not -- this function is stateful
4753 private static final int EVENT_NONE = 0;
4754 private static final int EVENT_UNKNOWN = 0;
4755 private static final int EVENT_CHEEK = 0;
4756 private static final int EVENT_IGNORE_DURATION = 300; // ms
4757 private static final float CHEEK_THRESHOLD = 0.6f;
4758 private int mEventState = EVENT_NONE;
4759 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004761 private int eventType(MotionEvent ev) {
4762 float size = ev.getSize();
4763 switch (ev.getAction()) {
4764 case MotionEvent.ACTION_DOWN:
4765 mEventSize = size;
4766 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4767 case MotionEvent.ACTION_UP:
4768 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004769 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 case MotionEvent.ACTION_MOVE:
4771 final int N = ev.getHistorySize();
4772 if (size > mEventSize) mEventSize = size;
4773 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4774 for (int i=0; i<N; i++) {
4775 size = ev.getHistoricalSize(i);
4776 if (size > mEventSize) mEventSize = size;
4777 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4778 }
4779 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4780 return TOUCH_EVENT;
4781 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004782 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783 }
4784 default:
4785 // not good
4786 return OTHER_EVENT;
4787 }
4788 }
4789
4790 /**
4791 * @return Returns true if event was dispatched, false if it was dropped for any reason
4792 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004793 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004794 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4795 "dispatchPointer " + ev);
4796
Michael Chan53071d62009-05-13 17:29:48 -07004797 if (MEASURE_LATENCY) {
4798 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4799 }
4800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004802 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004803
Michael Chan53071d62009-05-13 17:29:48 -07004804 if (MEASURE_LATENCY) {
4805 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4806 }
4807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004808 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004810 if (action == MotionEvent.ACTION_UP) {
4811 // let go of our target
4812 mKeyWaiter.mMotionTarget = null;
4813 mPowerManager.logPointerUpEvent();
4814 } else if (action == MotionEvent.ACTION_DOWN) {
4815 mPowerManager.logPointerDownEvent();
4816 }
4817
4818 if (targetObj == null) {
4819 // In this case we are either dropping the event, or have received
4820 // a move or up without a down. It is common to receive move
4821 // events in such a way, since this means the user is moving the
4822 // pointer without actually pressing down. All other cases should
4823 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004824 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004825 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4826 }
4827 if (qev != null) {
4828 mQueue.recycleEvent(qev);
4829 }
4830 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004831 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 }
4833 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4834 if (qev != null) {
4835 mQueue.recycleEvent(qev);
4836 }
4837 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004838 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004839 }
Romain Guy06882f82009-06-10 13:36:04 -07004840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004843 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004844 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004845
4846 //Log.i(TAG, "Sending " + ev + " to " + target);
4847
4848 if (uid != 0 && uid != target.mSession.mUid) {
4849 if (mContext.checkPermission(
4850 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4851 != PackageManager.PERMISSION_GRANTED) {
4852 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4853 + pid + " uid " + uid + " to window " + target
4854 + " owned by uid " + target.mSession.mUid);
4855 if (qev != null) {
4856 mQueue.recycleEvent(qev);
4857 }
4858 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004859 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860 }
4861 }
4862
Michael Chan53071d62009-05-13 17:29:48 -07004863 if (MEASURE_LATENCY) {
4864 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4865 }
4866
Romain Guy06882f82009-06-10 13:36:04 -07004867 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4869 //target wants to ignore fat touch events
4870 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4871 //explicit flag to return without processing event further
4872 boolean returnFlag = false;
4873 if((action == MotionEvent.ACTION_DOWN)) {
4874 mFatTouch = false;
4875 if(cheekPress) {
4876 mFatTouch = true;
4877 returnFlag = true;
4878 }
4879 } else {
4880 if(action == MotionEvent.ACTION_UP) {
4881 if(mFatTouch) {
4882 //earlier even was invalid doesnt matter if current up is cheekpress or not
4883 mFatTouch = false;
4884 returnFlag = true;
4885 } else if(cheekPress) {
4886 //cancel the earlier event
4887 ev.setAction(MotionEvent.ACTION_CANCEL);
4888 action = MotionEvent.ACTION_CANCEL;
4889 }
4890 } else if(action == MotionEvent.ACTION_MOVE) {
4891 if(mFatTouch) {
4892 //two cases here
4893 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004894 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 returnFlag = true;
4896 } else if(cheekPress) {
4897 //valid down followed by invalid moves
4898 //an invalid move have to cancel earlier action
4899 ev.setAction(MotionEvent.ACTION_CANCEL);
4900 action = MotionEvent.ACTION_CANCEL;
4901 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4902 //note that the subsequent invalid moves will not get here
4903 mFatTouch = true;
4904 }
4905 }
4906 } //else if action
4907 if(returnFlag) {
4908 //recycle que, ev
4909 if (qev != null) {
4910 mQueue.recycleEvent(qev);
4911 }
4912 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004913 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 }
4915 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004916
Michael Chan9f028e62009-08-04 17:37:46 -07004917 // Enable this for testing the "right" value
4918 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004919 int max_events_per_sec = 35;
4920 try {
4921 max_events_per_sec = Integer.parseInt(SystemProperties
4922 .get("windowsmgr.max_events_per_sec"));
4923 if (max_events_per_sec < 1) {
4924 max_events_per_sec = 35;
4925 }
4926 } catch (NumberFormatException e) {
4927 }
4928 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4929 }
4930
4931 /*
4932 * Throttle events to minimize CPU usage when there's a flood of events
4933 * e.g. constant contact with the screen
4934 */
4935 if (action == MotionEvent.ACTION_MOVE) {
4936 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4937 long now = SystemClock.uptimeMillis();
4938 if (now < nextEventTime) {
4939 try {
4940 Thread.sleep(nextEventTime - now);
4941 } catch (InterruptedException e) {
4942 }
4943 mLastTouchEventTime = nextEventTime;
4944 } else {
4945 mLastTouchEventTime = now;
4946 }
4947 }
4948
Michael Chan53071d62009-05-13 17:29:48 -07004949 if (MEASURE_LATENCY) {
4950 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4951 }
4952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953 synchronized(mWindowMap) {
4954 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4955 mKeyWaiter.bindTargetWindowLocked(target,
4956 KeyWaiter.RETURN_PENDING_POINTER, qev);
4957 ev = null;
4958 } else {
4959 if (action == MotionEvent.ACTION_DOWN) {
4960 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4961 if (out != null) {
4962 MotionEvent oev = MotionEvent.obtain(ev);
4963 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4964 do {
4965 final Rect frame = out.mFrame;
4966 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4967 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004968 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 } catch (android.os.RemoteException e) {
4970 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4971 }
4972 oev.offsetLocation((float)frame.left, (float)frame.top);
4973 out = out.mNextOutsideTouch;
4974 } while (out != null);
4975 mKeyWaiter.mOutsideTouchTargets = null;
4976 }
4977 }
4978 final Rect frame = target.mFrame;
4979 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4980 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004981
4982 // If we are on top of the wallpaper, then the wallpaper also
4983 // gets to see this movement.
4984 if (mWallpaperTarget == target) {
4985 sendPointerToWallpaperLocked(target, ev, eventTime);
4986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004987 }
4988 }
Romain Guy06882f82009-06-10 13:36:04 -07004989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004990 // finally offset the event to the target's coordinate system and
4991 // dispatch the event.
4992 try {
4993 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4994 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4995 }
Michael Chan53071d62009-05-13 17:29:48 -07004996
4997 if (MEASURE_LATENCY) {
4998 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4999 }
5000
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005001 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005002
5003 if (MEASURE_LATENCY) {
5004 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5005 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005006 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 } catch (android.os.RemoteException e) {
5008 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
5009 mKeyWaiter.mMotionTarget = null;
5010 try {
5011 removeWindow(target.mSession, target.mClient);
5012 } catch (java.util.NoSuchElementException ex) {
5013 // This will happen if the window has already been
5014 // removed.
5015 }
5016 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005017 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 }
Romain Guy06882f82009-06-10 13:36:04 -07005019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 /**
5021 * @return Returns true if event was dispatched, false if it was dropped for any reason
5022 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005023 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005024 if (DEBUG_INPUT) Log.v(
5025 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005028 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005029 if (focusObj == null) {
5030 Log.w(TAG, "No focus window, dropping trackball: " + ev);
5031 if (qev != null) {
5032 mQueue.recycleEvent(qev);
5033 }
5034 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005035 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 }
5037 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5038 if (qev != null) {
5039 mQueue.recycleEvent(qev);
5040 }
5041 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005042 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 }
Romain Guy06882f82009-06-10 13:36:04 -07005044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047 if (uid != 0 && uid != focus.mSession.mUid) {
5048 if (mContext.checkPermission(
5049 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5050 != PackageManager.PERMISSION_GRANTED) {
5051 Log.w(TAG, "Permission denied: injecting key event from pid "
5052 + pid + " uid " + uid + " to window " + focus
5053 + " owned by uid " + focus.mSession.mUid);
5054 if (qev != null) {
5055 mQueue.recycleEvent(qev);
5056 }
5057 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005058 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 }
5060 }
Romain Guy06882f82009-06-10 13:36:04 -07005061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 synchronized(mWindowMap) {
5065 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5066 mKeyWaiter.bindTargetWindowLocked(focus,
5067 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5068 // We don't deliver movement events to the client, we hold
5069 // them and wait for them to call back.
5070 ev = null;
5071 } else {
5072 mKeyWaiter.bindTargetWindowLocked(focus);
5073 }
5074 }
Romain Guy06882f82009-06-10 13:36:04 -07005075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005077 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005078 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 } catch (android.os.RemoteException e) {
5080 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5081 try {
5082 removeWindow(focus.mSession, focus.mClient);
5083 } catch (java.util.NoSuchElementException ex) {
5084 // This will happen if the window has already been
5085 // removed.
5086 }
5087 }
Romain Guy06882f82009-06-10 13:36:04 -07005088
Dianne Hackborncfaef692009-06-15 14:24:44 -07005089 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 }
Romain Guy06882f82009-06-10 13:36:04 -07005091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092 /**
5093 * @return Returns true if event was dispatched, false if it was dropped for any reason
5094 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005095 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5097
5098 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005099 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 if (focusObj == null) {
5101 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005102 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 }
5104 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005105 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 }
Romain Guy06882f82009-06-10 13:36:04 -07005107
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005108 // Okay we have finished waiting for the last event to be processed.
5109 // First off, if this is a repeat event, check to see if there is
5110 // a corresponding up event in the queue. If there is, we will
5111 // just drop the repeat, because it makes no sense to repeat after
5112 // the user has released a key. (This is especially important for
5113 // long presses.)
5114 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5115 return INJECT_SUCCEEDED;
5116 }
5117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 if (DEBUG_INPUT) Log.v(
5121 TAG, "Dispatching to " + focus + ": " + event);
5122
5123 if (uid != 0 && uid != focus.mSession.mUid) {
5124 if (mContext.checkPermission(
5125 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5126 != PackageManager.PERMISSION_GRANTED) {
5127 Log.w(TAG, "Permission denied: injecting key event from pid "
5128 + pid + " uid " + uid + " to window " + focus
5129 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005130 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131 }
5132 }
Romain Guy06882f82009-06-10 13:36:04 -07005133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005134 synchronized(mWindowMap) {
5135 mKeyWaiter.bindTargetWindowLocked(focus);
5136 }
5137
5138 // NOSHIP extra state logging
5139 mKeyWaiter.recordDispatchState(event, focus);
5140 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 try {
5143 if (DEBUG_INPUT || DEBUG_FOCUS) {
5144 Log.v(TAG, "Delivering key " + event.getKeyCode()
5145 + " to " + focus);
5146 }
5147 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005148 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 } catch (android.os.RemoteException e) {
5150 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5151 try {
5152 removeWindow(focus.mSession, focus.mClient);
5153 } catch (java.util.NoSuchElementException ex) {
5154 // This will happen if the window has already been
5155 // removed.
5156 }
5157 }
Romain Guy06882f82009-06-10 13:36:04 -07005158
Dianne Hackborncfaef692009-06-15 14:24:44 -07005159 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 }
Romain Guy06882f82009-06-10 13:36:04 -07005161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 public void pauseKeyDispatching(IBinder _token) {
5163 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5164 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005165 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 }
5167
5168 synchronized (mWindowMap) {
5169 WindowToken token = mTokenMap.get(_token);
5170 if (token != null) {
5171 mKeyWaiter.pauseDispatchingLocked(token);
5172 }
5173 }
5174 }
5175
5176 public void resumeKeyDispatching(IBinder _token) {
5177 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5178 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005179 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 }
5181
5182 synchronized (mWindowMap) {
5183 WindowToken token = mTokenMap.get(_token);
5184 if (token != null) {
5185 mKeyWaiter.resumeDispatchingLocked(token);
5186 }
5187 }
5188 }
5189
5190 public void setEventDispatching(boolean enabled) {
5191 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5192 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005193 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194 }
5195
5196 synchronized (mWindowMap) {
5197 mKeyWaiter.setEventDispatchingLocked(enabled);
5198 }
5199 }
Romain Guy06882f82009-06-10 13:36:04 -07005200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 /**
5202 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005203 *
5204 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 * {@link SystemClock#uptimeMillis()} as the timebase.)
5206 * @param sync If true, wait for the event to be completed before returning to the caller.
5207 * @return Returns true if event was dispatched, false if it was dropped for any reason
5208 */
5209 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5210 long downTime = ev.getDownTime();
5211 long eventTime = ev.getEventTime();
5212
5213 int action = ev.getAction();
5214 int code = ev.getKeyCode();
5215 int repeatCount = ev.getRepeatCount();
5216 int metaState = ev.getMetaState();
5217 int deviceId = ev.getDeviceId();
5218 int scancode = ev.getScanCode();
5219
5220 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5221 if (downTime == 0) downTime = eventTime;
5222
5223 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005224 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005226 final int pid = Binder.getCallingPid();
5227 final int uid = Binder.getCallingUid();
5228 final long ident = Binder.clearCallingIdentity();
5229 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005231 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005232 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005233 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005234 switch (result) {
5235 case INJECT_NO_PERMISSION:
5236 throw new SecurityException(
5237 "Injecting to another application requires INJECT_EVENT permission");
5238 case INJECT_SUCCEEDED:
5239 return true;
5240 }
5241 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 }
5243
5244 /**
5245 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005246 *
5247 * @param ev A motion event describing the pointer (touch) action. (As noted in
5248 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005249 * {@link SystemClock#uptimeMillis()} as the timebase.)
5250 * @param sync If true, wait for the event to be completed before returning to the caller.
5251 * @return Returns true if event was dispatched, false if it was dropped for any reason
5252 */
5253 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005254 final int pid = Binder.getCallingPid();
5255 final int uid = Binder.getCallingUid();
5256 final long ident = Binder.clearCallingIdentity();
5257 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005259 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005261 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005262 switch (result) {
5263 case INJECT_NO_PERMISSION:
5264 throw new SecurityException(
5265 "Injecting to another application requires INJECT_EVENT permission");
5266 case INJECT_SUCCEEDED:
5267 return true;
5268 }
5269 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005270 }
Romain Guy06882f82009-06-10 13:36:04 -07005271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005272 /**
5273 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005274 *
5275 * @param ev A motion event describing the trackball action. (As noted in
5276 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 * {@link SystemClock#uptimeMillis()} as the timebase.)
5278 * @param sync If true, wait for the event to be completed before returning to the caller.
5279 * @return Returns true if event was dispatched, false if it was dropped for any reason
5280 */
5281 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005282 final int pid = Binder.getCallingPid();
5283 final int uid = Binder.getCallingUid();
5284 final long ident = Binder.clearCallingIdentity();
5285 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005286 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005287 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005289 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005290 switch (result) {
5291 case INJECT_NO_PERMISSION:
5292 throw new SecurityException(
5293 "Injecting to another application requires INJECT_EVENT permission");
5294 case INJECT_SUCCEEDED:
5295 return true;
5296 }
5297 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005298 }
Romain Guy06882f82009-06-10 13:36:04 -07005299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 private WindowState getFocusedWindow() {
5301 synchronized (mWindowMap) {
5302 return getFocusedWindowLocked();
5303 }
5304 }
5305
5306 private WindowState getFocusedWindowLocked() {
5307 return mCurrentFocus;
5308 }
Romain Guy06882f82009-06-10 13:36:04 -07005309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 /**
5311 * This class holds the state for dispatching key events. This state
5312 * is protected by the KeyWaiter instance, NOT by the window lock. You
5313 * can be holding the main window lock while acquire the KeyWaiter lock,
5314 * but not the other way around.
5315 */
5316 final class KeyWaiter {
5317 // NOSHIP debugging
5318 public class DispatchState {
5319 private KeyEvent event;
5320 private WindowState focus;
5321 private long time;
5322 private WindowState lastWin;
5323 private IBinder lastBinder;
5324 private boolean finished;
5325 private boolean gotFirstWindow;
5326 private boolean eventDispatching;
5327 private long timeToSwitch;
5328 private boolean wasFrozen;
5329 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005330 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5333 focus = theFocus;
5334 event = theEvent;
5335 time = System.currentTimeMillis();
5336 // snapshot KeyWaiter state
5337 lastWin = mLastWin;
5338 lastBinder = mLastBinder;
5339 finished = mFinished;
5340 gotFirstWindow = mGotFirstWindow;
5341 eventDispatching = mEventDispatching;
5342 timeToSwitch = mTimeToSwitch;
5343 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005344 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005345 // cache the paused state at ctor time as well
5346 if (theFocus == null || theFocus.mToken == null) {
5347 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5348 focusPaused = false;
5349 } else {
5350 focusPaused = theFocus.mToken.paused;
5351 }
5352 }
Romain Guy06882f82009-06-10 13:36:04 -07005353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 public String toString() {
5355 return "{{" + event + " to " + focus + " @ " + time
5356 + " lw=" + lastWin + " lb=" + lastBinder
5357 + " fin=" + finished + " gfw=" + gotFirstWindow
5358 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005359 + " wf=" + wasFrozen + " fp=" + focusPaused
5360 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 }
5362 };
5363 private DispatchState mDispatchState = null;
5364 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5365 mDispatchState = new DispatchState(theEvent, theFocus);
5366 }
5367 // END NOSHIP
5368
5369 public static final int RETURN_NOTHING = 0;
5370 public static final int RETURN_PENDING_POINTER = 1;
5371 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 final Object SKIP_TARGET_TOKEN = new Object();
5374 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 private WindowState mLastWin = null;
5377 private IBinder mLastBinder = null;
5378 private boolean mFinished = true;
5379 private boolean mGotFirstWindow = false;
5380 private boolean mEventDispatching = true;
5381 private long mTimeToSwitch = 0;
5382 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005384 // Target of Motion events
5385 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005387 // Windows above the target who would like to receive an "outside"
5388 // touch event for any down events outside of them.
5389 WindowState mOutsideTouchTargets;
5390
5391 /**
5392 * Wait for the last event dispatch to complete, then find the next
5393 * target that should receive the given event and wait for that one
5394 * to be ready to receive it.
5395 */
5396 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5397 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005398 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 long startTime = SystemClock.uptimeMillis();
5400 long keyDispatchingTimeout = 5 * 1000;
5401 long waitedFor = 0;
5402
5403 while (true) {
5404 // Figure out which window we care about. It is either the
5405 // last window we are waiting to have process the event or,
5406 // if none, then the next window we think the event should go
5407 // to. Note: we retrieve mLastWin outside of the lock, so
5408 // it may change before we lock. Thus we must check it again.
5409 WindowState targetWin = mLastWin;
5410 boolean targetIsNew = targetWin == null;
5411 if (DEBUG_INPUT) Log.v(
5412 TAG, "waitForLastKey: mFinished=" + mFinished +
5413 ", mLastWin=" + mLastWin);
5414 if (targetIsNew) {
5415 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005416 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 if (target == SKIP_TARGET_TOKEN) {
5418 // The user has pressed a special key, and we are
5419 // dropping all pending events before it.
5420 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5421 + " " + nextMotion);
5422 return null;
5423 }
5424 if (target == CONSUMED_EVENT_TOKEN) {
5425 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5426 + " " + nextMotion);
5427 return target;
5428 }
5429 targetWin = (WindowState)target;
5430 }
Romain Guy06882f82009-06-10 13:36:04 -07005431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 // Now: is it okay to send the next event to this window?
5435 synchronized (this) {
5436 // First: did we come here based on the last window not
5437 // being null, but it changed by the time we got here?
5438 // If so, try again.
5439 if (!targetIsNew && mLastWin == null) {
5440 continue;
5441 }
Romain Guy06882f82009-06-10 13:36:04 -07005442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005443 // We never dispatch events if not finished with the
5444 // last one, or the display is frozen.
5445 if (mFinished && !mDisplayFrozen) {
5446 // If event dispatching is disabled, then we
5447 // just consume the events.
5448 if (!mEventDispatching) {
5449 if (DEBUG_INPUT) Log.v(TAG,
5450 "Skipping event; dispatching disabled: "
5451 + nextKey + " " + nextMotion);
5452 return null;
5453 }
5454 if (targetWin != null) {
5455 // If this is a new target, and that target is not
5456 // paused or unresponsive, then all looks good to
5457 // handle the event.
5458 if (targetIsNew && !targetWin.mToken.paused) {
5459 return targetWin;
5460 }
Romain Guy06882f82009-06-10 13:36:04 -07005461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005462 // If we didn't find a target window, and there is no
5463 // focused app window, then just eat the events.
5464 } else if (mFocusedApp == null) {
5465 if (DEBUG_INPUT) Log.v(TAG,
5466 "Skipping event; no focused app: "
5467 + nextKey + " " + nextMotion);
5468 return null;
5469 }
5470 }
Romain Guy06882f82009-06-10 13:36:04 -07005471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 if (DEBUG_INPUT) Log.v(
5473 TAG, "Waiting for last key in " + mLastBinder
5474 + " target=" + targetWin
5475 + " mFinished=" + mFinished
5476 + " mDisplayFrozen=" + mDisplayFrozen
5477 + " targetIsNew=" + targetIsNew
5478 + " paused="
5479 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005480 + " mFocusedApp=" + mFocusedApp
5481 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 targetApp = targetWin != null
5484 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 long curTimeout = keyDispatchingTimeout;
5487 if (mTimeToSwitch != 0) {
5488 long now = SystemClock.uptimeMillis();
5489 if (mTimeToSwitch <= now) {
5490 // If an app switch key has been pressed, and we have
5491 // waited too long for the current app to finish
5492 // processing keys, then wait no more!
5493 doFinishedKeyLocked(true);
5494 continue;
5495 }
5496 long switchTimeout = mTimeToSwitch - now;
5497 if (curTimeout > switchTimeout) {
5498 curTimeout = switchTimeout;
5499 }
5500 }
Romain Guy06882f82009-06-10 13:36:04 -07005501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 try {
5503 // after that continue
5504 // processing keys, so we don't get stuck.
5505 if (DEBUG_INPUT) Log.v(
5506 TAG, "Waiting for key dispatch: " + curTimeout);
5507 wait(curTimeout);
5508 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5509 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005510 + startTime + " switchTime=" + mTimeToSwitch
5511 + " target=" + targetWin + " mLW=" + mLastWin
5512 + " mLB=" + mLastBinder + " fin=" + mFinished
5513 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 } catch (InterruptedException e) {
5515 }
5516 }
5517
5518 // If we were frozen during configuration change, restart the
5519 // timeout checks from now; otherwise look at whether we timed
5520 // out before awakening.
5521 if (mWasFrozen) {
5522 waitedFor = 0;
5523 mWasFrozen = false;
5524 } else {
5525 waitedFor = SystemClock.uptimeMillis() - startTime;
5526 }
5527
5528 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5529 IApplicationToken at = null;
5530 synchronized (this) {
5531 Log.w(TAG, "Key dispatching timed out sending to " +
5532 (targetWin != null ? targetWin.mAttrs.getTitle()
5533 : "<null>"));
5534 // NOSHIP debugging
5535 Log.w(TAG, "Dispatch state: " + mDispatchState);
5536 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5537 // END NOSHIP
5538 //dump();
5539 if (targetWin != null) {
5540 at = targetWin.getAppToken();
5541 } else if (targetApp != null) {
5542 at = targetApp.appToken;
5543 }
5544 }
5545
5546 boolean abort = true;
5547 if (at != null) {
5548 try {
5549 long timeout = at.getKeyDispatchingTimeout();
5550 if (timeout > waitedFor) {
5551 // we did not wait the proper amount of time for this application.
5552 // set the timeout to be the real timeout and wait again.
5553 keyDispatchingTimeout = timeout - waitedFor;
5554 continue;
5555 } else {
5556 abort = at.keyDispatchingTimedOut();
5557 }
5558 } catch (RemoteException ex) {
5559 }
5560 }
5561
5562 synchronized (this) {
5563 if (abort && (mLastWin == targetWin || targetWin == null)) {
5564 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005565 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 if (DEBUG_INPUT) Log.v(TAG,
5567 "Window " + mLastWin +
5568 " timed out on key input");
5569 if (mLastWin.mToken.paused) {
5570 Log.w(TAG, "Un-pausing dispatching to this window");
5571 mLastWin.mToken.paused = false;
5572 }
5573 }
5574 if (mMotionTarget == targetWin) {
5575 mMotionTarget = null;
5576 }
5577 mLastWin = null;
5578 mLastBinder = null;
5579 if (failIfTimeout || targetWin == null) {
5580 return null;
5581 }
5582 } else {
5583 Log.w(TAG, "Continuing to wait for key to be dispatched");
5584 startTime = SystemClock.uptimeMillis();
5585 }
5586 }
5587 }
5588 }
5589 }
Romain Guy06882f82009-06-10 13:36:04 -07005590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005592 MotionEvent nextMotion, boolean isPointerEvent,
5593 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 if (nextKey != null) {
5597 // Find the target window for a normal key event.
5598 final int keycode = nextKey.getKeyCode();
5599 final int repeatCount = nextKey.getRepeatCount();
5600 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5601 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005604 if (callingUid == 0 ||
5605 mContext.checkPermission(
5606 android.Manifest.permission.INJECT_EVENTS,
5607 callingPid, callingUid)
5608 == PackageManager.PERMISSION_GRANTED) {
5609 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005610 nextKey.getMetaState(), down, repeatCount,
5611 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005613 Log.w(TAG, "Event timeout during app switch: dropping "
5614 + nextKey);
5615 return SKIP_TARGET_TOKEN;
5616 }
Romain Guy06882f82009-06-10 13:36:04 -07005617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 WindowState focus = null;
5621 synchronized(mWindowMap) {
5622 focus = getFocusedWindowLocked();
5623 }
Romain Guy06882f82009-06-10 13:36:04 -07005624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005626
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005627 if (callingUid == 0 ||
5628 (focus != null && callingUid == focus.mSession.mUid) ||
5629 mContext.checkPermission(
5630 android.Manifest.permission.INJECT_EVENTS,
5631 callingPid, callingUid)
5632 == PackageManager.PERMISSION_GRANTED) {
5633 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005634 keycode, nextKey.getMetaState(), down, repeatCount,
5635 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005636 return CONSUMED_EVENT_TOKEN;
5637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 }
Romain Guy06882f82009-06-10 13:36:04 -07005639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005642 } else if (!isPointerEvent) {
5643 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5644 if (!dispatch) {
5645 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5646 + nextMotion);
5647 return SKIP_TARGET_TOKEN;
5648 }
Romain Guy06882f82009-06-10 13:36:04 -07005649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 WindowState focus = null;
5651 synchronized(mWindowMap) {
5652 focus = getFocusedWindowLocked();
5653 }
Romain Guy06882f82009-06-10 13:36:04 -07005654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5656 return focus;
5657 }
Romain Guy06882f82009-06-10 13:36:04 -07005658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005659 if (nextMotion == null) {
5660 return SKIP_TARGET_TOKEN;
5661 }
Romain Guy06882f82009-06-10 13:36:04 -07005662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005663 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5664 KeyEvent.KEYCODE_UNKNOWN);
5665 if (!dispatch) {
5666 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5667 + nextMotion);
5668 return SKIP_TARGET_TOKEN;
5669 }
Romain Guy06882f82009-06-10 13:36:04 -07005670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 // Find the target window for a pointer event.
5672 int action = nextMotion.getAction();
5673 final float xf = nextMotion.getX();
5674 final float yf = nextMotion.getY();
5675 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 final boolean screenWasOff = qev != null
5678 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682 synchronized(mWindowMap) {
5683 synchronized (this) {
5684 if (action == MotionEvent.ACTION_DOWN) {
5685 if (mMotionTarget != null) {
5686 // this is weird, we got a pen down, but we thought it was
5687 // already down!
5688 // XXX: We should probably send an ACTION_UP to the current
5689 // target.
5690 Log.w(TAG, "Pointer down received while already down in: "
5691 + mMotionTarget);
5692 mMotionTarget = null;
5693 }
Romain Guy06882f82009-06-10 13:36:04 -07005694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 // ACTION_DOWN is special, because we need to lock next events to
5696 // the window we'll land onto.
5697 final int x = (int)xf;
5698 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005700 final ArrayList windows = mWindows;
5701 final int N = windows.size();
5702 WindowState topErrWindow = null;
5703 final Rect tmpRect = mTempRect;
5704 for (int i=N-1; i>=0; i--) {
5705 WindowState child = (WindowState)windows.get(i);
5706 //Log.i(TAG, "Checking dispatch to: " + child);
5707 final int flags = child.mAttrs.flags;
5708 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5709 if (topErrWindow == null) {
5710 topErrWindow = child;
5711 }
5712 }
5713 if (!child.isVisibleLw()) {
5714 //Log.i(TAG, "Not visible!");
5715 continue;
5716 }
5717 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5718 //Log.i(TAG, "Not touchable!");
5719 if ((flags & WindowManager.LayoutParams
5720 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5721 child.mNextOutsideTouch = mOutsideTouchTargets;
5722 mOutsideTouchTargets = child;
5723 }
5724 continue;
5725 }
5726 tmpRect.set(child.mFrame);
5727 if (child.mTouchableInsets == ViewTreeObserver
5728 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5729 // The touch is inside of the window if it is
5730 // inside the frame, AND the content part of that
5731 // frame that was given by the application.
5732 tmpRect.left += child.mGivenContentInsets.left;
5733 tmpRect.top += child.mGivenContentInsets.top;
5734 tmpRect.right -= child.mGivenContentInsets.right;
5735 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5736 } else if (child.mTouchableInsets == ViewTreeObserver
5737 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5738 // The touch is inside of the window if it is
5739 // inside the frame, AND the visible part of that
5740 // frame that was given by the application.
5741 tmpRect.left += child.mGivenVisibleInsets.left;
5742 tmpRect.top += child.mGivenVisibleInsets.top;
5743 tmpRect.right -= child.mGivenVisibleInsets.right;
5744 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5745 }
5746 final int touchFlags = flags &
5747 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5748 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5749 if (tmpRect.contains(x, y) || touchFlags == 0) {
5750 //Log.i(TAG, "Using this target!");
5751 if (!screenWasOff || (flags &
5752 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5753 mMotionTarget = child;
5754 } else {
5755 //Log.i(TAG, "Waking, skip!");
5756 mMotionTarget = null;
5757 }
5758 break;
5759 }
Romain Guy06882f82009-06-10 13:36:04 -07005760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 if ((flags & WindowManager.LayoutParams
5762 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5763 child.mNextOutsideTouch = mOutsideTouchTargets;
5764 mOutsideTouchTargets = child;
5765 //Log.i(TAG, "Adding to outside target list: " + child);
5766 }
5767 }
5768
5769 // if there's an error window but it's not accepting
5770 // focus (typically because it is not yet visible) just
5771 // wait for it -- any other focused window may in fact
5772 // be in ANR state.
5773 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5774 mMotionTarget = null;
5775 }
5776 }
Romain Guy06882f82009-06-10 13:36:04 -07005777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005778 target = mMotionTarget;
5779 }
5780 }
Romain Guy06882f82009-06-10 13:36:04 -07005781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 // Pointer events are a little different -- if there isn't a
5785 // target found for any event, then just drop it.
5786 return target != null ? target : SKIP_TARGET_TOKEN;
5787 }
Romain Guy06882f82009-06-10 13:36:04 -07005788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789 boolean checkShouldDispatchKey(int keycode) {
5790 synchronized (this) {
5791 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5792 mTimeToSwitch = 0;
5793 return true;
5794 }
5795 if (mTimeToSwitch != 0
5796 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5797 return false;
5798 }
5799 return true;
5800 }
5801 }
Romain Guy06882f82009-06-10 13:36:04 -07005802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 void bindTargetWindowLocked(WindowState win,
5804 int pendingWhat, QueuedEvent pendingMotion) {
5805 synchronized (this) {
5806 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5807 }
5808 }
Romain Guy06882f82009-06-10 13:36:04 -07005809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 void bindTargetWindowLocked(WindowState win) {
5811 synchronized (this) {
5812 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5813 }
5814 }
5815
5816 void bindTargetWindowLockedLocked(WindowState win,
5817 int pendingWhat, QueuedEvent pendingMotion) {
5818 mLastWin = win;
5819 mLastBinder = win.mClient.asBinder();
5820 mFinished = false;
5821 if (pendingMotion != null) {
5822 final Session s = win.mSession;
5823 if (pendingWhat == RETURN_PENDING_POINTER) {
5824 releasePendingPointerLocked(s);
5825 s.mPendingPointerMove = pendingMotion;
5826 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005827 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 "bindTargetToWindow " + s.mPendingPointerMove);
5829 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5830 releasePendingTrackballLocked(s);
5831 s.mPendingTrackballMove = pendingMotion;
5832 s.mPendingTrackballWindow = win;
5833 }
5834 }
5835 }
Romain Guy06882f82009-06-10 13:36:04 -07005836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005837 void releasePendingPointerLocked(Session s) {
5838 if (DEBUG_INPUT) Log.v(TAG,
5839 "releasePendingPointer " + s.mPendingPointerMove);
5840 if (s.mPendingPointerMove != null) {
5841 mQueue.recycleEvent(s.mPendingPointerMove);
5842 s.mPendingPointerMove = null;
5843 }
5844 }
Romain Guy06882f82009-06-10 13:36:04 -07005845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846 void releasePendingTrackballLocked(Session s) {
5847 if (s.mPendingTrackballMove != null) {
5848 mQueue.recycleEvent(s.mPendingTrackballMove);
5849 s.mPendingTrackballMove = null;
5850 }
5851 }
Romain Guy06882f82009-06-10 13:36:04 -07005852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005853 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5854 int returnWhat) {
5855 if (DEBUG_INPUT) Log.v(
5856 TAG, "finishedKey: client=" + client + ", force=" + force);
5857
5858 if (client == null) {
5859 return null;
5860 }
5861
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005862 MotionEvent res = null;
5863 QueuedEvent qev = null;
5864 WindowState win = null;
5865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 synchronized (this) {
5867 if (DEBUG_INPUT) Log.v(
5868 TAG, "finishedKey: client=" + client.asBinder()
5869 + ", force=" + force + ", last=" + mLastBinder
5870 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005872 if (returnWhat == RETURN_PENDING_POINTER) {
5873 qev = session.mPendingPointerMove;
5874 win = session.mPendingPointerWindow;
5875 session.mPendingPointerMove = null;
5876 session.mPendingPointerWindow = null;
5877 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5878 qev = session.mPendingTrackballMove;
5879 win = session.mPendingTrackballWindow;
5880 session.mPendingTrackballMove = null;
5881 session.mPendingTrackballWindow = null;
5882 }
Romain Guy06882f82009-06-10 13:36:04 -07005883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 if (mLastBinder == client.asBinder()) {
5885 if (DEBUG_INPUT) Log.v(
5886 TAG, "finishedKey: last paused="
5887 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5888 if (mLastWin != null && (!mLastWin.mToken.paused || force
5889 || !mEventDispatching)) {
5890 doFinishedKeyLocked(false);
5891 } else {
5892 // Make sure to wake up anyone currently waiting to
5893 // dispatch a key, so they can re-evaluate their
5894 // current situation.
5895 mFinished = true;
5896 notifyAll();
5897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 }
Romain Guy06882f82009-06-10 13:36:04 -07005899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005901 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 if (DEBUG_INPUT) Log.v(TAG,
5903 "Returning pending motion: " + res);
5904 mQueue.recycleEvent(qev);
5905 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5906 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005910
5911 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5912 synchronized (mWindowMap) {
5913 if (mWallpaperTarget == win) {
5914 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5915 }
5916 }
5917 }
5918
5919 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005920 }
5921
5922 void tickle() {
5923 synchronized (this) {
5924 notifyAll();
5925 }
5926 }
Romain Guy06882f82009-06-10 13:36:04 -07005927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005928 void handleNewWindowLocked(WindowState newWindow) {
5929 if (!newWindow.canReceiveKeys()) {
5930 return;
5931 }
5932 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005933 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 TAG, "New key dispatch window: win="
5935 + newWindow.mClient.asBinder()
5936 + ", last=" + mLastBinder
5937 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5938 + "), finished=" + mFinished + ", paused="
5939 + newWindow.mToken.paused);
5940
5941 // Displaying a window implicitly causes dispatching to
5942 // be unpaused. (This is to protect against bugs if someone
5943 // pauses dispatching but forgets to resume.)
5944 newWindow.mToken.paused = false;
5945
5946 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005947
5948 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5949 if (DEBUG_INPUT) Log.v(TAG,
5950 "New SYSTEM_ERROR window; resetting state");
5951 mLastWin = null;
5952 mLastBinder = null;
5953 mMotionTarget = null;
5954 mFinished = true;
5955 } else if (mLastWin != null) {
5956 // If the new window is above the window we are
5957 // waiting on, then stop waiting and let key dispatching
5958 // start on the new guy.
5959 if (DEBUG_INPUT) Log.v(
5960 TAG, "Last win layer=" + mLastWin.mLayer
5961 + ", new win layer=" + newWindow.mLayer);
5962 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005963 // The new window is above the old; finish pending input to the last
5964 // window and start directing it to the new one.
5965 mLastWin.mToken.paused = false;
5966 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005968 // Either the new window is lower, so there is no need to wake key waiters,
5969 // or we just finished key input to the previous window, which implicitly
5970 // notified the key waiters. In both cases, we don't need to issue the
5971 // notification here.
5972 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 }
5974
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005975 // Now that we've put a new window state in place, make the event waiter
5976 // take notice and retarget its attentions.
5977 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 }
5979 }
5980
5981 void pauseDispatchingLocked(WindowToken token) {
5982 synchronized (this)
5983 {
5984 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5985 token.paused = true;
5986
5987 /*
5988 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5989 mPaused = true;
5990 } else {
5991 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005992 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005994 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005996 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005997 }
5998 }
5999 */
6000 }
6001 }
6002
6003 void resumeDispatchingLocked(WindowToken token) {
6004 synchronized (this) {
6005 if (token.paused) {
6006 if (DEBUG_INPUT) Log.v(
6007 TAG, "Resuming WindowToken " + token
6008 + ", last=" + mLastBinder
6009 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6010 + "), finished=" + mFinished + ", paused="
6011 + token.paused);
6012 token.paused = false;
6013 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
6014 doFinishedKeyLocked(true);
6015 } else {
6016 notifyAll();
6017 }
6018 }
6019 }
6020 }
6021
6022 void setEventDispatchingLocked(boolean enabled) {
6023 synchronized (this) {
6024 mEventDispatching = enabled;
6025 notifyAll();
6026 }
6027 }
Romain Guy06882f82009-06-10 13:36:04 -07006028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 void appSwitchComing() {
6030 synchronized (this) {
6031 // Don't wait for more than .5 seconds for app to finish
6032 // processing the pending events.
6033 long now = SystemClock.uptimeMillis() + 500;
6034 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6035 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6036 mTimeToSwitch = now;
6037 }
6038 notifyAll();
6039 }
6040 }
Romain Guy06882f82009-06-10 13:36:04 -07006041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006042 private final void doFinishedKeyLocked(boolean doRecycle) {
6043 if (mLastWin != null) {
6044 releasePendingPointerLocked(mLastWin.mSession);
6045 releasePendingTrackballLocked(mLastWin.mSession);
6046 }
Romain Guy06882f82009-06-10 13:36:04 -07006047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 if (mLastWin == null || !mLastWin.mToken.paused
6049 || !mLastWin.isVisibleLw()) {
6050 // If the current window has been paused, we aren't -really-
6051 // finished... so let the waiters still wait.
6052 mLastWin = null;
6053 mLastBinder = null;
6054 }
6055 mFinished = true;
6056 notifyAll();
6057 }
6058 }
6059
6060 private class KeyQ extends KeyInputQueue
6061 implements KeyInputQueue.FilterCallback {
6062 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006064 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006065 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6067 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6068 "KEEP_SCREEN_ON_FLAG");
6069 mHoldingScreen.setReferenceCounted(false);
6070 }
6071
6072 @Override
6073 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6074 if (mPolicy.preprocessInputEventTq(event)) {
6075 return true;
6076 }
Romain Guy06882f82009-06-10 13:36:04 -07006077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 switch (event.type) {
6079 case RawInputEvent.EV_KEY: {
6080 // XXX begin hack
6081 if (DEBUG) {
6082 if (event.keycode == KeyEvent.KEYCODE_G) {
6083 if (event.value != 0) {
6084 // G down
6085 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6086 }
6087 return false;
6088 }
6089 if (event.keycode == KeyEvent.KEYCODE_D) {
6090 if (event.value != 0) {
6091 //dump();
6092 }
6093 return false;
6094 }
6095 }
6096 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 boolean screenIsOff = !mPowerManager.screenIsOn();
6099 boolean screenIsDim = !mPowerManager.screenIsBright();
6100 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6103 mPowerManager.goToSleep(event.when);
6104 }
6105
6106 if (screenIsOff) {
6107 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6108 }
6109 if (screenIsDim) {
6110 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6111 }
6112 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6113 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006114 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006115 }
Romain Guy06882f82009-06-10 13:36:04 -07006116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6118 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6119 filterQueue(this);
6120 mKeyWaiter.appSwitchComing();
6121 }
6122 return true;
6123 } else {
6124 return false;
6125 }
6126 }
Romain Guy06882f82009-06-10 13:36:04 -07006127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006128 case RawInputEvent.EV_REL: {
6129 boolean screenIsOff = !mPowerManager.screenIsOn();
6130 boolean screenIsDim = !mPowerManager.screenIsBright();
6131 if (screenIsOff) {
6132 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6133 device.classes, event)) {
6134 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6135 return false;
6136 }
6137 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6138 }
6139 if (screenIsDim) {
6140 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6141 }
6142 return true;
6143 }
Romain Guy06882f82009-06-10 13:36:04 -07006144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 case RawInputEvent.EV_ABS: {
6146 boolean screenIsOff = !mPowerManager.screenIsOn();
6147 boolean screenIsDim = !mPowerManager.screenIsBright();
6148 if (screenIsOff) {
6149 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6150 device.classes, event)) {
6151 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6152 return false;
6153 }
6154 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6155 }
6156 if (screenIsDim) {
6157 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6158 }
6159 return true;
6160 }
Romain Guy06882f82009-06-10 13:36:04 -07006161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006162 default:
6163 return true;
6164 }
6165 }
6166
6167 public int filterEvent(QueuedEvent ev) {
6168 switch (ev.classType) {
6169 case RawInputEvent.CLASS_KEYBOARD:
6170 KeyEvent ke = (KeyEvent)ev.event;
6171 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6172 Log.w(TAG, "Dropping movement key during app switch: "
6173 + ke.getKeyCode() + ", action=" + ke.getAction());
6174 return FILTER_REMOVE;
6175 }
6176 return FILTER_ABORT;
6177 default:
6178 return FILTER_KEEP;
6179 }
6180 }
Romain Guy06882f82009-06-10 13:36:04 -07006181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006182 /**
6183 * Must be called with the main window manager lock held.
6184 */
6185 void setHoldScreenLocked(boolean holding) {
6186 boolean state = mHoldingScreen.isHeld();
6187 if (holding != state) {
6188 if (holding) {
6189 mHoldingScreen.acquire();
6190 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006191 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006192 mHoldingScreen.release();
6193 }
6194 }
6195 }
Michael Chan53071d62009-05-13 17:29:48 -07006196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197
6198 public boolean detectSafeMode() {
6199 mSafeMode = mPolicy.detectSafeMode();
6200 return mSafeMode;
6201 }
Romain Guy06882f82009-06-10 13:36:04 -07006202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 public void systemReady() {
6204 mPolicy.systemReady();
6205 }
Romain Guy06882f82009-06-10 13:36:04 -07006206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006207 private final class InputDispatcherThread extends Thread {
6208 // Time to wait when there is nothing to do: 9999 seconds.
6209 static final int LONG_WAIT=9999*1000;
6210
6211 public InputDispatcherThread() {
6212 super("InputDispatcher");
6213 }
Romain Guy06882f82009-06-10 13:36:04 -07006214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006215 @Override
6216 public void run() {
6217 while (true) {
6218 try {
6219 process();
6220 } catch (Exception e) {
6221 Log.e(TAG, "Exception in input dispatcher", e);
6222 }
6223 }
6224 }
Romain Guy06882f82009-06-10 13:36:04 -07006225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006226 private void process() {
6227 android.os.Process.setThreadPriority(
6228 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006230 // The last key event we saw
6231 KeyEvent lastKey = null;
6232
6233 // Last keydown time for auto-repeating keys
6234 long lastKeyTime = SystemClock.uptimeMillis();
6235 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006236 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237
Romain Guy06882f82009-06-10 13:36:04 -07006238 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 int keyRepeatCount = 0;
6240
6241 // Need to report that configuration has changed?
6242 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 while (true) {
6245 long curTime = SystemClock.uptimeMillis();
6246
6247 if (DEBUG_INPUT) Log.v(
6248 TAG, "Waiting for next key: now=" + curTime
6249 + ", repeat @ " + nextKeyTime);
6250
6251 // Retrieve next event, waiting only as long as the next
6252 // repeat timeout. If the configuration has changed, then
6253 // don't wait at all -- we'll report the change as soon as
6254 // we have processed all events.
6255 QueuedEvent ev = mQueue.getEvent(
6256 (int)((!configChanged && curTime < nextKeyTime)
6257 ? (nextKeyTime-curTime) : 0));
6258
6259 if (DEBUG_INPUT && ev != null) Log.v(
6260 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6261
Michael Chan53071d62009-05-13 17:29:48 -07006262 if (MEASURE_LATENCY) {
6263 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6264 }
6265
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006266 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6267 // cancel key repeat at the request of the policy.
6268 lastKey = null;
6269 downTime = 0;
6270 lastKeyTime = curTime;
6271 nextKeyTime = curTime + LONG_WAIT;
6272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006273 try {
6274 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006275 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006276 int eventType;
6277 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6278 eventType = eventType((MotionEvent)ev.event);
6279 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6280 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6281 eventType = LocalPowerManager.BUTTON_EVENT;
6282 } else {
6283 eventType = LocalPowerManager.OTHER_EVENT;
6284 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006285 try {
Michael Chan53071d62009-05-13 17:29:48 -07006286 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006287 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006288 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006289 mBatteryStats.noteInputEvent();
6290 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006291 } catch (RemoteException e) {
6292 // Ignore
6293 }
Michael Chane10de972009-05-18 11:24:50 -07006294
6295 if (eventType != TOUCH_EVENT
6296 && eventType != LONG_TOUCH_EVENT
6297 && eventType != CHEEK_EVENT) {
6298 mPowerManager.userActivity(curTime, false,
6299 eventType, false);
6300 } else if (mLastTouchEventType != eventType
6301 || (curTime - mLastUserActivityCallTime)
6302 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6303 mLastUserActivityCallTime = curTime;
6304 mLastTouchEventType = eventType;
6305 mPowerManager.userActivity(curTime, false,
6306 eventType, false);
6307 }
6308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309 switch (ev.classType) {
6310 case RawInputEvent.CLASS_KEYBOARD:
6311 KeyEvent ke = (KeyEvent)ev.event;
6312 if (ke.isDown()) {
6313 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006314 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 keyRepeatCount = 0;
6316 lastKeyTime = curTime;
6317 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006318 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 if (DEBUG_INPUT) Log.v(
6320 TAG, "Received key down: first repeat @ "
6321 + nextKeyTime);
6322 } else {
6323 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006324 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006325 // Arbitrary long timeout.
6326 lastKeyTime = curTime;
6327 nextKeyTime = curTime + LONG_WAIT;
6328 if (DEBUG_INPUT) Log.v(
6329 TAG, "Received key up: ignore repeat @ "
6330 + nextKeyTime);
6331 }
6332 dispatchKey((KeyEvent)ev.event, 0, 0);
6333 mQueue.recycleEvent(ev);
6334 break;
6335 case RawInputEvent.CLASS_TOUCHSCREEN:
6336 //Log.i(TAG, "Read next event " + ev);
6337 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6338 break;
6339 case RawInputEvent.CLASS_TRACKBALL:
6340 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6341 break;
6342 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6343 configChanged = true;
6344 break;
6345 default:
6346 mQueue.recycleEvent(ev);
6347 break;
6348 }
Romain Guy06882f82009-06-10 13:36:04 -07006349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 } else if (configChanged) {
6351 configChanged = false;
6352 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006354 } else if (lastKey != null) {
6355 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 // Timeout occurred while key was down. If it is at or
6358 // past the key repeat time, dispatch the repeat.
6359 if (DEBUG_INPUT) Log.v(
6360 TAG, "Key timeout: repeat=" + nextKeyTime
6361 + ", now=" + curTime);
6362 if (curTime < nextKeyTime) {
6363 continue;
6364 }
Romain Guy06882f82009-06-10 13:36:04 -07006365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 lastKeyTime = nextKeyTime;
6367 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6368 keyRepeatCount++;
6369 if (DEBUG_INPUT) Log.v(
6370 TAG, "Key repeat: count=" + keyRepeatCount
6371 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006372 KeyEvent newEvent;
6373 if (downTime != 0 && (downTime
6374 + ViewConfiguration.getLongPressTimeout())
6375 <= curTime) {
6376 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6377 curTime, keyRepeatCount,
6378 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6379 downTime = 0;
6380 } else {
6381 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6382 curTime, keyRepeatCount);
6383 }
6384 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006386 } else {
6387 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006389 lastKeyTime = curTime;
6390 nextKeyTime = curTime + LONG_WAIT;
6391 }
Romain Guy06882f82009-06-10 13:36:04 -07006392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006393 } catch (Exception e) {
6394 Log.e(TAG,
6395 "Input thread received uncaught exception: " + e, e);
6396 }
6397 }
6398 }
6399 }
6400
6401 // -------------------------------------------------------------
6402 // Client Session State
6403 // -------------------------------------------------------------
6404
6405 private final class Session extends IWindowSession.Stub
6406 implements IBinder.DeathRecipient {
6407 final IInputMethodClient mClient;
6408 final IInputContext mInputContext;
6409 final int mUid;
6410 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006411 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006412 SurfaceSession mSurfaceSession;
6413 int mNumWindow = 0;
6414 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 /**
6417 * Current pointer move event being dispatched to client window... must
6418 * hold key lock to access.
6419 */
6420 QueuedEvent mPendingPointerMove;
6421 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006423 /**
6424 * Current trackball move event being dispatched to client window... must
6425 * hold key lock to access.
6426 */
6427 QueuedEvent mPendingTrackballMove;
6428 WindowState mPendingTrackballWindow;
6429
6430 public Session(IInputMethodClient client, IInputContext inputContext) {
6431 mClient = client;
6432 mInputContext = inputContext;
6433 mUid = Binder.getCallingUid();
6434 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006435 StringBuilder sb = new StringBuilder();
6436 sb.append("Session{");
6437 sb.append(Integer.toHexString(System.identityHashCode(this)));
6438 sb.append(" uid ");
6439 sb.append(mUid);
6440 sb.append("}");
6441 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006443 synchronized (mWindowMap) {
6444 if (mInputMethodManager == null && mHaveInputMethods) {
6445 IBinder b = ServiceManager.getService(
6446 Context.INPUT_METHOD_SERVICE);
6447 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6448 }
6449 }
6450 long ident = Binder.clearCallingIdentity();
6451 try {
6452 // Note: it is safe to call in to the input method manager
6453 // here because we are not holding our lock.
6454 if (mInputMethodManager != null) {
6455 mInputMethodManager.addClient(client, inputContext,
6456 mUid, mPid);
6457 } else {
6458 client.setUsingInputMethod(false);
6459 }
6460 client.asBinder().linkToDeath(this, 0);
6461 } catch (RemoteException e) {
6462 // The caller has died, so we can just forget about this.
6463 try {
6464 if (mInputMethodManager != null) {
6465 mInputMethodManager.removeClient(client);
6466 }
6467 } catch (RemoteException ee) {
6468 }
6469 } finally {
6470 Binder.restoreCallingIdentity(ident);
6471 }
6472 }
Romain Guy06882f82009-06-10 13:36:04 -07006473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006474 @Override
6475 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6476 throws RemoteException {
6477 try {
6478 return super.onTransact(code, data, reply, flags);
6479 } catch (RuntimeException e) {
6480 // Log all 'real' exceptions thrown to the caller
6481 if (!(e instanceof SecurityException)) {
6482 Log.e(TAG, "Window Session Crash", e);
6483 }
6484 throw e;
6485 }
6486 }
6487
6488 public void binderDied() {
6489 // Note: it is safe to call in to the input method manager
6490 // here because we are not holding our lock.
6491 try {
6492 if (mInputMethodManager != null) {
6493 mInputMethodManager.removeClient(mClient);
6494 }
6495 } catch (RemoteException e) {
6496 }
6497 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006498 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 mClientDead = true;
6500 killSessionLocked();
6501 }
6502 }
6503
6504 public int add(IWindow window, WindowManager.LayoutParams attrs,
6505 int viewVisibility, Rect outContentInsets) {
6506 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6507 }
Romain Guy06882f82009-06-10 13:36:04 -07006508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 public void remove(IWindow window) {
6510 removeWindow(this, window);
6511 }
Romain Guy06882f82009-06-10 13:36:04 -07006512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006513 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6514 int requestedWidth, int requestedHeight, int viewFlags,
6515 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6516 Rect outVisibleInsets, Surface outSurface) {
6517 return relayoutWindow(this, window, attrs,
6518 requestedWidth, requestedHeight, viewFlags, insetsPending,
6519 outFrame, outContentInsets, outVisibleInsets, outSurface);
6520 }
Romain Guy06882f82009-06-10 13:36:04 -07006521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006522 public void setTransparentRegion(IWindow window, Region region) {
6523 setTransparentRegionWindow(this, window, region);
6524 }
Romain Guy06882f82009-06-10 13:36:04 -07006525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006526 public void setInsets(IWindow window, int touchableInsets,
6527 Rect contentInsets, Rect visibleInsets) {
6528 setInsetsWindow(this, window, touchableInsets, contentInsets,
6529 visibleInsets);
6530 }
Romain Guy06882f82009-06-10 13:36:04 -07006531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006532 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6533 getWindowDisplayFrame(this, window, outDisplayFrame);
6534 }
Romain Guy06882f82009-06-10 13:36:04 -07006535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006536 public void finishDrawing(IWindow window) {
6537 if (localLOGV) Log.v(
6538 TAG, "IWindow finishDrawing called for " + window);
6539 finishDrawingWindow(this, window);
6540 }
6541
6542 public void finishKey(IWindow window) {
6543 if (localLOGV) Log.v(
6544 TAG, "IWindow finishKey called for " + window);
6545 mKeyWaiter.finishedKey(this, window, false,
6546 KeyWaiter.RETURN_NOTHING);
6547 }
6548
6549 public MotionEvent getPendingPointerMove(IWindow window) {
6550 if (localLOGV) Log.v(
6551 TAG, "IWindow getPendingMotionEvent called for " + window);
6552 return mKeyWaiter.finishedKey(this, window, false,
6553 KeyWaiter.RETURN_PENDING_POINTER);
6554 }
Romain Guy06882f82009-06-10 13:36:04 -07006555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 public MotionEvent getPendingTrackballMove(IWindow window) {
6557 if (localLOGV) Log.v(
6558 TAG, "IWindow getPendingMotionEvent called for " + window);
6559 return mKeyWaiter.finishedKey(this, window, false,
6560 KeyWaiter.RETURN_PENDING_TRACKBALL);
6561 }
6562
6563 public void setInTouchMode(boolean mode) {
6564 synchronized(mWindowMap) {
6565 mInTouchMode = mode;
6566 }
6567 }
6568
6569 public boolean getInTouchMode() {
6570 synchronized(mWindowMap) {
6571 return mInTouchMode;
6572 }
6573 }
6574
6575 public boolean performHapticFeedback(IWindow window, int effectId,
6576 boolean always) {
6577 synchronized(mWindowMap) {
6578 long ident = Binder.clearCallingIdentity();
6579 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006580 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006581 windowForClientLocked(this, window), effectId, always);
6582 } finally {
6583 Binder.restoreCallingIdentity(ident);
6584 }
6585 }
6586 }
Romain Guy06882f82009-06-10 13:36:04 -07006587
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006588 public void setWallpaperPosition(IBinder window, float x, float y) {
6589 synchronized(mWindowMap) {
6590 long ident = Binder.clearCallingIdentity();
6591 try {
6592 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6593 x, y);
6594 } finally {
6595 Binder.restoreCallingIdentity(ident);
6596 }
6597 }
6598 }
6599
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006600 public void wallpaperOffsetsComplete(IBinder window) {
6601 WindowManagerService.this.wallpaperOffsetsComplete(window);
6602 }
6603
Dianne Hackborn75804932009-10-20 20:15:20 -07006604 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6605 int z, Bundle extras, boolean sync) {
6606 synchronized(mWindowMap) {
6607 long ident = Binder.clearCallingIdentity();
6608 try {
6609 return sendWindowWallpaperCommandLocked(
6610 windowForClientLocked(this, window),
6611 action, x, y, z, extras, sync);
6612 } finally {
6613 Binder.restoreCallingIdentity(ident);
6614 }
6615 }
6616 }
6617
6618 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6619 WindowManagerService.this.wallpaperCommandComplete(window, result);
6620 }
6621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006622 void windowAddedLocked() {
6623 if (mSurfaceSession == null) {
6624 if (localLOGV) Log.v(
6625 TAG, "First window added to " + this + ", creating SurfaceSession");
6626 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006627 if (SHOW_TRANSACTIONS) Log.i(
6628 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006629 mSessions.add(this);
6630 }
6631 mNumWindow++;
6632 }
6633
6634 void windowRemovedLocked() {
6635 mNumWindow--;
6636 killSessionLocked();
6637 }
Romain Guy06882f82009-06-10 13:36:04 -07006638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006639 void killSessionLocked() {
6640 if (mNumWindow <= 0 && mClientDead) {
6641 mSessions.remove(this);
6642 if (mSurfaceSession != null) {
6643 if (localLOGV) Log.v(
6644 TAG, "Last window removed from " + this
6645 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006646 if (SHOW_TRANSACTIONS) Log.i(
6647 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648 try {
6649 mSurfaceSession.kill();
6650 } catch (Exception e) {
6651 Log.w(TAG, "Exception thrown when killing surface session "
6652 + mSurfaceSession + " in session " + this
6653 + ": " + e.toString());
6654 }
6655 mSurfaceSession = null;
6656 }
6657 }
6658 }
Romain Guy06882f82009-06-10 13:36:04 -07006659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006660 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006661 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6662 pw.print(" mClientDead="); pw.print(mClientDead);
6663 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6664 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6665 pw.print(prefix);
6666 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6667 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6668 }
6669 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6670 pw.print(prefix);
6671 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6672 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006674 }
6675
6676 @Override
6677 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006678 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 }
6680 }
6681
6682 // -------------------------------------------------------------
6683 // Client Window State
6684 // -------------------------------------------------------------
6685
6686 private final class WindowState implements WindowManagerPolicy.WindowState {
6687 final Session mSession;
6688 final IWindow mClient;
6689 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006690 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006691 AppWindowToken mAppToken;
6692 AppWindowToken mTargetAppToken;
6693 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6694 final DeathRecipient mDeathRecipient;
6695 final WindowState mAttachedWindow;
6696 final ArrayList mChildWindows = new ArrayList();
6697 final int mBaseLayer;
6698 final int mSubLayer;
6699 final boolean mLayoutAttached;
6700 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006701 final boolean mIsWallpaper;
6702 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703 int mViewVisibility;
6704 boolean mPolicyVisibility = true;
6705 boolean mPolicyVisibilityAfterAnim = true;
6706 boolean mAppFreezing;
6707 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006708 boolean mReportDestroySurface;
6709 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006710 boolean mAttachedHidden; // is our parent window hidden?
6711 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006712 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006713 int mRequestedWidth;
6714 int mRequestedHeight;
6715 int mLastRequestedWidth;
6716 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006717 int mLayer;
6718 int mAnimLayer;
6719 int mLastLayer;
6720 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006721 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006722 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006723
6724 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006726 // Actual frame shown on-screen (may be modified by animation)
6727 final Rect mShownFrame = new Rect();
6728 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006730 /**
6731 * Insets that determine the actually visible area
6732 */
6733 final Rect mVisibleInsets = new Rect();
6734 final Rect mLastVisibleInsets = new Rect();
6735 boolean mVisibleInsetsChanged;
6736
6737 /**
6738 * Insets that are covered by system windows
6739 */
6740 final Rect mContentInsets = new Rect();
6741 final Rect mLastContentInsets = new Rect();
6742 boolean mContentInsetsChanged;
6743
6744 /**
6745 * Set to true if we are waiting for this window to receive its
6746 * given internal insets before laying out other windows based on it.
6747 */
6748 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006750 /**
6751 * These are the content insets that were given during layout for
6752 * this window, to be applied to windows behind it.
6753 */
6754 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006756 /**
6757 * These are the visible insets that were given during layout for
6758 * this window, to be applied to windows behind it.
6759 */
6760 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 /**
6763 * Flag indicating whether the touchable region should be adjusted by
6764 * the visible insets; if false the area outside the visible insets is
6765 * NOT touchable, so we must use those to adjust the frame during hit
6766 * tests.
6767 */
6768 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006770 // Current transformation being applied.
6771 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6772 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6773 float mHScale=1, mVScale=1;
6774 float mLastHScale=1, mLastVScale=1;
6775 final Matrix mTmpMatrix = new Matrix();
6776
6777 // "Real" frame that the application sees.
6778 final Rect mFrame = new Rect();
6779 final Rect mLastFrame = new Rect();
6780
6781 final Rect mContainingFrame = new Rect();
6782 final Rect mDisplayFrame = new Rect();
6783 final Rect mContentFrame = new Rect();
6784 final Rect mVisibleFrame = new Rect();
6785
6786 float mShownAlpha = 1;
6787 float mAlpha = 1;
6788 float mLastAlpha = 1;
6789
6790 // Set to true if, when the window gets displayed, it should perform
6791 // an enter animation.
6792 boolean mEnterAnimationPending;
6793
6794 // Currently running animation.
6795 boolean mAnimating;
6796 boolean mLocalAnimating;
6797 Animation mAnimation;
6798 boolean mAnimationIsEntrance;
6799 boolean mHasTransformation;
6800 boolean mHasLocalTransformation;
6801 final Transformation mTransformation = new Transformation();
6802
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006803 // If a window showing a wallpaper: the requested offset for the
6804 // wallpaper; if a wallpaper window: the currently applied offset.
6805 float mWallpaperX = -1;
6806 float mWallpaperY = -1;
6807
6808 // Wallpaper windows: pixels offset based on above variables.
6809 int mXOffset;
6810 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812 // This is set after IWindowSession.relayout() has been called at
6813 // least once for the window. It allows us to detect the situation
6814 // where we don't yet have a surface, but should have one soon, so
6815 // we can give the window focus before waiting for the relayout.
6816 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 // This is set after the Surface has been created but before the
6819 // window has been drawn. During this time the surface is hidden.
6820 boolean mDrawPending;
6821
6822 // This is set after the window has finished drawing for the first
6823 // time but before its surface is shown. The surface will be
6824 // displayed when the next layout is run.
6825 boolean mCommitDrawPending;
6826
6827 // This is set during the time after the window's drawing has been
6828 // committed, and before its surface is actually shown. It is used
6829 // to delay showing the surface until all windows in a token are ready
6830 // to be shown.
6831 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 // Set when the window has been shown in the screen the first time.
6834 boolean mHasDrawn;
6835
6836 // Currently running an exit animation?
6837 boolean mExiting;
6838
6839 // Currently on the mDestroySurface list?
6840 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 // Completely remove from window manager after exit animation?
6843 boolean mRemoveOnExit;
6844
6845 // Set when the orientation is changing and this window has not yet
6846 // been updated for the new orientation.
6847 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006849 // Is this window now (or just being) removed?
6850 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006852 WindowState(Session s, IWindow c, WindowToken token,
6853 WindowState attachedWindow, WindowManager.LayoutParams a,
6854 int viewVisibility) {
6855 mSession = s;
6856 mClient = c;
6857 mToken = token;
6858 mAttrs.copyFrom(a);
6859 mViewVisibility = viewVisibility;
6860 DeathRecipient deathRecipient = new DeathRecipient();
6861 mAlpha = a.alpha;
6862 if (localLOGV) Log.v(
6863 TAG, "Window " + this + " client=" + c.asBinder()
6864 + " token=" + token + " (" + mAttrs.token + ")");
6865 try {
6866 c.asBinder().linkToDeath(deathRecipient, 0);
6867 } catch (RemoteException e) {
6868 mDeathRecipient = null;
6869 mAttachedWindow = null;
6870 mLayoutAttached = false;
6871 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006872 mIsWallpaper = false;
6873 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 mBaseLayer = 0;
6875 mSubLayer = 0;
6876 return;
6877 }
6878 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6881 mAttrs.type <= LAST_SUB_WINDOW)) {
6882 // The multiplier here is to reserve space for multiple
6883 // windows in the same type layer.
6884 mBaseLayer = mPolicy.windowTypeToLayerLw(
6885 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6886 + TYPE_LAYER_OFFSET;
6887 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6888 mAttachedWindow = attachedWindow;
6889 mAttachedWindow.mChildWindows.add(this);
6890 mLayoutAttached = mAttrs.type !=
6891 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6892 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6893 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006894 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6895 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896 } else {
6897 // The multiplier here is to reserve space for multiple
6898 // windows in the same type layer.
6899 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6900 * TYPE_LAYER_MULTIPLIER
6901 + TYPE_LAYER_OFFSET;
6902 mSubLayer = 0;
6903 mAttachedWindow = null;
6904 mLayoutAttached = false;
6905 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6906 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006907 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6908 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006909 }
6910
6911 WindowState appWin = this;
6912 while (appWin.mAttachedWindow != null) {
6913 appWin = mAttachedWindow;
6914 }
6915 WindowToken appToken = appWin.mToken;
6916 while (appToken.appWindowToken == null) {
6917 WindowToken parent = mTokenMap.get(appToken.token);
6918 if (parent == null || appToken == parent) {
6919 break;
6920 }
6921 appToken = parent;
6922 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006923 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924 mAppToken = appToken.appWindowToken;
6925
6926 mSurface = null;
6927 mRequestedWidth = 0;
6928 mRequestedHeight = 0;
6929 mLastRequestedWidth = 0;
6930 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006931 mXOffset = 0;
6932 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 mLayer = 0;
6934 mAnimLayer = 0;
6935 mLastLayer = 0;
6936 }
6937
6938 void attach() {
6939 if (localLOGV) Log.v(
6940 TAG, "Attaching " + this + " token=" + mToken
6941 + ", list=" + mToken.windows);
6942 mSession.windowAddedLocked();
6943 }
6944
6945 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6946 mHaveFrame = true;
6947
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006948 final Rect container = mContainingFrame;
6949 container.set(pf);
6950
6951 final Rect display = mDisplayFrame;
6952 display.set(df);
6953
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006954 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006955 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006956 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6957 display.intersect(mCompatibleScreenFrame);
6958 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006959 }
6960
6961 final int pw = container.right - container.left;
6962 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006963
6964 int w,h;
6965 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6966 w = mAttrs.width < 0 ? pw : mAttrs.width;
6967 h = mAttrs.height< 0 ? ph : mAttrs.height;
6968 } else {
6969 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6970 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6971 }
Romain Guy06882f82009-06-10 13:36:04 -07006972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 final Rect content = mContentFrame;
6974 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 final Rect visible = mVisibleFrame;
6977 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006979 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006980 final int fw = frame.width();
6981 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6984 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6985
6986 Gravity.apply(mAttrs.gravity, w, h, container,
6987 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6988 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6989
6990 //System.out.println("Out: " + mFrame);
6991
6992 // Now make sure the window fits in the overall display.
6993 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 // Make sure the content and visible frames are inside of the
6996 // final window frame.
6997 if (content.left < frame.left) content.left = frame.left;
6998 if (content.top < frame.top) content.top = frame.top;
6999 if (content.right > frame.right) content.right = frame.right;
7000 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7001 if (visible.left < frame.left) visible.left = frame.left;
7002 if (visible.top < frame.top) visible.top = frame.top;
7003 if (visible.right > frame.right) visible.right = frame.right;
7004 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 final Rect contentInsets = mContentInsets;
7007 contentInsets.left = content.left-frame.left;
7008 contentInsets.top = content.top-frame.top;
7009 contentInsets.right = frame.right-content.right;
7010 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 final Rect visibleInsets = mVisibleInsets;
7013 visibleInsets.left = visible.left-frame.left;
7014 visibleInsets.top = visible.top-frame.top;
7015 visibleInsets.right = frame.right-visible.right;
7016 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007017
Dianne Hackborn284ac932009-08-28 10:34:25 -07007018 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7019 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007020 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007021 }
7022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007023 if (localLOGV) {
7024 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7025 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7026 Log.v(TAG, "Resolving (mRequestedWidth="
7027 + mRequestedWidth + ", mRequestedheight="
7028 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7029 + "): frame=" + mFrame.toShortString()
7030 + " ci=" + contentInsets.toShortString()
7031 + " vi=" + visibleInsets.toShortString());
7032 //}
7033 }
7034 }
Romain Guy06882f82009-06-10 13:36:04 -07007035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007036 public Rect getFrameLw() {
7037 return mFrame;
7038 }
7039
7040 public Rect getShownFrameLw() {
7041 return mShownFrame;
7042 }
7043
7044 public Rect getDisplayFrameLw() {
7045 return mDisplayFrame;
7046 }
7047
7048 public Rect getContentFrameLw() {
7049 return mContentFrame;
7050 }
7051
7052 public Rect getVisibleFrameLw() {
7053 return mVisibleFrame;
7054 }
7055
7056 public boolean getGivenInsetsPendingLw() {
7057 return mGivenInsetsPending;
7058 }
7059
7060 public Rect getGivenContentInsetsLw() {
7061 return mGivenContentInsets;
7062 }
Romain Guy06882f82009-06-10 13:36:04 -07007063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 public Rect getGivenVisibleInsetsLw() {
7065 return mGivenVisibleInsets;
7066 }
Romain Guy06882f82009-06-10 13:36:04 -07007067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068 public WindowManager.LayoutParams getAttrs() {
7069 return mAttrs;
7070 }
7071
7072 public int getSurfaceLayer() {
7073 return mLayer;
7074 }
Romain Guy06882f82009-06-10 13:36:04 -07007075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 public IApplicationToken getAppToken() {
7077 return mAppToken != null ? mAppToken.appToken : null;
7078 }
7079
7080 public boolean hasAppShownWindows() {
7081 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7082 }
7083
7084 public boolean hasAppStartingIcon() {
7085 return mAppToken != null ? (mAppToken.startingData != null) : false;
7086 }
7087
7088 public WindowManagerPolicy.WindowState getAppStartingWindow() {
7089 return mAppToken != null ? mAppToken.startingWindow : null;
7090 }
7091
7092 public void setAnimation(Animation anim) {
7093 if (localLOGV) Log.v(
7094 TAG, "Setting animation in " + this + ": " + anim);
7095 mAnimating = false;
7096 mLocalAnimating = false;
7097 mAnimation = anim;
7098 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7099 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7100 }
7101
7102 public void clearAnimation() {
7103 if (mAnimation != null) {
7104 mAnimating = true;
7105 mLocalAnimating = false;
7106 mAnimation = null;
7107 }
7108 }
Romain Guy06882f82009-06-10 13:36:04 -07007109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 Surface createSurfaceLocked() {
7111 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007112 mReportDestroySurface = false;
7113 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 mDrawPending = true;
7115 mCommitDrawPending = false;
7116 mReadyToShow = false;
7117 if (mAppToken != null) {
7118 mAppToken.allDrawn = false;
7119 }
7120
7121 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007122 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 flags |= Surface.PUSH_BUFFERS;
7124 }
7125
7126 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7127 flags |= Surface.SECURE;
7128 }
7129 if (DEBUG_VISIBILITY) Log.v(
7130 TAG, "Creating surface in session "
7131 + mSession.mSurfaceSession + " window " + this
7132 + " w=" + mFrame.width()
7133 + " h=" + mFrame.height() + " format="
7134 + mAttrs.format + " flags=" + flags);
7135
7136 int w = mFrame.width();
7137 int h = mFrame.height();
7138 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7139 // for a scaled surface, we always want the requested
7140 // size.
7141 w = mRequestedWidth;
7142 h = mRequestedHeight;
7143 }
7144
Romain Guy9825ec62009-10-01 00:58:09 -07007145 // Something is wrong and SurfaceFlinger will not like this,
7146 // try to revert to sane values
7147 if (w <= 0) w = 1;
7148 if (h <= 0) h = 1;
7149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007150 try {
7151 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007152 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007153 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007154 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7155 + mSurface + " IN SESSION "
7156 + mSession.mSurfaceSession
7157 + ": pid=" + mSession.mPid + " format="
7158 + mAttrs.format + " flags=0x"
7159 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007160 } catch (Surface.OutOfResourcesException e) {
7161 Log.w(TAG, "OutOfResourcesException creating surface");
7162 reclaimSomeSurfaceMemoryLocked(this, "create");
7163 return null;
7164 } catch (Exception e) {
7165 Log.e(TAG, "Exception creating surface", e);
7166 return null;
7167 }
Romain Guy06882f82009-06-10 13:36:04 -07007168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007169 if (localLOGV) Log.v(
7170 TAG, "Got surface: " + mSurface
7171 + ", set left=" + mFrame.left + " top=" + mFrame.top
7172 + ", animLayer=" + mAnimLayer);
7173 if (SHOW_TRANSACTIONS) {
7174 Log.i(TAG, ">>> OPEN TRANSACTION");
7175 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7176 + mAttrs.getTitle() + ") pos=(" +
7177 mFrame.left + "," + mFrame.top + ") (" +
7178 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7179 mAnimLayer + " HIDE");
7180 }
7181 Surface.openTransaction();
7182 try {
7183 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007184 mSurface.setPosition(mFrame.left + mXOffset,
7185 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007186 mSurface.setLayer(mAnimLayer);
7187 mSurface.hide();
7188 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007189 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7190 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 mSurface.setFlags(Surface.SURFACE_DITHER,
7192 Surface.SURFACE_DITHER);
7193 }
7194 } catch (RuntimeException e) {
7195 Log.w(TAG, "Error creating surface in " + w, e);
7196 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7197 }
7198 mLastHidden = true;
7199 } finally {
7200 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7201 Surface.closeTransaction();
7202 }
7203 if (localLOGV) Log.v(
7204 TAG, "Created surface " + this);
7205 }
7206 return mSurface;
7207 }
Romain Guy06882f82009-06-10 13:36:04 -07007208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 void destroySurfaceLocked() {
7210 // Window is no longer on-screen, so can no longer receive
7211 // key events... if we were waiting for it to finish
7212 // handling a key event, the wait is over!
7213 mKeyWaiter.finishedKey(mSession, mClient, true,
7214 KeyWaiter.RETURN_NOTHING);
7215 mKeyWaiter.releasePendingPointerLocked(mSession);
7216 mKeyWaiter.releasePendingTrackballLocked(mSession);
7217
7218 if (mAppToken != null && this == mAppToken.startingWindow) {
7219 mAppToken.startingDisplayed = false;
7220 }
Romain Guy06882f82009-06-10 13:36:04 -07007221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007222 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007223 mDrawPending = false;
7224 mCommitDrawPending = false;
7225 mReadyToShow = false;
7226
7227 int i = mChildWindows.size();
7228 while (i > 0) {
7229 i--;
7230 WindowState c = (WindowState)mChildWindows.get(i);
7231 c.mAttachedHidden = true;
7232 }
7233
7234 if (mReportDestroySurface) {
7235 mReportDestroySurface = false;
7236 mSurfacePendingDestroy = true;
7237 try {
7238 mClient.dispatchGetNewSurface();
7239 // We'll really destroy on the next time around.
7240 return;
7241 } catch (RemoteException e) {
7242 }
7243 }
7244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007245 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007246 if (DEBUG_VISIBILITY) {
7247 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007248 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007249 Log.w(TAG, "Window " + this + " destroying surface "
7250 + mSurface + ", session " + mSession, e);
7251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007252 if (SHOW_TRANSACTIONS) {
7253 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007254 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007255 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7256 + mAttrs.getTitle() + ")", ex);
7257 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007258 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007259 } catch (RuntimeException e) {
7260 Log.w(TAG, "Exception thrown when destroying Window " + this
7261 + " surface " + mSurface + " session " + mSession
7262 + ": " + e.toString());
7263 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007265 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007266 }
7267 }
7268
7269 boolean finishDrawingLocked() {
7270 if (mDrawPending) {
7271 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7272 TAG, "finishDrawingLocked: " + mSurface);
7273 mCommitDrawPending = true;
7274 mDrawPending = false;
7275 return true;
7276 }
7277 return false;
7278 }
7279
7280 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007281 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007282 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7283 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007284 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 }
7286 mCommitDrawPending = false;
7287 mReadyToShow = true;
7288 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7289 final AppWindowToken atoken = mAppToken;
7290 if (atoken == null || atoken.allDrawn || starting) {
7291 performShowLocked();
7292 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007293 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007294 }
7295
7296 // This must be called while inside a transaction.
7297 boolean performShowLocked() {
7298 if (DEBUG_VISIBILITY) {
7299 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007300 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007301 Log.v(TAG, "performShow on " + this
7302 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7303 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7304 }
7305 if (mReadyToShow && isReadyForDisplay()) {
7306 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7307 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7308 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7309 + " during animation: policyVis=" + mPolicyVisibility
7310 + " attHidden=" + mAttachedHidden
7311 + " tok.hiddenRequested="
7312 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007313 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007314 + (mAppToken != null ? mAppToken.hidden : false)
7315 + " animating=" + mAnimating
7316 + " tok animating="
7317 + (mAppToken != null ? mAppToken.animating : false));
7318 if (!showSurfaceRobustlyLocked(this)) {
7319 return false;
7320 }
7321 mLastAlpha = -1;
7322 mHasDrawn = true;
7323 mLastHidden = false;
7324 mReadyToShow = false;
7325 enableScreenIfNeededLocked();
7326
7327 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 int i = mChildWindows.size();
7330 while (i > 0) {
7331 i--;
7332 WindowState c = (WindowState)mChildWindows.get(i);
7333 if (c.mSurface != null && c.mAttachedHidden) {
7334 c.mAttachedHidden = false;
7335 c.performShowLocked();
7336 }
7337 }
Romain Guy06882f82009-06-10 13:36:04 -07007338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007339 if (mAttrs.type != TYPE_APPLICATION_STARTING
7340 && mAppToken != null) {
7341 mAppToken.firstWindowDrawn = true;
Dianne Hackborn248b1882009-09-16 16:46:44 -07007342
7343 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007344 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7345 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007346 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007347 // If this initial window is animating, stop it -- we
7348 // will do an animation to reveal it from behind the
7349 // starting window, so there is no need for it to also
7350 // be doing its own stuff.
7351 if (mAnimation != null) {
7352 mAnimation = null;
7353 // Make sure we clean up the animation.
7354 mAnimating = true;
7355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007356 mFinishedStarting.add(mAppToken);
7357 mH.sendEmptyMessage(H.FINISHED_STARTING);
7358 }
7359 mAppToken.updateReportedVisibilityLocked();
7360 }
7361 }
7362 return true;
7363 }
Romain Guy06882f82009-06-10 13:36:04 -07007364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365 // This must be called while inside a transaction. Returns true if
7366 // there is more animation to run.
7367 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7368 if (!mDisplayFrozen) {
7369 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007371 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7372 mHasTransformation = true;
7373 mHasLocalTransformation = true;
7374 if (!mLocalAnimating) {
7375 if (DEBUG_ANIM) Log.v(
7376 TAG, "Starting animation in " + this +
7377 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7378 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7379 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7380 mAnimation.setStartTime(currentTime);
7381 mLocalAnimating = true;
7382 mAnimating = true;
7383 }
7384 mTransformation.clear();
7385 final boolean more = mAnimation.getTransformation(
7386 currentTime, mTransformation);
7387 if (DEBUG_ANIM) Log.v(
7388 TAG, "Stepped animation in " + this +
7389 ": more=" + more + ", xform=" + mTransformation);
7390 if (more) {
7391 // we're not done!
7392 return true;
7393 }
7394 if (DEBUG_ANIM) Log.v(
7395 TAG, "Finished animation in " + this +
7396 " @ " + currentTime);
7397 mAnimation = null;
7398 //WindowManagerService.this.dump();
7399 }
7400 mHasLocalTransformation = false;
7401 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007402 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 // When our app token is animating, we kind-of pretend like
7404 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7405 // part of this check means that we will only do this if
7406 // our window is not currently exiting, or it is not
7407 // locally animating itself. The idea being that one that
7408 // is exiting and doing a local animation should be removed
7409 // once that animation is done.
7410 mAnimating = true;
7411 mHasTransformation = true;
7412 mTransformation.clear();
7413 return false;
7414 } else if (mHasTransformation) {
7415 // Little trick to get through the path below to act like
7416 // we have finished an animation.
7417 mAnimating = true;
7418 } else if (isAnimating()) {
7419 mAnimating = true;
7420 }
7421 } else if (mAnimation != null) {
7422 // If the display is frozen, and there is a pending animation,
7423 // clear it and make sure we run the cleanup code.
7424 mAnimating = true;
7425 mLocalAnimating = true;
7426 mAnimation = null;
7427 }
Romain Guy06882f82009-06-10 13:36:04 -07007428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007429 if (!mAnimating && !mLocalAnimating) {
7430 return false;
7431 }
7432
7433 if (DEBUG_ANIM) Log.v(
7434 TAG, "Animation done in " + this + ": exiting=" + mExiting
7435 + ", reportedVisible="
7436 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007438 mAnimating = false;
7439 mLocalAnimating = false;
7440 mAnimation = null;
7441 mAnimLayer = mLayer;
7442 if (mIsImWindow) {
7443 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007444 } else if (mIsWallpaper) {
7445 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007446 }
7447 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7448 + " anim layer: " + mAnimLayer);
7449 mHasTransformation = false;
7450 mHasLocalTransformation = false;
7451 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7452 mTransformation.clear();
7453 if (mHasDrawn
7454 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7455 && mAppToken != null
7456 && mAppToken.firstWindowDrawn
7457 && mAppToken.startingData != null) {
7458 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7459 + mToken + ": first real window done animating");
7460 mFinishedStarting.add(mAppToken);
7461 mH.sendEmptyMessage(H.FINISHED_STARTING);
7462 }
Romain Guy06882f82009-06-10 13:36:04 -07007463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007464 finishExit();
7465
7466 if (mAppToken != null) {
7467 mAppToken.updateReportedVisibilityLocked();
7468 }
7469
7470 return false;
7471 }
7472
7473 void finishExit() {
7474 if (DEBUG_ANIM) Log.v(
7475 TAG, "finishExit in " + this
7476 + ": exiting=" + mExiting
7477 + " remove=" + mRemoveOnExit
7478 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 final int N = mChildWindows.size();
7481 for (int i=0; i<N; i++) {
7482 ((WindowState)mChildWindows.get(i)).finishExit();
7483 }
Romain Guy06882f82009-06-10 13:36:04 -07007484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007485 if (!mExiting) {
7486 return;
7487 }
Romain Guy06882f82009-06-10 13:36:04 -07007488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007489 if (isWindowAnimating()) {
7490 return;
7491 }
7492
7493 if (localLOGV) Log.v(
7494 TAG, "Exit animation finished in " + this
7495 + ": remove=" + mRemoveOnExit);
7496 if (mSurface != null) {
7497 mDestroySurface.add(this);
7498 mDestroying = true;
7499 if (SHOW_TRANSACTIONS) Log.i(
7500 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7501 try {
7502 mSurface.hide();
7503 } catch (RuntimeException e) {
7504 Log.w(TAG, "Error hiding surface in " + this, e);
7505 }
7506 mLastHidden = true;
7507 mKeyWaiter.releasePendingPointerLocked(mSession);
7508 }
7509 mExiting = false;
7510 if (mRemoveOnExit) {
7511 mPendingRemove.add(this);
7512 mRemoveOnExit = false;
7513 }
7514 }
Romain Guy06882f82009-06-10 13:36:04 -07007515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007516 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7517 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7518 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7519 if (dtdx < -.000001f || dtdx > .000001f) return false;
7520 if (dsdy < -.000001f || dsdy > .000001f) return false;
7521 return true;
7522 }
Romain Guy06882f82009-06-10 13:36:04 -07007523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007524 void computeShownFrameLocked() {
7525 final boolean selfTransformation = mHasLocalTransformation;
7526 Transformation attachedTransformation =
7527 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7528 ? mAttachedWindow.mTransformation : null;
7529 Transformation appTransformation =
7530 (mAppToken != null && mAppToken.hasTransformation)
7531 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007532
7533 // Wallpapers are animated based on the "real" window they
7534 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007535 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007536 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007537 if (mWallpaperTarget.mHasLocalTransformation &&
7538 mWallpaperTarget.mAnimation != null &&
7539 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007540 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007541 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7542 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7543 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007544 }
7545 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007546 mWallpaperTarget.mAppToken.hasTransformation &&
7547 mWallpaperTarget.mAppToken.animation != null &&
7548 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007549 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007550 if (DEBUG_WALLPAPER && appTransformation != null) {
7551 Log.v(TAG, "WP target app xform: " + appTransformation);
7552 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007553 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007554 }
7555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 if (selfTransformation || attachedTransformation != null
7557 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007558 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007559 final Rect frame = mFrame;
7560 final float tmpFloats[] = mTmpFloats;
7561 final Matrix tmpMatrix = mTmpMatrix;
7562
7563 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007564 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007565 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007566 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007567 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007568 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007569 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007570 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 }
7572 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007573 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007574 }
7575
7576 // "convert" it into SurfaceFlinger's format
7577 // (a 2x2 matrix + an offset)
7578 // Here we must not transform the position of the surface
7579 // since it is already included in the transformation.
7580 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007582 tmpMatrix.getValues(tmpFloats);
7583 mDsDx = tmpFloats[Matrix.MSCALE_X];
7584 mDtDx = tmpFloats[Matrix.MSKEW_X];
7585 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7586 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007587 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7588 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007589 int w = frame.width();
7590 int h = frame.height();
7591 mShownFrame.set(x, y, x+w, y+h);
7592
7593 // Now set the alpha... but because our current hardware
7594 // can't do alpha transformation on a non-opaque surface,
7595 // turn it off if we are running an animation that is also
7596 // transforming since it is more important to have that
7597 // animation be smooth.
7598 mShownAlpha = mAlpha;
7599 if (!mLimitedAlphaCompositing
7600 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7601 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7602 && x == frame.left && y == frame.top))) {
7603 //Log.i(TAG, "Applying alpha transform");
7604 if (selfTransformation) {
7605 mShownAlpha *= mTransformation.getAlpha();
7606 }
7607 if (attachedTransformation != null) {
7608 mShownAlpha *= attachedTransformation.getAlpha();
7609 }
7610 if (appTransformation != null) {
7611 mShownAlpha *= appTransformation.getAlpha();
7612 }
7613 } else {
7614 //Log.i(TAG, "Not applying alpha transform");
7615 }
Romain Guy06882f82009-06-10 13:36:04 -07007616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007617 if (localLOGV) Log.v(
7618 TAG, "Continuing animation in " + this +
7619 ": " + mShownFrame +
7620 ", alpha=" + mTransformation.getAlpha());
7621 return;
7622 }
Romain Guy06882f82009-06-10 13:36:04 -07007623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007624 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007625 if (mXOffset != 0 || mYOffset != 0) {
7626 mShownFrame.offset(mXOffset, mYOffset);
7627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007628 mShownAlpha = mAlpha;
7629 mDsDx = 1;
7630 mDtDx = 0;
7631 mDsDy = 0;
7632 mDtDy = 1;
7633 }
Romain Guy06882f82009-06-10 13:36:04 -07007634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007635 /**
7636 * Is this window visible? It is not visible if there is no
7637 * surface, or we are in the process of running an exit animation
7638 * that will remove the surface, or its app token has been hidden.
7639 */
7640 public boolean isVisibleLw() {
7641 final AppWindowToken atoken = mAppToken;
7642 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7643 && (atoken == null || !atoken.hiddenRequested)
7644 && !mExiting && !mDestroying;
7645 }
7646
7647 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007648 * Like {@link #isVisibleLw}, but also counts a window that is currently
7649 * "hidden" behind the keyguard as visible. This allows us to apply
7650 * things like window flags that impact the keyguard.
7651 * XXX I am starting to think we need to have ANOTHER visibility flag
7652 * for this "hidden behind keyguard" state rather than overloading
7653 * mPolicyVisibility. Ungh.
7654 */
7655 public boolean isVisibleOrBehindKeyguardLw() {
7656 final AppWindowToken atoken = mAppToken;
7657 return mSurface != null && !mAttachedHidden
7658 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7659 && !mExiting && !mDestroying;
7660 }
7661
7662 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007663 * Is this window visible, ignoring its app token? It is not visible
7664 * if there is no surface, or we are in the process of running an exit animation
7665 * that will remove the surface.
7666 */
7667 public boolean isWinVisibleLw() {
7668 final AppWindowToken atoken = mAppToken;
7669 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7670 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7671 && !mExiting && !mDestroying;
7672 }
7673
7674 /**
7675 * The same as isVisible(), but follows the current hidden state of
7676 * the associated app token, not the pending requested hidden state.
7677 */
7678 boolean isVisibleNow() {
7679 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007680 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007681 }
7682
7683 /**
7684 * Same as isVisible(), but we also count it as visible between the
7685 * call to IWindowSession.add() and the first relayout().
7686 */
7687 boolean isVisibleOrAdding() {
7688 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007689 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007690 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7691 && mPolicyVisibility && !mAttachedHidden
7692 && (atoken == null || !atoken.hiddenRequested)
7693 && !mExiting && !mDestroying;
7694 }
7695
7696 /**
7697 * Is this window currently on-screen? It is on-screen either if it
7698 * is visible or it is currently running an animation before no longer
7699 * being visible.
7700 */
7701 boolean isOnScreen() {
7702 final AppWindowToken atoken = mAppToken;
7703 if (atoken != null) {
7704 return mSurface != null && mPolicyVisibility && !mDestroying
7705 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007706 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007707 } else {
7708 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007709 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007710 }
7711 }
Romain Guy06882f82009-06-10 13:36:04 -07007712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 /**
7714 * Like isOnScreen(), but we don't return true if the window is part
7715 * of a transition that has not yet been started.
7716 */
7717 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007718 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007719 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007720 return false;
7721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007723 final boolean animating = atoken != null
7724 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007726 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7727 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007728 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007729 }
7730
7731 /** Is the window or its container currently animating? */
7732 boolean isAnimating() {
7733 final WindowState attached = mAttachedWindow;
7734 final AppWindowToken atoken = mAppToken;
7735 return mAnimation != null
7736 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007737 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007738 (atoken.animation != null
7739 || atoken.inPendingTransaction));
7740 }
7741
7742 /** Is this window currently animating? */
7743 boolean isWindowAnimating() {
7744 return mAnimation != null;
7745 }
7746
7747 /**
7748 * Like isOnScreen, but returns false if the surface hasn't yet
7749 * been drawn.
7750 */
7751 public boolean isDisplayedLw() {
7752 final AppWindowToken atoken = mAppToken;
7753 return mSurface != null && mPolicyVisibility && !mDestroying
7754 && !mDrawPending && !mCommitDrawPending
7755 && ((!mAttachedHidden &&
7756 (atoken == null || !atoken.hiddenRequested))
7757 || mAnimating);
7758 }
7759
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007760 /**
7761 * Returns true if the window has a surface that it has drawn a
7762 * complete UI in to.
7763 */
7764 public boolean isDrawnLw() {
7765 final AppWindowToken atoken = mAppToken;
7766 return mSurface != null && !mDestroying
7767 && !mDrawPending && !mCommitDrawPending;
7768 }
7769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7771 boolean shownFrame, boolean onlyOpaque) {
7772 if (mSurface == null) {
7773 return false;
7774 }
7775 if (mAppToken != null && !mAppToken.appFullscreen) {
7776 return false;
7777 }
7778 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7779 return false;
7780 }
7781 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007782
7783 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7784 return frame.left <= mCompatibleScreenFrame.left &&
7785 frame.top <= mCompatibleScreenFrame.top &&
7786 frame.right >= mCompatibleScreenFrame.right &&
7787 frame.bottom >= mCompatibleScreenFrame.bottom;
7788 } else {
7789 return frame.left <= 0 && frame.top <= 0
7790 && frame.right >= screenWidth
7791 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 }
Romain Guy06882f82009-06-10 13:36:04 -07007794
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007795 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007796 * Return true if the window is opaque and fully drawn. This indicates
7797 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007798 */
7799 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007800 return (mAttrs.format == PixelFormat.OPAQUE
7801 || mAttrs.type == TYPE_WALLPAPER)
7802 && mSurface != null && mAnimation == null
7803 && (mAppToken == null || mAppToken.animation == null)
7804 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007805 }
7806
7807 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7808 return
7809 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007810 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7811 // only if it's visible
7812 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007813 // and only if the application fills the compatible screen
7814 mFrame.left <= mCompatibleScreenFrame.left &&
7815 mFrame.top <= mCompatibleScreenFrame.top &&
7816 mFrame.right >= mCompatibleScreenFrame.right &&
7817 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007818 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007819 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007820 }
7821
7822 boolean isFullscreen(int screenWidth, int screenHeight) {
7823 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007824 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007825 }
7826
7827 void removeLocked() {
7828 if (mAttachedWindow != null) {
7829 mAttachedWindow.mChildWindows.remove(this);
7830 }
7831 destroySurfaceLocked();
7832 mSession.windowRemovedLocked();
7833 try {
7834 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7835 } catch (RuntimeException e) {
7836 // Ignore if it has already been removed (usually because
7837 // we are doing this as part of processing a death note.)
7838 }
7839 }
7840
7841 private class DeathRecipient implements IBinder.DeathRecipient {
7842 public void binderDied() {
7843 try {
7844 synchronized(mWindowMap) {
7845 WindowState win = windowForClientLocked(mSession, mClient);
7846 Log.i(TAG, "WIN DEATH: " + win);
7847 if (win != null) {
7848 removeWindowLocked(mSession, win);
7849 }
7850 }
7851 } catch (IllegalArgumentException ex) {
7852 // This will happen if the window has already been
7853 // removed.
7854 }
7855 }
7856 }
7857
7858 /** Returns true if this window desires key events. */
7859 public final boolean canReceiveKeys() {
7860 return isVisibleOrAdding()
7861 && (mViewVisibility == View.VISIBLE)
7862 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7863 }
7864
7865 public boolean hasDrawnLw() {
7866 return mHasDrawn;
7867 }
7868
7869 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007870 return showLw(doAnimation, true);
7871 }
7872
7873 boolean showLw(boolean doAnimation, boolean requestAnim) {
7874 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
7875 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007876 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007877 mPolicyVisibility = true;
7878 mPolicyVisibilityAfterAnim = true;
7879 if (doAnimation) {
7880 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7881 }
7882 if (requestAnim) {
7883 requestAnimationLocked(0);
7884 }
7885 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007886 }
7887
7888 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007889 return hideLw(doAnimation, true);
7890 }
7891
7892 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007893 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7894 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007895 if (!current) {
7896 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007898 if (doAnimation) {
7899 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7900 if (mAnimation == null) {
7901 doAnimation = false;
7902 }
7903 }
7904 if (doAnimation) {
7905 mPolicyVisibilityAfterAnim = false;
7906 } else {
7907 mPolicyVisibilityAfterAnim = false;
7908 mPolicyVisibility = false;
7909 }
7910 if (requestAnim) {
7911 requestAnimationLocked(0);
7912 }
7913 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007914 }
7915
7916 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007917 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007918
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007919 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7920 pw.print(" mClient="); pw.println(mClient.asBinder());
7921 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7922 if (mAttachedWindow != null || mLayoutAttached) {
7923 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7924 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7925 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007926 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7927 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7928 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007929 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7930 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007931 }
7932 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7933 pw.print(" mSubLayer="); pw.print(mSubLayer);
7934 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7935 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7936 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7937 pw.print("="); pw.print(mAnimLayer);
7938 pw.print(" mLastLayer="); pw.println(mLastLayer);
7939 if (mSurface != null) {
7940 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7941 }
7942 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7943 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7944 if (mAppToken != null) {
7945 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7946 }
7947 if (mTargetAppToken != null) {
7948 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7949 }
7950 pw.print(prefix); pw.print("mViewVisibility=0x");
7951 pw.print(Integer.toHexString(mViewVisibility));
7952 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007953 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7954 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007955 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7956 pw.print(prefix); pw.print("mPolicyVisibility=");
7957 pw.print(mPolicyVisibility);
7958 pw.print(" mPolicyVisibilityAfterAnim=");
7959 pw.print(mPolicyVisibilityAfterAnim);
7960 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7961 }
7962 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007963 pw.print(" h="); pw.println(mRequestedHeight);
7964 if (mXOffset != 0 || mYOffset != 0) {
7965 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7966 pw.print(" y="); pw.println(mYOffset);
7967 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007968 pw.print(prefix); pw.print("mGivenContentInsets=");
7969 mGivenContentInsets.printShortString(pw);
7970 pw.print(" mGivenVisibleInsets=");
7971 mGivenVisibleInsets.printShortString(pw);
7972 pw.println();
7973 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7974 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7975 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7976 }
7977 pw.print(prefix); pw.print("mShownFrame=");
7978 mShownFrame.printShortString(pw);
7979 pw.print(" last="); mLastShownFrame.printShortString(pw);
7980 pw.println();
7981 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7982 pw.print(" last="); mLastFrame.printShortString(pw);
7983 pw.println();
7984 pw.print(prefix); pw.print("mContainingFrame=");
7985 mContainingFrame.printShortString(pw);
7986 pw.print(" mDisplayFrame=");
7987 mDisplayFrame.printShortString(pw);
7988 pw.println();
7989 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7990 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7991 pw.println();
7992 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7993 pw.print(" last="); mLastContentInsets.printShortString(pw);
7994 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7995 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7996 pw.println();
7997 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7998 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7999 pw.print(" mAlpha="); pw.print(mAlpha);
8000 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8001 }
8002 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8003 || mAnimation != null) {
8004 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8005 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8006 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8007 pw.print(" mAnimation="); pw.println(mAnimation);
8008 }
8009 if (mHasTransformation || mHasLocalTransformation) {
8010 pw.print(prefix); pw.print("XForm: has=");
8011 pw.print(mHasTransformation);
8012 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8013 pw.print(" "); mTransformation.printShortString(pw);
8014 pw.println();
8015 }
8016 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8017 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8018 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8019 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8020 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8021 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8022 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8023 pw.print(" mDestroying="); pw.print(mDestroying);
8024 pw.print(" mRemoved="); pw.println(mRemoved);
8025 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008026 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008027 pw.print(prefix); pw.print("mOrientationChanging=");
8028 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008029 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8030 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008031 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008032 if (mHScale != 1 || mVScale != 1) {
8033 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8034 pw.print(" mVScale="); pw.println(mVScale);
8035 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008036 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008037 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8038 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008040 }
8041
8042 @Override
8043 public String toString() {
8044 return "Window{"
8045 + Integer.toHexString(System.identityHashCode(this))
8046 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8047 }
8048 }
Romain Guy06882f82009-06-10 13:36:04 -07008049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008050 // -------------------------------------------------------------
8051 // Window Token State
8052 // -------------------------------------------------------------
8053
8054 class WindowToken {
8055 // The actual token.
8056 final IBinder token;
8057
8058 // The type of window this token is for, as per WindowManager.LayoutParams.
8059 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008061 // Set if this token was explicitly added by a client, so should
8062 // not be removed when all windows are removed.
8063 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008064
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008065 // For printing.
8066 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008068 // If this is an AppWindowToken, this is non-null.
8069 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008071 // All of the windows associated with this token.
8072 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8073
8074 // Is key dispatching paused for this token?
8075 boolean paused = false;
8076
8077 // Should this token's windows be hidden?
8078 boolean hidden;
8079
8080 // Temporary for finding which tokens no longer have visible windows.
8081 boolean hasVisible;
8082
Dianne Hackborna8f60182009-09-01 19:01:50 -07008083 // Set to true when this token is in a pending transaction where it
8084 // will be shown.
8085 boolean waitingToShow;
8086
8087 // Set to true when this token is in a pending transaction where it
8088 // will be hidden.
8089 boolean waitingToHide;
8090
8091 // Set to true when this token is in a pending transaction where its
8092 // windows will be put to the bottom of the list.
8093 boolean sendingToBottom;
8094
8095 // Set to true when this token is in a pending transaction where its
8096 // windows will be put to the top of the list.
8097 boolean sendingToTop;
8098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 WindowToken(IBinder _token, int type, boolean _explicit) {
8100 token = _token;
8101 windowType = type;
8102 explicit = _explicit;
8103 }
8104
8105 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008106 pw.print(prefix); pw.print("token="); pw.println(token);
8107 pw.print(prefix); pw.print("windows="); pw.println(windows);
8108 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8109 pw.print(" hidden="); pw.print(hidden);
8110 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008111 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8112 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8113 pw.print(" waitingToHide="); pw.print(waitingToHide);
8114 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8115 pw.print(" sendingToTop="); pw.println(sendingToTop);
8116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 }
8118
8119 @Override
8120 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008121 if (stringName == null) {
8122 StringBuilder sb = new StringBuilder();
8123 sb.append("WindowToken{");
8124 sb.append(Integer.toHexString(System.identityHashCode(this)));
8125 sb.append(" token="); sb.append(token); sb.append('}');
8126 stringName = sb.toString();
8127 }
8128 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 }
8130 };
8131
8132 class AppWindowToken extends WindowToken {
8133 // Non-null only for application tokens.
8134 final IApplicationToken appToken;
8135
8136 // All of the windows and child windows that are included in this
8137 // application token. Note this list is NOT sorted!
8138 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8139
8140 int groupId = -1;
8141 boolean appFullscreen;
8142 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 // These are used for determining when all windows associated with
8145 // an activity have been drawn, so they can be made visible together
8146 // at the same time.
8147 int lastTransactionSequence = mTransactionSequence-1;
8148 int numInterestingWindows;
8149 int numDrawnWindows;
8150 boolean inPendingTransaction;
8151 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008153 // Is this token going to be hidden in a little while? If so, it
8154 // won't be taken into account for setting the screen orientation.
8155 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008157 // Is this window's surface needed? This is almost like hidden, except
8158 // it will sometimes be true a little earlier: when the token has
8159 // been shown, but is still waiting for its app transition to execute
8160 // before making its windows shown.
8161 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008163 // Have we told the window clients to hide themselves?
8164 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008166 // Last visibility state we reported to the app token.
8167 boolean reportedVisible;
8168
8169 // Set to true when the token has been removed from the window mgr.
8170 boolean removed;
8171
8172 // Have we been asked to have this token keep the screen frozen?
8173 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175 boolean animating;
8176 Animation animation;
8177 boolean hasTransformation;
8178 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008180 // Offset to the window of all layers in the token, for use by
8181 // AppWindowToken animations.
8182 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008184 // Information about an application starting window if displayed.
8185 StartingData startingData;
8186 WindowState startingWindow;
8187 View startingView;
8188 boolean startingDisplayed;
8189 boolean startingMoved;
8190 boolean firstWindowDrawn;
8191
8192 AppWindowToken(IApplicationToken _token) {
8193 super(_token.asBinder(),
8194 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8195 appWindowToken = this;
8196 appToken = _token;
8197 }
Romain Guy06882f82009-06-10 13:36:04 -07008198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008199 public void setAnimation(Animation anim) {
8200 if (localLOGV) Log.v(
8201 TAG, "Setting animation in " + this + ": " + anim);
8202 animation = anim;
8203 animating = false;
8204 anim.restrictDuration(MAX_ANIMATION_DURATION);
8205 anim.scaleCurrentDuration(mTransitionAnimationScale);
8206 int zorder = anim.getZAdjustment();
8207 int adj = 0;
8208 if (zorder == Animation.ZORDER_TOP) {
8209 adj = TYPE_LAYER_OFFSET;
8210 } else if (zorder == Animation.ZORDER_BOTTOM) {
8211 adj = -TYPE_LAYER_OFFSET;
8212 }
Romain Guy06882f82009-06-10 13:36:04 -07008213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214 if (animLayerAdjustment != adj) {
8215 animLayerAdjustment = adj;
8216 updateLayers();
8217 }
8218 }
Romain Guy06882f82009-06-10 13:36:04 -07008219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008220 public void setDummyAnimation() {
8221 if (animation == null) {
8222 if (localLOGV) Log.v(
8223 TAG, "Setting dummy animation in " + this);
8224 animation = sDummyAnimation;
8225 }
8226 }
8227
8228 public void clearAnimation() {
8229 if (animation != null) {
8230 animation = null;
8231 animating = true;
8232 }
8233 }
Romain Guy06882f82009-06-10 13:36:04 -07008234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 void updateLayers() {
8236 final int N = allAppWindows.size();
8237 final int adj = animLayerAdjustment;
8238 for (int i=0; i<N; i++) {
8239 WindowState w = allAppWindows.get(i);
8240 w.mAnimLayer = w.mLayer + adj;
8241 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8242 + w.mAnimLayer);
8243 if (w == mInputMethodTarget) {
8244 setInputMethodAnimLayerAdjustment(adj);
8245 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008246 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008247 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008249 }
8250 }
Romain Guy06882f82009-06-10 13:36:04 -07008251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008252 void sendAppVisibilityToClients() {
8253 final int N = allAppWindows.size();
8254 for (int i=0; i<N; i++) {
8255 WindowState win = allAppWindows.get(i);
8256 if (win == startingWindow && clientHidden) {
8257 // Don't hide the starting window.
8258 continue;
8259 }
8260 try {
8261 if (DEBUG_VISIBILITY) Log.v(TAG,
8262 "Setting visibility of " + win + ": " + (!clientHidden));
8263 win.mClient.dispatchAppVisibility(!clientHidden);
8264 } catch (RemoteException e) {
8265 }
8266 }
8267 }
Romain Guy06882f82009-06-10 13:36:04 -07008268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008269 void showAllWindowsLocked() {
8270 final int NW = allAppWindows.size();
8271 for (int i=0; i<NW; i++) {
8272 WindowState w = allAppWindows.get(i);
8273 if (DEBUG_VISIBILITY) Log.v(TAG,
8274 "performing show on: " + w);
8275 w.performShowLocked();
8276 }
8277 }
Romain Guy06882f82009-06-10 13:36:04 -07008278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008279 // This must be called while inside a transaction.
8280 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8281 if (!mDisplayFrozen) {
8282 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008284 if (animation == sDummyAnimation) {
8285 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008286 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008287 // when it is really time to animate, this will be set to
8288 // a real animation and the next call will execute normally.
8289 return false;
8290 }
Romain Guy06882f82009-06-10 13:36:04 -07008291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8293 if (!animating) {
8294 if (DEBUG_ANIM) Log.v(
8295 TAG, "Starting animation in " + this +
8296 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8297 + " scale=" + mTransitionAnimationScale
8298 + " allDrawn=" + allDrawn + " animating=" + animating);
8299 animation.initialize(dw, dh, dw, dh);
8300 animation.setStartTime(currentTime);
8301 animating = true;
8302 }
8303 transformation.clear();
8304 final boolean more = animation.getTransformation(
8305 currentTime, transformation);
8306 if (DEBUG_ANIM) Log.v(
8307 TAG, "Stepped animation in " + this +
8308 ": more=" + more + ", xform=" + transformation);
8309 if (more) {
8310 // we're done!
8311 hasTransformation = true;
8312 return true;
8313 }
8314 if (DEBUG_ANIM) Log.v(
8315 TAG, "Finished animation in " + this +
8316 " @ " + currentTime);
8317 animation = null;
8318 }
8319 } else if (animation != null) {
8320 // If the display is frozen, and there is a pending animation,
8321 // clear it and make sure we run the cleanup code.
8322 animating = true;
8323 animation = null;
8324 }
8325
8326 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 if (!animating) {
8329 return false;
8330 }
8331
8332 clearAnimation();
8333 animating = false;
8334 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8335 moveInputMethodWindowsIfNeededLocked(true);
8336 }
Romain Guy06882f82009-06-10 13:36:04 -07008337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008338 if (DEBUG_ANIM) Log.v(
8339 TAG, "Animation done in " + this
8340 + ": reportedVisible=" + reportedVisible);
8341
8342 transformation.clear();
8343 if (animLayerAdjustment != 0) {
8344 animLayerAdjustment = 0;
8345 updateLayers();
8346 }
Romain Guy06882f82009-06-10 13:36:04 -07008347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348 final int N = windows.size();
8349 for (int i=0; i<N; i++) {
8350 ((WindowState)windows.get(i)).finishExit();
8351 }
8352 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 return false;
8355 }
8356
8357 void updateReportedVisibilityLocked() {
8358 if (appToken == null) {
8359 return;
8360 }
Romain Guy06882f82009-06-10 13:36:04 -07008361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008362 int numInteresting = 0;
8363 int numVisible = 0;
8364 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008366 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8367 final int N = allAppWindows.size();
8368 for (int i=0; i<N; i++) {
8369 WindowState win = allAppWindows.get(i);
8370 if (win == startingWindow || win.mAppFreezing) {
8371 continue;
8372 }
8373 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008374 Log.v(TAG, "Win " + win + ": isDrawn="
8375 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008377 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 Log.v(TAG, "Not displayed: s=" + win.mSurface
8379 + " pv=" + win.mPolicyVisibility
8380 + " dp=" + win.mDrawPending
8381 + " cdp=" + win.mCommitDrawPending
8382 + " ah=" + win.mAttachedHidden
8383 + " th="
8384 + (win.mAppToken != null
8385 ? win.mAppToken.hiddenRequested : false)
8386 + " a=" + win.mAnimating);
8387 }
8388 }
8389 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008390 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008391 if (!win.isAnimating()) {
8392 numVisible++;
8393 }
8394 nowGone = false;
8395 } else if (win.isAnimating()) {
8396 nowGone = false;
8397 }
8398 }
Romain Guy06882f82009-06-10 13:36:04 -07008399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008400 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8401 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8402 + numInteresting + " visible=" + numVisible);
8403 if (nowVisible != reportedVisible) {
8404 if (DEBUG_VISIBILITY) Log.v(
8405 TAG, "Visibility changed in " + this
8406 + ": vis=" + nowVisible);
8407 reportedVisible = nowVisible;
8408 Message m = mH.obtainMessage(
8409 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8410 nowVisible ? 1 : 0,
8411 nowGone ? 1 : 0,
8412 this);
8413 mH.sendMessage(m);
8414 }
8415 }
Romain Guy06882f82009-06-10 13:36:04 -07008416
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008417 WindowState findMainWindow() {
8418 int j = windows.size();
8419 while (j > 0) {
8420 j--;
8421 WindowState win = windows.get(j);
8422 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8423 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8424 return win;
8425 }
8426 }
8427 return null;
8428 }
8429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 void dump(PrintWriter pw, String prefix) {
8431 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008432 if (appToken != null) {
8433 pw.print(prefix); pw.println("app=true");
8434 }
8435 if (allAppWindows.size() > 0) {
8436 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8437 }
8438 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008439 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008440 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8441 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8442 pw.print(" clientHidden="); pw.print(clientHidden);
8443 pw.print(" willBeHidden="); pw.print(willBeHidden);
8444 pw.print(" reportedVisible="); pw.println(reportedVisible);
8445 if (paused || freezingScreen) {
8446 pw.print(prefix); pw.print("paused="); pw.print(paused);
8447 pw.print(" freezingScreen="); pw.println(freezingScreen);
8448 }
8449 if (numInterestingWindows != 0 || numDrawnWindows != 0
8450 || inPendingTransaction || allDrawn) {
8451 pw.print(prefix); pw.print("numInterestingWindows=");
8452 pw.print(numInterestingWindows);
8453 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8454 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8455 pw.print(" allDrawn="); pw.println(allDrawn);
8456 }
8457 if (animating || animation != null) {
8458 pw.print(prefix); pw.print("animating="); pw.print(animating);
8459 pw.print(" animation="); pw.println(animation);
8460 }
8461 if (animLayerAdjustment != 0) {
8462 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8463 }
8464 if (hasTransformation) {
8465 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8466 pw.print(" transformation="); transformation.printShortString(pw);
8467 pw.println();
8468 }
8469 if (startingData != null || removed || firstWindowDrawn) {
8470 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8471 pw.print(" removed="); pw.print(removed);
8472 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8473 }
8474 if (startingWindow != null || startingView != null
8475 || startingDisplayed || startingMoved) {
8476 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8477 pw.print(" startingView="); pw.print(startingView);
8478 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8479 pw.print(" startingMoved"); pw.println(startingMoved);
8480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008481 }
8482
8483 @Override
8484 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008485 if (stringName == null) {
8486 StringBuilder sb = new StringBuilder();
8487 sb.append("AppWindowToken{");
8488 sb.append(Integer.toHexString(System.identityHashCode(this)));
8489 sb.append(" token="); sb.append(token); sb.append('}');
8490 stringName = sb.toString();
8491 }
8492 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008493 }
8494 }
Romain Guy06882f82009-06-10 13:36:04 -07008495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 // -------------------------------------------------------------
8497 // DummyAnimation
8498 // -------------------------------------------------------------
8499
8500 // This is an animation that does nothing: it just immediately finishes
8501 // itself every time it is called. It is used as a stub animation in cases
8502 // where we want to synchronize multiple things that may be animating.
8503 static final class DummyAnimation extends Animation {
8504 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8505 return false;
8506 }
8507 }
8508 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008510 // -------------------------------------------------------------
8511 // Async Handler
8512 // -------------------------------------------------------------
8513
8514 static final class StartingData {
8515 final String pkg;
8516 final int theme;
8517 final CharSequence nonLocalizedLabel;
8518 final int labelRes;
8519 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8522 int _labelRes, int _icon) {
8523 pkg = _pkg;
8524 theme = _theme;
8525 nonLocalizedLabel = _nonLocalizedLabel;
8526 labelRes = _labelRes;
8527 icon = _icon;
8528 }
8529 }
8530
8531 private final class H extends Handler {
8532 public static final int REPORT_FOCUS_CHANGE = 2;
8533 public static final int REPORT_LOSING_FOCUS = 3;
8534 public static final int ANIMATE = 4;
8535 public static final int ADD_STARTING = 5;
8536 public static final int REMOVE_STARTING = 6;
8537 public static final int FINISHED_STARTING = 7;
8538 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008539 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8540 public static final int HOLD_SCREEN_CHANGED = 12;
8541 public static final int APP_TRANSITION_TIMEOUT = 13;
8542 public static final int PERSIST_ANIMATION_SCALE = 14;
8543 public static final int FORCE_GC = 15;
8544 public static final int ENABLE_SCREEN = 16;
8545 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008546 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008550 public H() {
8551 }
Romain Guy06882f82009-06-10 13:36:04 -07008552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 @Override
8554 public void handleMessage(Message msg) {
8555 switch (msg.what) {
8556 case REPORT_FOCUS_CHANGE: {
8557 WindowState lastFocus;
8558 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008560 synchronized(mWindowMap) {
8561 lastFocus = mLastFocus;
8562 newFocus = mCurrentFocus;
8563 if (lastFocus == newFocus) {
8564 // Focus is not changing, so nothing to do.
8565 return;
8566 }
8567 mLastFocus = newFocus;
8568 //Log.i(TAG, "Focus moving from " + lastFocus
8569 // + " to " + newFocus);
8570 if (newFocus != null && lastFocus != null
8571 && !newFocus.isDisplayedLw()) {
8572 //Log.i(TAG, "Delaying loss of focus...");
8573 mLosingFocus.add(lastFocus);
8574 lastFocus = null;
8575 }
8576 }
8577
8578 if (lastFocus != newFocus) {
8579 //System.out.println("Changing focus from " + lastFocus
8580 // + " to " + newFocus);
8581 if (newFocus != null) {
8582 try {
8583 //Log.i(TAG, "Gaining focus: " + newFocus);
8584 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8585 } catch (RemoteException e) {
8586 // Ignore if process has died.
8587 }
8588 }
8589
8590 if (lastFocus != null) {
8591 try {
8592 //Log.i(TAG, "Losing focus: " + lastFocus);
8593 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8594 } catch (RemoteException e) {
8595 // Ignore if process has died.
8596 }
8597 }
8598 }
8599 } break;
8600
8601 case REPORT_LOSING_FOCUS: {
8602 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008604 synchronized(mWindowMap) {
8605 losers = mLosingFocus;
8606 mLosingFocus = new ArrayList<WindowState>();
8607 }
8608
8609 final int N = losers.size();
8610 for (int i=0; i<N; i++) {
8611 try {
8612 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8613 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8614 } catch (RemoteException e) {
8615 // Ignore if process has died.
8616 }
8617 }
8618 } break;
8619
8620 case ANIMATE: {
8621 synchronized(mWindowMap) {
8622 mAnimationPending = false;
8623 performLayoutAndPlaceSurfacesLocked();
8624 }
8625 } break;
8626
8627 case ADD_STARTING: {
8628 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8629 final StartingData sd = wtoken.startingData;
8630
8631 if (sd == null) {
8632 // Animation has been canceled... do nothing.
8633 return;
8634 }
Romain Guy06882f82009-06-10 13:36:04 -07008635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008636 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8637 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008639 View view = null;
8640 try {
8641 view = mPolicy.addStartingWindow(
8642 wtoken.token, sd.pkg,
8643 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8644 sd.icon);
8645 } catch (Exception e) {
8646 Log.w(TAG, "Exception when adding starting window", e);
8647 }
8648
8649 if (view != null) {
8650 boolean abort = false;
8651
8652 synchronized(mWindowMap) {
8653 if (wtoken.removed || wtoken.startingData == null) {
8654 // If the window was successfully added, then
8655 // we need to remove it.
8656 if (wtoken.startingWindow != null) {
8657 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8658 "Aborted starting " + wtoken
8659 + ": removed=" + wtoken.removed
8660 + " startingData=" + wtoken.startingData);
8661 wtoken.startingWindow = null;
8662 wtoken.startingData = null;
8663 abort = true;
8664 }
8665 } else {
8666 wtoken.startingView = view;
8667 }
8668 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8669 "Added starting " + wtoken
8670 + ": startingWindow="
8671 + wtoken.startingWindow + " startingView="
8672 + wtoken.startingView);
8673 }
8674
8675 if (abort) {
8676 try {
8677 mPolicy.removeStartingWindow(wtoken.token, view);
8678 } catch (Exception e) {
8679 Log.w(TAG, "Exception when removing starting window", e);
8680 }
8681 }
8682 }
8683 } break;
8684
8685 case REMOVE_STARTING: {
8686 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8687 IBinder token = null;
8688 View view = null;
8689 synchronized (mWindowMap) {
8690 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8691 + wtoken + ": startingWindow="
8692 + wtoken.startingWindow + " startingView="
8693 + wtoken.startingView);
8694 if (wtoken.startingWindow != null) {
8695 view = wtoken.startingView;
8696 token = wtoken.token;
8697 wtoken.startingData = null;
8698 wtoken.startingView = null;
8699 wtoken.startingWindow = null;
8700 }
8701 }
8702 if (view != null) {
8703 try {
8704 mPolicy.removeStartingWindow(token, view);
8705 } catch (Exception e) {
8706 Log.w(TAG, "Exception when removing starting window", e);
8707 }
8708 }
8709 } break;
8710
8711 case FINISHED_STARTING: {
8712 IBinder token = null;
8713 View view = null;
8714 while (true) {
8715 synchronized (mWindowMap) {
8716 final int N = mFinishedStarting.size();
8717 if (N <= 0) {
8718 break;
8719 }
8720 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8721
8722 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8723 "Finished starting " + wtoken
8724 + ": startingWindow=" + wtoken.startingWindow
8725 + " startingView=" + wtoken.startingView);
8726
8727 if (wtoken.startingWindow == null) {
8728 continue;
8729 }
8730
8731 view = wtoken.startingView;
8732 token = wtoken.token;
8733 wtoken.startingData = null;
8734 wtoken.startingView = null;
8735 wtoken.startingWindow = null;
8736 }
8737
8738 try {
8739 mPolicy.removeStartingWindow(token, view);
8740 } catch (Exception e) {
8741 Log.w(TAG, "Exception when removing starting window", e);
8742 }
8743 }
8744 } break;
8745
8746 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8747 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8748
8749 boolean nowVisible = msg.arg1 != 0;
8750 boolean nowGone = msg.arg2 != 0;
8751
8752 try {
8753 if (DEBUG_VISIBILITY) Log.v(
8754 TAG, "Reporting visible in " + wtoken
8755 + " visible=" + nowVisible
8756 + " gone=" + nowGone);
8757 if (nowVisible) {
8758 wtoken.appToken.windowsVisible();
8759 } else {
8760 wtoken.appToken.windowsGone();
8761 }
8762 } catch (RemoteException ex) {
8763 }
8764 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008766 case WINDOW_FREEZE_TIMEOUT: {
8767 synchronized (mWindowMap) {
8768 Log.w(TAG, "Window freeze timeout expired.");
8769 int i = mWindows.size();
8770 while (i > 0) {
8771 i--;
8772 WindowState w = (WindowState)mWindows.get(i);
8773 if (w.mOrientationChanging) {
8774 w.mOrientationChanging = false;
8775 Log.w(TAG, "Force clearing orientation change: " + w);
8776 }
8777 }
8778 performLayoutAndPlaceSurfacesLocked();
8779 }
8780 break;
8781 }
Romain Guy06882f82009-06-10 13:36:04 -07008782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008783 case HOLD_SCREEN_CHANGED: {
8784 Session oldHold;
8785 Session newHold;
8786 synchronized (mWindowMap) {
8787 oldHold = mLastReportedHold;
8788 newHold = (Session)msg.obj;
8789 mLastReportedHold = newHold;
8790 }
Romain Guy06882f82009-06-10 13:36:04 -07008791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008792 if (oldHold != newHold) {
8793 try {
8794 if (oldHold != null) {
8795 mBatteryStats.noteStopWakelock(oldHold.mUid,
8796 "window",
8797 BatteryStats.WAKE_TYPE_WINDOW);
8798 }
8799 if (newHold != null) {
8800 mBatteryStats.noteStartWakelock(newHold.mUid,
8801 "window",
8802 BatteryStats.WAKE_TYPE_WINDOW);
8803 }
8804 } catch (RemoteException e) {
8805 }
8806 }
8807 break;
8808 }
Romain Guy06882f82009-06-10 13:36:04 -07008809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008810 case APP_TRANSITION_TIMEOUT: {
8811 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008812 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008813 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8814 "*** APP TRANSITION TIMEOUT");
8815 mAppTransitionReady = true;
8816 mAppTransitionTimeout = true;
8817 performLayoutAndPlaceSurfacesLocked();
8818 }
8819 }
8820 break;
8821 }
Romain Guy06882f82009-06-10 13:36:04 -07008822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008823 case PERSIST_ANIMATION_SCALE: {
8824 Settings.System.putFloat(mContext.getContentResolver(),
8825 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8826 Settings.System.putFloat(mContext.getContentResolver(),
8827 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8828 break;
8829 }
Romain Guy06882f82009-06-10 13:36:04 -07008830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008831 case FORCE_GC: {
8832 synchronized(mWindowMap) {
8833 if (mAnimationPending) {
8834 // If we are animating, don't do the gc now but
8835 // delay a bit so we don't interrupt the animation.
8836 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8837 2000);
8838 return;
8839 }
8840 // If we are currently rotating the display, it will
8841 // schedule a new message when done.
8842 if (mDisplayFrozen) {
8843 return;
8844 }
8845 mFreezeGcPending = 0;
8846 }
8847 Runtime.getRuntime().gc();
8848 break;
8849 }
Romain Guy06882f82009-06-10 13:36:04 -07008850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008851 case ENABLE_SCREEN: {
8852 performEnableScreen();
8853 break;
8854 }
Romain Guy06882f82009-06-10 13:36:04 -07008855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008856 case APP_FREEZE_TIMEOUT: {
8857 synchronized (mWindowMap) {
8858 Log.w(TAG, "App freeze timeout expired.");
8859 int i = mAppTokens.size();
8860 while (i > 0) {
8861 i--;
8862 AppWindowToken tok = mAppTokens.get(i);
8863 if (tok.freezingScreen) {
8864 Log.w(TAG, "Force clearing freeze: " + tok);
8865 unsetAppFreezingScreenLocked(tok, true, true);
8866 }
8867 }
8868 }
8869 break;
8870 }
Romain Guy06882f82009-06-10 13:36:04 -07008871
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008872 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008873 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008874 sendNewConfiguration();
8875 }
8876 break;
8877 }
Romain Guy06882f82009-06-10 13:36:04 -07008878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008879 }
8880 }
8881 }
8882
8883 // -------------------------------------------------------------
8884 // IWindowManager API
8885 // -------------------------------------------------------------
8886
8887 public IWindowSession openSession(IInputMethodClient client,
8888 IInputContext inputContext) {
8889 if (client == null) throw new IllegalArgumentException("null client");
8890 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8891 return new Session(client, inputContext);
8892 }
8893
8894 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8895 synchronized (mWindowMap) {
8896 // The focus for the client is the window immediately below
8897 // where we would place the input method window.
8898 int idx = findDesiredInputMethodWindowIndexLocked(false);
8899 WindowState imFocus;
8900 if (idx > 0) {
8901 imFocus = (WindowState)mWindows.get(idx-1);
8902 if (imFocus != null) {
8903 if (imFocus.mSession.mClient != null &&
8904 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8905 return true;
8906 }
8907 }
8908 }
8909 }
8910 return false;
8911 }
Romain Guy06882f82009-06-10 13:36:04 -07008912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008913 // -------------------------------------------------------------
8914 // Internals
8915 // -------------------------------------------------------------
8916
8917 final WindowState windowForClientLocked(Session session, IWindow client) {
8918 return windowForClientLocked(session, client.asBinder());
8919 }
Romain Guy06882f82009-06-10 13:36:04 -07008920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008921 final WindowState windowForClientLocked(Session session, IBinder client) {
8922 WindowState win = mWindowMap.get(client);
8923 if (localLOGV) Log.v(
8924 TAG, "Looking up client " + client + ": " + win);
8925 if (win == null) {
8926 RuntimeException ex = new RuntimeException();
8927 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8928 return null;
8929 }
8930 if (session != null && win.mSession != session) {
8931 RuntimeException ex = new RuntimeException();
8932 Log.w(TAG, "Requested window " + client + " is in session " +
8933 win.mSession + ", not " + session, ex);
8934 return null;
8935 }
8936
8937 return win;
8938 }
8939
Dianne Hackborna8f60182009-09-01 19:01:50 -07008940 final void rebuildAppWindowListLocked() {
8941 int NW = mWindows.size();
8942 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008943 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008944 int numRemoved = 0;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008945
8946 // First remove all existing app windows.
8947 i=0;
8948 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008949 WindowState w = (WindowState)mWindows.get(i);
8950 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008951 WindowState win = (WindowState)mWindows.remove(i);
8952 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8953 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008954 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008955 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008956 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008957 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
8958 && lastWallpaper == i-1) {
8959 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008960 }
8961 i++;
8962 }
8963
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008964 // The wallpaper window(s) typically live at the bottom of the stack,
8965 // so skip them before adding app tokens.
8966 lastWallpaper++;
8967 i = lastWallpaper;
8968
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008969 // First add all of the exiting app tokens... these are no longer
8970 // in the main app list, but still have windows shown. We put them
8971 // in the back because now that the animation is over we no longer
8972 // will care about them.
8973 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008974 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008975 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8976 }
8977
8978 // And add in the still active app tokens in Z order.
8979 NT = mAppTokens.size();
8980 for (int j=0; j<NT; j++) {
8981 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008982 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008983
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008984 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07008985 if (i != numRemoved) {
8986 Log.w(TAG, "Rebuild removed " + numRemoved
8987 + " windows but added " + i);
8988 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07008989 }
8990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008991 private final void assignLayersLocked() {
8992 int N = mWindows.size();
8993 int curBaseLayer = 0;
8994 int curLayer = 0;
8995 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008997 for (i=0; i<N; i++) {
8998 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008999 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9000 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009001 curLayer += WINDOW_LAYER_MULTIPLIER;
9002 w.mLayer = curLayer;
9003 } else {
9004 curBaseLayer = curLayer = w.mBaseLayer;
9005 w.mLayer = curLayer;
9006 }
9007 if (w.mTargetAppToken != null) {
9008 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9009 } else if (w.mAppToken != null) {
9010 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9011 } else {
9012 w.mAnimLayer = w.mLayer;
9013 }
9014 if (w.mIsImWindow) {
9015 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009016 } else if (w.mIsWallpaper) {
9017 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009018 }
9019 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9020 + w.mAnimLayer);
9021 //System.out.println(
9022 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9023 }
9024 }
9025
9026 private boolean mInLayout = false;
9027 private final void performLayoutAndPlaceSurfacesLocked() {
9028 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009029 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009030 throw new RuntimeException("Recursive call!");
9031 }
9032 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9033 return;
9034 }
9035
9036 boolean recoveringMemory = false;
9037 if (mForceRemoves != null) {
9038 recoveringMemory = true;
9039 // Wait a little it for things to settle down, and off we go.
9040 for (int i=0; i<mForceRemoves.size(); i++) {
9041 WindowState ws = mForceRemoves.get(i);
9042 Log.i(TAG, "Force removing: " + ws);
9043 removeWindowInnerLocked(ws.mSession, ws);
9044 }
9045 mForceRemoves = null;
9046 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9047 Object tmp = new Object();
9048 synchronized (tmp) {
9049 try {
9050 tmp.wait(250);
9051 } catch (InterruptedException e) {
9052 }
9053 }
9054 }
Romain Guy06882f82009-06-10 13:36:04 -07009055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009056 mInLayout = true;
9057 try {
9058 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009060 int i = mPendingRemove.size()-1;
9061 if (i >= 0) {
9062 while (i >= 0) {
9063 WindowState w = mPendingRemove.get(i);
9064 removeWindowInnerLocked(w.mSession, w);
9065 i--;
9066 }
9067 mPendingRemove.clear();
9068
9069 mInLayout = false;
9070 assignLayersLocked();
9071 mLayoutNeeded = true;
9072 performLayoutAndPlaceSurfacesLocked();
9073
9074 } else {
9075 mInLayout = false;
9076 if (mLayoutNeeded) {
9077 requestAnimationLocked(0);
9078 }
9079 }
9080 } catch (RuntimeException e) {
9081 mInLayout = false;
9082 Log.e(TAG, "Unhandled exception while layout out windows", e);
9083 }
9084 }
9085
9086 private final void performLayoutLockedInner() {
9087 final int dw = mDisplay.getWidth();
9088 final int dh = mDisplay.getHeight();
9089
9090 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009091 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009092 int i;
9093
9094 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009095
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009096 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009097 mPolicy.beginLayoutLw(dw, dh);
9098
9099 // First perform layout of any root windows (not attached
9100 // to another window).
9101 int topAttached = -1;
9102 for (i = N-1; i >= 0; i--) {
9103 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009104
9105 // Don't do layout of a window if it is not visible, or
9106 // soon won't be visible, to avoid wasting time and funky
9107 // changes while a window is animating away.
9108 final AppWindowToken atoken = win.mAppToken;
9109 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009110 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009111 || win.mRootToken.hidden
9112 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009113 || win.mAttachedHidden
9114 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115
9116 // If this view is GONE, then skip it -- keep the current
9117 // frame, and let the caller know so they can ignore it
9118 // if they want. (We do the normal layout for INVISIBLE
9119 // windows, since that means "perform layout as normal,
9120 // just don't display").
9121 if (!gone || !win.mHaveFrame) {
9122 if (!win.mLayoutAttached) {
9123 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9124 } else {
9125 if (topAttached < 0) topAttached = i;
9126 }
9127 }
9128 }
Romain Guy06882f82009-06-10 13:36:04 -07009129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009130 // Now perform layout of attached windows, which usually
9131 // depend on the position of the window they are attached to.
9132 // XXX does not deal with windows that are attached to windows
9133 // that are themselves attached.
9134 for (i = topAttached; i >= 0; i--) {
9135 WindowState win = (WindowState) mWindows.get(i);
9136
9137 // If this view is GONE, then skip it -- keep the current
9138 // frame, and let the caller know so they can ignore it
9139 // if they want. (We do the normal layout for INVISIBLE
9140 // windows, since that means "perform layout as normal,
9141 // just don't display").
9142 if (win.mLayoutAttached) {
9143 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9144 || !win.mHaveFrame) {
9145 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9146 }
9147 }
9148 }
9149
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009150 int changes = mPolicy.finishLayoutLw();
9151 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9152 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9153 assignLayersLocked();
9154 }
9155 }
9156 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009157 mLayoutNeeded = false;
9158 } else if (repeats > 2) {
9159 Log.w(TAG, "Layout repeat aborted after too many iterations");
9160 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009161 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9162 Configuration newConfig = updateOrientationFromAppTokensLocked(
9163 null, null);
9164 if (newConfig != null) {
9165 mLayoutNeeded = true;
9166 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9167 }
9168 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009169 } else {
9170 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009171 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9172 Configuration newConfig = updateOrientationFromAppTokensLocked(
9173 null, null);
9174 if (newConfig != null) {
9175 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9176 }
9177 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009179 }
9180 }
Romain Guy06882f82009-06-10 13:36:04 -07009181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009182 private final void performLayoutAndPlaceSurfacesLockedInner(
9183 boolean recoveringMemory) {
9184 final long currentTime = SystemClock.uptimeMillis();
9185 final int dw = mDisplay.getWidth();
9186 final int dh = mDisplay.getHeight();
9187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009188 int i;
9189
9190 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009191 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009193 if (mFxSession == null) {
9194 mFxSession = new SurfaceSession();
9195 }
Romain Guy06882f82009-06-10 13:36:04 -07009196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009197 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9198
9199 // Initialize state of exiting tokens.
9200 for (i=mExitingTokens.size()-1; i>=0; i--) {
9201 mExitingTokens.get(i).hasVisible = false;
9202 }
9203
9204 // Initialize state of exiting applications.
9205 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9206 mExitingAppTokens.get(i).hasVisible = false;
9207 }
9208
9209 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009210 boolean orientationChangeComplete = true;
9211 Session holdScreen = null;
9212 float screenBrightness = -1;
9213 boolean focusDisplayed = false;
9214 boolean animating = false;
9215
9216 Surface.openTransaction();
9217 try {
9218 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009219 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009220
9221 do {
9222 final int transactionSequence = ++mTransactionSequence;
9223
9224 // Update animations of all applications, including those
9225 // associated with exiting/removed apps
9226 boolean tokensAnimating = false;
9227 final int NAT = mAppTokens.size();
9228 for (i=0; i<NAT; i++) {
9229 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9230 tokensAnimating = true;
9231 }
9232 }
9233 final int NEAT = mExitingAppTokens.size();
9234 for (i=0; i<NEAT; i++) {
9235 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9236 tokensAnimating = true;
9237 }
9238 }
9239
9240 animating = tokensAnimating;
9241 restart = false;
9242
9243 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009244 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009245 boolean focusMayChange = false;
9246 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009247
9248 mPolicy.beginAnimationLw(dw, dh);
9249
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009250 final int N = mWindows.size();
9251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009252 for (i=N-1; i>=0; i--) {
9253 WindowState w = (WindowState)mWindows.get(i);
9254
9255 final WindowManager.LayoutParams attrs = w.mAttrs;
9256
9257 if (w.mSurface != null) {
9258 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009259 if (w.commitFinishDrawingLocked(currentTime)) {
9260 if ((w.mAttrs.flags
9261 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009262 if (DEBUG_WALLPAPER) Log.v(TAG,
9263 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009264 wallpaperMayChange = true;
9265 }
9266 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009267
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009268 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009269 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9270 animating = true;
9271 //w.dump(" ");
9272 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009273 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9274 wallpaperMayChange = true;
9275 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009276
9277 if (mPolicy.doesForceHide(w, attrs)) {
9278 if (!wasAnimating && animating) {
9279 wallpaperForceHidingChanged = true;
9280 focusMayChange = true;
9281 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9282 forceHiding = true;
9283 }
9284 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9285 boolean changed;
9286 if (forceHiding) {
9287 changed = w.hideLw(false, false);
9288 } else {
9289 changed = w.showLw(false, false);
9290 if (changed && wallpaperForceHidingChanged
9291 && w.isReadyForDisplay()) {
9292 // Assume we will need to animate. If
9293 // we don't (because the wallpaper will
9294 // stay with the lock screen), then we will
9295 // clean up later.
9296 Animation a = mPolicy.createForceHideEnterAnimation();
9297 if (a != null) {
9298 w.setAnimation(a);
9299 }
9300 }
9301 }
9302 if (changed && (attrs.flags
9303 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9304 wallpaperMayChange = true;
9305 }
Christopher Tate405d0892009-10-27 20:23:28 -07009306 if (changed && !forceHiding
9307 && (mCurrentFocus == null)
9308 && (mFocusedApp != null)) {
9309 // It's possible that the last focus recalculation left no
9310 // current focused window even though the app has come to the
9311 // foreground already. In this case, we make sure to recalculate
9312 // focus when we show a window.
9313 focusMayChange = true;
9314 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009315 }
9316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009317 mPolicy.animatingWindowLw(w, attrs);
9318 }
9319
9320 final AppWindowToken atoken = w.mAppToken;
9321 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9322 if (atoken.lastTransactionSequence != transactionSequence) {
9323 atoken.lastTransactionSequence = transactionSequence;
9324 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9325 atoken.startingDisplayed = false;
9326 }
9327 if ((w.isOnScreen() || w.mAttrs.type
9328 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9329 && !w.mExiting && !w.mDestroying) {
9330 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009331 Log.v(TAG, "Eval win " + w + ": isDrawn="
9332 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009333 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009334 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009335 Log.v(TAG, "Not displayed: s=" + w.mSurface
9336 + " pv=" + w.mPolicyVisibility
9337 + " dp=" + w.mDrawPending
9338 + " cdp=" + w.mCommitDrawPending
9339 + " ah=" + w.mAttachedHidden
9340 + " th=" + atoken.hiddenRequested
9341 + " a=" + w.mAnimating);
9342 }
9343 }
9344 if (w != atoken.startingWindow) {
9345 if (!atoken.freezingScreen || !w.mAppFreezing) {
9346 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009347 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009348 atoken.numDrawnWindows++;
9349 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9350 "tokenMayBeDrawn: " + atoken
9351 + " freezingScreen=" + atoken.freezingScreen
9352 + " mAppFreezing=" + w.mAppFreezing);
9353 tokenMayBeDrawn = true;
9354 }
9355 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009356 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009357 atoken.startingDisplayed = true;
9358 }
9359 }
9360 } else if (w.mReadyToShow) {
9361 w.performShowLocked();
9362 }
9363 }
9364
9365 if (mPolicy.finishAnimationLw()) {
9366 restart = true;
9367 }
9368
9369 if (tokenMayBeDrawn) {
9370 // See if any windows have been drawn, so they (and others
9371 // associated with them) can now be shown.
9372 final int NT = mTokenList.size();
9373 for (i=0; i<NT; i++) {
9374 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9375 if (wtoken == null) {
9376 continue;
9377 }
9378 if (wtoken.freezingScreen) {
9379 int numInteresting = wtoken.numInterestingWindows;
9380 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9381 if (DEBUG_VISIBILITY) Log.v(TAG,
9382 "allDrawn: " + wtoken
9383 + " interesting=" + numInteresting
9384 + " drawn=" + wtoken.numDrawnWindows);
9385 wtoken.showAllWindowsLocked();
9386 unsetAppFreezingScreenLocked(wtoken, false, true);
9387 orientationChangeComplete = true;
9388 }
9389 } else if (!wtoken.allDrawn) {
9390 int numInteresting = wtoken.numInterestingWindows;
9391 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9392 if (DEBUG_VISIBILITY) Log.v(TAG,
9393 "allDrawn: " + wtoken
9394 + " interesting=" + numInteresting
9395 + " drawn=" + wtoken.numDrawnWindows);
9396 wtoken.allDrawn = true;
9397 restart = true;
9398
9399 // We can now show all of the drawn windows!
9400 if (!mOpeningApps.contains(wtoken)) {
9401 wtoken.showAllWindowsLocked();
9402 }
9403 }
9404 }
9405 }
9406 }
9407
9408 // If we are ready to perform an app transition, check through
9409 // all of the app tokens to be shown and see if they are ready
9410 // to go.
9411 if (mAppTransitionReady) {
9412 int NN = mOpeningApps.size();
9413 boolean goodToGo = true;
9414 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9415 "Checking " + NN + " opening apps (frozen="
9416 + mDisplayFrozen + " timeout="
9417 + mAppTransitionTimeout + ")...");
9418 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9419 // If the display isn't frozen, wait to do anything until
9420 // all of the apps are ready. Otherwise just go because
9421 // we'll unfreeze the display when everyone is ready.
9422 for (i=0; i<NN && goodToGo; i++) {
9423 AppWindowToken wtoken = mOpeningApps.get(i);
9424 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9425 "Check opening app" + wtoken + ": allDrawn="
9426 + wtoken.allDrawn + " startingDisplayed="
9427 + wtoken.startingDisplayed);
9428 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9429 && !wtoken.startingMoved) {
9430 goodToGo = false;
9431 }
9432 }
9433 }
9434 if (goodToGo) {
9435 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9436 int transit = mNextAppTransition;
9437 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009438 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009439 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009440 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009441 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009442 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009443 mAppTransitionTimeout = false;
9444 mStartingIconInTransition = false;
9445 mSkipAppTransitionAnimation = false;
9446
9447 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9448
Dianne Hackborna8f60182009-09-01 19:01:50 -07009449 // If there are applications waiting to come to the
9450 // top of the stack, now is the time to move their windows.
9451 // (Note that we don't do apps going to the bottom
9452 // here -- we want to keep their windows in the old
9453 // Z-order until the animation completes.)
9454 if (mToTopApps.size() > 0) {
9455 NN = mAppTokens.size();
9456 for (i=0; i<NN; i++) {
9457 AppWindowToken wtoken = mAppTokens.get(i);
9458 if (wtoken.sendingToTop) {
9459 wtoken.sendingToTop = false;
9460 moveAppWindowsLocked(wtoken, NN, false);
9461 }
9462 }
9463 mToTopApps.clear();
9464 }
9465
Dianne Hackborn25994b42009-09-04 14:21:19 -07009466 WindowState oldWallpaper = mWallpaperTarget;
9467
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009468 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009469 wallpaperMayChange = false;
9470
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009471 // The top-most window will supply the layout params,
9472 // and we will determine it below.
9473 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009474 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009475 int bestAnimLayer = -1;
9476
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009477 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009478 "New wallpaper target=" + mWallpaperTarget
9479 + ", lower target=" + mLowerWallpaperTarget
9480 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009481 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009482 // Do a first pass through the tokens for two
9483 // things:
9484 // (1) Determine if both the closing and opening
9485 // app token sets are wallpaper targets, in which
9486 // case special animations are needed
9487 // (since the wallpaper needs to stay static
9488 // behind them).
9489 // (2) Find the layout params of the top-most
9490 // application window in the tokens, which is
9491 // what will control the animation theme.
9492 final int NC = mClosingApps.size();
9493 NN = NC + mOpeningApps.size();
9494 for (i=0; i<NN; i++) {
9495 AppWindowToken wtoken;
9496 int mode;
9497 if (i < NC) {
9498 wtoken = mClosingApps.get(i);
9499 mode = 1;
9500 } else {
9501 wtoken = mOpeningApps.get(i-NC);
9502 mode = 2;
9503 }
9504 if (mLowerWallpaperTarget != null) {
9505 if (mLowerWallpaperTarget.mAppToken == wtoken
9506 || mUpperWallpaperTarget.mAppToken == wtoken) {
9507 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009508 }
9509 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009510 if (wtoken.appFullscreen) {
9511 WindowState ws = wtoken.findMainWindow();
9512 if (ws != null) {
9513 // If this is a compatibility mode
9514 // window, we will always use its anim.
9515 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9516 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009517 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009518 bestAnimLayer = Integer.MAX_VALUE;
9519 } else if (ws.mLayer > bestAnimLayer) {
9520 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009521 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009522 bestAnimLayer = ws.mLayer;
9523 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009524 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009525 }
9526 }
9527
Dianne Hackborn25994b42009-09-04 14:21:19 -07009528 if (foundWallpapers == 3) {
9529 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9530 "Wallpaper animation!");
9531 switch (transit) {
9532 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9533 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9534 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9535 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9536 break;
9537 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9538 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9539 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9540 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9541 break;
9542 }
9543 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9544 "New transit: " + transit);
9545 } else if (oldWallpaper != null) {
9546 // We are transitioning from an activity with
9547 // a wallpaper to one without.
9548 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9549 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9550 "New transit away from wallpaper: " + transit);
9551 } else if (mWallpaperTarget != null) {
9552 // We are transitioning from an activity without
9553 // a wallpaper to now showing the wallpaper
9554 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9555 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9556 "New transit into wallpaper: " + transit);
9557 }
9558
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009559 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9560 mLastEnterAnimToken = animToken;
9561 mLastEnterAnimParams = animLp;
9562 } else if (mLastEnterAnimParams != null) {
9563 animLp = mLastEnterAnimParams;
9564 mLastEnterAnimToken = null;
9565 mLastEnterAnimParams = null;
9566 }
9567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009568 NN = mOpeningApps.size();
9569 for (i=0; i<NN; i++) {
9570 AppWindowToken wtoken = mOpeningApps.get(i);
9571 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9572 "Now opening app" + wtoken);
9573 wtoken.reportedVisible = false;
9574 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009575 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009576 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009577 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009578 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009579 wtoken.showAllWindowsLocked();
9580 }
9581 NN = mClosingApps.size();
9582 for (i=0; i<NN; i++) {
9583 AppWindowToken wtoken = mClosingApps.get(i);
9584 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9585 "Now closing app" + wtoken);
9586 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009587 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009588 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009589 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009590 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009591 // Force the allDrawn flag, because we want to start
9592 // this guy's animations regardless of whether it's
9593 // gotten drawn.
9594 wtoken.allDrawn = true;
9595 }
9596
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009597 mNextAppTransitionPackage = null;
9598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009599 mOpeningApps.clear();
9600 mClosingApps.clear();
9601
9602 // This has changed the visibility of windows, so perform
9603 // a new layout to get them all up-to-date.
9604 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009605 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9606 assignLayersLocked();
9607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009608 performLayoutLockedInner();
9609 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009610 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009611
9612 restart = true;
9613 }
9614 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009615
Dianne Hackborna8f60182009-09-01 19:01:50 -07009616 if (!animating && mAppTransitionRunning) {
9617 // We have finished the animation of an app transition. To do
9618 // this, we have delayed a lot of operations like showing and
9619 // hiding apps, moving apps in Z-order, etc. The app token list
9620 // reflects the correct Z-order, but the window list may now
9621 // be out of sync with it. So here we will just rebuild the
9622 // entire app window list. Fun!
9623 mAppTransitionRunning = false;
9624 // Clear information about apps that were moving.
9625 mToBottomApps.clear();
9626
9627 rebuildAppWindowListLocked();
9628 restart = true;
9629 moveInputMethodWindowsIfNeededLocked(false);
9630 wallpaperMayChange = true;
9631 mLayoutNeeded = true;
9632 }
9633
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009634 int adjResult = 0;
9635
9636 if (wallpaperForceHidingChanged) {
9637 // At this point, there was a window with a wallpaper that
9638 // was force hiding other windows behind it, but now it
9639 // is going away. This may be simple -- just animate
9640 // away the wallpaper and its window -- or it may be
9641 // hard -- the wallpaper now needs to be shown behind
9642 // something that was hidden.
9643 WindowState oldWallpaper = mWallpaperTarget;
9644 adjResult = adjustWallpaperWindowsLocked();
9645 wallpaperMayChange = false;
9646 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9647 + " NEW: " + mWallpaperTarget);
9648 if (mLowerWallpaperTarget == null) {
9649 // Whoops, we don't need a special wallpaper animation.
9650 // Clear them out.
9651 forceHiding = false;
9652 for (i=N-1; i>=0; i--) {
9653 WindowState w = (WindowState)mWindows.get(i);
9654 if (w.mSurface != null) {
9655 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009656 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9657 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009658 forceHiding = true;
9659 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9660 if (!w.mAnimating) {
9661 // We set the animation above so it
9662 // is not yet running.
9663 w.clearAnimation();
9664 }
9665 }
9666 }
9667 }
9668 }
9669 }
9670
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009671 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009672 if (DEBUG_WALLPAPER) Log.v(TAG,
9673 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009674 adjResult = adjustWallpaperWindowsLocked();
9675 }
9676
9677 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9678 if (DEBUG_WALLPAPER) Log.v(TAG,
9679 "Wallpaper layer changed: assigning layers + relayout");
9680 restart = true;
9681 mLayoutNeeded = true;
9682 assignLayersLocked();
9683 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9684 if (DEBUG_WALLPAPER) Log.v(TAG,
9685 "Wallpaper visibility changed: relayout");
9686 restart = true;
9687 mLayoutNeeded = true;
9688 }
9689
9690 if (focusMayChange) {
9691 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009692 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009693 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009694 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009695 }
9696
9697 if (mLayoutNeeded) {
9698 restart = true;
9699 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009700 }
9701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009702 } while (restart);
9703
9704 // THIRD LOOP: Update the surfaces of all windows.
9705
9706 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9707
9708 boolean obscured = false;
9709 boolean blurring = false;
9710 boolean dimming = false;
9711 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009712 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009713 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009714
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009715 final int N = mWindows.size();
9716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009717 for (i=N-1; i>=0; i--) {
9718 WindowState w = (WindowState)mWindows.get(i);
9719
9720 boolean displayed = false;
9721 final WindowManager.LayoutParams attrs = w.mAttrs;
9722 final int attrFlags = attrs.flags;
9723
9724 if (w.mSurface != null) {
9725 w.computeShownFrameLocked();
9726 if (localLOGV) Log.v(
9727 TAG, "Placing surface #" + i + " " + w.mSurface
9728 + ": new=" + w.mShownFrame + ", old="
9729 + w.mLastShownFrame);
9730
9731 boolean resize;
9732 int width, height;
9733 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9734 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9735 w.mLastRequestedHeight != w.mRequestedHeight;
9736 // for a scaled surface, we just want to use
9737 // the requested size.
9738 width = w.mRequestedWidth;
9739 height = w.mRequestedHeight;
9740 w.mLastRequestedWidth = width;
9741 w.mLastRequestedHeight = height;
9742 w.mLastShownFrame.set(w.mShownFrame);
9743 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009744 if (SHOW_TRANSACTIONS) Log.i(
9745 TAG, " SURFACE " + w.mSurface
9746 + ": POS " + w.mShownFrame.left
9747 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009748 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9749 } catch (RuntimeException e) {
9750 Log.w(TAG, "Error positioning surface in " + w, e);
9751 if (!recoveringMemory) {
9752 reclaimSomeSurfaceMemoryLocked(w, "position");
9753 }
9754 }
9755 } else {
9756 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9757 width = w.mShownFrame.width();
9758 height = w.mShownFrame.height();
9759 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009760 }
9761
9762 if (resize) {
9763 if (width < 1) width = 1;
9764 if (height < 1) height = 1;
9765 if (w.mSurface != null) {
9766 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009767 if (SHOW_TRANSACTIONS) Log.i(
9768 TAG, " SURFACE " + w.mSurface + ": POS "
9769 + w.mShownFrame.left + ","
9770 + w.mShownFrame.top + " SIZE "
9771 + w.mShownFrame.width() + "x"
9772 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009773 w.mSurface.setSize(width, height);
9774 w.mSurface.setPosition(w.mShownFrame.left,
9775 w.mShownFrame.top);
9776 } catch (RuntimeException e) {
9777 // If something goes wrong with the surface (such
9778 // as running out of memory), don't take down the
9779 // entire system.
9780 Log.e(TAG, "Failure updating surface of " + w
9781 + "size=(" + width + "x" + height
9782 + "), pos=(" + w.mShownFrame.left
9783 + "," + w.mShownFrame.top + ")", e);
9784 if (!recoveringMemory) {
9785 reclaimSomeSurfaceMemoryLocked(w, "size");
9786 }
9787 }
9788 }
9789 }
9790 if (!w.mAppFreezing) {
9791 w.mContentInsetsChanged =
9792 !w.mLastContentInsets.equals(w.mContentInsets);
9793 w.mVisibleInsetsChanged =
9794 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009795 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009796 || w.mContentInsetsChanged
9797 || w.mVisibleInsetsChanged) {
9798 w.mLastFrame.set(w.mFrame);
9799 w.mLastContentInsets.set(w.mContentInsets);
9800 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009801 // If the screen is currently frozen, then keep
9802 // it frozen until this window draws at its new
9803 // orientation.
9804 if (mDisplayFrozen) {
9805 if (DEBUG_ORIENTATION) Log.v(TAG,
9806 "Resizing while display frozen: " + w);
9807 w.mOrientationChanging = true;
9808 if (mWindowsFreezingScreen) {
9809 mWindowsFreezingScreen = true;
9810 // XXX should probably keep timeout from
9811 // when we first froze the display.
9812 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9813 mH.sendMessageDelayed(mH.obtainMessage(
9814 H.WINDOW_FREEZE_TIMEOUT), 2000);
9815 }
9816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009817 // If the orientation is changing, then we need to
9818 // hold off on unfreezing the display until this
9819 // window has been redrawn; to do that, we need
9820 // to go through the process of getting informed
9821 // by the application when it has finished drawing.
9822 if (w.mOrientationChanging) {
9823 if (DEBUG_ORIENTATION) Log.v(TAG,
9824 "Orientation start waiting for draw in "
9825 + w + ", surface " + w.mSurface);
9826 w.mDrawPending = true;
9827 w.mCommitDrawPending = false;
9828 w.mReadyToShow = false;
9829 if (w.mAppToken != null) {
9830 w.mAppToken.allDrawn = false;
9831 }
9832 }
Romain Guy06882f82009-06-10 13:36:04 -07009833 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 "Resizing window " + w + " to " + w.mFrame);
9835 mResizingWindows.add(w);
9836 } else if (w.mOrientationChanging) {
9837 if (!w.mDrawPending && !w.mCommitDrawPending) {
9838 if (DEBUG_ORIENTATION) Log.v(TAG,
9839 "Orientation not waiting for draw in "
9840 + w + ", surface " + w.mSurface);
9841 w.mOrientationChanging = false;
9842 }
9843 }
9844 }
9845
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009846 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009847 if (!w.mLastHidden) {
9848 //dump();
9849 w.mLastHidden = true;
9850 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009851 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009852 if (w.mSurface != null) {
9853 try {
9854 w.mSurface.hide();
9855 } catch (RuntimeException e) {
9856 Log.w(TAG, "Exception hiding surface in " + w);
9857 }
9858 }
9859 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9860 }
9861 // If we are waiting for this window to handle an
9862 // orientation change, well, it is hidden, so
9863 // doesn't really matter. Note that this does
9864 // introduce a potential glitch if the window
9865 // becomes unhidden before it has drawn for the
9866 // new orientation.
9867 if (w.mOrientationChanging) {
9868 w.mOrientationChanging = false;
9869 if (DEBUG_ORIENTATION) Log.v(TAG,
9870 "Orientation change skips hidden " + w);
9871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009872 } else if (w.mLastLayer != w.mAnimLayer
9873 || w.mLastAlpha != w.mShownAlpha
9874 || w.mLastDsDx != w.mDsDx
9875 || w.mLastDtDx != w.mDtDx
9876 || w.mLastDsDy != w.mDsDy
9877 || w.mLastDtDy != w.mDtDy
9878 || w.mLastHScale != w.mHScale
9879 || w.mLastVScale != w.mVScale
9880 || w.mLastHidden) {
9881 displayed = true;
9882 w.mLastAlpha = w.mShownAlpha;
9883 w.mLastLayer = w.mAnimLayer;
9884 w.mLastDsDx = w.mDsDx;
9885 w.mLastDtDx = w.mDtDx;
9886 w.mLastDsDy = w.mDsDy;
9887 w.mLastDtDy = w.mDtDy;
9888 w.mLastHScale = w.mHScale;
9889 w.mLastVScale = w.mVScale;
9890 if (SHOW_TRANSACTIONS) Log.i(
9891 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009892 + w.mShownAlpha + " layer=" + w.mAnimLayer
9893 + " matrix=[" + (w.mDsDx*w.mHScale)
9894 + "," + (w.mDtDx*w.mVScale)
9895 + "][" + (w.mDsDy*w.mHScale)
9896 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 if (w.mSurface != null) {
9898 try {
9899 w.mSurface.setAlpha(w.mShownAlpha);
9900 w.mSurface.setLayer(w.mAnimLayer);
9901 w.mSurface.setMatrix(
9902 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9903 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9904 } catch (RuntimeException e) {
9905 Log.w(TAG, "Error updating surface in " + w, e);
9906 if (!recoveringMemory) {
9907 reclaimSomeSurfaceMemoryLocked(w, "update");
9908 }
9909 }
9910 }
9911
9912 if (w.mLastHidden && !w.mDrawPending
9913 && !w.mCommitDrawPending
9914 && !w.mReadyToShow) {
9915 if (SHOW_TRANSACTIONS) Log.i(
9916 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9917 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9918 + " during relayout");
9919 if (showSurfaceRobustlyLocked(w)) {
9920 w.mHasDrawn = true;
9921 w.mLastHidden = false;
9922 } else {
9923 w.mOrientationChanging = false;
9924 }
9925 }
9926 if (w.mSurface != null) {
9927 w.mToken.hasVisible = true;
9928 }
9929 } else {
9930 displayed = true;
9931 }
9932
9933 if (displayed) {
9934 if (!covered) {
9935 if (attrs.width == LayoutParams.FILL_PARENT
9936 && attrs.height == LayoutParams.FILL_PARENT) {
9937 covered = true;
9938 }
9939 }
9940 if (w.mOrientationChanging) {
9941 if (w.mDrawPending || w.mCommitDrawPending) {
9942 orientationChangeComplete = false;
9943 if (DEBUG_ORIENTATION) Log.v(TAG,
9944 "Orientation continue waiting for draw in " + w);
9945 } else {
9946 w.mOrientationChanging = false;
9947 if (DEBUG_ORIENTATION) Log.v(TAG,
9948 "Orientation change complete in " + w);
9949 }
9950 }
9951 w.mToken.hasVisible = true;
9952 }
9953 } else if (w.mOrientationChanging) {
9954 if (DEBUG_ORIENTATION) Log.v(TAG,
9955 "Orientation change skips hidden " + w);
9956 w.mOrientationChanging = false;
9957 }
9958
9959 final boolean canBeSeen = w.isDisplayedLw();
9960
9961 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9962 focusDisplayed = true;
9963 }
9964
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009965 final boolean obscuredChanged = w.mObscured != obscured;
9966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009968 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009969 if (w.mSurface != null) {
9970 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9971 holdScreen = w.mSession;
9972 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009973 if (!syswin && w.mAttrs.screenBrightness >= 0
9974 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009975 screenBrightness = w.mAttrs.screenBrightness;
9976 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009977 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9978 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9979 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9980 syswin = true;
9981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009982 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009983
Dianne Hackborn25994b42009-09-04 14:21:19 -07009984 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9985 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009986 // This window completely covers everything behind it,
9987 // so we want to leave all of them as unblurred (for
9988 // performance reasons).
9989 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009990 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9991 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009992 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009993 obscured = true;
9994 if (mBackgroundFillerSurface == null) {
9995 try {
9996 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9997 0, dw, dh,
9998 PixelFormat.OPAQUE,
9999 Surface.FX_SURFACE_NORMAL);
10000 } catch (Exception e) {
10001 Log.e(TAG, "Exception creating filler surface", e);
10002 }
10003 }
10004 try {
10005 mBackgroundFillerSurface.setPosition(0, 0);
10006 mBackgroundFillerSurface.setSize(dw, dh);
10007 // Using the same layer as Dim because they will never be shown at the
10008 // same time.
10009 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10010 mBackgroundFillerSurface.show();
10011 } catch (RuntimeException e) {
10012 Log.e(TAG, "Exception showing filler surface");
10013 }
10014 backgroundFillerShown = true;
10015 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010016 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010017 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10018 if (localLOGV) Log.v(TAG, "Win " + w
10019 + ": blurring=" + blurring
10020 + " obscured=" + obscured
10021 + " displayed=" + displayed);
10022 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10023 if (!dimming) {
10024 //Log.i(TAG, "DIM BEHIND: " + w);
10025 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010026 if (mDimAnimator == null) {
10027 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010028 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010029 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010030 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010031 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010032 }
10033 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10034 if (!blurring) {
10035 //Log.i(TAG, "BLUR BEHIND: " + w);
10036 blurring = true;
10037 mBlurShown = true;
10038 if (mBlurSurface == null) {
10039 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10040 + mBlurSurface + ": CREATE");
10041 try {
Romain Guy06882f82009-06-10 13:36:04 -070010042 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010043 -1, 16, 16,
10044 PixelFormat.OPAQUE,
10045 Surface.FX_SURFACE_BLUR);
10046 } catch (Exception e) {
10047 Log.e(TAG, "Exception creating Blur surface", e);
10048 }
10049 }
10050 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10051 + mBlurSurface + ": SHOW pos=(0,0) (" +
10052 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10053 if (mBlurSurface != null) {
10054 mBlurSurface.setPosition(0, 0);
10055 mBlurSurface.setSize(dw, dh);
10056 try {
10057 mBlurSurface.show();
10058 } catch (RuntimeException e) {
10059 Log.w(TAG, "Failure showing blur surface", e);
10060 }
10061 }
10062 }
10063 mBlurSurface.setLayer(w.mAnimLayer-2);
10064 }
10065 }
10066 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010067
10068 if (obscuredChanged && mWallpaperTarget == w) {
10069 // This is the wallpaper target and its obscured state
10070 // changed... make sure the current wallaper's visibility
10071 // has been updated accordingly.
10072 updateWallpaperVisibilityLocked();
10073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010074 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010075
10076 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10077 mBackgroundFillerShown = false;
10078 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10079 try {
10080 mBackgroundFillerSurface.hide();
10081 } catch (RuntimeException e) {
10082 Log.e(TAG, "Exception hiding filler surface", e);
10083 }
10084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010086 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10087 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010088 }
Romain Guy06882f82009-06-10 13:36:04 -070010089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010090 if (!blurring && mBlurShown) {
10091 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10092 + ": HIDE");
10093 try {
10094 mBlurSurface.hide();
10095 } catch (IllegalArgumentException e) {
10096 Log.w(TAG, "Illegal argument exception hiding blur surface");
10097 }
10098 mBlurShown = false;
10099 }
10100
10101 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10102 } catch (RuntimeException e) {
10103 Log.e(TAG, "Unhandled exception in Window Manager", e);
10104 }
10105
10106 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010108 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10109 "With display frozen, orientationChangeComplete="
10110 + orientationChangeComplete);
10111 if (orientationChangeComplete) {
10112 if (mWindowsFreezingScreen) {
10113 mWindowsFreezingScreen = false;
10114 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10115 }
10116 if (mAppsFreezingScreen == 0) {
10117 stopFreezingDisplayLocked();
10118 }
10119 }
Romain Guy06882f82009-06-10 13:36:04 -070010120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 i = mResizingWindows.size();
10122 if (i > 0) {
10123 do {
10124 i--;
10125 WindowState win = mResizingWindows.get(i);
10126 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010127 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10128 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010129 win.mClient.resized(win.mFrame.width(),
10130 win.mFrame.height(), win.mLastContentInsets,
10131 win.mLastVisibleInsets, win.mDrawPending);
10132 win.mContentInsetsChanged = false;
10133 win.mVisibleInsetsChanged = false;
10134 } catch (RemoteException e) {
10135 win.mOrientationChanging = false;
10136 }
10137 } while (i > 0);
10138 mResizingWindows.clear();
10139 }
Romain Guy06882f82009-06-10 13:36:04 -070010140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010141 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010142 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010143 i = mDestroySurface.size();
10144 if (i > 0) {
10145 do {
10146 i--;
10147 WindowState win = mDestroySurface.get(i);
10148 win.mDestroying = false;
10149 if (mInputMethodWindow == win) {
10150 mInputMethodWindow = null;
10151 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010152 if (win == mWallpaperTarget) {
10153 wallpaperDestroyed = true;
10154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 win.destroySurfaceLocked();
10156 } while (i > 0);
10157 mDestroySurface.clear();
10158 }
10159
10160 // Time to remove any exiting tokens?
10161 for (i=mExitingTokens.size()-1; i>=0; i--) {
10162 WindowToken token = mExitingTokens.get(i);
10163 if (!token.hasVisible) {
10164 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010165 if (token.windowType == TYPE_WALLPAPER) {
10166 mWallpaperTokens.remove(token);
10167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010168 }
10169 }
10170
10171 // Time to remove any exiting applications?
10172 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10173 AppWindowToken token = mExitingAppTokens.get(i);
10174 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010175 // Make sure there is no animation running on this token,
10176 // so any windows associated with it will be removed as
10177 // soon as their animations are complete
10178 token.animation = null;
10179 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010180 mAppTokens.remove(token);
10181 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010182 if (mLastEnterAnimToken == token) {
10183 mLastEnterAnimToken = null;
10184 mLastEnterAnimParams = null;
10185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010186 }
10187 }
10188
Dianne Hackborna8f60182009-09-01 19:01:50 -070010189 boolean needRelayout = false;
10190
10191 if (!animating && mAppTransitionRunning) {
10192 // We have finished the animation of an app transition. To do
10193 // this, we have delayed a lot of operations like showing and
10194 // hiding apps, moving apps in Z-order, etc. The app token list
10195 // reflects the correct Z-order, but the window list may now
10196 // be out of sync with it. So here we will just rebuild the
10197 // entire app window list. Fun!
10198 mAppTransitionRunning = false;
10199 needRelayout = true;
10200 rebuildAppWindowListLocked();
10201 // Clear information about apps that were moving.
10202 mToBottomApps.clear();
10203 }
10204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010205 if (focusDisplayed) {
10206 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10207 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010208 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010209 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010210 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010211 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010212 requestAnimationLocked(0);
10213 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010214 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10215 }
10216 mQueue.setHoldScreenLocked(holdScreen != null);
10217 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10218 mPowerManager.setScreenBrightnessOverride(-1);
10219 } else {
10220 mPowerManager.setScreenBrightnessOverride((int)
10221 (screenBrightness * Power.BRIGHTNESS_ON));
10222 }
10223 if (holdScreen != mHoldingScreenOn) {
10224 mHoldingScreenOn = holdScreen;
10225 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10226 mH.sendMessage(m);
10227 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010228
10229 if (mTurnOnScreen) {
10230 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10231 LocalPowerManager.BUTTON_EVENT, true);
10232 mTurnOnScreen = false;
10233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010234 }
10235
10236 void requestAnimationLocked(long delay) {
10237 if (!mAnimationPending) {
10238 mAnimationPending = true;
10239 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10240 }
10241 }
Romain Guy06882f82009-06-10 13:36:04 -070010242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010243 /**
10244 * Have the surface flinger show a surface, robustly dealing with
10245 * error conditions. In particular, if there is not enough memory
10246 * to show the surface, then we will try to get rid of other surfaces
10247 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010248 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010249 * @return Returns true if the surface was successfully shown.
10250 */
10251 boolean showSurfaceRobustlyLocked(WindowState win) {
10252 try {
10253 if (win.mSurface != null) {
10254 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010255 if (win.mTurnOnScreen) {
10256 win.mTurnOnScreen = false;
10257 mTurnOnScreen = true;
10258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010259 }
10260 return true;
10261 } catch (RuntimeException e) {
10262 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10263 }
Romain Guy06882f82009-06-10 13:36:04 -070010264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010265 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010267 return false;
10268 }
Romain Guy06882f82009-06-10 13:36:04 -070010269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010270 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10271 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010273 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
10274 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276 if (mForceRemoves == null) {
10277 mForceRemoves = new ArrayList<WindowState>();
10278 }
Romain Guy06882f82009-06-10 13:36:04 -070010279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010280 long callingIdentity = Binder.clearCallingIdentity();
10281 try {
10282 // There was some problem... first, do a sanity check of the
10283 // window list to make sure we haven't left any dangling surfaces
10284 // around.
10285 int N = mWindows.size();
10286 boolean leakedSurface = false;
10287 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10288 for (int i=0; i<N; i++) {
10289 WindowState ws = (WindowState)mWindows.get(i);
10290 if (ws.mSurface != null) {
10291 if (!mSessions.contains(ws.mSession)) {
10292 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10293 + ws + " surface=" + ws.mSurface
10294 + " token=" + win.mToken
10295 + " pid=" + ws.mSession.mPid
10296 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010297 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 ws.mSurface = null;
10299 mForceRemoves.add(ws);
10300 i--;
10301 N--;
10302 leakedSurface = true;
10303 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10304 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10305 + ws + " surface=" + ws.mSurface
10306 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010307 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010308 ws.mSurface = null;
10309 leakedSurface = true;
10310 }
10311 }
10312 }
Romain Guy06882f82009-06-10 13:36:04 -070010313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010314 boolean killedApps = false;
10315 if (!leakedSurface) {
10316 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10317 SparseIntArray pidCandidates = new SparseIntArray();
10318 for (int i=0; i<N; i++) {
10319 WindowState ws = (WindowState)mWindows.get(i);
10320 if (ws.mSurface != null) {
10321 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10322 }
10323 }
10324 if (pidCandidates.size() > 0) {
10325 int[] pids = new int[pidCandidates.size()];
10326 for (int i=0; i<pids.length; i++) {
10327 pids[i] = pidCandidates.keyAt(i);
10328 }
10329 try {
10330 if (mActivityManager.killPidsForMemory(pids)) {
10331 killedApps = true;
10332 }
10333 } catch (RemoteException e) {
10334 }
10335 }
10336 }
Romain Guy06882f82009-06-10 13:36:04 -070010337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010338 if (leakedSurface || killedApps) {
10339 // We managed to reclaim some memory, so get rid of the trouble
10340 // surface and ask the app to request another one.
10341 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10342 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010343 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344 win.mSurface = null;
10345 }
Romain Guy06882f82009-06-10 13:36:04 -070010346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010347 try {
10348 win.mClient.dispatchGetNewSurface();
10349 } catch (RemoteException e) {
10350 }
10351 }
10352 } finally {
10353 Binder.restoreCallingIdentity(callingIdentity);
10354 }
10355 }
Romain Guy06882f82009-06-10 13:36:04 -070010356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010357 private boolean updateFocusedWindowLocked(int mode) {
10358 WindowState newFocus = computeFocusedWindowLocked();
10359 if (mCurrentFocus != newFocus) {
10360 // This check makes sure that we don't already have the focus
10361 // change message pending.
10362 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10363 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10364 if (localLOGV) Log.v(
10365 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10366 final WindowState oldFocus = mCurrentFocus;
10367 mCurrentFocus = newFocus;
10368 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010370 final WindowState imWindow = mInputMethodWindow;
10371 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010372 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010373 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010374 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10375 mLayoutNeeded = true;
10376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10378 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010379 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10380 // Client will do the layout, but we need to assign layers
10381 // for handleNewWindowLocked() below.
10382 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010383 }
10384 }
Romain Guy06882f82009-06-10 13:36:04 -070010385
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010386 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10387 mKeyWaiter.handleNewWindowLocked(newFocus);
10388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010389 return true;
10390 }
10391 return false;
10392 }
10393
10394 private WindowState computeFocusedWindowLocked() {
10395 WindowState result = null;
10396 WindowState win;
10397
10398 int i = mWindows.size() - 1;
10399 int nextAppIndex = mAppTokens.size()-1;
10400 WindowToken nextApp = nextAppIndex >= 0
10401 ? mAppTokens.get(nextAppIndex) : null;
10402
10403 while (i >= 0) {
10404 win = (WindowState)mWindows.get(i);
10405
10406 if (localLOGV || DEBUG_FOCUS) Log.v(
10407 TAG, "Looking for focus: " + i
10408 + " = " + win
10409 + ", flags=" + win.mAttrs.flags
10410 + ", canReceive=" + win.canReceiveKeys());
10411
10412 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010414 // If this window's application has been removed, just skip it.
10415 if (thisApp != null && thisApp.removed) {
10416 i--;
10417 continue;
10418 }
Romain Guy06882f82009-06-10 13:36:04 -070010419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 // If there is a focused app, don't allow focus to go to any
10421 // windows below it. If this is an application window, step
10422 // through the app tokens until we find its app.
10423 if (thisApp != null && nextApp != null && thisApp != nextApp
10424 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10425 int origAppIndex = nextAppIndex;
10426 while (nextAppIndex > 0) {
10427 if (nextApp == mFocusedApp) {
10428 // Whoops, we are below the focused app... no focus
10429 // for you!
10430 if (localLOGV || DEBUG_FOCUS) Log.v(
10431 TAG, "Reached focused app: " + mFocusedApp);
10432 return null;
10433 }
10434 nextAppIndex--;
10435 nextApp = mAppTokens.get(nextAppIndex);
10436 if (nextApp == thisApp) {
10437 break;
10438 }
10439 }
10440 if (thisApp != nextApp) {
10441 // Uh oh, the app token doesn't exist! This shouldn't
10442 // happen, but if it does we can get totally hosed...
10443 // so restart at the original app.
10444 nextAppIndex = origAppIndex;
10445 nextApp = mAppTokens.get(nextAppIndex);
10446 }
10447 }
10448
10449 // Dispatch to this window if it is wants key events.
10450 if (win.canReceiveKeys()) {
10451 if (DEBUG_FOCUS) Log.v(
10452 TAG, "Found focus @ " + i + " = " + win);
10453 result = win;
10454 break;
10455 }
10456
10457 i--;
10458 }
10459
10460 return result;
10461 }
10462
10463 private void startFreezingDisplayLocked() {
10464 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010465 // Freezing the display also suspends key event delivery, to
10466 // keep events from going astray while the display is reconfigured.
10467 // If someone has changed orientation again while the screen is
10468 // still frozen, the events will continue to be blocked while the
10469 // successive orientation change is processed. To prevent spurious
10470 // ANRs, we reset the event dispatch timeout in this case.
10471 synchronized (mKeyWaiter) {
10472 mKeyWaiter.mWasFrozen = true;
10473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010474 return;
10475 }
Romain Guy06882f82009-06-10 13:36:04 -070010476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479 long now = SystemClock.uptimeMillis();
10480 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10481 if (mFreezeGcPending != 0) {
10482 if (now > (mFreezeGcPending+1000)) {
10483 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10484 mH.removeMessages(H.FORCE_GC);
10485 Runtime.getRuntime().gc();
10486 mFreezeGcPending = now;
10487 }
10488 } else {
10489 mFreezeGcPending = now;
10490 }
Romain Guy06882f82009-06-10 13:36:04 -070010491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010492 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010493 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10494 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010495 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496 mAppTransitionReady = true;
10497 }
Romain Guy06882f82009-06-10 13:36:04 -070010498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010499 if (PROFILE_ORIENTATION) {
10500 File file = new File("/data/system/frozen");
10501 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10502 }
10503 Surface.freezeDisplay(0);
10504 }
Romain Guy06882f82009-06-10 13:36:04 -070010505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010506 private void stopFreezingDisplayLocked() {
10507 if (!mDisplayFrozen) {
10508 return;
10509 }
Romain Guy06882f82009-06-10 13:36:04 -070010510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 mDisplayFrozen = false;
10512 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10513 if (PROFILE_ORIENTATION) {
10514 Debug.stopMethodTracing();
10515 }
10516 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010517
Chris Tate2ad63a92009-03-25 17:36:48 -070010518 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10519 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 synchronized (mKeyWaiter) {
10521 mKeyWaiter.mWasFrozen = true;
10522 mKeyWaiter.notifyAll();
10523 }
10524
10525 // A little kludge: a lot could have happened while the
10526 // display was frozen, so now that we are coming back we
10527 // do a gc so that any remote references the system
10528 // processes holds on others can be released if they are
10529 // no longer needed.
10530 mH.removeMessages(H.FORCE_GC);
10531 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10532 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 mScreenFrozenLock.release();
10535 }
Romain Guy06882f82009-06-10 13:36:04 -070010536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010537 @Override
10538 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10539 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10540 != PackageManager.PERMISSION_GRANTED) {
10541 pw.println("Permission Denial: can't dump WindowManager from from pid="
10542 + Binder.getCallingPid()
10543 + ", uid=" + Binder.getCallingUid());
10544 return;
10545 }
Romain Guy06882f82009-06-10 13:36:04 -070010546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 synchronized(mWindowMap) {
10548 pw.println("Current Window Manager state:");
10549 for (int i=mWindows.size()-1; i>=0; i--) {
10550 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010551 pw.print(" Window #"); pw.print(i); pw.print(' ');
10552 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 w.dump(pw, " ");
10554 }
10555 if (mInputMethodDialogs.size() > 0) {
10556 pw.println(" ");
10557 pw.println(" Input method dialogs:");
10558 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10559 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010560 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010561 }
10562 }
10563 if (mPendingRemove.size() > 0) {
10564 pw.println(" ");
10565 pw.println(" Remove pending for:");
10566 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10567 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010568 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10569 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 w.dump(pw, " ");
10571 }
10572 }
10573 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10574 pw.println(" ");
10575 pw.println(" Windows force removing:");
10576 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10577 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010578 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10579 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010580 w.dump(pw, " ");
10581 }
10582 }
10583 if (mDestroySurface.size() > 0) {
10584 pw.println(" ");
10585 pw.println(" Windows waiting to destroy their surface:");
10586 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10587 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010588 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10589 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010590 w.dump(pw, " ");
10591 }
10592 }
10593 if (mLosingFocus.size() > 0) {
10594 pw.println(" ");
10595 pw.println(" Windows losing focus:");
10596 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10597 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010598 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10599 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010600 w.dump(pw, " ");
10601 }
10602 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010603 if (mResizingWindows.size() > 0) {
10604 pw.println(" ");
10605 pw.println(" Windows waiting to resize:");
10606 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10607 WindowState w = mResizingWindows.get(i);
10608 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10609 pw.print(w); pw.println(":");
10610 w.dump(pw, " ");
10611 }
10612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010613 if (mSessions.size() > 0) {
10614 pw.println(" ");
10615 pw.println(" All active sessions:");
10616 Iterator<Session> it = mSessions.iterator();
10617 while (it.hasNext()) {
10618 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010619 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 s.dump(pw, " ");
10621 }
10622 }
10623 if (mTokenMap.size() > 0) {
10624 pw.println(" ");
10625 pw.println(" All tokens:");
10626 Iterator<WindowToken> it = mTokenMap.values().iterator();
10627 while (it.hasNext()) {
10628 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010629 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 token.dump(pw, " ");
10631 }
10632 }
10633 if (mTokenList.size() > 0) {
10634 pw.println(" ");
10635 pw.println(" Window token list:");
10636 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010637 pw.print(" #"); pw.print(i); pw.print(": ");
10638 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 }
10640 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010641 if (mWallpaperTokens.size() > 0) {
10642 pw.println(" ");
10643 pw.println(" Wallpaper tokens:");
10644 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10645 WindowToken token = mWallpaperTokens.get(i);
10646 pw.print(" Wallpaper #"); pw.print(i);
10647 pw.print(' '); pw.print(token); pw.println(':');
10648 token.dump(pw, " ");
10649 }
10650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 if (mAppTokens.size() > 0) {
10652 pw.println(" ");
10653 pw.println(" Application tokens in Z order:");
10654 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010655 pw.print(" App #"); pw.print(i); pw.print(": ");
10656 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010657 }
10658 }
10659 if (mFinishedStarting.size() > 0) {
10660 pw.println(" ");
10661 pw.println(" Finishing start of application tokens:");
10662 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10663 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010664 pw.print(" Finished Starting #"); pw.print(i);
10665 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 token.dump(pw, " ");
10667 }
10668 }
10669 if (mExitingTokens.size() > 0) {
10670 pw.println(" ");
10671 pw.println(" Exiting tokens:");
10672 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10673 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010674 pw.print(" Exiting #"); pw.print(i);
10675 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010676 token.dump(pw, " ");
10677 }
10678 }
10679 if (mExitingAppTokens.size() > 0) {
10680 pw.println(" ");
10681 pw.println(" Exiting application tokens:");
10682 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10683 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010684 pw.print(" Exiting App #"); pw.print(i);
10685 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 token.dump(pw, " ");
10687 }
10688 }
10689 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010690 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10691 pw.print(" mLastFocus="); pw.println(mLastFocus);
10692 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10693 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10694 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010695 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010696 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10697 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10698 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10699 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010700 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10701 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10702 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10703 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10704 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010705 if (mDimAnimator != null) {
10706 mDimAnimator.printTo(pw);
10707 } else {
10708 pw.print( " no DimAnimator ");
10709 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010710 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010711 pw.print(mInputMethodAnimLayerAdjustment);
10712 pw.print(" mWallpaperAnimLayerAdjustment=");
10713 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010714 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10715 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010716 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10717 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10718 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10719 pw.print(" mRotation="); pw.print(mRotation);
10720 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10721 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10722 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10723 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10724 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10725 pw.print(" mNextAppTransition=0x");
10726 pw.print(Integer.toHexString(mNextAppTransition));
10727 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010728 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010729 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010730 if (mNextAppTransitionPackage != null) {
10731 pw.print(" mNextAppTransitionPackage=");
10732 pw.print(mNextAppTransitionPackage);
10733 pw.print(", mNextAppTransitionEnter=0x");
10734 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10735 pw.print(", mNextAppTransitionExit=0x");
10736 pw.print(Integer.toHexString(mNextAppTransitionExit));
10737 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010738 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10739 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010740 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10741 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10742 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10743 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010744 if (mOpeningApps.size() > 0) {
10745 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10746 }
10747 if (mClosingApps.size() > 0) {
10748 pw.print(" mClosingApps="); pw.println(mClosingApps);
10749 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010750 if (mToTopApps.size() > 0) {
10751 pw.print(" mToTopApps="); pw.println(mToTopApps);
10752 }
10753 if (mToBottomApps.size() > 0) {
10754 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10755 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010756 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10757 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010758 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010759 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10760 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10761 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10762 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10763 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10764 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010765 }
10766 }
10767
10768 public void monitor() {
10769 synchronized (mWindowMap) { }
10770 synchronized (mKeyguardDisabled) { }
10771 synchronized (mKeyWaiter) { }
10772 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010773
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010774 public void virtualKeyFeedback(KeyEvent event) {
10775 mPolicy.keyFeedbackFromInput(event);
10776 }
10777
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010778 /**
10779 * DimAnimator class that controls the dim animation. This holds the surface and
10780 * all state used for dim animation.
10781 */
10782 private static class DimAnimator {
10783 Surface mDimSurface;
10784 boolean mDimShown = false;
10785 float mDimCurrentAlpha;
10786 float mDimTargetAlpha;
10787 float mDimDeltaPerMs;
10788 long mLastDimAnimTime;
10789
10790 DimAnimator (SurfaceSession session) {
10791 if (mDimSurface == null) {
10792 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10793 + mDimSurface + ": CREATE");
10794 try {
10795 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10796 Surface.FX_SURFACE_DIM);
10797 } catch (Exception e) {
10798 Log.e(TAG, "Exception creating Dim surface", e);
10799 }
10800 }
10801 }
10802
10803 /**
10804 * Show the dim surface.
10805 */
10806 void show(int dw, int dh) {
10807 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10808 dw + "x" + dh + ")");
10809 mDimShown = true;
10810 try {
10811 mDimSurface.setPosition(0, 0);
10812 mDimSurface.setSize(dw, dh);
10813 mDimSurface.show();
10814 } catch (RuntimeException e) {
10815 Log.w(TAG, "Failure showing dim surface", e);
10816 }
10817 }
10818
10819 /**
10820 * Set's the dim surface's layer and update dim parameters that will be used in
10821 * {@link updateSurface} after all windows are examined.
10822 */
10823 void updateParameters(WindowState w, long currentTime) {
10824 mDimSurface.setLayer(w.mAnimLayer-1);
10825
10826 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010827 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10828 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010829 if (mDimTargetAlpha != target) {
10830 // If the desired dim level has changed, then
10831 // start an animation to it.
10832 mLastDimAnimTime = currentTime;
10833 long duration = (w.mAnimating && w.mAnimation != null)
10834 ? w.mAnimation.computeDurationHint()
10835 : DEFAULT_DIM_DURATION;
10836 if (target > mDimTargetAlpha) {
10837 // This is happening behind the activity UI,
10838 // so we can make it run a little longer to
10839 // give a stronger impression without disrupting
10840 // the user.
10841 duration *= DIM_DURATION_MULTIPLIER;
10842 }
10843 if (duration < 1) {
10844 // Don't divide by zero
10845 duration = 1;
10846 }
10847 mDimTargetAlpha = target;
10848 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10849 }
10850 }
10851
10852 /**
10853 * Updating the surface's alpha. Returns true if the animation continues, or returns
10854 * false when the animation is finished and the dim surface is hidden.
10855 */
10856 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10857 if (!dimming) {
10858 if (mDimTargetAlpha != 0) {
10859 mLastDimAnimTime = currentTime;
10860 mDimTargetAlpha = 0;
10861 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10862 }
10863 }
10864
10865 boolean animating = false;
10866 if (mLastDimAnimTime != 0) {
10867 mDimCurrentAlpha += mDimDeltaPerMs
10868 * (currentTime-mLastDimAnimTime);
10869 boolean more = true;
10870 if (displayFrozen) {
10871 // If the display is frozen, there is no reason to animate.
10872 more = false;
10873 } else if (mDimDeltaPerMs > 0) {
10874 if (mDimCurrentAlpha > mDimTargetAlpha) {
10875 more = false;
10876 }
10877 } else if (mDimDeltaPerMs < 0) {
10878 if (mDimCurrentAlpha < mDimTargetAlpha) {
10879 more = false;
10880 }
10881 } else {
10882 more = false;
10883 }
10884
10885 // Do we need to continue animating?
10886 if (more) {
10887 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10888 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10889 mLastDimAnimTime = currentTime;
10890 mDimSurface.setAlpha(mDimCurrentAlpha);
10891 animating = true;
10892 } else {
10893 mDimCurrentAlpha = mDimTargetAlpha;
10894 mLastDimAnimTime = 0;
10895 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10896 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10897 mDimSurface.setAlpha(mDimCurrentAlpha);
10898 if (!dimming) {
10899 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10900 + ": HIDE");
10901 try {
10902 mDimSurface.hide();
10903 } catch (RuntimeException e) {
10904 Log.w(TAG, "Illegal argument exception hiding dim surface");
10905 }
10906 mDimShown = false;
10907 }
10908 }
10909 }
10910 return animating;
10911 }
10912
10913 public void printTo(PrintWriter pw) {
10914 pw.print(" mDimShown="); pw.print(mDimShown);
10915 pw.print(" current="); pw.print(mDimCurrentAlpha);
10916 pw.print(" target="); pw.print(mDimTargetAlpha);
10917 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10918 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10919 }
10920 }
10921
10922 /**
10923 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10924 * This is used for opening/closing transition for apps in compatible mode.
10925 */
10926 private static class FadeInOutAnimation extends Animation {
10927 int mWidth;
10928 boolean mFadeIn;
10929
10930 public FadeInOutAnimation(boolean fadeIn) {
10931 setInterpolator(new AccelerateInterpolator());
10932 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10933 mFadeIn = fadeIn;
10934 }
10935
10936 @Override
10937 protected void applyTransformation(float interpolatedTime, Transformation t) {
10938 float x = interpolatedTime;
10939 if (!mFadeIn) {
10940 x = 1.0f - x; // reverse the interpolation for fade out
10941 }
10942 if (x < 0.5) {
10943 // move the window out of the screen.
10944 t.getMatrix().setTranslate(mWidth, 0);
10945 } else {
10946 t.getMatrix().setTranslate(0, 0);// show
10947 t.setAlpha((x - 0.5f) * 2);
10948 }
10949 }
10950
10951 @Override
10952 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10953 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10954 mWidth = width;
10955 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010956
10957 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010958 public int getZAdjustment() {
10959 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010960 }
10961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010962}