blob: df011a215b3b5e3b13615ebb2a5a1dde672f436c [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 Hackborn7433e8a2009-09-27 13:21:20 -07004302 if (w.isVisibleLw() && !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 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006012
Dianne Hackborn6adba242009-11-10 11:10:09 -08006013 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6014 synchronized (mWindowMap) {
6015 if (mWallpaperTarget == win || mSendingPointersToWallpaper) {
6016 sendPointerToWallpaperLocked(win, res, res.getEventTime());
6017 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006018 }
6019 }
6020 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006021
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006022 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 }
6024
6025 void tickle() {
6026 synchronized (this) {
6027 notifyAll();
6028 }
6029 }
Romain Guy06882f82009-06-10 13:36:04 -07006030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 void handleNewWindowLocked(WindowState newWindow) {
6032 if (!newWindow.canReceiveKeys()) {
6033 return;
6034 }
6035 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006036 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 TAG, "New key dispatch window: win="
6038 + newWindow.mClient.asBinder()
6039 + ", last=" + mLastBinder
6040 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6041 + "), finished=" + mFinished + ", paused="
6042 + newWindow.mToken.paused);
6043
6044 // Displaying a window implicitly causes dispatching to
6045 // be unpaused. (This is to protect against bugs if someone
6046 // pauses dispatching but forgets to resume.)
6047 newWindow.mToken.paused = false;
6048
6049 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050
6051 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
6052 if (DEBUG_INPUT) Log.v(TAG,
6053 "New SYSTEM_ERROR window; resetting state");
6054 mLastWin = null;
6055 mLastBinder = null;
6056 mMotionTarget = null;
6057 mFinished = true;
6058 } else if (mLastWin != null) {
6059 // If the new window is above the window we are
6060 // waiting on, then stop waiting and let key dispatching
6061 // start on the new guy.
6062 if (DEBUG_INPUT) Log.v(
6063 TAG, "Last win layer=" + mLastWin.mLayer
6064 + ", new win layer=" + newWindow.mLayer);
6065 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006066 // The new window is above the old; finish pending input to the last
6067 // window and start directing it to the new one.
6068 mLastWin.mToken.paused = false;
6069 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006071 // Either the new window is lower, so there is no need to wake key waiters,
6072 // or we just finished key input to the previous window, which implicitly
6073 // notified the key waiters. In both cases, we don't need to issue the
6074 // notification here.
6075 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 }
6077
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006078 // Now that we've put a new window state in place, make the event waiter
6079 // take notice and retarget its attentions.
6080 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006081 }
6082 }
6083
6084 void pauseDispatchingLocked(WindowToken token) {
6085 synchronized (this)
6086 {
6087 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
6088 token.paused = true;
6089
6090 /*
6091 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6092 mPaused = true;
6093 } else {
6094 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07006095 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006096 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07006097 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07006099 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 }
6101 }
6102 */
6103 }
6104 }
6105
6106 void resumeDispatchingLocked(WindowToken token) {
6107 synchronized (this) {
6108 if (token.paused) {
6109 if (DEBUG_INPUT) Log.v(
6110 TAG, "Resuming WindowToken " + token
6111 + ", last=" + mLastBinder
6112 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6113 + "), finished=" + mFinished + ", paused="
6114 + token.paused);
6115 token.paused = false;
6116 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
6117 doFinishedKeyLocked(true);
6118 } else {
6119 notifyAll();
6120 }
6121 }
6122 }
6123 }
6124
6125 void setEventDispatchingLocked(boolean enabled) {
6126 synchronized (this) {
6127 mEventDispatching = enabled;
6128 notifyAll();
6129 }
6130 }
Romain Guy06882f82009-06-10 13:36:04 -07006131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 void appSwitchComing() {
6133 synchronized (this) {
6134 // Don't wait for more than .5 seconds for app to finish
6135 // processing the pending events.
6136 long now = SystemClock.uptimeMillis() + 500;
6137 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6138 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6139 mTimeToSwitch = now;
6140 }
6141 notifyAll();
6142 }
6143 }
Romain Guy06882f82009-06-10 13:36:04 -07006144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 private final void doFinishedKeyLocked(boolean doRecycle) {
6146 if (mLastWin != null) {
6147 releasePendingPointerLocked(mLastWin.mSession);
6148 releasePendingTrackballLocked(mLastWin.mSession);
6149 }
Romain Guy06882f82009-06-10 13:36:04 -07006150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 if (mLastWin == null || !mLastWin.mToken.paused
6152 || !mLastWin.isVisibleLw()) {
6153 // If the current window has been paused, we aren't -really-
6154 // finished... so let the waiters still wait.
6155 mLastWin = null;
6156 mLastBinder = null;
6157 }
6158 mFinished = true;
6159 notifyAll();
6160 }
6161 }
6162
6163 private class KeyQ extends KeyInputQueue
6164 implements KeyInputQueue.FilterCallback {
6165 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006167 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006168 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6170 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6171 "KEEP_SCREEN_ON_FLAG");
6172 mHoldingScreen.setReferenceCounted(false);
6173 }
6174
6175 @Override
6176 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6177 if (mPolicy.preprocessInputEventTq(event)) {
6178 return true;
6179 }
Romain Guy06882f82009-06-10 13:36:04 -07006180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006181 switch (event.type) {
6182 case RawInputEvent.EV_KEY: {
6183 // XXX begin hack
6184 if (DEBUG) {
6185 if (event.keycode == KeyEvent.KEYCODE_G) {
6186 if (event.value != 0) {
6187 // G down
6188 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6189 }
6190 return false;
6191 }
6192 if (event.keycode == KeyEvent.KEYCODE_D) {
6193 if (event.value != 0) {
6194 //dump();
6195 }
6196 return false;
6197 }
6198 }
6199 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006200
Charles Mendis322591c2009-10-29 11:06:59 -07006201 boolean screenIsOff = !mPowerManager.isScreenOn();
6202 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6206 mPowerManager.goToSleep(event.when);
6207 }
6208
6209 if (screenIsOff) {
6210 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6211 }
6212 if (screenIsDim) {
6213 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6214 }
6215 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6216 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006217 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218 }
Romain Guy06882f82009-06-10 13:36:04 -07006219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006220 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6221 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6222 filterQueue(this);
6223 mKeyWaiter.appSwitchComing();
6224 }
6225 return true;
6226 } else {
6227 return false;
6228 }
6229 }
Romain Guy06882f82009-06-10 13:36:04 -07006230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006232 boolean screenIsOff = !mPowerManager.isScreenOn();
6233 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 if (screenIsOff) {
6235 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6236 device.classes, event)) {
6237 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6238 return false;
6239 }
6240 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6241 }
6242 if (screenIsDim) {
6243 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6244 }
6245 return true;
6246 }
Romain Guy06882f82009-06-10 13:36:04 -07006247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006249 boolean screenIsOff = !mPowerManager.isScreenOn();
6250 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 if (screenIsOff) {
6252 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6253 device.classes, event)) {
6254 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6255 return false;
6256 }
6257 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6258 }
6259 if (screenIsDim) {
6260 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6261 }
6262 return true;
6263 }
Romain Guy06882f82009-06-10 13:36:04 -07006264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006265 default:
6266 return true;
6267 }
6268 }
6269
6270 public int filterEvent(QueuedEvent ev) {
6271 switch (ev.classType) {
6272 case RawInputEvent.CLASS_KEYBOARD:
6273 KeyEvent ke = (KeyEvent)ev.event;
6274 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6275 Log.w(TAG, "Dropping movement key during app switch: "
6276 + ke.getKeyCode() + ", action=" + ke.getAction());
6277 return FILTER_REMOVE;
6278 }
6279 return FILTER_ABORT;
6280 default:
6281 return FILTER_KEEP;
6282 }
6283 }
Romain Guy06882f82009-06-10 13:36:04 -07006284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 /**
6286 * Must be called with the main window manager lock held.
6287 */
6288 void setHoldScreenLocked(boolean holding) {
6289 boolean state = mHoldingScreen.isHeld();
6290 if (holding != state) {
6291 if (holding) {
6292 mHoldingScreen.acquire();
6293 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006294 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006295 mHoldingScreen.release();
6296 }
6297 }
6298 }
Michael Chan53071d62009-05-13 17:29:48 -07006299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006300
6301 public boolean detectSafeMode() {
6302 mSafeMode = mPolicy.detectSafeMode();
6303 return mSafeMode;
6304 }
Romain Guy06882f82009-06-10 13:36:04 -07006305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006306 public void systemReady() {
6307 mPolicy.systemReady();
6308 }
Romain Guy06882f82009-06-10 13:36:04 -07006309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006310 private final class InputDispatcherThread extends Thread {
6311 // Time to wait when there is nothing to do: 9999 seconds.
6312 static final int LONG_WAIT=9999*1000;
6313
6314 public InputDispatcherThread() {
6315 super("InputDispatcher");
6316 }
Romain Guy06882f82009-06-10 13:36:04 -07006317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006318 @Override
6319 public void run() {
6320 while (true) {
6321 try {
6322 process();
6323 } catch (Exception e) {
6324 Log.e(TAG, "Exception in input dispatcher", e);
6325 }
6326 }
6327 }
Romain Guy06882f82009-06-10 13:36:04 -07006328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 private void process() {
6330 android.os.Process.setThreadPriority(
6331 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 // The last key event we saw
6334 KeyEvent lastKey = null;
6335
6336 // Last keydown time for auto-repeating keys
6337 long lastKeyTime = SystemClock.uptimeMillis();
6338 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006339 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006340
Romain Guy06882f82009-06-10 13:36:04 -07006341 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342 int keyRepeatCount = 0;
6343
6344 // Need to report that configuration has changed?
6345 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 while (true) {
6348 long curTime = SystemClock.uptimeMillis();
6349
6350 if (DEBUG_INPUT) Log.v(
6351 TAG, "Waiting for next key: now=" + curTime
6352 + ", repeat @ " + nextKeyTime);
6353
6354 // Retrieve next event, waiting only as long as the next
6355 // repeat timeout. If the configuration has changed, then
6356 // don't wait at all -- we'll report the change as soon as
6357 // we have processed all events.
6358 QueuedEvent ev = mQueue.getEvent(
6359 (int)((!configChanged && curTime < nextKeyTime)
6360 ? (nextKeyTime-curTime) : 0));
6361
6362 if (DEBUG_INPUT && ev != null) Log.v(
6363 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6364
Michael Chan53071d62009-05-13 17:29:48 -07006365 if (MEASURE_LATENCY) {
6366 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6367 }
6368
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006369 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6370 // cancel key repeat at the request of the policy.
6371 lastKey = null;
6372 downTime = 0;
6373 lastKeyTime = curTime;
6374 nextKeyTime = curTime + LONG_WAIT;
6375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376 try {
6377 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006378 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006379 int eventType;
6380 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6381 eventType = eventType((MotionEvent)ev.event);
6382 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6383 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6384 eventType = LocalPowerManager.BUTTON_EVENT;
6385 } else {
6386 eventType = LocalPowerManager.OTHER_EVENT;
6387 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006388 try {
Michael Chan53071d62009-05-13 17:29:48 -07006389 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006390 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006391 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006392 mBatteryStats.noteInputEvent();
6393 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006394 } catch (RemoteException e) {
6395 // Ignore
6396 }
Michael Chane10de972009-05-18 11:24:50 -07006397
Mike Lockwood5db42402009-11-30 14:51:51 -05006398 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6399 // do not wake screen in this case
6400 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006401 && eventType != LONG_TOUCH_EVENT
6402 && eventType != CHEEK_EVENT) {
6403 mPowerManager.userActivity(curTime, false,
6404 eventType, false);
6405 } else if (mLastTouchEventType != eventType
6406 || (curTime - mLastUserActivityCallTime)
6407 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6408 mLastUserActivityCallTime = curTime;
6409 mLastTouchEventType = eventType;
6410 mPowerManager.userActivity(curTime, false,
6411 eventType, false);
6412 }
6413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 switch (ev.classType) {
6415 case RawInputEvent.CLASS_KEYBOARD:
6416 KeyEvent ke = (KeyEvent)ev.event;
6417 if (ke.isDown()) {
6418 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006419 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 keyRepeatCount = 0;
6421 lastKeyTime = curTime;
6422 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006423 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006424 if (DEBUG_INPUT) Log.v(
6425 TAG, "Received key down: first repeat @ "
6426 + nextKeyTime);
6427 } else {
6428 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006429 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 // Arbitrary long timeout.
6431 lastKeyTime = curTime;
6432 nextKeyTime = curTime + LONG_WAIT;
6433 if (DEBUG_INPUT) Log.v(
6434 TAG, "Received key up: ignore repeat @ "
6435 + nextKeyTime);
6436 }
6437 dispatchKey((KeyEvent)ev.event, 0, 0);
6438 mQueue.recycleEvent(ev);
6439 break;
6440 case RawInputEvent.CLASS_TOUCHSCREEN:
6441 //Log.i(TAG, "Read next event " + ev);
6442 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6443 break;
6444 case RawInputEvent.CLASS_TRACKBALL:
6445 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6446 break;
6447 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6448 configChanged = true;
6449 break;
6450 default:
6451 mQueue.recycleEvent(ev);
6452 break;
6453 }
Romain Guy06882f82009-06-10 13:36:04 -07006454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 } else if (configChanged) {
6456 configChanged = false;
6457 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 } else if (lastKey != null) {
6460 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 // Timeout occurred while key was down. If it is at or
6463 // past the key repeat time, dispatch the repeat.
6464 if (DEBUG_INPUT) Log.v(
6465 TAG, "Key timeout: repeat=" + nextKeyTime
6466 + ", now=" + curTime);
6467 if (curTime < nextKeyTime) {
6468 continue;
6469 }
Romain Guy06882f82009-06-10 13:36:04 -07006470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006471 lastKeyTime = nextKeyTime;
6472 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6473 keyRepeatCount++;
6474 if (DEBUG_INPUT) Log.v(
6475 TAG, "Key repeat: count=" + keyRepeatCount
6476 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006477 KeyEvent newEvent;
6478 if (downTime != 0 && (downTime
6479 + ViewConfiguration.getLongPressTimeout())
6480 <= curTime) {
6481 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6482 curTime, keyRepeatCount,
6483 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6484 downTime = 0;
6485 } else {
6486 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6487 curTime, keyRepeatCount);
6488 }
6489 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 } else {
6492 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 lastKeyTime = curTime;
6495 nextKeyTime = curTime + LONG_WAIT;
6496 }
Romain Guy06882f82009-06-10 13:36:04 -07006497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 } catch (Exception e) {
6499 Log.e(TAG,
6500 "Input thread received uncaught exception: " + e, e);
6501 }
6502 }
6503 }
6504 }
6505
6506 // -------------------------------------------------------------
6507 // Client Session State
6508 // -------------------------------------------------------------
6509
6510 private final class Session extends IWindowSession.Stub
6511 implements IBinder.DeathRecipient {
6512 final IInputMethodClient mClient;
6513 final IInputContext mInputContext;
6514 final int mUid;
6515 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006516 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006517 SurfaceSession mSurfaceSession;
6518 int mNumWindow = 0;
6519 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006521 /**
6522 * Current pointer move event being dispatched to client window... must
6523 * hold key lock to access.
6524 */
6525 QueuedEvent mPendingPointerMove;
6526 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006528 /**
6529 * Current trackball move event being dispatched to client window... must
6530 * hold key lock to access.
6531 */
6532 QueuedEvent mPendingTrackballMove;
6533 WindowState mPendingTrackballWindow;
6534
6535 public Session(IInputMethodClient client, IInputContext inputContext) {
6536 mClient = client;
6537 mInputContext = inputContext;
6538 mUid = Binder.getCallingUid();
6539 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006540 StringBuilder sb = new StringBuilder();
6541 sb.append("Session{");
6542 sb.append(Integer.toHexString(System.identityHashCode(this)));
6543 sb.append(" uid ");
6544 sb.append(mUid);
6545 sb.append("}");
6546 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006548 synchronized (mWindowMap) {
6549 if (mInputMethodManager == null && mHaveInputMethods) {
6550 IBinder b = ServiceManager.getService(
6551 Context.INPUT_METHOD_SERVICE);
6552 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6553 }
6554 }
6555 long ident = Binder.clearCallingIdentity();
6556 try {
6557 // Note: it is safe to call in to the input method manager
6558 // here because we are not holding our lock.
6559 if (mInputMethodManager != null) {
6560 mInputMethodManager.addClient(client, inputContext,
6561 mUid, mPid);
6562 } else {
6563 client.setUsingInputMethod(false);
6564 }
6565 client.asBinder().linkToDeath(this, 0);
6566 } catch (RemoteException e) {
6567 // The caller has died, so we can just forget about this.
6568 try {
6569 if (mInputMethodManager != null) {
6570 mInputMethodManager.removeClient(client);
6571 }
6572 } catch (RemoteException ee) {
6573 }
6574 } finally {
6575 Binder.restoreCallingIdentity(ident);
6576 }
6577 }
Romain Guy06882f82009-06-10 13:36:04 -07006578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 @Override
6580 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6581 throws RemoteException {
6582 try {
6583 return super.onTransact(code, data, reply, flags);
6584 } catch (RuntimeException e) {
6585 // Log all 'real' exceptions thrown to the caller
6586 if (!(e instanceof SecurityException)) {
6587 Log.e(TAG, "Window Session Crash", e);
6588 }
6589 throw e;
6590 }
6591 }
6592
6593 public void binderDied() {
6594 // Note: it is safe to call in to the input method manager
6595 // here because we are not holding our lock.
6596 try {
6597 if (mInputMethodManager != null) {
6598 mInputMethodManager.removeClient(mClient);
6599 }
6600 } catch (RemoteException e) {
6601 }
6602 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006603 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006604 mClientDead = true;
6605 killSessionLocked();
6606 }
6607 }
6608
6609 public int add(IWindow window, WindowManager.LayoutParams attrs,
6610 int viewVisibility, Rect outContentInsets) {
6611 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6612 }
Romain Guy06882f82009-06-10 13:36:04 -07006613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 public void remove(IWindow window) {
6615 removeWindow(this, window);
6616 }
Romain Guy06882f82009-06-10 13:36:04 -07006617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006618 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6619 int requestedWidth, int requestedHeight, int viewFlags,
6620 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6621 Rect outVisibleInsets, Surface outSurface) {
6622 return relayoutWindow(this, window, attrs,
6623 requestedWidth, requestedHeight, viewFlags, insetsPending,
6624 outFrame, outContentInsets, outVisibleInsets, outSurface);
6625 }
Romain Guy06882f82009-06-10 13:36:04 -07006626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006627 public void setTransparentRegion(IWindow window, Region region) {
6628 setTransparentRegionWindow(this, window, region);
6629 }
Romain Guy06882f82009-06-10 13:36:04 -07006630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006631 public void setInsets(IWindow window, int touchableInsets,
6632 Rect contentInsets, Rect visibleInsets) {
6633 setInsetsWindow(this, window, touchableInsets, contentInsets,
6634 visibleInsets);
6635 }
Romain Guy06882f82009-06-10 13:36:04 -07006636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6638 getWindowDisplayFrame(this, window, outDisplayFrame);
6639 }
Romain Guy06882f82009-06-10 13:36:04 -07006640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 public void finishDrawing(IWindow window) {
6642 if (localLOGV) Log.v(
6643 TAG, "IWindow finishDrawing called for " + window);
6644 finishDrawingWindow(this, window);
6645 }
6646
6647 public void finishKey(IWindow window) {
6648 if (localLOGV) Log.v(
6649 TAG, "IWindow finishKey called for " + window);
6650 mKeyWaiter.finishedKey(this, window, false,
6651 KeyWaiter.RETURN_NOTHING);
6652 }
6653
6654 public MotionEvent getPendingPointerMove(IWindow window) {
6655 if (localLOGV) Log.v(
6656 TAG, "IWindow getPendingMotionEvent called for " + window);
6657 return mKeyWaiter.finishedKey(this, window, false,
6658 KeyWaiter.RETURN_PENDING_POINTER);
6659 }
Romain Guy06882f82009-06-10 13:36:04 -07006660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 public MotionEvent getPendingTrackballMove(IWindow window) {
6662 if (localLOGV) Log.v(
6663 TAG, "IWindow getPendingMotionEvent called for " + window);
6664 return mKeyWaiter.finishedKey(this, window, false,
6665 KeyWaiter.RETURN_PENDING_TRACKBALL);
6666 }
6667
6668 public void setInTouchMode(boolean mode) {
6669 synchronized(mWindowMap) {
6670 mInTouchMode = mode;
6671 }
6672 }
6673
6674 public boolean getInTouchMode() {
6675 synchronized(mWindowMap) {
6676 return mInTouchMode;
6677 }
6678 }
6679
6680 public boolean performHapticFeedback(IWindow window, int effectId,
6681 boolean always) {
6682 synchronized(mWindowMap) {
6683 long ident = Binder.clearCallingIdentity();
6684 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006685 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006686 windowForClientLocked(this, window), effectId, always);
6687 } finally {
6688 Binder.restoreCallingIdentity(ident);
6689 }
6690 }
6691 }
Romain Guy06882f82009-06-10 13:36:04 -07006692
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006693 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006694 synchronized(mWindowMap) {
6695 long ident = Binder.clearCallingIdentity();
6696 try {
6697 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006698 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006699 } finally {
6700 Binder.restoreCallingIdentity(ident);
6701 }
6702 }
6703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006704
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006705 public void wallpaperOffsetsComplete(IBinder window) {
6706 WindowManagerService.this.wallpaperOffsetsComplete(window);
6707 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006708
Dianne Hackborn75804932009-10-20 20:15:20 -07006709 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6710 int z, Bundle extras, boolean sync) {
6711 synchronized(mWindowMap) {
6712 long ident = Binder.clearCallingIdentity();
6713 try {
6714 return sendWindowWallpaperCommandLocked(
6715 windowForClientLocked(this, window),
6716 action, x, y, z, extras, sync);
6717 } finally {
6718 Binder.restoreCallingIdentity(ident);
6719 }
6720 }
6721 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006722
Dianne Hackborn75804932009-10-20 20:15:20 -07006723 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6724 WindowManagerService.this.wallpaperCommandComplete(window, result);
6725 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006727 void windowAddedLocked() {
6728 if (mSurfaceSession == null) {
6729 if (localLOGV) Log.v(
6730 TAG, "First window added to " + this + ", creating SurfaceSession");
6731 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006732 if (SHOW_TRANSACTIONS) Log.i(
6733 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006734 mSessions.add(this);
6735 }
6736 mNumWindow++;
6737 }
6738
6739 void windowRemovedLocked() {
6740 mNumWindow--;
6741 killSessionLocked();
6742 }
Romain Guy06882f82009-06-10 13:36:04 -07006743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006744 void killSessionLocked() {
6745 if (mNumWindow <= 0 && mClientDead) {
6746 mSessions.remove(this);
6747 if (mSurfaceSession != null) {
6748 if (localLOGV) Log.v(
6749 TAG, "Last window removed from " + this
6750 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006751 if (SHOW_TRANSACTIONS) Log.i(
6752 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006753 try {
6754 mSurfaceSession.kill();
6755 } catch (Exception e) {
6756 Log.w(TAG, "Exception thrown when killing surface session "
6757 + mSurfaceSession + " in session " + this
6758 + ": " + e.toString());
6759 }
6760 mSurfaceSession = null;
6761 }
6762 }
6763 }
Romain Guy06882f82009-06-10 13:36:04 -07006764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006766 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6767 pw.print(" mClientDead="); pw.print(mClientDead);
6768 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6769 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6770 pw.print(prefix);
6771 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6772 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6773 }
6774 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6775 pw.print(prefix);
6776 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6777 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006779 }
6780
6781 @Override
6782 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006783 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006784 }
6785 }
6786
6787 // -------------------------------------------------------------
6788 // Client Window State
6789 // -------------------------------------------------------------
6790
6791 private final class WindowState implements WindowManagerPolicy.WindowState {
6792 final Session mSession;
6793 final IWindow mClient;
6794 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006795 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 AppWindowToken mAppToken;
6797 AppWindowToken mTargetAppToken;
6798 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6799 final DeathRecipient mDeathRecipient;
6800 final WindowState mAttachedWindow;
6801 final ArrayList mChildWindows = new ArrayList();
6802 final int mBaseLayer;
6803 final int mSubLayer;
6804 final boolean mLayoutAttached;
6805 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006806 final boolean mIsWallpaper;
6807 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006808 int mViewVisibility;
6809 boolean mPolicyVisibility = true;
6810 boolean mPolicyVisibilityAfterAnim = true;
6811 boolean mAppFreezing;
6812 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006813 boolean mReportDestroySurface;
6814 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006815 boolean mAttachedHidden; // is our parent window hidden?
6816 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006817 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 int mRequestedWidth;
6819 int mRequestedHeight;
6820 int mLastRequestedWidth;
6821 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 int mLayer;
6823 int mAnimLayer;
6824 int mLastLayer;
6825 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006826 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006827 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006828
6829 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006831 // Actual frame shown on-screen (may be modified by animation)
6832 final Rect mShownFrame = new Rect();
6833 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006835 /**
6836 * Insets that determine the actually visible area
6837 */
6838 final Rect mVisibleInsets = new Rect();
6839 final Rect mLastVisibleInsets = new Rect();
6840 boolean mVisibleInsetsChanged;
6841
6842 /**
6843 * Insets that are covered by system windows
6844 */
6845 final Rect mContentInsets = new Rect();
6846 final Rect mLastContentInsets = new Rect();
6847 boolean mContentInsetsChanged;
6848
6849 /**
6850 * Set to true if we are waiting for this window to receive its
6851 * given internal insets before laying out other windows based on it.
6852 */
6853 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855 /**
6856 * These are the content insets that were given during layout for
6857 * this window, to be applied to windows behind it.
6858 */
6859 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 /**
6862 * These are the visible insets that were given during layout for
6863 * this window, to be applied to windows behind it.
6864 */
6865 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006867 /**
6868 * Flag indicating whether the touchable region should be adjusted by
6869 * the visible insets; if false the area outside the visible insets is
6870 * NOT touchable, so we must use those to adjust the frame during hit
6871 * tests.
6872 */
6873 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 // Current transformation being applied.
6876 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6877 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6878 float mHScale=1, mVScale=1;
6879 float mLastHScale=1, mLastVScale=1;
6880 final Matrix mTmpMatrix = new Matrix();
6881
6882 // "Real" frame that the application sees.
6883 final Rect mFrame = new Rect();
6884 final Rect mLastFrame = new Rect();
6885
6886 final Rect mContainingFrame = new Rect();
6887 final Rect mDisplayFrame = new Rect();
6888 final Rect mContentFrame = new Rect();
6889 final Rect mVisibleFrame = new Rect();
6890
6891 float mShownAlpha = 1;
6892 float mAlpha = 1;
6893 float mLastAlpha = 1;
6894
6895 // Set to true if, when the window gets displayed, it should perform
6896 // an enter animation.
6897 boolean mEnterAnimationPending;
6898
6899 // Currently running animation.
6900 boolean mAnimating;
6901 boolean mLocalAnimating;
6902 Animation mAnimation;
6903 boolean mAnimationIsEntrance;
6904 boolean mHasTransformation;
6905 boolean mHasLocalTransformation;
6906 final Transformation mTransformation = new Transformation();
6907
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006908 // If a window showing a wallpaper: the requested offset for the
6909 // wallpaper; if a wallpaper window: the currently applied offset.
6910 float mWallpaperX = -1;
6911 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006912
6913 // If a window showing a wallpaper: what fraction of the offset
6914 // range corresponds to a full virtual screen.
6915 float mWallpaperXStep = -1;
6916 float mWallpaperYStep = -1;
6917
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006918 // Wallpaper windows: pixels offset based on above variables.
6919 int mXOffset;
6920 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 // This is set after IWindowSession.relayout() has been called at
6923 // least once for the window. It allows us to detect the situation
6924 // where we don't yet have a surface, but should have one soon, so
6925 // we can give the window focus before waiting for the relayout.
6926 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928 // This is set after the Surface has been created but before the
6929 // window has been drawn. During this time the surface is hidden.
6930 boolean mDrawPending;
6931
6932 // This is set after the window has finished drawing for the first
6933 // time but before its surface is shown. The surface will be
6934 // displayed when the next layout is run.
6935 boolean mCommitDrawPending;
6936
6937 // This is set during the time after the window's drawing has been
6938 // committed, and before its surface is actually shown. It is used
6939 // to delay showing the surface until all windows in a token are ready
6940 // to be shown.
6941 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006943 // Set when the window has been shown in the screen the first time.
6944 boolean mHasDrawn;
6945
6946 // Currently running an exit animation?
6947 boolean mExiting;
6948
6949 // Currently on the mDestroySurface list?
6950 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 // Completely remove from window manager after exit animation?
6953 boolean mRemoveOnExit;
6954
6955 // Set when the orientation is changing and this window has not yet
6956 // been updated for the new orientation.
6957 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 // Is this window now (or just being) removed?
6960 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 WindowState(Session s, IWindow c, WindowToken token,
6963 WindowState attachedWindow, WindowManager.LayoutParams a,
6964 int viewVisibility) {
6965 mSession = s;
6966 mClient = c;
6967 mToken = token;
6968 mAttrs.copyFrom(a);
6969 mViewVisibility = viewVisibility;
6970 DeathRecipient deathRecipient = new DeathRecipient();
6971 mAlpha = a.alpha;
6972 if (localLOGV) Log.v(
6973 TAG, "Window " + this + " client=" + c.asBinder()
6974 + " token=" + token + " (" + mAttrs.token + ")");
6975 try {
6976 c.asBinder().linkToDeath(deathRecipient, 0);
6977 } catch (RemoteException e) {
6978 mDeathRecipient = null;
6979 mAttachedWindow = null;
6980 mLayoutAttached = false;
6981 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006982 mIsWallpaper = false;
6983 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 mBaseLayer = 0;
6985 mSubLayer = 0;
6986 return;
6987 }
6988 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006990 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6991 mAttrs.type <= LAST_SUB_WINDOW)) {
6992 // The multiplier here is to reserve space for multiple
6993 // windows in the same type layer.
6994 mBaseLayer = mPolicy.windowTypeToLayerLw(
6995 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6996 + TYPE_LAYER_OFFSET;
6997 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6998 mAttachedWindow = attachedWindow;
6999 mAttachedWindow.mChildWindows.add(this);
7000 mLayoutAttached = mAttrs.type !=
7001 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7002 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7003 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007004 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7005 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 } else {
7007 // The multiplier here is to reserve space for multiple
7008 // windows in the same type layer.
7009 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7010 * TYPE_LAYER_MULTIPLIER
7011 + TYPE_LAYER_OFFSET;
7012 mSubLayer = 0;
7013 mAttachedWindow = null;
7014 mLayoutAttached = false;
7015 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7016 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007017 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7018 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019 }
7020
7021 WindowState appWin = this;
7022 while (appWin.mAttachedWindow != null) {
7023 appWin = mAttachedWindow;
7024 }
7025 WindowToken appToken = appWin.mToken;
7026 while (appToken.appWindowToken == null) {
7027 WindowToken parent = mTokenMap.get(appToken.token);
7028 if (parent == null || appToken == parent) {
7029 break;
7030 }
7031 appToken = parent;
7032 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007033 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007034 mAppToken = appToken.appWindowToken;
7035
7036 mSurface = null;
7037 mRequestedWidth = 0;
7038 mRequestedHeight = 0;
7039 mLastRequestedWidth = 0;
7040 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007041 mXOffset = 0;
7042 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007043 mLayer = 0;
7044 mAnimLayer = 0;
7045 mLastLayer = 0;
7046 }
7047
7048 void attach() {
7049 if (localLOGV) Log.v(
7050 TAG, "Attaching " + this + " token=" + mToken
7051 + ", list=" + mToken.windows);
7052 mSession.windowAddedLocked();
7053 }
7054
7055 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7056 mHaveFrame = true;
7057
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007058 final Rect container = mContainingFrame;
7059 container.set(pf);
7060
7061 final Rect display = mDisplayFrame;
7062 display.set(df);
7063
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007064 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007065 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007066 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7067 display.intersect(mCompatibleScreenFrame);
7068 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007069 }
7070
7071 final int pw = container.right - container.left;
7072 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007073
7074 int w,h;
7075 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7076 w = mAttrs.width < 0 ? pw : mAttrs.width;
7077 h = mAttrs.height< 0 ? ph : mAttrs.height;
7078 } else {
7079 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
7080 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
7081 }
Romain Guy06882f82009-06-10 13:36:04 -07007082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 final Rect content = mContentFrame;
7084 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 final Rect visible = mVisibleFrame;
7087 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007090 final int fw = frame.width();
7091 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007093 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7094 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7095
7096 Gravity.apply(mAttrs.gravity, w, h, container,
7097 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7098 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7099
7100 //System.out.println("Out: " + mFrame);
7101
7102 // Now make sure the window fits in the overall display.
7103 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007105 // Make sure the content and visible frames are inside of the
7106 // final window frame.
7107 if (content.left < frame.left) content.left = frame.left;
7108 if (content.top < frame.top) content.top = frame.top;
7109 if (content.right > frame.right) content.right = frame.right;
7110 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7111 if (visible.left < frame.left) visible.left = frame.left;
7112 if (visible.top < frame.top) visible.top = frame.top;
7113 if (visible.right > frame.right) visible.right = frame.right;
7114 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007116 final Rect contentInsets = mContentInsets;
7117 contentInsets.left = content.left-frame.left;
7118 contentInsets.top = content.top-frame.top;
7119 contentInsets.right = frame.right-content.right;
7120 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 final Rect visibleInsets = mVisibleInsets;
7123 visibleInsets.left = visible.left-frame.left;
7124 visibleInsets.top = visible.top-frame.top;
7125 visibleInsets.right = frame.right-visible.right;
7126 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007127
Dianne Hackborn284ac932009-08-28 10:34:25 -07007128 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7129 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007130 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007131 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 if (localLOGV) {
7134 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7135 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7136 Log.v(TAG, "Resolving (mRequestedWidth="
7137 + mRequestedWidth + ", mRequestedheight="
7138 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7139 + "): frame=" + mFrame.toShortString()
7140 + " ci=" + contentInsets.toShortString()
7141 + " vi=" + visibleInsets.toShortString());
7142 //}
7143 }
7144 }
Romain Guy06882f82009-06-10 13:36:04 -07007145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146 public Rect getFrameLw() {
7147 return mFrame;
7148 }
7149
7150 public Rect getShownFrameLw() {
7151 return mShownFrame;
7152 }
7153
7154 public Rect getDisplayFrameLw() {
7155 return mDisplayFrame;
7156 }
7157
7158 public Rect getContentFrameLw() {
7159 return mContentFrame;
7160 }
7161
7162 public Rect getVisibleFrameLw() {
7163 return mVisibleFrame;
7164 }
7165
7166 public boolean getGivenInsetsPendingLw() {
7167 return mGivenInsetsPending;
7168 }
7169
7170 public Rect getGivenContentInsetsLw() {
7171 return mGivenContentInsets;
7172 }
Romain Guy06882f82009-06-10 13:36:04 -07007173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007174 public Rect getGivenVisibleInsetsLw() {
7175 return mGivenVisibleInsets;
7176 }
Romain Guy06882f82009-06-10 13:36:04 -07007177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007178 public WindowManager.LayoutParams getAttrs() {
7179 return mAttrs;
7180 }
7181
7182 public int getSurfaceLayer() {
7183 return mLayer;
7184 }
Romain Guy06882f82009-06-10 13:36:04 -07007185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007186 public IApplicationToken getAppToken() {
7187 return mAppToken != null ? mAppToken.appToken : null;
7188 }
7189
7190 public boolean hasAppShownWindows() {
7191 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7192 }
7193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007194 public void setAnimation(Animation anim) {
7195 if (localLOGV) Log.v(
7196 TAG, "Setting animation in " + this + ": " + anim);
7197 mAnimating = false;
7198 mLocalAnimating = false;
7199 mAnimation = anim;
7200 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7201 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7202 }
7203
7204 public void clearAnimation() {
7205 if (mAnimation != null) {
7206 mAnimating = true;
7207 mLocalAnimating = false;
7208 mAnimation = null;
7209 }
7210 }
Romain Guy06882f82009-06-10 13:36:04 -07007211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007212 Surface createSurfaceLocked() {
7213 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007214 mReportDestroySurface = false;
7215 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 mDrawPending = true;
7217 mCommitDrawPending = false;
7218 mReadyToShow = false;
7219 if (mAppToken != null) {
7220 mAppToken.allDrawn = false;
7221 }
7222
7223 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007224 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007225 flags |= Surface.PUSH_BUFFERS;
7226 }
7227
7228 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7229 flags |= Surface.SECURE;
7230 }
7231 if (DEBUG_VISIBILITY) Log.v(
7232 TAG, "Creating surface in session "
7233 + mSession.mSurfaceSession + " window " + this
7234 + " w=" + mFrame.width()
7235 + " h=" + mFrame.height() + " format="
7236 + mAttrs.format + " flags=" + flags);
7237
7238 int w = mFrame.width();
7239 int h = mFrame.height();
7240 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7241 // for a scaled surface, we always want the requested
7242 // size.
7243 w = mRequestedWidth;
7244 h = mRequestedHeight;
7245 }
7246
Romain Guy9825ec62009-10-01 00:58:09 -07007247 // Something is wrong and SurfaceFlinger will not like this,
7248 // try to revert to sane values
7249 if (w <= 0) w = 1;
7250 if (h <= 0) h = 1;
7251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007252 try {
7253 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007254 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007255 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007256 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7257 + mSurface + " IN SESSION "
7258 + mSession.mSurfaceSession
7259 + ": pid=" + mSession.mPid + " format="
7260 + mAttrs.format + " flags=0x"
7261 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007262 } catch (Surface.OutOfResourcesException e) {
7263 Log.w(TAG, "OutOfResourcesException creating surface");
7264 reclaimSomeSurfaceMemoryLocked(this, "create");
7265 return null;
7266 } catch (Exception e) {
7267 Log.e(TAG, "Exception creating surface", e);
7268 return null;
7269 }
Romain Guy06882f82009-06-10 13:36:04 -07007270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007271 if (localLOGV) Log.v(
7272 TAG, "Got surface: " + mSurface
7273 + ", set left=" + mFrame.left + " top=" + mFrame.top
7274 + ", animLayer=" + mAnimLayer);
7275 if (SHOW_TRANSACTIONS) {
7276 Log.i(TAG, ">>> OPEN TRANSACTION");
7277 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7278 + mAttrs.getTitle() + ") pos=(" +
7279 mFrame.left + "," + mFrame.top + ") (" +
7280 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7281 mAnimLayer + " HIDE");
7282 }
7283 Surface.openTransaction();
7284 try {
7285 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007286 mSurface.setPosition(mFrame.left + mXOffset,
7287 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 mSurface.setLayer(mAnimLayer);
7289 mSurface.hide();
7290 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007291 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7292 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007293 mSurface.setFlags(Surface.SURFACE_DITHER,
7294 Surface.SURFACE_DITHER);
7295 }
7296 } catch (RuntimeException e) {
7297 Log.w(TAG, "Error creating surface in " + w, e);
7298 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7299 }
7300 mLastHidden = true;
7301 } finally {
7302 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7303 Surface.closeTransaction();
7304 }
7305 if (localLOGV) Log.v(
7306 TAG, "Created surface " + this);
7307 }
7308 return mSurface;
7309 }
Romain Guy06882f82009-06-10 13:36:04 -07007310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 void destroySurfaceLocked() {
7312 // Window is no longer on-screen, so can no longer receive
7313 // key events... if we were waiting for it to finish
7314 // handling a key event, the wait is over!
7315 mKeyWaiter.finishedKey(mSession, mClient, true,
7316 KeyWaiter.RETURN_NOTHING);
7317 mKeyWaiter.releasePendingPointerLocked(mSession);
7318 mKeyWaiter.releasePendingTrackballLocked(mSession);
7319
7320 if (mAppToken != null && this == mAppToken.startingWindow) {
7321 mAppToken.startingDisplayed = false;
7322 }
Romain Guy06882f82009-06-10 13:36:04 -07007323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007324 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007325 mDrawPending = false;
7326 mCommitDrawPending = false;
7327 mReadyToShow = false;
7328
7329 int i = mChildWindows.size();
7330 while (i > 0) {
7331 i--;
7332 WindowState c = (WindowState)mChildWindows.get(i);
7333 c.mAttachedHidden = true;
7334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007335
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007336 if (mReportDestroySurface) {
7337 mReportDestroySurface = false;
7338 mSurfacePendingDestroy = true;
7339 try {
7340 mClient.dispatchGetNewSurface();
7341 // We'll really destroy on the next time around.
7342 return;
7343 } catch (RemoteException e) {
7344 }
7345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007347 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007348 if (DEBUG_VISIBILITY) {
7349 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007350 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007351 Log.w(TAG, "Window " + this + " destroying surface "
7352 + mSurface + ", session " + mSession, e);
7353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007354 if (SHOW_TRANSACTIONS) {
7355 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007356 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7358 + mAttrs.getTitle() + ")", ex);
7359 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007360 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007361 } catch (RuntimeException e) {
7362 Log.w(TAG, "Exception thrown when destroying Window " + this
7363 + " surface " + mSurface + " session " + mSession
7364 + ": " + e.toString());
7365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007367 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007368 }
7369 }
7370
7371 boolean finishDrawingLocked() {
7372 if (mDrawPending) {
7373 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7374 TAG, "finishDrawingLocked: " + mSurface);
7375 mCommitDrawPending = true;
7376 mDrawPending = false;
7377 return true;
7378 }
7379 return false;
7380 }
7381
7382 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007383 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007384 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7385 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007386 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007387 }
7388 mCommitDrawPending = false;
7389 mReadyToShow = true;
7390 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7391 final AppWindowToken atoken = mAppToken;
7392 if (atoken == null || atoken.allDrawn || starting) {
7393 performShowLocked();
7394 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007395 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007396 }
7397
7398 // This must be called while inside a transaction.
7399 boolean performShowLocked() {
7400 if (DEBUG_VISIBILITY) {
7401 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007402 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 Log.v(TAG, "performShow on " + this
7404 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7405 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7406 }
7407 if (mReadyToShow && isReadyForDisplay()) {
7408 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7409 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7410 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7411 + " during animation: policyVis=" + mPolicyVisibility
7412 + " attHidden=" + mAttachedHidden
7413 + " tok.hiddenRequested="
7414 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007415 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007416 + (mAppToken != null ? mAppToken.hidden : false)
7417 + " animating=" + mAnimating
7418 + " tok animating="
7419 + (mAppToken != null ? mAppToken.animating : false));
7420 if (!showSurfaceRobustlyLocked(this)) {
7421 return false;
7422 }
7423 mLastAlpha = -1;
7424 mHasDrawn = true;
7425 mLastHidden = false;
7426 mReadyToShow = false;
7427 enableScreenIfNeededLocked();
7428
7429 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007431 int i = mChildWindows.size();
7432 while (i > 0) {
7433 i--;
7434 WindowState c = (WindowState)mChildWindows.get(i);
7435 if (c.mSurface != null && c.mAttachedHidden) {
7436 c.mAttachedHidden = false;
7437 c.performShowLocked();
7438 }
7439 }
Romain Guy06882f82009-06-10 13:36:04 -07007440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007441 if (mAttrs.type != TYPE_APPLICATION_STARTING
7442 && mAppToken != null) {
7443 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007444
Dianne Hackborn248b1882009-09-16 16:46:44 -07007445 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007446 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7447 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007449 // If this initial window is animating, stop it -- we
7450 // will do an animation to reveal it from behind the
7451 // starting window, so there is no need for it to also
7452 // be doing its own stuff.
7453 if (mAnimation != null) {
7454 mAnimation = null;
7455 // Make sure we clean up the animation.
7456 mAnimating = true;
7457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007458 mFinishedStarting.add(mAppToken);
7459 mH.sendEmptyMessage(H.FINISHED_STARTING);
7460 }
7461 mAppToken.updateReportedVisibilityLocked();
7462 }
7463 }
7464 return true;
7465 }
Romain Guy06882f82009-06-10 13:36:04 -07007466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007467 // This must be called while inside a transaction. Returns true if
7468 // there is more animation to run.
7469 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7470 if (!mDisplayFrozen) {
7471 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007473 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7474 mHasTransformation = true;
7475 mHasLocalTransformation = true;
7476 if (!mLocalAnimating) {
7477 if (DEBUG_ANIM) Log.v(
7478 TAG, "Starting animation in " + this +
7479 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7480 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7481 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7482 mAnimation.setStartTime(currentTime);
7483 mLocalAnimating = true;
7484 mAnimating = true;
7485 }
7486 mTransformation.clear();
7487 final boolean more = mAnimation.getTransformation(
7488 currentTime, mTransformation);
7489 if (DEBUG_ANIM) Log.v(
7490 TAG, "Stepped animation in " + this +
7491 ": more=" + more + ", xform=" + mTransformation);
7492 if (more) {
7493 // we're not done!
7494 return true;
7495 }
7496 if (DEBUG_ANIM) Log.v(
7497 TAG, "Finished animation in " + this +
7498 " @ " + currentTime);
7499 mAnimation = null;
7500 //WindowManagerService.this.dump();
7501 }
7502 mHasLocalTransformation = false;
7503 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007504 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 // When our app token is animating, we kind-of pretend like
7506 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7507 // part of this check means that we will only do this if
7508 // our window is not currently exiting, or it is not
7509 // locally animating itself. The idea being that one that
7510 // is exiting and doing a local animation should be removed
7511 // once that animation is done.
7512 mAnimating = true;
7513 mHasTransformation = true;
7514 mTransformation.clear();
7515 return false;
7516 } else if (mHasTransformation) {
7517 // Little trick to get through the path below to act like
7518 // we have finished an animation.
7519 mAnimating = true;
7520 } else if (isAnimating()) {
7521 mAnimating = true;
7522 }
7523 } else if (mAnimation != null) {
7524 // If the display is frozen, and there is a pending animation,
7525 // clear it and make sure we run the cleanup code.
7526 mAnimating = true;
7527 mLocalAnimating = true;
7528 mAnimation = null;
7529 }
Romain Guy06882f82009-06-10 13:36:04 -07007530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007531 if (!mAnimating && !mLocalAnimating) {
7532 return false;
7533 }
7534
7535 if (DEBUG_ANIM) Log.v(
7536 TAG, "Animation done in " + this + ": exiting=" + mExiting
7537 + ", reportedVisible="
7538 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007540 mAnimating = false;
7541 mLocalAnimating = false;
7542 mAnimation = null;
7543 mAnimLayer = mLayer;
7544 if (mIsImWindow) {
7545 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007546 } else if (mIsWallpaper) {
7547 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007548 }
7549 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7550 + " anim layer: " + mAnimLayer);
7551 mHasTransformation = false;
7552 mHasLocalTransformation = false;
7553 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7554 mTransformation.clear();
7555 if (mHasDrawn
7556 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7557 && mAppToken != null
7558 && mAppToken.firstWindowDrawn
7559 && mAppToken.startingData != null) {
7560 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7561 + mToken + ": first real window done animating");
7562 mFinishedStarting.add(mAppToken);
7563 mH.sendEmptyMessage(H.FINISHED_STARTING);
7564 }
Romain Guy06882f82009-06-10 13:36:04 -07007565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007566 finishExit();
7567
7568 if (mAppToken != null) {
7569 mAppToken.updateReportedVisibilityLocked();
7570 }
7571
7572 return false;
7573 }
7574
7575 void finishExit() {
7576 if (DEBUG_ANIM) Log.v(
7577 TAG, "finishExit in " + this
7578 + ": exiting=" + mExiting
7579 + " remove=" + mRemoveOnExit
7580 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007582 final int N = mChildWindows.size();
7583 for (int i=0; i<N; i++) {
7584 ((WindowState)mChildWindows.get(i)).finishExit();
7585 }
Romain Guy06882f82009-06-10 13:36:04 -07007586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 if (!mExiting) {
7588 return;
7589 }
Romain Guy06882f82009-06-10 13:36:04 -07007590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007591 if (isWindowAnimating()) {
7592 return;
7593 }
7594
7595 if (localLOGV) Log.v(
7596 TAG, "Exit animation finished in " + this
7597 + ": remove=" + mRemoveOnExit);
7598 if (mSurface != null) {
7599 mDestroySurface.add(this);
7600 mDestroying = true;
7601 if (SHOW_TRANSACTIONS) Log.i(
7602 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7603 try {
7604 mSurface.hide();
7605 } catch (RuntimeException e) {
7606 Log.w(TAG, "Error hiding surface in " + this, e);
7607 }
7608 mLastHidden = true;
7609 mKeyWaiter.releasePendingPointerLocked(mSession);
7610 }
7611 mExiting = false;
7612 if (mRemoveOnExit) {
7613 mPendingRemove.add(this);
7614 mRemoveOnExit = false;
7615 }
7616 }
Romain Guy06882f82009-06-10 13:36:04 -07007617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7619 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7620 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7621 if (dtdx < -.000001f || dtdx > .000001f) return false;
7622 if (dsdy < -.000001f || dsdy > .000001f) return false;
7623 return true;
7624 }
Romain Guy06882f82009-06-10 13:36:04 -07007625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 void computeShownFrameLocked() {
7627 final boolean selfTransformation = mHasLocalTransformation;
7628 Transformation attachedTransformation =
7629 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7630 ? mAttachedWindow.mTransformation : null;
7631 Transformation appTransformation =
7632 (mAppToken != null && mAppToken.hasTransformation)
7633 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007634
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007635 // Wallpapers are animated based on the "real" window they
7636 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007637 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007638 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007639 if (mWallpaperTarget.mHasLocalTransformation &&
7640 mWallpaperTarget.mAnimation != null &&
7641 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007642 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007643 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7644 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7645 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007646 }
7647 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007648 mWallpaperTarget.mAppToken.hasTransformation &&
7649 mWallpaperTarget.mAppToken.animation != null &&
7650 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007651 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007652 if (DEBUG_WALLPAPER && appTransformation != null) {
7653 Log.v(TAG, "WP target app xform: " + appTransformation);
7654 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007655 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007656 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007658 if (selfTransformation || attachedTransformation != null
7659 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007660 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661 final Rect frame = mFrame;
7662 final float tmpFloats[] = mTmpFloats;
7663 final Matrix tmpMatrix = mTmpMatrix;
7664
7665 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007666 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007668 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007670 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007671 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007672 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007673 }
7674 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007675 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 }
7677
7678 // "convert" it into SurfaceFlinger's format
7679 // (a 2x2 matrix + an offset)
7680 // Here we must not transform the position of the surface
7681 // since it is already included in the transformation.
7682 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684 tmpMatrix.getValues(tmpFloats);
7685 mDsDx = tmpFloats[Matrix.MSCALE_X];
7686 mDtDx = tmpFloats[Matrix.MSKEW_X];
7687 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7688 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007689 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7690 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007691 int w = frame.width();
7692 int h = frame.height();
7693 mShownFrame.set(x, y, x+w, y+h);
7694
7695 // Now set the alpha... but because our current hardware
7696 // can't do alpha transformation on a non-opaque surface,
7697 // turn it off if we are running an animation that is also
7698 // transforming since it is more important to have that
7699 // animation be smooth.
7700 mShownAlpha = mAlpha;
7701 if (!mLimitedAlphaCompositing
7702 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7703 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7704 && x == frame.left && y == frame.top))) {
7705 //Log.i(TAG, "Applying alpha transform");
7706 if (selfTransformation) {
7707 mShownAlpha *= mTransformation.getAlpha();
7708 }
7709 if (attachedTransformation != null) {
7710 mShownAlpha *= attachedTransformation.getAlpha();
7711 }
7712 if (appTransformation != null) {
7713 mShownAlpha *= appTransformation.getAlpha();
7714 }
7715 } else {
7716 //Log.i(TAG, "Not applying alpha transform");
7717 }
Romain Guy06882f82009-06-10 13:36:04 -07007718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 if (localLOGV) Log.v(
7720 TAG, "Continuing animation in " + this +
7721 ": " + mShownFrame +
7722 ", alpha=" + mTransformation.getAlpha());
7723 return;
7724 }
Romain Guy06882f82009-06-10 13:36:04 -07007725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007726 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007727 if (mXOffset != 0 || mYOffset != 0) {
7728 mShownFrame.offset(mXOffset, mYOffset);
7729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007730 mShownAlpha = mAlpha;
7731 mDsDx = 1;
7732 mDtDx = 0;
7733 mDsDy = 0;
7734 mDtDy = 1;
7735 }
Romain Guy06882f82009-06-10 13:36:04 -07007736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737 /**
7738 * Is this window visible? It is not visible if there is no
7739 * surface, or we are in the process of running an exit animation
7740 * that will remove the surface, or its app token has been hidden.
7741 */
7742 public boolean isVisibleLw() {
7743 final AppWindowToken atoken = mAppToken;
7744 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7745 && (atoken == null || !atoken.hiddenRequested)
7746 && !mExiting && !mDestroying;
7747 }
7748
7749 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007750 * Like {@link #isVisibleLw}, but also counts a window that is currently
7751 * "hidden" behind the keyguard as visible. This allows us to apply
7752 * things like window flags that impact the keyguard.
7753 * XXX I am starting to think we need to have ANOTHER visibility flag
7754 * for this "hidden behind keyguard" state rather than overloading
7755 * mPolicyVisibility. Ungh.
7756 */
7757 public boolean isVisibleOrBehindKeyguardLw() {
7758 final AppWindowToken atoken = mAppToken;
7759 return mSurface != null && !mAttachedHidden
7760 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7761 && !mExiting && !mDestroying;
7762 }
7763
7764 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 * Is this window visible, ignoring its app token? It is not visible
7766 * if there is no surface, or we are in the process of running an exit animation
7767 * that will remove the surface.
7768 */
7769 public boolean isWinVisibleLw() {
7770 final AppWindowToken atoken = mAppToken;
7771 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7772 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7773 && !mExiting && !mDestroying;
7774 }
7775
7776 /**
7777 * The same as isVisible(), but follows the current hidden state of
7778 * the associated app token, not the pending requested hidden state.
7779 */
7780 boolean isVisibleNow() {
7781 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007782 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 }
7784
7785 /**
7786 * Same as isVisible(), but we also count it as visible between the
7787 * call to IWindowSession.add() and the first relayout().
7788 */
7789 boolean isVisibleOrAdding() {
7790 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007791 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007792 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7793 && mPolicyVisibility && !mAttachedHidden
7794 && (atoken == null || !atoken.hiddenRequested)
7795 && !mExiting && !mDestroying;
7796 }
7797
7798 /**
7799 * Is this window currently on-screen? It is on-screen either if it
7800 * is visible or it is currently running an animation before no longer
7801 * being visible.
7802 */
7803 boolean isOnScreen() {
7804 final AppWindowToken atoken = mAppToken;
7805 if (atoken != null) {
7806 return mSurface != null && mPolicyVisibility && !mDestroying
7807 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007808 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007809 } else {
7810 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007811 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007812 }
7813 }
Romain Guy06882f82009-06-10 13:36:04 -07007814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007815 /**
7816 * Like isOnScreen(), but we don't return true if the window is part
7817 * of a transition that has not yet been started.
7818 */
7819 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007820 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007821 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007822 return false;
7823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007825 final boolean animating = atoken != null
7826 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007827 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007828 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7829 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007830 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007831 }
7832
7833 /** Is the window or its container currently animating? */
7834 boolean isAnimating() {
7835 final WindowState attached = mAttachedWindow;
7836 final AppWindowToken atoken = mAppToken;
7837 return mAnimation != null
7838 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007839 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 (atoken.animation != null
7841 || atoken.inPendingTransaction));
7842 }
7843
7844 /** Is this window currently animating? */
7845 boolean isWindowAnimating() {
7846 return mAnimation != null;
7847 }
7848
7849 /**
7850 * Like isOnScreen, but returns false if the surface hasn't yet
7851 * been drawn.
7852 */
7853 public boolean isDisplayedLw() {
7854 final AppWindowToken atoken = mAppToken;
7855 return mSurface != null && mPolicyVisibility && !mDestroying
7856 && !mDrawPending && !mCommitDrawPending
7857 && ((!mAttachedHidden &&
7858 (atoken == null || !atoken.hiddenRequested))
7859 || mAnimating);
7860 }
7861
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007862 /**
7863 * Returns true if the window has a surface that it has drawn a
7864 * complete UI in to.
7865 */
7866 public boolean isDrawnLw() {
7867 final AppWindowToken atoken = mAppToken;
7868 return mSurface != null && !mDestroying
7869 && !mDrawPending && !mCommitDrawPending;
7870 }
7871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7873 boolean shownFrame, boolean onlyOpaque) {
7874 if (mSurface == null) {
7875 return false;
7876 }
7877 if (mAppToken != null && !mAppToken.appFullscreen) {
7878 return false;
7879 }
7880 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7881 return false;
7882 }
7883 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007884
7885 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7886 return frame.left <= mCompatibleScreenFrame.left &&
7887 frame.top <= mCompatibleScreenFrame.top &&
7888 frame.right >= mCompatibleScreenFrame.right &&
7889 frame.bottom >= mCompatibleScreenFrame.bottom;
7890 } else {
7891 return frame.left <= 0 && frame.top <= 0
7892 && frame.right >= screenWidth
7893 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 }
Romain Guy06882f82009-06-10 13:36:04 -07007896
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007897 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007898 * Return true if the window is opaque and fully drawn. This indicates
7899 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007900 */
7901 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007902 return (mAttrs.format == PixelFormat.OPAQUE
7903 || mAttrs.type == TYPE_WALLPAPER)
7904 && mSurface != null && mAnimation == null
7905 && (mAppToken == null || mAppToken.animation == null)
7906 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007907 }
7908
7909 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7910 return
7911 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007912 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7913 // only if it's visible
7914 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007915 // and only if the application fills the compatible screen
7916 mFrame.left <= mCompatibleScreenFrame.left &&
7917 mFrame.top <= mCompatibleScreenFrame.top &&
7918 mFrame.right >= mCompatibleScreenFrame.right &&
7919 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007920 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007921 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007922 }
7923
7924 boolean isFullscreen(int screenWidth, int screenHeight) {
7925 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007926 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007927 }
7928
7929 void removeLocked() {
7930 if (mAttachedWindow != null) {
7931 mAttachedWindow.mChildWindows.remove(this);
7932 }
7933 destroySurfaceLocked();
7934 mSession.windowRemovedLocked();
7935 try {
7936 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7937 } catch (RuntimeException e) {
7938 // Ignore if it has already been removed (usually because
7939 // we are doing this as part of processing a death note.)
7940 }
7941 }
7942
7943 private class DeathRecipient implements IBinder.DeathRecipient {
7944 public void binderDied() {
7945 try {
7946 synchronized(mWindowMap) {
7947 WindowState win = windowForClientLocked(mSession, mClient);
7948 Log.i(TAG, "WIN DEATH: " + win);
7949 if (win != null) {
7950 removeWindowLocked(mSession, win);
7951 }
7952 }
7953 } catch (IllegalArgumentException ex) {
7954 // This will happen if the window has already been
7955 // removed.
7956 }
7957 }
7958 }
7959
7960 /** Returns true if this window desires key events. */
7961 public final boolean canReceiveKeys() {
7962 return isVisibleOrAdding()
7963 && (mViewVisibility == View.VISIBLE)
7964 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7965 }
7966
7967 public boolean hasDrawnLw() {
7968 return mHasDrawn;
7969 }
7970
7971 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007972 return showLw(doAnimation, true);
7973 }
7974
7975 boolean showLw(boolean doAnimation, boolean requestAnim) {
7976 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
7977 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007979 mPolicyVisibility = true;
7980 mPolicyVisibilityAfterAnim = true;
7981 if (doAnimation) {
7982 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7983 }
7984 if (requestAnim) {
7985 requestAnimationLocked(0);
7986 }
7987 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007988 }
7989
7990 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007991 return hideLw(doAnimation, true);
7992 }
7993
7994 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007995 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7996 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007997 if (!current) {
7998 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007999 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008000 if (doAnimation) {
8001 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8002 if (mAnimation == null) {
8003 doAnimation = false;
8004 }
8005 }
8006 if (doAnimation) {
8007 mPolicyVisibilityAfterAnim = false;
8008 } else {
8009 mPolicyVisibilityAfterAnim = false;
8010 mPolicyVisibility = false;
8011 }
8012 if (requestAnim) {
8013 requestAnimationLocked(0);
8014 }
8015 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 }
8017
8018 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008019 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07008020
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008021 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8022 pw.print(" mClient="); pw.println(mClient.asBinder());
8023 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8024 if (mAttachedWindow != null || mLayoutAttached) {
8025 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8026 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8027 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008028 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8029 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8030 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008031 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8032 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008033 }
8034 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8035 pw.print(" mSubLayer="); pw.print(mSubLayer);
8036 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8037 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8038 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8039 pw.print("="); pw.print(mAnimLayer);
8040 pw.print(" mLastLayer="); pw.println(mLastLayer);
8041 if (mSurface != null) {
8042 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8043 }
8044 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8045 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8046 if (mAppToken != null) {
8047 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8048 }
8049 if (mTargetAppToken != null) {
8050 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8051 }
8052 pw.print(prefix); pw.print("mViewVisibility=0x");
8053 pw.print(Integer.toHexString(mViewVisibility));
8054 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008055 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8056 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008057 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8058 pw.print(prefix); pw.print("mPolicyVisibility=");
8059 pw.print(mPolicyVisibility);
8060 pw.print(" mPolicyVisibilityAfterAnim=");
8061 pw.print(mPolicyVisibilityAfterAnim);
8062 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8063 }
8064 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008065 pw.print(" h="); pw.println(mRequestedHeight);
8066 if (mXOffset != 0 || mYOffset != 0) {
8067 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8068 pw.print(" y="); pw.println(mYOffset);
8069 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008070 pw.print(prefix); pw.print("mGivenContentInsets=");
8071 mGivenContentInsets.printShortString(pw);
8072 pw.print(" mGivenVisibleInsets=");
8073 mGivenVisibleInsets.printShortString(pw);
8074 pw.println();
8075 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8076 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8077 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8078 }
8079 pw.print(prefix); pw.print("mShownFrame=");
8080 mShownFrame.printShortString(pw);
8081 pw.print(" last="); mLastShownFrame.printShortString(pw);
8082 pw.println();
8083 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8084 pw.print(" last="); mLastFrame.printShortString(pw);
8085 pw.println();
8086 pw.print(prefix); pw.print("mContainingFrame=");
8087 mContainingFrame.printShortString(pw);
8088 pw.print(" mDisplayFrame=");
8089 mDisplayFrame.printShortString(pw);
8090 pw.println();
8091 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8092 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8093 pw.println();
8094 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8095 pw.print(" last="); mLastContentInsets.printShortString(pw);
8096 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8097 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8098 pw.println();
8099 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8100 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8101 pw.print(" mAlpha="); pw.print(mAlpha);
8102 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8103 }
8104 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8105 || mAnimation != null) {
8106 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8107 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8108 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8109 pw.print(" mAnimation="); pw.println(mAnimation);
8110 }
8111 if (mHasTransformation || mHasLocalTransformation) {
8112 pw.print(prefix); pw.print("XForm: has=");
8113 pw.print(mHasTransformation);
8114 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8115 pw.print(" "); mTransformation.printShortString(pw);
8116 pw.println();
8117 }
8118 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8119 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8120 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8121 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8122 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8123 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8124 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8125 pw.print(" mDestroying="); pw.print(mDestroying);
8126 pw.print(" mRemoved="); pw.println(mRemoved);
8127 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008128 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008129 pw.print(prefix); pw.print("mOrientationChanging=");
8130 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008131 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8132 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008133 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008134 if (mHScale != 1 || mVScale != 1) {
8135 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8136 pw.print(" mVScale="); pw.println(mVScale);
8137 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008138 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008139 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8140 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8141 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008142 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8143 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8144 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008146 }
8147
8148 @Override
8149 public String toString() {
8150 return "Window{"
8151 + Integer.toHexString(System.identityHashCode(this))
8152 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8153 }
8154 }
Romain Guy06882f82009-06-10 13:36:04 -07008155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008156 // -------------------------------------------------------------
8157 // Window Token State
8158 // -------------------------------------------------------------
8159
8160 class WindowToken {
8161 // The actual token.
8162 final IBinder token;
8163
8164 // The type of window this token is for, as per WindowManager.LayoutParams.
8165 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008167 // Set if this token was explicitly added by a client, so should
8168 // not be removed when all windows are removed.
8169 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008170
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008171 // For printing.
8172 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008174 // If this is an AppWindowToken, this is non-null.
8175 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008177 // All of the windows associated with this token.
8178 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8179
8180 // Is key dispatching paused for this token?
8181 boolean paused = false;
8182
8183 // Should this token's windows be hidden?
8184 boolean hidden;
8185
8186 // Temporary for finding which tokens no longer have visible windows.
8187 boolean hasVisible;
8188
Dianne Hackborna8f60182009-09-01 19:01:50 -07008189 // Set to true when this token is in a pending transaction where it
8190 // will be shown.
8191 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008192
Dianne Hackborna8f60182009-09-01 19:01:50 -07008193 // Set to true when this token is in a pending transaction where it
8194 // will be hidden.
8195 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008196
Dianne Hackborna8f60182009-09-01 19:01:50 -07008197 // Set to true when this token is in a pending transaction where its
8198 // windows will be put to the bottom of the list.
8199 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008200
Dianne Hackborna8f60182009-09-01 19:01:50 -07008201 // Set to true when this token is in a pending transaction where its
8202 // windows will be put to the top of the list.
8203 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008205 WindowToken(IBinder _token, int type, boolean _explicit) {
8206 token = _token;
8207 windowType = type;
8208 explicit = _explicit;
8209 }
8210
8211 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008212 pw.print(prefix); pw.print("token="); pw.println(token);
8213 pw.print(prefix); pw.print("windows="); pw.println(windows);
8214 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8215 pw.print(" hidden="); pw.print(hidden);
8216 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008217 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8218 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8219 pw.print(" waitingToHide="); pw.print(waitingToHide);
8220 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8221 pw.print(" sendingToTop="); pw.println(sendingToTop);
8222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223 }
8224
8225 @Override
8226 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008227 if (stringName == null) {
8228 StringBuilder sb = new StringBuilder();
8229 sb.append("WindowToken{");
8230 sb.append(Integer.toHexString(System.identityHashCode(this)));
8231 sb.append(" token="); sb.append(token); sb.append('}');
8232 stringName = sb.toString();
8233 }
8234 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 }
8236 };
8237
8238 class AppWindowToken extends WindowToken {
8239 // Non-null only for application tokens.
8240 final IApplicationToken appToken;
8241
8242 // All of the windows and child windows that are included in this
8243 // application token. Note this list is NOT sorted!
8244 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8245
8246 int groupId = -1;
8247 boolean appFullscreen;
8248 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008250 // These are used for determining when all windows associated with
8251 // an activity have been drawn, so they can be made visible together
8252 // at the same time.
8253 int lastTransactionSequence = mTransactionSequence-1;
8254 int numInterestingWindows;
8255 int numDrawnWindows;
8256 boolean inPendingTransaction;
8257 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 // Is this token going to be hidden in a little while? If so, it
8260 // won't be taken into account for setting the screen orientation.
8261 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 // Is this window's surface needed? This is almost like hidden, except
8264 // it will sometimes be true a little earlier: when the token has
8265 // been shown, but is still waiting for its app transition to execute
8266 // before making its windows shown.
8267 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008269 // Have we told the window clients to hide themselves?
8270 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008272 // Last visibility state we reported to the app token.
8273 boolean reportedVisible;
8274
8275 // Set to true when the token has been removed from the window mgr.
8276 boolean removed;
8277
8278 // Have we been asked to have this token keep the screen frozen?
8279 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008281 boolean animating;
8282 Animation animation;
8283 boolean hasTransformation;
8284 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008286 // Offset to the window of all layers in the token, for use by
8287 // AppWindowToken animations.
8288 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008290 // Information about an application starting window if displayed.
8291 StartingData startingData;
8292 WindowState startingWindow;
8293 View startingView;
8294 boolean startingDisplayed;
8295 boolean startingMoved;
8296 boolean firstWindowDrawn;
8297
8298 AppWindowToken(IApplicationToken _token) {
8299 super(_token.asBinder(),
8300 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8301 appWindowToken = this;
8302 appToken = _token;
8303 }
Romain Guy06882f82009-06-10 13:36:04 -07008304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008305 public void setAnimation(Animation anim) {
8306 if (localLOGV) Log.v(
8307 TAG, "Setting animation in " + this + ": " + anim);
8308 animation = anim;
8309 animating = false;
8310 anim.restrictDuration(MAX_ANIMATION_DURATION);
8311 anim.scaleCurrentDuration(mTransitionAnimationScale);
8312 int zorder = anim.getZAdjustment();
8313 int adj = 0;
8314 if (zorder == Animation.ZORDER_TOP) {
8315 adj = TYPE_LAYER_OFFSET;
8316 } else if (zorder == Animation.ZORDER_BOTTOM) {
8317 adj = -TYPE_LAYER_OFFSET;
8318 }
Romain Guy06882f82009-06-10 13:36:04 -07008319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008320 if (animLayerAdjustment != adj) {
8321 animLayerAdjustment = adj;
8322 updateLayers();
8323 }
8324 }
Romain Guy06882f82009-06-10 13:36:04 -07008325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008326 public void setDummyAnimation() {
8327 if (animation == null) {
8328 if (localLOGV) Log.v(
8329 TAG, "Setting dummy animation in " + this);
8330 animation = sDummyAnimation;
8331 }
8332 }
8333
8334 public void clearAnimation() {
8335 if (animation != null) {
8336 animation = null;
8337 animating = true;
8338 }
8339 }
Romain Guy06882f82009-06-10 13:36:04 -07008340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008341 void updateLayers() {
8342 final int N = allAppWindows.size();
8343 final int adj = animLayerAdjustment;
8344 for (int i=0; i<N; i++) {
8345 WindowState w = allAppWindows.get(i);
8346 w.mAnimLayer = w.mLayer + adj;
8347 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8348 + w.mAnimLayer);
8349 if (w == mInputMethodTarget) {
8350 setInputMethodAnimLayerAdjustment(adj);
8351 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008352 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008353 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 }
8356 }
Romain Guy06882f82009-06-10 13:36:04 -07008357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008358 void sendAppVisibilityToClients() {
8359 final int N = allAppWindows.size();
8360 for (int i=0; i<N; i++) {
8361 WindowState win = allAppWindows.get(i);
8362 if (win == startingWindow && clientHidden) {
8363 // Don't hide the starting window.
8364 continue;
8365 }
8366 try {
8367 if (DEBUG_VISIBILITY) Log.v(TAG,
8368 "Setting visibility of " + win + ": " + (!clientHidden));
8369 win.mClient.dispatchAppVisibility(!clientHidden);
8370 } catch (RemoteException e) {
8371 }
8372 }
8373 }
Romain Guy06882f82009-06-10 13:36:04 -07008374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008375 void showAllWindowsLocked() {
8376 final int NW = allAppWindows.size();
8377 for (int i=0; i<NW; i++) {
8378 WindowState w = allAppWindows.get(i);
8379 if (DEBUG_VISIBILITY) Log.v(TAG,
8380 "performing show on: " + w);
8381 w.performShowLocked();
8382 }
8383 }
Romain Guy06882f82009-06-10 13:36:04 -07008384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008385 // This must be called while inside a transaction.
8386 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8387 if (!mDisplayFrozen) {
8388 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 if (animation == sDummyAnimation) {
8391 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008392 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008393 // when it is really time to animate, this will be set to
8394 // a real animation and the next call will execute normally.
8395 return false;
8396 }
Romain Guy06882f82009-06-10 13:36:04 -07008397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8399 if (!animating) {
8400 if (DEBUG_ANIM) Log.v(
8401 TAG, "Starting animation in " + this +
8402 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8403 + " scale=" + mTransitionAnimationScale
8404 + " allDrawn=" + allDrawn + " animating=" + animating);
8405 animation.initialize(dw, dh, dw, dh);
8406 animation.setStartTime(currentTime);
8407 animating = true;
8408 }
8409 transformation.clear();
8410 final boolean more = animation.getTransformation(
8411 currentTime, transformation);
8412 if (DEBUG_ANIM) Log.v(
8413 TAG, "Stepped animation in " + this +
8414 ": more=" + more + ", xform=" + transformation);
8415 if (more) {
8416 // we're done!
8417 hasTransformation = true;
8418 return true;
8419 }
8420 if (DEBUG_ANIM) Log.v(
8421 TAG, "Finished animation in " + this +
8422 " @ " + currentTime);
8423 animation = null;
8424 }
8425 } else if (animation != null) {
8426 // If the display is frozen, and there is a pending animation,
8427 // clear it and make sure we run the cleanup code.
8428 animating = true;
8429 animation = null;
8430 }
8431
8432 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008434 if (!animating) {
8435 return false;
8436 }
8437
8438 clearAnimation();
8439 animating = false;
8440 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8441 moveInputMethodWindowsIfNeededLocked(true);
8442 }
Romain Guy06882f82009-06-10 13:36:04 -07008443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008444 if (DEBUG_ANIM) Log.v(
8445 TAG, "Animation done in " + this
8446 + ": reportedVisible=" + reportedVisible);
8447
8448 transformation.clear();
8449 if (animLayerAdjustment != 0) {
8450 animLayerAdjustment = 0;
8451 updateLayers();
8452 }
Romain Guy06882f82009-06-10 13:36:04 -07008453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008454 final int N = windows.size();
8455 for (int i=0; i<N; i++) {
8456 ((WindowState)windows.get(i)).finishExit();
8457 }
8458 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 return false;
8461 }
8462
8463 void updateReportedVisibilityLocked() {
8464 if (appToken == null) {
8465 return;
8466 }
Romain Guy06882f82009-06-10 13:36:04 -07008467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008468 int numInteresting = 0;
8469 int numVisible = 0;
8470 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008472 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8473 final int N = allAppWindows.size();
8474 for (int i=0; i<N; i++) {
8475 WindowState win = allAppWindows.get(i);
8476 if (win == startingWindow || win.mAppFreezing) {
8477 continue;
8478 }
8479 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008480 Log.v(TAG, "Win " + win + ": isDrawn="
8481 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008482 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008483 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008484 Log.v(TAG, "Not displayed: s=" + win.mSurface
8485 + " pv=" + win.mPolicyVisibility
8486 + " dp=" + win.mDrawPending
8487 + " cdp=" + win.mCommitDrawPending
8488 + " ah=" + win.mAttachedHidden
8489 + " th="
8490 + (win.mAppToken != null
8491 ? win.mAppToken.hiddenRequested : false)
8492 + " a=" + win.mAnimating);
8493 }
8494 }
8495 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008496 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008497 if (!win.isAnimating()) {
8498 numVisible++;
8499 }
8500 nowGone = false;
8501 } else if (win.isAnimating()) {
8502 nowGone = false;
8503 }
8504 }
Romain Guy06882f82009-06-10 13:36:04 -07008505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008506 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8507 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8508 + numInteresting + " visible=" + numVisible);
8509 if (nowVisible != reportedVisible) {
8510 if (DEBUG_VISIBILITY) Log.v(
8511 TAG, "Visibility changed in " + this
8512 + ": vis=" + nowVisible);
8513 reportedVisible = nowVisible;
8514 Message m = mH.obtainMessage(
8515 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8516 nowVisible ? 1 : 0,
8517 nowGone ? 1 : 0,
8518 this);
8519 mH.sendMessage(m);
8520 }
8521 }
Romain Guy06882f82009-06-10 13:36:04 -07008522
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008523 WindowState findMainWindow() {
8524 int j = windows.size();
8525 while (j > 0) {
8526 j--;
8527 WindowState win = windows.get(j);
8528 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8529 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8530 return win;
8531 }
8532 }
8533 return null;
8534 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 void dump(PrintWriter pw, String prefix) {
8537 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008538 if (appToken != null) {
8539 pw.print(prefix); pw.println("app=true");
8540 }
8541 if (allAppWindows.size() > 0) {
8542 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8543 }
8544 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008545 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008546 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8547 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8548 pw.print(" clientHidden="); pw.print(clientHidden);
8549 pw.print(" willBeHidden="); pw.print(willBeHidden);
8550 pw.print(" reportedVisible="); pw.println(reportedVisible);
8551 if (paused || freezingScreen) {
8552 pw.print(prefix); pw.print("paused="); pw.print(paused);
8553 pw.print(" freezingScreen="); pw.println(freezingScreen);
8554 }
8555 if (numInterestingWindows != 0 || numDrawnWindows != 0
8556 || inPendingTransaction || allDrawn) {
8557 pw.print(prefix); pw.print("numInterestingWindows=");
8558 pw.print(numInterestingWindows);
8559 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8560 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8561 pw.print(" allDrawn="); pw.println(allDrawn);
8562 }
8563 if (animating || animation != null) {
8564 pw.print(prefix); pw.print("animating="); pw.print(animating);
8565 pw.print(" animation="); pw.println(animation);
8566 }
8567 if (animLayerAdjustment != 0) {
8568 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8569 }
8570 if (hasTransformation) {
8571 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8572 pw.print(" transformation="); transformation.printShortString(pw);
8573 pw.println();
8574 }
8575 if (startingData != null || removed || firstWindowDrawn) {
8576 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8577 pw.print(" removed="); pw.print(removed);
8578 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8579 }
8580 if (startingWindow != null || startingView != null
8581 || startingDisplayed || startingMoved) {
8582 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8583 pw.print(" startingView="); pw.print(startingView);
8584 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8585 pw.print(" startingMoved"); pw.println(startingMoved);
8586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008587 }
8588
8589 @Override
8590 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008591 if (stringName == null) {
8592 StringBuilder sb = new StringBuilder();
8593 sb.append("AppWindowToken{");
8594 sb.append(Integer.toHexString(System.identityHashCode(this)));
8595 sb.append(" token="); sb.append(token); sb.append('}');
8596 stringName = sb.toString();
8597 }
8598 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008599 }
8600 }
Romain Guy06882f82009-06-10 13:36:04 -07008601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008602 // -------------------------------------------------------------
8603 // DummyAnimation
8604 // -------------------------------------------------------------
8605
8606 // This is an animation that does nothing: it just immediately finishes
8607 // itself every time it is called. It is used as a stub animation in cases
8608 // where we want to synchronize multiple things that may be animating.
8609 static final class DummyAnimation extends Animation {
8610 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8611 return false;
8612 }
8613 }
8614 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008616 // -------------------------------------------------------------
8617 // Async Handler
8618 // -------------------------------------------------------------
8619
8620 static final class StartingData {
8621 final String pkg;
8622 final int theme;
8623 final CharSequence nonLocalizedLabel;
8624 final int labelRes;
8625 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008627 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8628 int _labelRes, int _icon) {
8629 pkg = _pkg;
8630 theme = _theme;
8631 nonLocalizedLabel = _nonLocalizedLabel;
8632 labelRes = _labelRes;
8633 icon = _icon;
8634 }
8635 }
8636
8637 private final class H extends Handler {
8638 public static final int REPORT_FOCUS_CHANGE = 2;
8639 public static final int REPORT_LOSING_FOCUS = 3;
8640 public static final int ANIMATE = 4;
8641 public static final int ADD_STARTING = 5;
8642 public static final int REMOVE_STARTING = 6;
8643 public static final int FINISHED_STARTING = 7;
8644 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008645 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8646 public static final int HOLD_SCREEN_CHANGED = 12;
8647 public static final int APP_TRANSITION_TIMEOUT = 13;
8648 public static final int PERSIST_ANIMATION_SCALE = 14;
8649 public static final int FORCE_GC = 15;
8650 public static final int ENABLE_SCREEN = 16;
8651 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008652 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008654 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008656 public H() {
8657 }
Romain Guy06882f82009-06-10 13:36:04 -07008658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 @Override
8660 public void handleMessage(Message msg) {
8661 switch (msg.what) {
8662 case REPORT_FOCUS_CHANGE: {
8663 WindowState lastFocus;
8664 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008666 synchronized(mWindowMap) {
8667 lastFocus = mLastFocus;
8668 newFocus = mCurrentFocus;
8669 if (lastFocus == newFocus) {
8670 // Focus is not changing, so nothing to do.
8671 return;
8672 }
8673 mLastFocus = newFocus;
8674 //Log.i(TAG, "Focus moving from " + lastFocus
8675 // + " to " + newFocus);
8676 if (newFocus != null && lastFocus != null
8677 && !newFocus.isDisplayedLw()) {
8678 //Log.i(TAG, "Delaying loss of focus...");
8679 mLosingFocus.add(lastFocus);
8680 lastFocus = null;
8681 }
8682 }
8683
8684 if (lastFocus != newFocus) {
8685 //System.out.println("Changing focus from " + lastFocus
8686 // + " to " + newFocus);
8687 if (newFocus != null) {
8688 try {
8689 //Log.i(TAG, "Gaining focus: " + newFocus);
8690 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8691 } catch (RemoteException e) {
8692 // Ignore if process has died.
8693 }
8694 }
8695
8696 if (lastFocus != null) {
8697 try {
8698 //Log.i(TAG, "Losing focus: " + lastFocus);
8699 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8700 } catch (RemoteException e) {
8701 // Ignore if process has died.
8702 }
8703 }
8704 }
8705 } break;
8706
8707 case REPORT_LOSING_FOCUS: {
8708 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008710 synchronized(mWindowMap) {
8711 losers = mLosingFocus;
8712 mLosingFocus = new ArrayList<WindowState>();
8713 }
8714
8715 final int N = losers.size();
8716 for (int i=0; i<N; i++) {
8717 try {
8718 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8719 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8720 } catch (RemoteException e) {
8721 // Ignore if process has died.
8722 }
8723 }
8724 } break;
8725
8726 case ANIMATE: {
8727 synchronized(mWindowMap) {
8728 mAnimationPending = false;
8729 performLayoutAndPlaceSurfacesLocked();
8730 }
8731 } break;
8732
8733 case ADD_STARTING: {
8734 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8735 final StartingData sd = wtoken.startingData;
8736
8737 if (sd == null) {
8738 // Animation has been canceled... do nothing.
8739 return;
8740 }
Romain Guy06882f82009-06-10 13:36:04 -07008741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008742 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8743 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008745 View view = null;
8746 try {
8747 view = mPolicy.addStartingWindow(
8748 wtoken.token, sd.pkg,
8749 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8750 sd.icon);
8751 } catch (Exception e) {
8752 Log.w(TAG, "Exception when adding starting window", e);
8753 }
8754
8755 if (view != null) {
8756 boolean abort = false;
8757
8758 synchronized(mWindowMap) {
8759 if (wtoken.removed || wtoken.startingData == null) {
8760 // If the window was successfully added, then
8761 // we need to remove it.
8762 if (wtoken.startingWindow != null) {
8763 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8764 "Aborted starting " + wtoken
8765 + ": removed=" + wtoken.removed
8766 + " startingData=" + wtoken.startingData);
8767 wtoken.startingWindow = null;
8768 wtoken.startingData = null;
8769 abort = true;
8770 }
8771 } else {
8772 wtoken.startingView = view;
8773 }
8774 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8775 "Added starting " + wtoken
8776 + ": startingWindow="
8777 + wtoken.startingWindow + " startingView="
8778 + wtoken.startingView);
8779 }
8780
8781 if (abort) {
8782 try {
8783 mPolicy.removeStartingWindow(wtoken.token, view);
8784 } catch (Exception e) {
8785 Log.w(TAG, "Exception when removing starting window", e);
8786 }
8787 }
8788 }
8789 } break;
8790
8791 case REMOVE_STARTING: {
8792 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8793 IBinder token = null;
8794 View view = null;
8795 synchronized (mWindowMap) {
8796 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8797 + wtoken + ": startingWindow="
8798 + wtoken.startingWindow + " startingView="
8799 + wtoken.startingView);
8800 if (wtoken.startingWindow != null) {
8801 view = wtoken.startingView;
8802 token = wtoken.token;
8803 wtoken.startingData = null;
8804 wtoken.startingView = null;
8805 wtoken.startingWindow = null;
8806 }
8807 }
8808 if (view != null) {
8809 try {
8810 mPolicy.removeStartingWindow(token, view);
8811 } catch (Exception e) {
8812 Log.w(TAG, "Exception when removing starting window", e);
8813 }
8814 }
8815 } break;
8816
8817 case FINISHED_STARTING: {
8818 IBinder token = null;
8819 View view = null;
8820 while (true) {
8821 synchronized (mWindowMap) {
8822 final int N = mFinishedStarting.size();
8823 if (N <= 0) {
8824 break;
8825 }
8826 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8827
8828 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8829 "Finished starting " + wtoken
8830 + ": startingWindow=" + wtoken.startingWindow
8831 + " startingView=" + wtoken.startingView);
8832
8833 if (wtoken.startingWindow == null) {
8834 continue;
8835 }
8836
8837 view = wtoken.startingView;
8838 token = wtoken.token;
8839 wtoken.startingData = null;
8840 wtoken.startingView = null;
8841 wtoken.startingWindow = null;
8842 }
8843
8844 try {
8845 mPolicy.removeStartingWindow(token, view);
8846 } catch (Exception e) {
8847 Log.w(TAG, "Exception when removing starting window", e);
8848 }
8849 }
8850 } break;
8851
8852 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8853 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8854
8855 boolean nowVisible = msg.arg1 != 0;
8856 boolean nowGone = msg.arg2 != 0;
8857
8858 try {
8859 if (DEBUG_VISIBILITY) Log.v(
8860 TAG, "Reporting visible in " + wtoken
8861 + " visible=" + nowVisible
8862 + " gone=" + nowGone);
8863 if (nowVisible) {
8864 wtoken.appToken.windowsVisible();
8865 } else {
8866 wtoken.appToken.windowsGone();
8867 }
8868 } catch (RemoteException ex) {
8869 }
8870 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 case WINDOW_FREEZE_TIMEOUT: {
8873 synchronized (mWindowMap) {
8874 Log.w(TAG, "Window freeze timeout expired.");
8875 int i = mWindows.size();
8876 while (i > 0) {
8877 i--;
8878 WindowState w = (WindowState)mWindows.get(i);
8879 if (w.mOrientationChanging) {
8880 w.mOrientationChanging = false;
8881 Log.w(TAG, "Force clearing orientation change: " + w);
8882 }
8883 }
8884 performLayoutAndPlaceSurfacesLocked();
8885 }
8886 break;
8887 }
Romain Guy06882f82009-06-10 13:36:04 -07008888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 case HOLD_SCREEN_CHANGED: {
8890 Session oldHold;
8891 Session newHold;
8892 synchronized (mWindowMap) {
8893 oldHold = mLastReportedHold;
8894 newHold = (Session)msg.obj;
8895 mLastReportedHold = newHold;
8896 }
Romain Guy06882f82009-06-10 13:36:04 -07008897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008898 if (oldHold != newHold) {
8899 try {
8900 if (oldHold != null) {
8901 mBatteryStats.noteStopWakelock(oldHold.mUid,
8902 "window",
8903 BatteryStats.WAKE_TYPE_WINDOW);
8904 }
8905 if (newHold != null) {
8906 mBatteryStats.noteStartWakelock(newHold.mUid,
8907 "window",
8908 BatteryStats.WAKE_TYPE_WINDOW);
8909 }
8910 } catch (RemoteException e) {
8911 }
8912 }
8913 break;
8914 }
Romain Guy06882f82009-06-10 13:36:04 -07008915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 case APP_TRANSITION_TIMEOUT: {
8917 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008918 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008919 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8920 "*** APP TRANSITION TIMEOUT");
8921 mAppTransitionReady = true;
8922 mAppTransitionTimeout = true;
8923 performLayoutAndPlaceSurfacesLocked();
8924 }
8925 }
8926 break;
8927 }
Romain Guy06882f82009-06-10 13:36:04 -07008928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008929 case PERSIST_ANIMATION_SCALE: {
8930 Settings.System.putFloat(mContext.getContentResolver(),
8931 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8932 Settings.System.putFloat(mContext.getContentResolver(),
8933 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8934 break;
8935 }
Romain Guy06882f82009-06-10 13:36:04 -07008936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937 case FORCE_GC: {
8938 synchronized(mWindowMap) {
8939 if (mAnimationPending) {
8940 // If we are animating, don't do the gc now but
8941 // delay a bit so we don't interrupt the animation.
8942 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8943 2000);
8944 return;
8945 }
8946 // If we are currently rotating the display, it will
8947 // schedule a new message when done.
8948 if (mDisplayFrozen) {
8949 return;
8950 }
8951 mFreezeGcPending = 0;
8952 }
8953 Runtime.getRuntime().gc();
8954 break;
8955 }
Romain Guy06882f82009-06-10 13:36:04 -07008956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008957 case ENABLE_SCREEN: {
8958 performEnableScreen();
8959 break;
8960 }
Romain Guy06882f82009-06-10 13:36:04 -07008961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008962 case APP_FREEZE_TIMEOUT: {
8963 synchronized (mWindowMap) {
8964 Log.w(TAG, "App freeze timeout expired.");
8965 int i = mAppTokens.size();
8966 while (i > 0) {
8967 i--;
8968 AppWindowToken tok = mAppTokens.get(i);
8969 if (tok.freezingScreen) {
8970 Log.w(TAG, "Force clearing freeze: " + tok);
8971 unsetAppFreezingScreenLocked(tok, true, true);
8972 }
8973 }
8974 }
8975 break;
8976 }
Romain Guy06882f82009-06-10 13:36:04 -07008977
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008978 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008979 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008980 sendNewConfiguration();
8981 }
8982 break;
8983 }
Romain Guy06882f82009-06-10 13:36:04 -07008984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008985 }
8986 }
8987 }
8988
8989 // -------------------------------------------------------------
8990 // IWindowManager API
8991 // -------------------------------------------------------------
8992
8993 public IWindowSession openSession(IInputMethodClient client,
8994 IInputContext inputContext) {
8995 if (client == null) throw new IllegalArgumentException("null client");
8996 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8997 return new Session(client, inputContext);
8998 }
8999
9000 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9001 synchronized (mWindowMap) {
9002 // The focus for the client is the window immediately below
9003 // where we would place the input method window.
9004 int idx = findDesiredInputMethodWindowIndexLocked(false);
9005 WindowState imFocus;
9006 if (idx > 0) {
9007 imFocus = (WindowState)mWindows.get(idx-1);
9008 if (imFocus != null) {
9009 if (imFocus.mSession.mClient != null &&
9010 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9011 return true;
9012 }
9013 }
9014 }
9015 }
9016 return false;
9017 }
Romain Guy06882f82009-06-10 13:36:04 -07009018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009019 // -------------------------------------------------------------
9020 // Internals
9021 // -------------------------------------------------------------
9022
9023 final WindowState windowForClientLocked(Session session, IWindow client) {
9024 return windowForClientLocked(session, client.asBinder());
9025 }
Romain Guy06882f82009-06-10 13:36:04 -07009026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009027 final WindowState windowForClientLocked(Session session, IBinder client) {
9028 WindowState win = mWindowMap.get(client);
9029 if (localLOGV) Log.v(
9030 TAG, "Looking up client " + client + ": " + win);
9031 if (win == null) {
9032 RuntimeException ex = new RuntimeException();
9033 Log.w(TAG, "Requested window " + client + " does not exist", ex);
9034 return null;
9035 }
9036 if (session != null && win.mSession != session) {
9037 RuntimeException ex = new RuntimeException();
9038 Log.w(TAG, "Requested window " + client + " is in session " +
9039 win.mSession + ", not " + session, ex);
9040 return null;
9041 }
9042
9043 return win;
9044 }
9045
Dianne Hackborna8f60182009-09-01 19:01:50 -07009046 final void rebuildAppWindowListLocked() {
9047 int NW = mWindows.size();
9048 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009049 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009050 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009051
Dianne Hackborna8f60182009-09-01 19:01:50 -07009052 // First remove all existing app windows.
9053 i=0;
9054 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009055 WindowState w = (WindowState)mWindows.get(i);
9056 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009057 WindowState win = (WindowState)mWindows.remove(i);
9058 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
9059 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009060 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009061 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009062 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009063 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9064 && lastWallpaper == i-1) {
9065 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009066 }
9067 i++;
9068 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009069
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009070 // The wallpaper window(s) typically live at the bottom of the stack,
9071 // so skip them before adding app tokens.
9072 lastWallpaper++;
9073 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009074
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009075 // First add all of the exiting app tokens... these are no longer
9076 // in the main app list, but still have windows shown. We put them
9077 // in the back because now that the animation is over we no longer
9078 // will care about them.
9079 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009080 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009081 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9082 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009083
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009084 // And add in the still active app tokens in Z order.
9085 NT = mAppTokens.size();
9086 for (int j=0; j<NT; j++) {
9087 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009089
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009090 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009091 if (i != numRemoved) {
9092 Log.w(TAG, "Rebuild removed " + numRemoved
9093 + " windows but added " + i);
9094 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009097 private final void assignLayersLocked() {
9098 int N = mWindows.size();
9099 int curBaseLayer = 0;
9100 int curLayer = 0;
9101 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009103 for (i=0; i<N; i++) {
9104 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009105 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9106 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009107 curLayer += WINDOW_LAYER_MULTIPLIER;
9108 w.mLayer = curLayer;
9109 } else {
9110 curBaseLayer = curLayer = w.mBaseLayer;
9111 w.mLayer = curLayer;
9112 }
9113 if (w.mTargetAppToken != null) {
9114 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9115 } else if (w.mAppToken != null) {
9116 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9117 } else {
9118 w.mAnimLayer = w.mLayer;
9119 }
9120 if (w.mIsImWindow) {
9121 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009122 } else if (w.mIsWallpaper) {
9123 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009124 }
9125 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9126 + w.mAnimLayer);
9127 //System.out.println(
9128 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9129 }
9130 }
9131
9132 private boolean mInLayout = false;
9133 private final void performLayoutAndPlaceSurfacesLocked() {
9134 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009135 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009136 throw new RuntimeException("Recursive call!");
9137 }
9138 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9139 return;
9140 }
9141
9142 boolean recoveringMemory = false;
9143 if (mForceRemoves != null) {
9144 recoveringMemory = true;
9145 // Wait a little it for things to settle down, and off we go.
9146 for (int i=0; i<mForceRemoves.size(); i++) {
9147 WindowState ws = mForceRemoves.get(i);
9148 Log.i(TAG, "Force removing: " + ws);
9149 removeWindowInnerLocked(ws.mSession, ws);
9150 }
9151 mForceRemoves = null;
9152 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9153 Object tmp = new Object();
9154 synchronized (tmp) {
9155 try {
9156 tmp.wait(250);
9157 } catch (InterruptedException e) {
9158 }
9159 }
9160 }
Romain Guy06882f82009-06-10 13:36:04 -07009161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009162 mInLayout = true;
9163 try {
9164 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009166 int i = mPendingRemove.size()-1;
9167 if (i >= 0) {
9168 while (i >= 0) {
9169 WindowState w = mPendingRemove.get(i);
9170 removeWindowInnerLocked(w.mSession, w);
9171 i--;
9172 }
9173 mPendingRemove.clear();
9174
9175 mInLayout = false;
9176 assignLayersLocked();
9177 mLayoutNeeded = true;
9178 performLayoutAndPlaceSurfacesLocked();
9179
9180 } else {
9181 mInLayout = false;
9182 if (mLayoutNeeded) {
9183 requestAnimationLocked(0);
9184 }
9185 }
9186 } catch (RuntimeException e) {
9187 mInLayout = false;
9188 Log.e(TAG, "Unhandled exception while layout out windows", e);
9189 }
9190 }
9191
9192 private final void performLayoutLockedInner() {
9193 final int dw = mDisplay.getWidth();
9194 final int dh = mDisplay.getHeight();
9195
9196 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009197 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009198 int i;
9199
9200 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009201
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009202 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009203 mPolicy.beginLayoutLw(dw, dh);
9204
9205 // First perform layout of any root windows (not attached
9206 // to another window).
9207 int topAttached = -1;
9208 for (i = N-1; i >= 0; i--) {
9209 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009210
9211 // Don't do layout of a window if it is not visible, or
9212 // soon won't be visible, to avoid wasting time and funky
9213 // changes while a window is animating away.
9214 final AppWindowToken atoken = win.mAppToken;
9215 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009216 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009217 || win.mRootToken.hidden
9218 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009219 || win.mAttachedHidden
9220 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009221
9222 // If this view is GONE, then skip it -- keep the current
9223 // frame, and let the caller know so they can ignore it
9224 // if they want. (We do the normal layout for INVISIBLE
9225 // windows, since that means "perform layout as normal,
9226 // just don't display").
9227 if (!gone || !win.mHaveFrame) {
9228 if (!win.mLayoutAttached) {
9229 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9230 } else {
9231 if (topAttached < 0) topAttached = i;
9232 }
9233 }
9234 }
Romain Guy06882f82009-06-10 13:36:04 -07009235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009236 // Now perform layout of attached windows, which usually
9237 // depend on the position of the window they are attached to.
9238 // XXX does not deal with windows that are attached to windows
9239 // that are themselves attached.
9240 for (i = topAttached; i >= 0; i--) {
9241 WindowState win = (WindowState) mWindows.get(i);
9242
9243 // If this view is GONE, then skip it -- keep the current
9244 // frame, and let the caller know so they can ignore it
9245 // if they want. (We do the normal layout for INVISIBLE
9246 // windows, since that means "perform layout as normal,
9247 // just don't display").
9248 if (win.mLayoutAttached) {
9249 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9250 || !win.mHaveFrame) {
9251 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9252 }
9253 }
9254 }
9255
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009256 int changes = mPolicy.finishLayoutLw();
9257 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9258 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9259 assignLayersLocked();
9260 }
9261 }
9262 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009263 mLayoutNeeded = false;
9264 } else if (repeats > 2) {
9265 Log.w(TAG, "Layout repeat aborted after too many iterations");
9266 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009267 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9268 Configuration newConfig = updateOrientationFromAppTokensLocked(
9269 null, null);
9270 if (newConfig != null) {
9271 mLayoutNeeded = true;
9272 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9273 }
9274 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009275 } else {
9276 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009277 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9278 Configuration newConfig = updateOrientationFromAppTokensLocked(
9279 null, null);
9280 if (newConfig != null) {
9281 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9282 }
9283 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009285 }
9286 }
Romain Guy06882f82009-06-10 13:36:04 -07009287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009288 private final void performLayoutAndPlaceSurfacesLockedInner(
9289 boolean recoveringMemory) {
9290 final long currentTime = SystemClock.uptimeMillis();
9291 final int dw = mDisplay.getWidth();
9292 final int dh = mDisplay.getHeight();
9293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009294 int i;
9295
9296 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009297 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009299 if (mFxSession == null) {
9300 mFxSession = new SurfaceSession();
9301 }
Romain Guy06882f82009-06-10 13:36:04 -07009302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009303 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9304
9305 // Initialize state of exiting tokens.
9306 for (i=mExitingTokens.size()-1; i>=0; i--) {
9307 mExitingTokens.get(i).hasVisible = false;
9308 }
9309
9310 // Initialize state of exiting applications.
9311 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9312 mExitingAppTokens.get(i).hasVisible = false;
9313 }
9314
9315 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009316 boolean orientationChangeComplete = true;
9317 Session holdScreen = null;
9318 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009319 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009320 boolean focusDisplayed = false;
9321 boolean animating = false;
9322
9323 Surface.openTransaction();
9324 try {
9325 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009326 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327
9328 do {
9329 final int transactionSequence = ++mTransactionSequence;
9330
9331 // Update animations of all applications, including those
9332 // associated with exiting/removed apps
9333 boolean tokensAnimating = false;
9334 final int NAT = mAppTokens.size();
9335 for (i=0; i<NAT; i++) {
9336 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9337 tokensAnimating = true;
9338 }
9339 }
9340 final int NEAT = mExitingAppTokens.size();
9341 for (i=0; i<NEAT; i++) {
9342 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9343 tokensAnimating = true;
9344 }
9345 }
9346
9347 animating = tokensAnimating;
9348 restart = false;
9349
9350 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009351 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009352 boolean focusMayChange = false;
9353 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009354
9355 mPolicy.beginAnimationLw(dw, dh);
9356
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009357 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009359 for (i=N-1; i>=0; i--) {
9360 WindowState w = (WindowState)mWindows.get(i);
9361
9362 final WindowManager.LayoutParams attrs = w.mAttrs;
9363
9364 if (w.mSurface != null) {
9365 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009366 if (w.commitFinishDrawingLocked(currentTime)) {
9367 if ((w.mAttrs.flags
9368 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009369 if (DEBUG_WALLPAPER) Log.v(TAG,
9370 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009371 wallpaperMayChange = true;
9372 }
9373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009374
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009375 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009376 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9377 animating = true;
9378 //w.dump(" ");
9379 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009380 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9381 wallpaperMayChange = true;
9382 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009383
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009384 if (mPolicy.doesForceHide(w, attrs)) {
9385 if (!wasAnimating && animating) {
9386 wallpaperForceHidingChanged = true;
9387 focusMayChange = true;
9388 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9389 forceHiding = true;
9390 }
9391 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9392 boolean changed;
9393 if (forceHiding) {
9394 changed = w.hideLw(false, false);
9395 } else {
9396 changed = w.showLw(false, false);
9397 if (changed && wallpaperForceHidingChanged
9398 && w.isReadyForDisplay()) {
9399 // Assume we will need to animate. If
9400 // we don't (because the wallpaper will
9401 // stay with the lock screen), then we will
9402 // clean up later.
9403 Animation a = mPolicy.createForceHideEnterAnimation();
9404 if (a != null) {
9405 w.setAnimation(a);
9406 }
9407 }
9408 }
9409 if (changed && (attrs.flags
9410 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9411 wallpaperMayChange = true;
9412 }
9413 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 mPolicy.animatingWindowLw(w, attrs);
9416 }
9417
9418 final AppWindowToken atoken = w.mAppToken;
9419 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9420 if (atoken.lastTransactionSequence != transactionSequence) {
9421 atoken.lastTransactionSequence = transactionSequence;
9422 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9423 atoken.startingDisplayed = false;
9424 }
9425 if ((w.isOnScreen() || w.mAttrs.type
9426 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9427 && !w.mExiting && !w.mDestroying) {
9428 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009429 Log.v(TAG, "Eval win " + w + ": isDrawn="
9430 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009432 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009433 Log.v(TAG, "Not displayed: s=" + w.mSurface
9434 + " pv=" + w.mPolicyVisibility
9435 + " dp=" + w.mDrawPending
9436 + " cdp=" + w.mCommitDrawPending
9437 + " ah=" + w.mAttachedHidden
9438 + " th=" + atoken.hiddenRequested
9439 + " a=" + w.mAnimating);
9440 }
9441 }
9442 if (w != atoken.startingWindow) {
9443 if (!atoken.freezingScreen || !w.mAppFreezing) {
9444 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009445 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009446 atoken.numDrawnWindows++;
9447 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9448 "tokenMayBeDrawn: " + atoken
9449 + " freezingScreen=" + atoken.freezingScreen
9450 + " mAppFreezing=" + w.mAppFreezing);
9451 tokenMayBeDrawn = true;
9452 }
9453 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009454 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 atoken.startingDisplayed = true;
9456 }
9457 }
9458 } else if (w.mReadyToShow) {
9459 w.performShowLocked();
9460 }
9461 }
9462
9463 if (mPolicy.finishAnimationLw()) {
9464 restart = true;
9465 }
9466
9467 if (tokenMayBeDrawn) {
9468 // See if any windows have been drawn, so they (and others
9469 // associated with them) can now be shown.
9470 final int NT = mTokenList.size();
9471 for (i=0; i<NT; i++) {
9472 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9473 if (wtoken == null) {
9474 continue;
9475 }
9476 if (wtoken.freezingScreen) {
9477 int numInteresting = wtoken.numInterestingWindows;
9478 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9479 if (DEBUG_VISIBILITY) Log.v(TAG,
9480 "allDrawn: " + wtoken
9481 + " interesting=" + numInteresting
9482 + " drawn=" + wtoken.numDrawnWindows);
9483 wtoken.showAllWindowsLocked();
9484 unsetAppFreezingScreenLocked(wtoken, false, true);
9485 orientationChangeComplete = true;
9486 }
9487 } else if (!wtoken.allDrawn) {
9488 int numInteresting = wtoken.numInterestingWindows;
9489 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9490 if (DEBUG_VISIBILITY) Log.v(TAG,
9491 "allDrawn: " + wtoken
9492 + " interesting=" + numInteresting
9493 + " drawn=" + wtoken.numDrawnWindows);
9494 wtoken.allDrawn = true;
9495 restart = true;
9496
9497 // We can now show all of the drawn windows!
9498 if (!mOpeningApps.contains(wtoken)) {
9499 wtoken.showAllWindowsLocked();
9500 }
9501 }
9502 }
9503 }
9504 }
9505
9506 // If we are ready to perform an app transition, check through
9507 // all of the app tokens to be shown and see if they are ready
9508 // to go.
9509 if (mAppTransitionReady) {
9510 int NN = mOpeningApps.size();
9511 boolean goodToGo = true;
9512 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9513 "Checking " + NN + " opening apps (frozen="
9514 + mDisplayFrozen + " timeout="
9515 + mAppTransitionTimeout + ")...");
9516 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9517 // If the display isn't frozen, wait to do anything until
9518 // all of the apps are ready. Otherwise just go because
9519 // we'll unfreeze the display when everyone is ready.
9520 for (i=0; i<NN && goodToGo; i++) {
9521 AppWindowToken wtoken = mOpeningApps.get(i);
9522 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9523 "Check opening app" + wtoken + ": allDrawn="
9524 + wtoken.allDrawn + " startingDisplayed="
9525 + wtoken.startingDisplayed);
9526 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9527 && !wtoken.startingMoved) {
9528 goodToGo = false;
9529 }
9530 }
9531 }
9532 if (goodToGo) {
9533 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9534 int transit = mNextAppTransition;
9535 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009536 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009537 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009538 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009540 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009541 mAppTransitionTimeout = false;
9542 mStartingIconInTransition = false;
9543 mSkipAppTransitionAnimation = false;
9544
9545 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9546
Dianne Hackborna8f60182009-09-01 19:01:50 -07009547 // If there are applications waiting to come to the
9548 // top of the stack, now is the time to move their windows.
9549 // (Note that we don't do apps going to the bottom
9550 // here -- we want to keep their windows in the old
9551 // Z-order until the animation completes.)
9552 if (mToTopApps.size() > 0) {
9553 NN = mAppTokens.size();
9554 for (i=0; i<NN; i++) {
9555 AppWindowToken wtoken = mAppTokens.get(i);
9556 if (wtoken.sendingToTop) {
9557 wtoken.sendingToTop = false;
9558 moveAppWindowsLocked(wtoken, NN, false);
9559 }
9560 }
9561 mToTopApps.clear();
9562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009563
Dianne Hackborn25994b42009-09-04 14:21:19 -07009564 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009565
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009566 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009567 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009568
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009569 // The top-most window will supply the layout params,
9570 // and we will determine it below.
9571 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009572 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009573 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009574
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009575 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009576 "New wallpaper target=" + mWallpaperTarget
9577 + ", lower target=" + mLowerWallpaperTarget
9578 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009579 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009580 // Do a first pass through the tokens for two
9581 // things:
9582 // (1) Determine if both the closing and opening
9583 // app token sets are wallpaper targets, in which
9584 // case special animations are needed
9585 // (since the wallpaper needs to stay static
9586 // behind them).
9587 // (2) Find the layout params of the top-most
9588 // application window in the tokens, which is
9589 // what will control the animation theme.
9590 final int NC = mClosingApps.size();
9591 NN = NC + mOpeningApps.size();
9592 for (i=0; i<NN; i++) {
9593 AppWindowToken wtoken;
9594 int mode;
9595 if (i < NC) {
9596 wtoken = mClosingApps.get(i);
9597 mode = 1;
9598 } else {
9599 wtoken = mOpeningApps.get(i-NC);
9600 mode = 2;
9601 }
9602 if (mLowerWallpaperTarget != null) {
9603 if (mLowerWallpaperTarget.mAppToken == wtoken
9604 || mUpperWallpaperTarget.mAppToken == wtoken) {
9605 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009606 }
9607 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009608 if (wtoken.appFullscreen) {
9609 WindowState ws = wtoken.findMainWindow();
9610 if (ws != null) {
9611 // If this is a compatibility mode
9612 // window, we will always use its anim.
9613 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9614 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009615 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009616 bestAnimLayer = Integer.MAX_VALUE;
9617 } else if (ws.mLayer > bestAnimLayer) {
9618 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009619 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009620 bestAnimLayer = ws.mLayer;
9621 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009622 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009623 }
9624 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009625
Dianne Hackborn25994b42009-09-04 14:21:19 -07009626 if (foundWallpapers == 3) {
9627 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9628 "Wallpaper animation!");
9629 switch (transit) {
9630 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9631 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9632 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9633 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9634 break;
9635 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9636 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9637 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9638 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9639 break;
9640 }
9641 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9642 "New transit: " + transit);
9643 } else if (oldWallpaper != null) {
9644 // We are transitioning from an activity with
9645 // a wallpaper to one without.
9646 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9647 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9648 "New transit away from wallpaper: " + transit);
9649 } else if (mWallpaperTarget != null) {
9650 // We are transitioning from an activity without
9651 // a wallpaper to now showing the wallpaper
9652 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9653 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9654 "New transit into wallpaper: " + transit);
9655 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009656
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009657 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9658 mLastEnterAnimToken = animToken;
9659 mLastEnterAnimParams = animLp;
9660 } else if (mLastEnterAnimParams != null) {
9661 animLp = mLastEnterAnimParams;
9662 mLastEnterAnimToken = null;
9663 mLastEnterAnimParams = null;
9664 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009666 NN = mOpeningApps.size();
9667 for (i=0; i<NN; i++) {
9668 AppWindowToken wtoken = mOpeningApps.get(i);
9669 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9670 "Now opening app" + wtoken);
9671 wtoken.reportedVisible = false;
9672 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009673 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009674 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009675 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009676 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009677 wtoken.showAllWindowsLocked();
9678 }
9679 NN = mClosingApps.size();
9680 for (i=0; i<NN; i++) {
9681 AppWindowToken wtoken = mClosingApps.get(i);
9682 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9683 "Now closing app" + wtoken);
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, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009687 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009688 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 // Force the allDrawn flag, because we want to start
9690 // this guy's animations regardless of whether it's
9691 // gotten drawn.
9692 wtoken.allDrawn = true;
9693 }
9694
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009695 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009697 mOpeningApps.clear();
9698 mClosingApps.clear();
9699
9700 // This has changed the visibility of windows, so perform
9701 // a new layout to get them all up-to-date.
9702 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009703 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9704 assignLayersLocked();
9705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009706 performLayoutLockedInner();
9707 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009708 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009709
9710 restart = true;
9711 }
9712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009713
Dianne Hackborna8f60182009-09-01 19:01:50 -07009714 if (!animating && mAppTransitionRunning) {
9715 // We have finished the animation of an app transition. To do
9716 // this, we have delayed a lot of operations like showing and
9717 // hiding apps, moving apps in Z-order, etc. The app token list
9718 // reflects the correct Z-order, but the window list may now
9719 // be out of sync with it. So here we will just rebuild the
9720 // entire app window list. Fun!
9721 mAppTransitionRunning = false;
9722 // Clear information about apps that were moving.
9723 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009724
Dianne Hackborna8f60182009-09-01 19:01:50 -07009725 rebuildAppWindowListLocked();
9726 restart = true;
9727 moveInputMethodWindowsIfNeededLocked(false);
9728 wallpaperMayChange = true;
9729 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009730 // Since the window list has been rebuilt, focus might
9731 // have to be recomputed since the actual order of windows
9732 // might have changed again.
9733 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009735
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009736 int adjResult = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009737
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009738 if (wallpaperForceHidingChanged) {
9739 // At this point, there was a window with a wallpaper that
9740 // was force hiding other windows behind it, but now it
9741 // is going away. This may be simple -- just animate
9742 // away the wallpaper and its window -- or it may be
9743 // hard -- the wallpaper now needs to be shown behind
9744 // something that was hidden.
9745 WindowState oldWallpaper = mWallpaperTarget;
9746 adjResult = adjustWallpaperWindowsLocked();
9747 wallpaperMayChange = false;
9748 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9749 + " NEW: " + mWallpaperTarget);
9750 if (mLowerWallpaperTarget == null) {
9751 // Whoops, we don't need a special wallpaper animation.
9752 // Clear them out.
9753 forceHiding = false;
9754 for (i=N-1; i>=0; i--) {
9755 WindowState w = (WindowState)mWindows.get(i);
9756 if (w.mSurface != null) {
9757 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009758 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9759 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009760 forceHiding = true;
9761 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9762 if (!w.mAnimating) {
9763 // We set the animation above so it
9764 // is not yet running.
9765 w.clearAnimation();
9766 }
9767 }
9768 }
9769 }
9770 }
9771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009772
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009773 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009774 if (DEBUG_WALLPAPER) Log.v(TAG,
9775 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009776 adjResult = adjustWallpaperWindowsLocked();
9777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009778
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009779 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9780 if (DEBUG_WALLPAPER) Log.v(TAG,
9781 "Wallpaper layer changed: assigning layers + relayout");
9782 restart = true;
9783 mLayoutNeeded = true;
9784 assignLayersLocked();
9785 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9786 if (DEBUG_WALLPAPER) Log.v(TAG,
9787 "Wallpaper visibility changed: relayout");
9788 restart = true;
9789 mLayoutNeeded = true;
9790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009791
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009792 if (focusMayChange) {
9793 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009794 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009795 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009796 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009797 }
9798
9799 if (mLayoutNeeded) {
9800 restart = true;
9801 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009802 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009804 } while (restart);
9805
9806 // THIRD LOOP: Update the surfaces of all windows.
9807
9808 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9809
9810 boolean obscured = false;
9811 boolean blurring = false;
9812 boolean dimming = false;
9813 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009814 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009815 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009817 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 for (i=N-1; i>=0; i--) {
9820 WindowState w = (WindowState)mWindows.get(i);
9821
9822 boolean displayed = false;
9823 final WindowManager.LayoutParams attrs = w.mAttrs;
9824 final int attrFlags = attrs.flags;
9825
9826 if (w.mSurface != null) {
9827 w.computeShownFrameLocked();
9828 if (localLOGV) Log.v(
9829 TAG, "Placing surface #" + i + " " + w.mSurface
9830 + ": new=" + w.mShownFrame + ", old="
9831 + w.mLastShownFrame);
9832
9833 boolean resize;
9834 int width, height;
9835 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9836 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9837 w.mLastRequestedHeight != w.mRequestedHeight;
9838 // for a scaled surface, we just want to use
9839 // the requested size.
9840 width = w.mRequestedWidth;
9841 height = w.mRequestedHeight;
9842 w.mLastRequestedWidth = width;
9843 w.mLastRequestedHeight = height;
9844 w.mLastShownFrame.set(w.mShownFrame);
9845 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009846 if (SHOW_TRANSACTIONS) Log.i(
9847 TAG, " SURFACE " + w.mSurface
9848 + ": POS " + w.mShownFrame.left
9849 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009850 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9851 } catch (RuntimeException e) {
9852 Log.w(TAG, "Error positioning surface in " + w, e);
9853 if (!recoveringMemory) {
9854 reclaimSomeSurfaceMemoryLocked(w, "position");
9855 }
9856 }
9857 } else {
9858 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9859 width = w.mShownFrame.width();
9860 height = w.mShownFrame.height();
9861 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009862 }
9863
9864 if (resize) {
9865 if (width < 1) width = 1;
9866 if (height < 1) height = 1;
9867 if (w.mSurface != null) {
9868 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009869 if (SHOW_TRANSACTIONS) Log.i(
9870 TAG, " SURFACE " + w.mSurface + ": POS "
9871 + w.mShownFrame.left + ","
9872 + w.mShownFrame.top + " SIZE "
9873 + w.mShownFrame.width() + "x"
9874 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009875 w.mSurface.setSize(width, height);
9876 w.mSurface.setPosition(w.mShownFrame.left,
9877 w.mShownFrame.top);
9878 } catch (RuntimeException e) {
9879 // If something goes wrong with the surface (such
9880 // as running out of memory), don't take down the
9881 // entire system.
9882 Log.e(TAG, "Failure updating surface of " + w
9883 + "size=(" + width + "x" + height
9884 + "), pos=(" + w.mShownFrame.left
9885 + "," + w.mShownFrame.top + ")", e);
9886 if (!recoveringMemory) {
9887 reclaimSomeSurfaceMemoryLocked(w, "size");
9888 }
9889 }
9890 }
9891 }
9892 if (!w.mAppFreezing) {
9893 w.mContentInsetsChanged =
9894 !w.mLastContentInsets.equals(w.mContentInsets);
9895 w.mVisibleInsetsChanged =
9896 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009897 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009898 || w.mContentInsetsChanged
9899 || w.mVisibleInsetsChanged) {
9900 w.mLastFrame.set(w.mFrame);
9901 w.mLastContentInsets.set(w.mContentInsets);
9902 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009903 // If the screen is currently frozen, then keep
9904 // it frozen until this window draws at its new
9905 // orientation.
9906 if (mDisplayFrozen) {
9907 if (DEBUG_ORIENTATION) Log.v(TAG,
9908 "Resizing while display frozen: " + w);
9909 w.mOrientationChanging = true;
9910 if (mWindowsFreezingScreen) {
9911 mWindowsFreezingScreen = true;
9912 // XXX should probably keep timeout from
9913 // when we first froze the display.
9914 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9915 mH.sendMessageDelayed(mH.obtainMessage(
9916 H.WINDOW_FREEZE_TIMEOUT), 2000);
9917 }
9918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 // If the orientation is changing, then we need to
9920 // hold off on unfreezing the display until this
9921 // window has been redrawn; to do that, we need
9922 // to go through the process of getting informed
9923 // by the application when it has finished drawing.
9924 if (w.mOrientationChanging) {
9925 if (DEBUG_ORIENTATION) Log.v(TAG,
9926 "Orientation start waiting for draw in "
9927 + w + ", surface " + w.mSurface);
9928 w.mDrawPending = true;
9929 w.mCommitDrawPending = false;
9930 w.mReadyToShow = false;
9931 if (w.mAppToken != null) {
9932 w.mAppToken.allDrawn = false;
9933 }
9934 }
Romain Guy06882f82009-06-10 13:36:04 -07009935 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 "Resizing window " + w + " to " + w.mFrame);
9937 mResizingWindows.add(w);
9938 } else if (w.mOrientationChanging) {
9939 if (!w.mDrawPending && !w.mCommitDrawPending) {
9940 if (DEBUG_ORIENTATION) Log.v(TAG,
9941 "Orientation not waiting for draw in "
9942 + w + ", surface " + w.mSurface);
9943 w.mOrientationChanging = false;
9944 }
9945 }
9946 }
9947
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009948 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009949 if (!w.mLastHidden) {
9950 //dump();
9951 w.mLastHidden = true;
9952 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009953 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 if (w.mSurface != null) {
9955 try {
9956 w.mSurface.hide();
9957 } catch (RuntimeException e) {
9958 Log.w(TAG, "Exception hiding surface in " + w);
9959 }
9960 }
9961 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9962 }
9963 // If we are waiting for this window to handle an
9964 // orientation change, well, it is hidden, so
9965 // doesn't really matter. Note that this does
9966 // introduce a potential glitch if the window
9967 // becomes unhidden before it has drawn for the
9968 // new orientation.
9969 if (w.mOrientationChanging) {
9970 w.mOrientationChanging = false;
9971 if (DEBUG_ORIENTATION) Log.v(TAG,
9972 "Orientation change skips hidden " + w);
9973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009974 } else if (w.mLastLayer != w.mAnimLayer
9975 || w.mLastAlpha != w.mShownAlpha
9976 || w.mLastDsDx != w.mDsDx
9977 || w.mLastDtDx != w.mDtDx
9978 || w.mLastDsDy != w.mDsDy
9979 || w.mLastDtDy != w.mDtDy
9980 || w.mLastHScale != w.mHScale
9981 || w.mLastVScale != w.mVScale
9982 || w.mLastHidden) {
9983 displayed = true;
9984 w.mLastAlpha = w.mShownAlpha;
9985 w.mLastLayer = w.mAnimLayer;
9986 w.mLastDsDx = w.mDsDx;
9987 w.mLastDtDx = w.mDtDx;
9988 w.mLastDsDy = w.mDsDy;
9989 w.mLastDtDy = w.mDtDy;
9990 w.mLastHScale = w.mHScale;
9991 w.mLastVScale = w.mVScale;
9992 if (SHOW_TRANSACTIONS) Log.i(
9993 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009994 + w.mShownAlpha + " layer=" + w.mAnimLayer
9995 + " matrix=[" + (w.mDsDx*w.mHScale)
9996 + "," + (w.mDtDx*w.mVScale)
9997 + "][" + (w.mDsDy*w.mHScale)
9998 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999 if (w.mSurface != null) {
10000 try {
10001 w.mSurface.setAlpha(w.mShownAlpha);
10002 w.mSurface.setLayer(w.mAnimLayer);
10003 w.mSurface.setMatrix(
10004 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10005 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10006 } catch (RuntimeException e) {
10007 Log.w(TAG, "Error updating surface in " + w, e);
10008 if (!recoveringMemory) {
10009 reclaimSomeSurfaceMemoryLocked(w, "update");
10010 }
10011 }
10012 }
10013
10014 if (w.mLastHidden && !w.mDrawPending
10015 && !w.mCommitDrawPending
10016 && !w.mReadyToShow) {
10017 if (SHOW_TRANSACTIONS) Log.i(
10018 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
10019 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
10020 + " during relayout");
10021 if (showSurfaceRobustlyLocked(w)) {
10022 w.mHasDrawn = true;
10023 w.mLastHidden = false;
10024 } else {
10025 w.mOrientationChanging = false;
10026 }
10027 }
10028 if (w.mSurface != null) {
10029 w.mToken.hasVisible = true;
10030 }
10031 } else {
10032 displayed = true;
10033 }
10034
10035 if (displayed) {
10036 if (!covered) {
10037 if (attrs.width == LayoutParams.FILL_PARENT
10038 && attrs.height == LayoutParams.FILL_PARENT) {
10039 covered = true;
10040 }
10041 }
10042 if (w.mOrientationChanging) {
10043 if (w.mDrawPending || w.mCommitDrawPending) {
10044 orientationChangeComplete = false;
10045 if (DEBUG_ORIENTATION) Log.v(TAG,
10046 "Orientation continue waiting for draw in " + w);
10047 } else {
10048 w.mOrientationChanging = false;
10049 if (DEBUG_ORIENTATION) Log.v(TAG,
10050 "Orientation change complete in " + w);
10051 }
10052 }
10053 w.mToken.hasVisible = true;
10054 }
10055 } else if (w.mOrientationChanging) {
10056 if (DEBUG_ORIENTATION) Log.v(TAG,
10057 "Orientation change skips hidden " + w);
10058 w.mOrientationChanging = false;
10059 }
10060
10061 final boolean canBeSeen = w.isDisplayedLw();
10062
10063 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10064 focusDisplayed = true;
10065 }
10066
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010067 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010069 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010070 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010071 if (w.mSurface != null) {
10072 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10073 holdScreen = w.mSession;
10074 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010075 if (!syswin && w.mAttrs.screenBrightness >= 0
10076 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010077 screenBrightness = w.mAttrs.screenBrightness;
10078 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010079 if (!syswin && w.mAttrs.buttonBrightness >= 0
10080 && buttonBrightness < 0) {
10081 buttonBrightness = w.mAttrs.buttonBrightness;
10082 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010083 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10084 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10085 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10086 syswin = true;
10087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010088 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010089
Dianne Hackborn25994b42009-09-04 14:21:19 -070010090 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10091 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010092 // This window completely covers everything behind it,
10093 // so we want to leave all of them as unblurred (for
10094 // performance reasons).
10095 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010096 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
10097 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010098 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010099 obscured = true;
10100 if (mBackgroundFillerSurface == null) {
10101 try {
10102 mBackgroundFillerSurface = new Surface(mFxSession, 0,
10103 0, dw, dh,
10104 PixelFormat.OPAQUE,
10105 Surface.FX_SURFACE_NORMAL);
10106 } catch (Exception e) {
10107 Log.e(TAG, "Exception creating filler surface", e);
10108 }
10109 }
10110 try {
10111 mBackgroundFillerSurface.setPosition(0, 0);
10112 mBackgroundFillerSurface.setSize(dw, dh);
10113 // Using the same layer as Dim because they will never be shown at the
10114 // same time.
10115 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10116 mBackgroundFillerSurface.show();
10117 } catch (RuntimeException e) {
10118 Log.e(TAG, "Exception showing filler surface");
10119 }
10120 backgroundFillerShown = true;
10121 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010122 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010123 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10124 if (localLOGV) Log.v(TAG, "Win " + w
10125 + ": blurring=" + blurring
10126 + " obscured=" + obscured
10127 + " displayed=" + displayed);
10128 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10129 if (!dimming) {
10130 //Log.i(TAG, "DIM BEHIND: " + w);
10131 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010132 if (mDimAnimator == null) {
10133 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010134 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010135 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010136 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010137 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010138 }
10139 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10140 if (!blurring) {
10141 //Log.i(TAG, "BLUR BEHIND: " + w);
10142 blurring = true;
10143 mBlurShown = true;
10144 if (mBlurSurface == null) {
10145 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10146 + mBlurSurface + ": CREATE");
10147 try {
Romain Guy06882f82009-06-10 13:36:04 -070010148 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010149 -1, 16, 16,
10150 PixelFormat.OPAQUE,
10151 Surface.FX_SURFACE_BLUR);
10152 } catch (Exception e) {
10153 Log.e(TAG, "Exception creating Blur surface", e);
10154 }
10155 }
10156 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10157 + mBlurSurface + ": SHOW pos=(0,0) (" +
10158 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10159 if (mBlurSurface != null) {
10160 mBlurSurface.setPosition(0, 0);
10161 mBlurSurface.setSize(dw, dh);
10162 try {
10163 mBlurSurface.show();
10164 } catch (RuntimeException e) {
10165 Log.w(TAG, "Failure showing blur surface", e);
10166 }
10167 }
10168 }
10169 mBlurSurface.setLayer(w.mAnimLayer-2);
10170 }
10171 }
10172 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010173
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010174 if (obscuredChanged && mWallpaperTarget == w) {
10175 // This is the wallpaper target and its obscured state
10176 // changed... make sure the current wallaper's visibility
10177 // has been updated accordingly.
10178 updateWallpaperVisibilityLocked();
10179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010181
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010182 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10183 mBackgroundFillerShown = false;
10184 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10185 try {
10186 mBackgroundFillerSurface.hide();
10187 } catch (RuntimeException e) {
10188 Log.e(TAG, "Exception hiding filler surface", e);
10189 }
10190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010191
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010192 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10193 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010194 }
Romain Guy06882f82009-06-10 13:36:04 -070010195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010196 if (!blurring && mBlurShown) {
10197 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10198 + ": HIDE");
10199 try {
10200 mBlurSurface.hide();
10201 } catch (IllegalArgumentException e) {
10202 Log.w(TAG, "Illegal argument exception hiding blur surface");
10203 }
10204 mBlurShown = false;
10205 }
10206
10207 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10208 } catch (RuntimeException e) {
10209 Log.e(TAG, "Unhandled exception in Window Manager", e);
10210 }
10211
10212 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010214 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10215 "With display frozen, orientationChangeComplete="
10216 + orientationChangeComplete);
10217 if (orientationChangeComplete) {
10218 if (mWindowsFreezingScreen) {
10219 mWindowsFreezingScreen = false;
10220 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10221 }
10222 if (mAppsFreezingScreen == 0) {
10223 stopFreezingDisplayLocked();
10224 }
10225 }
Romain Guy06882f82009-06-10 13:36:04 -070010226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010227 i = mResizingWindows.size();
10228 if (i > 0) {
10229 do {
10230 i--;
10231 WindowState win = mResizingWindows.get(i);
10232 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010233 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10234 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 win.mClient.resized(win.mFrame.width(),
10236 win.mFrame.height(), win.mLastContentInsets,
10237 win.mLastVisibleInsets, win.mDrawPending);
10238 win.mContentInsetsChanged = false;
10239 win.mVisibleInsetsChanged = false;
10240 } catch (RemoteException e) {
10241 win.mOrientationChanging = false;
10242 }
10243 } while (i > 0);
10244 mResizingWindows.clear();
10245 }
Romain Guy06882f82009-06-10 13:36:04 -070010246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010247 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010248 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010249 i = mDestroySurface.size();
10250 if (i > 0) {
10251 do {
10252 i--;
10253 WindowState win = mDestroySurface.get(i);
10254 win.mDestroying = false;
10255 if (mInputMethodWindow == win) {
10256 mInputMethodWindow = null;
10257 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010258 if (win == mWallpaperTarget) {
10259 wallpaperDestroyed = true;
10260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010261 win.destroySurfaceLocked();
10262 } while (i > 0);
10263 mDestroySurface.clear();
10264 }
10265
10266 // Time to remove any exiting tokens?
10267 for (i=mExitingTokens.size()-1; i>=0; i--) {
10268 WindowToken token = mExitingTokens.get(i);
10269 if (!token.hasVisible) {
10270 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010271 if (token.windowType == TYPE_WALLPAPER) {
10272 mWallpaperTokens.remove(token);
10273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010274 }
10275 }
10276
10277 // Time to remove any exiting applications?
10278 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10279 AppWindowToken token = mExitingAppTokens.get(i);
10280 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010281 // Make sure there is no animation running on this token,
10282 // so any windows associated with it will be removed as
10283 // soon as their animations are complete
10284 token.animation = null;
10285 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010286 mAppTokens.remove(token);
10287 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010288 if (mLastEnterAnimToken == token) {
10289 mLastEnterAnimToken = null;
10290 mLastEnterAnimParams = null;
10291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010292 }
10293 }
10294
Dianne Hackborna8f60182009-09-01 19:01:50 -070010295 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010296
Dianne Hackborna8f60182009-09-01 19:01:50 -070010297 if (!animating && mAppTransitionRunning) {
10298 // We have finished the animation of an app transition. To do
10299 // this, we have delayed a lot of operations like showing and
10300 // hiding apps, moving apps in Z-order, etc. The app token list
10301 // reflects the correct Z-order, but the window list may now
10302 // be out of sync with it. So here we will just rebuild the
10303 // entire app window list. Fun!
10304 mAppTransitionRunning = false;
10305 needRelayout = true;
10306 rebuildAppWindowListLocked();
10307 // Clear information about apps that were moving.
10308 mToBottomApps.clear();
10309 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010311 if (focusDisplayed) {
10312 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10313 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010314 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010315 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010316 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010317 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010318 requestAnimationLocked(0);
10319 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10321 }
10322 mQueue.setHoldScreenLocked(holdScreen != null);
10323 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10324 mPowerManager.setScreenBrightnessOverride(-1);
10325 } else {
10326 mPowerManager.setScreenBrightnessOverride((int)
10327 (screenBrightness * Power.BRIGHTNESS_ON));
10328 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010329 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10330 mPowerManager.setButtonBrightnessOverride(-1);
10331 } else {
10332 mPowerManager.setButtonBrightnessOverride((int)
10333 (buttonBrightness * Power.BRIGHTNESS_ON));
10334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 if (holdScreen != mHoldingScreenOn) {
10336 mHoldingScreenOn = holdScreen;
10337 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10338 mH.sendMessage(m);
10339 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010340
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010341 if (mTurnOnScreen) {
10342 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10343 LocalPowerManager.BUTTON_EVENT, true);
10344 mTurnOnScreen = false;
10345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010346 }
10347
10348 void requestAnimationLocked(long delay) {
10349 if (!mAnimationPending) {
10350 mAnimationPending = true;
10351 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10352 }
10353 }
Romain Guy06882f82009-06-10 13:36:04 -070010354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010355 /**
10356 * Have the surface flinger show a surface, robustly dealing with
10357 * error conditions. In particular, if there is not enough memory
10358 * to show the surface, then we will try to get rid of other surfaces
10359 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010360 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 * @return Returns true if the surface was successfully shown.
10362 */
10363 boolean showSurfaceRobustlyLocked(WindowState win) {
10364 try {
10365 if (win.mSurface != null) {
10366 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010367 if (win.mTurnOnScreen) {
10368 win.mTurnOnScreen = false;
10369 mTurnOnScreen = true;
10370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010371 }
10372 return true;
10373 } catch (RuntimeException e) {
10374 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10375 }
Romain Guy06882f82009-06-10 13:36:04 -070010376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 return false;
10380 }
Romain Guy06882f82009-06-10 13:36:04 -070010381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10383 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010384
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010385 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010388 if (mForceRemoves == null) {
10389 mForceRemoves = new ArrayList<WindowState>();
10390 }
Romain Guy06882f82009-06-10 13:36:04 -070010391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010392 long callingIdentity = Binder.clearCallingIdentity();
10393 try {
10394 // There was some problem... first, do a sanity check of the
10395 // window list to make sure we haven't left any dangling surfaces
10396 // around.
10397 int N = mWindows.size();
10398 boolean leakedSurface = false;
10399 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10400 for (int i=0; i<N; i++) {
10401 WindowState ws = (WindowState)mWindows.get(i);
10402 if (ws.mSurface != null) {
10403 if (!mSessions.contains(ws.mSession)) {
10404 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10405 + ws + " surface=" + ws.mSurface
10406 + " token=" + win.mToken
10407 + " pid=" + ws.mSession.mPid
10408 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010409 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 ws.mSurface = null;
10411 mForceRemoves.add(ws);
10412 i--;
10413 N--;
10414 leakedSurface = true;
10415 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10416 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10417 + ws + " surface=" + ws.mSurface
10418 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010419 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 ws.mSurface = null;
10421 leakedSurface = true;
10422 }
10423 }
10424 }
Romain Guy06882f82009-06-10 13:36:04 -070010425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 boolean killedApps = false;
10427 if (!leakedSurface) {
10428 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10429 SparseIntArray pidCandidates = new SparseIntArray();
10430 for (int i=0; i<N; i++) {
10431 WindowState ws = (WindowState)mWindows.get(i);
10432 if (ws.mSurface != null) {
10433 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10434 }
10435 }
10436 if (pidCandidates.size() > 0) {
10437 int[] pids = new int[pidCandidates.size()];
10438 for (int i=0; i<pids.length; i++) {
10439 pids[i] = pidCandidates.keyAt(i);
10440 }
10441 try {
10442 if (mActivityManager.killPidsForMemory(pids)) {
10443 killedApps = true;
10444 }
10445 } catch (RemoteException e) {
10446 }
10447 }
10448 }
Romain Guy06882f82009-06-10 13:36:04 -070010449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010450 if (leakedSurface || killedApps) {
10451 // We managed to reclaim some memory, so get rid of the trouble
10452 // surface and ask the app to request another one.
10453 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10454 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010455 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010456 win.mSurface = null;
10457 }
Romain Guy06882f82009-06-10 13:36:04 -070010458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459 try {
10460 win.mClient.dispatchGetNewSurface();
10461 } catch (RemoteException e) {
10462 }
10463 }
10464 } finally {
10465 Binder.restoreCallingIdentity(callingIdentity);
10466 }
10467 }
Romain Guy06882f82009-06-10 13:36:04 -070010468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010469 private boolean updateFocusedWindowLocked(int mode) {
10470 WindowState newFocus = computeFocusedWindowLocked();
10471 if (mCurrentFocus != newFocus) {
10472 // This check makes sure that we don't already have the focus
10473 // change message pending.
10474 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10475 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10476 if (localLOGV) Log.v(
10477 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10478 final WindowState oldFocus = mCurrentFocus;
10479 mCurrentFocus = newFocus;
10480 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010482 final WindowState imWindow = mInputMethodWindow;
10483 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010484 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010485 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010486 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10487 mLayoutNeeded = true;
10488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010489 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10490 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010491 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10492 // Client will do the layout, but we need to assign layers
10493 // for handleNewWindowLocked() below.
10494 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010495 }
10496 }
Romain Guy06882f82009-06-10 13:36:04 -070010497
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010498 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10499 mKeyWaiter.handleNewWindowLocked(newFocus);
10500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 return true;
10502 }
10503 return false;
10504 }
10505
10506 private WindowState computeFocusedWindowLocked() {
10507 WindowState result = null;
10508 WindowState win;
10509
10510 int i = mWindows.size() - 1;
10511 int nextAppIndex = mAppTokens.size()-1;
10512 WindowToken nextApp = nextAppIndex >= 0
10513 ? mAppTokens.get(nextAppIndex) : null;
10514
10515 while (i >= 0) {
10516 win = (WindowState)mWindows.get(i);
10517
10518 if (localLOGV || DEBUG_FOCUS) Log.v(
10519 TAG, "Looking for focus: " + i
10520 + " = " + win
10521 + ", flags=" + win.mAttrs.flags
10522 + ", canReceive=" + win.canReceiveKeys());
10523
10524 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 // If this window's application has been removed, just skip it.
10527 if (thisApp != null && thisApp.removed) {
10528 i--;
10529 continue;
10530 }
Romain Guy06882f82009-06-10 13:36:04 -070010531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010532 // If there is a focused app, don't allow focus to go to any
10533 // windows below it. If this is an application window, step
10534 // through the app tokens until we find its app.
10535 if (thisApp != null && nextApp != null && thisApp != nextApp
10536 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10537 int origAppIndex = nextAppIndex;
10538 while (nextAppIndex > 0) {
10539 if (nextApp == mFocusedApp) {
10540 // Whoops, we are below the focused app... no focus
10541 // for you!
10542 if (localLOGV || DEBUG_FOCUS) Log.v(
10543 TAG, "Reached focused app: " + mFocusedApp);
10544 return null;
10545 }
10546 nextAppIndex--;
10547 nextApp = mAppTokens.get(nextAppIndex);
10548 if (nextApp == thisApp) {
10549 break;
10550 }
10551 }
10552 if (thisApp != nextApp) {
10553 // Uh oh, the app token doesn't exist! This shouldn't
10554 // happen, but if it does we can get totally hosed...
10555 // so restart at the original app.
10556 nextAppIndex = origAppIndex;
10557 nextApp = mAppTokens.get(nextAppIndex);
10558 }
10559 }
10560
10561 // Dispatch to this window if it is wants key events.
10562 if (win.canReceiveKeys()) {
10563 if (DEBUG_FOCUS) Log.v(
10564 TAG, "Found focus @ " + i + " = " + win);
10565 result = win;
10566 break;
10567 }
10568
10569 i--;
10570 }
10571
10572 return result;
10573 }
10574
10575 private void startFreezingDisplayLocked() {
10576 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010577 // Freezing the display also suspends key event delivery, to
10578 // keep events from going astray while the display is reconfigured.
10579 // If someone has changed orientation again while the screen is
10580 // still frozen, the events will continue to be blocked while the
10581 // successive orientation change is processed. To prevent spurious
10582 // ANRs, we reset the event dispatch timeout in this case.
10583 synchronized (mKeyWaiter) {
10584 mKeyWaiter.mWasFrozen = true;
10585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 return;
10587 }
Romain Guy06882f82009-06-10 13:36:04 -070010588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010589 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010591 long now = SystemClock.uptimeMillis();
10592 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10593 if (mFreezeGcPending != 0) {
10594 if (now > (mFreezeGcPending+1000)) {
10595 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10596 mH.removeMessages(H.FORCE_GC);
10597 Runtime.getRuntime().gc();
10598 mFreezeGcPending = now;
10599 }
10600 } else {
10601 mFreezeGcPending = now;
10602 }
Romain Guy06882f82009-06-10 13:36:04 -070010603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010604 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010605 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10606 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010607 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608 mAppTransitionReady = true;
10609 }
Romain Guy06882f82009-06-10 13:36:04 -070010610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010611 if (PROFILE_ORIENTATION) {
10612 File file = new File("/data/system/frozen");
10613 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10614 }
10615 Surface.freezeDisplay(0);
10616 }
Romain Guy06882f82009-06-10 13:36:04 -070010617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 private void stopFreezingDisplayLocked() {
10619 if (!mDisplayFrozen) {
10620 return;
10621 }
Romain Guy06882f82009-06-10 13:36:04 -070010622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010623 mDisplayFrozen = false;
10624 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10625 if (PROFILE_ORIENTATION) {
10626 Debug.stopMethodTracing();
10627 }
10628 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010629
Chris Tate2ad63a92009-03-25 17:36:48 -070010630 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10631 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010632 synchronized (mKeyWaiter) {
10633 mKeyWaiter.mWasFrozen = true;
10634 mKeyWaiter.notifyAll();
10635 }
10636
10637 // A little kludge: a lot could have happened while the
10638 // display was frozen, so now that we are coming back we
10639 // do a gc so that any remote references the system
10640 // processes holds on others can be released if they are
10641 // no longer needed.
10642 mH.removeMessages(H.FORCE_GC);
10643 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10644 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 mScreenFrozenLock.release();
10647 }
Romain Guy06882f82009-06-10 13:36:04 -070010648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649 @Override
10650 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10651 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10652 != PackageManager.PERMISSION_GRANTED) {
10653 pw.println("Permission Denial: can't dump WindowManager from from pid="
10654 + Binder.getCallingPid()
10655 + ", uid=" + Binder.getCallingUid());
10656 return;
10657 }
Romain Guy06882f82009-06-10 13:36:04 -070010658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 synchronized(mWindowMap) {
10660 pw.println("Current Window Manager state:");
10661 for (int i=mWindows.size()-1; i>=0; i--) {
10662 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010663 pw.print(" Window #"); pw.print(i); pw.print(' ');
10664 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 w.dump(pw, " ");
10666 }
10667 if (mInputMethodDialogs.size() > 0) {
10668 pw.println(" ");
10669 pw.println(" Input method dialogs:");
10670 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10671 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010672 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010673 }
10674 }
10675 if (mPendingRemove.size() > 0) {
10676 pw.println(" ");
10677 pw.println(" Remove pending for:");
10678 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10679 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010680 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10681 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010682 w.dump(pw, " ");
10683 }
10684 }
10685 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10686 pw.println(" ");
10687 pw.println(" Windows force removing:");
10688 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10689 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010690 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10691 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010692 w.dump(pw, " ");
10693 }
10694 }
10695 if (mDestroySurface.size() > 0) {
10696 pw.println(" ");
10697 pw.println(" Windows waiting to destroy their surface:");
10698 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10699 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010700 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10701 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 w.dump(pw, " ");
10703 }
10704 }
10705 if (mLosingFocus.size() > 0) {
10706 pw.println(" ");
10707 pw.println(" Windows losing focus:");
10708 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10709 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010710 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10711 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010712 w.dump(pw, " ");
10713 }
10714 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010715 if (mResizingWindows.size() > 0) {
10716 pw.println(" ");
10717 pw.println(" Windows waiting to resize:");
10718 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10719 WindowState w = mResizingWindows.get(i);
10720 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10721 pw.print(w); pw.println(":");
10722 w.dump(pw, " ");
10723 }
10724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010725 if (mSessions.size() > 0) {
10726 pw.println(" ");
10727 pw.println(" All active sessions:");
10728 Iterator<Session> it = mSessions.iterator();
10729 while (it.hasNext()) {
10730 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010731 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010732 s.dump(pw, " ");
10733 }
10734 }
10735 if (mTokenMap.size() > 0) {
10736 pw.println(" ");
10737 pw.println(" All tokens:");
10738 Iterator<WindowToken> it = mTokenMap.values().iterator();
10739 while (it.hasNext()) {
10740 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010741 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010742 token.dump(pw, " ");
10743 }
10744 }
10745 if (mTokenList.size() > 0) {
10746 pw.println(" ");
10747 pw.println(" Window token list:");
10748 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010749 pw.print(" #"); pw.print(i); pw.print(": ");
10750 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010751 }
10752 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010753 if (mWallpaperTokens.size() > 0) {
10754 pw.println(" ");
10755 pw.println(" Wallpaper tokens:");
10756 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10757 WindowToken token = mWallpaperTokens.get(i);
10758 pw.print(" Wallpaper #"); pw.print(i);
10759 pw.print(' '); pw.print(token); pw.println(':');
10760 token.dump(pw, " ");
10761 }
10762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010763 if (mAppTokens.size() > 0) {
10764 pw.println(" ");
10765 pw.println(" Application tokens in Z order:");
10766 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010767 pw.print(" App #"); pw.print(i); pw.print(": ");
10768 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010769 }
10770 }
10771 if (mFinishedStarting.size() > 0) {
10772 pw.println(" ");
10773 pw.println(" Finishing start of application tokens:");
10774 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10775 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010776 pw.print(" Finished Starting #"); pw.print(i);
10777 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 token.dump(pw, " ");
10779 }
10780 }
10781 if (mExitingTokens.size() > 0) {
10782 pw.println(" ");
10783 pw.println(" Exiting tokens:");
10784 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10785 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010786 pw.print(" Exiting #"); pw.print(i);
10787 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010788 token.dump(pw, " ");
10789 }
10790 }
10791 if (mExitingAppTokens.size() > 0) {
10792 pw.println(" ");
10793 pw.println(" Exiting application tokens:");
10794 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10795 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010796 pw.print(" Exiting App #"); pw.print(i);
10797 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010798 token.dump(pw, " ");
10799 }
10800 }
10801 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010802 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10803 pw.print(" mLastFocus="); pw.println(mLastFocus);
10804 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10805 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10806 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010807 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010808 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10809 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10810 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10811 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010812 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10813 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10814 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10815 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10816 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010817 if (mDimAnimator != null) {
10818 mDimAnimator.printTo(pw);
10819 } else {
10820 pw.print( " no DimAnimator ");
10821 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010822 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010823 pw.print(mInputMethodAnimLayerAdjustment);
10824 pw.print(" mWallpaperAnimLayerAdjustment=");
10825 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010826 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10827 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010828 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10829 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10830 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10831 pw.print(" mRotation="); pw.print(mRotation);
10832 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10833 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10834 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10835 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10836 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10837 pw.print(" mNextAppTransition=0x");
10838 pw.print(Integer.toHexString(mNextAppTransition));
10839 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010840 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010841 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010842 if (mNextAppTransitionPackage != null) {
10843 pw.print(" mNextAppTransitionPackage=");
10844 pw.print(mNextAppTransitionPackage);
10845 pw.print(", mNextAppTransitionEnter=0x");
10846 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10847 pw.print(", mNextAppTransitionExit=0x");
10848 pw.print(Integer.toHexString(mNextAppTransitionExit));
10849 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010850 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10851 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010852 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10853 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10854 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10855 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010856 if (mOpeningApps.size() > 0) {
10857 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10858 }
10859 if (mClosingApps.size() > 0) {
10860 pw.print(" mClosingApps="); pw.println(mClosingApps);
10861 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010862 if (mToTopApps.size() > 0) {
10863 pw.print(" mToTopApps="); pw.println(mToTopApps);
10864 }
10865 if (mToBottomApps.size() > 0) {
10866 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10867 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010868 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10869 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010870 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010871 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10872 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10873 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10874 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10875 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10876 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010877 }
10878 }
10879
10880 public void monitor() {
10881 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050010882 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010883 synchronized (mKeyWaiter) { }
10884 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010885
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010886 public void virtualKeyFeedback(KeyEvent event) {
10887 mPolicy.keyFeedbackFromInput(event);
10888 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010889
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010890 /**
10891 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010892 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010893 */
10894 private static class DimAnimator {
10895 Surface mDimSurface;
10896 boolean mDimShown = false;
10897 float mDimCurrentAlpha;
10898 float mDimTargetAlpha;
10899 float mDimDeltaPerMs;
10900 long mLastDimAnimTime;
10901
10902 DimAnimator (SurfaceSession session) {
10903 if (mDimSurface == null) {
10904 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10905 + mDimSurface + ": CREATE");
10906 try {
10907 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10908 Surface.FX_SURFACE_DIM);
10909 } catch (Exception e) {
10910 Log.e(TAG, "Exception creating Dim surface", e);
10911 }
10912 }
10913 }
10914
10915 /**
10916 * Show the dim surface.
10917 */
10918 void show(int dw, int dh) {
10919 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10920 dw + "x" + dh + ")");
10921 mDimShown = true;
10922 try {
10923 mDimSurface.setPosition(0, 0);
10924 mDimSurface.setSize(dw, dh);
10925 mDimSurface.show();
10926 } catch (RuntimeException e) {
10927 Log.w(TAG, "Failure showing dim surface", e);
10928 }
10929 }
10930
10931 /**
10932 * Set's the dim surface's layer and update dim parameters that will be used in
10933 * {@link updateSurface} after all windows are examined.
10934 */
10935 void updateParameters(WindowState w, long currentTime) {
10936 mDimSurface.setLayer(w.mAnimLayer-1);
10937
10938 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010939 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10940 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010941 if (mDimTargetAlpha != target) {
10942 // If the desired dim level has changed, then
10943 // start an animation to it.
10944 mLastDimAnimTime = currentTime;
10945 long duration = (w.mAnimating && w.mAnimation != null)
10946 ? w.mAnimation.computeDurationHint()
10947 : DEFAULT_DIM_DURATION;
10948 if (target > mDimTargetAlpha) {
10949 // This is happening behind the activity UI,
10950 // so we can make it run a little longer to
10951 // give a stronger impression without disrupting
10952 // the user.
10953 duration *= DIM_DURATION_MULTIPLIER;
10954 }
10955 if (duration < 1) {
10956 // Don't divide by zero
10957 duration = 1;
10958 }
10959 mDimTargetAlpha = target;
10960 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10961 }
10962 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010963
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010964 /**
10965 * Updating the surface's alpha. Returns true if the animation continues, or returns
10966 * false when the animation is finished and the dim surface is hidden.
10967 */
10968 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10969 if (!dimming) {
10970 if (mDimTargetAlpha != 0) {
10971 mLastDimAnimTime = currentTime;
10972 mDimTargetAlpha = 0;
10973 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10974 }
10975 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010976
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010977 boolean animating = false;
10978 if (mLastDimAnimTime != 0) {
10979 mDimCurrentAlpha += mDimDeltaPerMs
10980 * (currentTime-mLastDimAnimTime);
10981 boolean more = true;
10982 if (displayFrozen) {
10983 // If the display is frozen, there is no reason to animate.
10984 more = false;
10985 } else if (mDimDeltaPerMs > 0) {
10986 if (mDimCurrentAlpha > mDimTargetAlpha) {
10987 more = false;
10988 }
10989 } else if (mDimDeltaPerMs < 0) {
10990 if (mDimCurrentAlpha < mDimTargetAlpha) {
10991 more = false;
10992 }
10993 } else {
10994 more = false;
10995 }
10996
10997 // Do we need to continue animating?
10998 if (more) {
10999 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11000 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11001 mLastDimAnimTime = currentTime;
11002 mDimSurface.setAlpha(mDimCurrentAlpha);
11003 animating = true;
11004 } else {
11005 mDimCurrentAlpha = mDimTargetAlpha;
11006 mLastDimAnimTime = 0;
11007 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11008 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11009 mDimSurface.setAlpha(mDimCurrentAlpha);
11010 if (!dimming) {
11011 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11012 + ": HIDE");
11013 try {
11014 mDimSurface.hide();
11015 } catch (RuntimeException e) {
11016 Log.w(TAG, "Illegal argument exception hiding dim surface");
11017 }
11018 mDimShown = false;
11019 }
11020 }
11021 }
11022 return animating;
11023 }
11024
11025 public void printTo(PrintWriter pw) {
11026 pw.print(" mDimShown="); pw.print(mDimShown);
11027 pw.print(" current="); pw.print(mDimCurrentAlpha);
11028 pw.print(" target="); pw.print(mDimTargetAlpha);
11029 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11030 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11031 }
11032 }
11033
11034 /**
11035 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11036 * This is used for opening/closing transition for apps in compatible mode.
11037 */
11038 private static class FadeInOutAnimation extends Animation {
11039 int mWidth;
11040 boolean mFadeIn;
11041
11042 public FadeInOutAnimation(boolean fadeIn) {
11043 setInterpolator(new AccelerateInterpolator());
11044 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11045 mFadeIn = fadeIn;
11046 }
11047
11048 @Override
11049 protected void applyTransformation(float interpolatedTime, Transformation t) {
11050 float x = interpolatedTime;
11051 if (!mFadeIn) {
11052 x = 1.0f - x; // reverse the interpolation for fade out
11053 }
11054 if (x < 0.5) {
11055 // move the window out of the screen.
11056 t.getMatrix().setTranslate(mWidth, 0);
11057 } else {
11058 t.getMatrix().setTranslate(0, 0);// show
11059 t.setAlpha((x - 0.5f) * 2);
11060 }
11061 }
11062
11063 @Override
11064 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11065 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11066 mWidth = width;
11067 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011068
11069 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011070 public int getZAdjustment() {
11071 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011072 }
11073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011074}