blob: ea0b778df8cf25eb4c541477a099561a3bbf2f73 [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 /** How long to wait for subsequent key repeats, in milliseconds */
156 static final int KEY_REPEAT_DELAY = 50;
157
158 /** How much to multiply the policy's type layer, to reserve room
159 * for multiple windows of the same type and Z-ordering adjustment
160 * with TYPE_LAYER_OFFSET. */
161 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
164 * or below others in the same layer. */
165 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 /** How much to increment the layer for each window, to reserve room
168 * for effect surfaces between them.
169 */
170 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 /** The maximum length we will accept for a loaded animation duration:
173 * this is 10 seconds.
174 */
175 static final int MAX_ANIMATION_DURATION = 10*1000;
176
177 /** Amount of time (in milliseconds) to animate the dim surface from one
178 * value to another, when no window animation is driving it.
179 */
180 static final int DEFAULT_DIM_DURATION = 200;
181
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700182 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
183 * compatible windows.
184 */
185 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 /** Adjustment to time to perform a dim, to make it more dramatic.
188 */
189 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700190
Dianne Hackborncfaef692009-06-15 14:24:44 -0700191 static final int INJECT_FAILED = 0;
192 static final int INJECT_SUCCEEDED = 1;
193 static final int INJECT_NO_PERMISSION = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 static final int UPDATE_FOCUS_NORMAL = 0;
196 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
197 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
198 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700199
Michael Chane96440f2009-05-06 10:27:36 -0700200 /** The minimum time between dispatching touch events. */
201 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
202
203 // Last touch event time
204 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700205
Michael Chane96440f2009-05-06 10:27:36 -0700206 // Last touch event type
207 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700208
Michael Chane96440f2009-05-06 10:27:36 -0700209 // Time to wait before calling useractivity again. This saves CPU usage
210 // when we get a flood of touch events.
211 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
212
213 // Last time we call user activity
214 long mLastUserActivityCallTime = 0;
215
Romain Guy06882f82009-06-10 13:36:04 -0700216 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700217 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700220 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
222 /**
223 * Condition waited on by {@link #reenableKeyguard} to know the call to
224 * the window policy has finished.
Mike Lockwood983ee092009-11-22 01:42:24 -0500225 * This is set to true only if mKeyguardTokenWatcher.acquired() has
226 * actually disabled the keyguard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 */
Mike Lockwood983ee092009-11-22 01:42:24 -0500228 private boolean mKeyguardDisabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229
Mike Lockwood983ee092009-11-22 01:42:24 -0500230 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher(
231 new Handler(), "WindowManagerService.mKeyguardTokenWatcher") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 public void acquired() {
233 mPolicy.enableKeyguard(false);
Mike Lockwood983ee092009-11-22 01:42:24 -0500234 mKeyguardDisabled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 }
236 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700237 mPolicy.enableKeyguard(true);
Mike Lockwood983ee092009-11-22 01:42:24 -0500238 synchronized (mKeyguardTokenWatcher) {
239 mKeyguardDisabled = false;
240 mKeyguardTokenWatcher.notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
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;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700311 /**
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;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700317 /**
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>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700424 // If non-null, this is the currently visible window that is associated
425 // with the wallpaper.
426 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700427 // If non-null, we are in the middle of animating from one wallpaper target
428 // to another, and this is the lower one in Z-order.
429 WindowState mLowerWallpaperTarget = null;
430 // If non-null, we are in the middle of animating from one wallpaper target
431 // to another, and this is the higher one in Z-order.
432 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700433 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700434 float mLastWallpaperX = -1;
435 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800436 float mLastWallpaperXStep = -1;
437 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800438 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700439 // This is set when we are waiting for a wallpaper to tell us it is done
440 // changing its scroll position.
441 WindowState mWaitingOnWallpaper;
442 // The last time we had a timeout when waiting for a wallpaper.
443 long mLastWallpaperTimeoutTime;
444 // We give a wallpaper up to 150ms to finish scrolling.
445 static final long WALLPAPER_TIMEOUT = 150;
446 // Time we wait after a timeout before trying to wait again.
447 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 AppWindowToken mFocusedApp = null;
450
451 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 float mWindowAnimationScale = 1.0f;
454 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 final KeyWaiter mKeyWaiter = new KeyWaiter();
457 final KeyQ mQueue;
458 final InputDispatcherThread mInputThread;
459
460 // Who is holding the screen on.
461 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700462
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700463 boolean mTurnOnScreen;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 /**
466 * Whether the UI is currently running in touch mode (not showing
467 * navigational focus because the user is directly pressing the screen).
468 */
469 boolean mInTouchMode = false;
470
471 private ViewServer mViewServer;
472
473 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700474
Dianne Hackbornc485a602009-03-24 22:39:49 -0700475 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700476 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700477
478 // The frame use to limit the size of the app running in compatibility mode.
479 Rect mCompatibleScreenFrame = new Rect();
480 // The surface used to fill the outer rim of the app running in compatibility mode.
481 Surface mBackgroundFillerSurface = null;
482 boolean mBackgroundFillerShown = false;
483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 public static WindowManagerService main(Context context,
485 PowerManagerService pm, boolean haveInputMethods) {
486 WMThread thr = new WMThread(context, pm, haveInputMethods);
487 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 synchronized (thr) {
490 while (thr.mService == null) {
491 try {
492 thr.wait();
493 } catch (InterruptedException e) {
494 }
495 }
496 }
Romain Guy06882f82009-06-10 13:36:04 -0700497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 return thr.mService;
499 }
Romain Guy06882f82009-06-10 13:36:04 -0700500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 static class WMThread extends Thread {
502 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 private final Context mContext;
505 private final PowerManagerService mPM;
506 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 public WMThread(Context context, PowerManagerService pm,
509 boolean haveInputMethods) {
510 super("WindowManager");
511 mContext = context;
512 mPM = pm;
513 mHaveInputMethods = haveInputMethods;
514 }
Romain Guy06882f82009-06-10 13:36:04 -0700515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 public void run() {
517 Looper.prepare();
518 WindowManagerService s = new WindowManagerService(mContext, mPM,
519 mHaveInputMethods);
520 android.os.Process.setThreadPriority(
521 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 synchronized (this) {
524 mService = s;
525 notifyAll();
526 }
Romain Guy06882f82009-06-10 13:36:04 -0700527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 Looper.loop();
529 }
530 }
531
532 static class PolicyThread extends Thread {
533 private final WindowManagerPolicy mPolicy;
534 private final WindowManagerService mService;
535 private final Context mContext;
536 private final PowerManagerService mPM;
537 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 public PolicyThread(WindowManagerPolicy policy,
540 WindowManagerService service, Context context,
541 PowerManagerService pm) {
542 super("WindowManagerPolicy");
543 mPolicy = policy;
544 mService = service;
545 mContext = context;
546 mPM = pm;
547 }
Romain Guy06882f82009-06-10 13:36:04 -0700548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 public void run() {
550 Looper.prepare();
551 //Looper.myLooper().setMessageLogging(new LogPrinter(
552 // Log.VERBOSE, "WindowManagerPolicy"));
553 android.os.Process.setThreadPriority(
554 android.os.Process.THREAD_PRIORITY_FOREGROUND);
555 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 synchronized (this) {
558 mRunning = true;
559 notifyAll();
560 }
Romain Guy06882f82009-06-10 13:36:04 -0700561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 Looper.loop();
563 }
564 }
565
566 private WindowManagerService(Context context, PowerManagerService pm,
567 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700568 if (MEASURE_LATENCY) {
569 lt = new LatencyTimer(100, 1000);
570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 mContext = context;
573 mHaveInputMethods = haveInputMethods;
574 mLimitedAlphaCompositing = context.getResources().getBoolean(
575 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 mPowerManager = pm;
578 mPowerManager.setPolicy(mPolicy);
579 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
580 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
581 "SCREEN_FROZEN");
582 mScreenFrozenLock.setReferenceCounted(false);
583
584 mActivityManager = ActivityManagerNative.getDefault();
585 mBatteryStats = BatteryStatsService.getService();
586
587 // Get persisted window scale setting
588 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
589 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
590 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
591 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700592
Michael Chan9f028e62009-08-04 17:37:46 -0700593 int max_events_per_sec = 35;
594 try {
595 max_events_per_sec = Integer.parseInt(SystemProperties
596 .get("windowsmgr.max_events_per_sec"));
597 if (max_events_per_sec < 1) {
598 max_events_per_sec = 35;
599 }
600 } catch (NumberFormatException e) {
601 }
602 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 mQueue = new KeyQ();
605
606 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
609 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 synchronized (thr) {
612 while (!thr.mRunning) {
613 try {
614 thr.wait();
615 } catch (InterruptedException e) {
616 }
617 }
618 }
Romain Guy06882f82009-06-10 13:36:04 -0700619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 // Add ourself to the Watchdog monitors.
623 Watchdog.getInstance().addMonitor(this);
624 }
625
626 @Override
627 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
628 throws RemoteException {
629 try {
630 return super.onTransact(code, data, reply, flags);
631 } catch (RuntimeException e) {
632 // The window manager only throws security exceptions, so let's
633 // log all others.
634 if (!(e instanceof SecurityException)) {
635 Log.e(TAG, "Window Manager Crash", e);
636 }
637 throw e;
638 }
639 }
640
641 private void placeWindowAfter(Object pos, WindowState window) {
642 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700643 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 TAG, "Adding window " + window + " at "
645 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
646 mWindows.add(i+1, window);
647 }
648
649 private void placeWindowBefore(Object pos, WindowState window) {
650 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700651 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 TAG, "Adding window " + window + " at "
653 + i + " of " + mWindows.size() + " (before " + pos + ")");
654 mWindows.add(i, window);
655 }
656
657 //This method finds out the index of a window that has the same app token as
658 //win. used for z ordering the windows in mWindows
659 private int findIdxBasedOnAppTokens(WindowState win) {
660 //use a local variable to cache mWindows
661 ArrayList localmWindows = mWindows;
662 int jmax = localmWindows.size();
663 if(jmax == 0) {
664 return -1;
665 }
666 for(int j = (jmax-1); j >= 0; j--) {
667 WindowState wentry = (WindowState)localmWindows.get(j);
668 if(wentry.mAppToken == win.mAppToken) {
669 return j;
670 }
671 }
672 return -1;
673 }
Romain Guy06882f82009-06-10 13:36:04 -0700674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
676 final IWindow client = win.mClient;
677 final WindowToken token = win.mToken;
678 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 final int N = localmWindows.size();
681 final WindowState attached = win.mAttachedWindow;
682 int i;
683 if (attached == null) {
684 int tokenWindowsPos = token.windows.size();
685 if (token.appWindowToken != null) {
686 int index = tokenWindowsPos-1;
687 if (index >= 0) {
688 // If this application has existing windows, we
689 // simply place the new window on top of them... but
690 // keep the starting window on top.
691 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
692 // Base windows go behind everything else.
693 placeWindowBefore(token.windows.get(0), win);
694 tokenWindowsPos = 0;
695 } else {
696 AppWindowToken atoken = win.mAppToken;
697 if (atoken != null &&
698 token.windows.get(index) == atoken.startingWindow) {
699 placeWindowBefore(token.windows.get(index), win);
700 tokenWindowsPos--;
701 } else {
702 int newIdx = findIdxBasedOnAppTokens(win);
703 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700704 //there is a window above this one associated with the same
705 //apptoken note that the window could be a floating window
706 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700708 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
709 TAG, "Adding window " + win + " at "
710 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714 }
715 } else {
716 if (localLOGV) Log.v(
717 TAG, "Figuring out where to add app window "
718 + client.asBinder() + " (token=" + token + ")");
719 // Figure out where the window should go, based on the
720 // order of applications.
721 final int NA = mAppTokens.size();
722 Object pos = null;
723 for (i=NA-1; i>=0; i--) {
724 AppWindowToken t = mAppTokens.get(i);
725 if (t == token) {
726 i--;
727 break;
728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800729
Dianne Hackborna8f60182009-09-01 19:01:50 -0700730 // We haven't reached the token yet; if this token
731 // is not going to the bottom and has windows, we can
732 // use it as an anchor for when we do reach the token.
733 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 pos = t.windows.get(0);
735 }
736 }
737 // We now know the index into the apps. If we found
738 // an app window above, that gives us the position; else
739 // we need to look some more.
740 if (pos != null) {
741 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700742 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 mTokenMap.get(((WindowState)pos).mClient.asBinder());
744 if (atoken != null) {
745 final int NC = atoken.windows.size();
746 if (NC > 0) {
747 WindowState bottom = atoken.windows.get(0);
748 if (bottom.mSubLayer < 0) {
749 pos = bottom;
750 }
751 }
752 }
753 placeWindowBefore(pos, win);
754 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700755 // Continue looking down until we find the first
756 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 while (i >= 0) {
758 AppWindowToken t = mAppTokens.get(i);
759 final int NW = t.windows.size();
760 if (NW > 0) {
761 pos = t.windows.get(NW-1);
762 break;
763 }
764 i--;
765 }
766 if (pos != null) {
767 // Move in front of any windows attached to this
768 // one.
769 WindowToken atoken =
770 mTokenMap.get(((WindowState)pos).mClient.asBinder());
771 if (atoken != null) {
772 final int NC = atoken.windows.size();
773 if (NC > 0) {
774 WindowState top = atoken.windows.get(NC-1);
775 if (top.mSubLayer >= 0) {
776 pos = top;
777 }
778 }
779 }
780 placeWindowAfter(pos, win);
781 } else {
782 // Just search for the start of this layer.
783 final int myLayer = win.mBaseLayer;
784 for (i=0; i<N; i++) {
785 WindowState w = (WindowState)localmWindows.get(i);
786 if (w.mBaseLayer > myLayer) {
787 break;
788 }
789 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700790 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
791 TAG, "Adding window " + win + " at "
792 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 localmWindows.add(i, win);
794 }
795 }
796 }
797 } else {
798 // Figure out where window should go, based on layer.
799 final int myLayer = win.mBaseLayer;
800 for (i=N-1; i>=0; i--) {
801 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
802 i++;
803 break;
804 }
805 }
806 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700807 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
808 TAG, "Adding window " + win + " at "
809 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 localmWindows.add(i, win);
811 }
812 if (addToToken) {
813 token.windows.add(tokenWindowsPos, win);
814 }
815
816 } else {
817 // Figure out this window's ordering relative to the window
818 // it is attached to.
819 final int NA = token.windows.size();
820 final int sublayer = win.mSubLayer;
821 int largestSublayer = Integer.MIN_VALUE;
822 WindowState windowWithLargestSublayer = null;
823 for (i=0; i<NA; i++) {
824 WindowState w = token.windows.get(i);
825 final int wSublayer = w.mSubLayer;
826 if (wSublayer >= largestSublayer) {
827 largestSublayer = wSublayer;
828 windowWithLargestSublayer = w;
829 }
830 if (sublayer < 0) {
831 // For negative sublayers, we go below all windows
832 // in the same sublayer.
833 if (wSublayer >= sublayer) {
834 if (addToToken) {
835 token.windows.add(i, win);
836 }
837 placeWindowBefore(
838 wSublayer >= 0 ? attached : w, win);
839 break;
840 }
841 } else {
842 // For positive sublayers, we go above all windows
843 // in the same sublayer.
844 if (wSublayer > sublayer) {
845 if (addToToken) {
846 token.windows.add(i, win);
847 }
848 placeWindowBefore(w, win);
849 break;
850 }
851 }
852 }
853 if (i >= NA) {
854 if (addToToken) {
855 token.windows.add(win);
856 }
857 if (sublayer < 0) {
858 placeWindowBefore(attached, win);
859 } else {
860 placeWindowAfter(largestSublayer >= 0
861 ? windowWithLargestSublayer
862 : attached,
863 win);
864 }
865 }
866 }
Romain Guy06882f82009-06-10 13:36:04 -0700867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 if (win.mAppToken != null && addToToken) {
869 win.mAppToken.allAppWindows.add(win);
870 }
871 }
Romain Guy06882f82009-06-10 13:36:04 -0700872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 static boolean canBeImeTarget(WindowState w) {
874 final int fl = w.mAttrs.flags
875 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
876 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
877 return w.isVisibleOrAdding();
878 }
879 return false;
880 }
Romain Guy06882f82009-06-10 13:36:04 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
883 final ArrayList localmWindows = mWindows;
884 final int N = localmWindows.size();
885 WindowState w = null;
886 int i = N;
887 while (i > 0) {
888 i--;
889 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
892 // + Integer.toHexString(w.mAttrs.flags));
893 if (canBeImeTarget(w)) {
894 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 // Yet more tricksyness! If this window is a "starting"
897 // window, we do actually want to be on top of it, but
898 // it is not -really- where input will go. So if the caller
899 // is not actually looking to move the IME, look down below
900 // for a real window to target...
901 if (!willMove
902 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
903 && i > 0) {
904 WindowState wb = (WindowState)localmWindows.get(i-1);
905 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
906 i--;
907 w = wb;
908 }
909 }
910 break;
911 }
912 }
Romain Guy06882f82009-06-10 13:36:04 -0700913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
917 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 if (willMove && w != null) {
920 final WindowState curTarget = mInputMethodTarget;
921 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 // Now some fun for dealing with window animations that
924 // modify the Z order. We need to look at all windows below
925 // the current target that are in this app, finding the highest
926 // visible one in layering.
927 AppWindowToken token = curTarget.mAppToken;
928 WindowState highestTarget = null;
929 int highestPos = 0;
930 if (token.animating || token.animation != null) {
931 int pos = 0;
932 pos = localmWindows.indexOf(curTarget);
933 while (pos >= 0) {
934 WindowState win = (WindowState)localmWindows.get(pos);
935 if (win.mAppToken != token) {
936 break;
937 }
938 if (!win.mRemoved) {
939 if (highestTarget == null || win.mAnimLayer >
940 highestTarget.mAnimLayer) {
941 highestTarget = win;
942 highestPos = pos;
943 }
944 }
945 pos--;
946 }
947 }
Romain Guy06882f82009-06-10 13:36:04 -0700948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700950 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 + mNextAppTransition + " " + highestTarget
952 + " animating=" + highestTarget.isAnimating()
953 + " layer=" + highestTarget.mAnimLayer
954 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700955
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700956 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 // If we are currently setting up for an animation,
958 // hold everything until we can find out what will happen.
959 mInputMethodTargetWaitingAnim = true;
960 mInputMethodTarget = highestTarget;
961 return highestPos + 1;
962 } else if (highestTarget.isAnimating() &&
963 highestTarget.mAnimLayer > w.mAnimLayer) {
964 // If the window we are currently targeting is involved
965 // with an animation, and it is on top of the next target
966 // we will be over, then hold off on moving until
967 // that is done.
968 mInputMethodTarget = highestTarget;
969 return highestPos + 1;
970 }
971 }
972 }
973 }
Romain Guy06882f82009-06-10 13:36:04 -0700974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 //Log.i(TAG, "Placing input method @" + (i+1));
976 if (w != null) {
977 if (willMove) {
978 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700979 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
981 + mInputMethodTarget + " to " + w, e);
982 mInputMethodTarget = w;
983 if (w.mAppToken != null) {
984 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
985 } else {
986 setInputMethodAnimLayerAdjustment(0);
987 }
988 }
989 return i+1;
990 }
991 if (willMove) {
992 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700993 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
995 + mInputMethodTarget + " to null", e);
996 mInputMethodTarget = null;
997 setInputMethodAnimLayerAdjustment(0);
998 }
999 return -1;
1000 }
Romain Guy06882f82009-06-10 13:36:04 -07001001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 void addInputMethodWindowToListLocked(WindowState win) {
1003 int pos = findDesiredInputMethodWindowIndexLocked(true);
1004 if (pos >= 0) {
1005 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001006 if (DEBUG_WINDOW_MOVEMENT) Log.v(
1007 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 mWindows.add(pos, win);
1009 moveInputMethodDialogsLocked(pos+1);
1010 return;
1011 }
1012 win.mTargetAppToken = null;
1013 addWindowToListInOrderLocked(win, true);
1014 moveInputMethodDialogsLocked(pos);
1015 }
Romain Guy06882f82009-06-10 13:36:04 -07001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 void setInputMethodAnimLayerAdjustment(int adj) {
1018 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1019 mInputMethodAnimLayerAdjustment = adj;
1020 WindowState imw = mInputMethodWindow;
1021 if (imw != null) {
1022 imw.mAnimLayer = imw.mLayer + adj;
1023 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1024 + " anim layer: " + imw.mAnimLayer);
1025 int wi = imw.mChildWindows.size();
1026 while (wi > 0) {
1027 wi--;
1028 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1029 cw.mAnimLayer = cw.mLayer + adj;
1030 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1031 + " anim layer: " + cw.mAnimLayer);
1032 }
1033 }
1034 int di = mInputMethodDialogs.size();
1035 while (di > 0) {
1036 di --;
1037 imw = mInputMethodDialogs.get(di);
1038 imw.mAnimLayer = imw.mLayer + adj;
1039 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1040 + " anim layer: " + imw.mAnimLayer);
1041 }
1042 }
Romain Guy06882f82009-06-10 13:36:04 -07001043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1045 int wpos = mWindows.indexOf(win);
1046 if (wpos >= 0) {
1047 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001048 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 mWindows.remove(wpos);
1050 int NC = win.mChildWindows.size();
1051 while (NC > 0) {
1052 NC--;
1053 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1054 int cpos = mWindows.indexOf(cw);
1055 if (cpos >= 0) {
1056 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001057 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1058 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 mWindows.remove(cpos);
1060 }
1061 }
1062 }
1063 return interestingPos;
1064 }
Romain Guy06882f82009-06-10 13:36:04 -07001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 private void reAddWindowToListInOrderLocked(WindowState win) {
1067 addWindowToListInOrderLocked(win, false);
1068 // This is a hack to get all of the child windows added as well
1069 // at the right position. Child windows should be rare and
1070 // this case should be rare, so it shouldn't be that big a deal.
1071 int wpos = mWindows.indexOf(win);
1072 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001073 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1074 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 mWindows.remove(wpos);
1076 reAddWindowLocked(wpos, win);
1077 }
1078 }
Romain Guy06882f82009-06-10 13:36:04 -07001079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 void logWindowList(String prefix) {
1081 int N = mWindows.size();
1082 while (N > 0) {
1083 N--;
1084 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1085 }
1086 }
Romain Guy06882f82009-06-10 13:36:04 -07001087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 void moveInputMethodDialogsLocked(int pos) {
1089 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 final int N = dialogs.size();
1092 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1093 for (int i=0; i<N; i++) {
1094 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1095 }
1096 if (DEBUG_INPUT_METHOD) {
1097 Log.v(TAG, "Window list w/pos=" + pos);
1098 logWindowList(" ");
1099 }
Romain Guy06882f82009-06-10 13:36:04 -07001100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 if (pos >= 0) {
1102 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1103 if (pos < mWindows.size()) {
1104 WindowState wp = (WindowState)mWindows.get(pos);
1105 if (wp == mInputMethodWindow) {
1106 pos++;
1107 }
1108 }
1109 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1110 for (int i=0; i<N; i++) {
1111 WindowState win = dialogs.get(i);
1112 win.mTargetAppToken = targetAppToken;
1113 pos = reAddWindowLocked(pos, win);
1114 }
1115 if (DEBUG_INPUT_METHOD) {
1116 Log.v(TAG, "Final window list:");
1117 logWindowList(" ");
1118 }
1119 return;
1120 }
1121 for (int i=0; i<N; i++) {
1122 WindowState win = dialogs.get(i);
1123 win.mTargetAppToken = null;
1124 reAddWindowToListInOrderLocked(win);
1125 if (DEBUG_INPUT_METHOD) {
1126 Log.v(TAG, "No IM target, final list:");
1127 logWindowList(" ");
1128 }
1129 }
1130 }
Romain Guy06882f82009-06-10 13:36:04 -07001131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1133 final WindowState imWin = mInputMethodWindow;
1134 final int DN = mInputMethodDialogs.size();
1135 if (imWin == null && DN == 0) {
1136 return false;
1137 }
Romain Guy06882f82009-06-10 13:36:04 -07001138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1140 if (imPos >= 0) {
1141 // In this case, the input method windows are to be placed
1142 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 // First check to see if the input method windows are already
1145 // located here, and contiguous.
1146 final int N = mWindows.size();
1147 WindowState firstImWin = imPos < N
1148 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 // Figure out the actual input method window that should be
1151 // at the bottom of their stack.
1152 WindowState baseImWin = imWin != null
1153 ? imWin : mInputMethodDialogs.get(0);
1154 if (baseImWin.mChildWindows.size() > 0) {
1155 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1156 if (cw.mSubLayer < 0) baseImWin = cw;
1157 }
Romain Guy06882f82009-06-10 13:36:04 -07001158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 if (firstImWin == baseImWin) {
1160 // The windows haven't moved... but are they still contiguous?
1161 // First find the top IM window.
1162 int pos = imPos+1;
1163 while (pos < N) {
1164 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1165 break;
1166 }
1167 pos++;
1168 }
1169 pos++;
1170 // Now there should be no more input method windows above.
1171 while (pos < N) {
1172 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1173 break;
1174 }
1175 pos++;
1176 }
1177 if (pos >= N) {
1178 // All is good!
1179 return false;
1180 }
1181 }
Romain Guy06882f82009-06-10 13:36:04 -07001182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 if (imWin != null) {
1184 if (DEBUG_INPUT_METHOD) {
1185 Log.v(TAG, "Moving IM from " + imPos);
1186 logWindowList(" ");
1187 }
1188 imPos = tmpRemoveWindowLocked(imPos, imWin);
1189 if (DEBUG_INPUT_METHOD) {
1190 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1191 logWindowList(" ");
1192 }
1193 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1194 reAddWindowLocked(imPos, imWin);
1195 if (DEBUG_INPUT_METHOD) {
1196 Log.v(TAG, "List after moving IM to " + imPos + ":");
1197 logWindowList(" ");
1198 }
1199 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1200 } else {
1201 moveInputMethodDialogsLocked(imPos);
1202 }
Romain Guy06882f82009-06-10 13:36:04 -07001203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 } else {
1205 // In this case, the input method windows go in a fixed layer,
1206 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 if (imWin != null) {
1209 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1210 tmpRemoveWindowLocked(0, imWin);
1211 imWin.mTargetAppToken = null;
1212 reAddWindowToListInOrderLocked(imWin);
1213 if (DEBUG_INPUT_METHOD) {
1214 Log.v(TAG, "List with no IM target:");
1215 logWindowList(" ");
1216 }
1217 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1218 } else {
1219 moveInputMethodDialogsLocked(-1);;
1220 }
Romain Guy06882f82009-06-10 13:36:04 -07001221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
Romain Guy06882f82009-06-10 13:36:04 -07001223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 if (needAssignLayers) {
1225 assignLayersLocked();
1226 }
Romain Guy06882f82009-06-10 13:36:04 -07001227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 return true;
1229 }
Romain Guy06882f82009-06-10 13:36:04 -07001230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 void adjustInputMethodDialogsLocked() {
1232 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1233 }
Romain Guy06882f82009-06-10 13:36:04 -07001234
Dianne Hackborn25994b42009-09-04 14:21:19 -07001235 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1236 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1237 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1238 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1239 ? wallpaperTarget.mAppToken.animation : null)
1240 + " upper=" + mUpperWallpaperTarget
1241 + " lower=" + mLowerWallpaperTarget);
1242 return (wallpaperTarget != null
1243 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1244 && wallpaperTarget.mAppToken.animation != null)))
1245 || mUpperWallpaperTarget != null
1246 || mLowerWallpaperTarget != null;
1247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001248
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001249 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1250 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001251
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001252 int adjustWallpaperWindowsLocked() {
1253 int changed = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001254
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001255 final int dw = mDisplay.getWidth();
1256 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001257
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001258 // First find top-most window that has asked to be on top of the
1259 // wallpaper; all wallpapers go behind it.
1260 final ArrayList localmWindows = mWindows;
1261 int N = localmWindows.size();
1262 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001263 WindowState foundW = null;
1264 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001265 WindowState topCurW = null;
1266 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001267 int i = N;
1268 while (i > 0) {
1269 i--;
1270 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001271 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1272 if (topCurW == null) {
1273 topCurW = w;
1274 topCurI = i;
1275 }
1276 continue;
1277 }
1278 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001279 if (w.mAppToken != null) {
1280 // If this window's app token is hidden and not animating,
1281 // it is of no interest to us.
1282 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1283 if (DEBUG_WALLPAPER) Log.v(TAG,
1284 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001285 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001286 continue;
1287 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001288 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001289 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1290 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1291 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001292 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001293 && (mWallpaperTarget == w
1294 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001295 if (DEBUG_WALLPAPER) Log.v(TAG,
1296 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001297 foundW = w;
1298 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001299 if (w == mWallpaperTarget && ((w.mAppToken != null
1300 && w.mAppToken.animation != null)
1301 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001302 // The current wallpaper target is animating, so we'll
1303 // look behind it for another possible target and figure
1304 // out what is going on below.
1305 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1306 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001307 continue;
1308 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001309 break;
1310 }
1311 }
1312
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001313 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001314 // If we are currently waiting for an app transition, and either
1315 // the current target or the next target are involved with it,
1316 // then hold off on doing anything with the wallpaper.
1317 // Note that we are checking here for just whether the target
1318 // is part of an app token... which is potentially overly aggressive
1319 // (the app token may not be involved in the transition), but good
1320 // enough (we'll just wait until whatever transition is pending
1321 // executes).
1322 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001323 if (DEBUG_WALLPAPER) Log.v(TAG,
1324 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001325 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001326 }
1327 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001328 if (DEBUG_WALLPAPER) Log.v(TAG,
1329 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001330 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001331 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001333
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001334 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001335 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001336 Log.v(TAG, "New wallpaper target: " + foundW
1337 + " oldTarget: " + mWallpaperTarget);
1338 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001339
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001340 mLowerWallpaperTarget = null;
1341 mUpperWallpaperTarget = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001342
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001343 WindowState oldW = mWallpaperTarget;
1344 mWallpaperTarget = foundW;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001346 // Now what is happening... if the current and new targets are
1347 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001348 if (foundW != null && oldW != null) {
1349 boolean oldAnim = oldW.mAnimation != null
1350 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1351 boolean foundAnim = foundW.mAnimation != null
1352 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001353 if (DEBUG_WALLPAPER) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001354 Log.v(TAG, "New animation: " + foundAnim
1355 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001356 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001357 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001358 int oldI = localmWindows.indexOf(oldW);
1359 if (DEBUG_WALLPAPER) {
1360 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1361 }
1362 if (oldI >= 0) {
1363 if (DEBUG_WALLPAPER) {
1364 Log.v(TAG, "Animating wallpapers: old#" + oldI
1365 + "=" + oldW + "; new#" + foundI
1366 + "=" + foundW);
1367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001368
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001369 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001370 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001371 if (DEBUG_WALLPAPER) {
1372 Log.v(TAG, "Old wallpaper still the target.");
1373 }
1374 mWallpaperTarget = oldW;
1375 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001376
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001377 // Now set the upper and lower wallpaper targets
1378 // correctly, and make sure that we are positioning
1379 // the wallpaper below the lower.
1380 if (foundI > oldI) {
1381 // The new target is on top of the old one.
1382 if (DEBUG_WALLPAPER) {
1383 Log.v(TAG, "Found target above old target.");
1384 }
1385 mUpperWallpaperTarget = foundW;
1386 mLowerWallpaperTarget = oldW;
1387 foundW = oldW;
1388 foundI = oldI;
1389 } else {
1390 // The new target is below the old one.
1391 if (DEBUG_WALLPAPER) {
1392 Log.v(TAG, "Found target below old target.");
1393 }
1394 mUpperWallpaperTarget = oldW;
1395 mLowerWallpaperTarget = foundW;
1396 }
1397 }
1398 }
1399 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001400
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001401 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001402 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001403 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1404 || (mLowerWallpaperTarget.mAppToken != null
1405 && mLowerWallpaperTarget.mAppToken.animation != null);
1406 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1407 || (mUpperWallpaperTarget.mAppToken != null
1408 && mUpperWallpaperTarget.mAppToken.animation != null);
1409 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001410 if (DEBUG_WALLPAPER) {
1411 Log.v(TAG, "No longer animating wallpaper targets!");
1412 }
1413 mLowerWallpaperTarget = null;
1414 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001415 }
1416 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001417
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001418 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001419 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001420 // The window is visible to the compositor... but is it visible
1421 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001422 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001423 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001424
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001425 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001426 // its layer adjustment. Only do this if we are not transfering
1427 // between two wallpaper targets.
1428 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001429 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001430 ? foundW.mAppToken.animLayerAdjustment : 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001431
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001432 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1433 * TYPE_LAYER_MULTIPLIER
1434 + TYPE_LAYER_OFFSET;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001436 // Now w is the window we are supposed to be behind... but we
1437 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001438 // AND any starting window associated with it, AND below the
1439 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001440 while (foundI > 0) {
1441 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001442 if (wb.mBaseLayer < maxLayer &&
1443 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001444 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001445 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001446 // This window is not related to the previous one in any
1447 // interesting way, so stop here.
1448 break;
1449 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001450 foundW = wb;
1451 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001452 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001453 } else {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001454 if (DEBUG_WALLPAPER) Log.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001455 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001456
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001457 if (foundW == null && topCurW != null) {
1458 // There is no wallpaper target, so it goes at the bottom.
1459 // We will assume it is the same place as last time, if known.
1460 foundW = topCurW;
1461 foundI = topCurI+1;
1462 } else {
1463 // Okay i is the position immediately above the wallpaper. Look at
1464 // what is below it for later.
1465 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1466 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001467
Dianne Hackborn284ac932009-08-28 10:34:25 -07001468 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001469 if (mWallpaperTarget.mWallpaperX >= 0) {
1470 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001471 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001472 }
1473 if (mWallpaperTarget.mWallpaperY >= 0) {
1474 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001475 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001476 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001477 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001478
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001479 // Start stepping backwards from here, ensuring that our wallpaper windows
1480 // are correctly placed.
1481 int curTokenIndex = mWallpaperTokens.size();
1482 while (curTokenIndex > 0) {
1483 curTokenIndex--;
1484 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001485 if (token.hidden == visible) {
1486 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1487 token.hidden = !visible;
1488 // Need to do a layout to ensure the wallpaper now has the
1489 // correct size.
1490 mLayoutNeeded = true;
1491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001492
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001493 int curWallpaperIndex = token.windows.size();
1494 while (curWallpaperIndex > 0) {
1495 curWallpaperIndex--;
1496 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001498 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001499 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001500 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001501
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001502 // First, make sure the client has the current visibility
1503 // state.
1504 if (wallpaper.mWallpaperVisible != visible) {
1505 wallpaper.mWallpaperVisible = visible;
1506 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001507 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001508 "Setting visibility of wallpaper " + wallpaper
1509 + ": " + visible);
1510 wallpaper.mClient.dispatchAppVisibility(visible);
1511 } catch (RemoteException e) {
1512 }
1513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001514
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001515 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001516 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1517 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001518
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001519 // First, if this window is at the current index, then all
1520 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001521 if (wallpaper == foundW) {
1522 foundI--;
1523 foundW = foundI > 0
1524 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001525 continue;
1526 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001527
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001528 // The window didn't match... the current wallpaper window,
1529 // wherever it is, is in the wrong place, so make sure it is
1530 // not in the list.
1531 int oldIndex = localmWindows.indexOf(wallpaper);
1532 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001533 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1534 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001535 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001536 if (oldIndex < foundI) {
1537 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001538 }
1539 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001540
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001541 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001542 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1543 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001544 + " from " + oldIndex + " to " + foundI);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001545
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001546 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001547 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001548 }
1549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001550
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001551 return changed;
1552 }
1553
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001554 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001555 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1556 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001557 mWallpaperAnimLayerAdjustment = adj;
1558 int curTokenIndex = mWallpaperTokens.size();
1559 while (curTokenIndex > 0) {
1560 curTokenIndex--;
1561 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1562 int curWallpaperIndex = token.windows.size();
1563 while (curWallpaperIndex > 0) {
1564 curWallpaperIndex--;
1565 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1566 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001567 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1568 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001569 }
1570 }
1571 }
1572
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001573 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1574 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001575 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001576 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001577 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001578 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001579 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1580 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1581 changed = wallpaperWin.mXOffset != offset;
1582 if (changed) {
1583 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1584 + wallpaperWin + " x: " + offset);
1585 wallpaperWin.mXOffset = offset;
1586 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001587 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001588 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001589 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001590 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001591 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001592
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001593 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001594 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001595 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1596 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1597 if (wallpaperWin.mYOffset != offset) {
1598 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1599 + wallpaperWin + " y: " + offset);
1600 changed = true;
1601 wallpaperWin.mYOffset = offset;
1602 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001603 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001604 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001605 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001606 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001608
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001609 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001610 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001611 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1612 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1613 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001614 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001615 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001616 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001617 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001618 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1619 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001620 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001621 if (mWaitingOnWallpaper != null) {
1622 long start = SystemClock.uptimeMillis();
1623 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1624 < start) {
1625 try {
1626 if (DEBUG_WALLPAPER) Log.v(TAG,
1627 "Waiting for offset complete...");
1628 mWindowMap.wait(WALLPAPER_TIMEOUT);
1629 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001630 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001631 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1632 if ((start+WALLPAPER_TIMEOUT)
1633 < SystemClock.uptimeMillis()) {
1634 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1635 + wallpaperWin);
1636 mLastWallpaperTimeoutTime = start;
1637 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001638 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001639 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001640 }
1641 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001642 } catch (RemoteException e) {
1643 }
1644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001645
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001646 return changed;
1647 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001648
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001649 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001650 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001651 if (mWaitingOnWallpaper != null &&
1652 mWaitingOnWallpaper.mClient.asBinder() == window) {
1653 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001654 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001655 }
1656 }
1657 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001658
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001659 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001660 final int dw = mDisplay.getWidth();
1661 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001662
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001663 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001664
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001665 WindowState target = mWallpaperTarget;
1666 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001667 if (target.mWallpaperX >= 0) {
1668 mLastWallpaperX = target.mWallpaperX;
1669 } else if (changingTarget.mWallpaperX >= 0) {
1670 mLastWallpaperX = changingTarget.mWallpaperX;
1671 }
1672 if (target.mWallpaperY >= 0) {
1673 mLastWallpaperY = target.mWallpaperY;
1674 } else if (changingTarget.mWallpaperY >= 0) {
1675 mLastWallpaperY = changingTarget.mWallpaperY;
1676 }
1677 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001678
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001679 int curTokenIndex = mWallpaperTokens.size();
1680 while (curTokenIndex > 0) {
1681 curTokenIndex--;
1682 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1683 int curWallpaperIndex = token.windows.size();
1684 while (curWallpaperIndex > 0) {
1685 curWallpaperIndex--;
1686 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1687 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1688 wallpaper.computeShownFrameLocked();
1689 changed = true;
1690 // We only want to be synchronous with one wallpaper.
1691 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001692 }
1693 }
1694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001695
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001696 return changed;
1697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001698
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001699 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001700 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001701 final int dw = mDisplay.getWidth();
1702 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001703
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001704 int curTokenIndex = mWallpaperTokens.size();
1705 while (curTokenIndex > 0) {
1706 curTokenIndex--;
1707 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001708 if (token.hidden == visible) {
1709 token.hidden = !visible;
1710 // Need to do a layout to ensure the wallpaper now has the
1711 // correct size.
1712 mLayoutNeeded = true;
1713 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001714
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001715 int curWallpaperIndex = token.windows.size();
1716 while (curWallpaperIndex > 0) {
1717 curWallpaperIndex--;
1718 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1719 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001720 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001721 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001722
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001723 if (wallpaper.mWallpaperVisible != visible) {
1724 wallpaper.mWallpaperVisible = visible;
1725 try {
1726 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001727 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001728 + ": " + visible);
1729 wallpaper.mClient.dispatchAppVisibility(visible);
1730 } catch (RemoteException e) {
1731 }
1732 }
1733 }
1734 }
1735 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001736
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001737 void sendPointerToWallpaperLocked(WindowState srcWin,
1738 MotionEvent pointer, long eventTime) {
1739 int curTokenIndex = mWallpaperTokens.size();
1740 while (curTokenIndex > 0) {
1741 curTokenIndex--;
1742 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1743 int curWallpaperIndex = token.windows.size();
1744 while (curWallpaperIndex > 0) {
1745 curWallpaperIndex--;
1746 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1747 if ((wallpaper.mAttrs.flags &
1748 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1749 continue;
1750 }
1751 try {
1752 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001753 if (srcWin != null) {
1754 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1755 srcWin.mFrame.top-wallpaper.mFrame.top);
1756 } else {
1757 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1758 }
1759 switch (pointer.getAction()) {
1760 case MotionEvent.ACTION_DOWN:
1761 mSendingPointersToWallpaper = true;
1762 break;
1763 case MotionEvent.ACTION_UP:
1764 mSendingPointersToWallpaper = false;
1765 break;
1766 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001767 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1768 } catch (RemoteException e) {
1769 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1770 }
1771 }
1772 }
1773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 public int addWindow(Session session, IWindow client,
1776 WindowManager.LayoutParams attrs, int viewVisibility,
1777 Rect outContentInsets) {
1778 int res = mPolicy.checkAddPermission(attrs);
1779 if (res != WindowManagerImpl.ADD_OKAY) {
1780 return res;
1781 }
Romain Guy06882f82009-06-10 13:36:04 -07001782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 boolean reportNewConfig = false;
1784 WindowState attachedWindow = null;
1785 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 synchronized(mWindowMap) {
1788 // Instantiating a Display requires talking with the simulator,
1789 // so don't do it until we know the system is mostly up and
1790 // running.
1791 if (mDisplay == null) {
1792 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1793 mDisplay = wm.getDefaultDisplay();
1794 mQueue.setDisplay(mDisplay);
1795 reportNewConfig = true;
1796 }
Romain Guy06882f82009-06-10 13:36:04 -07001797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 if (mWindowMap.containsKey(client.asBinder())) {
1799 Log.w(TAG, "Window " + client + " is already added");
1800 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1801 }
1802
1803 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001804 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 if (attachedWindow == null) {
1806 Log.w(TAG, "Attempted to add window with token that is not a window: "
1807 + attrs.token + ". Aborting.");
1808 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1809 }
1810 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1811 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1812 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1813 + attrs.token + ". Aborting.");
1814 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1815 }
1816 }
1817
1818 boolean addToken = false;
1819 WindowToken token = mTokenMap.get(attrs.token);
1820 if (token == null) {
1821 if (attrs.type >= FIRST_APPLICATION_WINDOW
1822 && attrs.type <= LAST_APPLICATION_WINDOW) {
1823 Log.w(TAG, "Attempted to add application window with unknown token "
1824 + attrs.token + ". Aborting.");
1825 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1826 }
1827 if (attrs.type == TYPE_INPUT_METHOD) {
1828 Log.w(TAG, "Attempted to add input method window with unknown token "
1829 + attrs.token + ". Aborting.");
1830 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1831 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001832 if (attrs.type == TYPE_WALLPAPER) {
1833 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1834 + attrs.token + ". Aborting.");
1835 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 token = new WindowToken(attrs.token, -1, false);
1838 addToken = true;
1839 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1840 && attrs.type <= LAST_APPLICATION_WINDOW) {
1841 AppWindowToken atoken = token.appWindowToken;
1842 if (atoken == null) {
1843 Log.w(TAG, "Attempted to add window with non-application token "
1844 + token + ". Aborting.");
1845 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1846 } else if (atoken.removed) {
1847 Log.w(TAG, "Attempted to add window with exiting application token "
1848 + token + ". Aborting.");
1849 return WindowManagerImpl.ADD_APP_EXITING;
1850 }
1851 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1852 // No need for this guy!
1853 if (localLOGV) Log.v(
1854 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1855 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1856 }
1857 } else if (attrs.type == TYPE_INPUT_METHOD) {
1858 if (token.windowType != TYPE_INPUT_METHOD) {
1859 Log.w(TAG, "Attempted to add input method window with bad token "
1860 + attrs.token + ". Aborting.");
1861 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1862 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001863 } else if (attrs.type == TYPE_WALLPAPER) {
1864 if (token.windowType != TYPE_WALLPAPER) {
1865 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1866 + attrs.token + ". Aborting.");
1867 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
1870
1871 win = new WindowState(session, client, token,
1872 attachedWindow, attrs, viewVisibility);
1873 if (win.mDeathRecipient == null) {
1874 // Client has apparently died, so there is no reason to
1875 // continue.
1876 Log.w(TAG, "Adding window client " + client.asBinder()
1877 + " that is dead, aborting.");
1878 return WindowManagerImpl.ADD_APP_EXITING;
1879 }
1880
1881 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 res = mPolicy.prepareAddWindowLw(win, attrs);
1884 if (res != WindowManagerImpl.ADD_OKAY) {
1885 return res;
1886 }
1887
1888 // From now on, no exceptions or errors allowed!
1889
1890 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 if (addToken) {
1895 mTokenMap.put(attrs.token, token);
1896 mTokenList.add(token);
1897 }
1898 win.attach();
1899 mWindowMap.put(client.asBinder(), win);
1900
1901 if (attrs.type == TYPE_APPLICATION_STARTING &&
1902 token.appWindowToken != null) {
1903 token.appWindowToken.startingWindow = win;
1904 }
1905
1906 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 if (attrs.type == TYPE_INPUT_METHOD) {
1909 mInputMethodWindow = win;
1910 addInputMethodWindowToListLocked(win);
1911 imMayMove = false;
1912 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1913 mInputMethodDialogs.add(win);
1914 addWindowToListInOrderLocked(win, true);
1915 adjustInputMethodDialogsLocked();
1916 imMayMove = false;
1917 } else {
1918 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001919 if (attrs.type == TYPE_WALLPAPER) {
1920 mLastWallpaperTimeoutTime = 0;
1921 adjustWallpaperWindowsLocked();
1922 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001923 adjustWallpaperWindowsLocked();
1924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 }
Romain Guy06882f82009-06-10 13:36:04 -07001926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 if (mInTouchMode) {
1932 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1933 }
1934 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1935 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1936 }
Romain Guy06882f82009-06-10 13:36:04 -07001937
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001938 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001940 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1941 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 imMayMove = false;
1943 }
1944 }
Romain Guy06882f82009-06-10 13:36:04 -07001945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001947 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
Romain Guy06882f82009-06-10 13:36:04 -07001949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 assignLayersLocked();
1951 // Don't do layout here, the window must call
1952 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 //dump();
1955
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001956 if (focusChanged) {
1957 if (mCurrentFocus != null) {
1958 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1959 }
1960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 if (localLOGV) Log.v(
1962 TAG, "New client " + client.asBinder()
1963 + ": window=" + win);
1964 }
1965
1966 // sendNewConfiguration() checks caller permissions so we must call it with
1967 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1968 // identity anyway, so it's safe to just clear & restore around this whole
1969 // block.
1970 final long origId = Binder.clearCallingIdentity();
1971 if (reportNewConfig) {
1972 sendNewConfiguration();
1973 } else {
1974 // Update Orientation after adding a window, only if the window needs to be
1975 // displayed right away
1976 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001977 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 sendNewConfiguration();
1979 }
1980 }
1981 }
1982 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 return res;
1985 }
Romain Guy06882f82009-06-10 13:36:04 -07001986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 public void removeWindow(Session session, IWindow client) {
1988 synchronized(mWindowMap) {
1989 WindowState win = windowForClientLocked(session, client);
1990 if (win == null) {
1991 return;
1992 }
1993 removeWindowLocked(session, win);
1994 }
1995 }
Romain Guy06882f82009-06-10 13:36:04 -07001996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 public void removeWindowLocked(Session session, WindowState win) {
1998
1999 if (localLOGV || DEBUG_FOCUS) Log.v(
2000 TAG, "Remove " + win + " client="
2001 + Integer.toHexString(System.identityHashCode(
2002 win.mClient.asBinder()))
2003 + ", surface=" + win.mSurface);
2004
2005 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 if (DEBUG_APP_TRANSITIONS) Log.v(
2008 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2009 + " mExiting=" + win.mExiting
2010 + " isAnimating=" + win.isAnimating()
2011 + " app-animation="
2012 + (win.mAppToken != null ? win.mAppToken.animation : null)
2013 + " inPendingTransaction="
2014 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2015 + " mDisplayFrozen=" + mDisplayFrozen);
2016 // Visibility of the removed window. Will be used later to update orientation later on.
2017 boolean wasVisible = false;
2018 // First, see if we need to run an animation. If we do, we have
2019 // to hold off on removing the window until the animation is done.
2020 // If the display is frozen, just remove immediately, since the
2021 // animation wouldn't be seen.
2022 if (win.mSurface != null && !mDisplayFrozen) {
2023 // If we are not currently running the exit animation, we
2024 // need to see about starting one.
2025 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2028 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2029 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2030 }
2031 // Try starting an animation.
2032 if (applyAnimationLocked(win, transit, false)) {
2033 win.mExiting = true;
2034 }
2035 }
2036 if (win.mExiting || win.isAnimating()) {
2037 // The exit animation is running... wait for it!
2038 //Log.i(TAG, "*** Running exit animation...");
2039 win.mExiting = true;
2040 win.mRemoveOnExit = true;
2041 mLayoutNeeded = true;
2042 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2043 performLayoutAndPlaceSurfacesLocked();
2044 if (win.mAppToken != null) {
2045 win.mAppToken.updateReportedVisibilityLocked();
2046 }
2047 //dump();
2048 Binder.restoreCallingIdentity(origId);
2049 return;
2050 }
2051 }
2052
2053 removeWindowInnerLocked(session, win);
2054 // Removing a visible window will effect the computed orientation
2055 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002056 if (wasVisible && computeForcedAppOrientationLocked()
2057 != mForcedAppOrientation) {
2058 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 }
2060 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2061 Binder.restoreCallingIdentity(origId);
2062 }
Romain Guy06882f82009-06-10 13:36:04 -07002063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002065 mKeyWaiter.finishedKey(session, win.mClient, true,
2066 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2068 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 if (mInputMethodTarget == win) {
2073 moveInputMethodWindowsIfNeededLocked(false);
2074 }
Romain Guy06882f82009-06-10 13:36:04 -07002075
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002076 if (false) {
2077 RuntimeException e = new RuntimeException("here");
2078 e.fillInStackTrace();
2079 Log.w(TAG, "Removing window " + win, e);
2080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 mPolicy.removeWindowLw(win);
2083 win.removeLocked();
2084
2085 mWindowMap.remove(win.mClient.asBinder());
2086 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002087 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088
2089 if (mInputMethodWindow == win) {
2090 mInputMethodWindow = null;
2091 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2092 mInputMethodDialogs.remove(win);
2093 }
Romain Guy06882f82009-06-10 13:36:04 -07002094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 final WindowToken token = win.mToken;
2096 final AppWindowToken atoken = win.mAppToken;
2097 token.windows.remove(win);
2098 if (atoken != null) {
2099 atoken.allAppWindows.remove(win);
2100 }
2101 if (localLOGV) Log.v(
2102 TAG, "**** Removing window " + win + ": count="
2103 + token.windows.size());
2104 if (token.windows.size() == 0) {
2105 if (!token.explicit) {
2106 mTokenMap.remove(token.token);
2107 mTokenList.remove(token);
2108 } else if (atoken != null) {
2109 atoken.firstWindowDrawn = false;
2110 }
2111 }
2112
2113 if (atoken != null) {
2114 if (atoken.startingWindow == win) {
2115 atoken.startingWindow = null;
2116 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2117 // If this is the last window and we had requested a starting
2118 // transition window, well there is no point now.
2119 atoken.startingData = null;
2120 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2121 // If this is the last window except for a starting transition
2122 // window, we need to get rid of the starting transition.
2123 if (DEBUG_STARTING_WINDOW) {
2124 Log.v(TAG, "Schedule remove starting " + token
2125 + ": no more real windows");
2126 }
2127 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2128 mH.sendMessage(m);
2129 }
2130 }
Romain Guy06882f82009-06-10 13:36:04 -07002131
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002132 if (win.mAttrs.type == TYPE_WALLPAPER) {
2133 mLastWallpaperTimeoutTime = 0;
2134 adjustWallpaperWindowsLocked();
2135 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002136 adjustWallpaperWindowsLocked();
2137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 if (!mInLayout) {
2140 assignLayersLocked();
2141 mLayoutNeeded = true;
2142 performLayoutAndPlaceSurfacesLocked();
2143 if (win.mAppToken != null) {
2144 win.mAppToken.updateReportedVisibilityLocked();
2145 }
2146 }
2147 }
2148
2149 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2150 long origId = Binder.clearCallingIdentity();
2151 try {
2152 synchronized (mWindowMap) {
2153 WindowState w = windowForClientLocked(session, client);
2154 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002155 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 Surface.openTransaction();
2157 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002158 if (SHOW_TRANSACTIONS) Log.i(
2159 TAG, " SURFACE " + w.mSurface
2160 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 w.mSurface.setTransparentRegionHint(region);
2162 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002163 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 Surface.closeTransaction();
2165 }
2166 }
2167 }
2168 } finally {
2169 Binder.restoreCallingIdentity(origId);
2170 }
2171 }
2172
2173 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002174 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 Rect visibleInsets) {
2176 long origId = Binder.clearCallingIdentity();
2177 try {
2178 synchronized (mWindowMap) {
2179 WindowState w = windowForClientLocked(session, client);
2180 if (w != null) {
2181 w.mGivenInsetsPending = false;
2182 w.mGivenContentInsets.set(contentInsets);
2183 w.mGivenVisibleInsets.set(visibleInsets);
2184 w.mTouchableInsets = touchableInsets;
2185 mLayoutNeeded = true;
2186 performLayoutAndPlaceSurfacesLocked();
2187 }
2188 }
2189 } finally {
2190 Binder.restoreCallingIdentity(origId);
2191 }
2192 }
Romain Guy06882f82009-06-10 13:36:04 -07002193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 public void getWindowDisplayFrame(Session session, IWindow client,
2195 Rect outDisplayFrame) {
2196 synchronized(mWindowMap) {
2197 WindowState win = windowForClientLocked(session, client);
2198 if (win == null) {
2199 outDisplayFrame.setEmpty();
2200 return;
2201 }
2202 outDisplayFrame.set(win.mDisplayFrame);
2203 }
2204 }
2205
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002206 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2207 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002208 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2209 window.mWallpaperX = x;
2210 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002211 window.mWallpaperXStep = xStep;
2212 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002213 if (updateWallpaperOffsetLocked(window, true)) {
2214 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002215 }
2216 }
2217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002218
Dianne Hackborn75804932009-10-20 20:15:20 -07002219 void wallpaperCommandComplete(IBinder window, Bundle result) {
2220 synchronized (mWindowMap) {
2221 if (mWaitingOnWallpaper != null &&
2222 mWaitingOnWallpaper.mClient.asBinder() == window) {
2223 mWaitingOnWallpaper = null;
2224 mWindowMap.notifyAll();
2225 }
2226 }
2227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002228
Dianne Hackborn75804932009-10-20 20:15:20 -07002229 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2230 String action, int x, int y, int z, Bundle extras, boolean sync) {
2231 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2232 || window == mUpperWallpaperTarget) {
2233 boolean doWait = sync;
2234 int curTokenIndex = mWallpaperTokens.size();
2235 while (curTokenIndex > 0) {
2236 curTokenIndex--;
2237 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2238 int curWallpaperIndex = token.windows.size();
2239 while (curWallpaperIndex > 0) {
2240 curWallpaperIndex--;
2241 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2242 try {
2243 wallpaper.mClient.dispatchWallpaperCommand(action,
2244 x, y, z, extras, sync);
2245 // We only want to be synchronous with one wallpaper.
2246 sync = false;
2247 } catch (RemoteException e) {
2248 }
2249 }
2250 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002251
Dianne Hackborn75804932009-10-20 20:15:20 -07002252 if (doWait) {
2253 // XXX Need to wait for result.
2254 }
2255 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002256
Dianne Hackborn75804932009-10-20 20:15:20 -07002257 return null;
2258 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 public int relayoutWindow(Session session, IWindow client,
2261 WindowManager.LayoutParams attrs, int requestedWidth,
2262 int requestedHeight, int viewVisibility, boolean insetsPending,
2263 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2264 Surface outSurface) {
2265 boolean displayed = false;
2266 boolean inTouchMode;
2267 Configuration newConfig = null;
2268 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 synchronized(mWindowMap) {
2271 WindowState win = windowForClientLocked(session, client);
2272 if (win == null) {
2273 return 0;
2274 }
2275 win.mRequestedWidth = requestedWidth;
2276 win.mRequestedHeight = requestedHeight;
2277
2278 if (attrs != null) {
2279 mPolicy.adjustWindowParamsLw(attrs);
2280 }
Romain Guy06882f82009-06-10 13:36:04 -07002281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 int attrChanges = 0;
2283 int flagChanges = 0;
2284 if (attrs != null) {
2285 flagChanges = win.mAttrs.flags ^= attrs.flags;
2286 attrChanges = win.mAttrs.copyFrom(attrs);
2287 }
2288
2289 if (localLOGV) Log.v(
2290 TAG, "Relayout given client " + client.asBinder()
2291 + " (" + win.mAttrs.getTitle() + ")");
2292
2293
2294 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2295 win.mAlpha = attrs.alpha;
2296 }
2297
2298 final boolean scaledWindow =
2299 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2300
2301 if (scaledWindow) {
2302 // requested{Width|Height} Surface's physical size
2303 // attrs.{width|height} Size on screen
2304 win.mHScale = (attrs.width != requestedWidth) ?
2305 (attrs.width / (float)requestedWidth) : 1.0f;
2306 win.mVScale = (attrs.height != requestedHeight) ?
2307 (attrs.height / (float)requestedHeight) : 1.0f;
2308 }
2309
2310 boolean imMayMove = (flagChanges&(
2311 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2312 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 boolean focusMayChange = win.mViewVisibility != viewVisibility
2315 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2316 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002317
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002318 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2319 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 win.mRelayoutCalled = true;
2322 final int oldVisibility = win.mViewVisibility;
2323 win.mViewVisibility = viewVisibility;
2324 if (viewVisibility == View.VISIBLE &&
2325 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2326 displayed = !win.isVisibleLw();
2327 if (win.mExiting) {
2328 win.mExiting = false;
2329 win.mAnimation = null;
2330 }
2331 if (win.mDestroying) {
2332 win.mDestroying = false;
2333 mDestroySurface.remove(win);
2334 }
2335 if (oldVisibility == View.GONE) {
2336 win.mEnterAnimationPending = true;
2337 }
2338 if (displayed && win.mSurface != null && !win.mDrawPending
2339 && !win.mCommitDrawPending && !mDisplayFrozen) {
2340 applyEnterAnimationLocked(win);
2341 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002342 if (displayed && (win.mAttrs.flags
2343 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2344 win.mTurnOnScreen = true;
2345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2347 // To change the format, we need to re-build the surface.
2348 win.destroySurfaceLocked();
2349 displayed = true;
2350 }
2351 try {
2352 Surface surface = win.createSurfaceLocked();
2353 if (surface != null) {
2354 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002355 win.mReportDestroySurface = false;
2356 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002357 if (SHOW_TRANSACTIONS) Log.i(TAG,
2358 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002360 // For some reason there isn't a surface. Clear the
2361 // caller's object so they see the same state.
2362 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 }
2364 } catch (Exception e) {
2365 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002366 + client + " (" + win.mAttrs.getTitle() + ")",
2367 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 Binder.restoreCallingIdentity(origId);
2369 return 0;
2370 }
2371 if (displayed) {
2372 focusMayChange = true;
2373 }
2374 if (win.mAttrs.type == TYPE_INPUT_METHOD
2375 && mInputMethodWindow == null) {
2376 mInputMethodWindow = win;
2377 imMayMove = true;
2378 }
2379 } else {
2380 win.mEnterAnimationPending = false;
2381 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002382 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2383 + ": mExiting=" + win.mExiting
2384 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 // If we are not currently running the exit animation, we
2386 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002387 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 // Try starting an animation; if there isn't one, we
2389 // can destroy the surface right away.
2390 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2391 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2392 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2393 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002394 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002396 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 win.mExiting = true;
2398 mKeyWaiter.finishedKey(session, client, true,
2399 KeyWaiter.RETURN_NOTHING);
2400 } else if (win.isAnimating()) {
2401 // Currently in a hide animation... turn this into
2402 // an exit.
2403 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002404 } else if (win == mWallpaperTarget) {
2405 // If the wallpaper is currently behind this
2406 // window, we need to change both of them inside
2407 // of a transaction to avoid artifacts.
2408 win.mExiting = true;
2409 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 } else {
2411 if (mInputMethodWindow == win) {
2412 mInputMethodWindow = null;
2413 }
2414 win.destroySurfaceLocked();
2415 }
2416 }
2417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002418
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002419 if (win.mSurface == null || (win.getAttrs().flags
2420 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2421 || win.mSurfacePendingDestroy) {
2422 // We are being called from a local process, which
2423 // means outSurface holds its current surface. Ensure the
2424 // surface object is cleared, but we don't want it actually
2425 // destroyed at this point.
2426 win.mSurfacePendingDestroy = false;
2427 outSurface.release();
2428 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2429 } else if (win.mSurface != null) {
2430 if (DEBUG_VISIBILITY) Log.i(TAG,
2431 "Keeping surface, will report destroy: " + win);
2432 win.mReportDestroySurface = true;
2433 outSurface.copyFrom(win.mSurface);
2434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 }
2436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 if (focusMayChange) {
2438 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2439 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 imMayMove = false;
2441 }
2442 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2443 }
Romain Guy06882f82009-06-10 13:36:04 -07002444
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002445 // updateFocusedWindowLocked() already assigned layers so we only need to
2446 // reassign them at this point if the IM window state gets shuffled
2447 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002450 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2451 // Little hack here -- we -should- be able to rely on the
2452 // function to return true if the IME has moved and needs
2453 // its layer recomputed. However, if the IME was hidden
2454 // and isn't actually moved in the list, its layer may be
2455 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 assignLayers = true;
2457 }
2458 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002459 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002460 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002461 assignLayers = true;
2462 }
2463 }
Romain Guy06882f82009-06-10 13:36:04 -07002464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 mLayoutNeeded = true;
2466 win.mGivenInsetsPending = insetsPending;
2467 if (assignLayers) {
2468 assignLayersLocked();
2469 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002470 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002472 if (displayed && win.mIsWallpaper) {
2473 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002474 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 if (win.mAppToken != null) {
2477 win.mAppToken.updateReportedVisibilityLocked();
2478 }
2479 outFrame.set(win.mFrame);
2480 outContentInsets.set(win.mContentInsets);
2481 outVisibleInsets.set(win.mVisibleInsets);
2482 if (localLOGV) Log.v(
2483 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002484 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 + ", requestedHeight=" + requestedHeight
2486 + ", viewVisibility=" + viewVisibility
2487 + "\nRelayout returning frame=" + outFrame
2488 + ", surface=" + outSurface);
2489
2490 if (localLOGV || DEBUG_FOCUS) Log.v(
2491 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2492
2493 inTouchMode = mInTouchMode;
2494 }
2495
2496 if (newConfig != null) {
2497 sendNewConfiguration();
2498 }
Romain Guy06882f82009-06-10 13:36:04 -07002499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2503 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2504 }
2505
2506 public void finishDrawingWindow(Session session, IWindow client) {
2507 final long origId = Binder.clearCallingIdentity();
2508 synchronized(mWindowMap) {
2509 WindowState win = windowForClientLocked(session, client);
2510 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002511 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2512 adjustWallpaperWindowsLocked();
2513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 mLayoutNeeded = true;
2515 performLayoutAndPlaceSurfacesLocked();
2516 }
2517 }
2518 Binder.restoreCallingIdentity(origId);
2519 }
2520
2521 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2522 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2523 + (lp != null ? lp.packageName : null)
2524 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2525 if (lp != null && lp.windowAnimations != 0) {
2526 // If this is a system resource, don't try to load it from the
2527 // application resources. It is nice to avoid loading application
2528 // resources if we can.
2529 String packageName = lp.packageName != null ? lp.packageName : "android";
2530 int resId = lp.windowAnimations;
2531 if ((resId&0xFF000000) == 0x01000000) {
2532 packageName = "android";
2533 }
2534 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2535 + packageName);
2536 return AttributeCache.instance().get(packageName, resId,
2537 com.android.internal.R.styleable.WindowAnimation);
2538 }
2539 return null;
2540 }
Romain Guy06882f82009-06-10 13:36:04 -07002541
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002542 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2543 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2544 + packageName + " resId=0x" + Integer.toHexString(resId));
2545 if (packageName != null) {
2546 if ((resId&0xFF000000) == 0x01000000) {
2547 packageName = "android";
2548 }
2549 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2550 + packageName);
2551 return AttributeCache.instance().get(packageName, resId,
2552 com.android.internal.R.styleable.WindowAnimation);
2553 }
2554 return null;
2555 }
2556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 private void applyEnterAnimationLocked(WindowState win) {
2558 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2559 if (win.mEnterAnimationPending) {
2560 win.mEnterAnimationPending = false;
2561 transit = WindowManagerPolicy.TRANSIT_ENTER;
2562 }
2563
2564 applyAnimationLocked(win, transit, true);
2565 }
2566
2567 private boolean applyAnimationLocked(WindowState win,
2568 int transit, boolean isEntrance) {
2569 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2570 // If we are trying to apply an animation, but already running
2571 // an animation of the same type, then just leave that one alone.
2572 return true;
2573 }
Romain Guy06882f82009-06-10 13:36:04 -07002574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 // Only apply an animation if the display isn't frozen. If it is
2576 // frozen, there is no reason to animate and it can cause strange
2577 // artifacts when we unfreeze the display if some different animation
2578 // is running.
2579 if (!mDisplayFrozen) {
2580 int anim = mPolicy.selectAnimationLw(win, transit);
2581 int attr = -1;
2582 Animation a = null;
2583 if (anim != 0) {
2584 a = AnimationUtils.loadAnimation(mContext, anim);
2585 } else {
2586 switch (transit) {
2587 case WindowManagerPolicy.TRANSIT_ENTER:
2588 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2589 break;
2590 case WindowManagerPolicy.TRANSIT_EXIT:
2591 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2592 break;
2593 case WindowManagerPolicy.TRANSIT_SHOW:
2594 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2595 break;
2596 case WindowManagerPolicy.TRANSIT_HIDE:
2597 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2598 break;
2599 }
2600 if (attr >= 0) {
2601 a = loadAnimation(win.mAttrs, attr);
2602 }
2603 }
2604 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2605 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2606 + " mAnimation=" + win.mAnimation
2607 + " isEntrance=" + isEntrance);
2608 if (a != null) {
2609 if (DEBUG_ANIM) {
2610 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002611 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2613 }
2614 win.setAnimation(a);
2615 win.mAnimationIsEntrance = isEntrance;
2616 }
2617 } else {
2618 win.clearAnimation();
2619 }
2620
2621 return win.mAnimation != null;
2622 }
2623
2624 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2625 int anim = 0;
2626 Context context = mContext;
2627 if (animAttr >= 0) {
2628 AttributeCache.Entry ent = getCachedAnimations(lp);
2629 if (ent != null) {
2630 context = ent.context;
2631 anim = ent.array.getResourceId(animAttr, 0);
2632 }
2633 }
2634 if (anim != 0) {
2635 return AnimationUtils.loadAnimation(context, anim);
2636 }
2637 return null;
2638 }
Romain Guy06882f82009-06-10 13:36:04 -07002639
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002640 private Animation loadAnimation(String packageName, int resId) {
2641 int anim = 0;
2642 Context context = mContext;
2643 if (resId >= 0) {
2644 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2645 if (ent != null) {
2646 context = ent.context;
2647 anim = resId;
2648 }
2649 }
2650 if (anim != 0) {
2651 return AnimationUtils.loadAnimation(context, anim);
2652 }
2653 return null;
2654 }
2655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 private boolean applyAnimationLocked(AppWindowToken wtoken,
2657 WindowManager.LayoutParams lp, int transit, boolean enter) {
2658 // Only apply an animation if the display isn't frozen. If it is
2659 // frozen, there is no reason to animate and it can cause strange
2660 // artifacts when we unfreeze the display if some different animation
2661 // is running.
2662 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002663 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002664 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002665 a = new FadeInOutAnimation(enter);
2666 if (DEBUG_ANIM) Log.v(TAG,
2667 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002668 } else if (mNextAppTransitionPackage != null) {
2669 a = loadAnimation(mNextAppTransitionPackage, enter ?
2670 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002671 } else {
2672 int animAttr = 0;
2673 switch (transit) {
2674 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2675 animAttr = enter
2676 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2677 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2678 break;
2679 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2680 animAttr = enter
2681 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2682 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2683 break;
2684 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2685 animAttr = enter
2686 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2687 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2688 break;
2689 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2690 animAttr = enter
2691 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2692 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2693 break;
2694 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2695 animAttr = enter
2696 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2697 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2698 break;
2699 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2700 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002701 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002702 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2703 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002704 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002705 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002706 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2707 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002708 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002709 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002710 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002711 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2712 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2713 break;
2714 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2715 animAttr = enter
2716 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2717 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2718 break;
2719 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2720 animAttr = enter
2721 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2722 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002723 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002724 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002725 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002726 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2727 + " anim=" + a
2728 + " animAttr=0x" + Integer.toHexString(animAttr)
2729 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 if (a != null) {
2732 if (DEBUG_ANIM) {
2733 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002734 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2736 }
2737 wtoken.setAnimation(a);
2738 }
2739 } else {
2740 wtoken.clearAnimation();
2741 }
2742
2743 return wtoken.animation != null;
2744 }
2745
2746 // -------------------------------------------------------------
2747 // Application Window Tokens
2748 // -------------------------------------------------------------
2749
2750 public void validateAppTokens(List tokens) {
2751 int v = tokens.size()-1;
2752 int m = mAppTokens.size()-1;
2753 while (v >= 0 && m >= 0) {
2754 AppWindowToken wtoken = mAppTokens.get(m);
2755 if (wtoken.removed) {
2756 m--;
2757 continue;
2758 }
2759 if (tokens.get(v) != wtoken.token) {
2760 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2761 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2762 }
2763 v--;
2764 m--;
2765 }
2766 while (v >= 0) {
2767 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2768 v--;
2769 }
2770 while (m >= 0) {
2771 AppWindowToken wtoken = mAppTokens.get(m);
2772 if (!wtoken.removed) {
2773 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2774 }
2775 m--;
2776 }
2777 }
2778
2779 boolean checkCallingPermission(String permission, String func) {
2780 // Quick check: if the calling permission is me, it's all okay.
2781 if (Binder.getCallingPid() == Process.myPid()) {
2782 return true;
2783 }
Romain Guy06882f82009-06-10 13:36:04 -07002784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 if (mContext.checkCallingPermission(permission)
2786 == PackageManager.PERMISSION_GRANTED) {
2787 return true;
2788 }
2789 String msg = "Permission Denial: " + func + " from pid="
2790 + Binder.getCallingPid()
2791 + ", uid=" + Binder.getCallingUid()
2792 + " requires " + permission;
2793 Log.w(TAG, msg);
2794 return false;
2795 }
Romain Guy06882f82009-06-10 13:36:04 -07002796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 AppWindowToken findAppWindowToken(IBinder token) {
2798 WindowToken wtoken = mTokenMap.get(token);
2799 if (wtoken == null) {
2800 return null;
2801 }
2802 return wtoken.appWindowToken;
2803 }
Romain Guy06882f82009-06-10 13:36:04 -07002804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 public void addWindowToken(IBinder token, int type) {
2806 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2807 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002808 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 }
Romain Guy06882f82009-06-10 13:36:04 -07002810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 synchronized(mWindowMap) {
2812 WindowToken wtoken = mTokenMap.get(token);
2813 if (wtoken != null) {
2814 Log.w(TAG, "Attempted to add existing input method token: " + token);
2815 return;
2816 }
2817 wtoken = new WindowToken(token, type, true);
2818 mTokenMap.put(token, wtoken);
2819 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002820 if (type == TYPE_WALLPAPER) {
2821 mWallpaperTokens.add(wtoken);
2822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
2824 }
Romain Guy06882f82009-06-10 13:36:04 -07002825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 public void removeWindowToken(IBinder token) {
2827 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2828 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002829 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 }
2831
2832 final long origId = Binder.clearCallingIdentity();
2833 synchronized(mWindowMap) {
2834 WindowToken wtoken = mTokenMap.remove(token);
2835 mTokenList.remove(wtoken);
2836 if (wtoken != null) {
2837 boolean delayed = false;
2838 if (!wtoken.hidden) {
2839 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 final int N = wtoken.windows.size();
2842 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 for (int i=0; i<N; i++) {
2845 WindowState win = wtoken.windows.get(i);
2846
2847 if (win.isAnimating()) {
2848 delayed = true;
2849 }
Romain Guy06882f82009-06-10 13:36:04 -07002850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 if (win.isVisibleNow()) {
2852 applyAnimationLocked(win,
2853 WindowManagerPolicy.TRANSIT_EXIT, false);
2854 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2855 KeyWaiter.RETURN_NOTHING);
2856 changed = true;
2857 }
2858 }
2859
2860 if (changed) {
2861 mLayoutNeeded = true;
2862 performLayoutAndPlaceSurfacesLocked();
2863 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2864 }
Romain Guy06882f82009-06-10 13:36:04 -07002865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 if (delayed) {
2867 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002868 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2869 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 }
2871 }
Romain Guy06882f82009-06-10 13:36:04 -07002872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 } else {
2874 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2875 }
2876 }
2877 Binder.restoreCallingIdentity(origId);
2878 }
2879
2880 public void addAppToken(int addPos, IApplicationToken token,
2881 int groupId, int requestedOrientation, boolean fullscreen) {
2882 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2883 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002884 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 }
Romain Guy06882f82009-06-10 13:36:04 -07002886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 synchronized(mWindowMap) {
2888 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2889 if (wtoken != null) {
2890 Log.w(TAG, "Attempted to add existing app token: " + token);
2891 return;
2892 }
2893 wtoken = new AppWindowToken(token);
2894 wtoken.groupId = groupId;
2895 wtoken.appFullscreen = fullscreen;
2896 wtoken.requestedOrientation = requestedOrientation;
2897 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002898 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 mTokenMap.put(token.asBinder(), wtoken);
2900 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 // Application tokens start out hidden.
2903 wtoken.hidden = true;
2904 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 //dump();
2907 }
2908 }
Romain Guy06882f82009-06-10 13:36:04 -07002909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 public void setAppGroupId(IBinder token, int groupId) {
2911 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2912 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002913 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 }
2915
2916 synchronized(mWindowMap) {
2917 AppWindowToken wtoken = findAppWindowToken(token);
2918 if (wtoken == null) {
2919 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2920 return;
2921 }
2922 wtoken.groupId = groupId;
2923 }
2924 }
Romain Guy06882f82009-06-10 13:36:04 -07002925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 public int getOrientationFromWindowsLocked() {
2927 int pos = mWindows.size() - 1;
2928 while (pos >= 0) {
2929 WindowState wtoken = (WindowState) mWindows.get(pos);
2930 pos--;
2931 if (wtoken.mAppToken != null) {
2932 // We hit an application window. so the orientation will be determined by the
2933 // app window. No point in continuing further.
2934 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2935 }
2936 if (!wtoken.isVisibleLw()) {
2937 continue;
2938 }
2939 int req = wtoken.mAttrs.screenOrientation;
2940 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2941 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2942 continue;
2943 } else {
2944 return req;
2945 }
2946 }
2947 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2948 }
Romain Guy06882f82009-06-10 13:36:04 -07002949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 public int getOrientationFromAppTokensLocked() {
2951 int pos = mAppTokens.size() - 1;
2952 int curGroup = 0;
2953 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002954 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002956 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 while (pos >= 0) {
2958 AppWindowToken wtoken = mAppTokens.get(pos);
2959 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002960 // if we're about to tear down this window and not seek for
2961 // the behind activity, don't use it for orientation
2962 if (!findingBehind
2963 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002964 continue;
2965 }
2966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 if (!haveGroup) {
2968 // We ignore any hidden applications on the top.
2969 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2970 continue;
2971 }
2972 haveGroup = true;
2973 curGroup = wtoken.groupId;
2974 lastOrientation = wtoken.requestedOrientation;
2975 } else if (curGroup != wtoken.groupId) {
2976 // If we have hit a new application group, and the bottom
2977 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002978 // the orientation behind it, and the last app was
2979 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002981 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2982 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 return lastOrientation;
2984 }
2985 }
2986 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002987 // If this application is fullscreen, and didn't explicitly say
2988 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002990 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002991 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002992 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 return or;
2994 }
2995 // If this application has requested an explicit orientation,
2996 // then use it.
2997 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2998 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2999 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3000 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3001 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3002 return or;
3003 }
Owen Lin3413b892009-05-01 17:12:32 -07003004 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 }
3006 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3007 }
Romain Guy06882f82009-06-10 13:36:04 -07003008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003010 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003011 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3012 "updateOrientationFromAppTokens()")) {
3013 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3014 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 Configuration config;
3017 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003018 config = updateOrientationFromAppTokensUnchecked(currentConfig,
3019 freezeThisOneIfNeeded);
3020 Binder.restoreCallingIdentity(ident);
3021 return config;
3022 }
3023
3024 Configuration updateOrientationFromAppTokensUnchecked(
3025 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
3026 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003028 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003029 if (config != null) {
3030 mLayoutNeeded = true;
3031 performLayoutAndPlaceSurfacesLocked();
3032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 return config;
3035 }
Romain Guy06882f82009-06-10 13:36:04 -07003036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 /*
3038 * The orientation is computed from non-application windows first. If none of
3039 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003040 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3042 * android.os.IBinder)
3043 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003044 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07003045 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 long ident = Binder.clearCallingIdentity();
3048 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003049 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 if (req != mForcedAppOrientation) {
3052 changed = true;
3053 mForcedAppOrientation = req;
3054 //send a message to Policy indicating orientation change to take
3055 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003056 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 }
Romain Guy06882f82009-06-10 13:36:04 -07003058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 if (changed) {
3060 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07003061 WindowManagerPolicy.USE_LAST_ROTATION,
3062 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 if (changed) {
3064 if (freezeThisOneIfNeeded != null) {
3065 AppWindowToken wtoken = findAppWindowToken(
3066 freezeThisOneIfNeeded);
3067 if (wtoken != null) {
3068 startAppFreezingScreenLocked(wtoken,
3069 ActivityInfo.CONFIG_ORIENTATION);
3070 }
3071 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003072 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 }
3074 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003075
3076 // No obvious action we need to take, but if our current
3077 // state mismatches the activity maanager's, update it
3078 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003079 mTempConfiguration.setToDefaults();
3080 if (computeNewConfigurationLocked(mTempConfiguration)) {
3081 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003082 return new Configuration(mTempConfiguration);
3083 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003084 }
3085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 } finally {
3087 Binder.restoreCallingIdentity(ident);
3088 }
Romain Guy06882f82009-06-10 13:36:04 -07003089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 return null;
3091 }
Romain Guy06882f82009-06-10 13:36:04 -07003092
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003093 int computeForcedAppOrientationLocked() {
3094 int req = getOrientationFromWindowsLocked();
3095 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3096 req = getOrientationFromAppTokensLocked();
3097 }
3098 return req;
3099 }
Romain Guy06882f82009-06-10 13:36:04 -07003100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3102 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3103 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003104 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 }
Romain Guy06882f82009-06-10 13:36:04 -07003106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 synchronized(mWindowMap) {
3108 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3109 if (wtoken == null) {
3110 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3111 return;
3112 }
Romain Guy06882f82009-06-10 13:36:04 -07003113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 wtoken.requestedOrientation = requestedOrientation;
3115 }
3116 }
Romain Guy06882f82009-06-10 13:36:04 -07003117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 public int getAppOrientation(IApplicationToken token) {
3119 synchronized(mWindowMap) {
3120 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3121 if (wtoken == null) {
3122 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3123 }
Romain Guy06882f82009-06-10 13:36:04 -07003124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 return wtoken.requestedOrientation;
3126 }
3127 }
Romain Guy06882f82009-06-10 13:36:04 -07003128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3130 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3131 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003132 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 }
3134
3135 synchronized(mWindowMap) {
3136 boolean changed = false;
3137 if (token == null) {
3138 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3139 changed = mFocusedApp != null;
3140 mFocusedApp = null;
3141 mKeyWaiter.tickle();
3142 } else {
3143 AppWindowToken newFocus = findAppWindowToken(token);
3144 if (newFocus == null) {
3145 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3146 return;
3147 }
3148 changed = mFocusedApp != newFocus;
3149 mFocusedApp = newFocus;
3150 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3151 mKeyWaiter.tickle();
3152 }
3153
3154 if (moveFocusNow && changed) {
3155 final long origId = Binder.clearCallingIdentity();
3156 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3157 Binder.restoreCallingIdentity(origId);
3158 }
3159 }
3160 }
3161
3162 public void prepareAppTransition(int transit) {
3163 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3164 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003165 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 }
Romain Guy06882f82009-06-10 13:36:04 -07003167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 synchronized(mWindowMap) {
3169 if (DEBUG_APP_TRANSITIONS) Log.v(
3170 TAG, "Prepare app transition: transit=" + transit
3171 + " mNextAppTransition=" + mNextAppTransition);
3172 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003173 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3174 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003176 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3177 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3178 // Opening a new task always supersedes a close for the anim.
3179 mNextAppTransition = transit;
3180 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3181 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3182 // Opening a new activity always supersedes a close for the anim.
3183 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 }
3185 mAppTransitionReady = false;
3186 mAppTransitionTimeout = false;
3187 mStartingIconInTransition = false;
3188 mSkipAppTransitionAnimation = false;
3189 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3190 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3191 5000);
3192 }
3193 }
3194 }
3195
3196 public int getPendingAppTransition() {
3197 return mNextAppTransition;
3198 }
Romain Guy06882f82009-06-10 13:36:04 -07003199
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003200 public void overridePendingAppTransition(String packageName,
3201 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003202 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003203 mNextAppTransitionPackage = packageName;
3204 mNextAppTransitionEnter = enterAnim;
3205 mNextAppTransitionExit = exitAnim;
3206 }
3207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 public void executeAppTransition() {
3210 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3211 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003212 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 }
Romain Guy06882f82009-06-10 13:36:04 -07003214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003216 if (DEBUG_APP_TRANSITIONS) {
3217 RuntimeException e = new RuntimeException("here");
3218 e.fillInStackTrace();
3219 Log.w(TAG, "Execute app transition: mNextAppTransition="
3220 + mNextAppTransition, e);
3221 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003222 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 mAppTransitionReady = true;
3224 final long origId = Binder.clearCallingIdentity();
3225 performLayoutAndPlaceSurfacesLocked();
3226 Binder.restoreCallingIdentity(origId);
3227 }
3228 }
3229 }
3230
3231 public void setAppStartingWindow(IBinder token, String pkg,
3232 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3233 IBinder transferFrom, boolean createIfNeeded) {
3234 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3235 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003236 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 }
3238
3239 synchronized(mWindowMap) {
3240 if (DEBUG_STARTING_WINDOW) Log.v(
3241 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3242 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 AppWindowToken wtoken = findAppWindowToken(token);
3245 if (wtoken == null) {
3246 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3247 return;
3248 }
3249
3250 // If the display is frozen, we won't do anything until the
3251 // actual window is displayed so there is no reason to put in
3252 // the starting window.
3253 if (mDisplayFrozen) {
3254 return;
3255 }
Romain Guy06882f82009-06-10 13:36:04 -07003256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 if (wtoken.startingData != null) {
3258 return;
3259 }
Romain Guy06882f82009-06-10 13:36:04 -07003260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 if (transferFrom != null) {
3262 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3263 if (ttoken != null) {
3264 WindowState startingWindow = ttoken.startingWindow;
3265 if (startingWindow != null) {
3266 if (mStartingIconInTransition) {
3267 // In this case, the starting icon has already
3268 // been displayed, so start letting windows get
3269 // shown immediately without any more transitions.
3270 mSkipAppTransitionAnimation = true;
3271 }
3272 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3273 "Moving existing starting from " + ttoken
3274 + " to " + wtoken);
3275 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 // Transfer the starting window over to the new
3278 // token.
3279 wtoken.startingData = ttoken.startingData;
3280 wtoken.startingView = ttoken.startingView;
3281 wtoken.startingWindow = startingWindow;
3282 ttoken.startingData = null;
3283 ttoken.startingView = null;
3284 ttoken.startingWindow = null;
3285 ttoken.startingMoved = true;
3286 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003287 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003289 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3290 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 mWindows.remove(startingWindow);
3292 ttoken.windows.remove(startingWindow);
3293 ttoken.allAppWindows.remove(startingWindow);
3294 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 // Propagate other interesting state between the
3297 // tokens. If the old token is displayed, we should
3298 // immediately force the new one to be displayed. If
3299 // it is animating, we need to move that animation to
3300 // the new one.
3301 if (ttoken.allDrawn) {
3302 wtoken.allDrawn = true;
3303 }
3304 if (ttoken.firstWindowDrawn) {
3305 wtoken.firstWindowDrawn = true;
3306 }
3307 if (!ttoken.hidden) {
3308 wtoken.hidden = false;
3309 wtoken.hiddenRequested = false;
3310 wtoken.willBeHidden = false;
3311 }
3312 if (wtoken.clientHidden != ttoken.clientHidden) {
3313 wtoken.clientHidden = ttoken.clientHidden;
3314 wtoken.sendAppVisibilityToClients();
3315 }
3316 if (ttoken.animation != null) {
3317 wtoken.animation = ttoken.animation;
3318 wtoken.animating = ttoken.animating;
3319 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3320 ttoken.animation = null;
3321 ttoken.animLayerAdjustment = 0;
3322 wtoken.updateLayers();
3323 ttoken.updateLayers();
3324 }
Romain Guy06882f82009-06-10 13:36:04 -07003325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 mLayoutNeeded = true;
3328 performLayoutAndPlaceSurfacesLocked();
3329 Binder.restoreCallingIdentity(origId);
3330 return;
3331 } else if (ttoken.startingData != null) {
3332 // The previous app was getting ready to show a
3333 // starting window, but hasn't yet done so. Steal it!
3334 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3335 "Moving pending starting from " + ttoken
3336 + " to " + wtoken);
3337 wtoken.startingData = ttoken.startingData;
3338 ttoken.startingData = null;
3339 ttoken.startingMoved = true;
3340 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3341 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3342 // want to process the message ASAP, before any other queued
3343 // messages.
3344 mH.sendMessageAtFrontOfQueue(m);
3345 return;
3346 }
3347 }
3348 }
3349
3350 // There is no existing starting window, and the caller doesn't
3351 // want us to create one, so that's it!
3352 if (!createIfNeeded) {
3353 return;
3354 }
Romain Guy06882f82009-06-10 13:36:04 -07003355
Dianne Hackborn284ac932009-08-28 10:34:25 -07003356 // If this is a translucent or wallpaper window, then don't
3357 // show a starting window -- the current effect (a full-screen
3358 // opaque starting window that fades away to the real contents
3359 // when it is ready) does not work for this.
3360 if (theme != 0) {
3361 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3362 com.android.internal.R.styleable.Window);
3363 if (ent.array.getBoolean(
3364 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3365 return;
3366 }
3367 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003368 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3369 return;
3370 }
3371 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003372 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3373 return;
3374 }
3375 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 mStartingIconInTransition = true;
3378 wtoken.startingData = new StartingData(
3379 pkg, theme, nonLocalizedLabel,
3380 labelRes, icon);
3381 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3382 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3383 // want to process the message ASAP, before any other queued
3384 // messages.
3385 mH.sendMessageAtFrontOfQueue(m);
3386 }
3387 }
3388
3389 public void setAppWillBeHidden(IBinder token) {
3390 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3391 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003392 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 }
3394
3395 AppWindowToken wtoken;
3396
3397 synchronized(mWindowMap) {
3398 wtoken = findAppWindowToken(token);
3399 if (wtoken == null) {
3400 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3401 return;
3402 }
3403 wtoken.willBeHidden = true;
3404 }
3405 }
Romain Guy06882f82009-06-10 13:36:04 -07003406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3408 boolean visible, int transit, boolean performLayout) {
3409 boolean delayed = false;
3410
3411 if (wtoken.clientHidden == visible) {
3412 wtoken.clientHidden = !visible;
3413 wtoken.sendAppVisibilityToClients();
3414 }
Romain Guy06882f82009-06-10 13:36:04 -07003415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 wtoken.willBeHidden = false;
3417 if (wtoken.hidden == visible) {
3418 final int N = wtoken.allAppWindows.size();
3419 boolean changed = false;
3420 if (DEBUG_APP_TRANSITIONS) Log.v(
3421 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3422 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003425
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003426 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 if (wtoken.animation == sDummyAnimation) {
3428 wtoken.animation = null;
3429 }
3430 applyAnimationLocked(wtoken, lp, transit, visible);
3431 changed = true;
3432 if (wtoken.animation != null) {
3433 delayed = runningAppAnimation = true;
3434 }
3435 }
Romain Guy06882f82009-06-10 13:36:04 -07003436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 for (int i=0; i<N; i++) {
3438 WindowState win = wtoken.allAppWindows.get(i);
3439 if (win == wtoken.startingWindow) {
3440 continue;
3441 }
3442
3443 if (win.isAnimating()) {
3444 delayed = true;
3445 }
Romain Guy06882f82009-06-10 13:36:04 -07003446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3448 //win.dump(" ");
3449 if (visible) {
3450 if (!win.isVisibleNow()) {
3451 if (!runningAppAnimation) {
3452 applyAnimationLocked(win,
3453 WindowManagerPolicy.TRANSIT_ENTER, true);
3454 }
3455 changed = true;
3456 }
3457 } else if (win.isVisibleNow()) {
3458 if (!runningAppAnimation) {
3459 applyAnimationLocked(win,
3460 WindowManagerPolicy.TRANSIT_EXIT, false);
3461 }
3462 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3463 KeyWaiter.RETURN_NOTHING);
3464 changed = true;
3465 }
3466 }
3467
3468 wtoken.hidden = wtoken.hiddenRequested = !visible;
3469 if (!visible) {
3470 unsetAppFreezingScreenLocked(wtoken, true, true);
3471 } else {
3472 // If we are being set visible, and the starting window is
3473 // not yet displayed, then make sure it doesn't get displayed.
3474 WindowState swin = wtoken.startingWindow;
3475 if (swin != null && (swin.mDrawPending
3476 || swin.mCommitDrawPending)) {
3477 swin.mPolicyVisibility = false;
3478 swin.mPolicyVisibilityAfterAnim = false;
3479 }
3480 }
Romain Guy06882f82009-06-10 13:36:04 -07003481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3483 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3484 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 if (changed && performLayout) {
3487 mLayoutNeeded = true;
3488 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 performLayoutAndPlaceSurfacesLocked();
3490 }
3491 }
3492
3493 if (wtoken.animation != null) {
3494 delayed = true;
3495 }
Romain Guy06882f82009-06-10 13:36:04 -07003496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 return delayed;
3498 }
3499
3500 public void setAppVisibility(IBinder token, boolean visible) {
3501 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3502 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003503 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 }
3505
3506 AppWindowToken wtoken;
3507
3508 synchronized(mWindowMap) {
3509 wtoken = findAppWindowToken(token);
3510 if (wtoken == null) {
3511 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3512 return;
3513 }
3514
3515 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3516 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003517 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3519 + "): mNextAppTransition=" + mNextAppTransition
3520 + " hidden=" + wtoken.hidden
3521 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3522 }
Romain Guy06882f82009-06-10 13:36:04 -07003523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 // If we are preparing an app transition, then delay changing
3525 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003526 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 // Already in requested state, don't do anything more.
3528 if (wtoken.hiddenRequested != visible) {
3529 return;
3530 }
3531 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 if (DEBUG_APP_TRANSITIONS) Log.v(
3534 TAG, "Setting dummy animation on: " + wtoken);
3535 wtoken.setDummyAnimation();
3536 mOpeningApps.remove(wtoken);
3537 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003538 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 wtoken.inPendingTransaction = true;
3540 if (visible) {
3541 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 wtoken.startingDisplayed = false;
3543 wtoken.startingMoved = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003544
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003545 // If the token is currently hidden (should be the
3546 // common case), then we need to set up to wait for
3547 // its windows to be ready.
3548 if (wtoken.hidden) {
3549 wtoken.allDrawn = false;
3550 wtoken.waitingToShow = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003551
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003552 if (wtoken.clientHidden) {
3553 // In the case where we are making an app visible
3554 // but holding off for a transition, we still need
3555 // to tell the client to make its windows visible so
3556 // they get drawn. Otherwise, we will wait on
3557 // performing the transition until all windows have
3558 // been drawn, they never will be, and we are sad.
3559 wtoken.clientHidden = false;
3560 wtoken.sendAppVisibilityToClients();
3561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 }
3563 } else {
3564 mClosingApps.add(wtoken);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003565
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003566 // If the token is currently visible (should be the
3567 // common case), then set up to wait for it to be hidden.
3568 if (!wtoken.hidden) {
3569 wtoken.waitingToHide = true;
3570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 }
3572 return;
3573 }
Romain Guy06882f82009-06-10 13:36:04 -07003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003576 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 wtoken.updateReportedVisibilityLocked();
3578 Binder.restoreCallingIdentity(origId);
3579 }
3580 }
3581
3582 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3583 boolean unfreezeSurfaceNow, boolean force) {
3584 if (wtoken.freezingScreen) {
3585 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3586 + " force=" + force);
3587 final int N = wtoken.allAppWindows.size();
3588 boolean unfrozeWindows = false;
3589 for (int i=0; i<N; i++) {
3590 WindowState w = wtoken.allAppWindows.get(i);
3591 if (w.mAppFreezing) {
3592 w.mAppFreezing = false;
3593 if (w.mSurface != null && !w.mOrientationChanging) {
3594 w.mOrientationChanging = true;
3595 }
3596 unfrozeWindows = true;
3597 }
3598 }
3599 if (force || unfrozeWindows) {
3600 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3601 wtoken.freezingScreen = false;
3602 mAppsFreezingScreen--;
3603 }
3604 if (unfreezeSurfaceNow) {
3605 if (unfrozeWindows) {
3606 mLayoutNeeded = true;
3607 performLayoutAndPlaceSurfacesLocked();
3608 }
3609 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3610 stopFreezingDisplayLocked();
3611 }
3612 }
3613 }
3614 }
Romain Guy06882f82009-06-10 13:36:04 -07003615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3617 int configChanges) {
3618 if (DEBUG_ORIENTATION) {
3619 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003620 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 Log.i(TAG, "Set freezing of " + wtoken.appToken
3622 + ": hidden=" + wtoken.hidden + " freezing="
3623 + wtoken.freezingScreen, e);
3624 }
3625 if (!wtoken.hiddenRequested) {
3626 if (!wtoken.freezingScreen) {
3627 wtoken.freezingScreen = true;
3628 mAppsFreezingScreen++;
3629 if (mAppsFreezingScreen == 1) {
3630 startFreezingDisplayLocked();
3631 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3632 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3633 5000);
3634 }
3635 }
3636 final int N = wtoken.allAppWindows.size();
3637 for (int i=0; i<N; i++) {
3638 WindowState w = wtoken.allAppWindows.get(i);
3639 w.mAppFreezing = true;
3640 }
3641 }
3642 }
Romain Guy06882f82009-06-10 13:36:04 -07003643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 public void startAppFreezingScreen(IBinder token, int configChanges) {
3645 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3646 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003647 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 }
3649
3650 synchronized(mWindowMap) {
3651 if (configChanges == 0 && !mDisplayFrozen) {
3652 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3653 return;
3654 }
Romain Guy06882f82009-06-10 13:36:04 -07003655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 AppWindowToken wtoken = findAppWindowToken(token);
3657 if (wtoken == null || wtoken.appToken == null) {
3658 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3659 return;
3660 }
3661 final long origId = Binder.clearCallingIdentity();
3662 startAppFreezingScreenLocked(wtoken, configChanges);
3663 Binder.restoreCallingIdentity(origId);
3664 }
3665 }
Romain Guy06882f82009-06-10 13:36:04 -07003666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 public void stopAppFreezingScreen(IBinder token, boolean force) {
3668 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3669 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003670 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 }
3672
3673 synchronized(mWindowMap) {
3674 AppWindowToken wtoken = findAppWindowToken(token);
3675 if (wtoken == null || wtoken.appToken == null) {
3676 return;
3677 }
3678 final long origId = Binder.clearCallingIdentity();
3679 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3680 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3681 unsetAppFreezingScreenLocked(wtoken, true, force);
3682 Binder.restoreCallingIdentity(origId);
3683 }
3684 }
Romain Guy06882f82009-06-10 13:36:04 -07003685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 public void removeAppToken(IBinder token) {
3687 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3688 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003689 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 }
3691
3692 AppWindowToken wtoken = null;
3693 AppWindowToken startingToken = null;
3694 boolean delayed = false;
3695
3696 final long origId = Binder.clearCallingIdentity();
3697 synchronized(mWindowMap) {
3698 WindowToken basewtoken = mTokenMap.remove(token);
3699 mTokenList.remove(basewtoken);
3700 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3701 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003702 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 wtoken.inPendingTransaction = false;
3704 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003705 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 if (mClosingApps.contains(wtoken)) {
3707 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003708 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003710 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 delayed = true;
3712 }
3713 if (DEBUG_APP_TRANSITIONS) Log.v(
3714 TAG, "Removing app " + wtoken + " delayed=" + delayed
3715 + " animation=" + wtoken.animation
3716 + " animating=" + wtoken.animating);
3717 if (delayed) {
3718 // set the token aside because it has an active animation to be finished
3719 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003720 } else {
3721 // Make sure there is no animation running on this token,
3722 // so any windows associated with it will be removed as
3723 // soon as their animations are complete
3724 wtoken.animation = null;
3725 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 }
3727 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003728 if (mLastEnterAnimToken == wtoken) {
3729 mLastEnterAnimToken = null;
3730 mLastEnterAnimParams = null;
3731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 wtoken.removed = true;
3733 if (wtoken.startingData != null) {
3734 startingToken = wtoken;
3735 }
3736 unsetAppFreezingScreenLocked(wtoken, true, true);
3737 if (mFocusedApp == wtoken) {
3738 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3739 mFocusedApp = null;
3740 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3741 mKeyWaiter.tickle();
3742 }
3743 } else {
3744 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3745 }
Romain Guy06882f82009-06-10 13:36:04 -07003746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 if (!delayed && wtoken != null) {
3748 wtoken.updateReportedVisibilityLocked();
3749 }
3750 }
3751 Binder.restoreCallingIdentity(origId);
3752
3753 if (startingToken != null) {
3754 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3755 + startingToken + ": app token removed");
3756 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3757 mH.sendMessage(m);
3758 }
3759 }
3760
3761 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3762 final int NW = token.windows.size();
3763 for (int i=0; i<NW; i++) {
3764 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003765 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 mWindows.remove(win);
3767 int j = win.mChildWindows.size();
3768 while (j > 0) {
3769 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003770 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3771 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3772 "Tmp removing child window " + cwin);
3773 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 }
3775 }
3776 return NW > 0;
3777 }
3778
3779 void dumpAppTokensLocked() {
3780 for (int i=mAppTokens.size()-1; i>=0; i--) {
3781 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3782 }
3783 }
Romain Guy06882f82009-06-10 13:36:04 -07003784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 void dumpWindowsLocked() {
3786 for (int i=mWindows.size()-1; i>=0; i--) {
3787 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3788 }
3789 }
Romain Guy06882f82009-06-10 13:36:04 -07003790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 private int findWindowOffsetLocked(int tokenPos) {
3792 final int NW = mWindows.size();
3793
3794 if (tokenPos >= mAppTokens.size()) {
3795 int i = NW;
3796 while (i > 0) {
3797 i--;
3798 WindowState win = (WindowState)mWindows.get(i);
3799 if (win.getAppToken() != null) {
3800 return i+1;
3801 }
3802 }
3803 }
3804
3805 while (tokenPos > 0) {
3806 // Find the first app token below the new position that has
3807 // a window displayed.
3808 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3809 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3810 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003811 if (wtoken.sendingToBottom) {
3812 if (DEBUG_REORDER) Log.v(TAG,
3813 "Skipping token -- currently sending to bottom");
3814 tokenPos--;
3815 continue;
3816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 int i = wtoken.windows.size();
3818 while (i > 0) {
3819 i--;
3820 WindowState win = wtoken.windows.get(i);
3821 int j = win.mChildWindows.size();
3822 while (j > 0) {
3823 j--;
3824 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003825 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 for (int pos=NW-1; pos>=0; pos--) {
3827 if (mWindows.get(pos) == cwin) {
3828 if (DEBUG_REORDER) Log.v(TAG,
3829 "Found child win @" + (pos+1));
3830 return pos+1;
3831 }
3832 }
3833 }
3834 }
3835 for (int pos=NW-1; pos>=0; pos--) {
3836 if (mWindows.get(pos) == win) {
3837 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3838 return pos+1;
3839 }
3840 }
3841 }
3842 tokenPos--;
3843 }
3844
3845 return 0;
3846 }
3847
3848 private final int reAddWindowLocked(int index, WindowState win) {
3849 final int NCW = win.mChildWindows.size();
3850 boolean added = false;
3851 for (int j=0; j<NCW; j++) {
3852 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3853 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003854 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3855 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856 mWindows.add(index, win);
3857 index++;
3858 added = true;
3859 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003860 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3861 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 mWindows.add(index, cwin);
3863 index++;
3864 }
3865 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003866 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3867 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 mWindows.add(index, win);
3869 index++;
3870 }
3871 return index;
3872 }
Romain Guy06882f82009-06-10 13:36:04 -07003873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3875 final int NW = token.windows.size();
3876 for (int i=0; i<NW; i++) {
3877 index = reAddWindowLocked(index, token.windows.get(i));
3878 }
3879 return index;
3880 }
3881
3882 public void moveAppToken(int index, IBinder token) {
3883 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3884 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003885 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 }
3887
3888 synchronized(mWindowMap) {
3889 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3890 if (DEBUG_REORDER) dumpAppTokensLocked();
3891 final AppWindowToken wtoken = findAppWindowToken(token);
3892 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3893 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3894 + token + " (" + wtoken + ")");
3895 return;
3896 }
3897 mAppTokens.add(index, wtoken);
3898 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3899 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003901 final long origId = Binder.clearCallingIdentity();
3902 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3903 if (DEBUG_REORDER) dumpWindowsLocked();
3904 if (tmpRemoveAppWindowsLocked(wtoken)) {
3905 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3906 if (DEBUG_REORDER) dumpWindowsLocked();
3907 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3908 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3909 if (DEBUG_REORDER) dumpWindowsLocked();
3910 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 mLayoutNeeded = true;
3912 performLayoutAndPlaceSurfacesLocked();
3913 }
3914 Binder.restoreCallingIdentity(origId);
3915 }
3916 }
3917
3918 private void removeAppTokensLocked(List<IBinder> tokens) {
3919 // XXX This should be done more efficiently!
3920 // (take advantage of the fact that both lists should be
3921 // ordered in the same way.)
3922 int N = tokens.size();
3923 for (int i=0; i<N; i++) {
3924 IBinder token = tokens.get(i);
3925 final AppWindowToken wtoken = findAppWindowToken(token);
3926 if (!mAppTokens.remove(wtoken)) {
3927 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3928 + token + " (" + wtoken + ")");
3929 i--;
3930 N--;
3931 }
3932 }
3933 }
3934
Dianne Hackborna8f60182009-09-01 19:01:50 -07003935 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3936 boolean updateFocusAndLayout) {
3937 // First remove all of the windows from the list.
3938 tmpRemoveAppWindowsLocked(wtoken);
3939
3940 // Where to start adding?
3941 int pos = findWindowOffsetLocked(tokenPos);
3942
3943 // And now add them back at the correct place.
3944 pos = reAddAppWindowsLocked(pos, wtoken);
3945
3946 if (updateFocusAndLayout) {
3947 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3948 assignLayersLocked();
3949 }
3950 mLayoutNeeded = true;
3951 performLayoutAndPlaceSurfacesLocked();
3952 }
3953 }
3954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3956 // First remove all of the windows from the list.
3957 final int N = tokens.size();
3958 int i;
3959 for (i=0; i<N; i++) {
3960 WindowToken token = mTokenMap.get(tokens.get(i));
3961 if (token != null) {
3962 tmpRemoveAppWindowsLocked(token);
3963 }
3964 }
3965
3966 // Where to start adding?
3967 int pos = findWindowOffsetLocked(tokenPos);
3968
3969 // And now add them back at the correct place.
3970 for (i=0; i<N; i++) {
3971 WindowToken token = mTokenMap.get(tokens.get(i));
3972 if (token != null) {
3973 pos = reAddAppWindowsLocked(pos, token);
3974 }
3975 }
3976
Dianne Hackborna8f60182009-09-01 19:01:50 -07003977 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3978 assignLayersLocked();
3979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 mLayoutNeeded = true;
3981 performLayoutAndPlaceSurfacesLocked();
3982
3983 //dump();
3984 }
3985
3986 public void moveAppTokensToTop(List<IBinder> tokens) {
3987 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3988 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003989 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003990 }
3991
3992 final long origId = Binder.clearCallingIdentity();
3993 synchronized(mWindowMap) {
3994 removeAppTokensLocked(tokens);
3995 final int N = tokens.size();
3996 for (int i=0; i<N; i++) {
3997 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3998 if (wt != null) {
3999 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004000 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004001 mToTopApps.remove(wt);
4002 mToBottomApps.remove(wt);
4003 mToTopApps.add(wt);
4004 wt.sendingToBottom = false;
4005 wt.sendingToTop = true;
4006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 }
4008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004009
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004010 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004011 moveAppWindowsLocked(tokens, mAppTokens.size());
4012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 }
4014 Binder.restoreCallingIdentity(origId);
4015 }
4016
4017 public void moveAppTokensToBottom(List<IBinder> tokens) {
4018 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4019 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004020 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 }
4022
4023 final long origId = Binder.clearCallingIdentity();
4024 synchronized(mWindowMap) {
4025 removeAppTokensLocked(tokens);
4026 final int N = tokens.size();
4027 int pos = 0;
4028 for (int i=0; i<N; i++) {
4029 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4030 if (wt != null) {
4031 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004032 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004033 mToTopApps.remove(wt);
4034 mToBottomApps.remove(wt);
4035 mToBottomApps.add(i, wt);
4036 wt.sendingToTop = false;
4037 wt.sendingToBottom = true;
4038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 pos++;
4040 }
4041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004042
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004043 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004044 moveAppWindowsLocked(tokens, 0);
4045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 }
4047 Binder.restoreCallingIdentity(origId);
4048 }
4049
4050 // -------------------------------------------------------------
4051 // Misc IWindowSession methods
4052 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004055 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 != PackageManager.PERMISSION_GRANTED) {
4057 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4058 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004059 synchronized (mKeyguardTokenWatcher) {
4060 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 }
4063
4064 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004065 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 != PackageManager.PERMISSION_GRANTED) {
4067 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4068 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004069 synchronized (mKeyguardTokenWatcher) {
4070 mKeyguardTokenWatcher.release(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071
Mike Lockwood983ee092009-11-22 01:42:24 -05004072 if (!mKeyguardTokenWatcher.isAcquired()) {
4073 // If we are the last one to reenable the keyguard wait until
4074 // we have actaully finished reenabling until returning.
4075 // It is possible that reenableKeyguard() can be called before
4076 // the previous disableKeyguard() is handled, in which case
4077 // neither mKeyguardTokenWatcher.acquired() or released() would
4078 // be called. In that case mKeyguardDisabled will be false here
4079 // and we have nothing to wait for.
4080 while (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 try {
Mike Lockwood983ee092009-11-22 01:42:24 -05004082 mKeyguardTokenWatcher.wait();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 } catch (InterruptedException e) {
4084 Thread.currentThread().interrupt();
4085 }
4086 }
4087 }
4088 }
4089 }
4090
4091 /**
4092 * @see android.app.KeyguardManager#exitKeyguardSecurely
4093 */
4094 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004095 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 != PackageManager.PERMISSION_GRANTED) {
4097 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4098 }
4099 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4100 public void onKeyguardExitResult(boolean success) {
4101 try {
4102 callback.onKeyguardExitResult(success);
4103 } catch (RemoteException e) {
4104 // Client has died, we don't care.
4105 }
4106 }
4107 });
4108 }
4109
4110 public boolean inKeyguardRestrictedInputMode() {
4111 return mPolicy.inKeyguardRestrictedKeyInputMode();
4112 }
Romain Guy06882f82009-06-10 13:36:04 -07004113
Dianne Hackbornffa42482009-09-23 22:20:11 -07004114 public void closeSystemDialogs(String reason) {
4115 synchronized(mWindowMap) {
4116 for (int i=mWindows.size()-1; i>=0; i--) {
4117 WindowState w = (WindowState)mWindows.get(i);
4118 if (w.mSurface != null) {
4119 try {
4120 w.mClient.closeSystemDialogs(reason);
4121 } catch (RemoteException e) {
4122 }
4123 }
4124 }
4125 }
4126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 static float fixScale(float scale) {
4129 if (scale < 0) scale = 0;
4130 else if (scale > 20) scale = 20;
4131 return Math.abs(scale);
4132 }
Romain Guy06882f82009-06-10 13:36:04 -07004133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 public void setAnimationScale(int which, float scale) {
4135 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4136 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004137 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 }
4139
4140 if (scale < 0) scale = 0;
4141 else if (scale > 20) scale = 20;
4142 scale = Math.abs(scale);
4143 switch (which) {
4144 case 0: mWindowAnimationScale = fixScale(scale); break;
4145 case 1: mTransitionAnimationScale = fixScale(scale); break;
4146 }
Romain Guy06882f82009-06-10 13:36:04 -07004147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 // Persist setting
4149 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4150 }
Romain Guy06882f82009-06-10 13:36:04 -07004151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 public void setAnimationScales(float[] scales) {
4153 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4154 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004155 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 }
4157
4158 if (scales != null) {
4159 if (scales.length >= 1) {
4160 mWindowAnimationScale = fixScale(scales[0]);
4161 }
4162 if (scales.length >= 2) {
4163 mTransitionAnimationScale = fixScale(scales[1]);
4164 }
4165 }
Romain Guy06882f82009-06-10 13:36:04 -07004166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 // Persist setting
4168 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4169 }
Romain Guy06882f82009-06-10 13:36:04 -07004170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 public float getAnimationScale(int which) {
4172 switch (which) {
4173 case 0: return mWindowAnimationScale;
4174 case 1: return mTransitionAnimationScale;
4175 }
4176 return 0;
4177 }
Romain Guy06882f82009-06-10 13:36:04 -07004178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 public float[] getAnimationScales() {
4180 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4181 }
Romain Guy06882f82009-06-10 13:36:04 -07004182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183 public int getSwitchState(int sw) {
4184 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4185 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004186 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 }
4188 return KeyInputQueue.getSwitchState(sw);
4189 }
Romain Guy06882f82009-06-10 13:36:04 -07004190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191 public int getSwitchStateForDevice(int devid, int sw) {
4192 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4193 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004194 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 }
4196 return KeyInputQueue.getSwitchState(devid, sw);
4197 }
Romain Guy06882f82009-06-10 13:36:04 -07004198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 public int getScancodeState(int sw) {
4200 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4201 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004202 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004204 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 }
Romain Guy06882f82009-06-10 13:36:04 -07004206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 public int getScancodeStateForDevice(int devid, int sw) {
4208 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4209 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004210 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004212 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 }
Romain Guy06882f82009-06-10 13:36:04 -07004214
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004215 public int getTrackballScancodeState(int sw) {
4216 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4217 "getTrackballScancodeState()")) {
4218 throw new SecurityException("Requires READ_INPUT_STATE permission");
4219 }
4220 return mQueue.getTrackballScancodeState(sw);
4221 }
4222
4223 public int getDPadScancodeState(int sw) {
4224 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4225 "getDPadScancodeState()")) {
4226 throw new SecurityException("Requires READ_INPUT_STATE permission");
4227 }
4228 return mQueue.getDPadScancodeState(sw);
4229 }
4230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 public int getKeycodeState(int sw) {
4232 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4233 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004234 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004236 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 }
Romain Guy06882f82009-06-10 13:36:04 -07004238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 public int getKeycodeStateForDevice(int devid, int sw) {
4240 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4241 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004242 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004244 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 }
Romain Guy06882f82009-06-10 13:36:04 -07004246
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004247 public int getTrackballKeycodeState(int sw) {
4248 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4249 "getTrackballKeycodeState()")) {
4250 throw new SecurityException("Requires READ_INPUT_STATE permission");
4251 }
4252 return mQueue.getTrackballKeycodeState(sw);
4253 }
4254
4255 public int getDPadKeycodeState(int sw) {
4256 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4257 "getDPadKeycodeState()")) {
4258 throw new SecurityException("Requires READ_INPUT_STATE permission");
4259 }
4260 return mQueue.getDPadKeycodeState(sw);
4261 }
4262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4264 return KeyInputQueue.hasKeys(keycodes, keyExists);
4265 }
Romain Guy06882f82009-06-10 13:36:04 -07004266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 public void enableScreenAfterBoot() {
4268 synchronized(mWindowMap) {
4269 if (mSystemBooted) {
4270 return;
4271 }
4272 mSystemBooted = true;
4273 }
Romain Guy06882f82009-06-10 13:36:04 -07004274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 performEnableScreen();
4276 }
Romain Guy06882f82009-06-10 13:36:04 -07004277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 public void enableScreenIfNeededLocked() {
4279 if (mDisplayEnabled) {
4280 return;
4281 }
4282 if (!mSystemBooted) {
4283 return;
4284 }
4285 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4286 }
Romain Guy06882f82009-06-10 13:36:04 -07004287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 public void performEnableScreen() {
4289 synchronized(mWindowMap) {
4290 if (mDisplayEnabled) {
4291 return;
4292 }
4293 if (!mSystemBooted) {
4294 return;
4295 }
Romain Guy06882f82009-06-10 13:36:04 -07004296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 // Don't enable the screen until all existing windows
4298 // have been drawn.
4299 final int N = mWindows.size();
4300 for (int i=0; i<N; i++) {
4301 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08004302 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 return;
4304 }
4305 }
Romain Guy06882f82009-06-10 13:36:04 -07004306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 mDisplayEnabled = true;
4308 if (false) {
4309 Log.i(TAG, "ENABLING SCREEN!");
4310 StringWriter sw = new StringWriter();
4311 PrintWriter pw = new PrintWriter(sw);
4312 this.dump(null, pw, null);
4313 Log.i(TAG, sw.toString());
4314 }
4315 try {
4316 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4317 if (surfaceFlinger != null) {
4318 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4319 Parcel data = Parcel.obtain();
4320 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4321 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4322 data, null, 0);
4323 data.recycle();
4324 }
4325 } catch (RemoteException ex) {
4326 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4327 }
4328 }
Romain Guy06882f82009-06-10 13:36:04 -07004329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004333 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4334 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335 }
Romain Guy06882f82009-06-10 13:36:04 -07004336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 public void setInTouchMode(boolean mode) {
4338 synchronized(mWindowMap) {
4339 mInTouchMode = mode;
4340 }
4341 }
4342
Romain Guy06882f82009-06-10 13:36:04 -07004343 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004344 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004346 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004347 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 }
4349
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004350 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 }
Romain Guy06882f82009-06-10 13:36:04 -07004352
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004353 public void setRotationUnchecked(int rotation,
4354 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 if(DEBUG_ORIENTATION) Log.v(TAG,
4356 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 long origId = Binder.clearCallingIdentity();
4359 boolean changed;
4360 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004361 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004362 }
Romain Guy06882f82009-06-10 13:36:04 -07004363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 if (changed) {
4365 sendNewConfiguration();
4366 synchronized(mWindowMap) {
4367 mLayoutNeeded = true;
4368 performLayoutAndPlaceSurfacesLocked();
4369 }
4370 } else if (alwaysSendConfiguration) {
4371 //update configuration ignoring orientation change
4372 sendNewConfiguration();
4373 }
Romain Guy06882f82009-06-10 13:36:04 -07004374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 Binder.restoreCallingIdentity(origId);
4376 }
Romain Guy06882f82009-06-10 13:36:04 -07004377
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004378 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 boolean changed;
4380 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4381 rotation = mRequestedRotation;
4382 } else {
4383 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004384 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004385 }
4386 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004387 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 mRotation, mDisplayEnabled);
4389 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4390 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004392 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004393 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 "Rotation changed to " + rotation
4395 + " from " + mRotation
4396 + " (forceApp=" + mForcedAppOrientation
4397 + ", req=" + mRequestedRotation + ")");
4398 mRotation = rotation;
4399 mWindowsFreezingScreen = true;
4400 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4401 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4402 2000);
4403 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004404 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 mQueue.setOrientation(rotation);
4406 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004407 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 }
4409 for (int i=mWindows.size()-1; i>=0; i--) {
4410 WindowState w = (WindowState)mWindows.get(i);
4411 if (w.mSurface != null) {
4412 w.mOrientationChanging = true;
4413 }
4414 }
4415 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4416 try {
4417 mRotationWatchers.get(i).onRotationChanged(rotation);
4418 } catch (RemoteException e) {
4419 }
4420 }
4421 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 return changed;
4424 }
Romain Guy06882f82009-06-10 13:36:04 -07004425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004426 public int getRotation() {
4427 return mRotation;
4428 }
4429
4430 public int watchRotation(IRotationWatcher watcher) {
4431 final IBinder watcherBinder = watcher.asBinder();
4432 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4433 public void binderDied() {
4434 synchronized (mWindowMap) {
4435 for (int i=0; i<mRotationWatchers.size(); i++) {
4436 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004437 IRotationWatcher removed = mRotationWatchers.remove(i);
4438 if (removed != null) {
4439 removed.asBinder().unlinkToDeath(this, 0);
4440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 i--;
4442 }
4443 }
4444 }
4445 }
4446 };
Romain Guy06882f82009-06-10 13:36:04 -07004447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 synchronized (mWindowMap) {
4449 try {
4450 watcher.asBinder().linkToDeath(dr, 0);
4451 mRotationWatchers.add(watcher);
4452 } catch (RemoteException e) {
4453 // Client died, no cleanup needed.
4454 }
Romain Guy06882f82009-06-10 13:36:04 -07004455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 return mRotation;
4457 }
4458 }
4459
4460 /**
4461 * Starts the view server on the specified port.
4462 *
4463 * @param port The port to listener to.
4464 *
4465 * @return True if the server was successfully started, false otherwise.
4466 *
4467 * @see com.android.server.ViewServer
4468 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4469 */
4470 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004471 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 return false;
4473 }
4474
4475 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4476 return false;
4477 }
4478
4479 if (port < 1024) {
4480 return false;
4481 }
4482
4483 if (mViewServer != null) {
4484 if (!mViewServer.isRunning()) {
4485 try {
4486 return mViewServer.start();
4487 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004488 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 }
4490 }
4491 return false;
4492 }
4493
4494 try {
4495 mViewServer = new ViewServer(this, port);
4496 return mViewServer.start();
4497 } catch (IOException e) {
4498 Log.w(TAG, "View server did not start");
4499 }
4500 return false;
4501 }
4502
Romain Guy06882f82009-06-10 13:36:04 -07004503 private boolean isSystemSecure() {
4504 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4505 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4506 }
4507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 /**
4509 * Stops the view server if it exists.
4510 *
4511 * @return True if the server stopped, false if it wasn't started or
4512 * couldn't be stopped.
4513 *
4514 * @see com.android.server.ViewServer
4515 */
4516 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004517 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 return false;
4519 }
4520
4521 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4522 return false;
4523 }
4524
4525 if (mViewServer != null) {
4526 return mViewServer.stop();
4527 }
4528 return false;
4529 }
4530
4531 /**
4532 * Indicates whether the view server is running.
4533 *
4534 * @return True if the server is running, false otherwise.
4535 *
4536 * @see com.android.server.ViewServer
4537 */
4538 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004539 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 return false;
4541 }
4542
4543 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4544 return false;
4545 }
4546
4547 return mViewServer != null && mViewServer.isRunning();
4548 }
4549
4550 /**
4551 * Lists all availble windows in the system. The listing is written in the
4552 * specified Socket's output stream with the following syntax:
4553 * windowHashCodeInHexadecimal windowName
4554 * Each line of the ouput represents a different window.
4555 *
4556 * @param client The remote client to send the listing to.
4557 * @return False if an error occured, true otherwise.
4558 */
4559 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004560 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004561 return false;
4562 }
4563
4564 boolean result = true;
4565
4566 Object[] windows;
4567 synchronized (mWindowMap) {
4568 windows = new Object[mWindows.size()];
4569 //noinspection unchecked
4570 windows = mWindows.toArray(windows);
4571 }
4572
4573 BufferedWriter out = null;
4574
4575 // Any uncaught exception will crash the system process
4576 try {
4577 OutputStream clientStream = client.getOutputStream();
4578 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4579
4580 final int count = windows.length;
4581 for (int i = 0; i < count; i++) {
4582 final WindowState w = (WindowState) windows[i];
4583 out.write(Integer.toHexString(System.identityHashCode(w)));
4584 out.write(' ');
4585 out.append(w.mAttrs.getTitle());
4586 out.write('\n');
4587 }
4588
4589 out.write("DONE.\n");
4590 out.flush();
4591 } catch (Exception e) {
4592 result = false;
4593 } finally {
4594 if (out != null) {
4595 try {
4596 out.close();
4597 } catch (IOException e) {
4598 result = false;
4599 }
4600 }
4601 }
4602
4603 return result;
4604 }
4605
4606 /**
4607 * Sends a command to a target window. The result of the command, if any, will be
4608 * written in the output stream of the specified socket.
4609 *
4610 * The parameters must follow this syntax:
4611 * windowHashcode extra
4612 *
4613 * Where XX is the length in characeters of the windowTitle.
4614 *
4615 * The first parameter is the target window. The window with the specified hashcode
4616 * will be the target. If no target can be found, nothing happens. The extra parameters
4617 * will be delivered to the target window and as parameters to the command itself.
4618 *
4619 * @param client The remote client to sent the result, if any, to.
4620 * @param command The command to execute.
4621 * @param parameters The command parameters.
4622 *
4623 * @return True if the command was successfully delivered, false otherwise. This does
4624 * not indicate whether the command itself was successful.
4625 */
4626 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004627 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004628 return false;
4629 }
4630
4631 boolean success = true;
4632 Parcel data = null;
4633 Parcel reply = null;
4634
4635 // Any uncaught exception will crash the system process
4636 try {
4637 // Find the hashcode of the window
4638 int index = parameters.indexOf(' ');
4639 if (index == -1) {
4640 index = parameters.length();
4641 }
4642 final String code = parameters.substring(0, index);
4643 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4644
4645 // Extract the command's parameter after the window description
4646 if (index < parameters.length()) {
4647 parameters = parameters.substring(index + 1);
4648 } else {
4649 parameters = "";
4650 }
4651
4652 final WindowManagerService.WindowState window = findWindow(hashCode);
4653 if (window == null) {
4654 return false;
4655 }
4656
4657 data = Parcel.obtain();
4658 data.writeInterfaceToken("android.view.IWindow");
4659 data.writeString(command);
4660 data.writeString(parameters);
4661 data.writeInt(1);
4662 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4663
4664 reply = Parcel.obtain();
4665
4666 final IBinder binder = window.mClient.asBinder();
4667 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4668 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4669
4670 reply.readException();
4671
4672 } catch (Exception e) {
4673 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4674 success = false;
4675 } finally {
4676 if (data != null) {
4677 data.recycle();
4678 }
4679 if (reply != null) {
4680 reply.recycle();
4681 }
4682 }
4683
4684 return success;
4685 }
4686
4687 private WindowState findWindow(int hashCode) {
4688 if (hashCode == -1) {
4689 return getFocusedWindow();
4690 }
4691
4692 synchronized (mWindowMap) {
4693 final ArrayList windows = mWindows;
4694 final int count = windows.size();
4695
4696 for (int i = 0; i < count; i++) {
4697 WindowState w = (WindowState) windows.get(i);
4698 if (System.identityHashCode(w) == hashCode) {
4699 return w;
4700 }
4701 }
4702 }
4703
4704 return null;
4705 }
4706
4707 /*
4708 * Instruct the Activity Manager to fetch the current configuration and broadcast
4709 * that to config-changed listeners if appropriate.
4710 */
4711 void sendNewConfiguration() {
4712 try {
4713 mActivityManager.updateConfiguration(null);
4714 } catch (RemoteException e) {
4715 }
4716 }
Romain Guy06882f82009-06-10 13:36:04 -07004717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 public Configuration computeNewConfiguration() {
4719 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004720 return computeNewConfigurationLocked();
4721 }
4722 }
Romain Guy06882f82009-06-10 13:36:04 -07004723
Dianne Hackbornc485a602009-03-24 22:39:49 -07004724 Configuration computeNewConfigurationLocked() {
4725 Configuration config = new Configuration();
4726 if (!computeNewConfigurationLocked(config)) {
4727 return null;
4728 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004729 return config;
4730 }
Romain Guy06882f82009-06-10 13:36:04 -07004731
Dianne Hackbornc485a602009-03-24 22:39:49 -07004732 boolean computeNewConfigurationLocked(Configuration config) {
4733 if (mDisplay == null) {
4734 return false;
4735 }
4736 mQueue.getInputConfiguration(config);
4737 final int dw = mDisplay.getWidth();
4738 final int dh = mDisplay.getHeight();
4739 int orientation = Configuration.ORIENTATION_SQUARE;
4740 if (dw < dh) {
4741 orientation = Configuration.ORIENTATION_PORTRAIT;
4742 } else if (dw > dh) {
4743 orientation = Configuration.ORIENTATION_LANDSCAPE;
4744 }
4745 config.orientation = orientation;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004746
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004747 DisplayMetrics dm = new DisplayMetrics();
4748 mDisplay.getMetrics(dm);
4749 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4750
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004751 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004752 // Note we only do this once because at this point we don't
4753 // expect the screen to change in this way at runtime, and want
4754 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004755 int longSize = dw;
4756 int shortSize = dh;
4757 if (longSize < shortSize) {
4758 int tmp = longSize;
4759 longSize = shortSize;
4760 shortSize = tmp;
4761 }
4762 longSize = (int)(longSize/dm.density);
4763 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004764
Dianne Hackborn723738c2009-06-25 19:48:04 -07004765 // These semi-magic numbers define our compatibility modes for
4766 // applications with different screens. Don't change unless you
4767 // make sure to test lots and lots of apps!
4768 if (longSize < 470) {
4769 // This is shorter than an HVGA normal density screen (which
4770 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004771 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4772 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004773 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004774 // Is this a large screen?
4775 if (longSize > 640 && shortSize >= 480) {
4776 // VGA or larger screens at medium density are the point
4777 // at which we consider it to be a large screen.
4778 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4779 } else {
4780 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004781
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004782 // If this screen is wider than normal HVGA, or taller
4783 // than FWVGA, then for old apps we want to run in size
4784 // compatibility mode.
4785 if (shortSize > 321 || longSize > 570) {
4786 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4787 }
4788 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004789
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004790 // Is this a long screen?
4791 if (((longSize*3)/5) >= (shortSize-1)) {
4792 // Anything wider than WVGA (5:3) is considering to be long.
4793 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4794 } else {
4795 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4796 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004797 }
4798 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004799 config.screenLayout = mScreenLayout;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004800
Dianne Hackbornc485a602009-03-24 22:39:49 -07004801 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4802 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4803 mPolicy.adjustConfigurationLw(config);
4804 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004805 }
Romain Guy06882f82009-06-10 13:36:04 -07004806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 // -------------------------------------------------------------
4808 // Input Events and Focus Management
4809 // -------------------------------------------------------------
4810
4811 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004812 long curTime = SystemClock.uptimeMillis();
4813
Michael Chane10de972009-05-18 11:24:50 -07004814 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004815 if (mLastTouchEventType == eventType &&
4816 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4817 return;
4818 }
4819 mLastUserActivityCallTime = curTime;
4820 mLastTouchEventType = eventType;
4821 }
4822
4823 if (targetWin == null
4824 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4825 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826 }
4827 }
4828
4829 // tells if it's a cheek event or not -- this function is stateful
4830 private static final int EVENT_NONE = 0;
4831 private static final int EVENT_UNKNOWN = 0;
4832 private static final int EVENT_CHEEK = 0;
4833 private static final int EVENT_IGNORE_DURATION = 300; // ms
4834 private static final float CHEEK_THRESHOLD = 0.6f;
4835 private int mEventState = EVENT_NONE;
4836 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 private int eventType(MotionEvent ev) {
4839 float size = ev.getSize();
4840 switch (ev.getAction()) {
4841 case MotionEvent.ACTION_DOWN:
4842 mEventSize = size;
4843 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4844 case MotionEvent.ACTION_UP:
4845 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004846 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847 case MotionEvent.ACTION_MOVE:
4848 final int N = ev.getHistorySize();
4849 if (size > mEventSize) mEventSize = size;
4850 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4851 for (int i=0; i<N; i++) {
4852 size = ev.getHistoricalSize(i);
4853 if (size > mEventSize) mEventSize = size;
4854 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4855 }
4856 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4857 return TOUCH_EVENT;
4858 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004859 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860 }
4861 default:
4862 // not good
4863 return OTHER_EVENT;
4864 }
4865 }
4866
4867 /**
4868 * @return Returns true if event was dispatched, false if it was dropped for any reason
4869 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004870 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4872 "dispatchPointer " + ev);
4873
Michael Chan53071d62009-05-13 17:29:48 -07004874 if (MEASURE_LATENCY) {
4875 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4876 }
4877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004879 ev, true, false, pid, uid);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004880
Michael Chan53071d62009-05-13 17:29:48 -07004881 if (MEASURE_LATENCY) {
4882 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4883 }
4884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 if (action == MotionEvent.ACTION_UP) {
4888 // let go of our target
4889 mKeyWaiter.mMotionTarget = null;
4890 mPowerManager.logPointerUpEvent();
4891 } else if (action == MotionEvent.ACTION_DOWN) {
4892 mPowerManager.logPointerDownEvent();
4893 }
4894
4895 if (targetObj == null) {
4896 // In this case we are either dropping the event, or have received
4897 // a move or up without a down. It is common to receive move
4898 // events in such a way, since this means the user is moving the
4899 // pointer without actually pressing down. All other cases should
4900 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004901 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4903 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08004904 synchronized (mWindowMap) {
4905 if (mSendingPointersToWallpaper) {
4906 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4907 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4908 }
4909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004910 if (qev != null) {
4911 mQueue.recycleEvent(qev);
4912 }
4913 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004914 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004915 }
4916 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004917 synchronized (mWindowMap) {
4918 if (mSendingPointersToWallpaper) {
4919 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4920 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4921 }
4922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004923 if (qev != null) {
4924 mQueue.recycleEvent(qev);
4925 }
4926 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004927 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004928 }
Romain Guy06882f82009-06-10 13:36:04 -07004929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004933 final long eventTimeNano = ev.getEventTimeNano();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935 //Log.i(TAG, "Sending " + ev + " to " + target);
4936
4937 if (uid != 0 && uid != target.mSession.mUid) {
4938 if (mContext.checkPermission(
4939 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4940 != PackageManager.PERMISSION_GRANTED) {
4941 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4942 + pid + " uid " + uid + " to window " + target
4943 + " owned by uid " + target.mSession.mUid);
4944 if (qev != null) {
4945 mQueue.recycleEvent(qev);
4946 }
4947 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004948 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004949 }
4950 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004951
Michael Chan53071d62009-05-13 17:29:48 -07004952 if (MEASURE_LATENCY) {
4953 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4954 }
4955
Romain Guy06882f82009-06-10 13:36:04 -07004956 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004957 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4958 //target wants to ignore fat touch events
4959 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4960 //explicit flag to return without processing event further
4961 boolean returnFlag = false;
4962 if((action == MotionEvent.ACTION_DOWN)) {
4963 mFatTouch = false;
4964 if(cheekPress) {
4965 mFatTouch = true;
4966 returnFlag = true;
4967 }
4968 } else {
4969 if(action == MotionEvent.ACTION_UP) {
4970 if(mFatTouch) {
4971 //earlier even was invalid doesnt matter if current up is cheekpress or not
4972 mFatTouch = false;
4973 returnFlag = true;
4974 } else if(cheekPress) {
4975 //cancel the earlier event
4976 ev.setAction(MotionEvent.ACTION_CANCEL);
4977 action = MotionEvent.ACTION_CANCEL;
4978 }
4979 } else if(action == MotionEvent.ACTION_MOVE) {
4980 if(mFatTouch) {
4981 //two cases here
4982 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004983 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004984 returnFlag = true;
4985 } else if(cheekPress) {
4986 //valid down followed by invalid moves
4987 //an invalid move have to cancel earlier action
4988 ev.setAction(MotionEvent.ACTION_CANCEL);
4989 action = MotionEvent.ACTION_CANCEL;
4990 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4991 //note that the subsequent invalid moves will not get here
4992 mFatTouch = true;
4993 }
4994 }
4995 } //else if action
4996 if(returnFlag) {
4997 //recycle que, ev
4998 if (qev != null) {
4999 mQueue.recycleEvent(qev);
5000 }
5001 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005002 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005003 }
5004 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005005
Michael Chan9f028e62009-08-04 17:37:46 -07005006 // Enable this for testing the "right" value
5007 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005008 int max_events_per_sec = 35;
5009 try {
5010 max_events_per_sec = Integer.parseInt(SystemProperties
5011 .get("windowsmgr.max_events_per_sec"));
5012 if (max_events_per_sec < 1) {
5013 max_events_per_sec = 35;
5014 }
5015 } catch (NumberFormatException e) {
5016 }
5017 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5018 }
5019
5020 /*
5021 * Throttle events to minimize CPU usage when there's a flood of events
5022 * e.g. constant contact with the screen
5023 */
5024 if (action == MotionEvent.ACTION_MOVE) {
5025 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5026 long now = SystemClock.uptimeMillis();
5027 if (now < nextEventTime) {
5028 try {
5029 Thread.sleep(nextEventTime - now);
5030 } catch (InterruptedException e) {
5031 }
5032 mLastTouchEventTime = nextEventTime;
5033 } else {
5034 mLastTouchEventTime = now;
5035 }
5036 }
5037
Michael Chan53071d62009-05-13 17:29:48 -07005038 if (MEASURE_LATENCY) {
5039 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5040 }
5041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005043 if (!target.isVisibleLw()) {
5044 // During this motion dispatch, the target window has become
5045 // invisible.
5046 if (mSendingPointersToWallpaper) {
5047 sendPointerToWallpaperLocked(null, ev, eventTime);
5048 }
5049 if (qev != null) {
5050 mQueue.recycleEvent(qev);
5051 }
5052 ev.recycle();
5053 return INJECT_SUCCEEDED;
5054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005056 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5057 mKeyWaiter.bindTargetWindowLocked(target,
5058 KeyWaiter.RETURN_PENDING_POINTER, qev);
5059 ev = null;
5060 } else {
5061 if (action == MotionEvent.ACTION_DOWN) {
5062 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5063 if (out != null) {
5064 MotionEvent oev = MotionEvent.obtain(ev);
5065 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5066 do {
5067 final Rect frame = out.mFrame;
5068 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5069 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005070 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 } catch (android.os.RemoteException e) {
5072 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
5073 }
5074 oev.offsetLocation((float)frame.left, (float)frame.top);
5075 out = out.mNextOutsideTouch;
5076 } while (out != null);
5077 mKeyWaiter.mOutsideTouchTargets = null;
5078 }
5079 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005080
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005081 // If we are on top of the wallpaper, then the wallpaper also
5082 // gets to see this movement.
Dianne Hackborn6adba242009-11-10 11:10:09 -08005083 if (mWallpaperTarget == target || mSendingPointersToWallpaper) {
5084 sendPointerToWallpaperLocked(null, ev, eventTime);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005085 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005086
Dianne Hackborn6adba242009-11-10 11:10:09 -08005087 final Rect frame = target.mFrame;
5088 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5089 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 }
5091 }
Romain Guy06882f82009-06-10 13:36:04 -07005092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005093 // finally offset the event to the target's coordinate system and
5094 // dispatch the event.
5095 try {
5096 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
5097 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
5098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005099
Michael Chan53071d62009-05-13 17:29:48 -07005100 if (MEASURE_LATENCY) {
5101 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5102 }
5103
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005104 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005105
5106 if (MEASURE_LATENCY) {
5107 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5108 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005109 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 } catch (android.os.RemoteException e) {
5111 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
5112 mKeyWaiter.mMotionTarget = null;
5113 try {
5114 removeWindow(target.mSession, target.mClient);
5115 } catch (java.util.NoSuchElementException ex) {
5116 // This will happen if the window has already been
5117 // removed.
5118 }
5119 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005120 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005121 }
Romain Guy06882f82009-06-10 13:36:04 -07005122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 /**
5124 * @return Returns true if event was dispatched, false if it was dropped for any reason
5125 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005126 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 if (DEBUG_INPUT) Log.v(
5128 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005131 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 if (focusObj == null) {
5133 Log.w(TAG, "No focus window, dropping trackball: " + ev);
5134 if (qev != null) {
5135 mQueue.recycleEvent(qev);
5136 }
5137 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005138 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 }
5140 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5141 if (qev != null) {
5142 mQueue.recycleEvent(qev);
5143 }
5144 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005145 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 }
Romain Guy06882f82009-06-10 13:36:04 -07005147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 if (uid != 0 && uid != focus.mSession.mUid) {
5151 if (mContext.checkPermission(
5152 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5153 != PackageManager.PERMISSION_GRANTED) {
5154 Log.w(TAG, "Permission denied: injecting key event from pid "
5155 + pid + " uid " + uid + " to window " + focus
5156 + " owned by uid " + focus.mSession.mUid);
5157 if (qev != null) {
5158 mQueue.recycleEvent(qev);
5159 }
5160 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005161 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 }
5163 }
Romain Guy06882f82009-06-10 13:36:04 -07005164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005165 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 synchronized(mWindowMap) {
5168 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5169 mKeyWaiter.bindTargetWindowLocked(focus,
5170 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5171 // We don't deliver movement events to the client, we hold
5172 // them and wait for them to call back.
5173 ev = null;
5174 } else {
5175 mKeyWaiter.bindTargetWindowLocked(focus);
5176 }
5177 }
Romain Guy06882f82009-06-10 13:36:04 -07005178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005180 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005181 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005182 } catch (android.os.RemoteException e) {
5183 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5184 try {
5185 removeWindow(focus.mSession, focus.mClient);
5186 } catch (java.util.NoSuchElementException ex) {
5187 // This will happen if the window has already been
5188 // removed.
5189 }
5190 }
Romain Guy06882f82009-06-10 13:36:04 -07005191
Dianne Hackborncfaef692009-06-15 14:24:44 -07005192 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 }
Romain Guy06882f82009-06-10 13:36:04 -07005194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 /**
5196 * @return Returns true if event was dispatched, false if it was dropped for any reason
5197 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005198 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5200
5201 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005202 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 if (focusObj == null) {
5204 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005205 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005206 }
5207 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005208 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 }
Romain Guy06882f82009-06-10 13:36:04 -07005210
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005211 // Okay we have finished waiting for the last event to be processed.
5212 // First off, if this is a repeat event, check to see if there is
5213 // a corresponding up event in the queue. If there is, we will
5214 // just drop the repeat, because it makes no sense to repeat after
5215 // the user has released a key. (This is especially important for
5216 // long presses.)
5217 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5218 return INJECT_SUCCEEDED;
5219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 if (DEBUG_INPUT) Log.v(
5224 TAG, "Dispatching to " + focus + ": " + event);
5225
5226 if (uid != 0 && uid != focus.mSession.mUid) {
5227 if (mContext.checkPermission(
5228 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5229 != PackageManager.PERMISSION_GRANTED) {
5230 Log.w(TAG, "Permission denied: injecting key event from pid "
5231 + pid + " uid " + uid + " to window " + focus
5232 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005233 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 }
5235 }
Romain Guy06882f82009-06-10 13:36:04 -07005236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 synchronized(mWindowMap) {
5238 mKeyWaiter.bindTargetWindowLocked(focus);
5239 }
5240
5241 // NOSHIP extra state logging
5242 mKeyWaiter.recordDispatchState(event, focus);
5243 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 try {
5246 if (DEBUG_INPUT || DEBUG_FOCUS) {
5247 Log.v(TAG, "Delivering key " + event.getKeyCode()
5248 + " to " + focus);
5249 }
5250 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005251 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 } catch (android.os.RemoteException e) {
5253 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5254 try {
5255 removeWindow(focus.mSession, focus.mClient);
5256 } catch (java.util.NoSuchElementException ex) {
5257 // This will happen if the window has already been
5258 // removed.
5259 }
5260 }
Romain Guy06882f82009-06-10 13:36:04 -07005261
Dianne Hackborncfaef692009-06-15 14:24:44 -07005262 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 }
Romain Guy06882f82009-06-10 13:36:04 -07005264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005265 public void pauseKeyDispatching(IBinder _token) {
5266 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5267 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005268 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269 }
5270
5271 synchronized (mWindowMap) {
5272 WindowToken token = mTokenMap.get(_token);
5273 if (token != null) {
5274 mKeyWaiter.pauseDispatchingLocked(token);
5275 }
5276 }
5277 }
5278
5279 public void resumeKeyDispatching(IBinder _token) {
5280 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5281 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005282 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 }
5284
5285 synchronized (mWindowMap) {
5286 WindowToken token = mTokenMap.get(_token);
5287 if (token != null) {
5288 mKeyWaiter.resumeDispatchingLocked(token);
5289 }
5290 }
5291 }
5292
5293 public void setEventDispatching(boolean enabled) {
5294 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5295 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005296 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 }
5298
5299 synchronized (mWindowMap) {
5300 mKeyWaiter.setEventDispatchingLocked(enabled);
5301 }
5302 }
Romain Guy06882f82009-06-10 13:36:04 -07005303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005304 /**
5305 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005306 *
5307 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 * {@link SystemClock#uptimeMillis()} as the timebase.)
5309 * @param sync If true, wait for the event to be completed before returning to the caller.
5310 * @return Returns true if event was dispatched, false if it was dropped for any reason
5311 */
5312 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5313 long downTime = ev.getDownTime();
5314 long eventTime = ev.getEventTime();
5315
5316 int action = ev.getAction();
5317 int code = ev.getKeyCode();
5318 int repeatCount = ev.getRepeatCount();
5319 int metaState = ev.getMetaState();
5320 int deviceId = ev.getDeviceId();
5321 int scancode = ev.getScanCode();
5322
5323 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5324 if (downTime == 0) downTime = eventTime;
5325
5326 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005327 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005328
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005329 final int pid = Binder.getCallingPid();
5330 final int uid = Binder.getCallingUid();
5331 final long ident = Binder.clearCallingIdentity();
5332 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005333 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005334 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005335 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005336 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005337 switch (result) {
5338 case INJECT_NO_PERMISSION:
5339 throw new SecurityException(
5340 "Injecting to another application requires INJECT_EVENT permission");
5341 case INJECT_SUCCEEDED:
5342 return true;
5343 }
5344 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005345 }
5346
5347 /**
5348 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005349 *
5350 * @param ev A motion event describing the pointer (touch) action. (As noted in
5351 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 * {@link SystemClock#uptimeMillis()} as the timebase.)
5353 * @param sync If true, wait for the event to be completed before returning to the caller.
5354 * @return Returns true if event was dispatched, false if it was dropped for any reason
5355 */
5356 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005357 final int pid = Binder.getCallingPid();
5358 final int uid = Binder.getCallingUid();
5359 final long ident = Binder.clearCallingIdentity();
5360 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005362 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005364 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005365 switch (result) {
5366 case INJECT_NO_PERMISSION:
5367 throw new SecurityException(
5368 "Injecting to another application requires INJECT_EVENT permission");
5369 case INJECT_SUCCEEDED:
5370 return true;
5371 }
5372 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 }
Romain Guy06882f82009-06-10 13:36:04 -07005374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 /**
5376 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005377 *
5378 * @param ev A motion event describing the trackball action. (As noted in
5379 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005380 * {@link SystemClock#uptimeMillis()} as the timebase.)
5381 * @param sync If true, wait for the event to be completed before returning to the caller.
5382 * @return Returns true if event was dispatched, false if it was dropped for any reason
5383 */
5384 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005385 final int pid = Binder.getCallingPid();
5386 final int uid = Binder.getCallingUid();
5387 final long ident = Binder.clearCallingIdentity();
5388 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005390 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005392 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005393 switch (result) {
5394 case INJECT_NO_PERMISSION:
5395 throw new SecurityException(
5396 "Injecting to another application requires INJECT_EVENT permission");
5397 case INJECT_SUCCEEDED:
5398 return true;
5399 }
5400 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 }
Romain Guy06882f82009-06-10 13:36:04 -07005402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 private WindowState getFocusedWindow() {
5404 synchronized (mWindowMap) {
5405 return getFocusedWindowLocked();
5406 }
5407 }
5408
5409 private WindowState getFocusedWindowLocked() {
5410 return mCurrentFocus;
5411 }
Romain Guy06882f82009-06-10 13:36:04 -07005412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005413 /**
5414 * This class holds the state for dispatching key events. This state
5415 * is protected by the KeyWaiter instance, NOT by the window lock. You
5416 * can be holding the main window lock while acquire the KeyWaiter lock,
5417 * but not the other way around.
5418 */
5419 final class KeyWaiter {
5420 // NOSHIP debugging
5421 public class DispatchState {
5422 private KeyEvent event;
5423 private WindowState focus;
5424 private long time;
5425 private WindowState lastWin;
5426 private IBinder lastBinder;
5427 private boolean finished;
5428 private boolean gotFirstWindow;
5429 private boolean eventDispatching;
5430 private long timeToSwitch;
5431 private boolean wasFrozen;
5432 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005433 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5436 focus = theFocus;
5437 event = theEvent;
5438 time = System.currentTimeMillis();
5439 // snapshot KeyWaiter state
5440 lastWin = mLastWin;
5441 lastBinder = mLastBinder;
5442 finished = mFinished;
5443 gotFirstWindow = mGotFirstWindow;
5444 eventDispatching = mEventDispatching;
5445 timeToSwitch = mTimeToSwitch;
5446 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005447 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 // cache the paused state at ctor time as well
5449 if (theFocus == null || theFocus.mToken == null) {
5450 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5451 focusPaused = false;
5452 } else {
5453 focusPaused = theFocus.mToken.paused;
5454 }
5455 }
Romain Guy06882f82009-06-10 13:36:04 -07005456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005457 public String toString() {
5458 return "{{" + event + " to " + focus + " @ " + time
5459 + " lw=" + lastWin + " lb=" + lastBinder
5460 + " fin=" + finished + " gfw=" + gotFirstWindow
5461 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005462 + " wf=" + wasFrozen + " fp=" + focusPaused
5463 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 }
5465 };
5466 private DispatchState mDispatchState = null;
5467 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5468 mDispatchState = new DispatchState(theEvent, theFocus);
5469 }
5470 // END NOSHIP
5471
5472 public static final int RETURN_NOTHING = 0;
5473 public static final int RETURN_PENDING_POINTER = 1;
5474 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 final Object SKIP_TARGET_TOKEN = new Object();
5477 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005479 private WindowState mLastWin = null;
5480 private IBinder mLastBinder = null;
5481 private boolean mFinished = true;
5482 private boolean mGotFirstWindow = false;
5483 private boolean mEventDispatching = true;
5484 private long mTimeToSwitch = 0;
5485 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 // Target of Motion events
5488 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 // Windows above the target who would like to receive an "outside"
5491 // touch event for any down events outside of them.
5492 WindowState mOutsideTouchTargets;
5493
5494 /**
5495 * Wait for the last event dispatch to complete, then find the next
5496 * target that should receive the given event and wait for that one
5497 * to be ready to receive it.
5498 */
5499 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5500 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005501 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 long startTime = SystemClock.uptimeMillis();
5503 long keyDispatchingTimeout = 5 * 1000;
5504 long waitedFor = 0;
5505
5506 while (true) {
5507 // Figure out which window we care about. It is either the
5508 // last window we are waiting to have process the event or,
5509 // if none, then the next window we think the event should go
5510 // to. Note: we retrieve mLastWin outside of the lock, so
5511 // it may change before we lock. Thus we must check it again.
5512 WindowState targetWin = mLastWin;
5513 boolean targetIsNew = targetWin == null;
5514 if (DEBUG_INPUT) Log.v(
5515 TAG, "waitForLastKey: mFinished=" + mFinished +
5516 ", mLastWin=" + mLastWin);
5517 if (targetIsNew) {
5518 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005519 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 if (target == SKIP_TARGET_TOKEN) {
5521 // The user has pressed a special key, and we are
5522 // dropping all pending events before it.
5523 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5524 + " " + nextMotion);
5525 return null;
5526 }
5527 if (target == CONSUMED_EVENT_TOKEN) {
5528 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5529 + " " + nextMotion);
5530 return target;
5531 }
5532 targetWin = (WindowState)target;
5533 }
Romain Guy06882f82009-06-10 13:36:04 -07005534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 // Now: is it okay to send the next event to this window?
5538 synchronized (this) {
5539 // First: did we come here based on the last window not
5540 // being null, but it changed by the time we got here?
5541 // If so, try again.
5542 if (!targetIsNew && mLastWin == null) {
5543 continue;
5544 }
Romain Guy06882f82009-06-10 13:36:04 -07005545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005546 // We never dispatch events if not finished with the
5547 // last one, or the display is frozen.
5548 if (mFinished && !mDisplayFrozen) {
5549 // If event dispatching is disabled, then we
5550 // just consume the events.
5551 if (!mEventDispatching) {
5552 if (DEBUG_INPUT) Log.v(TAG,
5553 "Skipping event; dispatching disabled: "
5554 + nextKey + " " + nextMotion);
5555 return null;
5556 }
5557 if (targetWin != null) {
5558 // If this is a new target, and that target is not
5559 // paused or unresponsive, then all looks good to
5560 // handle the event.
5561 if (targetIsNew && !targetWin.mToken.paused) {
5562 return targetWin;
5563 }
Romain Guy06882f82009-06-10 13:36:04 -07005564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 // If we didn't find a target window, and there is no
5566 // focused app window, then just eat the events.
5567 } else if (mFocusedApp == null) {
5568 if (DEBUG_INPUT) Log.v(TAG,
5569 "Skipping event; no focused app: "
5570 + nextKey + " " + nextMotion);
5571 return null;
5572 }
5573 }
Romain Guy06882f82009-06-10 13:36:04 -07005574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005575 if (DEBUG_INPUT) Log.v(
5576 TAG, "Waiting for last key in " + mLastBinder
5577 + " target=" + targetWin
5578 + " mFinished=" + mFinished
5579 + " mDisplayFrozen=" + mDisplayFrozen
5580 + " targetIsNew=" + targetIsNew
5581 + " paused="
5582 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005583 + " mFocusedApp=" + mFocusedApp
5584 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005586 targetApp = targetWin != null
5587 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005589 long curTimeout = keyDispatchingTimeout;
5590 if (mTimeToSwitch != 0) {
5591 long now = SystemClock.uptimeMillis();
5592 if (mTimeToSwitch <= now) {
5593 // If an app switch key has been pressed, and we have
5594 // waited too long for the current app to finish
5595 // processing keys, then wait no more!
5596 doFinishedKeyLocked(true);
5597 continue;
5598 }
5599 long switchTimeout = mTimeToSwitch - now;
5600 if (curTimeout > switchTimeout) {
5601 curTimeout = switchTimeout;
5602 }
5603 }
Romain Guy06882f82009-06-10 13:36:04 -07005604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005605 try {
5606 // after that continue
5607 // processing keys, so we don't get stuck.
5608 if (DEBUG_INPUT) Log.v(
5609 TAG, "Waiting for key dispatch: " + curTimeout);
5610 wait(curTimeout);
5611 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5612 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005613 + startTime + " switchTime=" + mTimeToSwitch
5614 + " target=" + targetWin + " mLW=" + mLastWin
5615 + " mLB=" + mLastBinder + " fin=" + mFinished
5616 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617 } catch (InterruptedException e) {
5618 }
5619 }
5620
5621 // If we were frozen during configuration change, restart the
5622 // timeout checks from now; otherwise look at whether we timed
5623 // out before awakening.
5624 if (mWasFrozen) {
5625 waitedFor = 0;
5626 mWasFrozen = false;
5627 } else {
5628 waitedFor = SystemClock.uptimeMillis() - startTime;
5629 }
5630
5631 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5632 IApplicationToken at = null;
5633 synchronized (this) {
5634 Log.w(TAG, "Key dispatching timed out sending to " +
5635 (targetWin != null ? targetWin.mAttrs.getTitle()
5636 : "<null>"));
5637 // NOSHIP debugging
5638 Log.w(TAG, "Dispatch state: " + mDispatchState);
5639 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5640 // END NOSHIP
5641 //dump();
5642 if (targetWin != null) {
5643 at = targetWin.getAppToken();
5644 } else if (targetApp != null) {
5645 at = targetApp.appToken;
5646 }
5647 }
5648
5649 boolean abort = true;
5650 if (at != null) {
5651 try {
5652 long timeout = at.getKeyDispatchingTimeout();
5653 if (timeout > waitedFor) {
5654 // we did not wait the proper amount of time for this application.
5655 // set the timeout to be the real timeout and wait again.
5656 keyDispatchingTimeout = timeout - waitedFor;
5657 continue;
5658 } else {
5659 abort = at.keyDispatchingTimedOut();
5660 }
5661 } catch (RemoteException ex) {
5662 }
5663 }
5664
5665 synchronized (this) {
5666 if (abort && (mLastWin == targetWin || targetWin == null)) {
5667 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005668 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669 if (DEBUG_INPUT) Log.v(TAG,
5670 "Window " + mLastWin +
5671 " timed out on key input");
5672 if (mLastWin.mToken.paused) {
5673 Log.w(TAG, "Un-pausing dispatching to this window");
5674 mLastWin.mToken.paused = false;
5675 }
5676 }
5677 if (mMotionTarget == targetWin) {
5678 mMotionTarget = null;
5679 }
5680 mLastWin = null;
5681 mLastBinder = null;
5682 if (failIfTimeout || targetWin == null) {
5683 return null;
5684 }
5685 } else {
5686 Log.w(TAG, "Continuing to wait for key to be dispatched");
5687 startTime = SystemClock.uptimeMillis();
5688 }
5689 }
5690 }
5691 }
5692 }
Romain Guy06882f82009-06-10 13:36:04 -07005693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005694 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005695 MotionEvent nextMotion, boolean isPointerEvent,
5696 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005697 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 if (nextKey != null) {
5700 // Find the target window for a normal key event.
5701 final int keycode = nextKey.getKeyCode();
5702 final int repeatCount = nextKey.getRepeatCount();
5703 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5704 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005707 if (callingUid == 0 ||
5708 mContext.checkPermission(
5709 android.Manifest.permission.INJECT_EVENTS,
5710 callingPid, callingUid)
5711 == PackageManager.PERMISSION_GRANTED) {
5712 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005713 nextKey.getMetaState(), down, repeatCount,
5714 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005716 Log.w(TAG, "Event timeout during app switch: dropping "
5717 + nextKey);
5718 return SKIP_TARGET_TOKEN;
5719 }
Romain Guy06882f82009-06-10 13:36:04 -07005720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005721 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005723 WindowState focus = null;
5724 synchronized(mWindowMap) {
5725 focus = getFocusedWindowLocked();
5726 }
Romain Guy06882f82009-06-10 13:36:04 -07005727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005729
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005730 if (callingUid == 0 ||
5731 (focus != null && callingUid == focus.mSession.mUid) ||
5732 mContext.checkPermission(
5733 android.Manifest.permission.INJECT_EVENTS,
5734 callingPid, callingUid)
5735 == PackageManager.PERMISSION_GRANTED) {
5736 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005737 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005738 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005739 return CONSUMED_EVENT_TOKEN;
5740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 }
Romain Guy06882f82009-06-10 13:36:04 -07005742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 } else if (!isPointerEvent) {
5746 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5747 if (!dispatch) {
5748 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5749 + nextMotion);
5750 return SKIP_TARGET_TOKEN;
5751 }
Romain Guy06882f82009-06-10 13:36:04 -07005752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005753 WindowState focus = null;
5754 synchronized(mWindowMap) {
5755 focus = getFocusedWindowLocked();
5756 }
Romain Guy06882f82009-06-10 13:36:04 -07005757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5759 return focus;
5760 }
Romain Guy06882f82009-06-10 13:36:04 -07005761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 if (nextMotion == null) {
5763 return SKIP_TARGET_TOKEN;
5764 }
Romain Guy06882f82009-06-10 13:36:04 -07005765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5767 KeyEvent.KEYCODE_UNKNOWN);
5768 if (!dispatch) {
5769 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5770 + nextMotion);
5771 return SKIP_TARGET_TOKEN;
5772 }
Romain Guy06882f82009-06-10 13:36:04 -07005773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 // Find the target window for a pointer event.
5775 int action = nextMotion.getAction();
5776 final float xf = nextMotion.getX();
5777 final float yf = nextMotion.getY();
5778 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 final boolean screenWasOff = qev != null
5781 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005785 synchronized(mWindowMap) {
5786 synchronized (this) {
5787 if (action == MotionEvent.ACTION_DOWN) {
5788 if (mMotionTarget != null) {
5789 // this is weird, we got a pen down, but we thought it was
5790 // already down!
5791 // XXX: We should probably send an ACTION_UP to the current
5792 // target.
5793 Log.w(TAG, "Pointer down received while already down in: "
5794 + mMotionTarget);
5795 mMotionTarget = null;
5796 }
Romain Guy06882f82009-06-10 13:36:04 -07005797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 // ACTION_DOWN is special, because we need to lock next events to
5799 // the window we'll land onto.
5800 final int x = (int)xf;
5801 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 final ArrayList windows = mWindows;
5804 final int N = windows.size();
5805 WindowState topErrWindow = null;
5806 final Rect tmpRect = mTempRect;
5807 for (int i=N-1; i>=0; i--) {
5808 WindowState child = (WindowState)windows.get(i);
5809 //Log.i(TAG, "Checking dispatch to: " + child);
5810 final int flags = child.mAttrs.flags;
5811 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5812 if (topErrWindow == null) {
5813 topErrWindow = child;
5814 }
5815 }
5816 if (!child.isVisibleLw()) {
5817 //Log.i(TAG, "Not visible!");
5818 continue;
5819 }
5820 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5821 //Log.i(TAG, "Not touchable!");
5822 if ((flags & WindowManager.LayoutParams
5823 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5824 child.mNextOutsideTouch = mOutsideTouchTargets;
5825 mOutsideTouchTargets = child;
5826 }
5827 continue;
5828 }
5829 tmpRect.set(child.mFrame);
5830 if (child.mTouchableInsets == ViewTreeObserver
5831 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5832 // The touch is inside of the window if it is
5833 // inside the frame, AND the content part of that
5834 // frame that was given by the application.
5835 tmpRect.left += child.mGivenContentInsets.left;
5836 tmpRect.top += child.mGivenContentInsets.top;
5837 tmpRect.right -= child.mGivenContentInsets.right;
5838 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5839 } else if (child.mTouchableInsets == ViewTreeObserver
5840 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5841 // The touch is inside of the window if it is
5842 // inside the frame, AND the visible part of that
5843 // frame that was given by the application.
5844 tmpRect.left += child.mGivenVisibleInsets.left;
5845 tmpRect.top += child.mGivenVisibleInsets.top;
5846 tmpRect.right -= child.mGivenVisibleInsets.right;
5847 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5848 }
5849 final int touchFlags = flags &
5850 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5851 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5852 if (tmpRect.contains(x, y) || touchFlags == 0) {
5853 //Log.i(TAG, "Using this target!");
5854 if (!screenWasOff || (flags &
5855 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5856 mMotionTarget = child;
5857 } else {
5858 //Log.i(TAG, "Waking, skip!");
5859 mMotionTarget = null;
5860 }
5861 break;
5862 }
Romain Guy06882f82009-06-10 13:36:04 -07005863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 if ((flags & WindowManager.LayoutParams
5865 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5866 child.mNextOutsideTouch = mOutsideTouchTargets;
5867 mOutsideTouchTargets = child;
5868 //Log.i(TAG, "Adding to outside target list: " + child);
5869 }
5870 }
5871
5872 // if there's an error window but it's not accepting
5873 // focus (typically because it is not yet visible) just
5874 // wait for it -- any other focused window may in fact
5875 // be in ANR state.
5876 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5877 mMotionTarget = null;
5878 }
5879 }
Romain Guy06882f82009-06-10 13:36:04 -07005880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005881 target = mMotionTarget;
5882 }
5883 }
Romain Guy06882f82009-06-10 13:36:04 -07005884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005885 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 // Pointer events are a little different -- if there isn't a
5888 // target found for any event, then just drop it.
5889 return target != null ? target : SKIP_TARGET_TOKEN;
5890 }
Romain Guy06882f82009-06-10 13:36:04 -07005891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 boolean checkShouldDispatchKey(int keycode) {
5893 synchronized (this) {
5894 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5895 mTimeToSwitch = 0;
5896 return true;
5897 }
5898 if (mTimeToSwitch != 0
5899 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5900 return false;
5901 }
5902 return true;
5903 }
5904 }
Romain Guy06882f82009-06-10 13:36:04 -07005905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005906 void bindTargetWindowLocked(WindowState win,
5907 int pendingWhat, QueuedEvent pendingMotion) {
5908 synchronized (this) {
5909 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5910 }
5911 }
Romain Guy06882f82009-06-10 13:36:04 -07005912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913 void bindTargetWindowLocked(WindowState win) {
5914 synchronized (this) {
5915 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5916 }
5917 }
5918
5919 void bindTargetWindowLockedLocked(WindowState win,
5920 int pendingWhat, QueuedEvent pendingMotion) {
5921 mLastWin = win;
5922 mLastBinder = win.mClient.asBinder();
5923 mFinished = false;
5924 if (pendingMotion != null) {
5925 final Session s = win.mSession;
5926 if (pendingWhat == RETURN_PENDING_POINTER) {
5927 releasePendingPointerLocked(s);
5928 s.mPendingPointerMove = pendingMotion;
5929 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005930 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 "bindTargetToWindow " + s.mPendingPointerMove);
5932 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5933 releasePendingTrackballLocked(s);
5934 s.mPendingTrackballMove = pendingMotion;
5935 s.mPendingTrackballWindow = win;
5936 }
5937 }
5938 }
Romain Guy06882f82009-06-10 13:36:04 -07005939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940 void releasePendingPointerLocked(Session s) {
5941 if (DEBUG_INPUT) Log.v(TAG,
5942 "releasePendingPointer " + s.mPendingPointerMove);
5943 if (s.mPendingPointerMove != null) {
5944 mQueue.recycleEvent(s.mPendingPointerMove);
5945 s.mPendingPointerMove = null;
5946 }
5947 }
Romain Guy06882f82009-06-10 13:36:04 -07005948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 void releasePendingTrackballLocked(Session s) {
5950 if (s.mPendingTrackballMove != null) {
5951 mQueue.recycleEvent(s.mPendingTrackballMove);
5952 s.mPendingTrackballMove = null;
5953 }
5954 }
Romain Guy06882f82009-06-10 13:36:04 -07005955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5957 int returnWhat) {
5958 if (DEBUG_INPUT) Log.v(
5959 TAG, "finishedKey: client=" + client + ", force=" + force);
5960
5961 if (client == null) {
5962 return null;
5963 }
5964
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005965 MotionEvent res = null;
5966 QueuedEvent qev = null;
5967 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005969 synchronized (this) {
5970 if (DEBUG_INPUT) Log.v(
5971 TAG, "finishedKey: client=" + client.asBinder()
5972 + ", force=" + force + ", last=" + mLastBinder
5973 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 if (returnWhat == RETURN_PENDING_POINTER) {
5976 qev = session.mPendingPointerMove;
5977 win = session.mPendingPointerWindow;
5978 session.mPendingPointerMove = null;
5979 session.mPendingPointerWindow = null;
5980 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5981 qev = session.mPendingTrackballMove;
5982 win = session.mPendingTrackballWindow;
5983 session.mPendingTrackballMove = null;
5984 session.mPendingTrackballWindow = null;
5985 }
Romain Guy06882f82009-06-10 13:36:04 -07005986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005987 if (mLastBinder == client.asBinder()) {
5988 if (DEBUG_INPUT) Log.v(
5989 TAG, "finishedKey: last paused="
5990 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5991 if (mLastWin != null && (!mLastWin.mToken.paused || force
5992 || !mEventDispatching)) {
5993 doFinishedKeyLocked(false);
5994 } else {
5995 // Make sure to wake up anyone currently waiting to
5996 // dispatch a key, so they can re-evaluate their
5997 // current situation.
5998 mFinished = true;
5999 notifyAll();
6000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006001 }
Romain Guy06882f82009-06-10 13:36:04 -07006002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006004 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 if (DEBUG_INPUT) Log.v(TAG,
6006 "Returning pending motion: " + res);
6007 mQueue.recycleEvent(qev);
6008 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6009 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006012 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006013
Christopher Tate2624fbc2009-12-11 12:11:31 -08006014 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6015 synchronized (mWindowMap) {
6016 if ((mWallpaperTarget == win &&
6017 win.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD)
6018 || mSendingPointersToWallpaper) {
6019 sendPointerToWallpaperLocked(win, res, res.getEventTime());
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006020 }
6021 }
6022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006023
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006024 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006025 }
6026
6027 void tickle() {
6028 synchronized (this) {
6029 notifyAll();
6030 }
6031 }
Romain Guy06882f82009-06-10 13:36:04 -07006032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 void handleNewWindowLocked(WindowState newWindow) {
6034 if (!newWindow.canReceiveKeys()) {
6035 return;
6036 }
6037 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006038 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 TAG, "New key dispatch window: win="
6040 + newWindow.mClient.asBinder()
6041 + ", last=" + mLastBinder
6042 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6043 + "), finished=" + mFinished + ", paused="
6044 + newWindow.mToken.paused);
6045
6046 // Displaying a window implicitly causes dispatching to
6047 // be unpaused. (This is to protect against bugs if someone
6048 // pauses dispatching but forgets to resume.)
6049 newWindow.mToken.paused = false;
6050
6051 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052
6053 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
6054 if (DEBUG_INPUT) Log.v(TAG,
6055 "New SYSTEM_ERROR window; resetting state");
6056 mLastWin = null;
6057 mLastBinder = null;
6058 mMotionTarget = null;
6059 mFinished = true;
6060 } else if (mLastWin != null) {
6061 // If the new window is above the window we are
6062 // waiting on, then stop waiting and let key dispatching
6063 // start on the new guy.
6064 if (DEBUG_INPUT) Log.v(
6065 TAG, "Last win layer=" + mLastWin.mLayer
6066 + ", new win layer=" + newWindow.mLayer);
6067 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006068 // The new window is above the old; finish pending input to the last
6069 // window and start directing it to the new one.
6070 mLastWin.mToken.paused = false;
6071 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006073 // Either the new window is lower, so there is no need to wake key waiters,
6074 // or we just finished key input to the previous window, which implicitly
6075 // notified the key waiters. In both cases, we don't need to issue the
6076 // notification here.
6077 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 }
6079
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006080 // Now that we've put a new window state in place, make the event waiter
6081 // take notice and retarget its attentions.
6082 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 }
6084 }
6085
6086 void pauseDispatchingLocked(WindowToken token) {
6087 synchronized (this)
6088 {
6089 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
6090 token.paused = true;
6091
6092 /*
6093 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6094 mPaused = true;
6095 } else {
6096 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07006097 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07006099 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07006101 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 }
6103 }
6104 */
6105 }
6106 }
6107
6108 void resumeDispatchingLocked(WindowToken token) {
6109 synchronized (this) {
6110 if (token.paused) {
6111 if (DEBUG_INPUT) Log.v(
6112 TAG, "Resuming WindowToken " + token
6113 + ", last=" + mLastBinder
6114 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6115 + "), finished=" + mFinished + ", paused="
6116 + token.paused);
6117 token.paused = false;
6118 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
6119 doFinishedKeyLocked(true);
6120 } else {
6121 notifyAll();
6122 }
6123 }
6124 }
6125 }
6126
6127 void setEventDispatchingLocked(boolean enabled) {
6128 synchronized (this) {
6129 mEventDispatching = enabled;
6130 notifyAll();
6131 }
6132 }
Romain Guy06882f82009-06-10 13:36:04 -07006133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006134 void appSwitchComing() {
6135 synchronized (this) {
6136 // Don't wait for more than .5 seconds for app to finish
6137 // processing the pending events.
6138 long now = SystemClock.uptimeMillis() + 500;
6139 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6140 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6141 mTimeToSwitch = now;
6142 }
6143 notifyAll();
6144 }
6145 }
Romain Guy06882f82009-06-10 13:36:04 -07006146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006147 private final void doFinishedKeyLocked(boolean doRecycle) {
6148 if (mLastWin != null) {
6149 releasePendingPointerLocked(mLastWin.mSession);
6150 releasePendingTrackballLocked(mLastWin.mSession);
6151 }
Romain Guy06882f82009-06-10 13:36:04 -07006152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 if (mLastWin == null || !mLastWin.mToken.paused
6154 || !mLastWin.isVisibleLw()) {
6155 // If the current window has been paused, we aren't -really-
6156 // finished... so let the waiters still wait.
6157 mLastWin = null;
6158 mLastBinder = null;
6159 }
6160 mFinished = true;
6161 notifyAll();
6162 }
6163 }
6164
6165 private class KeyQ extends KeyInputQueue
6166 implements KeyInputQueue.FilterCallback {
6167 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006170 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006171 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6172 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6173 "KEEP_SCREEN_ON_FLAG");
6174 mHoldingScreen.setReferenceCounted(false);
6175 }
6176
6177 @Override
6178 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6179 if (mPolicy.preprocessInputEventTq(event)) {
6180 return true;
6181 }
Romain Guy06882f82009-06-10 13:36:04 -07006182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183 switch (event.type) {
6184 case RawInputEvent.EV_KEY: {
6185 // XXX begin hack
6186 if (DEBUG) {
6187 if (event.keycode == KeyEvent.KEYCODE_G) {
6188 if (event.value != 0) {
6189 // G down
6190 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6191 }
6192 return false;
6193 }
6194 if (event.keycode == KeyEvent.KEYCODE_D) {
6195 if (event.value != 0) {
6196 //dump();
6197 }
6198 return false;
6199 }
6200 }
6201 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006202
Charles Mendis322591c2009-10-29 11:06:59 -07006203 boolean screenIsOff = !mPowerManager.isScreenOn();
6204 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006207 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6208 mPowerManager.goToSleep(event.when);
6209 }
6210
6211 if (screenIsOff) {
6212 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6213 }
6214 if (screenIsDim) {
6215 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6216 }
6217 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6218 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006219 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006220 }
Romain Guy06882f82009-06-10 13:36:04 -07006221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006222 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6223 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6224 filterQueue(this);
6225 mKeyWaiter.appSwitchComing();
6226 }
6227 return true;
6228 } else {
6229 return false;
6230 }
6231 }
Romain Guy06882f82009-06-10 13:36:04 -07006232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006234 boolean screenIsOff = !mPowerManager.isScreenOn();
6235 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006236 if (screenIsOff) {
6237 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6238 device.classes, event)) {
6239 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6240 return false;
6241 }
6242 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6243 }
6244 if (screenIsDim) {
6245 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6246 }
6247 return true;
6248 }
Romain Guy06882f82009-06-10 13:36:04 -07006249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006251 boolean screenIsOff = !mPowerManager.isScreenOn();
6252 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006253 if (screenIsOff) {
6254 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6255 device.classes, event)) {
6256 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6257 return false;
6258 }
6259 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6260 }
6261 if (screenIsDim) {
6262 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6263 }
6264 return true;
6265 }
Romain Guy06882f82009-06-10 13:36:04 -07006266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006267 default:
6268 return true;
6269 }
6270 }
6271
6272 public int filterEvent(QueuedEvent ev) {
6273 switch (ev.classType) {
6274 case RawInputEvent.CLASS_KEYBOARD:
6275 KeyEvent ke = (KeyEvent)ev.event;
6276 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6277 Log.w(TAG, "Dropping movement key during app switch: "
6278 + ke.getKeyCode() + ", action=" + ke.getAction());
6279 return FILTER_REMOVE;
6280 }
6281 return FILTER_ABORT;
6282 default:
6283 return FILTER_KEEP;
6284 }
6285 }
Romain Guy06882f82009-06-10 13:36:04 -07006286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006287 /**
6288 * Must be called with the main window manager lock held.
6289 */
6290 void setHoldScreenLocked(boolean holding) {
6291 boolean state = mHoldingScreen.isHeld();
6292 if (holding != state) {
6293 if (holding) {
6294 mHoldingScreen.acquire();
6295 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006296 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006297 mHoldingScreen.release();
6298 }
6299 }
6300 }
Michael Chan53071d62009-05-13 17:29:48 -07006301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302
6303 public boolean detectSafeMode() {
6304 mSafeMode = mPolicy.detectSafeMode();
6305 return mSafeMode;
6306 }
Romain Guy06882f82009-06-10 13:36:04 -07006307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 public void systemReady() {
6309 mPolicy.systemReady();
6310 }
Romain Guy06882f82009-06-10 13:36:04 -07006311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006312 private final class InputDispatcherThread extends Thread {
6313 // Time to wait when there is nothing to do: 9999 seconds.
6314 static final int LONG_WAIT=9999*1000;
6315
6316 public InputDispatcherThread() {
6317 super("InputDispatcher");
6318 }
Romain Guy06882f82009-06-10 13:36:04 -07006319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006320 @Override
6321 public void run() {
6322 while (true) {
6323 try {
6324 process();
6325 } catch (Exception e) {
6326 Log.e(TAG, "Exception in input dispatcher", e);
6327 }
6328 }
6329 }
Romain Guy06882f82009-06-10 13:36:04 -07006330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 private void process() {
6332 android.os.Process.setThreadPriority(
6333 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 // The last key event we saw
6336 KeyEvent lastKey = null;
6337
6338 // Last keydown time for auto-repeating keys
6339 long lastKeyTime = SystemClock.uptimeMillis();
6340 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006341 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342
Romain Guy06882f82009-06-10 13:36:04 -07006343 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 int keyRepeatCount = 0;
6345
6346 // Need to report that configuration has changed?
6347 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 while (true) {
6350 long curTime = SystemClock.uptimeMillis();
6351
6352 if (DEBUG_INPUT) Log.v(
6353 TAG, "Waiting for next key: now=" + curTime
6354 + ", repeat @ " + nextKeyTime);
6355
6356 // Retrieve next event, waiting only as long as the next
6357 // repeat timeout. If the configuration has changed, then
6358 // don't wait at all -- we'll report the change as soon as
6359 // we have processed all events.
6360 QueuedEvent ev = mQueue.getEvent(
6361 (int)((!configChanged && curTime < nextKeyTime)
6362 ? (nextKeyTime-curTime) : 0));
6363
6364 if (DEBUG_INPUT && ev != null) Log.v(
6365 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6366
Michael Chan53071d62009-05-13 17:29:48 -07006367 if (MEASURE_LATENCY) {
6368 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6369 }
6370
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006371 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6372 // cancel key repeat at the request of the policy.
6373 lastKey = null;
6374 downTime = 0;
6375 lastKeyTime = curTime;
6376 nextKeyTime = curTime + LONG_WAIT;
6377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 try {
6379 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006380 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 int eventType;
6382 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6383 eventType = eventType((MotionEvent)ev.event);
6384 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6385 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6386 eventType = LocalPowerManager.BUTTON_EVENT;
6387 } else {
6388 eventType = LocalPowerManager.OTHER_EVENT;
6389 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006390 try {
Michael Chan53071d62009-05-13 17:29:48 -07006391 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006392 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006393 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006394 mBatteryStats.noteInputEvent();
6395 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006396 } catch (RemoteException e) {
6397 // Ignore
6398 }
Michael Chane10de972009-05-18 11:24:50 -07006399
Mike Lockwood5db42402009-11-30 14:51:51 -05006400 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6401 // do not wake screen in this case
6402 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006403 && eventType != LONG_TOUCH_EVENT
6404 && eventType != CHEEK_EVENT) {
6405 mPowerManager.userActivity(curTime, false,
6406 eventType, false);
6407 } else if (mLastTouchEventType != eventType
6408 || (curTime - mLastUserActivityCallTime)
6409 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6410 mLastUserActivityCallTime = curTime;
6411 mLastTouchEventType = eventType;
6412 mPowerManager.userActivity(curTime, false,
6413 eventType, false);
6414 }
6415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 switch (ev.classType) {
6417 case RawInputEvent.CLASS_KEYBOARD:
6418 KeyEvent ke = (KeyEvent)ev.event;
6419 if (ke.isDown()) {
6420 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006421 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006422 keyRepeatCount = 0;
6423 lastKeyTime = curTime;
6424 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006425 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006426 if (DEBUG_INPUT) Log.v(
6427 TAG, "Received key down: first repeat @ "
6428 + nextKeyTime);
6429 } else {
6430 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006431 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006432 // Arbitrary long timeout.
6433 lastKeyTime = curTime;
6434 nextKeyTime = curTime + LONG_WAIT;
6435 if (DEBUG_INPUT) Log.v(
6436 TAG, "Received key up: ignore repeat @ "
6437 + nextKeyTime);
6438 }
6439 dispatchKey((KeyEvent)ev.event, 0, 0);
6440 mQueue.recycleEvent(ev);
6441 break;
6442 case RawInputEvent.CLASS_TOUCHSCREEN:
6443 //Log.i(TAG, "Read next event " + ev);
6444 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6445 break;
6446 case RawInputEvent.CLASS_TRACKBALL:
6447 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6448 break;
6449 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6450 configChanged = true;
6451 break;
6452 default:
6453 mQueue.recycleEvent(ev);
6454 break;
6455 }
Romain Guy06882f82009-06-10 13:36:04 -07006456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457 } else if (configChanged) {
6458 configChanged = false;
6459 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006461 } else if (lastKey != null) {
6462 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 // Timeout occurred while key was down. If it is at or
6465 // past the key repeat time, dispatch the repeat.
6466 if (DEBUG_INPUT) Log.v(
6467 TAG, "Key timeout: repeat=" + nextKeyTime
6468 + ", now=" + curTime);
6469 if (curTime < nextKeyTime) {
6470 continue;
6471 }
Romain Guy06882f82009-06-10 13:36:04 -07006472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006473 lastKeyTime = nextKeyTime;
6474 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6475 keyRepeatCount++;
6476 if (DEBUG_INPUT) Log.v(
6477 TAG, "Key repeat: count=" + keyRepeatCount
6478 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006479 KeyEvent newEvent;
6480 if (downTime != 0 && (downTime
6481 + ViewConfiguration.getLongPressTimeout())
6482 <= curTime) {
6483 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6484 curTime, keyRepeatCount,
6485 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6486 downTime = 0;
6487 } else {
6488 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6489 curTime, keyRepeatCount);
6490 }
6491 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 } else {
6494 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006496 lastKeyTime = curTime;
6497 nextKeyTime = curTime + LONG_WAIT;
6498 }
Romain Guy06882f82009-06-10 13:36:04 -07006499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500 } catch (Exception e) {
6501 Log.e(TAG,
6502 "Input thread received uncaught exception: " + e, e);
6503 }
6504 }
6505 }
6506 }
6507
6508 // -------------------------------------------------------------
6509 // Client Session State
6510 // -------------------------------------------------------------
6511
6512 private final class Session extends IWindowSession.Stub
6513 implements IBinder.DeathRecipient {
6514 final IInputMethodClient mClient;
6515 final IInputContext mInputContext;
6516 final int mUid;
6517 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006518 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006519 SurfaceSession mSurfaceSession;
6520 int mNumWindow = 0;
6521 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006523 /**
6524 * Current pointer move event being dispatched to client window... must
6525 * hold key lock to access.
6526 */
6527 QueuedEvent mPendingPointerMove;
6528 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006530 /**
6531 * Current trackball move event being dispatched to client window... must
6532 * hold key lock to access.
6533 */
6534 QueuedEvent mPendingTrackballMove;
6535 WindowState mPendingTrackballWindow;
6536
6537 public Session(IInputMethodClient client, IInputContext inputContext) {
6538 mClient = client;
6539 mInputContext = inputContext;
6540 mUid = Binder.getCallingUid();
6541 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006542 StringBuilder sb = new StringBuilder();
6543 sb.append("Session{");
6544 sb.append(Integer.toHexString(System.identityHashCode(this)));
6545 sb.append(" uid ");
6546 sb.append(mUid);
6547 sb.append("}");
6548 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 synchronized (mWindowMap) {
6551 if (mInputMethodManager == null && mHaveInputMethods) {
6552 IBinder b = ServiceManager.getService(
6553 Context.INPUT_METHOD_SERVICE);
6554 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6555 }
6556 }
6557 long ident = Binder.clearCallingIdentity();
6558 try {
6559 // Note: it is safe to call in to the input method manager
6560 // here because we are not holding our lock.
6561 if (mInputMethodManager != null) {
6562 mInputMethodManager.addClient(client, inputContext,
6563 mUid, mPid);
6564 } else {
6565 client.setUsingInputMethod(false);
6566 }
6567 client.asBinder().linkToDeath(this, 0);
6568 } catch (RemoteException e) {
6569 // The caller has died, so we can just forget about this.
6570 try {
6571 if (mInputMethodManager != null) {
6572 mInputMethodManager.removeClient(client);
6573 }
6574 } catch (RemoteException ee) {
6575 }
6576 } finally {
6577 Binder.restoreCallingIdentity(ident);
6578 }
6579 }
Romain Guy06882f82009-06-10 13:36:04 -07006580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006581 @Override
6582 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6583 throws RemoteException {
6584 try {
6585 return super.onTransact(code, data, reply, flags);
6586 } catch (RuntimeException e) {
6587 // Log all 'real' exceptions thrown to the caller
6588 if (!(e instanceof SecurityException)) {
6589 Log.e(TAG, "Window Session Crash", e);
6590 }
6591 throw e;
6592 }
6593 }
6594
6595 public void binderDied() {
6596 // Note: it is safe to call in to the input method manager
6597 // here because we are not holding our lock.
6598 try {
6599 if (mInputMethodManager != null) {
6600 mInputMethodManager.removeClient(mClient);
6601 }
6602 } catch (RemoteException e) {
6603 }
6604 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006605 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 mClientDead = true;
6607 killSessionLocked();
6608 }
6609 }
6610
6611 public int add(IWindow window, WindowManager.LayoutParams attrs,
6612 int viewVisibility, Rect outContentInsets) {
6613 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6614 }
Romain Guy06882f82009-06-10 13:36:04 -07006615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 public void remove(IWindow window) {
6617 removeWindow(this, window);
6618 }
Romain Guy06882f82009-06-10 13:36:04 -07006619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006620 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6621 int requestedWidth, int requestedHeight, int viewFlags,
6622 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6623 Rect outVisibleInsets, Surface outSurface) {
6624 return relayoutWindow(this, window, attrs,
6625 requestedWidth, requestedHeight, viewFlags, insetsPending,
6626 outFrame, outContentInsets, outVisibleInsets, outSurface);
6627 }
Romain Guy06882f82009-06-10 13:36:04 -07006628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006629 public void setTransparentRegion(IWindow window, Region region) {
6630 setTransparentRegionWindow(this, window, region);
6631 }
Romain Guy06882f82009-06-10 13:36:04 -07006632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006633 public void setInsets(IWindow window, int touchableInsets,
6634 Rect contentInsets, Rect visibleInsets) {
6635 setInsetsWindow(this, window, touchableInsets, contentInsets,
6636 visibleInsets);
6637 }
Romain Guy06882f82009-06-10 13:36:04 -07006638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006639 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6640 getWindowDisplayFrame(this, window, outDisplayFrame);
6641 }
Romain Guy06882f82009-06-10 13:36:04 -07006642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006643 public void finishDrawing(IWindow window) {
6644 if (localLOGV) Log.v(
6645 TAG, "IWindow finishDrawing called for " + window);
6646 finishDrawingWindow(this, window);
6647 }
6648
6649 public void finishKey(IWindow window) {
6650 if (localLOGV) Log.v(
6651 TAG, "IWindow finishKey called for " + window);
6652 mKeyWaiter.finishedKey(this, window, false,
6653 KeyWaiter.RETURN_NOTHING);
6654 }
6655
6656 public MotionEvent getPendingPointerMove(IWindow window) {
6657 if (localLOGV) Log.v(
6658 TAG, "IWindow getPendingMotionEvent called for " + window);
6659 return mKeyWaiter.finishedKey(this, window, false,
6660 KeyWaiter.RETURN_PENDING_POINTER);
6661 }
Romain Guy06882f82009-06-10 13:36:04 -07006662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006663 public MotionEvent getPendingTrackballMove(IWindow window) {
6664 if (localLOGV) Log.v(
6665 TAG, "IWindow getPendingMotionEvent called for " + window);
6666 return mKeyWaiter.finishedKey(this, window, false,
6667 KeyWaiter.RETURN_PENDING_TRACKBALL);
6668 }
6669
6670 public void setInTouchMode(boolean mode) {
6671 synchronized(mWindowMap) {
6672 mInTouchMode = mode;
6673 }
6674 }
6675
6676 public boolean getInTouchMode() {
6677 synchronized(mWindowMap) {
6678 return mInTouchMode;
6679 }
6680 }
6681
6682 public boolean performHapticFeedback(IWindow window, int effectId,
6683 boolean always) {
6684 synchronized(mWindowMap) {
6685 long ident = Binder.clearCallingIdentity();
6686 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006687 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688 windowForClientLocked(this, window), effectId, always);
6689 } finally {
6690 Binder.restoreCallingIdentity(ident);
6691 }
6692 }
6693 }
Romain Guy06882f82009-06-10 13:36:04 -07006694
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006695 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006696 synchronized(mWindowMap) {
6697 long ident = Binder.clearCallingIdentity();
6698 try {
6699 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006700 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006701 } finally {
6702 Binder.restoreCallingIdentity(ident);
6703 }
6704 }
6705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006706
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006707 public void wallpaperOffsetsComplete(IBinder window) {
6708 WindowManagerService.this.wallpaperOffsetsComplete(window);
6709 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006710
Dianne Hackborn75804932009-10-20 20:15:20 -07006711 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6712 int z, Bundle extras, boolean sync) {
6713 synchronized(mWindowMap) {
6714 long ident = Binder.clearCallingIdentity();
6715 try {
6716 return sendWindowWallpaperCommandLocked(
6717 windowForClientLocked(this, window),
6718 action, x, y, z, extras, sync);
6719 } finally {
6720 Binder.restoreCallingIdentity(ident);
6721 }
6722 }
6723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006724
Dianne Hackborn75804932009-10-20 20:15:20 -07006725 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6726 WindowManagerService.this.wallpaperCommandComplete(window, result);
6727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 void windowAddedLocked() {
6730 if (mSurfaceSession == null) {
6731 if (localLOGV) Log.v(
6732 TAG, "First window added to " + this + ", creating SurfaceSession");
6733 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006734 if (SHOW_TRANSACTIONS) Log.i(
6735 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006736 mSessions.add(this);
6737 }
6738 mNumWindow++;
6739 }
6740
6741 void windowRemovedLocked() {
6742 mNumWindow--;
6743 killSessionLocked();
6744 }
Romain Guy06882f82009-06-10 13:36:04 -07006745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 void killSessionLocked() {
6747 if (mNumWindow <= 0 && mClientDead) {
6748 mSessions.remove(this);
6749 if (mSurfaceSession != null) {
6750 if (localLOGV) Log.v(
6751 TAG, "Last window removed from " + this
6752 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006753 if (SHOW_TRANSACTIONS) Log.i(
6754 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006755 try {
6756 mSurfaceSession.kill();
6757 } catch (Exception e) {
6758 Log.w(TAG, "Exception thrown when killing surface session "
6759 + mSurfaceSession + " in session " + this
6760 + ": " + e.toString());
6761 }
6762 mSurfaceSession = null;
6763 }
6764 }
6765 }
Romain Guy06882f82009-06-10 13:36:04 -07006766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006767 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006768 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6769 pw.print(" mClientDead="); pw.print(mClientDead);
6770 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6771 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6772 pw.print(prefix);
6773 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6774 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6775 }
6776 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6777 pw.print(prefix);
6778 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6779 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 }
6782
6783 @Override
6784 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006785 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006786 }
6787 }
6788
6789 // -------------------------------------------------------------
6790 // Client Window State
6791 // -------------------------------------------------------------
6792
6793 private final class WindowState implements WindowManagerPolicy.WindowState {
6794 final Session mSession;
6795 final IWindow mClient;
6796 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006797 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006798 AppWindowToken mAppToken;
6799 AppWindowToken mTargetAppToken;
6800 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6801 final DeathRecipient mDeathRecipient;
6802 final WindowState mAttachedWindow;
6803 final ArrayList mChildWindows = new ArrayList();
6804 final int mBaseLayer;
6805 final int mSubLayer;
6806 final boolean mLayoutAttached;
6807 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006808 final boolean mIsWallpaper;
6809 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006810 int mViewVisibility;
6811 boolean mPolicyVisibility = true;
6812 boolean mPolicyVisibilityAfterAnim = true;
6813 boolean mAppFreezing;
6814 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006815 boolean mReportDestroySurface;
6816 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006817 boolean mAttachedHidden; // is our parent window hidden?
6818 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006819 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 int mRequestedWidth;
6821 int mRequestedHeight;
6822 int mLastRequestedWidth;
6823 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006824 int mLayer;
6825 int mAnimLayer;
6826 int mLastLayer;
6827 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006828 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006829 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830
6831 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 // Actual frame shown on-screen (may be modified by animation)
6834 final Rect mShownFrame = new Rect();
6835 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006837 /**
6838 * Insets that determine the actually visible area
6839 */
6840 final Rect mVisibleInsets = new Rect();
6841 final Rect mLastVisibleInsets = new Rect();
6842 boolean mVisibleInsetsChanged;
6843
6844 /**
6845 * Insets that are covered by system windows
6846 */
6847 final Rect mContentInsets = new Rect();
6848 final Rect mLastContentInsets = new Rect();
6849 boolean mContentInsetsChanged;
6850
6851 /**
6852 * Set to true if we are waiting for this window to receive its
6853 * given internal insets before laying out other windows based on it.
6854 */
6855 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006857 /**
6858 * These are the content insets that were given during layout for
6859 * this window, to be applied to windows behind it.
6860 */
6861 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 /**
6864 * These are the visible insets that were given during layout for
6865 * this window, to be applied to windows behind it.
6866 */
6867 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869 /**
6870 * Flag indicating whether the touchable region should be adjusted by
6871 * the visible insets; if false the area outside the visible insets is
6872 * NOT touchable, so we must use those to adjust the frame during hit
6873 * tests.
6874 */
6875 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006877 // Current transformation being applied.
6878 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6879 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6880 float mHScale=1, mVScale=1;
6881 float mLastHScale=1, mLastVScale=1;
6882 final Matrix mTmpMatrix = new Matrix();
6883
6884 // "Real" frame that the application sees.
6885 final Rect mFrame = new Rect();
6886 final Rect mLastFrame = new Rect();
6887
6888 final Rect mContainingFrame = new Rect();
6889 final Rect mDisplayFrame = new Rect();
6890 final Rect mContentFrame = new Rect();
6891 final Rect mVisibleFrame = new Rect();
6892
6893 float mShownAlpha = 1;
6894 float mAlpha = 1;
6895 float mLastAlpha = 1;
6896
6897 // Set to true if, when the window gets displayed, it should perform
6898 // an enter animation.
6899 boolean mEnterAnimationPending;
6900
6901 // Currently running animation.
6902 boolean mAnimating;
6903 boolean mLocalAnimating;
6904 Animation mAnimation;
6905 boolean mAnimationIsEntrance;
6906 boolean mHasTransformation;
6907 boolean mHasLocalTransformation;
6908 final Transformation mTransformation = new Transformation();
6909
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006910 // If a window showing a wallpaper: the requested offset for the
6911 // wallpaper; if a wallpaper window: the currently applied offset.
6912 float mWallpaperX = -1;
6913 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006914
6915 // If a window showing a wallpaper: what fraction of the offset
6916 // range corresponds to a full virtual screen.
6917 float mWallpaperXStep = -1;
6918 float mWallpaperYStep = -1;
6919
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006920 // Wallpaper windows: pixels offset based on above variables.
6921 int mXOffset;
6922 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924 // This is set after IWindowSession.relayout() has been called at
6925 // least once for the window. It allows us to detect the situation
6926 // where we don't yet have a surface, but should have one soon, so
6927 // we can give the window focus before waiting for the relayout.
6928 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006930 // This is set after the Surface has been created but before the
6931 // window has been drawn. During this time the surface is hidden.
6932 boolean mDrawPending;
6933
6934 // This is set after the window has finished drawing for the first
6935 // time but before its surface is shown. The surface will be
6936 // displayed when the next layout is run.
6937 boolean mCommitDrawPending;
6938
6939 // This is set during the time after the window's drawing has been
6940 // committed, and before its surface is actually shown. It is used
6941 // to delay showing the surface until all windows in a token are ready
6942 // to be shown.
6943 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 // Set when the window has been shown in the screen the first time.
6946 boolean mHasDrawn;
6947
6948 // Currently running an exit animation?
6949 boolean mExiting;
6950
6951 // Currently on the mDestroySurface list?
6952 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006954 // Completely remove from window manager after exit animation?
6955 boolean mRemoveOnExit;
6956
6957 // Set when the orientation is changing and this window has not yet
6958 // been updated for the new orientation.
6959 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006961 // Is this window now (or just being) removed?
6962 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006964 WindowState(Session s, IWindow c, WindowToken token,
6965 WindowState attachedWindow, WindowManager.LayoutParams a,
6966 int viewVisibility) {
6967 mSession = s;
6968 mClient = c;
6969 mToken = token;
6970 mAttrs.copyFrom(a);
6971 mViewVisibility = viewVisibility;
6972 DeathRecipient deathRecipient = new DeathRecipient();
6973 mAlpha = a.alpha;
6974 if (localLOGV) Log.v(
6975 TAG, "Window " + this + " client=" + c.asBinder()
6976 + " token=" + token + " (" + mAttrs.token + ")");
6977 try {
6978 c.asBinder().linkToDeath(deathRecipient, 0);
6979 } catch (RemoteException e) {
6980 mDeathRecipient = null;
6981 mAttachedWindow = null;
6982 mLayoutAttached = false;
6983 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006984 mIsWallpaper = false;
6985 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 mBaseLayer = 0;
6987 mSubLayer = 0;
6988 return;
6989 }
6990 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006992 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6993 mAttrs.type <= LAST_SUB_WINDOW)) {
6994 // The multiplier here is to reserve space for multiple
6995 // windows in the same type layer.
6996 mBaseLayer = mPolicy.windowTypeToLayerLw(
6997 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6998 + TYPE_LAYER_OFFSET;
6999 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7000 mAttachedWindow = attachedWindow;
7001 mAttachedWindow.mChildWindows.add(this);
7002 mLayoutAttached = mAttrs.type !=
7003 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7004 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7005 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007006 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7007 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 } else {
7009 // The multiplier here is to reserve space for multiple
7010 // windows in the same type layer.
7011 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7012 * TYPE_LAYER_MULTIPLIER
7013 + TYPE_LAYER_OFFSET;
7014 mSubLayer = 0;
7015 mAttachedWindow = null;
7016 mLayoutAttached = false;
7017 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7018 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007019 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7020 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007021 }
7022
7023 WindowState appWin = this;
7024 while (appWin.mAttachedWindow != null) {
7025 appWin = mAttachedWindow;
7026 }
7027 WindowToken appToken = appWin.mToken;
7028 while (appToken.appWindowToken == null) {
7029 WindowToken parent = mTokenMap.get(appToken.token);
7030 if (parent == null || appToken == parent) {
7031 break;
7032 }
7033 appToken = parent;
7034 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007035 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007036 mAppToken = appToken.appWindowToken;
7037
7038 mSurface = null;
7039 mRequestedWidth = 0;
7040 mRequestedHeight = 0;
7041 mLastRequestedWidth = 0;
7042 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007043 mXOffset = 0;
7044 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007045 mLayer = 0;
7046 mAnimLayer = 0;
7047 mLastLayer = 0;
7048 }
7049
7050 void attach() {
7051 if (localLOGV) Log.v(
7052 TAG, "Attaching " + this + " token=" + mToken
7053 + ", list=" + mToken.windows);
7054 mSession.windowAddedLocked();
7055 }
7056
7057 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7058 mHaveFrame = true;
7059
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007060 final Rect container = mContainingFrame;
7061 container.set(pf);
7062
7063 final Rect display = mDisplayFrame;
7064 display.set(df);
7065
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007066 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007067 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007068 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7069 display.intersect(mCompatibleScreenFrame);
7070 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007071 }
7072
7073 final int pw = container.right - container.left;
7074 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007075
7076 int w,h;
7077 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7078 w = mAttrs.width < 0 ? pw : mAttrs.width;
7079 h = mAttrs.height< 0 ? ph : mAttrs.height;
7080 } else {
7081 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
7082 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
7083 }
Romain Guy06882f82009-06-10 13:36:04 -07007084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007085 final Rect content = mContentFrame;
7086 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 final Rect visible = mVisibleFrame;
7089 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007092 final int fw = frame.width();
7093 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007095 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7096 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7097
7098 Gravity.apply(mAttrs.gravity, w, h, container,
7099 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7100 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7101
7102 //System.out.println("Out: " + mFrame);
7103
7104 // Now make sure the window fits in the overall display.
7105 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007107 // Make sure the content and visible frames are inside of the
7108 // final window frame.
7109 if (content.left < frame.left) content.left = frame.left;
7110 if (content.top < frame.top) content.top = frame.top;
7111 if (content.right > frame.right) content.right = frame.right;
7112 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7113 if (visible.left < frame.left) visible.left = frame.left;
7114 if (visible.top < frame.top) visible.top = frame.top;
7115 if (visible.right > frame.right) visible.right = frame.right;
7116 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 final Rect contentInsets = mContentInsets;
7119 contentInsets.left = content.left-frame.left;
7120 contentInsets.top = content.top-frame.top;
7121 contentInsets.right = frame.right-content.right;
7122 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007124 final Rect visibleInsets = mVisibleInsets;
7125 visibleInsets.left = visible.left-frame.left;
7126 visibleInsets.top = visible.top-frame.top;
7127 visibleInsets.right = frame.right-visible.right;
7128 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007129
Dianne Hackborn284ac932009-08-28 10:34:25 -07007130 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7131 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007132 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007135 if (localLOGV) {
7136 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7137 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7138 Log.v(TAG, "Resolving (mRequestedWidth="
7139 + mRequestedWidth + ", mRequestedheight="
7140 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7141 + "): frame=" + mFrame.toShortString()
7142 + " ci=" + contentInsets.toShortString()
7143 + " vi=" + visibleInsets.toShortString());
7144 //}
7145 }
7146 }
Romain Guy06882f82009-06-10 13:36:04 -07007147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007148 public Rect getFrameLw() {
7149 return mFrame;
7150 }
7151
7152 public Rect getShownFrameLw() {
7153 return mShownFrame;
7154 }
7155
7156 public Rect getDisplayFrameLw() {
7157 return mDisplayFrame;
7158 }
7159
7160 public Rect getContentFrameLw() {
7161 return mContentFrame;
7162 }
7163
7164 public Rect getVisibleFrameLw() {
7165 return mVisibleFrame;
7166 }
7167
7168 public boolean getGivenInsetsPendingLw() {
7169 return mGivenInsetsPending;
7170 }
7171
7172 public Rect getGivenContentInsetsLw() {
7173 return mGivenContentInsets;
7174 }
Romain Guy06882f82009-06-10 13:36:04 -07007175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007176 public Rect getGivenVisibleInsetsLw() {
7177 return mGivenVisibleInsets;
7178 }
Romain Guy06882f82009-06-10 13:36:04 -07007179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007180 public WindowManager.LayoutParams getAttrs() {
7181 return mAttrs;
7182 }
7183
7184 public int getSurfaceLayer() {
7185 return mLayer;
7186 }
Romain Guy06882f82009-06-10 13:36:04 -07007187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188 public IApplicationToken getAppToken() {
7189 return mAppToken != null ? mAppToken.appToken : null;
7190 }
7191
7192 public boolean hasAppShownWindows() {
7193 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7194 }
7195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007196 public void setAnimation(Animation anim) {
7197 if (localLOGV) Log.v(
7198 TAG, "Setting animation in " + this + ": " + anim);
7199 mAnimating = false;
7200 mLocalAnimating = false;
7201 mAnimation = anim;
7202 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7203 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7204 }
7205
7206 public void clearAnimation() {
7207 if (mAnimation != null) {
7208 mAnimating = true;
7209 mLocalAnimating = false;
7210 mAnimation = null;
7211 }
7212 }
Romain Guy06882f82009-06-10 13:36:04 -07007213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007214 Surface createSurfaceLocked() {
7215 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007216 mReportDestroySurface = false;
7217 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007218 mDrawPending = true;
7219 mCommitDrawPending = false;
7220 mReadyToShow = false;
7221 if (mAppToken != null) {
7222 mAppToken.allDrawn = false;
7223 }
7224
7225 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007226 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007227 flags |= Surface.PUSH_BUFFERS;
7228 }
7229
7230 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7231 flags |= Surface.SECURE;
7232 }
7233 if (DEBUG_VISIBILITY) Log.v(
7234 TAG, "Creating surface in session "
7235 + mSession.mSurfaceSession + " window " + this
7236 + " w=" + mFrame.width()
7237 + " h=" + mFrame.height() + " format="
7238 + mAttrs.format + " flags=" + flags);
7239
7240 int w = mFrame.width();
7241 int h = mFrame.height();
7242 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7243 // for a scaled surface, we always want the requested
7244 // size.
7245 w = mRequestedWidth;
7246 h = mRequestedHeight;
7247 }
7248
Romain Guy9825ec62009-10-01 00:58:09 -07007249 // Something is wrong and SurfaceFlinger will not like this,
7250 // try to revert to sane values
7251 if (w <= 0) w = 1;
7252 if (h <= 0) h = 1;
7253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007254 try {
7255 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007256 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007258 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7259 + mSurface + " IN SESSION "
7260 + mSession.mSurfaceSession
7261 + ": pid=" + mSession.mPid + " format="
7262 + mAttrs.format + " flags=0x"
7263 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 } catch (Surface.OutOfResourcesException e) {
7265 Log.w(TAG, "OutOfResourcesException creating surface");
7266 reclaimSomeSurfaceMemoryLocked(this, "create");
7267 return null;
7268 } catch (Exception e) {
7269 Log.e(TAG, "Exception creating surface", e);
7270 return null;
7271 }
Romain Guy06882f82009-06-10 13:36:04 -07007272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007273 if (localLOGV) Log.v(
7274 TAG, "Got surface: " + mSurface
7275 + ", set left=" + mFrame.left + " top=" + mFrame.top
7276 + ", animLayer=" + mAnimLayer);
7277 if (SHOW_TRANSACTIONS) {
7278 Log.i(TAG, ">>> OPEN TRANSACTION");
7279 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7280 + mAttrs.getTitle() + ") pos=(" +
7281 mFrame.left + "," + mFrame.top + ") (" +
7282 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7283 mAnimLayer + " HIDE");
7284 }
7285 Surface.openTransaction();
7286 try {
7287 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007288 mSurface.setPosition(mFrame.left + mXOffset,
7289 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 mSurface.setLayer(mAnimLayer);
7291 mSurface.hide();
7292 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007293 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7294 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007295 mSurface.setFlags(Surface.SURFACE_DITHER,
7296 Surface.SURFACE_DITHER);
7297 }
7298 } catch (RuntimeException e) {
7299 Log.w(TAG, "Error creating surface in " + w, e);
7300 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7301 }
7302 mLastHidden = true;
7303 } finally {
7304 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7305 Surface.closeTransaction();
7306 }
7307 if (localLOGV) Log.v(
7308 TAG, "Created surface " + this);
7309 }
7310 return mSurface;
7311 }
Romain Guy06882f82009-06-10 13:36:04 -07007312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007313 void destroySurfaceLocked() {
7314 // Window is no longer on-screen, so can no longer receive
7315 // key events... if we were waiting for it to finish
7316 // handling a key event, the wait is over!
7317 mKeyWaiter.finishedKey(mSession, mClient, true,
7318 KeyWaiter.RETURN_NOTHING);
7319 mKeyWaiter.releasePendingPointerLocked(mSession);
7320 mKeyWaiter.releasePendingTrackballLocked(mSession);
7321
7322 if (mAppToken != null && this == mAppToken.startingWindow) {
7323 mAppToken.startingDisplayed = false;
7324 }
Romain Guy06882f82009-06-10 13:36:04 -07007325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007326 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007327 mDrawPending = false;
7328 mCommitDrawPending = false;
7329 mReadyToShow = false;
7330
7331 int i = mChildWindows.size();
7332 while (i > 0) {
7333 i--;
7334 WindowState c = (WindowState)mChildWindows.get(i);
7335 c.mAttachedHidden = true;
7336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007337
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007338 if (mReportDestroySurface) {
7339 mReportDestroySurface = false;
7340 mSurfacePendingDestroy = true;
7341 try {
7342 mClient.dispatchGetNewSurface();
7343 // We'll really destroy on the next time around.
7344 return;
7345 } catch (RemoteException e) {
7346 }
7347 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007349 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007350 if (DEBUG_VISIBILITY) {
7351 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007352 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007353 Log.w(TAG, "Window " + this + " destroying surface "
7354 + mSurface + ", session " + mSession, e);
7355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007356 if (SHOW_TRANSACTIONS) {
7357 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007358 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7360 + mAttrs.getTitle() + ")", ex);
7361 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007362 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007363 } catch (RuntimeException e) {
7364 Log.w(TAG, "Exception thrown when destroying Window " + this
7365 + " surface " + mSurface + " session " + mSession
7366 + ": " + e.toString());
7367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370 }
7371 }
7372
7373 boolean finishDrawingLocked() {
7374 if (mDrawPending) {
7375 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7376 TAG, "finishDrawingLocked: " + mSurface);
7377 mCommitDrawPending = true;
7378 mDrawPending = false;
7379 return true;
7380 }
7381 return false;
7382 }
7383
7384 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007385 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7387 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007388 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389 }
7390 mCommitDrawPending = false;
7391 mReadyToShow = true;
7392 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7393 final AppWindowToken atoken = mAppToken;
7394 if (atoken == null || atoken.allDrawn || starting) {
7395 performShowLocked();
7396 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007397 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007398 }
7399
7400 // This must be called while inside a transaction.
7401 boolean performShowLocked() {
7402 if (DEBUG_VISIBILITY) {
7403 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007404 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007405 Log.v(TAG, "performShow on " + this
7406 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7407 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7408 }
7409 if (mReadyToShow && isReadyForDisplay()) {
7410 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7411 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7412 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7413 + " during animation: policyVis=" + mPolicyVisibility
7414 + " attHidden=" + mAttachedHidden
7415 + " tok.hiddenRequested="
7416 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007417 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007418 + (mAppToken != null ? mAppToken.hidden : false)
7419 + " animating=" + mAnimating
7420 + " tok animating="
7421 + (mAppToken != null ? mAppToken.animating : false));
7422 if (!showSurfaceRobustlyLocked(this)) {
7423 return false;
7424 }
7425 mLastAlpha = -1;
7426 mHasDrawn = true;
7427 mLastHidden = false;
7428 mReadyToShow = false;
7429 enableScreenIfNeededLocked();
7430
7431 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007433 int i = mChildWindows.size();
7434 while (i > 0) {
7435 i--;
7436 WindowState c = (WindowState)mChildWindows.get(i);
7437 if (c.mSurface != null && c.mAttachedHidden) {
7438 c.mAttachedHidden = false;
7439 c.performShowLocked();
7440 }
7441 }
Romain Guy06882f82009-06-10 13:36:04 -07007442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007443 if (mAttrs.type != TYPE_APPLICATION_STARTING
7444 && mAppToken != null) {
7445 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007446
Dianne Hackborn248b1882009-09-16 16:46:44 -07007447 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007448 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7449 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007451 // If this initial window is animating, stop it -- we
7452 // will do an animation to reveal it from behind the
7453 // starting window, so there is no need for it to also
7454 // be doing its own stuff.
7455 if (mAnimation != null) {
7456 mAnimation = null;
7457 // Make sure we clean up the animation.
7458 mAnimating = true;
7459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007460 mFinishedStarting.add(mAppToken);
7461 mH.sendEmptyMessage(H.FINISHED_STARTING);
7462 }
7463 mAppToken.updateReportedVisibilityLocked();
7464 }
7465 }
7466 return true;
7467 }
Romain Guy06882f82009-06-10 13:36:04 -07007468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007469 // This must be called while inside a transaction. Returns true if
7470 // there is more animation to run.
7471 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7472 if (!mDisplayFrozen) {
7473 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007475 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7476 mHasTransformation = true;
7477 mHasLocalTransformation = true;
7478 if (!mLocalAnimating) {
7479 if (DEBUG_ANIM) Log.v(
7480 TAG, "Starting animation in " + this +
7481 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7482 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7483 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7484 mAnimation.setStartTime(currentTime);
7485 mLocalAnimating = true;
7486 mAnimating = true;
7487 }
7488 mTransformation.clear();
7489 final boolean more = mAnimation.getTransformation(
7490 currentTime, mTransformation);
7491 if (DEBUG_ANIM) Log.v(
7492 TAG, "Stepped animation in " + this +
7493 ": more=" + more + ", xform=" + mTransformation);
7494 if (more) {
7495 // we're not done!
7496 return true;
7497 }
7498 if (DEBUG_ANIM) Log.v(
7499 TAG, "Finished animation in " + this +
7500 " @ " + currentTime);
7501 mAnimation = null;
7502 //WindowManagerService.this.dump();
7503 }
7504 mHasLocalTransformation = false;
7505 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007506 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 // When our app token is animating, we kind-of pretend like
7508 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7509 // part of this check means that we will only do this if
7510 // our window is not currently exiting, or it is not
7511 // locally animating itself. The idea being that one that
7512 // is exiting and doing a local animation should be removed
7513 // once that animation is done.
7514 mAnimating = true;
7515 mHasTransformation = true;
7516 mTransformation.clear();
7517 return false;
7518 } else if (mHasTransformation) {
7519 // Little trick to get through the path below to act like
7520 // we have finished an animation.
7521 mAnimating = true;
7522 } else if (isAnimating()) {
7523 mAnimating = true;
7524 }
7525 } else if (mAnimation != null) {
7526 // If the display is frozen, and there is a pending animation,
7527 // clear it and make sure we run the cleanup code.
7528 mAnimating = true;
7529 mLocalAnimating = true;
7530 mAnimation = null;
7531 }
Romain Guy06882f82009-06-10 13:36:04 -07007532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007533 if (!mAnimating && !mLocalAnimating) {
7534 return false;
7535 }
7536
7537 if (DEBUG_ANIM) Log.v(
7538 TAG, "Animation done in " + this + ": exiting=" + mExiting
7539 + ", reportedVisible="
7540 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007542 mAnimating = false;
7543 mLocalAnimating = false;
7544 mAnimation = null;
7545 mAnimLayer = mLayer;
7546 if (mIsImWindow) {
7547 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007548 } else if (mIsWallpaper) {
7549 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007550 }
7551 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7552 + " anim layer: " + mAnimLayer);
7553 mHasTransformation = false;
7554 mHasLocalTransformation = false;
7555 mPolicyVisibility = mPolicyVisibilityAfterAnim;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007556 if (!mPolicyVisibility) {
7557 // Window is no longer visible -- make sure if we were waiting
7558 // for it to be displayed before enabling the display, that
7559 // we allow the display to be enabled now.
7560 enableScreenIfNeededLocked();
7561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 mTransformation.clear();
7563 if (mHasDrawn
7564 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7565 && mAppToken != null
7566 && mAppToken.firstWindowDrawn
7567 && mAppToken.startingData != null) {
7568 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7569 + mToken + ": first real window done animating");
7570 mFinishedStarting.add(mAppToken);
7571 mH.sendEmptyMessage(H.FINISHED_STARTING);
7572 }
Romain Guy06882f82009-06-10 13:36:04 -07007573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007574 finishExit();
7575
7576 if (mAppToken != null) {
7577 mAppToken.updateReportedVisibilityLocked();
7578 }
7579
7580 return false;
7581 }
7582
7583 void finishExit() {
7584 if (DEBUG_ANIM) Log.v(
7585 TAG, "finishExit in " + this
7586 + ": exiting=" + mExiting
7587 + " remove=" + mRemoveOnExit
7588 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007590 final int N = mChildWindows.size();
7591 for (int i=0; i<N; i++) {
7592 ((WindowState)mChildWindows.get(i)).finishExit();
7593 }
Romain Guy06882f82009-06-10 13:36:04 -07007594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007595 if (!mExiting) {
7596 return;
7597 }
Romain Guy06882f82009-06-10 13:36:04 -07007598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007599 if (isWindowAnimating()) {
7600 return;
7601 }
7602
7603 if (localLOGV) Log.v(
7604 TAG, "Exit animation finished in " + this
7605 + ": remove=" + mRemoveOnExit);
7606 if (mSurface != null) {
7607 mDestroySurface.add(this);
7608 mDestroying = true;
7609 if (SHOW_TRANSACTIONS) Log.i(
7610 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7611 try {
7612 mSurface.hide();
7613 } catch (RuntimeException e) {
7614 Log.w(TAG, "Error hiding surface in " + this, e);
7615 }
7616 mLastHidden = true;
7617 mKeyWaiter.releasePendingPointerLocked(mSession);
7618 }
7619 mExiting = false;
7620 if (mRemoveOnExit) {
7621 mPendingRemove.add(this);
7622 mRemoveOnExit = false;
7623 }
7624 }
Romain Guy06882f82009-06-10 13:36:04 -07007625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7627 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7628 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7629 if (dtdx < -.000001f || dtdx > .000001f) return false;
7630 if (dsdy < -.000001f || dsdy > .000001f) return false;
7631 return true;
7632 }
Romain Guy06882f82009-06-10 13:36:04 -07007633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 void computeShownFrameLocked() {
7635 final boolean selfTransformation = mHasLocalTransformation;
7636 Transformation attachedTransformation =
7637 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7638 ? mAttachedWindow.mTransformation : null;
7639 Transformation appTransformation =
7640 (mAppToken != null && mAppToken.hasTransformation)
7641 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007642
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007643 // Wallpapers are animated based on the "real" window they
7644 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007645 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007646 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007647 if (mWallpaperTarget.mHasLocalTransformation &&
7648 mWallpaperTarget.mAnimation != null &&
7649 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007650 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007651 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7652 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7653 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007654 }
7655 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007656 mWallpaperTarget.mAppToken.hasTransformation &&
7657 mWallpaperTarget.mAppToken.animation != null &&
7658 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007659 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007660 if (DEBUG_WALLPAPER && appTransformation != null) {
7661 Log.v(TAG, "WP target app xform: " + appTransformation);
7662 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007663 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007664 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007666 if (selfTransformation || attachedTransformation != null
7667 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007668 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 final Rect frame = mFrame;
7670 final float tmpFloats[] = mTmpFloats;
7671 final Matrix tmpMatrix = mTmpMatrix;
7672
7673 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007674 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007675 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007676 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007678 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007679 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007680 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007681 }
7682 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007683 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684 }
7685
7686 // "convert" it into SurfaceFlinger's format
7687 // (a 2x2 matrix + an offset)
7688 // Here we must not transform the position of the surface
7689 // since it is already included in the transformation.
7690 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007692 tmpMatrix.getValues(tmpFloats);
7693 mDsDx = tmpFloats[Matrix.MSCALE_X];
7694 mDtDx = tmpFloats[Matrix.MSKEW_X];
7695 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7696 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007697 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7698 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 int w = frame.width();
7700 int h = frame.height();
7701 mShownFrame.set(x, y, x+w, y+h);
7702
7703 // Now set the alpha... but because our current hardware
7704 // can't do alpha transformation on a non-opaque surface,
7705 // turn it off if we are running an animation that is also
7706 // transforming since it is more important to have that
7707 // animation be smooth.
7708 mShownAlpha = mAlpha;
7709 if (!mLimitedAlphaCompositing
7710 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7711 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7712 && x == frame.left && y == frame.top))) {
7713 //Log.i(TAG, "Applying alpha transform");
7714 if (selfTransformation) {
7715 mShownAlpha *= mTransformation.getAlpha();
7716 }
7717 if (attachedTransformation != null) {
7718 mShownAlpha *= attachedTransformation.getAlpha();
7719 }
7720 if (appTransformation != null) {
7721 mShownAlpha *= appTransformation.getAlpha();
7722 }
7723 } else {
7724 //Log.i(TAG, "Not applying alpha transform");
7725 }
Romain Guy06882f82009-06-10 13:36:04 -07007726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007727 if (localLOGV) Log.v(
7728 TAG, "Continuing animation in " + this +
7729 ": " + mShownFrame +
7730 ", alpha=" + mTransformation.getAlpha());
7731 return;
7732 }
Romain Guy06882f82009-06-10 13:36:04 -07007733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007735 if (mXOffset != 0 || mYOffset != 0) {
7736 mShownFrame.offset(mXOffset, mYOffset);
7737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007738 mShownAlpha = mAlpha;
7739 mDsDx = 1;
7740 mDtDx = 0;
7741 mDsDy = 0;
7742 mDtDy = 1;
7743 }
Romain Guy06882f82009-06-10 13:36:04 -07007744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007745 /**
7746 * Is this window visible? It is not visible if there is no
7747 * surface, or we are in the process of running an exit animation
7748 * that will remove the surface, or its app token has been hidden.
7749 */
7750 public boolean isVisibleLw() {
7751 final AppWindowToken atoken = mAppToken;
7752 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7753 && (atoken == null || !atoken.hiddenRequested)
7754 && !mExiting && !mDestroying;
7755 }
7756
7757 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007758 * Like {@link #isVisibleLw}, but also counts a window that is currently
7759 * "hidden" behind the keyguard as visible. This allows us to apply
7760 * things like window flags that impact the keyguard.
7761 * XXX I am starting to think we need to have ANOTHER visibility flag
7762 * for this "hidden behind keyguard" state rather than overloading
7763 * mPolicyVisibility. Ungh.
7764 */
7765 public boolean isVisibleOrBehindKeyguardLw() {
7766 final AppWindowToken atoken = mAppToken;
7767 return mSurface != null && !mAttachedHidden
7768 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7769 && !mExiting && !mDestroying;
7770 }
7771
7772 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 * Is this window visible, ignoring its app token? It is not visible
7774 * if there is no surface, or we are in the process of running an exit animation
7775 * that will remove the surface.
7776 */
7777 public boolean isWinVisibleLw() {
7778 final AppWindowToken atoken = mAppToken;
7779 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7780 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7781 && !mExiting && !mDestroying;
7782 }
7783
7784 /**
7785 * The same as isVisible(), but follows the current hidden state of
7786 * the associated app token, not the pending requested hidden state.
7787 */
7788 boolean isVisibleNow() {
7789 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007790 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007791 }
7792
7793 /**
7794 * Same as isVisible(), but we also count it as visible between the
7795 * call to IWindowSession.add() and the first relayout().
7796 */
7797 boolean isVisibleOrAdding() {
7798 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007799 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7801 && mPolicyVisibility && !mAttachedHidden
7802 && (atoken == null || !atoken.hiddenRequested)
7803 && !mExiting && !mDestroying;
7804 }
7805
7806 /**
7807 * Is this window currently on-screen? It is on-screen either if it
7808 * is visible or it is currently running an animation before no longer
7809 * being visible.
7810 */
7811 boolean isOnScreen() {
7812 final AppWindowToken atoken = mAppToken;
7813 if (atoken != null) {
7814 return mSurface != null && mPolicyVisibility && !mDestroying
7815 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007816 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 } else {
7818 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007819 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 }
7821 }
Romain Guy06882f82009-06-10 13:36:04 -07007822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 /**
7824 * Like isOnScreen(), but we don't return true if the window is part
7825 * of a transition that has not yet been started.
7826 */
7827 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007828 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007829 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007830 return false;
7831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007833 final boolean animating = atoken != null
7834 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007835 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007836 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7837 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007838 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 }
7840
7841 /** Is the window or its container currently animating? */
7842 boolean isAnimating() {
7843 final WindowState attached = mAttachedWindow;
7844 final AppWindowToken atoken = mAppToken;
7845 return mAnimation != null
7846 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007847 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 (atoken.animation != null
7849 || atoken.inPendingTransaction));
7850 }
7851
7852 /** Is this window currently animating? */
7853 boolean isWindowAnimating() {
7854 return mAnimation != null;
7855 }
7856
7857 /**
7858 * Like isOnScreen, but returns false if the surface hasn't yet
7859 * been drawn.
7860 */
7861 public boolean isDisplayedLw() {
7862 final AppWindowToken atoken = mAppToken;
7863 return mSurface != null && mPolicyVisibility && !mDestroying
7864 && !mDrawPending && !mCommitDrawPending
7865 && ((!mAttachedHidden &&
7866 (atoken == null || !atoken.hiddenRequested))
7867 || mAnimating);
7868 }
7869
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007870 /**
7871 * Returns true if the window has a surface that it has drawn a
7872 * complete UI in to.
7873 */
7874 public boolean isDrawnLw() {
7875 final AppWindowToken atoken = mAppToken;
7876 return mSurface != null && !mDestroying
7877 && !mDrawPending && !mCommitDrawPending;
7878 }
7879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007880 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7881 boolean shownFrame, boolean onlyOpaque) {
7882 if (mSurface == null) {
7883 return false;
7884 }
7885 if (mAppToken != null && !mAppToken.appFullscreen) {
7886 return false;
7887 }
7888 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7889 return false;
7890 }
7891 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007892
7893 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7894 return frame.left <= mCompatibleScreenFrame.left &&
7895 frame.top <= mCompatibleScreenFrame.top &&
7896 frame.right >= mCompatibleScreenFrame.right &&
7897 frame.bottom >= mCompatibleScreenFrame.bottom;
7898 } else {
7899 return frame.left <= 0 && frame.top <= 0
7900 && frame.right >= screenWidth
7901 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 }
Romain Guy06882f82009-06-10 13:36:04 -07007904
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007905 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007906 * Return true if the window is opaque and fully drawn. This indicates
7907 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007908 */
7909 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007910 return (mAttrs.format == PixelFormat.OPAQUE
7911 || mAttrs.type == TYPE_WALLPAPER)
7912 && mSurface != null && mAnimation == null
7913 && (mAppToken == null || mAppToken.animation == null)
7914 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007915 }
7916
7917 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7918 return
7919 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007920 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7921 // only if it's visible
7922 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007923 // and only if the application fills the compatible screen
7924 mFrame.left <= mCompatibleScreenFrame.left &&
7925 mFrame.top <= mCompatibleScreenFrame.top &&
7926 mFrame.right >= mCompatibleScreenFrame.right &&
7927 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007928 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007929 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007930 }
7931
7932 boolean isFullscreen(int screenWidth, int screenHeight) {
7933 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007934 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 }
7936
7937 void removeLocked() {
7938 if (mAttachedWindow != null) {
7939 mAttachedWindow.mChildWindows.remove(this);
7940 }
7941 destroySurfaceLocked();
7942 mSession.windowRemovedLocked();
7943 try {
7944 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7945 } catch (RuntimeException e) {
7946 // Ignore if it has already been removed (usually because
7947 // we are doing this as part of processing a death note.)
7948 }
7949 }
7950
7951 private class DeathRecipient implements IBinder.DeathRecipient {
7952 public void binderDied() {
7953 try {
7954 synchronized(mWindowMap) {
7955 WindowState win = windowForClientLocked(mSession, mClient);
7956 Log.i(TAG, "WIN DEATH: " + win);
7957 if (win != null) {
7958 removeWindowLocked(mSession, win);
7959 }
7960 }
7961 } catch (IllegalArgumentException ex) {
7962 // This will happen if the window has already been
7963 // removed.
7964 }
7965 }
7966 }
7967
7968 /** Returns true if this window desires key events. */
7969 public final boolean canReceiveKeys() {
7970 return isVisibleOrAdding()
7971 && (mViewVisibility == View.VISIBLE)
7972 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7973 }
7974
7975 public boolean hasDrawnLw() {
7976 return mHasDrawn;
7977 }
7978
7979 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007980 return showLw(doAnimation, true);
7981 }
7982
7983 boolean showLw(boolean doAnimation, boolean requestAnim) {
7984 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
7985 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007987 mPolicyVisibility = true;
7988 mPolicyVisibilityAfterAnim = true;
7989 if (doAnimation) {
7990 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7991 }
7992 if (requestAnim) {
7993 requestAnimationLocked(0);
7994 }
7995 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007996 }
7997
7998 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007999 return hideLw(doAnimation, true);
8000 }
8001
8002 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008003 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8004 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008005 if (!current) {
8006 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008008 if (doAnimation) {
8009 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8010 if (mAnimation == null) {
8011 doAnimation = false;
8012 }
8013 }
8014 if (doAnimation) {
8015 mPolicyVisibilityAfterAnim = false;
8016 } else {
8017 mPolicyVisibilityAfterAnim = false;
8018 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008019 // Window is no longer visible -- make sure if we were waiting
8020 // for it to be displayed before enabling the display, that
8021 // we allow the display to be enabled now.
8022 enableScreenIfNeededLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008023 }
8024 if (requestAnim) {
8025 requestAnimationLocked(0);
8026 }
8027 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008028 }
8029
8030 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008031 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07008032
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008033 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8034 pw.print(" mClient="); pw.println(mClient.asBinder());
8035 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8036 if (mAttachedWindow != null || mLayoutAttached) {
8037 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8038 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8039 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008040 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8041 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8042 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008043 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8044 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008045 }
8046 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8047 pw.print(" mSubLayer="); pw.print(mSubLayer);
8048 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8049 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8050 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8051 pw.print("="); pw.print(mAnimLayer);
8052 pw.print(" mLastLayer="); pw.println(mLastLayer);
8053 if (mSurface != null) {
8054 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8055 }
8056 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8057 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8058 if (mAppToken != null) {
8059 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8060 }
8061 if (mTargetAppToken != null) {
8062 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8063 }
8064 pw.print(prefix); pw.print("mViewVisibility=0x");
8065 pw.print(Integer.toHexString(mViewVisibility));
8066 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008067 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8068 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008069 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8070 pw.print(prefix); pw.print("mPolicyVisibility=");
8071 pw.print(mPolicyVisibility);
8072 pw.print(" mPolicyVisibilityAfterAnim=");
8073 pw.print(mPolicyVisibilityAfterAnim);
8074 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8075 }
8076 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008077 pw.print(" h="); pw.println(mRequestedHeight);
8078 if (mXOffset != 0 || mYOffset != 0) {
8079 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8080 pw.print(" y="); pw.println(mYOffset);
8081 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008082 pw.print(prefix); pw.print("mGivenContentInsets=");
8083 mGivenContentInsets.printShortString(pw);
8084 pw.print(" mGivenVisibleInsets=");
8085 mGivenVisibleInsets.printShortString(pw);
8086 pw.println();
8087 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8088 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8089 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8090 }
8091 pw.print(prefix); pw.print("mShownFrame=");
8092 mShownFrame.printShortString(pw);
8093 pw.print(" last="); mLastShownFrame.printShortString(pw);
8094 pw.println();
8095 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8096 pw.print(" last="); mLastFrame.printShortString(pw);
8097 pw.println();
8098 pw.print(prefix); pw.print("mContainingFrame=");
8099 mContainingFrame.printShortString(pw);
8100 pw.print(" mDisplayFrame=");
8101 mDisplayFrame.printShortString(pw);
8102 pw.println();
8103 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8104 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8105 pw.println();
8106 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8107 pw.print(" last="); mLastContentInsets.printShortString(pw);
8108 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8109 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8110 pw.println();
8111 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8112 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8113 pw.print(" mAlpha="); pw.print(mAlpha);
8114 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8115 }
8116 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8117 || mAnimation != null) {
8118 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8119 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8120 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8121 pw.print(" mAnimation="); pw.println(mAnimation);
8122 }
8123 if (mHasTransformation || mHasLocalTransformation) {
8124 pw.print(prefix); pw.print("XForm: has=");
8125 pw.print(mHasTransformation);
8126 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8127 pw.print(" "); mTransformation.printShortString(pw);
8128 pw.println();
8129 }
8130 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8131 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8132 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8133 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8134 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8135 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8136 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8137 pw.print(" mDestroying="); pw.print(mDestroying);
8138 pw.print(" mRemoved="); pw.println(mRemoved);
8139 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008140 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008141 pw.print(prefix); pw.print("mOrientationChanging=");
8142 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008143 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8144 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008145 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008146 if (mHScale != 1 || mVScale != 1) {
8147 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8148 pw.print(" mVScale="); pw.println(mVScale);
8149 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008150 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008151 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8152 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8153 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008154 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8155 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8156 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008158 }
8159
8160 @Override
8161 public String toString() {
8162 return "Window{"
8163 + Integer.toHexString(System.identityHashCode(this))
8164 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8165 }
8166 }
Romain Guy06882f82009-06-10 13:36:04 -07008167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008168 // -------------------------------------------------------------
8169 // Window Token State
8170 // -------------------------------------------------------------
8171
8172 class WindowToken {
8173 // The actual token.
8174 final IBinder token;
8175
8176 // The type of window this token is for, as per WindowManager.LayoutParams.
8177 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 // Set if this token was explicitly added by a client, so should
8180 // not be removed when all windows are removed.
8181 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008182
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008183 // For printing.
8184 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008186 // If this is an AppWindowToken, this is non-null.
8187 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008189 // All of the windows associated with this token.
8190 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8191
8192 // Is key dispatching paused for this token?
8193 boolean paused = false;
8194
8195 // Should this token's windows be hidden?
8196 boolean hidden;
8197
8198 // Temporary for finding which tokens no longer have visible windows.
8199 boolean hasVisible;
8200
Dianne Hackborna8f60182009-09-01 19:01:50 -07008201 // Set to true when this token is in a pending transaction where it
8202 // will be shown.
8203 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008204
Dianne Hackborna8f60182009-09-01 19:01:50 -07008205 // Set to true when this token is in a pending transaction where it
8206 // will be hidden.
8207 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008208
Dianne Hackborna8f60182009-09-01 19:01:50 -07008209 // Set to true when this token is in a pending transaction where its
8210 // windows will be put to the bottom of the list.
8211 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008212
Dianne Hackborna8f60182009-09-01 19:01:50 -07008213 // Set to true when this token is in a pending transaction where its
8214 // windows will be put to the top of the list.
8215 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008217 WindowToken(IBinder _token, int type, boolean _explicit) {
8218 token = _token;
8219 windowType = type;
8220 explicit = _explicit;
8221 }
8222
8223 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008224 pw.print(prefix); pw.print("token="); pw.println(token);
8225 pw.print(prefix); pw.print("windows="); pw.println(windows);
8226 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8227 pw.print(" hidden="); pw.print(hidden);
8228 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008229 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8230 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8231 pw.print(" waitingToHide="); pw.print(waitingToHide);
8232 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8233 pw.print(" sendingToTop="); pw.println(sendingToTop);
8234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 }
8236
8237 @Override
8238 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008239 if (stringName == null) {
8240 StringBuilder sb = new StringBuilder();
8241 sb.append("WindowToken{");
8242 sb.append(Integer.toHexString(System.identityHashCode(this)));
8243 sb.append(" token="); sb.append(token); sb.append('}');
8244 stringName = sb.toString();
8245 }
8246 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008247 }
8248 };
8249
8250 class AppWindowToken extends WindowToken {
8251 // Non-null only for application tokens.
8252 final IApplicationToken appToken;
8253
8254 // All of the windows and child windows that are included in this
8255 // application token. Note this list is NOT sorted!
8256 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8257
8258 int groupId = -1;
8259 boolean appFullscreen;
8260 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008262 // These are used for determining when all windows associated with
8263 // an activity have been drawn, so they can be made visible together
8264 // at the same time.
8265 int lastTransactionSequence = mTransactionSequence-1;
8266 int numInterestingWindows;
8267 int numDrawnWindows;
8268 boolean inPendingTransaction;
8269 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008271 // Is this token going to be hidden in a little while? If so, it
8272 // won't be taken into account for setting the screen orientation.
8273 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 // Is this window's surface needed? This is almost like hidden, except
8276 // it will sometimes be true a little earlier: when the token has
8277 // been shown, but is still waiting for its app transition to execute
8278 // before making its windows shown.
8279 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008281 // Have we told the window clients to hide themselves?
8282 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008284 // Last visibility state we reported to the app token.
8285 boolean reportedVisible;
8286
8287 // Set to true when the token has been removed from the window mgr.
8288 boolean removed;
8289
8290 // Have we been asked to have this token keep the screen frozen?
8291 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008293 boolean animating;
8294 Animation animation;
8295 boolean hasTransformation;
8296 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008298 // Offset to the window of all layers in the token, for use by
8299 // AppWindowToken animations.
8300 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008302 // Information about an application starting window if displayed.
8303 StartingData startingData;
8304 WindowState startingWindow;
8305 View startingView;
8306 boolean startingDisplayed;
8307 boolean startingMoved;
8308 boolean firstWindowDrawn;
8309
8310 AppWindowToken(IApplicationToken _token) {
8311 super(_token.asBinder(),
8312 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8313 appWindowToken = this;
8314 appToken = _token;
8315 }
Romain Guy06882f82009-06-10 13:36:04 -07008316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008317 public void setAnimation(Animation anim) {
8318 if (localLOGV) Log.v(
8319 TAG, "Setting animation in " + this + ": " + anim);
8320 animation = anim;
8321 animating = false;
8322 anim.restrictDuration(MAX_ANIMATION_DURATION);
8323 anim.scaleCurrentDuration(mTransitionAnimationScale);
8324 int zorder = anim.getZAdjustment();
8325 int adj = 0;
8326 if (zorder == Animation.ZORDER_TOP) {
8327 adj = TYPE_LAYER_OFFSET;
8328 } else if (zorder == Animation.ZORDER_BOTTOM) {
8329 adj = -TYPE_LAYER_OFFSET;
8330 }
Romain Guy06882f82009-06-10 13:36:04 -07008331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 if (animLayerAdjustment != adj) {
8333 animLayerAdjustment = adj;
8334 updateLayers();
8335 }
8336 }
Romain Guy06882f82009-06-10 13:36:04 -07008337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008338 public void setDummyAnimation() {
8339 if (animation == null) {
8340 if (localLOGV) Log.v(
8341 TAG, "Setting dummy animation in " + this);
8342 animation = sDummyAnimation;
8343 }
8344 }
8345
8346 public void clearAnimation() {
8347 if (animation != null) {
8348 animation = null;
8349 animating = true;
8350 }
8351 }
Romain Guy06882f82009-06-10 13:36:04 -07008352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 void updateLayers() {
8354 final int N = allAppWindows.size();
8355 final int adj = animLayerAdjustment;
8356 for (int i=0; i<N; i++) {
8357 WindowState w = allAppWindows.get(i);
8358 w.mAnimLayer = w.mLayer + adj;
8359 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8360 + w.mAnimLayer);
8361 if (w == mInputMethodTarget) {
8362 setInputMethodAnimLayerAdjustment(adj);
8363 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008364 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008365 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008367 }
8368 }
Romain Guy06882f82009-06-10 13:36:04 -07008369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008370 void sendAppVisibilityToClients() {
8371 final int N = allAppWindows.size();
8372 for (int i=0; i<N; i++) {
8373 WindowState win = allAppWindows.get(i);
8374 if (win == startingWindow && clientHidden) {
8375 // Don't hide the starting window.
8376 continue;
8377 }
8378 try {
8379 if (DEBUG_VISIBILITY) Log.v(TAG,
8380 "Setting visibility of " + win + ": " + (!clientHidden));
8381 win.mClient.dispatchAppVisibility(!clientHidden);
8382 } catch (RemoteException e) {
8383 }
8384 }
8385 }
Romain Guy06882f82009-06-10 13:36:04 -07008386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008387 void showAllWindowsLocked() {
8388 final int NW = allAppWindows.size();
8389 for (int i=0; i<NW; i++) {
8390 WindowState w = allAppWindows.get(i);
8391 if (DEBUG_VISIBILITY) Log.v(TAG,
8392 "performing show on: " + w);
8393 w.performShowLocked();
8394 }
8395 }
Romain Guy06882f82009-06-10 13:36:04 -07008396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008397 // This must be called while inside a transaction.
8398 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8399 if (!mDisplayFrozen) {
8400 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008402 if (animation == sDummyAnimation) {
8403 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008404 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008405 // when it is really time to animate, this will be set to
8406 // a real animation and the next call will execute normally.
8407 return false;
8408 }
Romain Guy06882f82009-06-10 13:36:04 -07008409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008410 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8411 if (!animating) {
8412 if (DEBUG_ANIM) Log.v(
8413 TAG, "Starting animation in " + this +
8414 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8415 + " scale=" + mTransitionAnimationScale
8416 + " allDrawn=" + allDrawn + " animating=" + animating);
8417 animation.initialize(dw, dh, dw, dh);
8418 animation.setStartTime(currentTime);
8419 animating = true;
8420 }
8421 transformation.clear();
8422 final boolean more = animation.getTransformation(
8423 currentTime, transformation);
8424 if (DEBUG_ANIM) Log.v(
8425 TAG, "Stepped animation in " + this +
8426 ": more=" + more + ", xform=" + transformation);
8427 if (more) {
8428 // we're done!
8429 hasTransformation = true;
8430 return true;
8431 }
8432 if (DEBUG_ANIM) Log.v(
8433 TAG, "Finished animation in " + this +
8434 " @ " + currentTime);
8435 animation = null;
8436 }
8437 } else if (animation != null) {
8438 // If the display is frozen, and there is a pending animation,
8439 // clear it and make sure we run the cleanup code.
8440 animating = true;
8441 animation = null;
8442 }
8443
8444 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 if (!animating) {
8447 return false;
8448 }
8449
8450 clearAnimation();
8451 animating = false;
8452 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8453 moveInputMethodWindowsIfNeededLocked(true);
8454 }
Romain Guy06882f82009-06-10 13:36:04 -07008455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008456 if (DEBUG_ANIM) Log.v(
8457 TAG, "Animation done in " + this
8458 + ": reportedVisible=" + reportedVisible);
8459
8460 transformation.clear();
8461 if (animLayerAdjustment != 0) {
8462 animLayerAdjustment = 0;
8463 updateLayers();
8464 }
Romain Guy06882f82009-06-10 13:36:04 -07008465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466 final int N = windows.size();
8467 for (int i=0; i<N; i++) {
8468 ((WindowState)windows.get(i)).finishExit();
8469 }
8470 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008472 return false;
8473 }
8474
8475 void updateReportedVisibilityLocked() {
8476 if (appToken == null) {
8477 return;
8478 }
Romain Guy06882f82009-06-10 13:36:04 -07008479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008480 int numInteresting = 0;
8481 int numVisible = 0;
8482 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008484 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8485 final int N = allAppWindows.size();
8486 for (int i=0; i<N; i++) {
8487 WindowState win = allAppWindows.get(i);
8488 if (win == startingWindow || win.mAppFreezing) {
8489 continue;
8490 }
8491 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008492 Log.v(TAG, "Win " + win + ": isDrawn="
8493 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008494 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008495 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 Log.v(TAG, "Not displayed: s=" + win.mSurface
8497 + " pv=" + win.mPolicyVisibility
8498 + " dp=" + win.mDrawPending
8499 + " cdp=" + win.mCommitDrawPending
8500 + " ah=" + win.mAttachedHidden
8501 + " th="
8502 + (win.mAppToken != null
8503 ? win.mAppToken.hiddenRequested : false)
8504 + " a=" + win.mAnimating);
8505 }
8506 }
8507 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008508 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 if (!win.isAnimating()) {
8510 numVisible++;
8511 }
8512 nowGone = false;
8513 } else if (win.isAnimating()) {
8514 nowGone = false;
8515 }
8516 }
Romain Guy06882f82009-06-10 13:36:04 -07008517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008518 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8519 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8520 + numInteresting + " visible=" + numVisible);
8521 if (nowVisible != reportedVisible) {
8522 if (DEBUG_VISIBILITY) Log.v(
8523 TAG, "Visibility changed in " + this
8524 + ": vis=" + nowVisible);
8525 reportedVisible = nowVisible;
8526 Message m = mH.obtainMessage(
8527 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8528 nowVisible ? 1 : 0,
8529 nowGone ? 1 : 0,
8530 this);
8531 mH.sendMessage(m);
8532 }
8533 }
Romain Guy06882f82009-06-10 13:36:04 -07008534
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008535 WindowState findMainWindow() {
8536 int j = windows.size();
8537 while (j > 0) {
8538 j--;
8539 WindowState win = windows.get(j);
8540 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8541 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8542 return win;
8543 }
8544 }
8545 return null;
8546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 void dump(PrintWriter pw, String prefix) {
8549 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008550 if (appToken != null) {
8551 pw.print(prefix); pw.println("app=true");
8552 }
8553 if (allAppWindows.size() > 0) {
8554 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8555 }
8556 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008557 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008558 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8559 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8560 pw.print(" clientHidden="); pw.print(clientHidden);
8561 pw.print(" willBeHidden="); pw.print(willBeHidden);
8562 pw.print(" reportedVisible="); pw.println(reportedVisible);
8563 if (paused || freezingScreen) {
8564 pw.print(prefix); pw.print("paused="); pw.print(paused);
8565 pw.print(" freezingScreen="); pw.println(freezingScreen);
8566 }
8567 if (numInterestingWindows != 0 || numDrawnWindows != 0
8568 || inPendingTransaction || allDrawn) {
8569 pw.print(prefix); pw.print("numInterestingWindows=");
8570 pw.print(numInterestingWindows);
8571 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8572 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8573 pw.print(" allDrawn="); pw.println(allDrawn);
8574 }
8575 if (animating || animation != null) {
8576 pw.print(prefix); pw.print("animating="); pw.print(animating);
8577 pw.print(" animation="); pw.println(animation);
8578 }
8579 if (animLayerAdjustment != 0) {
8580 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8581 }
8582 if (hasTransformation) {
8583 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8584 pw.print(" transformation="); transformation.printShortString(pw);
8585 pw.println();
8586 }
8587 if (startingData != null || removed || firstWindowDrawn) {
8588 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8589 pw.print(" removed="); pw.print(removed);
8590 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8591 }
8592 if (startingWindow != null || startingView != null
8593 || startingDisplayed || startingMoved) {
8594 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8595 pw.print(" startingView="); pw.print(startingView);
8596 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8597 pw.print(" startingMoved"); pw.println(startingMoved);
8598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008599 }
8600
8601 @Override
8602 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008603 if (stringName == null) {
8604 StringBuilder sb = new StringBuilder();
8605 sb.append("AppWindowToken{");
8606 sb.append(Integer.toHexString(System.identityHashCode(this)));
8607 sb.append(" token="); sb.append(token); sb.append('}');
8608 stringName = sb.toString();
8609 }
8610 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008611 }
8612 }
Romain Guy06882f82009-06-10 13:36:04 -07008613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008614 // -------------------------------------------------------------
8615 // DummyAnimation
8616 // -------------------------------------------------------------
8617
8618 // This is an animation that does nothing: it just immediately finishes
8619 // itself every time it is called. It is used as a stub animation in cases
8620 // where we want to synchronize multiple things that may be animating.
8621 static final class DummyAnimation extends Animation {
8622 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8623 return false;
8624 }
8625 }
8626 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008628 // -------------------------------------------------------------
8629 // Async Handler
8630 // -------------------------------------------------------------
8631
8632 static final class StartingData {
8633 final String pkg;
8634 final int theme;
8635 final CharSequence nonLocalizedLabel;
8636 final int labelRes;
8637 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008639 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8640 int _labelRes, int _icon) {
8641 pkg = _pkg;
8642 theme = _theme;
8643 nonLocalizedLabel = _nonLocalizedLabel;
8644 labelRes = _labelRes;
8645 icon = _icon;
8646 }
8647 }
8648
8649 private final class H extends Handler {
8650 public static final int REPORT_FOCUS_CHANGE = 2;
8651 public static final int REPORT_LOSING_FOCUS = 3;
8652 public static final int ANIMATE = 4;
8653 public static final int ADD_STARTING = 5;
8654 public static final int REMOVE_STARTING = 6;
8655 public static final int FINISHED_STARTING = 7;
8656 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008657 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8658 public static final int HOLD_SCREEN_CHANGED = 12;
8659 public static final int APP_TRANSITION_TIMEOUT = 13;
8660 public static final int PERSIST_ANIMATION_SCALE = 14;
8661 public static final int FORCE_GC = 15;
8662 public static final int ENABLE_SCREEN = 16;
8663 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008664 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008666 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 public H() {
8669 }
Romain Guy06882f82009-06-10 13:36:04 -07008670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008671 @Override
8672 public void handleMessage(Message msg) {
8673 switch (msg.what) {
8674 case REPORT_FOCUS_CHANGE: {
8675 WindowState lastFocus;
8676 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 synchronized(mWindowMap) {
8679 lastFocus = mLastFocus;
8680 newFocus = mCurrentFocus;
8681 if (lastFocus == newFocus) {
8682 // Focus is not changing, so nothing to do.
8683 return;
8684 }
8685 mLastFocus = newFocus;
8686 //Log.i(TAG, "Focus moving from " + lastFocus
8687 // + " to " + newFocus);
8688 if (newFocus != null && lastFocus != null
8689 && !newFocus.isDisplayedLw()) {
8690 //Log.i(TAG, "Delaying loss of focus...");
8691 mLosingFocus.add(lastFocus);
8692 lastFocus = null;
8693 }
8694 }
8695
8696 if (lastFocus != newFocus) {
8697 //System.out.println("Changing focus from " + lastFocus
8698 // + " to " + newFocus);
8699 if (newFocus != null) {
8700 try {
8701 //Log.i(TAG, "Gaining focus: " + newFocus);
8702 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8703 } catch (RemoteException e) {
8704 // Ignore if process has died.
8705 }
8706 }
8707
8708 if (lastFocus != null) {
8709 try {
8710 //Log.i(TAG, "Losing focus: " + lastFocus);
8711 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8712 } catch (RemoteException e) {
8713 // Ignore if process has died.
8714 }
8715 }
8716 }
8717 } break;
8718
8719 case REPORT_LOSING_FOCUS: {
8720 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 synchronized(mWindowMap) {
8723 losers = mLosingFocus;
8724 mLosingFocus = new ArrayList<WindowState>();
8725 }
8726
8727 final int N = losers.size();
8728 for (int i=0; i<N; i++) {
8729 try {
8730 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8731 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8732 } catch (RemoteException e) {
8733 // Ignore if process has died.
8734 }
8735 }
8736 } break;
8737
8738 case ANIMATE: {
8739 synchronized(mWindowMap) {
8740 mAnimationPending = false;
8741 performLayoutAndPlaceSurfacesLocked();
8742 }
8743 } break;
8744
8745 case ADD_STARTING: {
8746 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8747 final StartingData sd = wtoken.startingData;
8748
8749 if (sd == null) {
8750 // Animation has been canceled... do nothing.
8751 return;
8752 }
Romain Guy06882f82009-06-10 13:36:04 -07008753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008754 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8755 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008757 View view = null;
8758 try {
8759 view = mPolicy.addStartingWindow(
8760 wtoken.token, sd.pkg,
8761 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8762 sd.icon);
8763 } catch (Exception e) {
8764 Log.w(TAG, "Exception when adding starting window", e);
8765 }
8766
8767 if (view != null) {
8768 boolean abort = false;
8769
8770 synchronized(mWindowMap) {
8771 if (wtoken.removed || wtoken.startingData == null) {
8772 // If the window was successfully added, then
8773 // we need to remove it.
8774 if (wtoken.startingWindow != null) {
8775 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8776 "Aborted starting " + wtoken
8777 + ": removed=" + wtoken.removed
8778 + " startingData=" + wtoken.startingData);
8779 wtoken.startingWindow = null;
8780 wtoken.startingData = null;
8781 abort = true;
8782 }
8783 } else {
8784 wtoken.startingView = view;
8785 }
8786 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8787 "Added starting " + wtoken
8788 + ": startingWindow="
8789 + wtoken.startingWindow + " startingView="
8790 + wtoken.startingView);
8791 }
8792
8793 if (abort) {
8794 try {
8795 mPolicy.removeStartingWindow(wtoken.token, view);
8796 } catch (Exception e) {
8797 Log.w(TAG, "Exception when removing starting window", e);
8798 }
8799 }
8800 }
8801 } break;
8802
8803 case REMOVE_STARTING: {
8804 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8805 IBinder token = null;
8806 View view = null;
8807 synchronized (mWindowMap) {
8808 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8809 + wtoken + ": startingWindow="
8810 + wtoken.startingWindow + " startingView="
8811 + wtoken.startingView);
8812 if (wtoken.startingWindow != null) {
8813 view = wtoken.startingView;
8814 token = wtoken.token;
8815 wtoken.startingData = null;
8816 wtoken.startingView = null;
8817 wtoken.startingWindow = null;
8818 }
8819 }
8820 if (view != null) {
8821 try {
8822 mPolicy.removeStartingWindow(token, view);
8823 } catch (Exception e) {
8824 Log.w(TAG, "Exception when removing starting window", e);
8825 }
8826 }
8827 } break;
8828
8829 case FINISHED_STARTING: {
8830 IBinder token = null;
8831 View view = null;
8832 while (true) {
8833 synchronized (mWindowMap) {
8834 final int N = mFinishedStarting.size();
8835 if (N <= 0) {
8836 break;
8837 }
8838 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8839
8840 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8841 "Finished starting " + wtoken
8842 + ": startingWindow=" + wtoken.startingWindow
8843 + " startingView=" + wtoken.startingView);
8844
8845 if (wtoken.startingWindow == null) {
8846 continue;
8847 }
8848
8849 view = wtoken.startingView;
8850 token = wtoken.token;
8851 wtoken.startingData = null;
8852 wtoken.startingView = null;
8853 wtoken.startingWindow = null;
8854 }
8855
8856 try {
8857 mPolicy.removeStartingWindow(token, view);
8858 } catch (Exception e) {
8859 Log.w(TAG, "Exception when removing starting window", e);
8860 }
8861 }
8862 } break;
8863
8864 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8865 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8866
8867 boolean nowVisible = msg.arg1 != 0;
8868 boolean nowGone = msg.arg2 != 0;
8869
8870 try {
8871 if (DEBUG_VISIBILITY) Log.v(
8872 TAG, "Reporting visible in " + wtoken
8873 + " visible=" + nowVisible
8874 + " gone=" + nowGone);
8875 if (nowVisible) {
8876 wtoken.appToken.windowsVisible();
8877 } else {
8878 wtoken.appToken.windowsGone();
8879 }
8880 } catch (RemoteException ex) {
8881 }
8882 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008884 case WINDOW_FREEZE_TIMEOUT: {
8885 synchronized (mWindowMap) {
8886 Log.w(TAG, "Window freeze timeout expired.");
8887 int i = mWindows.size();
8888 while (i > 0) {
8889 i--;
8890 WindowState w = (WindowState)mWindows.get(i);
8891 if (w.mOrientationChanging) {
8892 w.mOrientationChanging = false;
8893 Log.w(TAG, "Force clearing orientation change: " + w);
8894 }
8895 }
8896 performLayoutAndPlaceSurfacesLocked();
8897 }
8898 break;
8899 }
Romain Guy06882f82009-06-10 13:36:04 -07008900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008901 case HOLD_SCREEN_CHANGED: {
8902 Session oldHold;
8903 Session newHold;
8904 synchronized (mWindowMap) {
8905 oldHold = mLastReportedHold;
8906 newHold = (Session)msg.obj;
8907 mLastReportedHold = newHold;
8908 }
Romain Guy06882f82009-06-10 13:36:04 -07008909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008910 if (oldHold != newHold) {
8911 try {
8912 if (oldHold != null) {
8913 mBatteryStats.noteStopWakelock(oldHold.mUid,
8914 "window",
8915 BatteryStats.WAKE_TYPE_WINDOW);
8916 }
8917 if (newHold != null) {
8918 mBatteryStats.noteStartWakelock(newHold.mUid,
8919 "window",
8920 BatteryStats.WAKE_TYPE_WINDOW);
8921 }
8922 } catch (RemoteException e) {
8923 }
8924 }
8925 break;
8926 }
Romain Guy06882f82009-06-10 13:36:04 -07008927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008928 case APP_TRANSITION_TIMEOUT: {
8929 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008930 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008931 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8932 "*** APP TRANSITION TIMEOUT");
8933 mAppTransitionReady = true;
8934 mAppTransitionTimeout = true;
8935 performLayoutAndPlaceSurfacesLocked();
8936 }
8937 }
8938 break;
8939 }
Romain Guy06882f82009-06-10 13:36:04 -07008940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008941 case PERSIST_ANIMATION_SCALE: {
8942 Settings.System.putFloat(mContext.getContentResolver(),
8943 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8944 Settings.System.putFloat(mContext.getContentResolver(),
8945 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8946 break;
8947 }
Romain Guy06882f82009-06-10 13:36:04 -07008948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008949 case FORCE_GC: {
8950 synchronized(mWindowMap) {
8951 if (mAnimationPending) {
8952 // If we are animating, don't do the gc now but
8953 // delay a bit so we don't interrupt the animation.
8954 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8955 2000);
8956 return;
8957 }
8958 // If we are currently rotating the display, it will
8959 // schedule a new message when done.
8960 if (mDisplayFrozen) {
8961 return;
8962 }
8963 mFreezeGcPending = 0;
8964 }
8965 Runtime.getRuntime().gc();
8966 break;
8967 }
Romain Guy06882f82009-06-10 13:36:04 -07008968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008969 case ENABLE_SCREEN: {
8970 performEnableScreen();
8971 break;
8972 }
Romain Guy06882f82009-06-10 13:36:04 -07008973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008974 case APP_FREEZE_TIMEOUT: {
8975 synchronized (mWindowMap) {
8976 Log.w(TAG, "App freeze timeout expired.");
8977 int i = mAppTokens.size();
8978 while (i > 0) {
8979 i--;
8980 AppWindowToken tok = mAppTokens.get(i);
8981 if (tok.freezingScreen) {
8982 Log.w(TAG, "Force clearing freeze: " + tok);
8983 unsetAppFreezingScreenLocked(tok, true, true);
8984 }
8985 }
8986 }
8987 break;
8988 }
Romain Guy06882f82009-06-10 13:36:04 -07008989
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008990 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008991 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008992 sendNewConfiguration();
8993 }
8994 break;
8995 }
Romain Guy06882f82009-06-10 13:36:04 -07008996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008997 }
8998 }
8999 }
9000
9001 // -------------------------------------------------------------
9002 // IWindowManager API
9003 // -------------------------------------------------------------
9004
9005 public IWindowSession openSession(IInputMethodClient client,
9006 IInputContext inputContext) {
9007 if (client == null) throw new IllegalArgumentException("null client");
9008 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9009 return new Session(client, inputContext);
9010 }
9011
9012 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9013 synchronized (mWindowMap) {
9014 // The focus for the client is the window immediately below
9015 // where we would place the input method window.
9016 int idx = findDesiredInputMethodWindowIndexLocked(false);
9017 WindowState imFocus;
9018 if (idx > 0) {
9019 imFocus = (WindowState)mWindows.get(idx-1);
9020 if (imFocus != null) {
9021 if (imFocus.mSession.mClient != null &&
9022 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9023 return true;
9024 }
9025 }
9026 }
9027 }
9028 return false;
9029 }
Romain Guy06882f82009-06-10 13:36:04 -07009030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009031 // -------------------------------------------------------------
9032 // Internals
9033 // -------------------------------------------------------------
9034
9035 final WindowState windowForClientLocked(Session session, IWindow client) {
9036 return windowForClientLocked(session, client.asBinder());
9037 }
Romain Guy06882f82009-06-10 13:36:04 -07009038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009039 final WindowState windowForClientLocked(Session session, IBinder client) {
9040 WindowState win = mWindowMap.get(client);
9041 if (localLOGV) Log.v(
9042 TAG, "Looking up client " + client + ": " + win);
9043 if (win == null) {
9044 RuntimeException ex = new RuntimeException();
9045 Log.w(TAG, "Requested window " + client + " does not exist", ex);
9046 return null;
9047 }
9048 if (session != null && win.mSession != session) {
9049 RuntimeException ex = new RuntimeException();
9050 Log.w(TAG, "Requested window " + client + " is in session " +
9051 win.mSession + ", not " + session, ex);
9052 return null;
9053 }
9054
9055 return win;
9056 }
9057
Dianne Hackborna8f60182009-09-01 19:01:50 -07009058 final void rebuildAppWindowListLocked() {
9059 int NW = mWindows.size();
9060 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009061 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009062 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009063
Dianne Hackborna8f60182009-09-01 19:01:50 -07009064 // First remove all existing app windows.
9065 i=0;
9066 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009067 WindowState w = (WindowState)mWindows.get(i);
9068 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009069 WindowState win = (WindowState)mWindows.remove(i);
9070 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
9071 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009072 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009073 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009074 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009075 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9076 && lastWallpaper == i-1) {
9077 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009078 }
9079 i++;
9080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009081
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009082 // The wallpaper window(s) typically live at the bottom of the stack,
9083 // so skip them before adding app tokens.
9084 lastWallpaper++;
9085 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009086
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009087 // First add all of the exiting app tokens... these are no longer
9088 // in the main app list, but still have windows shown. We put them
9089 // in the back because now that the animation is over we no longer
9090 // will care about them.
9091 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009092 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009093 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009095
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009096 // And add in the still active app tokens in Z order.
9097 NT = mAppTokens.size();
9098 for (int j=0; j<NT; j++) {
9099 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009101
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009102 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009103 if (i != numRemoved) {
9104 Log.w(TAG, "Rebuild removed " + numRemoved
9105 + " windows but added " + i);
9106 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009109 private final void assignLayersLocked() {
9110 int N = mWindows.size();
9111 int curBaseLayer = 0;
9112 int curLayer = 0;
9113 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 for (i=0; i<N; i++) {
9116 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009117 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9118 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009119 curLayer += WINDOW_LAYER_MULTIPLIER;
9120 w.mLayer = curLayer;
9121 } else {
9122 curBaseLayer = curLayer = w.mBaseLayer;
9123 w.mLayer = curLayer;
9124 }
9125 if (w.mTargetAppToken != null) {
9126 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9127 } else if (w.mAppToken != null) {
9128 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9129 } else {
9130 w.mAnimLayer = w.mLayer;
9131 }
9132 if (w.mIsImWindow) {
9133 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009134 } else if (w.mIsWallpaper) {
9135 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009136 }
9137 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9138 + w.mAnimLayer);
9139 //System.out.println(
9140 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9141 }
9142 }
9143
9144 private boolean mInLayout = false;
9145 private final void performLayoutAndPlaceSurfacesLocked() {
9146 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009147 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009148 throw new RuntimeException("Recursive call!");
9149 }
9150 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9151 return;
9152 }
9153
9154 boolean recoveringMemory = false;
9155 if (mForceRemoves != null) {
9156 recoveringMemory = true;
9157 // Wait a little it for things to settle down, and off we go.
9158 for (int i=0; i<mForceRemoves.size(); i++) {
9159 WindowState ws = mForceRemoves.get(i);
9160 Log.i(TAG, "Force removing: " + ws);
9161 removeWindowInnerLocked(ws.mSession, ws);
9162 }
9163 mForceRemoves = null;
9164 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9165 Object tmp = new Object();
9166 synchronized (tmp) {
9167 try {
9168 tmp.wait(250);
9169 } catch (InterruptedException e) {
9170 }
9171 }
9172 }
Romain Guy06882f82009-06-10 13:36:04 -07009173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009174 mInLayout = true;
9175 try {
9176 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009178 int i = mPendingRemove.size()-1;
9179 if (i >= 0) {
9180 while (i >= 0) {
9181 WindowState w = mPendingRemove.get(i);
9182 removeWindowInnerLocked(w.mSession, w);
9183 i--;
9184 }
9185 mPendingRemove.clear();
9186
9187 mInLayout = false;
9188 assignLayersLocked();
9189 mLayoutNeeded = true;
9190 performLayoutAndPlaceSurfacesLocked();
9191
9192 } else {
9193 mInLayout = false;
9194 if (mLayoutNeeded) {
9195 requestAnimationLocked(0);
9196 }
9197 }
9198 } catch (RuntimeException e) {
9199 mInLayout = false;
9200 Log.e(TAG, "Unhandled exception while layout out windows", e);
9201 }
9202 }
9203
9204 private final void performLayoutLockedInner() {
9205 final int dw = mDisplay.getWidth();
9206 final int dh = mDisplay.getHeight();
9207
9208 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009209 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009210 int i;
9211
9212 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009213
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009214 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215 mPolicy.beginLayoutLw(dw, dh);
9216
9217 // First perform layout of any root windows (not attached
9218 // to another window).
9219 int topAttached = -1;
9220 for (i = N-1; i >= 0; i--) {
9221 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009222
9223 // Don't do layout of a window if it is not visible, or
9224 // soon won't be visible, to avoid wasting time and funky
9225 // changes while a window is animating away.
9226 final AppWindowToken atoken = win.mAppToken;
9227 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009229 || win.mRootToken.hidden
9230 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009231 || win.mAttachedHidden
9232 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233
9234 // If this view is GONE, then skip it -- keep the current
9235 // frame, and let the caller know so they can ignore it
9236 // if they want. (We do the normal layout for INVISIBLE
9237 // windows, since that means "perform layout as normal,
9238 // just don't display").
9239 if (!gone || !win.mHaveFrame) {
9240 if (!win.mLayoutAttached) {
9241 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9242 } else {
9243 if (topAttached < 0) topAttached = i;
9244 }
9245 }
9246 }
Romain Guy06882f82009-06-10 13:36:04 -07009247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009248 // Now perform layout of attached windows, which usually
9249 // depend on the position of the window they are attached to.
9250 // XXX does not deal with windows that are attached to windows
9251 // that are themselves attached.
9252 for (i = topAttached; i >= 0; i--) {
9253 WindowState win = (WindowState) mWindows.get(i);
9254
9255 // If this view is GONE, then skip it -- keep the current
9256 // frame, and let the caller know so they can ignore it
9257 // if they want. (We do the normal layout for INVISIBLE
9258 // windows, since that means "perform layout as normal,
9259 // just don't display").
9260 if (win.mLayoutAttached) {
9261 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9262 || !win.mHaveFrame) {
9263 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9264 }
9265 }
9266 }
9267
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009268 int changes = mPolicy.finishLayoutLw();
9269 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9270 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9271 assignLayersLocked();
9272 }
9273 }
9274 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009275 mLayoutNeeded = false;
9276 } else if (repeats > 2) {
9277 Log.w(TAG, "Layout repeat aborted after too many iterations");
9278 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009279 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9280 Configuration newConfig = updateOrientationFromAppTokensLocked(
9281 null, null);
9282 if (newConfig != null) {
9283 mLayoutNeeded = true;
9284 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9285 }
9286 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009287 } else {
9288 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009289 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9290 Configuration newConfig = updateOrientationFromAppTokensLocked(
9291 null, null);
9292 if (newConfig != null) {
9293 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9294 }
9295 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009297 }
9298 }
Romain Guy06882f82009-06-10 13:36:04 -07009299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300 private final void performLayoutAndPlaceSurfacesLockedInner(
9301 boolean recoveringMemory) {
9302 final long currentTime = SystemClock.uptimeMillis();
9303 final int dw = mDisplay.getWidth();
9304 final int dh = mDisplay.getHeight();
9305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009306 int i;
9307
9308 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009309 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009311 if (mFxSession == null) {
9312 mFxSession = new SurfaceSession();
9313 }
Romain Guy06882f82009-06-10 13:36:04 -07009314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009315 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9316
9317 // Initialize state of exiting tokens.
9318 for (i=mExitingTokens.size()-1; i>=0; i--) {
9319 mExitingTokens.get(i).hasVisible = false;
9320 }
9321
9322 // Initialize state of exiting applications.
9323 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9324 mExitingAppTokens.get(i).hasVisible = false;
9325 }
9326
9327 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009328 boolean orientationChangeComplete = true;
9329 Session holdScreen = null;
9330 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009331 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009332 boolean focusDisplayed = false;
9333 boolean animating = false;
9334
9335 Surface.openTransaction();
9336 try {
9337 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009338 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339
9340 do {
9341 final int transactionSequence = ++mTransactionSequence;
9342
9343 // Update animations of all applications, including those
9344 // associated with exiting/removed apps
9345 boolean tokensAnimating = false;
9346 final int NAT = mAppTokens.size();
9347 for (i=0; i<NAT; i++) {
9348 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9349 tokensAnimating = true;
9350 }
9351 }
9352 final int NEAT = mExitingAppTokens.size();
9353 for (i=0; i<NEAT; i++) {
9354 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9355 tokensAnimating = true;
9356 }
9357 }
9358
9359 animating = tokensAnimating;
9360 restart = false;
9361
9362 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009363 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009364 boolean focusMayChange = false;
9365 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009366
9367 mPolicy.beginAnimationLw(dw, dh);
9368
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009369 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009371 for (i=N-1; i>=0; i--) {
9372 WindowState w = (WindowState)mWindows.get(i);
9373
9374 final WindowManager.LayoutParams attrs = w.mAttrs;
9375
9376 if (w.mSurface != null) {
9377 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009378 if (w.commitFinishDrawingLocked(currentTime)) {
9379 if ((w.mAttrs.flags
9380 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009381 if (DEBUG_WALLPAPER) Log.v(TAG,
9382 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009383 wallpaperMayChange = true;
9384 }
9385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009386
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009387 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009388 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9389 animating = true;
9390 //w.dump(" ");
9391 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009392 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9393 wallpaperMayChange = true;
9394 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009395
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009396 if (mPolicy.doesForceHide(w, attrs)) {
9397 if (!wasAnimating && animating) {
9398 wallpaperForceHidingChanged = true;
9399 focusMayChange = true;
9400 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9401 forceHiding = true;
9402 }
9403 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9404 boolean changed;
9405 if (forceHiding) {
9406 changed = w.hideLw(false, false);
9407 } else {
9408 changed = w.showLw(false, false);
9409 if (changed && wallpaperForceHidingChanged
9410 && w.isReadyForDisplay()) {
9411 // Assume we will need to animate. If
9412 // we don't (because the wallpaper will
9413 // stay with the lock screen), then we will
9414 // clean up later.
9415 Animation a = mPolicy.createForceHideEnterAnimation();
9416 if (a != null) {
9417 w.setAnimation(a);
9418 }
9419 }
9420 }
9421 if (changed && (attrs.flags
9422 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9423 wallpaperMayChange = true;
9424 }
9425 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 mPolicy.animatingWindowLw(w, attrs);
9428 }
9429
9430 final AppWindowToken atoken = w.mAppToken;
9431 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9432 if (atoken.lastTransactionSequence != transactionSequence) {
9433 atoken.lastTransactionSequence = transactionSequence;
9434 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9435 atoken.startingDisplayed = false;
9436 }
9437 if ((w.isOnScreen() || w.mAttrs.type
9438 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9439 && !w.mExiting && !w.mDestroying) {
9440 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009441 Log.v(TAG, "Eval win " + w + ": isDrawn="
9442 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009443 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009444 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009445 Log.v(TAG, "Not displayed: s=" + w.mSurface
9446 + " pv=" + w.mPolicyVisibility
9447 + " dp=" + w.mDrawPending
9448 + " cdp=" + w.mCommitDrawPending
9449 + " ah=" + w.mAttachedHidden
9450 + " th=" + atoken.hiddenRequested
9451 + " a=" + w.mAnimating);
9452 }
9453 }
9454 if (w != atoken.startingWindow) {
9455 if (!atoken.freezingScreen || !w.mAppFreezing) {
9456 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009457 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009458 atoken.numDrawnWindows++;
9459 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9460 "tokenMayBeDrawn: " + atoken
9461 + " freezingScreen=" + atoken.freezingScreen
9462 + " mAppFreezing=" + w.mAppFreezing);
9463 tokenMayBeDrawn = true;
9464 }
9465 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009466 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009467 atoken.startingDisplayed = true;
9468 }
9469 }
9470 } else if (w.mReadyToShow) {
9471 w.performShowLocked();
9472 }
9473 }
9474
9475 if (mPolicy.finishAnimationLw()) {
9476 restart = true;
9477 }
9478
9479 if (tokenMayBeDrawn) {
9480 // See if any windows have been drawn, so they (and others
9481 // associated with them) can now be shown.
9482 final int NT = mTokenList.size();
9483 for (i=0; i<NT; i++) {
9484 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9485 if (wtoken == null) {
9486 continue;
9487 }
9488 if (wtoken.freezingScreen) {
9489 int numInteresting = wtoken.numInterestingWindows;
9490 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9491 if (DEBUG_VISIBILITY) Log.v(TAG,
9492 "allDrawn: " + wtoken
9493 + " interesting=" + numInteresting
9494 + " drawn=" + wtoken.numDrawnWindows);
9495 wtoken.showAllWindowsLocked();
9496 unsetAppFreezingScreenLocked(wtoken, false, true);
9497 orientationChangeComplete = true;
9498 }
9499 } else if (!wtoken.allDrawn) {
9500 int numInteresting = wtoken.numInterestingWindows;
9501 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9502 if (DEBUG_VISIBILITY) Log.v(TAG,
9503 "allDrawn: " + wtoken
9504 + " interesting=" + numInteresting
9505 + " drawn=" + wtoken.numDrawnWindows);
9506 wtoken.allDrawn = true;
9507 restart = true;
9508
9509 // We can now show all of the drawn windows!
9510 if (!mOpeningApps.contains(wtoken)) {
9511 wtoken.showAllWindowsLocked();
9512 }
9513 }
9514 }
9515 }
9516 }
9517
9518 // If we are ready to perform an app transition, check through
9519 // all of the app tokens to be shown and see if they are ready
9520 // to go.
9521 if (mAppTransitionReady) {
9522 int NN = mOpeningApps.size();
9523 boolean goodToGo = true;
9524 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9525 "Checking " + NN + " opening apps (frozen="
9526 + mDisplayFrozen + " timeout="
9527 + mAppTransitionTimeout + ")...");
9528 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9529 // If the display isn't frozen, wait to do anything until
9530 // all of the apps are ready. Otherwise just go because
9531 // we'll unfreeze the display when everyone is ready.
9532 for (i=0; i<NN && goodToGo; i++) {
9533 AppWindowToken wtoken = mOpeningApps.get(i);
9534 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9535 "Check opening app" + wtoken + ": allDrawn="
9536 + wtoken.allDrawn + " startingDisplayed="
9537 + wtoken.startingDisplayed);
9538 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9539 && !wtoken.startingMoved) {
9540 goodToGo = false;
9541 }
9542 }
9543 }
9544 if (goodToGo) {
9545 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9546 int transit = mNextAppTransition;
9547 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009548 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009549 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009550 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009551 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009552 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009553 mAppTransitionTimeout = false;
9554 mStartingIconInTransition = false;
9555 mSkipAppTransitionAnimation = false;
9556
9557 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9558
Dianne Hackborna8f60182009-09-01 19:01:50 -07009559 // If there are applications waiting to come to the
9560 // top of the stack, now is the time to move their windows.
9561 // (Note that we don't do apps going to the bottom
9562 // here -- we want to keep their windows in the old
9563 // Z-order until the animation completes.)
9564 if (mToTopApps.size() > 0) {
9565 NN = mAppTokens.size();
9566 for (i=0; i<NN; i++) {
9567 AppWindowToken wtoken = mAppTokens.get(i);
9568 if (wtoken.sendingToTop) {
9569 wtoken.sendingToTop = false;
9570 moveAppWindowsLocked(wtoken, NN, false);
9571 }
9572 }
9573 mToTopApps.clear();
9574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009575
Dianne Hackborn25994b42009-09-04 14:21:19 -07009576 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009577
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009578 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009579 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009580
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009581 // The top-most window will supply the layout params,
9582 // and we will determine it below.
9583 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009584 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009585 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009586
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009587 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009588 "New wallpaper target=" + mWallpaperTarget
9589 + ", lower target=" + mLowerWallpaperTarget
9590 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009591 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009592 // Do a first pass through the tokens for two
9593 // things:
9594 // (1) Determine if both the closing and opening
9595 // app token sets are wallpaper targets, in which
9596 // case special animations are needed
9597 // (since the wallpaper needs to stay static
9598 // behind them).
9599 // (2) Find the layout params of the top-most
9600 // application window in the tokens, which is
9601 // what will control the animation theme.
9602 final int NC = mClosingApps.size();
9603 NN = NC + mOpeningApps.size();
9604 for (i=0; i<NN; i++) {
9605 AppWindowToken wtoken;
9606 int mode;
9607 if (i < NC) {
9608 wtoken = mClosingApps.get(i);
9609 mode = 1;
9610 } else {
9611 wtoken = mOpeningApps.get(i-NC);
9612 mode = 2;
9613 }
9614 if (mLowerWallpaperTarget != null) {
9615 if (mLowerWallpaperTarget.mAppToken == wtoken
9616 || mUpperWallpaperTarget.mAppToken == wtoken) {
9617 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009618 }
9619 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009620 if (wtoken.appFullscreen) {
9621 WindowState ws = wtoken.findMainWindow();
9622 if (ws != null) {
9623 // If this is a compatibility mode
9624 // window, we will always use its anim.
9625 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9626 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009627 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009628 bestAnimLayer = Integer.MAX_VALUE;
9629 } else if (ws.mLayer > bestAnimLayer) {
9630 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009631 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009632 bestAnimLayer = ws.mLayer;
9633 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009634 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009635 }
9636 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009637
Dianne Hackborn25994b42009-09-04 14:21:19 -07009638 if (foundWallpapers == 3) {
9639 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9640 "Wallpaper animation!");
9641 switch (transit) {
9642 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9643 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9644 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9645 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9646 break;
9647 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9648 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9649 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9650 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9651 break;
9652 }
9653 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9654 "New transit: " + transit);
9655 } else if (oldWallpaper != null) {
9656 // We are transitioning from an activity with
9657 // a wallpaper to one without.
9658 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9659 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9660 "New transit away from wallpaper: " + transit);
9661 } else if (mWallpaperTarget != null) {
9662 // We are transitioning from an activity without
9663 // a wallpaper to now showing the wallpaper
9664 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9665 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9666 "New transit into wallpaper: " + transit);
9667 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009668
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009669 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9670 mLastEnterAnimToken = animToken;
9671 mLastEnterAnimParams = animLp;
9672 } else if (mLastEnterAnimParams != null) {
9673 animLp = mLastEnterAnimParams;
9674 mLastEnterAnimToken = null;
9675 mLastEnterAnimParams = null;
9676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 NN = mOpeningApps.size();
9679 for (i=0; i<NN; i++) {
9680 AppWindowToken wtoken = mOpeningApps.get(i);
9681 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9682 "Now opening app" + wtoken);
9683 wtoken.reportedVisible = false;
9684 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009685 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009686 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009687 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009688 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 wtoken.showAllWindowsLocked();
9690 }
9691 NN = mClosingApps.size();
9692 for (i=0; i<NN; i++) {
9693 AppWindowToken wtoken = mClosingApps.get(i);
9694 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9695 "Now closing app" + wtoken);
9696 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009697 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009698 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009699 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009700 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009701 // Force the allDrawn flag, because we want to start
9702 // this guy's animations regardless of whether it's
9703 // gotten drawn.
9704 wtoken.allDrawn = true;
9705 }
9706
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009707 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009709 mOpeningApps.clear();
9710 mClosingApps.clear();
9711
9712 // This has changed the visibility of windows, so perform
9713 // a new layout to get them all up-to-date.
9714 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009715 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9716 assignLayersLocked();
9717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009718 performLayoutLockedInner();
9719 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009720 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009721
9722 restart = true;
9723 }
9724 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009725
Dianne Hackborna8f60182009-09-01 19:01:50 -07009726 if (!animating && mAppTransitionRunning) {
9727 // We have finished the animation of an app transition. To do
9728 // this, we have delayed a lot of operations like showing and
9729 // hiding apps, moving apps in Z-order, etc. The app token list
9730 // reflects the correct Z-order, but the window list may now
9731 // be out of sync with it. So here we will just rebuild the
9732 // entire app window list. Fun!
9733 mAppTransitionRunning = false;
9734 // Clear information about apps that were moving.
9735 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009736
Dianne Hackborna8f60182009-09-01 19:01:50 -07009737 rebuildAppWindowListLocked();
9738 restart = true;
9739 moveInputMethodWindowsIfNeededLocked(false);
9740 wallpaperMayChange = true;
9741 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009742 // Since the window list has been rebuilt, focus might
9743 // have to be recomputed since the actual order of windows
9744 // might have changed again.
9745 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009747
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009748 int adjResult = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009749
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009750 if (wallpaperForceHidingChanged) {
9751 // At this point, there was a window with a wallpaper that
9752 // was force hiding other windows behind it, but now it
9753 // is going away. This may be simple -- just animate
9754 // away the wallpaper and its window -- or it may be
9755 // hard -- the wallpaper now needs to be shown behind
9756 // something that was hidden.
9757 WindowState oldWallpaper = mWallpaperTarget;
9758 adjResult = adjustWallpaperWindowsLocked();
9759 wallpaperMayChange = false;
9760 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9761 + " NEW: " + mWallpaperTarget);
9762 if (mLowerWallpaperTarget == null) {
9763 // Whoops, we don't need a special wallpaper animation.
9764 // Clear them out.
9765 forceHiding = false;
9766 for (i=N-1; i>=0; i--) {
9767 WindowState w = (WindowState)mWindows.get(i);
9768 if (w.mSurface != null) {
9769 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009770 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9771 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009772 forceHiding = true;
9773 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9774 if (!w.mAnimating) {
9775 // We set the animation above so it
9776 // is not yet running.
9777 w.clearAnimation();
9778 }
9779 }
9780 }
9781 }
9782 }
9783 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009784
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009785 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009786 if (DEBUG_WALLPAPER) Log.v(TAG,
9787 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009788 adjResult = adjustWallpaperWindowsLocked();
9789 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009790
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009791 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9792 if (DEBUG_WALLPAPER) Log.v(TAG,
9793 "Wallpaper layer changed: assigning layers + relayout");
9794 restart = true;
9795 mLayoutNeeded = true;
9796 assignLayersLocked();
9797 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9798 if (DEBUG_WALLPAPER) Log.v(TAG,
9799 "Wallpaper visibility changed: relayout");
9800 restart = true;
9801 mLayoutNeeded = true;
9802 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009803
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009804 if (focusMayChange) {
9805 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009806 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009807 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009808 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009809 }
9810
9811 if (mLayoutNeeded) {
9812 restart = true;
9813 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009814 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 } while (restart);
9817
9818 // THIRD LOOP: Update the surfaces of all windows.
9819
9820 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9821
9822 boolean obscured = false;
9823 boolean blurring = false;
9824 boolean dimming = false;
9825 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009826 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009827 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009828
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009829 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009831 for (i=N-1; i>=0; i--) {
9832 WindowState w = (WindowState)mWindows.get(i);
9833
9834 boolean displayed = false;
9835 final WindowManager.LayoutParams attrs = w.mAttrs;
9836 final int attrFlags = attrs.flags;
9837
9838 if (w.mSurface != null) {
9839 w.computeShownFrameLocked();
9840 if (localLOGV) Log.v(
9841 TAG, "Placing surface #" + i + " " + w.mSurface
9842 + ": new=" + w.mShownFrame + ", old="
9843 + w.mLastShownFrame);
9844
9845 boolean resize;
9846 int width, height;
9847 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9848 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9849 w.mLastRequestedHeight != w.mRequestedHeight;
9850 // for a scaled surface, we just want to use
9851 // the requested size.
9852 width = w.mRequestedWidth;
9853 height = w.mRequestedHeight;
9854 w.mLastRequestedWidth = width;
9855 w.mLastRequestedHeight = height;
9856 w.mLastShownFrame.set(w.mShownFrame);
9857 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009858 if (SHOW_TRANSACTIONS) Log.i(
9859 TAG, " SURFACE " + w.mSurface
9860 + ": POS " + w.mShownFrame.left
9861 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009862 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9863 } catch (RuntimeException e) {
9864 Log.w(TAG, "Error positioning surface in " + w, e);
9865 if (!recoveringMemory) {
9866 reclaimSomeSurfaceMemoryLocked(w, "position");
9867 }
9868 }
9869 } else {
9870 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9871 width = w.mShownFrame.width();
9872 height = w.mShownFrame.height();
9873 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009874 }
9875
9876 if (resize) {
9877 if (width < 1) width = 1;
9878 if (height < 1) height = 1;
9879 if (w.mSurface != null) {
9880 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009881 if (SHOW_TRANSACTIONS) Log.i(
9882 TAG, " SURFACE " + w.mSurface + ": POS "
9883 + w.mShownFrame.left + ","
9884 + w.mShownFrame.top + " SIZE "
9885 + w.mShownFrame.width() + "x"
9886 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009887 w.mSurface.setSize(width, height);
9888 w.mSurface.setPosition(w.mShownFrame.left,
9889 w.mShownFrame.top);
9890 } catch (RuntimeException e) {
9891 // If something goes wrong with the surface (such
9892 // as running out of memory), don't take down the
9893 // entire system.
9894 Log.e(TAG, "Failure updating surface of " + w
9895 + "size=(" + width + "x" + height
9896 + "), pos=(" + w.mShownFrame.left
9897 + "," + w.mShownFrame.top + ")", e);
9898 if (!recoveringMemory) {
9899 reclaimSomeSurfaceMemoryLocked(w, "size");
9900 }
9901 }
9902 }
9903 }
9904 if (!w.mAppFreezing) {
9905 w.mContentInsetsChanged =
9906 !w.mLastContentInsets.equals(w.mContentInsets);
9907 w.mVisibleInsetsChanged =
9908 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009909 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009910 || w.mContentInsetsChanged
9911 || w.mVisibleInsetsChanged) {
9912 w.mLastFrame.set(w.mFrame);
9913 w.mLastContentInsets.set(w.mContentInsets);
9914 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009915 // If the screen is currently frozen, then keep
9916 // it frozen until this window draws at its new
9917 // orientation.
9918 if (mDisplayFrozen) {
9919 if (DEBUG_ORIENTATION) Log.v(TAG,
9920 "Resizing while display frozen: " + w);
9921 w.mOrientationChanging = true;
9922 if (mWindowsFreezingScreen) {
9923 mWindowsFreezingScreen = true;
9924 // XXX should probably keep timeout from
9925 // when we first froze the display.
9926 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9927 mH.sendMessageDelayed(mH.obtainMessage(
9928 H.WINDOW_FREEZE_TIMEOUT), 2000);
9929 }
9930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009931 // If the orientation is changing, then we need to
9932 // hold off on unfreezing the display until this
9933 // window has been redrawn; to do that, we need
9934 // to go through the process of getting informed
9935 // by the application when it has finished drawing.
9936 if (w.mOrientationChanging) {
9937 if (DEBUG_ORIENTATION) Log.v(TAG,
9938 "Orientation start waiting for draw in "
9939 + w + ", surface " + w.mSurface);
9940 w.mDrawPending = true;
9941 w.mCommitDrawPending = false;
9942 w.mReadyToShow = false;
9943 if (w.mAppToken != null) {
9944 w.mAppToken.allDrawn = false;
9945 }
9946 }
Romain Guy06882f82009-06-10 13:36:04 -07009947 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948 "Resizing window " + w + " to " + w.mFrame);
9949 mResizingWindows.add(w);
9950 } else if (w.mOrientationChanging) {
9951 if (!w.mDrawPending && !w.mCommitDrawPending) {
9952 if (DEBUG_ORIENTATION) Log.v(TAG,
9953 "Orientation not waiting for draw in "
9954 + w + ", surface " + w.mSurface);
9955 w.mOrientationChanging = false;
9956 }
9957 }
9958 }
9959
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009960 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009961 if (!w.mLastHidden) {
9962 //dump();
9963 w.mLastHidden = true;
9964 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009965 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009966 if (w.mSurface != null) {
9967 try {
9968 w.mSurface.hide();
9969 } catch (RuntimeException e) {
9970 Log.w(TAG, "Exception hiding surface in " + w);
9971 }
9972 }
9973 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9974 }
9975 // If we are waiting for this window to handle an
9976 // orientation change, well, it is hidden, so
9977 // doesn't really matter. Note that this does
9978 // introduce a potential glitch if the window
9979 // becomes unhidden before it has drawn for the
9980 // new orientation.
9981 if (w.mOrientationChanging) {
9982 w.mOrientationChanging = false;
9983 if (DEBUG_ORIENTATION) Log.v(TAG,
9984 "Orientation change skips hidden " + w);
9985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009986 } else if (w.mLastLayer != w.mAnimLayer
9987 || w.mLastAlpha != w.mShownAlpha
9988 || w.mLastDsDx != w.mDsDx
9989 || w.mLastDtDx != w.mDtDx
9990 || w.mLastDsDy != w.mDsDy
9991 || w.mLastDtDy != w.mDtDy
9992 || w.mLastHScale != w.mHScale
9993 || w.mLastVScale != w.mVScale
9994 || w.mLastHidden) {
9995 displayed = true;
9996 w.mLastAlpha = w.mShownAlpha;
9997 w.mLastLayer = w.mAnimLayer;
9998 w.mLastDsDx = w.mDsDx;
9999 w.mLastDtDx = w.mDtDx;
10000 w.mLastDsDy = w.mDsDy;
10001 w.mLastDtDy = w.mDtDy;
10002 w.mLastHScale = w.mHScale;
10003 w.mLastVScale = w.mVScale;
10004 if (SHOW_TRANSACTIONS) Log.i(
10005 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010006 + w.mShownAlpha + " layer=" + w.mAnimLayer
10007 + " matrix=[" + (w.mDsDx*w.mHScale)
10008 + "," + (w.mDtDx*w.mVScale)
10009 + "][" + (w.mDsDy*w.mHScale)
10010 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011 if (w.mSurface != null) {
10012 try {
10013 w.mSurface.setAlpha(w.mShownAlpha);
10014 w.mSurface.setLayer(w.mAnimLayer);
10015 w.mSurface.setMatrix(
10016 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10017 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10018 } catch (RuntimeException e) {
10019 Log.w(TAG, "Error updating surface in " + w, e);
10020 if (!recoveringMemory) {
10021 reclaimSomeSurfaceMemoryLocked(w, "update");
10022 }
10023 }
10024 }
10025
10026 if (w.mLastHidden && !w.mDrawPending
10027 && !w.mCommitDrawPending
10028 && !w.mReadyToShow) {
10029 if (SHOW_TRANSACTIONS) Log.i(
10030 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
10031 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
10032 + " during relayout");
10033 if (showSurfaceRobustlyLocked(w)) {
10034 w.mHasDrawn = true;
10035 w.mLastHidden = false;
10036 } else {
10037 w.mOrientationChanging = false;
10038 }
10039 }
10040 if (w.mSurface != null) {
10041 w.mToken.hasVisible = true;
10042 }
10043 } else {
10044 displayed = true;
10045 }
10046
10047 if (displayed) {
10048 if (!covered) {
10049 if (attrs.width == LayoutParams.FILL_PARENT
10050 && attrs.height == LayoutParams.FILL_PARENT) {
10051 covered = true;
10052 }
10053 }
10054 if (w.mOrientationChanging) {
10055 if (w.mDrawPending || w.mCommitDrawPending) {
10056 orientationChangeComplete = false;
10057 if (DEBUG_ORIENTATION) Log.v(TAG,
10058 "Orientation continue waiting for draw in " + w);
10059 } else {
10060 w.mOrientationChanging = false;
10061 if (DEBUG_ORIENTATION) Log.v(TAG,
10062 "Orientation change complete in " + w);
10063 }
10064 }
10065 w.mToken.hasVisible = true;
10066 }
10067 } else if (w.mOrientationChanging) {
10068 if (DEBUG_ORIENTATION) Log.v(TAG,
10069 "Orientation change skips hidden " + w);
10070 w.mOrientationChanging = false;
10071 }
10072
10073 final boolean canBeSeen = w.isDisplayedLw();
10074
10075 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10076 focusDisplayed = true;
10077 }
10078
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010079 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010082 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010083 if (w.mSurface != null) {
10084 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10085 holdScreen = w.mSession;
10086 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010087 if (!syswin && w.mAttrs.screenBrightness >= 0
10088 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010089 screenBrightness = w.mAttrs.screenBrightness;
10090 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010091 if (!syswin && w.mAttrs.buttonBrightness >= 0
10092 && buttonBrightness < 0) {
10093 buttonBrightness = w.mAttrs.buttonBrightness;
10094 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010095 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10096 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10097 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10098 syswin = true;
10099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010100 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010101
Dianne Hackborn25994b42009-09-04 14:21:19 -070010102 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10103 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010104 // This window completely covers everything behind it,
10105 // so we want to leave all of them as unblurred (for
10106 // performance reasons).
10107 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010108 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
10109 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010110 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010111 obscured = true;
10112 if (mBackgroundFillerSurface == null) {
10113 try {
10114 mBackgroundFillerSurface = new Surface(mFxSession, 0,
10115 0, dw, dh,
10116 PixelFormat.OPAQUE,
10117 Surface.FX_SURFACE_NORMAL);
10118 } catch (Exception e) {
10119 Log.e(TAG, "Exception creating filler surface", e);
10120 }
10121 }
10122 try {
10123 mBackgroundFillerSurface.setPosition(0, 0);
10124 mBackgroundFillerSurface.setSize(dw, dh);
10125 // Using the same layer as Dim because they will never be shown at the
10126 // same time.
10127 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10128 mBackgroundFillerSurface.show();
10129 } catch (RuntimeException e) {
10130 Log.e(TAG, "Exception showing filler surface");
10131 }
10132 backgroundFillerShown = true;
10133 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010134 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10136 if (localLOGV) Log.v(TAG, "Win " + w
10137 + ": blurring=" + blurring
10138 + " obscured=" + obscured
10139 + " displayed=" + displayed);
10140 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10141 if (!dimming) {
10142 //Log.i(TAG, "DIM BEHIND: " + w);
10143 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010144 if (mDimAnimator == null) {
10145 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010147 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010148 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010149 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010150 }
10151 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10152 if (!blurring) {
10153 //Log.i(TAG, "BLUR BEHIND: " + w);
10154 blurring = true;
10155 mBlurShown = true;
10156 if (mBlurSurface == null) {
10157 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10158 + mBlurSurface + ": CREATE");
10159 try {
Romain Guy06882f82009-06-10 13:36:04 -070010160 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010161 -1, 16, 16,
10162 PixelFormat.OPAQUE,
10163 Surface.FX_SURFACE_BLUR);
10164 } catch (Exception e) {
10165 Log.e(TAG, "Exception creating Blur surface", e);
10166 }
10167 }
10168 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10169 + mBlurSurface + ": SHOW pos=(0,0) (" +
10170 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10171 if (mBlurSurface != null) {
10172 mBlurSurface.setPosition(0, 0);
10173 mBlurSurface.setSize(dw, dh);
10174 try {
10175 mBlurSurface.show();
10176 } catch (RuntimeException e) {
10177 Log.w(TAG, "Failure showing blur surface", e);
10178 }
10179 }
10180 }
10181 mBlurSurface.setLayer(w.mAnimLayer-2);
10182 }
10183 }
10184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010185
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010186 if (obscuredChanged && mWallpaperTarget == w) {
10187 // This is the wallpaper target and its obscured state
10188 // changed... make sure the current wallaper's visibility
10189 // has been updated accordingly.
10190 updateWallpaperVisibilityLocked();
10191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010193
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010194 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10195 mBackgroundFillerShown = false;
10196 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10197 try {
10198 mBackgroundFillerSurface.hide();
10199 } catch (RuntimeException e) {
10200 Log.e(TAG, "Exception hiding filler surface", e);
10201 }
10202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010203
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010204 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10205 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010206 }
Romain Guy06882f82009-06-10 13:36:04 -070010207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010208 if (!blurring && mBlurShown) {
10209 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10210 + ": HIDE");
10211 try {
10212 mBlurSurface.hide();
10213 } catch (IllegalArgumentException e) {
10214 Log.w(TAG, "Illegal argument exception hiding blur surface");
10215 }
10216 mBlurShown = false;
10217 }
10218
10219 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10220 } catch (RuntimeException e) {
10221 Log.e(TAG, "Unhandled exception in Window Manager", e);
10222 }
10223
10224 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010226 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10227 "With display frozen, orientationChangeComplete="
10228 + orientationChangeComplete);
10229 if (orientationChangeComplete) {
10230 if (mWindowsFreezingScreen) {
10231 mWindowsFreezingScreen = false;
10232 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10233 }
10234 if (mAppsFreezingScreen == 0) {
10235 stopFreezingDisplayLocked();
10236 }
10237 }
Romain Guy06882f82009-06-10 13:36:04 -070010238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010239 i = mResizingWindows.size();
10240 if (i > 0) {
10241 do {
10242 i--;
10243 WindowState win = mResizingWindows.get(i);
10244 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010245 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10246 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010247 win.mClient.resized(win.mFrame.width(),
10248 win.mFrame.height(), win.mLastContentInsets,
10249 win.mLastVisibleInsets, win.mDrawPending);
10250 win.mContentInsetsChanged = false;
10251 win.mVisibleInsetsChanged = false;
10252 } catch (RemoteException e) {
10253 win.mOrientationChanging = false;
10254 }
10255 } while (i > 0);
10256 mResizingWindows.clear();
10257 }
Romain Guy06882f82009-06-10 13:36:04 -070010258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010259 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010260 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010261 i = mDestroySurface.size();
10262 if (i > 0) {
10263 do {
10264 i--;
10265 WindowState win = mDestroySurface.get(i);
10266 win.mDestroying = false;
10267 if (mInputMethodWindow == win) {
10268 mInputMethodWindow = null;
10269 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010270 if (win == mWallpaperTarget) {
10271 wallpaperDestroyed = true;
10272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010273 win.destroySurfaceLocked();
10274 } while (i > 0);
10275 mDestroySurface.clear();
10276 }
10277
10278 // Time to remove any exiting tokens?
10279 for (i=mExitingTokens.size()-1; i>=0; i--) {
10280 WindowToken token = mExitingTokens.get(i);
10281 if (!token.hasVisible) {
10282 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010283 if (token.windowType == TYPE_WALLPAPER) {
10284 mWallpaperTokens.remove(token);
10285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010286 }
10287 }
10288
10289 // Time to remove any exiting applications?
10290 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10291 AppWindowToken token = mExitingAppTokens.get(i);
10292 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010293 // Make sure there is no animation running on this token,
10294 // so any windows associated with it will be removed as
10295 // soon as their animations are complete
10296 token.animation = null;
10297 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 mAppTokens.remove(token);
10299 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010300 if (mLastEnterAnimToken == token) {
10301 mLastEnterAnimToken = null;
10302 mLastEnterAnimParams = null;
10303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010304 }
10305 }
10306
Dianne Hackborna8f60182009-09-01 19:01:50 -070010307 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010308
Dianne Hackborna8f60182009-09-01 19:01:50 -070010309 if (!animating && mAppTransitionRunning) {
10310 // We have finished the animation of an app transition. To do
10311 // this, we have delayed a lot of operations like showing and
10312 // hiding apps, moving apps in Z-order, etc. The app token list
10313 // reflects the correct Z-order, but the window list may now
10314 // be out of sync with it. So here we will just rebuild the
10315 // entire app window list. Fun!
10316 mAppTransitionRunning = false;
10317 needRelayout = true;
10318 rebuildAppWindowListLocked();
10319 // Clear information about apps that were moving.
10320 mToBottomApps.clear();
10321 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 if (focusDisplayed) {
10324 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10325 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010326 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010327 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010328 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010329 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010330 requestAnimationLocked(0);
10331 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010332 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10333 }
10334 mQueue.setHoldScreenLocked(holdScreen != null);
10335 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10336 mPowerManager.setScreenBrightnessOverride(-1);
10337 } else {
10338 mPowerManager.setScreenBrightnessOverride((int)
10339 (screenBrightness * Power.BRIGHTNESS_ON));
10340 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010341 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10342 mPowerManager.setButtonBrightnessOverride(-1);
10343 } else {
10344 mPowerManager.setButtonBrightnessOverride((int)
10345 (buttonBrightness * Power.BRIGHTNESS_ON));
10346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010347 if (holdScreen != mHoldingScreenOn) {
10348 mHoldingScreenOn = holdScreen;
10349 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10350 mH.sendMessage(m);
10351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010352
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010353 if (mTurnOnScreen) {
10354 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10355 LocalPowerManager.BUTTON_EVENT, true);
10356 mTurnOnScreen = false;
10357 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010358
10359 // Check to see if we are now in a state where the screen should
10360 // be enabled, because the window obscured flags have changed.
10361 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010362 }
10363
10364 void requestAnimationLocked(long delay) {
10365 if (!mAnimationPending) {
10366 mAnimationPending = true;
10367 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10368 }
10369 }
Romain Guy06882f82009-06-10 13:36:04 -070010370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010371 /**
10372 * Have the surface flinger show a surface, robustly dealing with
10373 * error conditions. In particular, if there is not enough memory
10374 * to show the surface, then we will try to get rid of other surfaces
10375 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010376 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 * @return Returns true if the surface was successfully shown.
10378 */
10379 boolean showSurfaceRobustlyLocked(WindowState win) {
10380 try {
10381 if (win.mSurface != null) {
10382 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010383 if (win.mTurnOnScreen) {
10384 win.mTurnOnScreen = false;
10385 mTurnOnScreen = true;
10386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010387 }
10388 return true;
10389 } catch (RuntimeException e) {
10390 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10391 }
Romain Guy06882f82009-06-10 13:36:04 -070010392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010393 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395 return false;
10396 }
Romain Guy06882f82009-06-10 13:36:04 -070010397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010398 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10399 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010400
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010401 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010402 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010404 if (mForceRemoves == null) {
10405 mForceRemoves = new ArrayList<WindowState>();
10406 }
Romain Guy06882f82009-06-10 13:36:04 -070010407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010408 long callingIdentity = Binder.clearCallingIdentity();
10409 try {
10410 // There was some problem... first, do a sanity check of the
10411 // window list to make sure we haven't left any dangling surfaces
10412 // around.
10413 int N = mWindows.size();
10414 boolean leakedSurface = false;
10415 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10416 for (int i=0; i<N; i++) {
10417 WindowState ws = (WindowState)mWindows.get(i);
10418 if (ws.mSurface != null) {
10419 if (!mSessions.contains(ws.mSession)) {
10420 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10421 + ws + " surface=" + ws.mSurface
10422 + " token=" + win.mToken
10423 + " pid=" + ws.mSession.mPid
10424 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010425 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 ws.mSurface = null;
10427 mForceRemoves.add(ws);
10428 i--;
10429 N--;
10430 leakedSurface = true;
10431 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10432 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10433 + ws + " surface=" + ws.mSurface
10434 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010435 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010436 ws.mSurface = null;
10437 leakedSurface = true;
10438 }
10439 }
10440 }
Romain Guy06882f82009-06-10 13:36:04 -070010441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 boolean killedApps = false;
10443 if (!leakedSurface) {
10444 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10445 SparseIntArray pidCandidates = new SparseIntArray();
10446 for (int i=0; i<N; i++) {
10447 WindowState ws = (WindowState)mWindows.get(i);
10448 if (ws.mSurface != null) {
10449 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10450 }
10451 }
10452 if (pidCandidates.size() > 0) {
10453 int[] pids = new int[pidCandidates.size()];
10454 for (int i=0; i<pids.length; i++) {
10455 pids[i] = pidCandidates.keyAt(i);
10456 }
10457 try {
10458 if (mActivityManager.killPidsForMemory(pids)) {
10459 killedApps = true;
10460 }
10461 } catch (RemoteException e) {
10462 }
10463 }
10464 }
Romain Guy06882f82009-06-10 13:36:04 -070010465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010466 if (leakedSurface || killedApps) {
10467 // We managed to reclaim some memory, so get rid of the trouble
10468 // surface and ask the app to request another one.
10469 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10470 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010471 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010472 win.mSurface = null;
10473 }
Romain Guy06882f82009-06-10 13:36:04 -070010474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475 try {
10476 win.mClient.dispatchGetNewSurface();
10477 } catch (RemoteException e) {
10478 }
10479 }
10480 } finally {
10481 Binder.restoreCallingIdentity(callingIdentity);
10482 }
10483 }
Romain Guy06882f82009-06-10 13:36:04 -070010484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010485 private boolean updateFocusedWindowLocked(int mode) {
10486 WindowState newFocus = computeFocusedWindowLocked();
10487 if (mCurrentFocus != newFocus) {
10488 // This check makes sure that we don't already have the focus
10489 // change message pending.
10490 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10491 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10492 if (localLOGV) Log.v(
10493 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10494 final WindowState oldFocus = mCurrentFocus;
10495 mCurrentFocus = newFocus;
10496 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 final WindowState imWindow = mInputMethodWindow;
10499 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010500 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010502 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10503 mLayoutNeeded = true;
10504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010505 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10506 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010507 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10508 // Client will do the layout, but we need to assign layers
10509 // for handleNewWindowLocked() below.
10510 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 }
10512 }
Romain Guy06882f82009-06-10 13:36:04 -070010513
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010514 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10515 mKeyWaiter.handleNewWindowLocked(newFocus);
10516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 return true;
10518 }
10519 return false;
10520 }
10521
10522 private WindowState computeFocusedWindowLocked() {
10523 WindowState result = null;
10524 WindowState win;
10525
10526 int i = mWindows.size() - 1;
10527 int nextAppIndex = mAppTokens.size()-1;
10528 WindowToken nextApp = nextAppIndex >= 0
10529 ? mAppTokens.get(nextAppIndex) : null;
10530
10531 while (i >= 0) {
10532 win = (WindowState)mWindows.get(i);
10533
10534 if (localLOGV || DEBUG_FOCUS) Log.v(
10535 TAG, "Looking for focus: " + i
10536 + " = " + win
10537 + ", flags=" + win.mAttrs.flags
10538 + ", canReceive=" + win.canReceiveKeys());
10539
10540 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010542 // If this window's application has been removed, just skip it.
10543 if (thisApp != null && thisApp.removed) {
10544 i--;
10545 continue;
10546 }
Romain Guy06882f82009-06-10 13:36:04 -070010547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010548 // If there is a focused app, don't allow focus to go to any
10549 // windows below it. If this is an application window, step
10550 // through the app tokens until we find its app.
10551 if (thisApp != null && nextApp != null && thisApp != nextApp
10552 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10553 int origAppIndex = nextAppIndex;
10554 while (nextAppIndex > 0) {
10555 if (nextApp == mFocusedApp) {
10556 // Whoops, we are below the focused app... no focus
10557 // for you!
10558 if (localLOGV || DEBUG_FOCUS) Log.v(
10559 TAG, "Reached focused app: " + mFocusedApp);
10560 return null;
10561 }
10562 nextAppIndex--;
10563 nextApp = mAppTokens.get(nextAppIndex);
10564 if (nextApp == thisApp) {
10565 break;
10566 }
10567 }
10568 if (thisApp != nextApp) {
10569 // Uh oh, the app token doesn't exist! This shouldn't
10570 // happen, but if it does we can get totally hosed...
10571 // so restart at the original app.
10572 nextAppIndex = origAppIndex;
10573 nextApp = mAppTokens.get(nextAppIndex);
10574 }
10575 }
10576
10577 // Dispatch to this window if it is wants key events.
10578 if (win.canReceiveKeys()) {
10579 if (DEBUG_FOCUS) Log.v(
10580 TAG, "Found focus @ " + i + " = " + win);
10581 result = win;
10582 break;
10583 }
10584
10585 i--;
10586 }
10587
10588 return result;
10589 }
10590
10591 private void startFreezingDisplayLocked() {
10592 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010593 // Freezing the display also suspends key event delivery, to
10594 // keep events from going astray while the display is reconfigured.
10595 // If someone has changed orientation again while the screen is
10596 // still frozen, the events will continue to be blocked while the
10597 // successive orientation change is processed. To prevent spurious
10598 // ANRs, we reset the event dispatch timeout in this case.
10599 synchronized (mKeyWaiter) {
10600 mKeyWaiter.mWasFrozen = true;
10601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010602 return;
10603 }
Romain Guy06882f82009-06-10 13:36:04 -070010604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010605 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 long now = SystemClock.uptimeMillis();
10608 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10609 if (mFreezeGcPending != 0) {
10610 if (now > (mFreezeGcPending+1000)) {
10611 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10612 mH.removeMessages(H.FORCE_GC);
10613 Runtime.getRuntime().gc();
10614 mFreezeGcPending = now;
10615 }
10616 } else {
10617 mFreezeGcPending = now;
10618 }
Romain Guy06882f82009-06-10 13:36:04 -070010619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010621 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10622 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010623 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 mAppTransitionReady = true;
10625 }
Romain Guy06882f82009-06-10 13:36:04 -070010626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010627 if (PROFILE_ORIENTATION) {
10628 File file = new File("/data/system/frozen");
10629 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10630 }
10631 Surface.freezeDisplay(0);
10632 }
Romain Guy06882f82009-06-10 13:36:04 -070010633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010634 private void stopFreezingDisplayLocked() {
10635 if (!mDisplayFrozen) {
10636 return;
10637 }
Romain Guy06882f82009-06-10 13:36:04 -070010638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 mDisplayFrozen = false;
10640 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10641 if (PROFILE_ORIENTATION) {
10642 Debug.stopMethodTracing();
10643 }
10644 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010645
Chris Tate2ad63a92009-03-25 17:36:48 -070010646 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10647 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648 synchronized (mKeyWaiter) {
10649 mKeyWaiter.mWasFrozen = true;
10650 mKeyWaiter.notifyAll();
10651 }
10652
10653 // A little kludge: a lot could have happened while the
10654 // display was frozen, so now that we are coming back we
10655 // do a gc so that any remote references the system
10656 // processes holds on others can be released if they are
10657 // no longer needed.
10658 mH.removeMessages(H.FORCE_GC);
10659 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10660 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010662 mScreenFrozenLock.release();
10663 }
Romain Guy06882f82009-06-10 13:36:04 -070010664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 @Override
10666 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10667 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10668 != PackageManager.PERMISSION_GRANTED) {
10669 pw.println("Permission Denial: can't dump WindowManager from from pid="
10670 + Binder.getCallingPid()
10671 + ", uid=" + Binder.getCallingUid());
10672 return;
10673 }
Romain Guy06882f82009-06-10 13:36:04 -070010674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675 synchronized(mWindowMap) {
10676 pw.println("Current Window Manager state:");
10677 for (int i=mWindows.size()-1; i>=0; i--) {
10678 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010679 pw.print(" Window #"); pw.print(i); pw.print(' ');
10680 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010681 w.dump(pw, " ");
10682 }
10683 if (mInputMethodDialogs.size() > 0) {
10684 pw.println(" ");
10685 pw.println(" Input method dialogs:");
10686 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10687 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010688 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 }
10690 }
10691 if (mPendingRemove.size() > 0) {
10692 pw.println(" ");
10693 pw.println(" Remove pending for:");
10694 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10695 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010696 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10697 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698 w.dump(pw, " ");
10699 }
10700 }
10701 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10702 pw.println(" ");
10703 pw.println(" Windows force removing:");
10704 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10705 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010706 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10707 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 w.dump(pw, " ");
10709 }
10710 }
10711 if (mDestroySurface.size() > 0) {
10712 pw.println(" ");
10713 pw.println(" Windows waiting to destroy their surface:");
10714 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10715 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010716 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10717 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010718 w.dump(pw, " ");
10719 }
10720 }
10721 if (mLosingFocus.size() > 0) {
10722 pw.println(" ");
10723 pw.println(" Windows losing focus:");
10724 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10725 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010726 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10727 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010728 w.dump(pw, " ");
10729 }
10730 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010731 if (mResizingWindows.size() > 0) {
10732 pw.println(" ");
10733 pw.println(" Windows waiting to resize:");
10734 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10735 WindowState w = mResizingWindows.get(i);
10736 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10737 pw.print(w); pw.println(":");
10738 w.dump(pw, " ");
10739 }
10740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010741 if (mSessions.size() > 0) {
10742 pw.println(" ");
10743 pw.println(" All active sessions:");
10744 Iterator<Session> it = mSessions.iterator();
10745 while (it.hasNext()) {
10746 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010747 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010748 s.dump(pw, " ");
10749 }
10750 }
10751 if (mTokenMap.size() > 0) {
10752 pw.println(" ");
10753 pw.println(" All tokens:");
10754 Iterator<WindowToken> it = mTokenMap.values().iterator();
10755 while (it.hasNext()) {
10756 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010757 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010758 token.dump(pw, " ");
10759 }
10760 }
10761 if (mTokenList.size() > 0) {
10762 pw.println(" ");
10763 pw.println(" Window token list:");
10764 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010765 pw.print(" #"); pw.print(i); pw.print(": ");
10766 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010767 }
10768 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010769 if (mWallpaperTokens.size() > 0) {
10770 pw.println(" ");
10771 pw.println(" Wallpaper tokens:");
10772 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10773 WindowToken token = mWallpaperTokens.get(i);
10774 pw.print(" Wallpaper #"); pw.print(i);
10775 pw.print(' '); pw.print(token); pw.println(':');
10776 token.dump(pw, " ");
10777 }
10778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010779 if (mAppTokens.size() > 0) {
10780 pw.println(" ");
10781 pw.println(" Application tokens in Z order:");
10782 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010783 pw.print(" App #"); pw.print(i); pw.print(": ");
10784 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010785 }
10786 }
10787 if (mFinishedStarting.size() > 0) {
10788 pw.println(" ");
10789 pw.println(" Finishing start of application tokens:");
10790 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10791 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010792 pw.print(" Finished Starting #"); pw.print(i);
10793 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 token.dump(pw, " ");
10795 }
10796 }
10797 if (mExitingTokens.size() > 0) {
10798 pw.println(" ");
10799 pw.println(" Exiting tokens:");
10800 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10801 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010802 pw.print(" Exiting #"); pw.print(i);
10803 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010804 token.dump(pw, " ");
10805 }
10806 }
10807 if (mExitingAppTokens.size() > 0) {
10808 pw.println(" ");
10809 pw.println(" Exiting application tokens:");
10810 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10811 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010812 pw.print(" Exiting App #"); pw.print(i);
10813 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010814 token.dump(pw, " ");
10815 }
10816 }
10817 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010818 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10819 pw.print(" mLastFocus="); pw.println(mLastFocus);
10820 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10821 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10822 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010823 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010824 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10825 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10826 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10827 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010828 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10829 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10830 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10831 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10832 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010833 if (mDimAnimator != null) {
10834 mDimAnimator.printTo(pw);
10835 } else {
10836 pw.print( " no DimAnimator ");
10837 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010838 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010839 pw.print(mInputMethodAnimLayerAdjustment);
10840 pw.print(" mWallpaperAnimLayerAdjustment=");
10841 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010842 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10843 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010844 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10845 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10846 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10847 pw.print(" mRotation="); pw.print(mRotation);
10848 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10849 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10850 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10851 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10852 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10853 pw.print(" mNextAppTransition=0x");
10854 pw.print(Integer.toHexString(mNextAppTransition));
10855 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010856 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010857 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010858 if (mNextAppTransitionPackage != null) {
10859 pw.print(" mNextAppTransitionPackage=");
10860 pw.print(mNextAppTransitionPackage);
10861 pw.print(", mNextAppTransitionEnter=0x");
10862 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10863 pw.print(", mNextAppTransitionExit=0x");
10864 pw.print(Integer.toHexString(mNextAppTransitionExit));
10865 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010866 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10867 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010868 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10869 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10870 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10871 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010872 if (mOpeningApps.size() > 0) {
10873 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10874 }
10875 if (mClosingApps.size() > 0) {
10876 pw.print(" mClosingApps="); pw.println(mClosingApps);
10877 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010878 if (mToTopApps.size() > 0) {
10879 pw.print(" mToTopApps="); pw.println(mToTopApps);
10880 }
10881 if (mToBottomApps.size() > 0) {
10882 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10883 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010884 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10885 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010886 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010887 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10888 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10889 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10890 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10891 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10892 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010893 }
10894 }
10895
10896 public void monitor() {
10897 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050010898 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010899 synchronized (mKeyWaiter) { }
10900 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010901
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010902 public void virtualKeyFeedback(KeyEvent event) {
10903 mPolicy.keyFeedbackFromInput(event);
10904 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010905
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010906 /**
10907 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010908 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010909 */
10910 private static class DimAnimator {
10911 Surface mDimSurface;
10912 boolean mDimShown = false;
10913 float mDimCurrentAlpha;
10914 float mDimTargetAlpha;
10915 float mDimDeltaPerMs;
10916 long mLastDimAnimTime;
10917
10918 DimAnimator (SurfaceSession session) {
10919 if (mDimSurface == null) {
10920 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10921 + mDimSurface + ": CREATE");
10922 try {
10923 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10924 Surface.FX_SURFACE_DIM);
10925 } catch (Exception e) {
10926 Log.e(TAG, "Exception creating Dim surface", e);
10927 }
10928 }
10929 }
10930
10931 /**
10932 * Show the dim surface.
10933 */
10934 void show(int dw, int dh) {
10935 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10936 dw + "x" + dh + ")");
10937 mDimShown = true;
10938 try {
10939 mDimSurface.setPosition(0, 0);
10940 mDimSurface.setSize(dw, dh);
10941 mDimSurface.show();
10942 } catch (RuntimeException e) {
10943 Log.w(TAG, "Failure showing dim surface", e);
10944 }
10945 }
10946
10947 /**
10948 * Set's the dim surface's layer and update dim parameters that will be used in
10949 * {@link updateSurface} after all windows are examined.
10950 */
10951 void updateParameters(WindowState w, long currentTime) {
10952 mDimSurface.setLayer(w.mAnimLayer-1);
10953
10954 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010955 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10956 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010957 if (mDimTargetAlpha != target) {
10958 // If the desired dim level has changed, then
10959 // start an animation to it.
10960 mLastDimAnimTime = currentTime;
10961 long duration = (w.mAnimating && w.mAnimation != null)
10962 ? w.mAnimation.computeDurationHint()
10963 : DEFAULT_DIM_DURATION;
10964 if (target > mDimTargetAlpha) {
10965 // This is happening behind the activity UI,
10966 // so we can make it run a little longer to
10967 // give a stronger impression without disrupting
10968 // the user.
10969 duration *= DIM_DURATION_MULTIPLIER;
10970 }
10971 if (duration < 1) {
10972 // Don't divide by zero
10973 duration = 1;
10974 }
10975 mDimTargetAlpha = target;
10976 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10977 }
10978 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010979
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010980 /**
10981 * Updating the surface's alpha. Returns true if the animation continues, or returns
10982 * false when the animation is finished and the dim surface is hidden.
10983 */
10984 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10985 if (!dimming) {
10986 if (mDimTargetAlpha != 0) {
10987 mLastDimAnimTime = currentTime;
10988 mDimTargetAlpha = 0;
10989 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10990 }
10991 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010992
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010993 boolean animating = false;
10994 if (mLastDimAnimTime != 0) {
10995 mDimCurrentAlpha += mDimDeltaPerMs
10996 * (currentTime-mLastDimAnimTime);
10997 boolean more = true;
10998 if (displayFrozen) {
10999 // If the display is frozen, there is no reason to animate.
11000 more = false;
11001 } else if (mDimDeltaPerMs > 0) {
11002 if (mDimCurrentAlpha > mDimTargetAlpha) {
11003 more = false;
11004 }
11005 } else if (mDimDeltaPerMs < 0) {
11006 if (mDimCurrentAlpha < mDimTargetAlpha) {
11007 more = false;
11008 }
11009 } else {
11010 more = false;
11011 }
11012
11013 // Do we need to continue animating?
11014 if (more) {
11015 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11016 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11017 mLastDimAnimTime = currentTime;
11018 mDimSurface.setAlpha(mDimCurrentAlpha);
11019 animating = true;
11020 } else {
11021 mDimCurrentAlpha = mDimTargetAlpha;
11022 mLastDimAnimTime = 0;
11023 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11024 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11025 mDimSurface.setAlpha(mDimCurrentAlpha);
11026 if (!dimming) {
11027 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11028 + ": HIDE");
11029 try {
11030 mDimSurface.hide();
11031 } catch (RuntimeException e) {
11032 Log.w(TAG, "Illegal argument exception hiding dim surface");
11033 }
11034 mDimShown = false;
11035 }
11036 }
11037 }
11038 return animating;
11039 }
11040
11041 public void printTo(PrintWriter pw) {
11042 pw.print(" mDimShown="); pw.print(mDimShown);
11043 pw.print(" current="); pw.print(mDimCurrentAlpha);
11044 pw.print(" target="); pw.print(mDimTargetAlpha);
11045 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11046 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11047 }
11048 }
11049
11050 /**
11051 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11052 * This is used for opening/closing transition for apps in compatible mode.
11053 */
11054 private static class FadeInOutAnimation extends Animation {
11055 int mWidth;
11056 boolean mFadeIn;
11057
11058 public FadeInOutAnimation(boolean fadeIn) {
11059 setInterpolator(new AccelerateInterpolator());
11060 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11061 mFadeIn = fadeIn;
11062 }
11063
11064 @Override
11065 protected void applyTransformation(float interpolatedTime, Transformation t) {
11066 float x = interpolatedTime;
11067 if (!mFadeIn) {
11068 x = 1.0f - x; // reverse the interpolation for fade out
11069 }
11070 if (x < 0.5) {
11071 // move the window out of the screen.
11072 t.getMatrix().setTranslate(mWidth, 0);
11073 } else {
11074 t.getMatrix().setTranslate(0, 0);// show
11075 t.setAlpha((x - 0.5f) * 2);
11076 }
11077 }
11078
11079 @Override
11080 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11081 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11082 mWidth = width;
11083 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011084
11085 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011086 public int getZAdjustment() {
11087 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011088 }
11089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011090}