blob: b2cf9ec9f4bd7f45cf81ca74e1f03611b6fe1b8f [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;
Dianne Hackborn9b52a212009-12-11 14:51:35 -0800136 static final boolean DEBUG_LAYOUT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final boolean DEBUG_LAYERS = false;
138 static final boolean DEBUG_INPUT = false;
139 static final boolean DEBUG_INPUT_METHOD = false;
140 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700141 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 static final boolean DEBUG_ORIENTATION = false;
143 static final boolean DEBUG_APP_TRANSITIONS = false;
144 static final boolean DEBUG_STARTING_WINDOW = false;
145 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700146 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final boolean SHOW_TRANSACTIONS = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700148 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700149 static final boolean MEASURE_LATENCY = false;
150 static private LatencyTimer lt;
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final boolean PROFILE_ORIENTATION = false;
153 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700154 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 /** How long to wait for subsequent key repeats, in milliseconds */
157 static final int KEY_REPEAT_DELAY = 50;
158
159 /** How much to multiply the policy's type layer, to reserve room
160 * for multiple windows of the same type and Z-ordering adjustment
161 * with TYPE_LAYER_OFFSET. */
162 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
165 * or below others in the same layer. */
166 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /** How much to increment the layer for each window, to reserve room
169 * for effect surfaces between them.
170 */
171 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 /** The maximum length we will accept for a loaded animation duration:
174 * this is 10 seconds.
175 */
176 static final int MAX_ANIMATION_DURATION = 10*1000;
177
178 /** Amount of time (in milliseconds) to animate the dim surface from one
179 * value to another, when no window animation is driving it.
180 */
181 static final int DEFAULT_DIM_DURATION = 200;
182
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700183 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
184 * compatible windows.
185 */
186 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /** Adjustment to time to perform a dim, to make it more dramatic.
189 */
190 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700191
Dianne Hackborncfaef692009-06-15 14:24:44 -0700192 static final int INJECT_FAILED = 0;
193 static final int INJECT_SUCCEEDED = 1;
194 static final int INJECT_NO_PERMISSION = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 static final int UPDATE_FOCUS_NORMAL = 0;
197 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
198 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
199 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700200
Michael Chane96440f2009-05-06 10:27:36 -0700201 /** The minimum time between dispatching touch events. */
202 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
203
204 // Last touch event time
205 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700206
Michael Chane96440f2009-05-06 10:27:36 -0700207 // Last touch event type
208 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700209
Michael Chane96440f2009-05-06 10:27:36 -0700210 // Time to wait before calling useractivity again. This saves CPU usage
211 // when we get a flood of touch events.
212 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
213
214 // Last time we call user activity
215 long mLastUserActivityCallTime = 0;
216
Romain Guy06882f82009-06-10 13:36:04 -0700217 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700218 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700221 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
223 /**
224 * Condition waited on by {@link #reenableKeyguard} to know the call to
225 * the window policy has finished.
Mike Lockwood983ee092009-11-22 01:42:24 -0500226 * This is set to true only if mKeyguardTokenWatcher.acquired() has
227 * actually disabled the keyguard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 */
Mike Lockwood983ee092009-11-22 01:42:24 -0500229 private boolean mKeyguardDisabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
Mike Lockwood983ee092009-11-22 01:42:24 -0500231 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher(
232 new Handler(), "WindowManagerService.mKeyguardTokenWatcher") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 public void acquired() {
234 mPolicy.enableKeyguard(false);
Mike Lockwood983ee092009-11-22 01:42:24 -0500235 mKeyguardDisabled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
237 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700238 mPolicy.enableKeyguard(true);
Mike Lockwood983ee092009-11-22 01:42:24 -0500239 synchronized (mKeyguardTokenWatcher) {
240 mKeyguardDisabled = false;
241 mKeyguardTokenWatcher.notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 }
243 }
244 };
245
246 final Context mContext;
247
248 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
253
254 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 /**
259 * All currently active sessions with clients.
260 */
261 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
264 * Mapping from an IWindow IBinder to the server's Window object.
265 * This is also used as the lock for all of our state.
266 */
267 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
268
269 /**
270 * Mapping from a token IBinder to a WindowToken object.
271 */
272 final HashMap<IBinder, WindowToken> mTokenMap =
273 new HashMap<IBinder, WindowToken>();
274
275 /**
276 * The same tokens as mTokenMap, stored in a list for efficient iteration
277 * over them.
278 */
279 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
282 * Window tokens that are in the process of exiting, but still
283 * on screen for animations.
284 */
285 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
286
287 /**
288 * Z-ordered (bottom-most first) list of all application tokens, for
289 * controlling the ordering of windows in different applications. This
290 * contains WindowToken objects.
291 */
292 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
293
294 /**
295 * Application tokens that are in the process of exiting, but still
296 * on screen for animations.
297 */
298 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
299
300 /**
301 * List of window tokens that have finished starting their application,
302 * and now need to have the policy remove their windows.
303 */
304 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
305
306 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700307 * This was the app token that was used to retrieve the last enter
308 * animation. It will be used for the next exit animation.
309 */
310 AppWindowToken mLastEnterAnimToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700312 /**
313 * These were the layout params used to retrieve the last enter animation.
314 * They will be used for the next exit animation.
315 */
316 LayoutParams mLastEnterAnimParams;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * Z-ordered (bottom-most first) list of all Window objects.
320 */
321 final ArrayList mWindows = new ArrayList();
322
323 /**
324 * Windows that are being resized. Used so we can tell the client about
325 * the resize after closing the transaction in which we resized the
326 * underlying surface.
327 */
328 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
329
330 /**
331 * Windows whose animations have ended and now must be removed.
332 */
333 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
334
335 /**
336 * Windows whose surface should be destroyed.
337 */
338 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
339
340 /**
341 * Windows that have lost input focus and are waiting for the new
342 * focus window to be displayed before they are told about this.
343 */
344 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
345
346 /**
347 * This is set when we have run out of memory, and will either be an empty
348 * list or contain windows that need to be force removed.
349 */
350 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700355 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 Surface mBlurSurface;
357 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 final float[] mTmpFloats = new float[9];
362
363 boolean mSafeMode;
364 boolean mDisplayEnabled = false;
365 boolean mSystemBooted = false;
366 int mRotation = 0;
367 int mRequestedRotation = 0;
368 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700369 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 ArrayList<IRotationWatcher> mRotationWatchers
371 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 boolean mLayoutNeeded = true;
374 boolean mAnimationPending = false;
375 boolean mDisplayFrozen = false;
376 boolean mWindowsFreezingScreen = false;
377 long mFreezeGcPending = 0;
378 int mAppsFreezingScreen = 0;
379
380 // This is held as long as we have the screen frozen, to give us time to
381 // perform a rotation animation when turning off shows the lock screen which
382 // changes the orientation.
383 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 // State management of app transitions. When we are preparing for a
386 // transition, mNextAppTransition will be the kind of transition to
387 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
388 // mOpeningApps and mClosingApps are the lists of tokens that will be
389 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700390 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700391 String mNextAppTransitionPackage;
392 int mNextAppTransitionEnter;
393 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700395 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 boolean mAppTransitionTimeout = false;
397 boolean mStartingIconInTransition = false;
398 boolean mSkipAppTransitionAnimation = false;
399 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
400 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700401 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
402 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 //flag to detect fat touch events
405 boolean mFatTouch = false;
406 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 H mH = new H();
409
410 WindowState mCurrentFocus = null;
411 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 // This just indicates the window the input method is on top of, not
414 // necessarily the window its input is going to.
415 WindowState mInputMethodTarget = null;
416 WindowState mUpcomingInputMethodTarget = null;
417 boolean mInputMethodTargetWaitingAnim;
418 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 WindowState mInputMethodWindow = null;
421 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
422
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700423 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700425 // If non-null, this is the currently visible window that is associated
426 // with the wallpaper.
427 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700428 // If non-null, we are in the middle of animating from one wallpaper target
429 // to another, and this is the lower one in Z-order.
430 WindowState mLowerWallpaperTarget = null;
431 // If non-null, we are in the middle of animating from one wallpaper target
432 // to another, and this is the higher one in Z-order.
433 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700434 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700435 float mLastWallpaperX = -1;
436 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800437 float mLastWallpaperXStep = -1;
438 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800439 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700440 // This is set when we are waiting for a wallpaper to tell us it is done
441 // changing its scroll position.
442 WindowState mWaitingOnWallpaper;
443 // The last time we had a timeout when waiting for a wallpaper.
444 long mLastWallpaperTimeoutTime;
445 // We give a wallpaper up to 150ms to finish scrolling.
446 static final long WALLPAPER_TIMEOUT = 150;
447 // Time we wait after a timeout before trying to wait again.
448 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 AppWindowToken mFocusedApp = null;
451
452 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 float mWindowAnimationScale = 1.0f;
455 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 final KeyWaiter mKeyWaiter = new KeyWaiter();
458 final KeyQ mQueue;
459 final InputDispatcherThread mInputThread;
460
461 // Who is holding the screen on.
462 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700463
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700464 boolean mTurnOnScreen;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 /**
467 * Whether the UI is currently running in touch mode (not showing
468 * navigational focus because the user is directly pressing the screen).
469 */
470 boolean mInTouchMode = false;
471
472 private ViewServer mViewServer;
473
474 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700475
Dianne Hackbornc485a602009-03-24 22:39:49 -0700476 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700477 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700478
479 // The frame use to limit the size of the app running in compatibility mode.
480 Rect mCompatibleScreenFrame = new Rect();
481 // The surface used to fill the outer rim of the app running in compatibility mode.
482 Surface mBackgroundFillerSurface = null;
483 boolean mBackgroundFillerShown = false;
484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 public static WindowManagerService main(Context context,
486 PowerManagerService pm, boolean haveInputMethods) {
487 WMThread thr = new WMThread(context, pm, haveInputMethods);
488 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 synchronized (thr) {
491 while (thr.mService == null) {
492 try {
493 thr.wait();
494 } catch (InterruptedException e) {
495 }
496 }
497 }
Romain Guy06882f82009-06-10 13:36:04 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 return thr.mService;
500 }
Romain Guy06882f82009-06-10 13:36:04 -0700501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 static class WMThread extends Thread {
503 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 private final Context mContext;
506 private final PowerManagerService mPM;
507 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public WMThread(Context context, PowerManagerService pm,
510 boolean haveInputMethods) {
511 super("WindowManager");
512 mContext = context;
513 mPM = pm;
514 mHaveInputMethods = haveInputMethods;
515 }
Romain Guy06882f82009-06-10 13:36:04 -0700516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 public void run() {
518 Looper.prepare();
519 WindowManagerService s = new WindowManagerService(mContext, mPM,
520 mHaveInputMethods);
521 android.os.Process.setThreadPriority(
522 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 synchronized (this) {
525 mService = s;
526 notifyAll();
527 }
Romain Guy06882f82009-06-10 13:36:04 -0700528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 Looper.loop();
530 }
531 }
532
533 static class PolicyThread extends Thread {
534 private final WindowManagerPolicy mPolicy;
535 private final WindowManagerService mService;
536 private final Context mContext;
537 private final PowerManagerService mPM;
538 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 public PolicyThread(WindowManagerPolicy policy,
541 WindowManagerService service, Context context,
542 PowerManagerService pm) {
543 super("WindowManagerPolicy");
544 mPolicy = policy;
545 mService = service;
546 mContext = context;
547 mPM = pm;
548 }
Romain Guy06882f82009-06-10 13:36:04 -0700549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 public void run() {
551 Looper.prepare();
552 //Looper.myLooper().setMessageLogging(new LogPrinter(
553 // Log.VERBOSE, "WindowManagerPolicy"));
554 android.os.Process.setThreadPriority(
555 android.os.Process.THREAD_PRIORITY_FOREGROUND);
556 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 synchronized (this) {
559 mRunning = true;
560 notifyAll();
561 }
Romain Guy06882f82009-06-10 13:36:04 -0700562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 Looper.loop();
564 }
565 }
566
567 private WindowManagerService(Context context, PowerManagerService pm,
568 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700569 if (MEASURE_LATENCY) {
570 lt = new LatencyTimer(100, 1000);
571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 mContext = context;
574 mHaveInputMethods = haveInputMethods;
575 mLimitedAlphaCompositing = context.getResources().getBoolean(
576 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 mPowerManager = pm;
579 mPowerManager.setPolicy(mPolicy);
580 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
581 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
582 "SCREEN_FROZEN");
583 mScreenFrozenLock.setReferenceCounted(false);
584
585 mActivityManager = ActivityManagerNative.getDefault();
586 mBatteryStats = BatteryStatsService.getService();
587
588 // Get persisted window scale setting
589 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
590 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
591 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
592 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700593
Michael Chan9f028e62009-08-04 17:37:46 -0700594 int max_events_per_sec = 35;
595 try {
596 max_events_per_sec = Integer.parseInt(SystemProperties
597 .get("windowsmgr.max_events_per_sec"));
598 if (max_events_per_sec < 1) {
599 max_events_per_sec = 35;
600 }
601 } catch (NumberFormatException e) {
602 }
603 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 mQueue = new KeyQ();
606
607 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
610 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 synchronized (thr) {
613 while (!thr.mRunning) {
614 try {
615 thr.wait();
616 } catch (InterruptedException e) {
617 }
618 }
619 }
Romain Guy06882f82009-06-10 13:36:04 -0700620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 // Add ourself to the Watchdog monitors.
624 Watchdog.getInstance().addMonitor(this);
625 }
626
627 @Override
628 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
629 throws RemoteException {
630 try {
631 return super.onTransact(code, data, reply, flags);
632 } catch (RuntimeException e) {
633 // The window manager only throws security exceptions, so let's
634 // log all others.
635 if (!(e instanceof SecurityException)) {
636 Log.e(TAG, "Window Manager Crash", e);
637 }
638 throw e;
639 }
640 }
641
642 private void placeWindowAfter(Object pos, WindowState window) {
643 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700644 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 TAG, "Adding window " + window + " at "
646 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
647 mWindows.add(i+1, window);
648 }
649
650 private void placeWindowBefore(Object pos, WindowState window) {
651 final int i = mWindows.indexOf(pos);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700652 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 TAG, "Adding window " + window + " at "
654 + i + " of " + mWindows.size() + " (before " + pos + ")");
655 mWindows.add(i, window);
656 }
657
658 //This method finds out the index of a window that has the same app token as
659 //win. used for z ordering the windows in mWindows
660 private int findIdxBasedOnAppTokens(WindowState win) {
661 //use a local variable to cache mWindows
662 ArrayList localmWindows = mWindows;
663 int jmax = localmWindows.size();
664 if(jmax == 0) {
665 return -1;
666 }
667 for(int j = (jmax-1); j >= 0; j--) {
668 WindowState wentry = (WindowState)localmWindows.get(j);
669 if(wentry.mAppToken == win.mAppToken) {
670 return j;
671 }
672 }
673 return -1;
674 }
Romain Guy06882f82009-06-10 13:36:04 -0700675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
677 final IWindow client = win.mClient;
678 final WindowToken token = win.mToken;
679 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 final int N = localmWindows.size();
682 final WindowState attached = win.mAttachedWindow;
683 int i;
684 if (attached == null) {
685 int tokenWindowsPos = token.windows.size();
686 if (token.appWindowToken != null) {
687 int index = tokenWindowsPos-1;
688 if (index >= 0) {
689 // If this application has existing windows, we
690 // simply place the new window on top of them... but
691 // keep the starting window on top.
692 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
693 // Base windows go behind everything else.
694 placeWindowBefore(token.windows.get(0), win);
695 tokenWindowsPos = 0;
696 } else {
697 AppWindowToken atoken = win.mAppToken;
698 if (atoken != null &&
699 token.windows.get(index) == atoken.startingWindow) {
700 placeWindowBefore(token.windows.get(index), win);
701 tokenWindowsPos--;
702 } else {
703 int newIdx = findIdxBasedOnAppTokens(win);
704 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700705 //there is a window above this one associated with the same
706 //apptoken note that the window could be a floating window
707 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 //windows associated with this token.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700709 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
710 TAG, "Adding window " + win + " at "
711 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 }
715 }
716 } else {
717 if (localLOGV) Log.v(
718 TAG, "Figuring out where to add app window "
719 + client.asBinder() + " (token=" + token + ")");
720 // Figure out where the window should go, based on the
721 // order of applications.
722 final int NA = mAppTokens.size();
723 Object pos = null;
724 for (i=NA-1; i>=0; i--) {
725 AppWindowToken t = mAppTokens.get(i);
726 if (t == token) {
727 i--;
728 break;
729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800730
Dianne Hackborna8f60182009-09-01 19:01:50 -0700731 // We haven't reached the token yet; if this token
732 // is not going to the bottom and has windows, we can
733 // use it as an anchor for when we do reach the token.
734 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 pos = t.windows.get(0);
736 }
737 }
738 // We now know the index into the apps. If we found
739 // an app window above, that gives us the position; else
740 // we need to look some more.
741 if (pos != null) {
742 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700743 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 mTokenMap.get(((WindowState)pos).mClient.asBinder());
745 if (atoken != null) {
746 final int NC = atoken.windows.size();
747 if (NC > 0) {
748 WindowState bottom = atoken.windows.get(0);
749 if (bottom.mSubLayer < 0) {
750 pos = bottom;
751 }
752 }
753 }
754 placeWindowBefore(pos, win);
755 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700756 // Continue looking down until we find the first
757 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 while (i >= 0) {
759 AppWindowToken t = mAppTokens.get(i);
760 final int NW = t.windows.size();
761 if (NW > 0) {
762 pos = t.windows.get(NW-1);
763 break;
764 }
765 i--;
766 }
767 if (pos != null) {
768 // Move in front of any windows attached to this
769 // one.
770 WindowToken atoken =
771 mTokenMap.get(((WindowState)pos).mClient.asBinder());
772 if (atoken != null) {
773 final int NC = atoken.windows.size();
774 if (NC > 0) {
775 WindowState top = atoken.windows.get(NC-1);
776 if (top.mSubLayer >= 0) {
777 pos = top;
778 }
779 }
780 }
781 placeWindowAfter(pos, win);
782 } else {
783 // Just search for the start of this layer.
784 final int myLayer = win.mBaseLayer;
785 for (i=0; i<N; i++) {
786 WindowState w = (WindowState)localmWindows.get(i);
787 if (w.mBaseLayer > myLayer) {
788 break;
789 }
790 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700791 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
792 TAG, "Adding window " + win + " at "
793 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 localmWindows.add(i, win);
795 }
796 }
797 }
798 } else {
799 // Figure out where window should go, based on layer.
800 final int myLayer = win.mBaseLayer;
801 for (i=N-1; i>=0; i--) {
802 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
803 i++;
804 break;
805 }
806 }
807 if (i < 0) i = 0;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700808 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Log.v(
809 TAG, "Adding window " + win + " at "
810 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 localmWindows.add(i, win);
812 }
813 if (addToToken) {
814 token.windows.add(tokenWindowsPos, win);
815 }
816
817 } else {
818 // Figure out this window's ordering relative to the window
819 // it is attached to.
820 final int NA = token.windows.size();
821 final int sublayer = win.mSubLayer;
822 int largestSublayer = Integer.MIN_VALUE;
823 WindowState windowWithLargestSublayer = null;
824 for (i=0; i<NA; i++) {
825 WindowState w = token.windows.get(i);
826 final int wSublayer = w.mSubLayer;
827 if (wSublayer >= largestSublayer) {
828 largestSublayer = wSublayer;
829 windowWithLargestSublayer = w;
830 }
831 if (sublayer < 0) {
832 // For negative sublayers, we go below all windows
833 // in the same sublayer.
834 if (wSublayer >= sublayer) {
835 if (addToToken) {
836 token.windows.add(i, win);
837 }
838 placeWindowBefore(
839 wSublayer >= 0 ? attached : w, win);
840 break;
841 }
842 } else {
843 // For positive sublayers, we go above all windows
844 // in the same sublayer.
845 if (wSublayer > sublayer) {
846 if (addToToken) {
847 token.windows.add(i, win);
848 }
849 placeWindowBefore(w, win);
850 break;
851 }
852 }
853 }
854 if (i >= NA) {
855 if (addToToken) {
856 token.windows.add(win);
857 }
858 if (sublayer < 0) {
859 placeWindowBefore(attached, win);
860 } else {
861 placeWindowAfter(largestSublayer >= 0
862 ? windowWithLargestSublayer
863 : attached,
864 win);
865 }
866 }
867 }
Romain Guy06882f82009-06-10 13:36:04 -0700868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 if (win.mAppToken != null && addToToken) {
870 win.mAppToken.allAppWindows.add(win);
871 }
872 }
Romain Guy06882f82009-06-10 13:36:04 -0700873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 static boolean canBeImeTarget(WindowState w) {
875 final int fl = w.mAttrs.flags
876 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
877 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
878 return w.isVisibleOrAdding();
879 }
880 return false;
881 }
Romain Guy06882f82009-06-10 13:36:04 -0700882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
884 final ArrayList localmWindows = mWindows;
885 final int N = localmWindows.size();
886 WindowState w = null;
887 int i = N;
888 while (i > 0) {
889 i--;
890 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
893 // + Integer.toHexString(w.mAttrs.flags));
894 if (canBeImeTarget(w)) {
895 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 // Yet more tricksyness! If this window is a "starting"
898 // window, we do actually want to be on top of it, but
899 // it is not -really- where input will go. So if the caller
900 // is not actually looking to move the IME, look down below
901 // for a real window to target...
902 if (!willMove
903 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
904 && i > 0) {
905 WindowState wb = (WindowState)localmWindows.get(i-1);
906 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
907 i--;
908 w = wb;
909 }
910 }
911 break;
912 }
913 }
Romain Guy06882f82009-06-10 13:36:04 -0700914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
918 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 if (willMove && w != null) {
921 final WindowState curTarget = mInputMethodTarget;
922 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 // Now some fun for dealing with window animations that
925 // modify the Z order. We need to look at all windows below
926 // the current target that are in this app, finding the highest
927 // visible one in layering.
928 AppWindowToken token = curTarget.mAppToken;
929 WindowState highestTarget = null;
930 int highestPos = 0;
931 if (token.animating || token.animation != null) {
932 int pos = 0;
933 pos = localmWindows.indexOf(curTarget);
934 while (pos >= 0) {
935 WindowState win = (WindowState)localmWindows.get(pos);
936 if (win.mAppToken != token) {
937 break;
938 }
939 if (!win.mRemoved) {
940 if (highestTarget == null || win.mAnimLayer >
941 highestTarget.mAnimLayer) {
942 highestTarget = win;
943 highestPos = pos;
944 }
945 }
946 pos--;
947 }
948 }
Romain Guy06882f82009-06-10 13:36:04 -0700949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700951 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 + mNextAppTransition + " " + highestTarget
953 + " animating=" + highestTarget.isAnimating()
954 + " layer=" + highestTarget.mAnimLayer
955 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700956
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700957 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 // If we are currently setting up for an animation,
959 // hold everything until we can find out what will happen.
960 mInputMethodTargetWaitingAnim = true;
961 mInputMethodTarget = highestTarget;
962 return highestPos + 1;
963 } else if (highestTarget.isAnimating() &&
964 highestTarget.mAnimLayer > w.mAnimLayer) {
965 // If the window we are currently targeting is involved
966 // with an animation, and it is on top of the next target
967 // we will be over, then hold off on moving until
968 // that is done.
969 mInputMethodTarget = highestTarget;
970 return highestPos + 1;
971 }
972 }
973 }
974 }
Romain Guy06882f82009-06-10 13:36:04 -0700975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 //Log.i(TAG, "Placing input method @" + (i+1));
977 if (w != null) {
978 if (willMove) {
979 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700980 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
982 + mInputMethodTarget + " to " + w, e);
983 mInputMethodTarget = w;
984 if (w.mAppToken != null) {
985 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
986 } else {
987 setInputMethodAnimLayerAdjustment(0);
988 }
989 }
990 return i+1;
991 }
992 if (willMove) {
993 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700994 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
996 + mInputMethodTarget + " to null", e);
997 mInputMethodTarget = null;
998 setInputMethodAnimLayerAdjustment(0);
999 }
1000 return -1;
1001 }
Romain Guy06882f82009-06-10 13:36:04 -07001002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 void addInputMethodWindowToListLocked(WindowState win) {
1004 int pos = findDesiredInputMethodWindowIndexLocked(true);
1005 if (pos >= 0) {
1006 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001007 if (DEBUG_WINDOW_MOVEMENT) Log.v(
1008 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 mWindows.add(pos, win);
1010 moveInputMethodDialogsLocked(pos+1);
1011 return;
1012 }
1013 win.mTargetAppToken = null;
1014 addWindowToListInOrderLocked(win, true);
1015 moveInputMethodDialogsLocked(pos);
1016 }
Romain Guy06882f82009-06-10 13:36:04 -07001017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 void setInputMethodAnimLayerAdjustment(int adj) {
1019 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
1020 mInputMethodAnimLayerAdjustment = adj;
1021 WindowState imw = mInputMethodWindow;
1022 if (imw != null) {
1023 imw.mAnimLayer = imw.mLayer + adj;
1024 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1025 + " anim layer: " + imw.mAnimLayer);
1026 int wi = imw.mChildWindows.size();
1027 while (wi > 0) {
1028 wi--;
1029 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1030 cw.mAnimLayer = cw.mLayer + adj;
1031 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1032 + " anim layer: " + cw.mAnimLayer);
1033 }
1034 }
1035 int di = mInputMethodDialogs.size();
1036 while (di > 0) {
1037 di --;
1038 imw = mInputMethodDialogs.get(di);
1039 imw.mAnimLayer = imw.mLayer + adj;
1040 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1041 + " anim layer: " + imw.mAnimLayer);
1042 }
1043 }
Romain Guy06882f82009-06-10 13:36:04 -07001044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1046 int wpos = mWindows.indexOf(win);
1047 if (wpos >= 0) {
1048 if (wpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001049 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 mWindows.remove(wpos);
1051 int NC = win.mChildWindows.size();
1052 while (NC > 0) {
1053 NC--;
1054 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1055 int cpos = mWindows.indexOf(cw);
1056 if (cpos >= 0) {
1057 if (cpos < interestingPos) interestingPos--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001058 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Temp removing child at "
1059 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 mWindows.remove(cpos);
1061 }
1062 }
1063 }
1064 return interestingPos;
1065 }
Romain Guy06882f82009-06-10 13:36:04 -07001066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 private void reAddWindowToListInOrderLocked(WindowState win) {
1068 addWindowToListInOrderLocked(win, false);
1069 // This is a hack to get all of the child windows added as well
1070 // at the right position. Child windows should be rare and
1071 // this case should be rare, so it shouldn't be that big a deal.
1072 int wpos = mWindows.indexOf(win);
1073 if (wpos >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001074 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "ReAdd removing from " + wpos
1075 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 mWindows.remove(wpos);
1077 reAddWindowLocked(wpos, win);
1078 }
1079 }
Romain Guy06882f82009-06-10 13:36:04 -07001080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 void logWindowList(String prefix) {
1082 int N = mWindows.size();
1083 while (N > 0) {
1084 N--;
1085 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1086 }
1087 }
Romain Guy06882f82009-06-10 13:36:04 -07001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 void moveInputMethodDialogsLocked(int pos) {
1090 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 final int N = dialogs.size();
1093 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1094 for (int i=0; i<N; i++) {
1095 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1096 }
1097 if (DEBUG_INPUT_METHOD) {
1098 Log.v(TAG, "Window list w/pos=" + pos);
1099 logWindowList(" ");
1100 }
Romain Guy06882f82009-06-10 13:36:04 -07001101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 if (pos >= 0) {
1103 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1104 if (pos < mWindows.size()) {
1105 WindowState wp = (WindowState)mWindows.get(pos);
1106 if (wp == mInputMethodWindow) {
1107 pos++;
1108 }
1109 }
1110 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1111 for (int i=0; i<N; i++) {
1112 WindowState win = dialogs.get(i);
1113 win.mTargetAppToken = targetAppToken;
1114 pos = reAddWindowLocked(pos, win);
1115 }
1116 if (DEBUG_INPUT_METHOD) {
1117 Log.v(TAG, "Final window list:");
1118 logWindowList(" ");
1119 }
1120 return;
1121 }
1122 for (int i=0; i<N; i++) {
1123 WindowState win = dialogs.get(i);
1124 win.mTargetAppToken = null;
1125 reAddWindowToListInOrderLocked(win);
1126 if (DEBUG_INPUT_METHOD) {
1127 Log.v(TAG, "No IM target, final list:");
1128 logWindowList(" ");
1129 }
1130 }
1131 }
Romain Guy06882f82009-06-10 13:36:04 -07001132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1134 final WindowState imWin = mInputMethodWindow;
1135 final int DN = mInputMethodDialogs.size();
1136 if (imWin == null && DN == 0) {
1137 return false;
1138 }
Romain Guy06882f82009-06-10 13:36:04 -07001139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1141 if (imPos >= 0) {
1142 // In this case, the input method windows are to be placed
1143 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 // First check to see if the input method windows are already
1146 // located here, and contiguous.
1147 final int N = mWindows.size();
1148 WindowState firstImWin = imPos < N
1149 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 // Figure out the actual input method window that should be
1152 // at the bottom of their stack.
1153 WindowState baseImWin = imWin != null
1154 ? imWin : mInputMethodDialogs.get(0);
1155 if (baseImWin.mChildWindows.size() > 0) {
1156 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1157 if (cw.mSubLayer < 0) baseImWin = cw;
1158 }
Romain Guy06882f82009-06-10 13:36:04 -07001159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 if (firstImWin == baseImWin) {
1161 // The windows haven't moved... but are they still contiguous?
1162 // First find the top IM window.
1163 int pos = imPos+1;
1164 while (pos < N) {
1165 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1166 break;
1167 }
1168 pos++;
1169 }
1170 pos++;
1171 // Now there should be no more input method windows above.
1172 while (pos < N) {
1173 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1174 break;
1175 }
1176 pos++;
1177 }
1178 if (pos >= N) {
1179 // All is good!
1180 return false;
1181 }
1182 }
Romain Guy06882f82009-06-10 13:36:04 -07001183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 if (imWin != null) {
1185 if (DEBUG_INPUT_METHOD) {
1186 Log.v(TAG, "Moving IM from " + imPos);
1187 logWindowList(" ");
1188 }
1189 imPos = tmpRemoveWindowLocked(imPos, imWin);
1190 if (DEBUG_INPUT_METHOD) {
1191 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1192 logWindowList(" ");
1193 }
1194 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1195 reAddWindowLocked(imPos, imWin);
1196 if (DEBUG_INPUT_METHOD) {
1197 Log.v(TAG, "List after moving IM to " + imPos + ":");
1198 logWindowList(" ");
1199 }
1200 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1201 } else {
1202 moveInputMethodDialogsLocked(imPos);
1203 }
Romain Guy06882f82009-06-10 13:36:04 -07001204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 } else {
1206 // In this case, the input method windows go in a fixed layer,
1207 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 if (imWin != null) {
1210 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1211 tmpRemoveWindowLocked(0, imWin);
1212 imWin.mTargetAppToken = null;
1213 reAddWindowToListInOrderLocked(imWin);
1214 if (DEBUG_INPUT_METHOD) {
1215 Log.v(TAG, "List with no IM target:");
1216 logWindowList(" ");
1217 }
1218 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1219 } else {
1220 moveInputMethodDialogsLocked(-1);;
1221 }
Romain Guy06882f82009-06-10 13:36:04 -07001222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
Romain Guy06882f82009-06-10 13:36:04 -07001224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 if (needAssignLayers) {
1226 assignLayersLocked();
1227 }
Romain Guy06882f82009-06-10 13:36:04 -07001228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 return true;
1230 }
Romain Guy06882f82009-06-10 13:36:04 -07001231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 void adjustInputMethodDialogsLocked() {
1233 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1234 }
Romain Guy06882f82009-06-10 13:36:04 -07001235
Dianne Hackborn25994b42009-09-04 14:21:19 -07001236 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1237 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1238 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1239 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1240 ? wallpaperTarget.mAppToken.animation : null)
1241 + " upper=" + mUpperWallpaperTarget
1242 + " lower=" + mLowerWallpaperTarget);
1243 return (wallpaperTarget != null
1244 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1245 && wallpaperTarget.mAppToken.animation != null)))
1246 || mUpperWallpaperTarget != null
1247 || mLowerWallpaperTarget != null;
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001250 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1251 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001252
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001253 int adjustWallpaperWindowsLocked() {
1254 int changed = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001256 final int dw = mDisplay.getWidth();
1257 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001258
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001259 // First find top-most window that has asked to be on top of the
1260 // wallpaper; all wallpapers go behind it.
1261 final ArrayList localmWindows = mWindows;
1262 int N = localmWindows.size();
1263 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001264 WindowState foundW = null;
1265 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001266 WindowState topCurW = null;
1267 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001268 int i = N;
1269 while (i > 0) {
1270 i--;
1271 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001272 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1273 if (topCurW == null) {
1274 topCurW = w;
1275 topCurI = i;
1276 }
1277 continue;
1278 }
1279 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001280 if (w.mAppToken != null) {
1281 // If this window's app token is hidden and not animating,
1282 // it is of no interest to us.
1283 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1284 if (DEBUG_WALLPAPER) Log.v(TAG,
1285 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001286 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001287 continue;
1288 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001289 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001290 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1291 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1292 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001293 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001294 && (mWallpaperTarget == w
1295 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001296 if (DEBUG_WALLPAPER) Log.v(TAG,
1297 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001298 foundW = w;
1299 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001300 if (w == mWallpaperTarget && ((w.mAppToken != null
1301 && w.mAppToken.animation != null)
1302 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001303 // The current wallpaper target is animating, so we'll
1304 // look behind it for another possible target and figure
1305 // out what is going on below.
1306 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1307 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001308 continue;
1309 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001310 break;
1311 }
1312 }
1313
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001314 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001315 // If we are currently waiting for an app transition, and either
1316 // the current target or the next target are involved with it,
1317 // then hold off on doing anything with the wallpaper.
1318 // Note that we are checking here for just whether the target
1319 // is part of an app token... which is potentially overly aggressive
1320 // (the app token may not be involved in the transition), but good
1321 // enough (we'll just wait until whatever transition is pending
1322 // executes).
1323 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001324 if (DEBUG_WALLPAPER) Log.v(TAG,
1325 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001326 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001327 }
1328 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001329 if (DEBUG_WALLPAPER) Log.v(TAG,
1330 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001331 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001332 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001335 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001336 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001337 Log.v(TAG, "New wallpaper target: " + foundW
1338 + " oldTarget: " + mWallpaperTarget);
1339 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001340
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001341 mLowerWallpaperTarget = null;
1342 mUpperWallpaperTarget = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001343
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001344 WindowState oldW = mWallpaperTarget;
1345 mWallpaperTarget = foundW;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001346
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001347 // Now what is happening... if the current and new targets are
1348 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001349 if (foundW != null && oldW != null) {
1350 boolean oldAnim = oldW.mAnimation != null
1351 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1352 boolean foundAnim = foundW.mAnimation != null
1353 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001354 if (DEBUG_WALLPAPER) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001355 Log.v(TAG, "New animation: " + foundAnim
1356 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001357 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001358 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001359 int oldI = localmWindows.indexOf(oldW);
1360 if (DEBUG_WALLPAPER) {
1361 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1362 }
1363 if (oldI >= 0) {
1364 if (DEBUG_WALLPAPER) {
1365 Log.v(TAG, "Animating wallpapers: old#" + oldI
1366 + "=" + oldW + "; new#" + foundI
1367 + "=" + foundW);
1368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001369
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001370 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001371 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001372 if (DEBUG_WALLPAPER) {
1373 Log.v(TAG, "Old wallpaper still the target.");
1374 }
1375 mWallpaperTarget = oldW;
1376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001378 // Now set the upper and lower wallpaper targets
1379 // correctly, and make sure that we are positioning
1380 // the wallpaper below the lower.
1381 if (foundI > oldI) {
1382 // The new target is on top of the old one.
1383 if (DEBUG_WALLPAPER) {
1384 Log.v(TAG, "Found target above old target.");
1385 }
1386 mUpperWallpaperTarget = foundW;
1387 mLowerWallpaperTarget = oldW;
1388 foundW = oldW;
1389 foundI = oldI;
1390 } else {
1391 // The new target is below the old one.
1392 if (DEBUG_WALLPAPER) {
1393 Log.v(TAG, "Found target below old target.");
1394 }
1395 mUpperWallpaperTarget = oldW;
1396 mLowerWallpaperTarget = foundW;
1397 }
1398 }
1399 }
1400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001401
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001402 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001403 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001404 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1405 || (mLowerWallpaperTarget.mAppToken != null
1406 && mLowerWallpaperTarget.mAppToken.animation != null);
1407 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1408 || (mUpperWallpaperTarget.mAppToken != null
1409 && mUpperWallpaperTarget.mAppToken.animation != null);
1410 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001411 if (DEBUG_WALLPAPER) {
1412 Log.v(TAG, "No longer animating wallpaper targets!");
1413 }
1414 mLowerWallpaperTarget = null;
1415 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001416 }
1417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001418
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001419 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001420 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001421 // The window is visible to the compositor... but is it visible
1422 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001423 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001424 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001426 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001427 // its layer adjustment. Only do this if we are not transfering
1428 // between two wallpaper targets.
1429 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001430 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001431 ? foundW.mAppToken.animLayerAdjustment : 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001432
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001433 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1434 * TYPE_LAYER_MULTIPLIER
1435 + TYPE_LAYER_OFFSET;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001436
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001437 // Now w is the window we are supposed to be behind... but we
1438 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001439 // AND any starting window associated with it, AND below the
1440 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001441 while (foundI > 0) {
1442 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001443 if (wb.mBaseLayer < maxLayer &&
1444 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001445 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001446 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001447 // This window is not related to the previous one in any
1448 // interesting way, so stop here.
1449 break;
1450 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001451 foundW = wb;
1452 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001453 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001454 } else {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001455 if (DEBUG_WALLPAPER) Log.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001457
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001458 if (foundW == null && topCurW != null) {
1459 // There is no wallpaper target, so it goes at the bottom.
1460 // We will assume it is the same place as last time, if known.
1461 foundW = topCurW;
1462 foundI = topCurI+1;
1463 } else {
1464 // Okay i is the position immediately above the wallpaper. Look at
1465 // what is below it for later.
1466 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001468
Dianne Hackborn284ac932009-08-28 10:34:25 -07001469 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001470 if (mWallpaperTarget.mWallpaperX >= 0) {
1471 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001472 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001473 }
1474 if (mWallpaperTarget.mWallpaperY >= 0) {
1475 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001476 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001477 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001479
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001480 // Start stepping backwards from here, ensuring that our wallpaper windows
1481 // are correctly placed.
1482 int curTokenIndex = mWallpaperTokens.size();
1483 while (curTokenIndex > 0) {
1484 curTokenIndex--;
1485 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001486 if (token.hidden == visible) {
1487 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1488 token.hidden = !visible;
1489 // Need to do a layout to ensure the wallpaper now has the
1490 // correct size.
1491 mLayoutNeeded = true;
1492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001493
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001494 int curWallpaperIndex = token.windows.size();
1495 while (curWallpaperIndex > 0) {
1496 curWallpaperIndex--;
1497 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001499 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001500 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001503 // First, make sure the client has the current visibility
1504 // state.
1505 if (wallpaper.mWallpaperVisible != visible) {
1506 wallpaper.mWallpaperVisible = visible;
1507 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001508 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001509 "Setting visibility of wallpaper " + wallpaper
1510 + ": " + visible);
1511 wallpaper.mClient.dispatchAppVisibility(visible);
1512 } catch (RemoteException e) {
1513 }
1514 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001515
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001516 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001517 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1518 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001519
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001520 // First, if this window is at the current index, then all
1521 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001522 if (wallpaper == foundW) {
1523 foundI--;
1524 foundW = foundI > 0
1525 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001526 continue;
1527 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001528
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001529 // The window didn't match... the current wallpaper window,
1530 // wherever it is, is in the wrong place, so make sure it is
1531 // not in the list.
1532 int oldIndex = localmWindows.indexOf(wallpaper);
1533 if (oldIndex >= 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001534 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Wallpaper removing at "
1535 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001536 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001537 if (oldIndex < foundI) {
1538 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001539 }
1540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001541
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001542 // Now stick it in.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001543 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
1544 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001545 + " from " + oldIndex + " to " + foundI);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001546
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001547 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001548 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001549 }
1550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001551
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001552 return changed;
1553 }
1554
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001555 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001556 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1557 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001558 mWallpaperAnimLayerAdjustment = adj;
1559 int curTokenIndex = mWallpaperTokens.size();
1560 while (curTokenIndex > 0) {
1561 curTokenIndex--;
1562 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1563 int curWallpaperIndex = token.windows.size();
1564 while (curWallpaperIndex > 0) {
1565 curWallpaperIndex--;
1566 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1567 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001568 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1569 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001570 }
1571 }
1572 }
1573
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001574 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1575 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001576 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001577 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001578 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001579 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001580 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1581 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1582 changed = wallpaperWin.mXOffset != offset;
1583 if (changed) {
1584 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1585 + wallpaperWin + " x: " + offset);
1586 wallpaperWin.mXOffset = offset;
1587 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001588 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001589 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001590 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001591 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001593
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001594 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001595 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001596 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1597 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1598 if (wallpaperWin.mYOffset != offset) {
1599 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1600 + wallpaperWin + " y: " + offset);
1601 changed = true;
1602 wallpaperWin.mYOffset = offset;
1603 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001604 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001605 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001606 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001607 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001608 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001609
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001610 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001611 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001612 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1613 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1614 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001615 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001616 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001617 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001618 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001619 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1620 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001621 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001622 if (mWaitingOnWallpaper != null) {
1623 long start = SystemClock.uptimeMillis();
1624 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1625 < start) {
1626 try {
1627 if (DEBUG_WALLPAPER) Log.v(TAG,
1628 "Waiting for offset complete...");
1629 mWindowMap.wait(WALLPAPER_TIMEOUT);
1630 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001631 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001632 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1633 if ((start+WALLPAPER_TIMEOUT)
1634 < SystemClock.uptimeMillis()) {
1635 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1636 + wallpaperWin);
1637 mLastWallpaperTimeoutTime = start;
1638 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001639 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001640 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001641 }
1642 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001643 } catch (RemoteException e) {
1644 }
1645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001646
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001647 return changed;
1648 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001649
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001650 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001651 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001652 if (mWaitingOnWallpaper != null &&
1653 mWaitingOnWallpaper.mClient.asBinder() == window) {
1654 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001655 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001656 }
1657 }
1658 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001659
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001660 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001661 final int dw = mDisplay.getWidth();
1662 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001663
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001664 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001665
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001666 WindowState target = mWallpaperTarget;
1667 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001668 if (target.mWallpaperX >= 0) {
1669 mLastWallpaperX = target.mWallpaperX;
1670 } else if (changingTarget.mWallpaperX >= 0) {
1671 mLastWallpaperX = changingTarget.mWallpaperX;
1672 }
1673 if (target.mWallpaperY >= 0) {
1674 mLastWallpaperY = target.mWallpaperY;
1675 } else if (changingTarget.mWallpaperY >= 0) {
1676 mLastWallpaperY = changingTarget.mWallpaperY;
1677 }
1678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001679
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001680 int curTokenIndex = mWallpaperTokens.size();
1681 while (curTokenIndex > 0) {
1682 curTokenIndex--;
1683 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1684 int curWallpaperIndex = token.windows.size();
1685 while (curWallpaperIndex > 0) {
1686 curWallpaperIndex--;
1687 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1688 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1689 wallpaper.computeShownFrameLocked();
1690 changed = true;
1691 // We only want to be synchronous with one wallpaper.
1692 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001693 }
1694 }
1695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001696
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001697 return changed;
1698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001699
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001700 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001701 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001702 final int dw = mDisplay.getWidth();
1703 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001705 int curTokenIndex = mWallpaperTokens.size();
1706 while (curTokenIndex > 0) {
1707 curTokenIndex--;
1708 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001709 if (token.hidden == visible) {
1710 token.hidden = !visible;
1711 // Need to do a layout to ensure the wallpaper now has the
1712 // correct size.
1713 mLayoutNeeded = true;
1714 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001715
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001716 int curWallpaperIndex = token.windows.size();
1717 while (curWallpaperIndex > 0) {
1718 curWallpaperIndex--;
1719 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1720 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001721 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001723
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001724 if (wallpaper.mWallpaperVisible != visible) {
1725 wallpaper.mWallpaperVisible = visible;
1726 try {
1727 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001728 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001729 + ": " + visible);
1730 wallpaper.mClient.dispatchAppVisibility(visible);
1731 } catch (RemoteException e) {
1732 }
1733 }
1734 }
1735 }
1736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001737
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001738 void sendPointerToWallpaperLocked(WindowState srcWin,
1739 MotionEvent pointer, long eventTime) {
1740 int curTokenIndex = mWallpaperTokens.size();
1741 while (curTokenIndex > 0) {
1742 curTokenIndex--;
1743 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1744 int curWallpaperIndex = token.windows.size();
1745 while (curWallpaperIndex > 0) {
1746 curWallpaperIndex--;
1747 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1748 if ((wallpaper.mAttrs.flags &
1749 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1750 continue;
1751 }
1752 try {
1753 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001754 if (srcWin != null) {
1755 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1756 srcWin.mFrame.top-wallpaper.mFrame.top);
1757 } else {
1758 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1759 }
1760 switch (pointer.getAction()) {
1761 case MotionEvent.ACTION_DOWN:
1762 mSendingPointersToWallpaper = true;
1763 break;
1764 case MotionEvent.ACTION_UP:
1765 mSendingPointersToWallpaper = false;
1766 break;
1767 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001768 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1769 } catch (RemoteException e) {
1770 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1771 }
1772 }
1773 }
1774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 public int addWindow(Session session, IWindow client,
1777 WindowManager.LayoutParams attrs, int viewVisibility,
1778 Rect outContentInsets) {
1779 int res = mPolicy.checkAddPermission(attrs);
1780 if (res != WindowManagerImpl.ADD_OKAY) {
1781 return res;
1782 }
Romain Guy06882f82009-06-10 13:36:04 -07001783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 boolean reportNewConfig = false;
1785 WindowState attachedWindow = null;
1786 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 synchronized(mWindowMap) {
1789 // Instantiating a Display requires talking with the simulator,
1790 // so don't do it until we know the system is mostly up and
1791 // running.
1792 if (mDisplay == null) {
1793 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1794 mDisplay = wm.getDefaultDisplay();
1795 mQueue.setDisplay(mDisplay);
1796 reportNewConfig = true;
1797 }
Romain Guy06882f82009-06-10 13:36:04 -07001798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 if (mWindowMap.containsKey(client.asBinder())) {
1800 Log.w(TAG, "Window " + client + " is already added");
1801 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1802 }
1803
1804 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001805 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 if (attachedWindow == null) {
1807 Log.w(TAG, "Attempted to add window with token that is not a window: "
1808 + attrs.token + ". Aborting.");
1809 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1810 }
1811 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1812 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1813 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1814 + attrs.token + ". Aborting.");
1815 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1816 }
1817 }
1818
1819 boolean addToken = false;
1820 WindowToken token = mTokenMap.get(attrs.token);
1821 if (token == null) {
1822 if (attrs.type >= FIRST_APPLICATION_WINDOW
1823 && attrs.type <= LAST_APPLICATION_WINDOW) {
1824 Log.w(TAG, "Attempted to add application window with unknown token "
1825 + attrs.token + ". Aborting.");
1826 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1827 }
1828 if (attrs.type == TYPE_INPUT_METHOD) {
1829 Log.w(TAG, "Attempted to add input method window with unknown token "
1830 + attrs.token + ". Aborting.");
1831 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1832 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001833 if (attrs.type == TYPE_WALLPAPER) {
1834 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1835 + attrs.token + ". Aborting.");
1836 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 token = new WindowToken(attrs.token, -1, false);
1839 addToken = true;
1840 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1841 && attrs.type <= LAST_APPLICATION_WINDOW) {
1842 AppWindowToken atoken = token.appWindowToken;
1843 if (atoken == null) {
1844 Log.w(TAG, "Attempted to add window with non-application token "
1845 + token + ". Aborting.");
1846 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1847 } else if (atoken.removed) {
1848 Log.w(TAG, "Attempted to add window with exiting application token "
1849 + token + ". Aborting.");
1850 return WindowManagerImpl.ADD_APP_EXITING;
1851 }
1852 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1853 // No need for this guy!
1854 if (localLOGV) Log.v(
1855 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1856 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1857 }
1858 } else if (attrs.type == TYPE_INPUT_METHOD) {
1859 if (token.windowType != TYPE_INPUT_METHOD) {
1860 Log.w(TAG, "Attempted to add input method window with bad token "
1861 + attrs.token + ". Aborting.");
1862 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1863 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001864 } else if (attrs.type == TYPE_WALLPAPER) {
1865 if (token.windowType != TYPE_WALLPAPER) {
1866 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1867 + attrs.token + ". Aborting.");
1868 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 }
1871
1872 win = new WindowState(session, client, token,
1873 attachedWindow, attrs, viewVisibility);
1874 if (win.mDeathRecipient == null) {
1875 // Client has apparently died, so there is no reason to
1876 // continue.
1877 Log.w(TAG, "Adding window client " + client.asBinder()
1878 + " that is dead, aborting.");
1879 return WindowManagerImpl.ADD_APP_EXITING;
1880 }
1881
1882 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 res = mPolicy.prepareAddWindowLw(win, attrs);
1885 if (res != WindowManagerImpl.ADD_OKAY) {
1886 return res;
1887 }
1888
1889 // From now on, no exceptions or errors allowed!
1890
1891 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 if (addToken) {
1896 mTokenMap.put(attrs.token, token);
1897 mTokenList.add(token);
1898 }
1899 win.attach();
1900 mWindowMap.put(client.asBinder(), win);
1901
1902 if (attrs.type == TYPE_APPLICATION_STARTING &&
1903 token.appWindowToken != null) {
1904 token.appWindowToken.startingWindow = win;
1905 }
1906
1907 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 if (attrs.type == TYPE_INPUT_METHOD) {
1910 mInputMethodWindow = win;
1911 addInputMethodWindowToListLocked(win);
1912 imMayMove = false;
1913 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1914 mInputMethodDialogs.add(win);
1915 addWindowToListInOrderLocked(win, true);
1916 adjustInputMethodDialogsLocked();
1917 imMayMove = false;
1918 } else {
1919 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001920 if (attrs.type == TYPE_WALLPAPER) {
1921 mLastWallpaperTimeoutTime = 0;
1922 adjustWallpaperWindowsLocked();
1923 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001924 adjustWallpaperWindowsLocked();
1925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 }
Romain Guy06882f82009-06-10 13:36:04 -07001927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 if (mInTouchMode) {
1933 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1934 }
1935 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1936 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1937 }
Romain Guy06882f82009-06-10 13:36:04 -07001938
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001939 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001941 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1942 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 imMayMove = false;
1944 }
1945 }
Romain Guy06882f82009-06-10 13:36:04 -07001946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001948 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
Romain Guy06882f82009-06-10 13:36:04 -07001950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 assignLayersLocked();
1952 // Don't do layout here, the window must call
1953 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 //dump();
1956
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001957 if (focusChanged) {
1958 if (mCurrentFocus != null) {
1959 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1960 }
1961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 if (localLOGV) Log.v(
1963 TAG, "New client " + client.asBinder()
1964 + ": window=" + win);
1965 }
1966
1967 // sendNewConfiguration() checks caller permissions so we must call it with
1968 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1969 // identity anyway, so it's safe to just clear & restore around this whole
1970 // block.
1971 final long origId = Binder.clearCallingIdentity();
1972 if (reportNewConfig) {
1973 sendNewConfiguration();
1974 } else {
1975 // Update Orientation after adding a window, only if the window needs to be
1976 // displayed right away
1977 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001978 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 sendNewConfiguration();
1980 }
1981 }
1982 }
1983 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 return res;
1986 }
Romain Guy06882f82009-06-10 13:36:04 -07001987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 public void removeWindow(Session session, IWindow client) {
1989 synchronized(mWindowMap) {
1990 WindowState win = windowForClientLocked(session, client);
1991 if (win == null) {
1992 return;
1993 }
1994 removeWindowLocked(session, win);
1995 }
1996 }
Romain Guy06882f82009-06-10 13:36:04 -07001997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 public void removeWindowLocked(Session session, WindowState win) {
1999
2000 if (localLOGV || DEBUG_FOCUS) Log.v(
2001 TAG, "Remove " + win + " client="
2002 + Integer.toHexString(System.identityHashCode(
2003 win.mClient.asBinder()))
2004 + ", surface=" + win.mSurface);
2005
2006 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 if (DEBUG_APP_TRANSITIONS) Log.v(
2009 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2010 + " mExiting=" + win.mExiting
2011 + " isAnimating=" + win.isAnimating()
2012 + " app-animation="
2013 + (win.mAppToken != null ? win.mAppToken.animation : null)
2014 + " inPendingTransaction="
2015 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2016 + " mDisplayFrozen=" + mDisplayFrozen);
2017 // Visibility of the removed window. Will be used later to update orientation later on.
2018 boolean wasVisible = false;
2019 // First, see if we need to run an animation. If we do, we have
2020 // to hold off on removing the window until the animation is done.
2021 // If the display is frozen, just remove immediately, since the
2022 // animation wouldn't be seen.
2023 if (win.mSurface != null && !mDisplayFrozen) {
2024 // If we are not currently running the exit animation, we
2025 // need to see about starting one.
2026 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2029 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2030 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2031 }
2032 // Try starting an animation.
2033 if (applyAnimationLocked(win, transit, false)) {
2034 win.mExiting = true;
2035 }
2036 }
2037 if (win.mExiting || win.isAnimating()) {
2038 // The exit animation is running... wait for it!
2039 //Log.i(TAG, "*** Running exit animation...");
2040 win.mExiting = true;
2041 win.mRemoveOnExit = true;
2042 mLayoutNeeded = true;
2043 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2044 performLayoutAndPlaceSurfacesLocked();
2045 if (win.mAppToken != null) {
2046 win.mAppToken.updateReportedVisibilityLocked();
2047 }
2048 //dump();
2049 Binder.restoreCallingIdentity(origId);
2050 return;
2051 }
2052 }
2053
2054 removeWindowInnerLocked(session, win);
2055 // Removing a visible window will effect the computed orientation
2056 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002057 if (wasVisible && computeForcedAppOrientationLocked()
2058 != mForcedAppOrientation) {
2059 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
2061 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2062 Binder.restoreCallingIdentity(origId);
2063 }
Romain Guy06882f82009-06-10 13:36:04 -07002064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002066 mKeyWaiter.finishedKey(session, win.mClient, true,
2067 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2069 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 if (mInputMethodTarget == win) {
2074 moveInputMethodWindowsIfNeededLocked(false);
2075 }
Romain Guy06882f82009-06-10 13:36:04 -07002076
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002077 if (false) {
2078 RuntimeException e = new RuntimeException("here");
2079 e.fillInStackTrace();
2080 Log.w(TAG, "Removing window " + win, e);
2081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 mPolicy.removeWindowLw(win);
2084 win.removeLocked();
2085
2086 mWindowMap.remove(win.mClient.asBinder());
2087 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002088 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089
2090 if (mInputMethodWindow == win) {
2091 mInputMethodWindow = null;
2092 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2093 mInputMethodDialogs.remove(win);
2094 }
Romain Guy06882f82009-06-10 13:36:04 -07002095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 final WindowToken token = win.mToken;
2097 final AppWindowToken atoken = win.mAppToken;
2098 token.windows.remove(win);
2099 if (atoken != null) {
2100 atoken.allAppWindows.remove(win);
2101 }
2102 if (localLOGV) Log.v(
2103 TAG, "**** Removing window " + win + ": count="
2104 + token.windows.size());
2105 if (token.windows.size() == 0) {
2106 if (!token.explicit) {
2107 mTokenMap.remove(token.token);
2108 mTokenList.remove(token);
2109 } else if (atoken != null) {
2110 atoken.firstWindowDrawn = false;
2111 }
2112 }
2113
2114 if (atoken != null) {
2115 if (atoken.startingWindow == win) {
2116 atoken.startingWindow = null;
2117 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2118 // If this is the last window and we had requested a starting
2119 // transition window, well there is no point now.
2120 atoken.startingData = null;
2121 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2122 // If this is the last window except for a starting transition
2123 // window, we need to get rid of the starting transition.
2124 if (DEBUG_STARTING_WINDOW) {
2125 Log.v(TAG, "Schedule remove starting " + token
2126 + ": no more real windows");
2127 }
2128 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2129 mH.sendMessage(m);
2130 }
2131 }
Romain Guy06882f82009-06-10 13:36:04 -07002132
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002133 if (win.mAttrs.type == TYPE_WALLPAPER) {
2134 mLastWallpaperTimeoutTime = 0;
2135 adjustWallpaperWindowsLocked();
2136 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002137 adjustWallpaperWindowsLocked();
2138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 if (!mInLayout) {
2141 assignLayersLocked();
2142 mLayoutNeeded = true;
2143 performLayoutAndPlaceSurfacesLocked();
2144 if (win.mAppToken != null) {
2145 win.mAppToken.updateReportedVisibilityLocked();
2146 }
2147 }
2148 }
2149
2150 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2151 long origId = Binder.clearCallingIdentity();
2152 try {
2153 synchronized (mWindowMap) {
2154 WindowState w = windowForClientLocked(session, client);
2155 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002156 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 Surface.openTransaction();
2158 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002159 if (SHOW_TRANSACTIONS) Log.i(
2160 TAG, " SURFACE " + w.mSurface
2161 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 w.mSurface.setTransparentRegionHint(region);
2163 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002164 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 Surface.closeTransaction();
2166 }
2167 }
2168 }
2169 } finally {
2170 Binder.restoreCallingIdentity(origId);
2171 }
2172 }
2173
2174 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002175 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 Rect visibleInsets) {
2177 long origId = Binder.clearCallingIdentity();
2178 try {
2179 synchronized (mWindowMap) {
2180 WindowState w = windowForClientLocked(session, client);
2181 if (w != null) {
2182 w.mGivenInsetsPending = false;
2183 w.mGivenContentInsets.set(contentInsets);
2184 w.mGivenVisibleInsets.set(visibleInsets);
2185 w.mTouchableInsets = touchableInsets;
2186 mLayoutNeeded = true;
2187 performLayoutAndPlaceSurfacesLocked();
2188 }
2189 }
2190 } finally {
2191 Binder.restoreCallingIdentity(origId);
2192 }
2193 }
Romain Guy06882f82009-06-10 13:36:04 -07002194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 public void getWindowDisplayFrame(Session session, IWindow client,
2196 Rect outDisplayFrame) {
2197 synchronized(mWindowMap) {
2198 WindowState win = windowForClientLocked(session, client);
2199 if (win == null) {
2200 outDisplayFrame.setEmpty();
2201 return;
2202 }
2203 outDisplayFrame.set(win.mDisplayFrame);
2204 }
2205 }
2206
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002207 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2208 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002209 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2210 window.mWallpaperX = x;
2211 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002212 window.mWallpaperXStep = xStep;
2213 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002214 if (updateWallpaperOffsetLocked(window, true)) {
2215 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002216 }
2217 }
2218 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002219
Dianne Hackborn75804932009-10-20 20:15:20 -07002220 void wallpaperCommandComplete(IBinder window, Bundle result) {
2221 synchronized (mWindowMap) {
2222 if (mWaitingOnWallpaper != null &&
2223 mWaitingOnWallpaper.mClient.asBinder() == window) {
2224 mWaitingOnWallpaper = null;
2225 mWindowMap.notifyAll();
2226 }
2227 }
2228 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002229
Dianne Hackborn75804932009-10-20 20:15:20 -07002230 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2231 String action, int x, int y, int z, Bundle extras, boolean sync) {
2232 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2233 || window == mUpperWallpaperTarget) {
2234 boolean doWait = sync;
2235 int curTokenIndex = mWallpaperTokens.size();
2236 while (curTokenIndex > 0) {
2237 curTokenIndex--;
2238 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2239 int curWallpaperIndex = token.windows.size();
2240 while (curWallpaperIndex > 0) {
2241 curWallpaperIndex--;
2242 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2243 try {
2244 wallpaper.mClient.dispatchWallpaperCommand(action,
2245 x, y, z, extras, sync);
2246 // We only want to be synchronous with one wallpaper.
2247 sync = false;
2248 } catch (RemoteException e) {
2249 }
2250 }
2251 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002252
Dianne Hackborn75804932009-10-20 20:15:20 -07002253 if (doWait) {
2254 // XXX Need to wait for result.
2255 }
2256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002257
Dianne Hackborn75804932009-10-20 20:15:20 -07002258 return null;
2259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 public int relayoutWindow(Session session, IWindow client,
2262 WindowManager.LayoutParams attrs, int requestedWidth,
2263 int requestedHeight, int viewVisibility, boolean insetsPending,
2264 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2265 Surface outSurface) {
2266 boolean displayed = false;
2267 boolean inTouchMode;
2268 Configuration newConfig = null;
2269 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 synchronized(mWindowMap) {
2272 WindowState win = windowForClientLocked(session, client);
2273 if (win == null) {
2274 return 0;
2275 }
2276 win.mRequestedWidth = requestedWidth;
2277 win.mRequestedHeight = requestedHeight;
2278
2279 if (attrs != null) {
2280 mPolicy.adjustWindowParamsLw(attrs);
2281 }
Romain Guy06882f82009-06-10 13:36:04 -07002282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 int attrChanges = 0;
2284 int flagChanges = 0;
2285 if (attrs != null) {
2286 flagChanges = win.mAttrs.flags ^= attrs.flags;
2287 attrChanges = win.mAttrs.copyFrom(attrs);
2288 }
2289
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002290 if (DEBUG_LAYOUT) Log.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291
2292 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2293 win.mAlpha = attrs.alpha;
2294 }
2295
2296 final boolean scaledWindow =
2297 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2298
2299 if (scaledWindow) {
2300 // requested{Width|Height} Surface's physical size
2301 // attrs.{width|height} Size on screen
2302 win.mHScale = (attrs.width != requestedWidth) ?
2303 (attrs.width / (float)requestedWidth) : 1.0f;
2304 win.mVScale = (attrs.height != requestedHeight) ?
2305 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002306 } else {
2307 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 }
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 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002379 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2380 && win.mAppToken != null
2381 && win.mAppToken.startingWindow != null) {
2382 // Special handling of starting window over the base
2383 // window of the app: propagate lock screen flags to it,
2384 // to provide the correct semantics while starting.
2385 final int mask =
2386 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
2387 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
2388 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2389 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 } else {
2392 win.mEnterAnimationPending = false;
2393 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002394 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2395 + ": mExiting=" + win.mExiting
2396 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 // If we are not currently running the exit animation, we
2398 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002399 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 // Try starting an animation; if there isn't one, we
2401 // can destroy the surface right away.
2402 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2403 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2404 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2405 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002406 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002408 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 win.mExiting = true;
2410 mKeyWaiter.finishedKey(session, client, true,
2411 KeyWaiter.RETURN_NOTHING);
2412 } else if (win.isAnimating()) {
2413 // Currently in a hide animation... turn this into
2414 // an exit.
2415 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002416 } else if (win == mWallpaperTarget) {
2417 // If the wallpaper is currently behind this
2418 // window, we need to change both of them inside
2419 // of a transaction to avoid artifacts.
2420 win.mExiting = true;
2421 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 } else {
2423 if (mInputMethodWindow == win) {
2424 mInputMethodWindow = null;
2425 }
2426 win.destroySurfaceLocked();
2427 }
2428 }
2429 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002430
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002431 if (win.mSurface == null || (win.getAttrs().flags
2432 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2433 || win.mSurfacePendingDestroy) {
2434 // We are being called from a local process, which
2435 // means outSurface holds its current surface. Ensure the
2436 // surface object is cleared, but we don't want it actually
2437 // destroyed at this point.
2438 win.mSurfacePendingDestroy = false;
2439 outSurface.release();
2440 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2441 } else if (win.mSurface != null) {
2442 if (DEBUG_VISIBILITY) Log.i(TAG,
2443 "Keeping surface, will report destroy: " + win);
2444 win.mReportDestroySurface = true;
2445 outSurface.copyFrom(win.mSurface);
2446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
2448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 if (focusMayChange) {
2450 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2451 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 imMayMove = false;
2453 }
2454 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2455 }
Romain Guy06882f82009-06-10 13:36:04 -07002456
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002457 // updateFocusedWindowLocked() already assigned layers so we only need to
2458 // reassign them at this point if the IM window state gets shuffled
2459 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002462 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2463 // Little hack here -- we -should- be able to rely on the
2464 // function to return true if the IME has moved and needs
2465 // its layer recomputed. However, if the IME was hidden
2466 // and isn't actually moved in the list, its layer may be
2467 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 assignLayers = true;
2469 }
2470 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002471 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002472 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002473 assignLayers = true;
2474 }
2475 }
Romain Guy06882f82009-06-10 13:36:04 -07002476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 mLayoutNeeded = true;
2478 win.mGivenInsetsPending = insetsPending;
2479 if (assignLayers) {
2480 assignLayersLocked();
2481 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002482 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002484 if (displayed && win.mIsWallpaper) {
2485 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002486 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 if (win.mAppToken != null) {
2489 win.mAppToken.updateReportedVisibilityLocked();
2490 }
2491 outFrame.set(win.mFrame);
2492 outContentInsets.set(win.mContentInsets);
2493 outVisibleInsets.set(win.mVisibleInsets);
2494 if (localLOGV) Log.v(
2495 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002496 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 + ", requestedHeight=" + requestedHeight
2498 + ", viewVisibility=" + viewVisibility
2499 + "\nRelayout returning frame=" + outFrame
2500 + ", surface=" + outSurface);
2501
2502 if (localLOGV || DEBUG_FOCUS) Log.v(
2503 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2504
2505 inTouchMode = mInTouchMode;
2506 }
2507
2508 if (newConfig != null) {
2509 sendNewConfiguration();
2510 }
Romain Guy06882f82009-06-10 13:36:04 -07002511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2515 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2516 }
2517
2518 public void finishDrawingWindow(Session session, IWindow client) {
2519 final long origId = Binder.clearCallingIdentity();
2520 synchronized(mWindowMap) {
2521 WindowState win = windowForClientLocked(session, client);
2522 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002523 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2524 adjustWallpaperWindowsLocked();
2525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 mLayoutNeeded = true;
2527 performLayoutAndPlaceSurfacesLocked();
2528 }
2529 }
2530 Binder.restoreCallingIdentity(origId);
2531 }
2532
2533 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2534 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2535 + (lp != null ? lp.packageName : null)
2536 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2537 if (lp != null && lp.windowAnimations != 0) {
2538 // If this is a system resource, don't try to load it from the
2539 // application resources. It is nice to avoid loading application
2540 // resources if we can.
2541 String packageName = lp.packageName != null ? lp.packageName : "android";
2542 int resId = lp.windowAnimations;
2543 if ((resId&0xFF000000) == 0x01000000) {
2544 packageName = "android";
2545 }
2546 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2547 + packageName);
2548 return AttributeCache.instance().get(packageName, resId,
2549 com.android.internal.R.styleable.WindowAnimation);
2550 }
2551 return null;
2552 }
Romain Guy06882f82009-06-10 13:36:04 -07002553
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002554 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2555 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2556 + packageName + " resId=0x" + Integer.toHexString(resId));
2557 if (packageName != null) {
2558 if ((resId&0xFF000000) == 0x01000000) {
2559 packageName = "android";
2560 }
2561 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2562 + packageName);
2563 return AttributeCache.instance().get(packageName, resId,
2564 com.android.internal.R.styleable.WindowAnimation);
2565 }
2566 return null;
2567 }
2568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 private void applyEnterAnimationLocked(WindowState win) {
2570 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2571 if (win.mEnterAnimationPending) {
2572 win.mEnterAnimationPending = false;
2573 transit = WindowManagerPolicy.TRANSIT_ENTER;
2574 }
2575
2576 applyAnimationLocked(win, transit, true);
2577 }
2578
2579 private boolean applyAnimationLocked(WindowState win,
2580 int transit, boolean isEntrance) {
2581 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2582 // If we are trying to apply an animation, but already running
2583 // an animation of the same type, then just leave that one alone.
2584 return true;
2585 }
Romain Guy06882f82009-06-10 13:36:04 -07002586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 // Only apply an animation if the display isn't frozen. If it is
2588 // frozen, there is no reason to animate and it can cause strange
2589 // artifacts when we unfreeze the display if some different animation
2590 // is running.
2591 if (!mDisplayFrozen) {
2592 int anim = mPolicy.selectAnimationLw(win, transit);
2593 int attr = -1;
2594 Animation a = null;
2595 if (anim != 0) {
2596 a = AnimationUtils.loadAnimation(mContext, anim);
2597 } else {
2598 switch (transit) {
2599 case WindowManagerPolicy.TRANSIT_ENTER:
2600 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2601 break;
2602 case WindowManagerPolicy.TRANSIT_EXIT:
2603 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2604 break;
2605 case WindowManagerPolicy.TRANSIT_SHOW:
2606 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2607 break;
2608 case WindowManagerPolicy.TRANSIT_HIDE:
2609 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2610 break;
2611 }
2612 if (attr >= 0) {
2613 a = loadAnimation(win.mAttrs, attr);
2614 }
2615 }
2616 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2617 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2618 + " mAnimation=" + win.mAnimation
2619 + " isEntrance=" + isEntrance);
2620 if (a != null) {
2621 if (DEBUG_ANIM) {
2622 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002623 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2625 }
2626 win.setAnimation(a);
2627 win.mAnimationIsEntrance = isEntrance;
2628 }
2629 } else {
2630 win.clearAnimation();
2631 }
2632
2633 return win.mAnimation != null;
2634 }
2635
2636 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2637 int anim = 0;
2638 Context context = mContext;
2639 if (animAttr >= 0) {
2640 AttributeCache.Entry ent = getCachedAnimations(lp);
2641 if (ent != null) {
2642 context = ent.context;
2643 anim = ent.array.getResourceId(animAttr, 0);
2644 }
2645 }
2646 if (anim != 0) {
2647 return AnimationUtils.loadAnimation(context, anim);
2648 }
2649 return null;
2650 }
Romain Guy06882f82009-06-10 13:36:04 -07002651
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002652 private Animation loadAnimation(String packageName, int resId) {
2653 int anim = 0;
2654 Context context = mContext;
2655 if (resId >= 0) {
2656 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2657 if (ent != null) {
2658 context = ent.context;
2659 anim = resId;
2660 }
2661 }
2662 if (anim != 0) {
2663 return AnimationUtils.loadAnimation(context, anim);
2664 }
2665 return null;
2666 }
2667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 private boolean applyAnimationLocked(AppWindowToken wtoken,
2669 WindowManager.LayoutParams lp, int transit, boolean enter) {
2670 // Only apply an animation if the display isn't frozen. If it is
2671 // frozen, there is no reason to animate and it can cause strange
2672 // artifacts when we unfreeze the display if some different animation
2673 // is running.
2674 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002675 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002676 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002677 a = new FadeInOutAnimation(enter);
2678 if (DEBUG_ANIM) Log.v(TAG,
2679 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002680 } else if (mNextAppTransitionPackage != null) {
2681 a = loadAnimation(mNextAppTransitionPackage, enter ?
2682 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002683 } else {
2684 int animAttr = 0;
2685 switch (transit) {
2686 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2687 animAttr = enter
2688 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2689 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2690 break;
2691 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2692 animAttr = enter
2693 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2694 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2695 break;
2696 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2697 animAttr = enter
2698 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2699 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2700 break;
2701 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2702 animAttr = enter
2703 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2704 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2705 break;
2706 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2707 animAttr = enter
2708 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2709 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2710 break;
2711 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2712 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002713 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002714 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2715 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002716 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002717 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002718 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2719 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002720 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002721 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002722 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002723 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2724 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2725 break;
2726 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2727 animAttr = enter
2728 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2729 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2730 break;
2731 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2732 animAttr = enter
2733 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2734 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002735 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002736 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002737 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002738 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2739 + " anim=" + a
2740 + " animAttr=0x" + Integer.toHexString(animAttr)
2741 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 if (a != null) {
2744 if (DEBUG_ANIM) {
2745 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002746 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2748 }
2749 wtoken.setAnimation(a);
2750 }
2751 } else {
2752 wtoken.clearAnimation();
2753 }
2754
2755 return wtoken.animation != null;
2756 }
2757
2758 // -------------------------------------------------------------
2759 // Application Window Tokens
2760 // -------------------------------------------------------------
2761
2762 public void validateAppTokens(List tokens) {
2763 int v = tokens.size()-1;
2764 int m = mAppTokens.size()-1;
2765 while (v >= 0 && m >= 0) {
2766 AppWindowToken wtoken = mAppTokens.get(m);
2767 if (wtoken.removed) {
2768 m--;
2769 continue;
2770 }
2771 if (tokens.get(v) != wtoken.token) {
2772 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2773 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2774 }
2775 v--;
2776 m--;
2777 }
2778 while (v >= 0) {
2779 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2780 v--;
2781 }
2782 while (m >= 0) {
2783 AppWindowToken wtoken = mAppTokens.get(m);
2784 if (!wtoken.removed) {
2785 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2786 }
2787 m--;
2788 }
2789 }
2790
2791 boolean checkCallingPermission(String permission, String func) {
2792 // Quick check: if the calling permission is me, it's all okay.
2793 if (Binder.getCallingPid() == Process.myPid()) {
2794 return true;
2795 }
Romain Guy06882f82009-06-10 13:36:04 -07002796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 if (mContext.checkCallingPermission(permission)
2798 == PackageManager.PERMISSION_GRANTED) {
2799 return true;
2800 }
2801 String msg = "Permission Denial: " + func + " from pid="
2802 + Binder.getCallingPid()
2803 + ", uid=" + Binder.getCallingUid()
2804 + " requires " + permission;
2805 Log.w(TAG, msg);
2806 return false;
2807 }
Romain Guy06882f82009-06-10 13:36:04 -07002808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 AppWindowToken findAppWindowToken(IBinder token) {
2810 WindowToken wtoken = mTokenMap.get(token);
2811 if (wtoken == null) {
2812 return null;
2813 }
2814 return wtoken.appWindowToken;
2815 }
Romain Guy06882f82009-06-10 13:36:04 -07002816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 public void addWindowToken(IBinder token, int type) {
2818 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2819 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002820 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 }
Romain Guy06882f82009-06-10 13:36:04 -07002822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 synchronized(mWindowMap) {
2824 WindowToken wtoken = mTokenMap.get(token);
2825 if (wtoken != null) {
2826 Log.w(TAG, "Attempted to add existing input method token: " + token);
2827 return;
2828 }
2829 wtoken = new WindowToken(token, type, true);
2830 mTokenMap.put(token, wtoken);
2831 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002832 if (type == TYPE_WALLPAPER) {
2833 mWallpaperTokens.add(wtoken);
2834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 }
2836 }
Romain Guy06882f82009-06-10 13:36:04 -07002837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 public void removeWindowToken(IBinder token) {
2839 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2840 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002841 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 }
2843
2844 final long origId = Binder.clearCallingIdentity();
2845 synchronized(mWindowMap) {
2846 WindowToken wtoken = mTokenMap.remove(token);
2847 mTokenList.remove(wtoken);
2848 if (wtoken != null) {
2849 boolean delayed = false;
2850 if (!wtoken.hidden) {
2851 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 final int N = wtoken.windows.size();
2854 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 for (int i=0; i<N; i++) {
2857 WindowState win = wtoken.windows.get(i);
2858
2859 if (win.isAnimating()) {
2860 delayed = true;
2861 }
Romain Guy06882f82009-06-10 13:36:04 -07002862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 if (win.isVisibleNow()) {
2864 applyAnimationLocked(win,
2865 WindowManagerPolicy.TRANSIT_EXIT, false);
2866 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2867 KeyWaiter.RETURN_NOTHING);
2868 changed = true;
2869 }
2870 }
2871
2872 if (changed) {
2873 mLayoutNeeded = true;
2874 performLayoutAndPlaceSurfacesLocked();
2875 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2876 }
Romain Guy06882f82009-06-10 13:36:04 -07002877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 if (delayed) {
2879 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002880 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2881 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 }
2883 }
Romain Guy06882f82009-06-10 13:36:04 -07002884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 } else {
2886 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2887 }
2888 }
2889 Binder.restoreCallingIdentity(origId);
2890 }
2891
2892 public void addAppToken(int addPos, IApplicationToken token,
2893 int groupId, int requestedOrientation, boolean fullscreen) {
2894 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2895 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002896 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 }
Romain Guy06882f82009-06-10 13:36:04 -07002898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 synchronized(mWindowMap) {
2900 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2901 if (wtoken != null) {
2902 Log.w(TAG, "Attempted to add existing app token: " + token);
2903 return;
2904 }
2905 wtoken = new AppWindowToken(token);
2906 wtoken.groupId = groupId;
2907 wtoken.appFullscreen = fullscreen;
2908 wtoken.requestedOrientation = requestedOrientation;
2909 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002910 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 mTokenMap.put(token.asBinder(), wtoken);
2912 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 // Application tokens start out hidden.
2915 wtoken.hidden = true;
2916 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 //dump();
2919 }
2920 }
Romain Guy06882f82009-06-10 13:36:04 -07002921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 public void setAppGroupId(IBinder token, int groupId) {
2923 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2924 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002925 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 }
2927
2928 synchronized(mWindowMap) {
2929 AppWindowToken wtoken = findAppWindowToken(token);
2930 if (wtoken == null) {
2931 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2932 return;
2933 }
2934 wtoken.groupId = groupId;
2935 }
2936 }
Romain Guy06882f82009-06-10 13:36:04 -07002937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 public int getOrientationFromWindowsLocked() {
2939 int pos = mWindows.size() - 1;
2940 while (pos >= 0) {
2941 WindowState wtoken = (WindowState) mWindows.get(pos);
2942 pos--;
2943 if (wtoken.mAppToken != null) {
2944 // We hit an application window. so the orientation will be determined by the
2945 // app window. No point in continuing further.
2946 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2947 }
2948 if (!wtoken.isVisibleLw()) {
2949 continue;
2950 }
2951 int req = wtoken.mAttrs.screenOrientation;
2952 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2953 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2954 continue;
2955 } else {
2956 return req;
2957 }
2958 }
2959 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2960 }
Romain Guy06882f82009-06-10 13:36:04 -07002961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 public int getOrientationFromAppTokensLocked() {
2963 int pos = mAppTokens.size() - 1;
2964 int curGroup = 0;
2965 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002966 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002968 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 while (pos >= 0) {
2970 AppWindowToken wtoken = mAppTokens.get(pos);
2971 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002972 // if we're about to tear down this window and not seek for
2973 // the behind activity, don't use it for orientation
2974 if (!findingBehind
2975 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002976 continue;
2977 }
2978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 if (!haveGroup) {
2980 // We ignore any hidden applications on the top.
2981 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2982 continue;
2983 }
2984 haveGroup = true;
2985 curGroup = wtoken.groupId;
2986 lastOrientation = wtoken.requestedOrientation;
2987 } else if (curGroup != wtoken.groupId) {
2988 // If we have hit a new application group, and the bottom
2989 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002990 // the orientation behind it, and the last app was
2991 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002993 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2994 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 return lastOrientation;
2996 }
2997 }
2998 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002999 // If this application is fullscreen, and didn't explicitly say
3000 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08003002 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07003003 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07003004 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 return or;
3006 }
3007 // If this application has requested an explicit orientation,
3008 // then use it.
3009 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3010 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3011 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3012 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3013 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3014 return or;
3015 }
Owen Lin3413b892009-05-01 17:12:32 -07003016 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 }
3018 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3019 }
Romain Guy06882f82009-06-10 13:36:04 -07003020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003022 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003023 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3024 "updateOrientationFromAppTokens()")) {
3025 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3026 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 Configuration config;
3029 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003030 config = updateOrientationFromAppTokensUnchecked(currentConfig,
3031 freezeThisOneIfNeeded);
3032 Binder.restoreCallingIdentity(ident);
3033 return config;
3034 }
3035
3036 Configuration updateOrientationFromAppTokensUnchecked(
3037 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
3038 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003040 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003041 if (config != null) {
3042 mLayoutNeeded = true;
3043 performLayoutAndPlaceSurfacesLocked();
3044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 return config;
3047 }
Romain Guy06882f82009-06-10 13:36:04 -07003048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 /*
3050 * The orientation is computed from non-application windows first. If none of
3051 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003052 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3054 * android.os.IBinder)
3055 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003056 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07003057 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 long ident = Binder.clearCallingIdentity();
3060 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003061 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 if (req != mForcedAppOrientation) {
3064 changed = true;
3065 mForcedAppOrientation = req;
3066 //send a message to Policy indicating orientation change to take
3067 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003068 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 }
Romain Guy06882f82009-06-10 13:36:04 -07003070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 if (changed) {
3072 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07003073 WindowManagerPolicy.USE_LAST_ROTATION,
3074 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 if (changed) {
3076 if (freezeThisOneIfNeeded != null) {
3077 AppWindowToken wtoken = findAppWindowToken(
3078 freezeThisOneIfNeeded);
3079 if (wtoken != null) {
3080 startAppFreezingScreenLocked(wtoken,
3081 ActivityInfo.CONFIG_ORIENTATION);
3082 }
3083 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003084 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 }
3086 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003087
3088 // No obvious action we need to take, but if our current
3089 // state mismatches the activity maanager's, update it
3090 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003091 mTempConfiguration.setToDefaults();
3092 if (computeNewConfigurationLocked(mTempConfiguration)) {
3093 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003094 return new Configuration(mTempConfiguration);
3095 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003096 }
3097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 } finally {
3099 Binder.restoreCallingIdentity(ident);
3100 }
Romain Guy06882f82009-06-10 13:36:04 -07003101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 return null;
3103 }
Romain Guy06882f82009-06-10 13:36:04 -07003104
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003105 int computeForcedAppOrientationLocked() {
3106 int req = getOrientationFromWindowsLocked();
3107 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3108 req = getOrientationFromAppTokensLocked();
3109 }
3110 return req;
3111 }
Romain Guy06882f82009-06-10 13:36:04 -07003112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3114 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3115 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003116 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 }
Romain Guy06882f82009-06-10 13:36:04 -07003118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 synchronized(mWindowMap) {
3120 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3121 if (wtoken == null) {
3122 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3123 return;
3124 }
Romain Guy06882f82009-06-10 13:36:04 -07003125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 wtoken.requestedOrientation = requestedOrientation;
3127 }
3128 }
Romain Guy06882f82009-06-10 13:36:04 -07003129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 public int getAppOrientation(IApplicationToken token) {
3131 synchronized(mWindowMap) {
3132 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3133 if (wtoken == null) {
3134 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3135 }
Romain Guy06882f82009-06-10 13:36:04 -07003136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 return wtoken.requestedOrientation;
3138 }
3139 }
Romain Guy06882f82009-06-10 13:36:04 -07003140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3142 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3143 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003144 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 }
3146
3147 synchronized(mWindowMap) {
3148 boolean changed = false;
3149 if (token == null) {
3150 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3151 changed = mFocusedApp != null;
3152 mFocusedApp = null;
3153 mKeyWaiter.tickle();
3154 } else {
3155 AppWindowToken newFocus = findAppWindowToken(token);
3156 if (newFocus == null) {
3157 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3158 return;
3159 }
3160 changed = mFocusedApp != newFocus;
3161 mFocusedApp = newFocus;
3162 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3163 mKeyWaiter.tickle();
3164 }
3165
3166 if (moveFocusNow && changed) {
3167 final long origId = Binder.clearCallingIdentity();
3168 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3169 Binder.restoreCallingIdentity(origId);
3170 }
3171 }
3172 }
3173
3174 public void prepareAppTransition(int transit) {
3175 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3176 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003177 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 }
Romain Guy06882f82009-06-10 13:36:04 -07003179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 synchronized(mWindowMap) {
3181 if (DEBUG_APP_TRANSITIONS) Log.v(
3182 TAG, "Prepare app transition: transit=" + transit
3183 + " mNextAppTransition=" + mNextAppTransition);
3184 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003185 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3186 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003188 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3189 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3190 // Opening a new task always supersedes a close for the anim.
3191 mNextAppTransition = transit;
3192 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3193 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3194 // Opening a new activity always supersedes a close for the anim.
3195 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 }
3197 mAppTransitionReady = false;
3198 mAppTransitionTimeout = false;
3199 mStartingIconInTransition = false;
3200 mSkipAppTransitionAnimation = false;
3201 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3202 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3203 5000);
3204 }
3205 }
3206 }
3207
3208 public int getPendingAppTransition() {
3209 return mNextAppTransition;
3210 }
Romain Guy06882f82009-06-10 13:36:04 -07003211
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003212 public void overridePendingAppTransition(String packageName,
3213 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003214 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003215 mNextAppTransitionPackage = packageName;
3216 mNextAppTransitionEnter = enterAnim;
3217 mNextAppTransitionExit = exitAnim;
3218 }
3219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 public void executeAppTransition() {
3222 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3223 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003224 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 }
Romain Guy06882f82009-06-10 13:36:04 -07003226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003228 if (DEBUG_APP_TRANSITIONS) {
3229 RuntimeException e = new RuntimeException("here");
3230 e.fillInStackTrace();
3231 Log.w(TAG, "Execute app transition: mNextAppTransition="
3232 + mNextAppTransition, e);
3233 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003234 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 mAppTransitionReady = true;
3236 final long origId = Binder.clearCallingIdentity();
3237 performLayoutAndPlaceSurfacesLocked();
3238 Binder.restoreCallingIdentity(origId);
3239 }
3240 }
3241 }
3242
3243 public void setAppStartingWindow(IBinder token, String pkg,
3244 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3245 IBinder transferFrom, boolean createIfNeeded) {
3246 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3247 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003248 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 }
3250
3251 synchronized(mWindowMap) {
3252 if (DEBUG_STARTING_WINDOW) Log.v(
3253 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3254 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 AppWindowToken wtoken = findAppWindowToken(token);
3257 if (wtoken == null) {
3258 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3259 return;
3260 }
3261
3262 // If the display is frozen, we won't do anything until the
3263 // actual window is displayed so there is no reason to put in
3264 // the starting window.
3265 if (mDisplayFrozen) {
3266 return;
3267 }
Romain Guy06882f82009-06-10 13:36:04 -07003268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 if (wtoken.startingData != null) {
3270 return;
3271 }
Romain Guy06882f82009-06-10 13:36:04 -07003272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 if (transferFrom != null) {
3274 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3275 if (ttoken != null) {
3276 WindowState startingWindow = ttoken.startingWindow;
3277 if (startingWindow != null) {
3278 if (mStartingIconInTransition) {
3279 // In this case, the starting icon has already
3280 // been displayed, so start letting windows get
3281 // shown immediately without any more transitions.
3282 mSkipAppTransitionAnimation = true;
3283 }
3284 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3285 "Moving existing starting from " + ttoken
3286 + " to " + wtoken);
3287 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 // Transfer the starting window over to the new
3290 // token.
3291 wtoken.startingData = ttoken.startingData;
3292 wtoken.startingView = ttoken.startingView;
3293 wtoken.startingWindow = startingWindow;
3294 ttoken.startingData = null;
3295 ttoken.startingView = null;
3296 ttoken.startingWindow = null;
3297 ttoken.startingMoved = true;
3298 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003299 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003301 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3302 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 mWindows.remove(startingWindow);
3304 ttoken.windows.remove(startingWindow);
3305 ttoken.allAppWindows.remove(startingWindow);
3306 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 // Propagate other interesting state between the
3309 // tokens. If the old token is displayed, we should
3310 // immediately force the new one to be displayed. If
3311 // it is animating, we need to move that animation to
3312 // the new one.
3313 if (ttoken.allDrawn) {
3314 wtoken.allDrawn = true;
3315 }
3316 if (ttoken.firstWindowDrawn) {
3317 wtoken.firstWindowDrawn = true;
3318 }
3319 if (!ttoken.hidden) {
3320 wtoken.hidden = false;
3321 wtoken.hiddenRequested = false;
3322 wtoken.willBeHidden = false;
3323 }
3324 if (wtoken.clientHidden != ttoken.clientHidden) {
3325 wtoken.clientHidden = ttoken.clientHidden;
3326 wtoken.sendAppVisibilityToClients();
3327 }
3328 if (ttoken.animation != null) {
3329 wtoken.animation = ttoken.animation;
3330 wtoken.animating = ttoken.animating;
3331 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3332 ttoken.animation = null;
3333 ttoken.animLayerAdjustment = 0;
3334 wtoken.updateLayers();
3335 ttoken.updateLayers();
3336 }
Romain Guy06882f82009-06-10 13:36:04 -07003337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 mLayoutNeeded = true;
3340 performLayoutAndPlaceSurfacesLocked();
3341 Binder.restoreCallingIdentity(origId);
3342 return;
3343 } else if (ttoken.startingData != null) {
3344 // The previous app was getting ready to show a
3345 // starting window, but hasn't yet done so. Steal it!
3346 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3347 "Moving pending starting from " + ttoken
3348 + " to " + wtoken);
3349 wtoken.startingData = ttoken.startingData;
3350 ttoken.startingData = null;
3351 ttoken.startingMoved = true;
3352 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3353 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3354 // want to process the message ASAP, before any other queued
3355 // messages.
3356 mH.sendMessageAtFrontOfQueue(m);
3357 return;
3358 }
3359 }
3360 }
3361
3362 // There is no existing starting window, and the caller doesn't
3363 // want us to create one, so that's it!
3364 if (!createIfNeeded) {
3365 return;
3366 }
Romain Guy06882f82009-06-10 13:36:04 -07003367
Dianne Hackborn284ac932009-08-28 10:34:25 -07003368 // If this is a translucent or wallpaper window, then don't
3369 // show a starting window -- the current effect (a full-screen
3370 // opaque starting window that fades away to the real contents
3371 // when it is ready) does not work for this.
3372 if (theme != 0) {
3373 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3374 com.android.internal.R.styleable.Window);
3375 if (ent.array.getBoolean(
3376 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3377 return;
3378 }
3379 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003380 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3381 return;
3382 }
3383 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003384 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3385 return;
3386 }
3387 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 mStartingIconInTransition = true;
3390 wtoken.startingData = new StartingData(
3391 pkg, theme, nonLocalizedLabel,
3392 labelRes, icon);
3393 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3394 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3395 // want to process the message ASAP, before any other queued
3396 // messages.
3397 mH.sendMessageAtFrontOfQueue(m);
3398 }
3399 }
3400
3401 public void setAppWillBeHidden(IBinder token) {
3402 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3403 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003404 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 }
3406
3407 AppWindowToken wtoken;
3408
3409 synchronized(mWindowMap) {
3410 wtoken = findAppWindowToken(token);
3411 if (wtoken == null) {
3412 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3413 return;
3414 }
3415 wtoken.willBeHidden = true;
3416 }
3417 }
Romain Guy06882f82009-06-10 13:36:04 -07003418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3420 boolean visible, int transit, boolean performLayout) {
3421 boolean delayed = false;
3422
3423 if (wtoken.clientHidden == visible) {
3424 wtoken.clientHidden = !visible;
3425 wtoken.sendAppVisibilityToClients();
3426 }
Romain Guy06882f82009-06-10 13:36:04 -07003427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 wtoken.willBeHidden = false;
3429 if (wtoken.hidden == visible) {
3430 final int N = wtoken.allAppWindows.size();
3431 boolean changed = false;
3432 if (DEBUG_APP_TRANSITIONS) Log.v(
3433 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3434 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003437
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003438 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 if (wtoken.animation == sDummyAnimation) {
3440 wtoken.animation = null;
3441 }
3442 applyAnimationLocked(wtoken, lp, transit, visible);
3443 changed = true;
3444 if (wtoken.animation != null) {
3445 delayed = runningAppAnimation = true;
3446 }
3447 }
Romain Guy06882f82009-06-10 13:36:04 -07003448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 for (int i=0; i<N; i++) {
3450 WindowState win = wtoken.allAppWindows.get(i);
3451 if (win == wtoken.startingWindow) {
3452 continue;
3453 }
3454
3455 if (win.isAnimating()) {
3456 delayed = true;
3457 }
Romain Guy06882f82009-06-10 13:36:04 -07003458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3460 //win.dump(" ");
3461 if (visible) {
3462 if (!win.isVisibleNow()) {
3463 if (!runningAppAnimation) {
3464 applyAnimationLocked(win,
3465 WindowManagerPolicy.TRANSIT_ENTER, true);
3466 }
3467 changed = true;
3468 }
3469 } else if (win.isVisibleNow()) {
3470 if (!runningAppAnimation) {
3471 applyAnimationLocked(win,
3472 WindowManagerPolicy.TRANSIT_EXIT, false);
3473 }
3474 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3475 KeyWaiter.RETURN_NOTHING);
3476 changed = true;
3477 }
3478 }
3479
3480 wtoken.hidden = wtoken.hiddenRequested = !visible;
3481 if (!visible) {
3482 unsetAppFreezingScreenLocked(wtoken, true, true);
3483 } else {
3484 // If we are being set visible, and the starting window is
3485 // not yet displayed, then make sure it doesn't get displayed.
3486 WindowState swin = wtoken.startingWindow;
3487 if (swin != null && (swin.mDrawPending
3488 || swin.mCommitDrawPending)) {
3489 swin.mPolicyVisibility = false;
3490 swin.mPolicyVisibilityAfterAnim = false;
3491 }
3492 }
Romain Guy06882f82009-06-10 13:36:04 -07003493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3495 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3496 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003497
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003498 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003500 if (performLayout) {
3501 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3502 performLayoutAndPlaceSurfacesLocked();
3503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 }
3505 }
3506
3507 if (wtoken.animation != null) {
3508 delayed = true;
3509 }
Romain Guy06882f82009-06-10 13:36:04 -07003510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 return delayed;
3512 }
3513
3514 public void setAppVisibility(IBinder token, boolean visible) {
3515 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3516 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003517 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 }
3519
3520 AppWindowToken wtoken;
3521
3522 synchronized(mWindowMap) {
3523 wtoken = findAppWindowToken(token);
3524 if (wtoken == null) {
3525 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3526 return;
3527 }
3528
3529 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3530 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003531 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3533 + "): mNextAppTransition=" + mNextAppTransition
3534 + " hidden=" + wtoken.hidden
3535 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3536 }
Romain Guy06882f82009-06-10 13:36:04 -07003537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 // If we are preparing an app transition, then delay changing
3539 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003540 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 // Already in requested state, don't do anything more.
3542 if (wtoken.hiddenRequested != visible) {
3543 return;
3544 }
3545 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 if (DEBUG_APP_TRANSITIONS) Log.v(
3548 TAG, "Setting dummy animation on: " + wtoken);
3549 wtoken.setDummyAnimation();
3550 mOpeningApps.remove(wtoken);
3551 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003552 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 wtoken.inPendingTransaction = true;
3554 if (visible) {
3555 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 wtoken.startingDisplayed = false;
3557 wtoken.startingMoved = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003558
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003559 // If the token is currently hidden (should be the
3560 // common case), then we need to set up to wait for
3561 // its windows to be ready.
3562 if (wtoken.hidden) {
3563 wtoken.allDrawn = false;
3564 wtoken.waitingToShow = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003565
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003566 if (wtoken.clientHidden) {
3567 // In the case where we are making an app visible
3568 // but holding off for a transition, we still need
3569 // to tell the client to make its windows visible so
3570 // they get drawn. Otherwise, we will wait on
3571 // performing the transition until all windows have
3572 // been drawn, they never will be, and we are sad.
3573 wtoken.clientHidden = false;
3574 wtoken.sendAppVisibilityToClients();
3575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 }
3577 } else {
3578 mClosingApps.add(wtoken);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003579
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003580 // If the token is currently visible (should be the
3581 // common case), then set up to wait for it to be hidden.
3582 if (!wtoken.hidden) {
3583 wtoken.waitingToHide = true;
3584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 }
3586 return;
3587 }
Romain Guy06882f82009-06-10 13:36:04 -07003588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003590 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 wtoken.updateReportedVisibilityLocked();
3592 Binder.restoreCallingIdentity(origId);
3593 }
3594 }
3595
3596 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3597 boolean unfreezeSurfaceNow, boolean force) {
3598 if (wtoken.freezingScreen) {
3599 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3600 + " force=" + force);
3601 final int N = wtoken.allAppWindows.size();
3602 boolean unfrozeWindows = false;
3603 for (int i=0; i<N; i++) {
3604 WindowState w = wtoken.allAppWindows.get(i);
3605 if (w.mAppFreezing) {
3606 w.mAppFreezing = false;
3607 if (w.mSurface != null && !w.mOrientationChanging) {
3608 w.mOrientationChanging = true;
3609 }
3610 unfrozeWindows = true;
3611 }
3612 }
3613 if (force || unfrozeWindows) {
3614 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3615 wtoken.freezingScreen = false;
3616 mAppsFreezingScreen--;
3617 }
3618 if (unfreezeSurfaceNow) {
3619 if (unfrozeWindows) {
3620 mLayoutNeeded = true;
3621 performLayoutAndPlaceSurfacesLocked();
3622 }
3623 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3624 stopFreezingDisplayLocked();
3625 }
3626 }
3627 }
3628 }
Romain Guy06882f82009-06-10 13:36:04 -07003629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3631 int configChanges) {
3632 if (DEBUG_ORIENTATION) {
3633 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003634 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 Log.i(TAG, "Set freezing of " + wtoken.appToken
3636 + ": hidden=" + wtoken.hidden + " freezing="
3637 + wtoken.freezingScreen, e);
3638 }
3639 if (!wtoken.hiddenRequested) {
3640 if (!wtoken.freezingScreen) {
3641 wtoken.freezingScreen = true;
3642 mAppsFreezingScreen++;
3643 if (mAppsFreezingScreen == 1) {
3644 startFreezingDisplayLocked();
3645 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3646 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3647 5000);
3648 }
3649 }
3650 final int N = wtoken.allAppWindows.size();
3651 for (int i=0; i<N; i++) {
3652 WindowState w = wtoken.allAppWindows.get(i);
3653 w.mAppFreezing = true;
3654 }
3655 }
3656 }
Romain Guy06882f82009-06-10 13:36:04 -07003657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 public void startAppFreezingScreen(IBinder token, int configChanges) {
3659 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3660 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003661 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 }
3663
3664 synchronized(mWindowMap) {
3665 if (configChanges == 0 && !mDisplayFrozen) {
3666 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3667 return;
3668 }
Romain Guy06882f82009-06-10 13:36:04 -07003669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 AppWindowToken wtoken = findAppWindowToken(token);
3671 if (wtoken == null || wtoken.appToken == null) {
3672 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3673 return;
3674 }
3675 final long origId = Binder.clearCallingIdentity();
3676 startAppFreezingScreenLocked(wtoken, configChanges);
3677 Binder.restoreCallingIdentity(origId);
3678 }
3679 }
Romain Guy06882f82009-06-10 13:36:04 -07003680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681 public void stopAppFreezingScreen(IBinder token, boolean force) {
3682 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3683 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003684 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 }
3686
3687 synchronized(mWindowMap) {
3688 AppWindowToken wtoken = findAppWindowToken(token);
3689 if (wtoken == null || wtoken.appToken == null) {
3690 return;
3691 }
3692 final long origId = Binder.clearCallingIdentity();
3693 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3694 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3695 unsetAppFreezingScreenLocked(wtoken, true, force);
3696 Binder.restoreCallingIdentity(origId);
3697 }
3698 }
Romain Guy06882f82009-06-10 13:36:04 -07003699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 public void removeAppToken(IBinder token) {
3701 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3702 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003703 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 }
3705
3706 AppWindowToken wtoken = null;
3707 AppWindowToken startingToken = null;
3708 boolean delayed = false;
3709
3710 final long origId = Binder.clearCallingIdentity();
3711 synchronized(mWindowMap) {
3712 WindowToken basewtoken = mTokenMap.remove(token);
3713 mTokenList.remove(basewtoken);
3714 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3715 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003716 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 wtoken.inPendingTransaction = false;
3718 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003719 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 if (mClosingApps.contains(wtoken)) {
3721 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003722 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003724 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 delayed = true;
3726 }
3727 if (DEBUG_APP_TRANSITIONS) Log.v(
3728 TAG, "Removing app " + wtoken + " delayed=" + delayed
3729 + " animation=" + wtoken.animation
3730 + " animating=" + wtoken.animating);
3731 if (delayed) {
3732 // set the token aside because it has an active animation to be finished
3733 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003734 } else {
3735 // Make sure there is no animation running on this token,
3736 // so any windows associated with it will be removed as
3737 // soon as their animations are complete
3738 wtoken.animation = null;
3739 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 }
3741 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003742 if (mLastEnterAnimToken == wtoken) {
3743 mLastEnterAnimToken = null;
3744 mLastEnterAnimParams = null;
3745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 wtoken.removed = true;
3747 if (wtoken.startingData != null) {
3748 startingToken = wtoken;
3749 }
3750 unsetAppFreezingScreenLocked(wtoken, true, true);
3751 if (mFocusedApp == wtoken) {
3752 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3753 mFocusedApp = null;
3754 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3755 mKeyWaiter.tickle();
3756 }
3757 } else {
3758 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3759 }
Romain Guy06882f82009-06-10 13:36:04 -07003760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 if (!delayed && wtoken != null) {
3762 wtoken.updateReportedVisibilityLocked();
3763 }
3764 }
3765 Binder.restoreCallingIdentity(origId);
3766
3767 if (startingToken != null) {
3768 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3769 + startingToken + ": app token removed");
3770 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3771 mH.sendMessage(m);
3772 }
3773 }
3774
3775 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3776 final int NW = token.windows.size();
3777 for (int i=0; i<NW; i++) {
3778 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003779 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780 mWindows.remove(win);
3781 int j = win.mChildWindows.size();
3782 while (j > 0) {
3783 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003784 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3785 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3786 "Tmp removing child window " + cwin);
3787 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 }
3789 }
3790 return NW > 0;
3791 }
3792
3793 void dumpAppTokensLocked() {
3794 for (int i=mAppTokens.size()-1; i>=0; i--) {
3795 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3796 }
3797 }
Romain Guy06882f82009-06-10 13:36:04 -07003798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 void dumpWindowsLocked() {
3800 for (int i=mWindows.size()-1; i>=0; i--) {
3801 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3802 }
3803 }
Romain Guy06882f82009-06-10 13:36:04 -07003804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 private int findWindowOffsetLocked(int tokenPos) {
3806 final int NW = mWindows.size();
3807
3808 if (tokenPos >= mAppTokens.size()) {
3809 int i = NW;
3810 while (i > 0) {
3811 i--;
3812 WindowState win = (WindowState)mWindows.get(i);
3813 if (win.getAppToken() != null) {
3814 return i+1;
3815 }
3816 }
3817 }
3818
3819 while (tokenPos > 0) {
3820 // Find the first app token below the new position that has
3821 // a window displayed.
3822 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3823 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3824 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003825 if (wtoken.sendingToBottom) {
3826 if (DEBUG_REORDER) Log.v(TAG,
3827 "Skipping token -- currently sending to bottom");
3828 tokenPos--;
3829 continue;
3830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831 int i = wtoken.windows.size();
3832 while (i > 0) {
3833 i--;
3834 WindowState win = wtoken.windows.get(i);
3835 int j = win.mChildWindows.size();
3836 while (j > 0) {
3837 j--;
3838 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003839 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 for (int pos=NW-1; pos>=0; pos--) {
3841 if (mWindows.get(pos) == cwin) {
3842 if (DEBUG_REORDER) Log.v(TAG,
3843 "Found child win @" + (pos+1));
3844 return pos+1;
3845 }
3846 }
3847 }
3848 }
3849 for (int pos=NW-1; pos>=0; pos--) {
3850 if (mWindows.get(pos) == win) {
3851 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3852 return pos+1;
3853 }
3854 }
3855 }
3856 tokenPos--;
3857 }
3858
3859 return 0;
3860 }
3861
3862 private final int reAddWindowLocked(int index, WindowState win) {
3863 final int NCW = win.mChildWindows.size();
3864 boolean added = false;
3865 for (int j=0; j<NCW; j++) {
3866 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3867 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003868 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3869 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 mWindows.add(index, win);
3871 index++;
3872 added = true;
3873 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003874 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3875 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 mWindows.add(index, cwin);
3877 index++;
3878 }
3879 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003880 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3881 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 mWindows.add(index, win);
3883 index++;
3884 }
3885 return index;
3886 }
Romain Guy06882f82009-06-10 13:36:04 -07003887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3889 final int NW = token.windows.size();
3890 for (int i=0; i<NW; i++) {
3891 index = reAddWindowLocked(index, token.windows.get(i));
3892 }
3893 return index;
3894 }
3895
3896 public void moveAppToken(int index, IBinder token) {
3897 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3898 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003899 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 }
3901
3902 synchronized(mWindowMap) {
3903 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3904 if (DEBUG_REORDER) dumpAppTokensLocked();
3905 final AppWindowToken wtoken = findAppWindowToken(token);
3906 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3907 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3908 + token + " (" + wtoken + ")");
3909 return;
3910 }
3911 mAppTokens.add(index, wtoken);
3912 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3913 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 final long origId = Binder.clearCallingIdentity();
3916 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3917 if (DEBUG_REORDER) dumpWindowsLocked();
3918 if (tmpRemoveAppWindowsLocked(wtoken)) {
3919 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3920 if (DEBUG_REORDER) dumpWindowsLocked();
3921 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3922 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3923 if (DEBUG_REORDER) dumpWindowsLocked();
3924 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 mLayoutNeeded = true;
3926 performLayoutAndPlaceSurfacesLocked();
3927 }
3928 Binder.restoreCallingIdentity(origId);
3929 }
3930 }
3931
3932 private void removeAppTokensLocked(List<IBinder> tokens) {
3933 // XXX This should be done more efficiently!
3934 // (take advantage of the fact that both lists should be
3935 // ordered in the same way.)
3936 int N = tokens.size();
3937 for (int i=0; i<N; i++) {
3938 IBinder token = tokens.get(i);
3939 final AppWindowToken wtoken = findAppWindowToken(token);
3940 if (!mAppTokens.remove(wtoken)) {
3941 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3942 + token + " (" + wtoken + ")");
3943 i--;
3944 N--;
3945 }
3946 }
3947 }
3948
Dianne Hackborna8f60182009-09-01 19:01:50 -07003949 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3950 boolean updateFocusAndLayout) {
3951 // First remove all of the windows from the list.
3952 tmpRemoveAppWindowsLocked(wtoken);
3953
3954 // Where to start adding?
3955 int pos = findWindowOffsetLocked(tokenPos);
3956
3957 // And now add them back at the correct place.
3958 pos = reAddAppWindowsLocked(pos, wtoken);
3959
3960 if (updateFocusAndLayout) {
3961 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3962 assignLayersLocked();
3963 }
3964 mLayoutNeeded = true;
3965 performLayoutAndPlaceSurfacesLocked();
3966 }
3967 }
3968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3970 // First remove all of the windows from the list.
3971 final int N = tokens.size();
3972 int i;
3973 for (i=0; i<N; i++) {
3974 WindowToken token = mTokenMap.get(tokens.get(i));
3975 if (token != null) {
3976 tmpRemoveAppWindowsLocked(token);
3977 }
3978 }
3979
3980 // Where to start adding?
3981 int pos = findWindowOffsetLocked(tokenPos);
3982
3983 // And now add them back at the correct place.
3984 for (i=0; i<N; i++) {
3985 WindowToken token = mTokenMap.get(tokens.get(i));
3986 if (token != null) {
3987 pos = reAddAppWindowsLocked(pos, token);
3988 }
3989 }
3990
Dianne Hackborna8f60182009-09-01 19:01:50 -07003991 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3992 assignLayersLocked();
3993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 mLayoutNeeded = true;
3995 performLayoutAndPlaceSurfacesLocked();
3996
3997 //dump();
3998 }
3999
4000 public void moveAppTokensToTop(List<IBinder> tokens) {
4001 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4002 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004003 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 }
4005
4006 final long origId = Binder.clearCallingIdentity();
4007 synchronized(mWindowMap) {
4008 removeAppTokensLocked(tokens);
4009 final int N = tokens.size();
4010 for (int i=0; i<N; i++) {
4011 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4012 if (wt != null) {
4013 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004014 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004015 mToTopApps.remove(wt);
4016 mToBottomApps.remove(wt);
4017 mToTopApps.add(wt);
4018 wt.sendingToBottom = false;
4019 wt.sendingToTop = true;
4020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 }
4022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004023
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004024 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004025 moveAppWindowsLocked(tokens, mAppTokens.size());
4026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 }
4028 Binder.restoreCallingIdentity(origId);
4029 }
4030
4031 public void moveAppTokensToBottom(List<IBinder> tokens) {
4032 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4033 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004034 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 }
4036
4037 final long origId = Binder.clearCallingIdentity();
4038 synchronized(mWindowMap) {
4039 removeAppTokensLocked(tokens);
4040 final int N = tokens.size();
4041 int pos = 0;
4042 for (int i=0; i<N; i++) {
4043 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4044 if (wt != null) {
4045 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004046 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004047 mToTopApps.remove(wt);
4048 mToBottomApps.remove(wt);
4049 mToBottomApps.add(i, wt);
4050 wt.sendingToTop = false;
4051 wt.sendingToBottom = true;
4052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 pos++;
4054 }
4055 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004056
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004057 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004058 moveAppWindowsLocked(tokens, 0);
4059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 }
4061 Binder.restoreCallingIdentity(origId);
4062 }
4063
4064 // -------------------------------------------------------------
4065 // Misc IWindowSession methods
4066 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004069 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 != PackageManager.PERMISSION_GRANTED) {
4071 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4072 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004073 synchronized (mKeyguardTokenWatcher) {
4074 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076 }
4077
4078 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004079 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 != PackageManager.PERMISSION_GRANTED) {
4081 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4082 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004083 synchronized (mKeyguardTokenWatcher) {
4084 mKeyguardTokenWatcher.release(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085
Mike Lockwood983ee092009-11-22 01:42:24 -05004086 if (!mKeyguardTokenWatcher.isAcquired()) {
4087 // If we are the last one to reenable the keyguard wait until
4088 // we have actaully finished reenabling until returning.
4089 // It is possible that reenableKeyguard() can be called before
4090 // the previous disableKeyguard() is handled, in which case
4091 // neither mKeyguardTokenWatcher.acquired() or released() would
4092 // be called. In that case mKeyguardDisabled will be false here
4093 // and we have nothing to wait for.
4094 while (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 try {
Mike Lockwood983ee092009-11-22 01:42:24 -05004096 mKeyguardTokenWatcher.wait();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 } catch (InterruptedException e) {
4098 Thread.currentThread().interrupt();
4099 }
4100 }
4101 }
4102 }
4103 }
4104
4105 /**
4106 * @see android.app.KeyguardManager#exitKeyguardSecurely
4107 */
4108 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004109 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 != PackageManager.PERMISSION_GRANTED) {
4111 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4112 }
4113 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4114 public void onKeyguardExitResult(boolean success) {
4115 try {
4116 callback.onKeyguardExitResult(success);
4117 } catch (RemoteException e) {
4118 // Client has died, we don't care.
4119 }
4120 }
4121 });
4122 }
4123
4124 public boolean inKeyguardRestrictedInputMode() {
4125 return mPolicy.inKeyguardRestrictedKeyInputMode();
4126 }
Romain Guy06882f82009-06-10 13:36:04 -07004127
Dianne Hackbornffa42482009-09-23 22:20:11 -07004128 public void closeSystemDialogs(String reason) {
4129 synchronized(mWindowMap) {
4130 for (int i=mWindows.size()-1; i>=0; i--) {
4131 WindowState w = (WindowState)mWindows.get(i);
4132 if (w.mSurface != null) {
4133 try {
4134 w.mClient.closeSystemDialogs(reason);
4135 } catch (RemoteException e) {
4136 }
4137 }
4138 }
4139 }
4140 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 static float fixScale(float scale) {
4143 if (scale < 0) scale = 0;
4144 else if (scale > 20) scale = 20;
4145 return Math.abs(scale);
4146 }
Romain Guy06882f82009-06-10 13:36:04 -07004147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 public void setAnimationScale(int which, float scale) {
4149 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4150 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004151 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 }
4153
4154 if (scale < 0) scale = 0;
4155 else if (scale > 20) scale = 20;
4156 scale = Math.abs(scale);
4157 switch (which) {
4158 case 0: mWindowAnimationScale = fixScale(scale); break;
4159 case 1: mTransitionAnimationScale = fixScale(scale); break;
4160 }
Romain Guy06882f82009-06-10 13:36:04 -07004161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 // Persist setting
4163 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4164 }
Romain Guy06882f82009-06-10 13:36:04 -07004165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 public void setAnimationScales(float[] scales) {
4167 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4168 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004169 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 }
4171
4172 if (scales != null) {
4173 if (scales.length >= 1) {
4174 mWindowAnimationScale = fixScale(scales[0]);
4175 }
4176 if (scales.length >= 2) {
4177 mTransitionAnimationScale = fixScale(scales[1]);
4178 }
4179 }
Romain Guy06882f82009-06-10 13:36:04 -07004180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 // Persist setting
4182 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4183 }
Romain Guy06882f82009-06-10 13:36:04 -07004184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 public float getAnimationScale(int which) {
4186 switch (which) {
4187 case 0: return mWindowAnimationScale;
4188 case 1: return mTransitionAnimationScale;
4189 }
4190 return 0;
4191 }
Romain Guy06882f82009-06-10 13:36:04 -07004192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 public float[] getAnimationScales() {
4194 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4195 }
Romain Guy06882f82009-06-10 13:36:04 -07004196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 public int getSwitchState(int sw) {
4198 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4199 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004200 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 }
4202 return KeyInputQueue.getSwitchState(sw);
4203 }
Romain Guy06882f82009-06-10 13:36:04 -07004204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 public int getSwitchStateForDevice(int devid, int sw) {
4206 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4207 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004208 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 }
4210 return KeyInputQueue.getSwitchState(devid, sw);
4211 }
Romain Guy06882f82009-06-10 13:36:04 -07004212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 public int getScancodeState(int sw) {
4214 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4215 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004216 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004218 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 }
Romain Guy06882f82009-06-10 13:36:04 -07004220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 public int getScancodeStateForDevice(int devid, int sw) {
4222 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4223 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004224 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004226 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
Romain Guy06882f82009-06-10 13:36:04 -07004228
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004229 public int getTrackballScancodeState(int sw) {
4230 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4231 "getTrackballScancodeState()")) {
4232 throw new SecurityException("Requires READ_INPUT_STATE permission");
4233 }
4234 return mQueue.getTrackballScancodeState(sw);
4235 }
4236
4237 public int getDPadScancodeState(int sw) {
4238 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4239 "getDPadScancodeState()")) {
4240 throw new SecurityException("Requires READ_INPUT_STATE permission");
4241 }
4242 return mQueue.getDPadScancodeState(sw);
4243 }
4244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 public int getKeycodeState(int sw) {
4246 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4247 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004248 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004250 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 }
Romain Guy06882f82009-06-10 13:36:04 -07004252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 public int getKeycodeStateForDevice(int devid, int sw) {
4254 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4255 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004256 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004258 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 }
Romain Guy06882f82009-06-10 13:36:04 -07004260
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004261 public int getTrackballKeycodeState(int sw) {
4262 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4263 "getTrackballKeycodeState()")) {
4264 throw new SecurityException("Requires READ_INPUT_STATE permission");
4265 }
4266 return mQueue.getTrackballKeycodeState(sw);
4267 }
4268
4269 public int getDPadKeycodeState(int sw) {
4270 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4271 "getDPadKeycodeState()")) {
4272 throw new SecurityException("Requires READ_INPUT_STATE permission");
4273 }
4274 return mQueue.getDPadKeycodeState(sw);
4275 }
4276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4278 return KeyInputQueue.hasKeys(keycodes, keyExists);
4279 }
Romain Guy06882f82009-06-10 13:36:04 -07004280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 public void enableScreenAfterBoot() {
4282 synchronized(mWindowMap) {
4283 if (mSystemBooted) {
4284 return;
4285 }
4286 mSystemBooted = true;
4287 }
Romain Guy06882f82009-06-10 13:36:04 -07004288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 performEnableScreen();
4290 }
Romain Guy06882f82009-06-10 13:36:04 -07004291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 public void enableScreenIfNeededLocked() {
4293 if (mDisplayEnabled) {
4294 return;
4295 }
4296 if (!mSystemBooted) {
4297 return;
4298 }
4299 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4300 }
Romain Guy06882f82009-06-10 13:36:04 -07004301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 public void performEnableScreen() {
4303 synchronized(mWindowMap) {
4304 if (mDisplayEnabled) {
4305 return;
4306 }
4307 if (!mSystemBooted) {
4308 return;
4309 }
Romain Guy06882f82009-06-10 13:36:04 -07004310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 // Don't enable the screen until all existing windows
4312 // have been drawn.
4313 final int N = mWindows.size();
4314 for (int i=0; i<N; i++) {
4315 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08004316 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 return;
4318 }
4319 }
Romain Guy06882f82009-06-10 13:36:04 -07004320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 mDisplayEnabled = true;
4322 if (false) {
4323 Log.i(TAG, "ENABLING SCREEN!");
4324 StringWriter sw = new StringWriter();
4325 PrintWriter pw = new PrintWriter(sw);
4326 this.dump(null, pw, null);
4327 Log.i(TAG, sw.toString());
4328 }
4329 try {
4330 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4331 if (surfaceFlinger != null) {
4332 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4333 Parcel data = Parcel.obtain();
4334 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4335 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4336 data, null, 0);
4337 data.recycle();
4338 }
4339 } catch (RemoteException ex) {
4340 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4341 }
4342 }
Romain Guy06882f82009-06-10 13:36:04 -07004343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004347 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4348 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 }
Romain Guy06882f82009-06-10 13:36:04 -07004350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 public void setInTouchMode(boolean mode) {
4352 synchronized(mWindowMap) {
4353 mInTouchMode = mode;
4354 }
4355 }
4356
Romain Guy06882f82009-06-10 13:36:04 -07004357 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004358 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004360 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004361 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004362 }
4363
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004364 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 }
Romain Guy06882f82009-06-10 13:36:04 -07004366
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004367 public void setRotationUnchecked(int rotation,
4368 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 if(DEBUG_ORIENTATION) Log.v(TAG,
4370 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 long origId = Binder.clearCallingIdentity();
4373 boolean changed;
4374 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004375 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 }
Romain Guy06882f82009-06-10 13:36:04 -07004377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 if (changed) {
4379 sendNewConfiguration();
4380 synchronized(mWindowMap) {
4381 mLayoutNeeded = true;
4382 performLayoutAndPlaceSurfacesLocked();
4383 }
4384 } else if (alwaysSendConfiguration) {
4385 //update configuration ignoring orientation change
4386 sendNewConfiguration();
4387 }
Romain Guy06882f82009-06-10 13:36:04 -07004388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004389 Binder.restoreCallingIdentity(origId);
4390 }
Romain Guy06882f82009-06-10 13:36:04 -07004391
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004392 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 boolean changed;
4394 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4395 rotation = mRequestedRotation;
4396 } else {
4397 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004398 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 }
4400 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004401 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 mRotation, mDisplayEnabled);
4403 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4404 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004407 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 "Rotation changed to " + rotation
4409 + " from " + mRotation
4410 + " (forceApp=" + mForcedAppOrientation
4411 + ", req=" + mRequestedRotation + ")");
4412 mRotation = rotation;
4413 mWindowsFreezingScreen = true;
4414 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4415 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4416 2000);
4417 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004418 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 mQueue.setOrientation(rotation);
4420 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004421 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 }
4423 for (int i=mWindows.size()-1; i>=0; i--) {
4424 WindowState w = (WindowState)mWindows.get(i);
4425 if (w.mSurface != null) {
4426 w.mOrientationChanging = true;
4427 }
4428 }
4429 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4430 try {
4431 mRotationWatchers.get(i).onRotationChanged(rotation);
4432 } catch (RemoteException e) {
4433 }
4434 }
4435 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437 return changed;
4438 }
Romain Guy06882f82009-06-10 13:36:04 -07004439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 public int getRotation() {
4441 return mRotation;
4442 }
4443
4444 public int watchRotation(IRotationWatcher watcher) {
4445 final IBinder watcherBinder = watcher.asBinder();
4446 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4447 public void binderDied() {
4448 synchronized (mWindowMap) {
4449 for (int i=0; i<mRotationWatchers.size(); i++) {
4450 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004451 IRotationWatcher removed = mRotationWatchers.remove(i);
4452 if (removed != null) {
4453 removed.asBinder().unlinkToDeath(this, 0);
4454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 i--;
4456 }
4457 }
4458 }
4459 }
4460 };
Romain Guy06882f82009-06-10 13:36:04 -07004461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 synchronized (mWindowMap) {
4463 try {
4464 watcher.asBinder().linkToDeath(dr, 0);
4465 mRotationWatchers.add(watcher);
4466 } catch (RemoteException e) {
4467 // Client died, no cleanup needed.
4468 }
Romain Guy06882f82009-06-10 13:36:04 -07004469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004470 return mRotation;
4471 }
4472 }
4473
4474 /**
4475 * Starts the view server on the specified port.
4476 *
4477 * @param port The port to listener to.
4478 *
4479 * @return True if the server was successfully started, false otherwise.
4480 *
4481 * @see com.android.server.ViewServer
4482 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4483 */
4484 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004485 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 return false;
4487 }
4488
4489 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4490 return false;
4491 }
4492
4493 if (port < 1024) {
4494 return false;
4495 }
4496
4497 if (mViewServer != null) {
4498 if (!mViewServer.isRunning()) {
4499 try {
4500 return mViewServer.start();
4501 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004502 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 }
4504 }
4505 return false;
4506 }
4507
4508 try {
4509 mViewServer = new ViewServer(this, port);
4510 return mViewServer.start();
4511 } catch (IOException e) {
4512 Log.w(TAG, "View server did not start");
4513 }
4514 return false;
4515 }
4516
Romain Guy06882f82009-06-10 13:36:04 -07004517 private boolean isSystemSecure() {
4518 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4519 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4520 }
4521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 /**
4523 * Stops the view server if it exists.
4524 *
4525 * @return True if the server stopped, false if it wasn't started or
4526 * couldn't be stopped.
4527 *
4528 * @see com.android.server.ViewServer
4529 */
4530 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004531 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 return false;
4533 }
4534
4535 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4536 return false;
4537 }
4538
4539 if (mViewServer != null) {
4540 return mViewServer.stop();
4541 }
4542 return false;
4543 }
4544
4545 /**
4546 * Indicates whether the view server is running.
4547 *
4548 * @return True if the server is running, false otherwise.
4549 *
4550 * @see com.android.server.ViewServer
4551 */
4552 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004553 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 return false;
4555 }
4556
4557 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4558 return false;
4559 }
4560
4561 return mViewServer != null && mViewServer.isRunning();
4562 }
4563
4564 /**
4565 * Lists all availble windows in the system. The listing is written in the
4566 * specified Socket's output stream with the following syntax:
4567 * windowHashCodeInHexadecimal windowName
4568 * Each line of the ouput represents a different window.
4569 *
4570 * @param client The remote client to send the listing to.
4571 * @return False if an error occured, true otherwise.
4572 */
4573 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004574 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004575 return false;
4576 }
4577
4578 boolean result = true;
4579
4580 Object[] windows;
4581 synchronized (mWindowMap) {
4582 windows = new Object[mWindows.size()];
4583 //noinspection unchecked
4584 windows = mWindows.toArray(windows);
4585 }
4586
4587 BufferedWriter out = null;
4588
4589 // Any uncaught exception will crash the system process
4590 try {
4591 OutputStream clientStream = client.getOutputStream();
4592 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4593
4594 final int count = windows.length;
4595 for (int i = 0; i < count; i++) {
4596 final WindowState w = (WindowState) windows[i];
4597 out.write(Integer.toHexString(System.identityHashCode(w)));
4598 out.write(' ');
4599 out.append(w.mAttrs.getTitle());
4600 out.write('\n');
4601 }
4602
4603 out.write("DONE.\n");
4604 out.flush();
4605 } catch (Exception e) {
4606 result = false;
4607 } finally {
4608 if (out != null) {
4609 try {
4610 out.close();
4611 } catch (IOException e) {
4612 result = false;
4613 }
4614 }
4615 }
4616
4617 return result;
4618 }
4619
4620 /**
4621 * Sends a command to a target window. The result of the command, if any, will be
4622 * written in the output stream of the specified socket.
4623 *
4624 * The parameters must follow this syntax:
4625 * windowHashcode extra
4626 *
4627 * Where XX is the length in characeters of the windowTitle.
4628 *
4629 * The first parameter is the target window. The window with the specified hashcode
4630 * will be the target. If no target can be found, nothing happens. The extra parameters
4631 * will be delivered to the target window and as parameters to the command itself.
4632 *
4633 * @param client The remote client to sent the result, if any, to.
4634 * @param command The command to execute.
4635 * @param parameters The command parameters.
4636 *
4637 * @return True if the command was successfully delivered, false otherwise. This does
4638 * not indicate whether the command itself was successful.
4639 */
4640 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004641 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004642 return false;
4643 }
4644
4645 boolean success = true;
4646 Parcel data = null;
4647 Parcel reply = null;
4648
4649 // Any uncaught exception will crash the system process
4650 try {
4651 // Find the hashcode of the window
4652 int index = parameters.indexOf(' ');
4653 if (index == -1) {
4654 index = parameters.length();
4655 }
4656 final String code = parameters.substring(0, index);
Romain Guy236092a2009-12-14 15:31:48 -08004657 int hashCode = (int) Long.parseLong(code, 16);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658
4659 // Extract the command's parameter after the window description
4660 if (index < parameters.length()) {
4661 parameters = parameters.substring(index + 1);
4662 } else {
4663 parameters = "";
4664 }
4665
4666 final WindowManagerService.WindowState window = findWindow(hashCode);
4667 if (window == null) {
4668 return false;
4669 }
4670
4671 data = Parcel.obtain();
4672 data.writeInterfaceToken("android.view.IWindow");
4673 data.writeString(command);
4674 data.writeString(parameters);
4675 data.writeInt(1);
4676 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4677
4678 reply = Parcel.obtain();
4679
4680 final IBinder binder = window.mClient.asBinder();
4681 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4682 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4683
4684 reply.readException();
4685
4686 } catch (Exception e) {
4687 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4688 success = false;
4689 } finally {
4690 if (data != null) {
4691 data.recycle();
4692 }
4693 if (reply != null) {
4694 reply.recycle();
4695 }
4696 }
4697
4698 return success;
4699 }
4700
4701 private WindowState findWindow(int hashCode) {
4702 if (hashCode == -1) {
4703 return getFocusedWindow();
4704 }
4705
4706 synchronized (mWindowMap) {
4707 final ArrayList windows = mWindows;
4708 final int count = windows.size();
4709
4710 for (int i = 0; i < count; i++) {
4711 WindowState w = (WindowState) windows.get(i);
4712 if (System.identityHashCode(w) == hashCode) {
4713 return w;
4714 }
4715 }
4716 }
4717
4718 return null;
4719 }
4720
4721 /*
4722 * Instruct the Activity Manager to fetch the current configuration and broadcast
4723 * that to config-changed listeners if appropriate.
4724 */
4725 void sendNewConfiguration() {
4726 try {
4727 mActivityManager.updateConfiguration(null);
4728 } catch (RemoteException e) {
4729 }
4730 }
Romain Guy06882f82009-06-10 13:36:04 -07004731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004732 public Configuration computeNewConfiguration() {
4733 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004734 return computeNewConfigurationLocked();
4735 }
4736 }
Romain Guy06882f82009-06-10 13:36:04 -07004737
Dianne Hackbornc485a602009-03-24 22:39:49 -07004738 Configuration computeNewConfigurationLocked() {
4739 Configuration config = new Configuration();
4740 if (!computeNewConfigurationLocked(config)) {
4741 return null;
4742 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004743 return config;
4744 }
Romain Guy06882f82009-06-10 13:36:04 -07004745
Dianne Hackbornc485a602009-03-24 22:39:49 -07004746 boolean computeNewConfigurationLocked(Configuration config) {
4747 if (mDisplay == null) {
4748 return false;
4749 }
4750 mQueue.getInputConfiguration(config);
4751 final int dw = mDisplay.getWidth();
4752 final int dh = mDisplay.getHeight();
4753 int orientation = Configuration.ORIENTATION_SQUARE;
4754 if (dw < dh) {
4755 orientation = Configuration.ORIENTATION_PORTRAIT;
4756 } else if (dw > dh) {
4757 orientation = Configuration.ORIENTATION_LANDSCAPE;
4758 }
4759 config.orientation = orientation;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004760
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004761 DisplayMetrics dm = new DisplayMetrics();
4762 mDisplay.getMetrics(dm);
4763 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4764
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004765 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004766 // Note we only do this once because at this point we don't
4767 // expect the screen to change in this way at runtime, and want
4768 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004769 int longSize = dw;
4770 int shortSize = dh;
4771 if (longSize < shortSize) {
4772 int tmp = longSize;
4773 longSize = shortSize;
4774 shortSize = tmp;
4775 }
4776 longSize = (int)(longSize/dm.density);
4777 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004778
Dianne Hackborn723738c2009-06-25 19:48:04 -07004779 // These semi-magic numbers define our compatibility modes for
4780 // applications with different screens. Don't change unless you
4781 // make sure to test lots and lots of apps!
4782 if (longSize < 470) {
4783 // This is shorter than an HVGA normal density screen (which
4784 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004785 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4786 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004787 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004788 // Is this a large screen?
4789 if (longSize > 640 && shortSize >= 480) {
4790 // VGA or larger screens at medium density are the point
4791 // at which we consider it to be a large screen.
4792 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4793 } else {
4794 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004795
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004796 // If this screen is wider than normal HVGA, or taller
4797 // than FWVGA, then for old apps we want to run in size
4798 // compatibility mode.
4799 if (shortSize > 321 || longSize > 570) {
4800 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4801 }
4802 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004803
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004804 // Is this a long screen?
4805 if (((longSize*3)/5) >= (shortSize-1)) {
4806 // Anything wider than WVGA (5:3) is considering to be long.
4807 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4808 } else {
4809 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4810 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004811 }
4812 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004813 config.screenLayout = mScreenLayout;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004814
Dianne Hackbornc485a602009-03-24 22:39:49 -07004815 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4816 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4817 mPolicy.adjustConfigurationLw(config);
4818 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004819 }
Romain Guy06882f82009-06-10 13:36:04 -07004820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 // -------------------------------------------------------------
4822 // Input Events and Focus Management
4823 // -------------------------------------------------------------
4824
4825 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004826 long curTime = SystemClock.uptimeMillis();
4827
Michael Chane10de972009-05-18 11:24:50 -07004828 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004829 if (mLastTouchEventType == eventType &&
4830 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4831 return;
4832 }
4833 mLastUserActivityCallTime = curTime;
4834 mLastTouchEventType = eventType;
4835 }
4836
4837 if (targetWin == null
4838 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4839 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 }
4841 }
4842
4843 // tells if it's a cheek event or not -- this function is stateful
4844 private static final int EVENT_NONE = 0;
4845 private static final int EVENT_UNKNOWN = 0;
4846 private static final int EVENT_CHEEK = 0;
4847 private static final int EVENT_IGNORE_DURATION = 300; // ms
4848 private static final float CHEEK_THRESHOLD = 0.6f;
4849 private int mEventState = EVENT_NONE;
4850 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004852 private int eventType(MotionEvent ev) {
4853 float size = ev.getSize();
4854 switch (ev.getAction()) {
4855 case MotionEvent.ACTION_DOWN:
4856 mEventSize = size;
4857 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4858 case MotionEvent.ACTION_UP:
4859 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004860 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004861 case MotionEvent.ACTION_MOVE:
4862 final int N = ev.getHistorySize();
4863 if (size > mEventSize) mEventSize = size;
4864 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4865 for (int i=0; i<N; i++) {
4866 size = ev.getHistoricalSize(i);
4867 if (size > mEventSize) mEventSize = size;
4868 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4869 }
4870 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4871 return TOUCH_EVENT;
4872 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004873 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 }
4875 default:
4876 // not good
4877 return OTHER_EVENT;
4878 }
4879 }
4880
4881 /**
4882 * @return Returns true if event was dispatched, false if it was dropped for any reason
4883 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004884 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4886 "dispatchPointer " + ev);
4887
Michael Chan53071d62009-05-13 17:29:48 -07004888 if (MEASURE_LATENCY) {
4889 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4890 }
4891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004893 ev, true, false, pid, uid);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004894
Michael Chan53071d62009-05-13 17:29:48 -07004895 if (MEASURE_LATENCY) {
4896 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4897 }
4898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 if (action == MotionEvent.ACTION_UP) {
4902 // let go of our target
4903 mKeyWaiter.mMotionTarget = null;
4904 mPowerManager.logPointerUpEvent();
4905 } else if (action == MotionEvent.ACTION_DOWN) {
4906 mPowerManager.logPointerDownEvent();
4907 }
4908
4909 if (targetObj == null) {
4910 // In this case we are either dropping the event, or have received
4911 // a move or up without a down. It is common to receive move
4912 // events in such a way, since this means the user is moving the
4913 // pointer without actually pressing down. All other cases should
4914 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004915 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4917 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08004918 synchronized (mWindowMap) {
4919 if (mSendingPointersToWallpaper) {
4920 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4921 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4922 }
4923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 if (qev != null) {
4925 mQueue.recycleEvent(qev);
4926 }
4927 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004928 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 }
4930 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004931 synchronized (mWindowMap) {
4932 if (mSendingPointersToWallpaper) {
4933 Log.i(TAG, "Sending skipped pointer to wallpaper!");
4934 sendPointerToWallpaperLocked(null, ev, ev.getEventTime());
4935 }
4936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004937 if (qev != null) {
4938 mQueue.recycleEvent(qev);
4939 }
4940 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004941 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942 }
Romain Guy06882f82009-06-10 13:36:04 -07004943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004946 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004947 final long eventTimeNano = ev.getEventTimeNano();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004949 //Log.i(TAG, "Sending " + ev + " to " + target);
4950
4951 if (uid != 0 && uid != target.mSession.mUid) {
4952 if (mContext.checkPermission(
4953 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4954 != PackageManager.PERMISSION_GRANTED) {
4955 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4956 + pid + " uid " + uid + " to window " + target
4957 + " owned by uid " + target.mSession.mUid);
4958 if (qev != null) {
4959 mQueue.recycleEvent(qev);
4960 }
4961 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004962 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 }
4964 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004965
Michael Chan53071d62009-05-13 17:29:48 -07004966 if (MEASURE_LATENCY) {
4967 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4968 }
4969
Romain Guy06882f82009-06-10 13:36:04 -07004970 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004971 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4972 //target wants to ignore fat touch events
4973 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4974 //explicit flag to return without processing event further
4975 boolean returnFlag = false;
4976 if((action == MotionEvent.ACTION_DOWN)) {
4977 mFatTouch = false;
4978 if(cheekPress) {
4979 mFatTouch = true;
4980 returnFlag = true;
4981 }
4982 } else {
4983 if(action == MotionEvent.ACTION_UP) {
4984 if(mFatTouch) {
4985 //earlier even was invalid doesnt matter if current up is cheekpress or not
4986 mFatTouch = false;
4987 returnFlag = true;
4988 } else if(cheekPress) {
4989 //cancel the earlier event
4990 ev.setAction(MotionEvent.ACTION_CANCEL);
4991 action = MotionEvent.ACTION_CANCEL;
4992 }
4993 } else if(action == MotionEvent.ACTION_MOVE) {
4994 if(mFatTouch) {
4995 //two cases here
4996 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004997 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004998 returnFlag = true;
4999 } else if(cheekPress) {
5000 //valid down followed by invalid moves
5001 //an invalid move have to cancel earlier action
5002 ev.setAction(MotionEvent.ACTION_CANCEL);
5003 action = MotionEvent.ACTION_CANCEL;
5004 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
5005 //note that the subsequent invalid moves will not get here
5006 mFatTouch = true;
5007 }
5008 }
5009 } //else if action
5010 if(returnFlag) {
5011 //recycle que, ev
5012 if (qev != null) {
5013 mQueue.recycleEvent(qev);
5014 }
5015 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005016 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017 }
5018 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005019
Michael Chan9f028e62009-08-04 17:37:46 -07005020 // Enable this for testing the "right" value
5021 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005022 int max_events_per_sec = 35;
5023 try {
5024 max_events_per_sec = Integer.parseInt(SystemProperties
5025 .get("windowsmgr.max_events_per_sec"));
5026 if (max_events_per_sec < 1) {
5027 max_events_per_sec = 35;
5028 }
5029 } catch (NumberFormatException e) {
5030 }
5031 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5032 }
5033
5034 /*
5035 * Throttle events to minimize CPU usage when there's a flood of events
5036 * e.g. constant contact with the screen
5037 */
5038 if (action == MotionEvent.ACTION_MOVE) {
5039 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5040 long now = SystemClock.uptimeMillis();
5041 if (now < nextEventTime) {
5042 try {
5043 Thread.sleep(nextEventTime - now);
5044 } catch (InterruptedException e) {
5045 }
5046 mLastTouchEventTime = nextEventTime;
5047 } else {
5048 mLastTouchEventTime = now;
5049 }
5050 }
5051
Michael Chan53071d62009-05-13 17:29:48 -07005052 if (MEASURE_LATENCY) {
5053 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5054 }
5055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005056 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005057 if (!target.isVisibleLw()) {
5058 // During this motion dispatch, the target window has become
5059 // invisible.
5060 if (mSendingPointersToWallpaper) {
5061 sendPointerToWallpaperLocked(null, ev, eventTime);
5062 }
5063 if (qev != null) {
5064 mQueue.recycleEvent(qev);
5065 }
5066 ev.recycle();
5067 return INJECT_SUCCEEDED;
5068 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5071 mKeyWaiter.bindTargetWindowLocked(target,
5072 KeyWaiter.RETURN_PENDING_POINTER, qev);
5073 ev = null;
5074 } else {
5075 if (action == MotionEvent.ACTION_DOWN) {
5076 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5077 if (out != null) {
5078 MotionEvent oev = MotionEvent.obtain(ev);
5079 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5080 do {
5081 final Rect frame = out.mFrame;
5082 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5083 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005084 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 } catch (android.os.RemoteException e) {
5086 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
5087 }
5088 oev.offsetLocation((float)frame.left, (float)frame.top);
5089 out = out.mNextOutsideTouch;
5090 } while (out != null);
5091 mKeyWaiter.mOutsideTouchTargets = null;
5092 }
5093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005094
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005095 // If we are on top of the wallpaper, then the wallpaper also
5096 // gets to see this movement.
Dianne Hackborn9b52a212009-12-11 14:51:35 -08005097 if ((mWallpaperTarget == target &&
5098 target.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD)
5099 || mSendingPointersToWallpaper) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005100 sendPointerToWallpaperLocked(null, ev, eventTime);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005102
Dianne Hackborn6adba242009-11-10 11:10:09 -08005103 final Rect frame = target.mFrame;
5104 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5105 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 }
5107 }
Romain Guy06882f82009-06-10 13:36:04 -07005108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 // finally offset the event to the target's coordinate system and
5110 // dispatch the event.
5111 try {
5112 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
5113 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
5114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005115
Michael Chan53071d62009-05-13 17:29:48 -07005116 if (MEASURE_LATENCY) {
5117 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5118 }
5119
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005120 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005121
5122 if (MEASURE_LATENCY) {
5123 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5124 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005125 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 } catch (android.os.RemoteException e) {
5127 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
5128 mKeyWaiter.mMotionTarget = null;
5129 try {
5130 removeWindow(target.mSession, target.mClient);
5131 } catch (java.util.NoSuchElementException ex) {
5132 // This will happen if the window has already been
5133 // removed.
5134 }
5135 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005136 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005137 }
Romain Guy06882f82009-06-10 13:36:04 -07005138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 /**
5140 * @return Returns true if event was dispatched, false if it was dropped for any reason
5141 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005142 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143 if (DEBUG_INPUT) Log.v(
5144 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005147 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 if (focusObj == null) {
5149 Log.w(TAG, "No focus window, dropping trackball: " + ev);
5150 if (qev != null) {
5151 mQueue.recycleEvent(qev);
5152 }
5153 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005154 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 }
5156 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5157 if (qev != null) {
5158 mQueue.recycleEvent(qev);
5159 }
5160 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005161 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 }
Romain Guy06882f82009-06-10 13:36:04 -07005163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 if (uid != 0 && uid != focus.mSession.mUid) {
5167 if (mContext.checkPermission(
5168 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5169 != PackageManager.PERMISSION_GRANTED) {
5170 Log.w(TAG, "Permission denied: injecting key event from pid "
5171 + pid + " uid " + uid + " to window " + focus
5172 + " owned by uid " + focus.mSession.mUid);
5173 if (qev != null) {
5174 mQueue.recycleEvent(qev);
5175 }
5176 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005177 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005178 }
5179 }
Romain Guy06882f82009-06-10 13:36:04 -07005180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005183 synchronized(mWindowMap) {
5184 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5185 mKeyWaiter.bindTargetWindowLocked(focus,
5186 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5187 // We don't deliver movement events to the client, we hold
5188 // them and wait for them to call back.
5189 ev = null;
5190 } else {
5191 mKeyWaiter.bindTargetWindowLocked(focus);
5192 }
5193 }
Romain Guy06882f82009-06-10 13:36:04 -07005194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005196 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005197 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005198 } catch (android.os.RemoteException e) {
5199 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5200 try {
5201 removeWindow(focus.mSession, focus.mClient);
5202 } catch (java.util.NoSuchElementException ex) {
5203 // This will happen if the window has already been
5204 // removed.
5205 }
5206 }
Romain Guy06882f82009-06-10 13:36:04 -07005207
Dianne Hackborncfaef692009-06-15 14:24:44 -07005208 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 }
Romain Guy06882f82009-06-10 13:36:04 -07005210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 /**
5212 * @return Returns true if event was dispatched, false if it was dropped for any reason
5213 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005214 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5216
5217 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005218 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 if (focusObj == null) {
5220 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005221 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 }
5223 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005224 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 }
Romain Guy06882f82009-06-10 13:36:04 -07005226
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005227 // Okay we have finished waiting for the last event to be processed.
5228 // First off, if this is a repeat event, check to see if there is
5229 // a corresponding up event in the queue. If there is, we will
5230 // just drop the repeat, because it makes no sense to repeat after
5231 // the user has released a key. (This is especially important for
5232 // long presses.)
5233 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5234 return INJECT_SUCCEEDED;
5235 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 if (DEBUG_INPUT) Log.v(
5240 TAG, "Dispatching to " + focus + ": " + event);
5241
5242 if (uid != 0 && uid != focus.mSession.mUid) {
5243 if (mContext.checkPermission(
5244 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5245 != PackageManager.PERMISSION_GRANTED) {
5246 Log.w(TAG, "Permission denied: injecting key event from pid "
5247 + pid + " uid " + uid + " to window " + focus
5248 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005249 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005250 }
5251 }
Romain Guy06882f82009-06-10 13:36:04 -07005252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 synchronized(mWindowMap) {
5254 mKeyWaiter.bindTargetWindowLocked(focus);
5255 }
5256
5257 // NOSHIP extra state logging
5258 mKeyWaiter.recordDispatchState(event, focus);
5259 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005261 try {
5262 if (DEBUG_INPUT || DEBUG_FOCUS) {
5263 Log.v(TAG, "Delivering key " + event.getKeyCode()
5264 + " to " + focus);
5265 }
5266 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005267 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005268 } catch (android.os.RemoteException e) {
5269 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5270 try {
5271 removeWindow(focus.mSession, focus.mClient);
5272 } catch (java.util.NoSuchElementException ex) {
5273 // This will happen if the window has already been
5274 // removed.
5275 }
5276 }
Romain Guy06882f82009-06-10 13:36:04 -07005277
Dianne Hackborncfaef692009-06-15 14:24:44 -07005278 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279 }
Romain Guy06882f82009-06-10 13:36:04 -07005280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 public void pauseKeyDispatching(IBinder _token) {
5282 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5283 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005284 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 }
5286
5287 synchronized (mWindowMap) {
5288 WindowToken token = mTokenMap.get(_token);
5289 if (token != null) {
5290 mKeyWaiter.pauseDispatchingLocked(token);
5291 }
5292 }
5293 }
5294
5295 public void resumeKeyDispatching(IBinder _token) {
5296 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5297 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005298 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 }
5300
5301 synchronized (mWindowMap) {
5302 WindowToken token = mTokenMap.get(_token);
5303 if (token != null) {
5304 mKeyWaiter.resumeDispatchingLocked(token);
5305 }
5306 }
5307 }
5308
5309 public void setEventDispatching(boolean enabled) {
5310 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5311 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005312 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 }
5314
5315 synchronized (mWindowMap) {
5316 mKeyWaiter.setEventDispatchingLocked(enabled);
5317 }
5318 }
Romain Guy06882f82009-06-10 13:36:04 -07005319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005320 /**
5321 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005322 *
5323 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005324 * {@link SystemClock#uptimeMillis()} as the timebase.)
5325 * @param sync If true, wait for the event to be completed before returning to the caller.
5326 * @return Returns true if event was dispatched, false if it was dropped for any reason
5327 */
5328 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5329 long downTime = ev.getDownTime();
5330 long eventTime = ev.getEventTime();
5331
5332 int action = ev.getAction();
5333 int code = ev.getKeyCode();
5334 int repeatCount = ev.getRepeatCount();
5335 int metaState = ev.getMetaState();
5336 int deviceId = ev.getDeviceId();
5337 int scancode = ev.getScanCode();
5338
5339 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5340 if (downTime == 0) downTime = eventTime;
5341
5342 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005343 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005344
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005345 final int pid = Binder.getCallingPid();
5346 final int uid = Binder.getCallingUid();
5347 final long ident = Binder.clearCallingIdentity();
5348 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005350 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005352 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005353 switch (result) {
5354 case INJECT_NO_PERMISSION:
5355 throw new SecurityException(
5356 "Injecting to another application requires INJECT_EVENT permission");
5357 case INJECT_SUCCEEDED:
5358 return true;
5359 }
5360 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 }
5362
5363 /**
5364 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005365 *
5366 * @param ev A motion event describing the pointer (touch) action. (As noted in
5367 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 * {@link SystemClock#uptimeMillis()} as the timebase.)
5369 * @param sync If true, wait for the event to be completed before returning to the caller.
5370 * @return Returns true if event was dispatched, false if it was dropped for any reason
5371 */
5372 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005373 final int pid = Binder.getCallingPid();
5374 final int uid = Binder.getCallingUid();
5375 final long ident = Binder.clearCallingIdentity();
5376 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005378 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005379 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005380 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005381 switch (result) {
5382 case INJECT_NO_PERMISSION:
5383 throw new SecurityException(
5384 "Injecting to another application requires INJECT_EVENT permission");
5385 case INJECT_SUCCEEDED:
5386 return true;
5387 }
5388 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 }
Romain Guy06882f82009-06-10 13:36:04 -07005390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 /**
5392 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005393 *
5394 * @param ev A motion event describing the trackball action. (As noted in
5395 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 * {@link SystemClock#uptimeMillis()} as the timebase.)
5397 * @param sync If true, wait for the event to be completed before returning to the caller.
5398 * @return Returns true if event was dispatched, false if it was dropped for any reason
5399 */
5400 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005401 final int pid = Binder.getCallingPid();
5402 final int uid = Binder.getCallingUid();
5403 final long ident = Binder.clearCallingIdentity();
5404 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005406 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005408 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005409 switch (result) {
5410 case INJECT_NO_PERMISSION:
5411 throw new SecurityException(
5412 "Injecting to another application requires INJECT_EVENT permission");
5413 case INJECT_SUCCEEDED:
5414 return true;
5415 }
5416 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 }
Romain Guy06882f82009-06-10 13:36:04 -07005418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 private WindowState getFocusedWindow() {
5420 synchronized (mWindowMap) {
5421 return getFocusedWindowLocked();
5422 }
5423 }
5424
5425 private WindowState getFocusedWindowLocked() {
5426 return mCurrentFocus;
5427 }
Romain Guy06882f82009-06-10 13:36:04 -07005428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005429 /**
5430 * This class holds the state for dispatching key events. This state
5431 * is protected by the KeyWaiter instance, NOT by the window lock. You
5432 * can be holding the main window lock while acquire the KeyWaiter lock,
5433 * but not the other way around.
5434 */
5435 final class KeyWaiter {
5436 // NOSHIP debugging
5437 public class DispatchState {
5438 private KeyEvent event;
5439 private WindowState focus;
5440 private long time;
5441 private WindowState lastWin;
5442 private IBinder lastBinder;
5443 private boolean finished;
5444 private boolean gotFirstWindow;
5445 private boolean eventDispatching;
5446 private long timeToSwitch;
5447 private boolean wasFrozen;
5448 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005449 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5452 focus = theFocus;
5453 event = theEvent;
5454 time = System.currentTimeMillis();
5455 // snapshot KeyWaiter state
5456 lastWin = mLastWin;
5457 lastBinder = mLastBinder;
5458 finished = mFinished;
5459 gotFirstWindow = mGotFirstWindow;
5460 eventDispatching = mEventDispatching;
5461 timeToSwitch = mTimeToSwitch;
5462 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005463 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 // cache the paused state at ctor time as well
5465 if (theFocus == null || theFocus.mToken == null) {
5466 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5467 focusPaused = false;
5468 } else {
5469 focusPaused = theFocus.mToken.paused;
5470 }
5471 }
Romain Guy06882f82009-06-10 13:36:04 -07005472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 public String toString() {
5474 return "{{" + event + " to " + focus + " @ " + time
5475 + " lw=" + lastWin + " lb=" + lastBinder
5476 + " fin=" + finished + " gfw=" + gotFirstWindow
5477 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005478 + " wf=" + wasFrozen + " fp=" + focusPaused
5479 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005480 }
5481 };
5482 private DispatchState mDispatchState = null;
5483 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5484 mDispatchState = new DispatchState(theEvent, theFocus);
5485 }
5486 // END NOSHIP
5487
5488 public static final int RETURN_NOTHING = 0;
5489 public static final int RETURN_PENDING_POINTER = 1;
5490 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005492 final Object SKIP_TARGET_TOKEN = new Object();
5493 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005495 private WindowState mLastWin = null;
5496 private IBinder mLastBinder = null;
5497 private boolean mFinished = true;
5498 private boolean mGotFirstWindow = false;
5499 private boolean mEventDispatching = true;
5500 private long mTimeToSwitch = 0;
5501 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 // Target of Motion events
5504 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 // Windows above the target who would like to receive an "outside"
5507 // touch event for any down events outside of them.
5508 WindowState mOutsideTouchTargets;
5509
5510 /**
5511 * Wait for the last event dispatch to complete, then find the next
5512 * target that should receive the given event and wait for that one
5513 * to be ready to receive it.
5514 */
5515 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5516 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005517 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 long startTime = SystemClock.uptimeMillis();
5519 long keyDispatchingTimeout = 5 * 1000;
5520 long waitedFor = 0;
5521
5522 while (true) {
5523 // Figure out which window we care about. It is either the
5524 // last window we are waiting to have process the event or,
5525 // if none, then the next window we think the event should go
5526 // to. Note: we retrieve mLastWin outside of the lock, so
5527 // it may change before we lock. Thus we must check it again.
5528 WindowState targetWin = mLastWin;
5529 boolean targetIsNew = targetWin == null;
5530 if (DEBUG_INPUT) Log.v(
5531 TAG, "waitForLastKey: mFinished=" + mFinished +
5532 ", mLastWin=" + mLastWin);
5533 if (targetIsNew) {
5534 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005535 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536 if (target == SKIP_TARGET_TOKEN) {
5537 // The user has pressed a special key, and we are
5538 // dropping all pending events before it.
5539 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5540 + " " + nextMotion);
5541 return null;
5542 }
5543 if (target == CONSUMED_EVENT_TOKEN) {
5544 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5545 + " " + nextMotion);
5546 return target;
5547 }
5548 targetWin = (WindowState)target;
5549 }
Romain Guy06882f82009-06-10 13:36:04 -07005550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005553 // Now: is it okay to send the next event to this window?
5554 synchronized (this) {
5555 // First: did we come here based on the last window not
5556 // being null, but it changed by the time we got here?
5557 // If so, try again.
5558 if (!targetIsNew && mLastWin == null) {
5559 continue;
5560 }
Romain Guy06882f82009-06-10 13:36:04 -07005561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562 // We never dispatch events if not finished with the
5563 // last one, or the display is frozen.
5564 if (mFinished && !mDisplayFrozen) {
5565 // If event dispatching is disabled, then we
5566 // just consume the events.
5567 if (!mEventDispatching) {
5568 if (DEBUG_INPUT) Log.v(TAG,
5569 "Skipping event; dispatching disabled: "
5570 + nextKey + " " + nextMotion);
5571 return null;
5572 }
5573 if (targetWin != null) {
5574 // If this is a new target, and that target is not
5575 // paused or unresponsive, then all looks good to
5576 // handle the event.
5577 if (targetIsNew && !targetWin.mToken.paused) {
5578 return targetWin;
5579 }
Romain Guy06882f82009-06-10 13:36:04 -07005580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 // If we didn't find a target window, and there is no
5582 // focused app window, then just eat the events.
5583 } else if (mFocusedApp == null) {
5584 if (DEBUG_INPUT) Log.v(TAG,
5585 "Skipping event; no focused app: "
5586 + nextKey + " " + nextMotion);
5587 return null;
5588 }
5589 }
Romain Guy06882f82009-06-10 13:36:04 -07005590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 if (DEBUG_INPUT) Log.v(
5592 TAG, "Waiting for last key in " + mLastBinder
5593 + " target=" + targetWin
5594 + " mFinished=" + mFinished
5595 + " mDisplayFrozen=" + mDisplayFrozen
5596 + " targetIsNew=" + targetIsNew
5597 + " paused="
5598 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005599 + " mFocusedApp=" + mFocusedApp
5600 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005602 targetApp = targetWin != null
5603 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005605 long curTimeout = keyDispatchingTimeout;
5606 if (mTimeToSwitch != 0) {
5607 long now = SystemClock.uptimeMillis();
5608 if (mTimeToSwitch <= now) {
5609 // If an app switch key has been pressed, and we have
5610 // waited too long for the current app to finish
5611 // processing keys, then wait no more!
5612 doFinishedKeyLocked(true);
5613 continue;
5614 }
5615 long switchTimeout = mTimeToSwitch - now;
5616 if (curTimeout > switchTimeout) {
5617 curTimeout = switchTimeout;
5618 }
5619 }
Romain Guy06882f82009-06-10 13:36:04 -07005620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005621 try {
5622 // after that continue
5623 // processing keys, so we don't get stuck.
5624 if (DEBUG_INPUT) Log.v(
5625 TAG, "Waiting for key dispatch: " + curTimeout);
5626 wait(curTimeout);
5627 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5628 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005629 + startTime + " switchTime=" + mTimeToSwitch
5630 + " target=" + targetWin + " mLW=" + mLastWin
5631 + " mLB=" + mLastBinder + " fin=" + mFinished
5632 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 } catch (InterruptedException e) {
5634 }
5635 }
5636
5637 // If we were frozen during configuration change, restart the
5638 // timeout checks from now; otherwise look at whether we timed
5639 // out before awakening.
5640 if (mWasFrozen) {
5641 waitedFor = 0;
5642 mWasFrozen = false;
5643 } else {
5644 waitedFor = SystemClock.uptimeMillis() - startTime;
5645 }
5646
5647 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5648 IApplicationToken at = null;
5649 synchronized (this) {
5650 Log.w(TAG, "Key dispatching timed out sending to " +
5651 (targetWin != null ? targetWin.mAttrs.getTitle()
5652 : "<null>"));
5653 // NOSHIP debugging
5654 Log.w(TAG, "Dispatch state: " + mDispatchState);
5655 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5656 // END NOSHIP
5657 //dump();
5658 if (targetWin != null) {
5659 at = targetWin.getAppToken();
5660 } else if (targetApp != null) {
5661 at = targetApp.appToken;
5662 }
5663 }
5664
5665 boolean abort = true;
5666 if (at != null) {
5667 try {
5668 long timeout = at.getKeyDispatchingTimeout();
5669 if (timeout > waitedFor) {
5670 // we did not wait the proper amount of time for this application.
5671 // set the timeout to be the real timeout and wait again.
5672 keyDispatchingTimeout = timeout - waitedFor;
5673 continue;
5674 } else {
5675 abort = at.keyDispatchingTimedOut();
5676 }
5677 } catch (RemoteException ex) {
5678 }
5679 }
5680
5681 synchronized (this) {
5682 if (abort && (mLastWin == targetWin || targetWin == null)) {
5683 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005684 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 if (DEBUG_INPUT) Log.v(TAG,
5686 "Window " + mLastWin +
5687 " timed out on key input");
5688 if (mLastWin.mToken.paused) {
5689 Log.w(TAG, "Un-pausing dispatching to this window");
5690 mLastWin.mToken.paused = false;
5691 }
5692 }
5693 if (mMotionTarget == targetWin) {
5694 mMotionTarget = null;
5695 }
5696 mLastWin = null;
5697 mLastBinder = null;
5698 if (failIfTimeout || targetWin == null) {
5699 return null;
5700 }
5701 } else {
5702 Log.w(TAG, "Continuing to wait for key to be dispatched");
5703 startTime = SystemClock.uptimeMillis();
5704 }
5705 }
5706 }
5707 }
5708 }
Romain Guy06882f82009-06-10 13:36:04 -07005709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005711 MotionEvent nextMotion, boolean isPointerEvent,
5712 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 if (nextKey != null) {
5716 // Find the target window for a normal key event.
5717 final int keycode = nextKey.getKeyCode();
5718 final int repeatCount = nextKey.getRepeatCount();
5719 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5720 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005723 if (callingUid == 0 ||
5724 mContext.checkPermission(
5725 android.Manifest.permission.INJECT_EVENTS,
5726 callingPid, callingUid)
5727 == PackageManager.PERMISSION_GRANTED) {
5728 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005729 nextKey.getMetaState(), down, repeatCount,
5730 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 Log.w(TAG, "Event timeout during app switch: dropping "
5733 + nextKey);
5734 return SKIP_TARGET_TOKEN;
5735 }
Romain Guy06882f82009-06-10 13:36:04 -07005736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 WindowState focus = null;
5740 synchronized(mWindowMap) {
5741 focus = getFocusedWindowLocked();
5742 }
Romain Guy06882f82009-06-10 13:36:04 -07005743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005745
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005746 if (callingUid == 0 ||
5747 (focus != null && callingUid == focus.mSession.mUid) ||
5748 mContext.checkPermission(
5749 android.Manifest.permission.INJECT_EVENTS,
5750 callingPid, callingUid)
5751 == PackageManager.PERMISSION_GRANTED) {
5752 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005753 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005754 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005755 return CONSUMED_EVENT_TOKEN;
5756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 }
Romain Guy06882f82009-06-10 13:36:04 -07005758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 } else if (!isPointerEvent) {
5762 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5763 if (!dispatch) {
5764 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5765 + nextMotion);
5766 return SKIP_TARGET_TOKEN;
5767 }
Romain Guy06882f82009-06-10 13:36:04 -07005768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005769 WindowState focus = null;
5770 synchronized(mWindowMap) {
5771 focus = getFocusedWindowLocked();
5772 }
Romain Guy06882f82009-06-10 13:36:04 -07005773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5775 return focus;
5776 }
Romain Guy06882f82009-06-10 13:36:04 -07005777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005778 if (nextMotion == null) {
5779 return SKIP_TARGET_TOKEN;
5780 }
Romain Guy06882f82009-06-10 13:36:04 -07005781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5783 KeyEvent.KEYCODE_UNKNOWN);
5784 if (!dispatch) {
5785 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5786 + nextMotion);
5787 return SKIP_TARGET_TOKEN;
5788 }
Romain Guy06882f82009-06-10 13:36:04 -07005789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 // Find the target window for a pointer event.
5791 int action = nextMotion.getAction();
5792 final float xf = nextMotion.getX();
5793 final float yf = nextMotion.getY();
5794 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796 final boolean screenWasOff = qev != null
5797 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005799 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005801 synchronized(mWindowMap) {
5802 synchronized (this) {
5803 if (action == MotionEvent.ACTION_DOWN) {
5804 if (mMotionTarget != null) {
5805 // this is weird, we got a pen down, but we thought it was
5806 // already down!
5807 // XXX: We should probably send an ACTION_UP to the current
5808 // target.
5809 Log.w(TAG, "Pointer down received while already down in: "
5810 + mMotionTarget);
5811 mMotionTarget = null;
5812 }
Romain Guy06882f82009-06-10 13:36:04 -07005813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 // ACTION_DOWN is special, because we need to lock next events to
5815 // the window we'll land onto.
5816 final int x = (int)xf;
5817 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005819 final ArrayList windows = mWindows;
5820 final int N = windows.size();
5821 WindowState topErrWindow = null;
5822 final Rect tmpRect = mTempRect;
5823 for (int i=N-1; i>=0; i--) {
5824 WindowState child = (WindowState)windows.get(i);
5825 //Log.i(TAG, "Checking dispatch to: " + child);
5826 final int flags = child.mAttrs.flags;
5827 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5828 if (topErrWindow == null) {
5829 topErrWindow = child;
5830 }
5831 }
5832 if (!child.isVisibleLw()) {
5833 //Log.i(TAG, "Not visible!");
5834 continue;
5835 }
5836 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5837 //Log.i(TAG, "Not touchable!");
5838 if ((flags & WindowManager.LayoutParams
5839 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5840 child.mNextOutsideTouch = mOutsideTouchTargets;
5841 mOutsideTouchTargets = child;
5842 }
5843 continue;
5844 }
5845 tmpRect.set(child.mFrame);
5846 if (child.mTouchableInsets == ViewTreeObserver
5847 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5848 // The touch is inside of the window if it is
5849 // inside the frame, AND the content part of that
5850 // frame that was given by the application.
5851 tmpRect.left += child.mGivenContentInsets.left;
5852 tmpRect.top += child.mGivenContentInsets.top;
5853 tmpRect.right -= child.mGivenContentInsets.right;
5854 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5855 } else if (child.mTouchableInsets == ViewTreeObserver
5856 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5857 // The touch is inside of the window if it is
5858 // inside the frame, AND the visible part of that
5859 // frame that was given by the application.
5860 tmpRect.left += child.mGivenVisibleInsets.left;
5861 tmpRect.top += child.mGivenVisibleInsets.top;
5862 tmpRect.right -= child.mGivenVisibleInsets.right;
5863 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5864 }
5865 final int touchFlags = flags &
5866 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5867 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5868 if (tmpRect.contains(x, y) || touchFlags == 0) {
5869 //Log.i(TAG, "Using this target!");
5870 if (!screenWasOff || (flags &
5871 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5872 mMotionTarget = child;
5873 } else {
5874 //Log.i(TAG, "Waking, skip!");
5875 mMotionTarget = null;
5876 }
5877 break;
5878 }
Romain Guy06882f82009-06-10 13:36:04 -07005879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 if ((flags & WindowManager.LayoutParams
5881 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5882 child.mNextOutsideTouch = mOutsideTouchTargets;
5883 mOutsideTouchTargets = child;
5884 //Log.i(TAG, "Adding to outside target list: " + child);
5885 }
5886 }
5887
5888 // if there's an error window but it's not accepting
5889 // focus (typically because it is not yet visible) just
5890 // wait for it -- any other focused window may in fact
5891 // be in ANR state.
5892 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5893 mMotionTarget = null;
5894 }
5895 }
Romain Guy06882f82009-06-10 13:36:04 -07005896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 target = mMotionTarget;
5898 }
5899 }
Romain Guy06882f82009-06-10 13:36:04 -07005900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005901 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903 // Pointer events are a little different -- if there isn't a
5904 // target found for any event, then just drop it.
5905 return target != null ? target : SKIP_TARGET_TOKEN;
5906 }
Romain Guy06882f82009-06-10 13:36:04 -07005907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 boolean checkShouldDispatchKey(int keycode) {
5909 synchronized (this) {
5910 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5911 mTimeToSwitch = 0;
5912 return true;
5913 }
5914 if (mTimeToSwitch != 0
5915 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5916 return false;
5917 }
5918 return true;
5919 }
5920 }
Romain Guy06882f82009-06-10 13:36:04 -07005921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922 void bindTargetWindowLocked(WindowState win,
5923 int pendingWhat, QueuedEvent pendingMotion) {
5924 synchronized (this) {
5925 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5926 }
5927 }
Romain Guy06882f82009-06-10 13:36:04 -07005928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005929 void bindTargetWindowLocked(WindowState win) {
5930 synchronized (this) {
5931 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5932 }
5933 }
5934
5935 void bindTargetWindowLockedLocked(WindowState win,
5936 int pendingWhat, QueuedEvent pendingMotion) {
5937 mLastWin = win;
5938 mLastBinder = win.mClient.asBinder();
5939 mFinished = false;
5940 if (pendingMotion != null) {
5941 final Session s = win.mSession;
5942 if (pendingWhat == RETURN_PENDING_POINTER) {
5943 releasePendingPointerLocked(s);
5944 s.mPendingPointerMove = pendingMotion;
5945 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005946 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005947 "bindTargetToWindow " + s.mPendingPointerMove);
5948 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5949 releasePendingTrackballLocked(s);
5950 s.mPendingTrackballMove = pendingMotion;
5951 s.mPendingTrackballWindow = win;
5952 }
5953 }
5954 }
Romain Guy06882f82009-06-10 13:36:04 -07005955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956 void releasePendingPointerLocked(Session s) {
5957 if (DEBUG_INPUT) Log.v(TAG,
5958 "releasePendingPointer " + s.mPendingPointerMove);
5959 if (s.mPendingPointerMove != null) {
5960 mQueue.recycleEvent(s.mPendingPointerMove);
5961 s.mPendingPointerMove = null;
5962 }
5963 }
Romain Guy06882f82009-06-10 13:36:04 -07005964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005965 void releasePendingTrackballLocked(Session s) {
5966 if (s.mPendingTrackballMove != null) {
5967 mQueue.recycleEvent(s.mPendingTrackballMove);
5968 s.mPendingTrackballMove = null;
5969 }
5970 }
Romain Guy06882f82009-06-10 13:36:04 -07005971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5973 int returnWhat) {
5974 if (DEBUG_INPUT) Log.v(
5975 TAG, "finishedKey: client=" + client + ", force=" + force);
5976
5977 if (client == null) {
5978 return null;
5979 }
5980
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005981 MotionEvent res = null;
5982 QueuedEvent qev = null;
5983 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 synchronized (this) {
5986 if (DEBUG_INPUT) Log.v(
5987 TAG, "finishedKey: client=" + client.asBinder()
5988 + ", force=" + force + ", last=" + mLastBinder
5989 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 if (returnWhat == RETURN_PENDING_POINTER) {
5992 qev = session.mPendingPointerMove;
5993 win = session.mPendingPointerWindow;
5994 session.mPendingPointerMove = null;
5995 session.mPendingPointerWindow = null;
5996 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5997 qev = session.mPendingTrackballMove;
5998 win = session.mPendingTrackballWindow;
5999 session.mPendingTrackballMove = null;
6000 session.mPendingTrackballWindow = null;
6001 }
Romain Guy06882f82009-06-10 13:36:04 -07006002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 if (mLastBinder == client.asBinder()) {
6004 if (DEBUG_INPUT) Log.v(
6005 TAG, "finishedKey: last paused="
6006 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6007 if (mLastWin != null && (!mLastWin.mToken.paused || force
6008 || !mEventDispatching)) {
6009 doFinishedKeyLocked(false);
6010 } else {
6011 // Make sure to wake up anyone currently waiting to
6012 // dispatch a key, so they can re-evaluate their
6013 // current situation.
6014 mFinished = true;
6015 notifyAll();
6016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 }
Romain Guy06882f82009-06-10 13:36:04 -07006018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006020 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 if (DEBUG_INPUT) Log.v(TAG,
6022 "Returning pending motion: " + res);
6023 mQueue.recycleEvent(qev);
6024 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6025 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006027 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006029
Christopher Tate2624fbc2009-12-11 12:11:31 -08006030 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6031 synchronized (mWindowMap) {
6032 if ((mWallpaperTarget == win &&
6033 win.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD)
6034 || mSendingPointersToWallpaper) {
6035 sendPointerToWallpaperLocked(win, res, res.getEventTime());
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006036 }
6037 }
6038 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006039
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006040 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 }
6042
6043 void tickle() {
6044 synchronized (this) {
6045 notifyAll();
6046 }
6047 }
Romain Guy06882f82009-06-10 13:36:04 -07006048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 void handleNewWindowLocked(WindowState newWindow) {
6050 if (!newWindow.canReceiveKeys()) {
6051 return;
6052 }
6053 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006054 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055 TAG, "New key dispatch window: win="
6056 + newWindow.mClient.asBinder()
6057 + ", last=" + mLastBinder
6058 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6059 + "), finished=" + mFinished + ", paused="
6060 + newWindow.mToken.paused);
6061
6062 // Displaying a window implicitly causes dispatching to
6063 // be unpaused. (This is to protect against bugs if someone
6064 // pauses dispatching but forgets to resume.)
6065 newWindow.mToken.paused = false;
6066
6067 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068
6069 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
6070 if (DEBUG_INPUT) Log.v(TAG,
6071 "New SYSTEM_ERROR window; resetting state");
6072 mLastWin = null;
6073 mLastBinder = null;
6074 mMotionTarget = null;
6075 mFinished = true;
6076 } else if (mLastWin != null) {
6077 // If the new window is above the window we are
6078 // waiting on, then stop waiting and let key dispatching
6079 // start on the new guy.
6080 if (DEBUG_INPUT) Log.v(
6081 TAG, "Last win layer=" + mLastWin.mLayer
6082 + ", new win layer=" + newWindow.mLayer);
6083 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006084 // The new window is above the old; finish pending input to the last
6085 // window and start directing it to the new one.
6086 mLastWin.mToken.paused = false;
6087 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006088 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006089 // Either the new window is lower, so there is no need to wake key waiters,
6090 // or we just finished key input to the previous window, which implicitly
6091 // notified the key waiters. In both cases, we don't need to issue the
6092 // notification here.
6093 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 }
6095
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006096 // Now that we've put a new window state in place, make the event waiter
6097 // take notice and retarget its attentions.
6098 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 }
6100 }
6101
6102 void pauseDispatchingLocked(WindowToken token) {
6103 synchronized (this)
6104 {
6105 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
6106 token.paused = true;
6107
6108 /*
6109 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6110 mPaused = true;
6111 } else {
6112 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07006113 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07006115 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07006117 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 }
6119 }
6120 */
6121 }
6122 }
6123
6124 void resumeDispatchingLocked(WindowToken token) {
6125 synchronized (this) {
6126 if (token.paused) {
6127 if (DEBUG_INPUT) Log.v(
6128 TAG, "Resuming WindowToken " + token
6129 + ", last=" + mLastBinder
6130 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6131 + "), finished=" + mFinished + ", paused="
6132 + token.paused);
6133 token.paused = false;
6134 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
6135 doFinishedKeyLocked(true);
6136 } else {
6137 notifyAll();
6138 }
6139 }
6140 }
6141 }
6142
6143 void setEventDispatchingLocked(boolean enabled) {
6144 synchronized (this) {
6145 mEventDispatching = enabled;
6146 notifyAll();
6147 }
6148 }
Romain Guy06882f82009-06-10 13:36:04 -07006149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006150 void appSwitchComing() {
6151 synchronized (this) {
6152 // Don't wait for more than .5 seconds for app to finish
6153 // processing the pending events.
6154 long now = SystemClock.uptimeMillis() + 500;
6155 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6156 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6157 mTimeToSwitch = now;
6158 }
6159 notifyAll();
6160 }
6161 }
Romain Guy06882f82009-06-10 13:36:04 -07006162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 private final void doFinishedKeyLocked(boolean doRecycle) {
6164 if (mLastWin != null) {
6165 releasePendingPointerLocked(mLastWin.mSession);
6166 releasePendingTrackballLocked(mLastWin.mSession);
6167 }
Romain Guy06882f82009-06-10 13:36:04 -07006168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 if (mLastWin == null || !mLastWin.mToken.paused
6170 || !mLastWin.isVisibleLw()) {
6171 // If the current window has been paused, we aren't -really-
6172 // finished... so let the waiters still wait.
6173 mLastWin = null;
6174 mLastBinder = null;
6175 }
6176 mFinished = true;
6177 notifyAll();
6178 }
6179 }
6180
6181 private class KeyQ extends KeyInputQueue
6182 implements KeyInputQueue.FilterCallback {
6183 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006186 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6188 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6189 "KEEP_SCREEN_ON_FLAG");
6190 mHoldingScreen.setReferenceCounted(false);
6191 }
6192
6193 @Override
6194 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6195 if (mPolicy.preprocessInputEventTq(event)) {
6196 return true;
6197 }
Romain Guy06882f82009-06-10 13:36:04 -07006198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006199 switch (event.type) {
6200 case RawInputEvent.EV_KEY: {
6201 // XXX begin hack
6202 if (DEBUG) {
6203 if (event.keycode == KeyEvent.KEYCODE_G) {
6204 if (event.value != 0) {
6205 // G down
6206 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6207 }
6208 return false;
6209 }
6210 if (event.keycode == KeyEvent.KEYCODE_D) {
6211 if (event.value != 0) {
6212 //dump();
6213 }
6214 return false;
6215 }
6216 }
6217 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006218
Charles Mendis322591c2009-10-29 11:06:59 -07006219 boolean screenIsOff = !mPowerManager.isScreenOn();
6220 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006221 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6224 mPowerManager.goToSleep(event.when);
6225 }
6226
6227 if (screenIsOff) {
6228 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6229 }
6230 if (screenIsDim) {
6231 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6232 }
6233 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6234 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006235 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006236 }
Romain Guy06882f82009-06-10 13:36:04 -07006237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006238 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6239 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6240 filterQueue(this);
6241 mKeyWaiter.appSwitchComing();
6242 }
6243 return true;
6244 } else {
6245 return false;
6246 }
6247 }
Romain Guy06882f82009-06-10 13:36:04 -07006248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006249 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006250 boolean screenIsOff = !mPowerManager.isScreenOn();
6251 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252 if (screenIsOff) {
6253 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6254 device.classes, event)) {
6255 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6256 return false;
6257 }
6258 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6259 }
6260 if (screenIsDim) {
6261 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6262 }
6263 return true;
6264 }
Romain Guy06882f82009-06-10 13:36:04 -07006265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006267 boolean screenIsOff = !mPowerManager.isScreenOn();
6268 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 if (screenIsOff) {
6270 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6271 device.classes, event)) {
6272 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6273 return false;
6274 }
6275 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6276 }
6277 if (screenIsDim) {
6278 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6279 }
6280 return true;
6281 }
Romain Guy06882f82009-06-10 13:36:04 -07006282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006283 default:
6284 return true;
6285 }
6286 }
6287
6288 public int filterEvent(QueuedEvent ev) {
6289 switch (ev.classType) {
6290 case RawInputEvent.CLASS_KEYBOARD:
6291 KeyEvent ke = (KeyEvent)ev.event;
6292 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6293 Log.w(TAG, "Dropping movement key during app switch: "
6294 + ke.getKeyCode() + ", action=" + ke.getAction());
6295 return FILTER_REMOVE;
6296 }
6297 return FILTER_ABORT;
6298 default:
6299 return FILTER_KEEP;
6300 }
6301 }
Romain Guy06882f82009-06-10 13:36:04 -07006302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006303 /**
6304 * Must be called with the main window manager lock held.
6305 */
6306 void setHoldScreenLocked(boolean holding) {
6307 boolean state = mHoldingScreen.isHeld();
6308 if (holding != state) {
6309 if (holding) {
6310 mHoldingScreen.acquire();
6311 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006312 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 mHoldingScreen.release();
6314 }
6315 }
6316 }
Michael Chan53071d62009-05-13 17:29:48 -07006317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006318
6319 public boolean detectSafeMode() {
6320 mSafeMode = mPolicy.detectSafeMode();
6321 return mSafeMode;
6322 }
Romain Guy06882f82009-06-10 13:36:04 -07006323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324 public void systemReady() {
6325 mPolicy.systemReady();
6326 }
Romain Guy06882f82009-06-10 13:36:04 -07006327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 private final class InputDispatcherThread extends Thread {
6329 // Time to wait when there is nothing to do: 9999 seconds.
6330 static final int LONG_WAIT=9999*1000;
6331
6332 public InputDispatcherThread() {
6333 super("InputDispatcher");
6334 }
Romain Guy06882f82009-06-10 13:36:04 -07006335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006336 @Override
6337 public void run() {
6338 while (true) {
6339 try {
6340 process();
6341 } catch (Exception e) {
6342 Log.e(TAG, "Exception in input dispatcher", e);
6343 }
6344 }
6345 }
Romain Guy06882f82009-06-10 13:36:04 -07006346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 private void process() {
6348 android.os.Process.setThreadPriority(
6349 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 // The last key event we saw
6352 KeyEvent lastKey = null;
6353
6354 // Last keydown time for auto-repeating keys
6355 long lastKeyTime = SystemClock.uptimeMillis();
6356 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006357 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006358
Romain Guy06882f82009-06-10 13:36:04 -07006359 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 int keyRepeatCount = 0;
6361
6362 // Need to report that configuration has changed?
6363 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006365 while (true) {
6366 long curTime = SystemClock.uptimeMillis();
6367
6368 if (DEBUG_INPUT) Log.v(
6369 TAG, "Waiting for next key: now=" + curTime
6370 + ", repeat @ " + nextKeyTime);
6371
6372 // Retrieve next event, waiting only as long as the next
6373 // repeat timeout. If the configuration has changed, then
6374 // don't wait at all -- we'll report the change as soon as
6375 // we have processed all events.
6376 QueuedEvent ev = mQueue.getEvent(
6377 (int)((!configChanged && curTime < nextKeyTime)
6378 ? (nextKeyTime-curTime) : 0));
6379
6380 if (DEBUG_INPUT && ev != null) Log.v(
6381 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6382
Michael Chan53071d62009-05-13 17:29:48 -07006383 if (MEASURE_LATENCY) {
6384 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6385 }
6386
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006387 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6388 // cancel key repeat at the request of the policy.
6389 lastKey = null;
6390 downTime = 0;
6391 lastKeyTime = curTime;
6392 nextKeyTime = curTime + LONG_WAIT;
6393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006394 try {
6395 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006396 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006397 int eventType;
6398 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6399 eventType = eventType((MotionEvent)ev.event);
6400 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6401 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6402 eventType = LocalPowerManager.BUTTON_EVENT;
6403 } else {
6404 eventType = LocalPowerManager.OTHER_EVENT;
6405 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006406 try {
Michael Chan53071d62009-05-13 17:29:48 -07006407 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006408 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006409 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006410 mBatteryStats.noteInputEvent();
6411 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006412 } catch (RemoteException e) {
6413 // Ignore
6414 }
Michael Chane10de972009-05-18 11:24:50 -07006415
Mike Lockwood5db42402009-11-30 14:51:51 -05006416 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6417 // do not wake screen in this case
6418 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006419 && eventType != LONG_TOUCH_EVENT
6420 && eventType != CHEEK_EVENT) {
6421 mPowerManager.userActivity(curTime, false,
6422 eventType, false);
6423 } else if (mLastTouchEventType != eventType
6424 || (curTime - mLastUserActivityCallTime)
6425 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6426 mLastUserActivityCallTime = curTime;
6427 mLastTouchEventType = eventType;
6428 mPowerManager.userActivity(curTime, false,
6429 eventType, false);
6430 }
6431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006432 switch (ev.classType) {
6433 case RawInputEvent.CLASS_KEYBOARD:
6434 KeyEvent ke = (KeyEvent)ev.event;
6435 if (ke.isDown()) {
6436 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006437 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 keyRepeatCount = 0;
6439 lastKeyTime = curTime;
6440 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006441 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006442 if (DEBUG_INPUT) Log.v(
6443 TAG, "Received key down: first repeat @ "
6444 + nextKeyTime);
6445 } else {
6446 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006447 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 // Arbitrary long timeout.
6449 lastKeyTime = curTime;
6450 nextKeyTime = curTime + LONG_WAIT;
6451 if (DEBUG_INPUT) Log.v(
6452 TAG, "Received key up: ignore repeat @ "
6453 + nextKeyTime);
6454 }
6455 dispatchKey((KeyEvent)ev.event, 0, 0);
6456 mQueue.recycleEvent(ev);
6457 break;
6458 case RawInputEvent.CLASS_TOUCHSCREEN:
6459 //Log.i(TAG, "Read next event " + ev);
6460 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6461 break;
6462 case RawInputEvent.CLASS_TRACKBALL:
6463 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6464 break;
6465 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6466 configChanged = true;
6467 break;
6468 default:
6469 mQueue.recycleEvent(ev);
6470 break;
6471 }
Romain Guy06882f82009-06-10 13:36:04 -07006472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006473 } else if (configChanged) {
6474 configChanged = false;
6475 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006477 } else if (lastKey != null) {
6478 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006480 // Timeout occurred while key was down. If it is at or
6481 // past the key repeat time, dispatch the repeat.
6482 if (DEBUG_INPUT) Log.v(
6483 TAG, "Key timeout: repeat=" + nextKeyTime
6484 + ", now=" + curTime);
6485 if (curTime < nextKeyTime) {
6486 continue;
6487 }
Romain Guy06882f82009-06-10 13:36:04 -07006488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 lastKeyTime = nextKeyTime;
6490 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6491 keyRepeatCount++;
6492 if (DEBUG_INPUT) Log.v(
6493 TAG, "Key repeat: count=" + keyRepeatCount
6494 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006495 KeyEvent newEvent;
6496 if (downTime != 0 && (downTime
6497 + ViewConfiguration.getLongPressTimeout())
6498 <= curTime) {
6499 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6500 curTime, keyRepeatCount,
6501 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6502 downTime = 0;
6503 } else {
6504 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6505 curTime, keyRepeatCount);
6506 }
6507 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 } else {
6510 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006512 lastKeyTime = curTime;
6513 nextKeyTime = curTime + LONG_WAIT;
6514 }
Romain Guy06882f82009-06-10 13:36:04 -07006515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006516 } catch (Exception e) {
6517 Log.e(TAG,
6518 "Input thread received uncaught exception: " + e, e);
6519 }
6520 }
6521 }
6522 }
6523
6524 // -------------------------------------------------------------
6525 // Client Session State
6526 // -------------------------------------------------------------
6527
6528 private final class Session extends IWindowSession.Stub
6529 implements IBinder.DeathRecipient {
6530 final IInputMethodClient mClient;
6531 final IInputContext mInputContext;
6532 final int mUid;
6533 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006534 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 SurfaceSession mSurfaceSession;
6536 int mNumWindow = 0;
6537 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 /**
6540 * Current pointer move event being dispatched to client window... must
6541 * hold key lock to access.
6542 */
6543 QueuedEvent mPendingPointerMove;
6544 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006546 /**
6547 * Current trackball move event being dispatched to client window... must
6548 * hold key lock to access.
6549 */
6550 QueuedEvent mPendingTrackballMove;
6551 WindowState mPendingTrackballWindow;
6552
6553 public Session(IInputMethodClient client, IInputContext inputContext) {
6554 mClient = client;
6555 mInputContext = inputContext;
6556 mUid = Binder.getCallingUid();
6557 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006558 StringBuilder sb = new StringBuilder();
6559 sb.append("Session{");
6560 sb.append(Integer.toHexString(System.identityHashCode(this)));
6561 sb.append(" uid ");
6562 sb.append(mUid);
6563 sb.append("}");
6564 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006566 synchronized (mWindowMap) {
6567 if (mInputMethodManager == null && mHaveInputMethods) {
6568 IBinder b = ServiceManager.getService(
6569 Context.INPUT_METHOD_SERVICE);
6570 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6571 }
6572 }
6573 long ident = Binder.clearCallingIdentity();
6574 try {
6575 // Note: it is safe to call in to the input method manager
6576 // here because we are not holding our lock.
6577 if (mInputMethodManager != null) {
6578 mInputMethodManager.addClient(client, inputContext,
6579 mUid, mPid);
6580 } else {
6581 client.setUsingInputMethod(false);
6582 }
6583 client.asBinder().linkToDeath(this, 0);
6584 } catch (RemoteException e) {
6585 // The caller has died, so we can just forget about this.
6586 try {
6587 if (mInputMethodManager != null) {
6588 mInputMethodManager.removeClient(client);
6589 }
6590 } catch (RemoteException ee) {
6591 }
6592 } finally {
6593 Binder.restoreCallingIdentity(ident);
6594 }
6595 }
Romain Guy06882f82009-06-10 13:36:04 -07006596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 @Override
6598 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6599 throws RemoteException {
6600 try {
6601 return super.onTransact(code, data, reply, flags);
6602 } catch (RuntimeException e) {
6603 // Log all 'real' exceptions thrown to the caller
6604 if (!(e instanceof SecurityException)) {
6605 Log.e(TAG, "Window Session Crash", e);
6606 }
6607 throw e;
6608 }
6609 }
6610
6611 public void binderDied() {
6612 // Note: it is safe to call in to the input method manager
6613 // here because we are not holding our lock.
6614 try {
6615 if (mInputMethodManager != null) {
6616 mInputMethodManager.removeClient(mClient);
6617 }
6618 } catch (RemoteException e) {
6619 }
6620 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006621 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006622 mClientDead = true;
6623 killSessionLocked();
6624 }
6625 }
6626
6627 public int add(IWindow window, WindowManager.LayoutParams attrs,
6628 int viewVisibility, Rect outContentInsets) {
6629 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6630 }
Romain Guy06882f82009-06-10 13:36:04 -07006631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 public void remove(IWindow window) {
6633 removeWindow(this, window);
6634 }
Romain Guy06882f82009-06-10 13:36:04 -07006635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6637 int requestedWidth, int requestedHeight, int viewFlags,
6638 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6639 Rect outVisibleInsets, Surface outSurface) {
6640 return relayoutWindow(this, window, attrs,
6641 requestedWidth, requestedHeight, viewFlags, insetsPending,
6642 outFrame, outContentInsets, outVisibleInsets, outSurface);
6643 }
Romain Guy06882f82009-06-10 13:36:04 -07006644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 public void setTransparentRegion(IWindow window, Region region) {
6646 setTransparentRegionWindow(this, window, region);
6647 }
Romain Guy06882f82009-06-10 13:36:04 -07006648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006649 public void setInsets(IWindow window, int touchableInsets,
6650 Rect contentInsets, Rect visibleInsets) {
6651 setInsetsWindow(this, window, touchableInsets, contentInsets,
6652 visibleInsets);
6653 }
Romain Guy06882f82009-06-10 13:36:04 -07006654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006655 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6656 getWindowDisplayFrame(this, window, outDisplayFrame);
6657 }
Romain Guy06882f82009-06-10 13:36:04 -07006658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006659 public void finishDrawing(IWindow window) {
6660 if (localLOGV) Log.v(
6661 TAG, "IWindow finishDrawing called for " + window);
6662 finishDrawingWindow(this, window);
6663 }
6664
6665 public void finishKey(IWindow window) {
6666 if (localLOGV) Log.v(
6667 TAG, "IWindow finishKey called for " + window);
6668 mKeyWaiter.finishedKey(this, window, false,
6669 KeyWaiter.RETURN_NOTHING);
6670 }
6671
6672 public MotionEvent getPendingPointerMove(IWindow window) {
6673 if (localLOGV) Log.v(
6674 TAG, "IWindow getPendingMotionEvent called for " + window);
6675 return mKeyWaiter.finishedKey(this, window, false,
6676 KeyWaiter.RETURN_PENDING_POINTER);
6677 }
Romain Guy06882f82009-06-10 13:36:04 -07006678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 public MotionEvent getPendingTrackballMove(IWindow window) {
6680 if (localLOGV) Log.v(
6681 TAG, "IWindow getPendingMotionEvent called for " + window);
6682 return mKeyWaiter.finishedKey(this, window, false,
6683 KeyWaiter.RETURN_PENDING_TRACKBALL);
6684 }
6685
6686 public void setInTouchMode(boolean mode) {
6687 synchronized(mWindowMap) {
6688 mInTouchMode = mode;
6689 }
6690 }
6691
6692 public boolean getInTouchMode() {
6693 synchronized(mWindowMap) {
6694 return mInTouchMode;
6695 }
6696 }
6697
6698 public boolean performHapticFeedback(IWindow window, int effectId,
6699 boolean always) {
6700 synchronized(mWindowMap) {
6701 long ident = Binder.clearCallingIdentity();
6702 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006703 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006704 windowForClientLocked(this, window), effectId, always);
6705 } finally {
6706 Binder.restoreCallingIdentity(ident);
6707 }
6708 }
6709 }
Romain Guy06882f82009-06-10 13:36:04 -07006710
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006711 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006712 synchronized(mWindowMap) {
6713 long ident = Binder.clearCallingIdentity();
6714 try {
6715 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006716 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006717 } finally {
6718 Binder.restoreCallingIdentity(ident);
6719 }
6720 }
6721 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006722
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006723 public void wallpaperOffsetsComplete(IBinder window) {
6724 WindowManagerService.this.wallpaperOffsetsComplete(window);
6725 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006726
Dianne Hackborn75804932009-10-20 20:15:20 -07006727 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6728 int z, Bundle extras, boolean sync) {
6729 synchronized(mWindowMap) {
6730 long ident = Binder.clearCallingIdentity();
6731 try {
6732 return sendWindowWallpaperCommandLocked(
6733 windowForClientLocked(this, window),
6734 action, x, y, z, extras, sync);
6735 } finally {
6736 Binder.restoreCallingIdentity(ident);
6737 }
6738 }
6739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006740
Dianne Hackborn75804932009-10-20 20:15:20 -07006741 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6742 WindowManagerService.this.wallpaperCommandComplete(window, result);
6743 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006745 void windowAddedLocked() {
6746 if (mSurfaceSession == null) {
6747 if (localLOGV) Log.v(
6748 TAG, "First window added to " + this + ", creating SurfaceSession");
6749 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006750 if (SHOW_TRANSACTIONS) Log.i(
6751 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006752 mSessions.add(this);
6753 }
6754 mNumWindow++;
6755 }
6756
6757 void windowRemovedLocked() {
6758 mNumWindow--;
6759 killSessionLocked();
6760 }
Romain Guy06882f82009-06-10 13:36:04 -07006761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 void killSessionLocked() {
6763 if (mNumWindow <= 0 && mClientDead) {
6764 mSessions.remove(this);
6765 if (mSurfaceSession != null) {
6766 if (localLOGV) Log.v(
6767 TAG, "Last window removed from " + this
6768 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006769 if (SHOW_TRANSACTIONS) Log.i(
6770 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 try {
6772 mSurfaceSession.kill();
6773 } catch (Exception e) {
6774 Log.w(TAG, "Exception thrown when killing surface session "
6775 + mSurfaceSession + " in session " + this
6776 + ": " + e.toString());
6777 }
6778 mSurfaceSession = null;
6779 }
6780 }
6781 }
Romain Guy06882f82009-06-10 13:36:04 -07006782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006784 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6785 pw.print(" mClientDead="); pw.print(mClientDead);
6786 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6787 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6788 pw.print(prefix);
6789 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6790 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6791 }
6792 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6793 pw.print(prefix);
6794 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6795 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797 }
6798
6799 @Override
6800 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006801 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802 }
6803 }
6804
6805 // -------------------------------------------------------------
6806 // Client Window State
6807 // -------------------------------------------------------------
6808
6809 private final class WindowState implements WindowManagerPolicy.WindowState {
6810 final Session mSession;
6811 final IWindow mClient;
6812 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006813 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814 AppWindowToken mAppToken;
6815 AppWindowToken mTargetAppToken;
6816 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6817 final DeathRecipient mDeathRecipient;
6818 final WindowState mAttachedWindow;
6819 final ArrayList mChildWindows = new ArrayList();
6820 final int mBaseLayer;
6821 final int mSubLayer;
6822 final boolean mLayoutAttached;
6823 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006824 final boolean mIsWallpaper;
6825 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 int mViewVisibility;
6827 boolean mPolicyVisibility = true;
6828 boolean mPolicyVisibilityAfterAnim = true;
6829 boolean mAppFreezing;
6830 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006831 boolean mReportDestroySurface;
6832 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 boolean mAttachedHidden; // is our parent window hidden?
6834 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006835 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006836 int mRequestedWidth;
6837 int mRequestedHeight;
6838 int mLastRequestedWidth;
6839 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006840 int mLayer;
6841 int mAnimLayer;
6842 int mLastLayer;
6843 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006844 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006845 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846
6847 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006849 // Actual frame shown on-screen (may be modified by animation)
6850 final Rect mShownFrame = new Rect();
6851 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853 /**
6854 * Insets that determine the actually visible area
6855 */
6856 final Rect mVisibleInsets = new Rect();
6857 final Rect mLastVisibleInsets = new Rect();
6858 boolean mVisibleInsetsChanged;
6859
6860 /**
6861 * Insets that are covered by system windows
6862 */
6863 final Rect mContentInsets = new Rect();
6864 final Rect mLastContentInsets = new Rect();
6865 boolean mContentInsetsChanged;
6866
6867 /**
6868 * Set to true if we are waiting for this window to receive its
6869 * given internal insets before laying out other windows based on it.
6870 */
6871 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 /**
6874 * These are the content insets that were given during layout for
6875 * this window, to be applied to windows behind it.
6876 */
6877 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879 /**
6880 * These are the visible insets that were given during layout for
6881 * this window, to be applied to windows behind it.
6882 */
6883 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006885 /**
6886 * Flag indicating whether the touchable region should be adjusted by
6887 * the visible insets; if false the area outside the visible insets is
6888 * NOT touchable, so we must use those to adjust the frame during hit
6889 * tests.
6890 */
6891 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 // Current transformation being applied.
6894 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6895 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6896 float mHScale=1, mVScale=1;
6897 float mLastHScale=1, mLastVScale=1;
6898 final Matrix mTmpMatrix = new Matrix();
6899
6900 // "Real" frame that the application sees.
6901 final Rect mFrame = new Rect();
6902 final Rect mLastFrame = new Rect();
6903
6904 final Rect mContainingFrame = new Rect();
6905 final Rect mDisplayFrame = new Rect();
6906 final Rect mContentFrame = new Rect();
6907 final Rect mVisibleFrame = new Rect();
6908
6909 float mShownAlpha = 1;
6910 float mAlpha = 1;
6911 float mLastAlpha = 1;
6912
6913 // Set to true if, when the window gets displayed, it should perform
6914 // an enter animation.
6915 boolean mEnterAnimationPending;
6916
6917 // Currently running animation.
6918 boolean mAnimating;
6919 boolean mLocalAnimating;
6920 Animation mAnimation;
6921 boolean mAnimationIsEntrance;
6922 boolean mHasTransformation;
6923 boolean mHasLocalTransformation;
6924 final Transformation mTransformation = new Transformation();
6925
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006926 // If a window showing a wallpaper: the requested offset for the
6927 // wallpaper; if a wallpaper window: the currently applied offset.
6928 float mWallpaperX = -1;
6929 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006930
6931 // If a window showing a wallpaper: what fraction of the offset
6932 // range corresponds to a full virtual screen.
6933 float mWallpaperXStep = -1;
6934 float mWallpaperYStep = -1;
6935
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006936 // Wallpaper windows: pixels offset based on above variables.
6937 int mXOffset;
6938 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006940 // This is set after IWindowSession.relayout() has been called at
6941 // least once for the window. It allows us to detect the situation
6942 // where we don't yet have a surface, but should have one soon, so
6943 // we can give the window focus before waiting for the relayout.
6944 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 // This is set after the Surface has been created but before the
6947 // window has been drawn. During this time the surface is hidden.
6948 boolean mDrawPending;
6949
6950 // This is set after the window has finished drawing for the first
6951 // time but before its surface is shown. The surface will be
6952 // displayed when the next layout is run.
6953 boolean mCommitDrawPending;
6954
6955 // This is set during the time after the window's drawing has been
6956 // committed, and before its surface is actually shown. It is used
6957 // to delay showing the surface until all windows in a token are ready
6958 // to be shown.
6959 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006961 // Set when the window has been shown in the screen the first time.
6962 boolean mHasDrawn;
6963
6964 // Currently running an exit animation?
6965 boolean mExiting;
6966
6967 // Currently on the mDestroySurface list?
6968 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006970 // Completely remove from window manager after exit animation?
6971 boolean mRemoveOnExit;
6972
6973 // Set when the orientation is changing and this window has not yet
6974 // been updated for the new orientation.
6975 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977 // Is this window now (or just being) removed?
6978 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006980 WindowState(Session s, IWindow c, WindowToken token,
6981 WindowState attachedWindow, WindowManager.LayoutParams a,
6982 int viewVisibility) {
6983 mSession = s;
6984 mClient = c;
6985 mToken = token;
6986 mAttrs.copyFrom(a);
6987 mViewVisibility = viewVisibility;
6988 DeathRecipient deathRecipient = new DeathRecipient();
6989 mAlpha = a.alpha;
6990 if (localLOGV) Log.v(
6991 TAG, "Window " + this + " client=" + c.asBinder()
6992 + " token=" + token + " (" + mAttrs.token + ")");
6993 try {
6994 c.asBinder().linkToDeath(deathRecipient, 0);
6995 } catch (RemoteException e) {
6996 mDeathRecipient = null;
6997 mAttachedWindow = null;
6998 mLayoutAttached = false;
6999 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007000 mIsWallpaper = false;
7001 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 mBaseLayer = 0;
7003 mSubLayer = 0;
7004 return;
7005 }
7006 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7009 mAttrs.type <= LAST_SUB_WINDOW)) {
7010 // The multiplier here is to reserve space for multiple
7011 // windows in the same type layer.
7012 mBaseLayer = mPolicy.windowTypeToLayerLw(
7013 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7014 + TYPE_LAYER_OFFSET;
7015 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7016 mAttachedWindow = attachedWindow;
7017 mAttachedWindow.mChildWindows.add(this);
7018 mLayoutAttached = mAttrs.type !=
7019 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7020 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7021 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007022 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7023 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007024 } else {
7025 // The multiplier here is to reserve space for multiple
7026 // windows in the same type layer.
7027 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7028 * TYPE_LAYER_MULTIPLIER
7029 + TYPE_LAYER_OFFSET;
7030 mSubLayer = 0;
7031 mAttachedWindow = null;
7032 mLayoutAttached = false;
7033 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7034 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007035 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7036 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007037 }
7038
7039 WindowState appWin = this;
7040 while (appWin.mAttachedWindow != null) {
7041 appWin = mAttachedWindow;
7042 }
7043 WindowToken appToken = appWin.mToken;
7044 while (appToken.appWindowToken == null) {
7045 WindowToken parent = mTokenMap.get(appToken.token);
7046 if (parent == null || appToken == parent) {
7047 break;
7048 }
7049 appToken = parent;
7050 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007051 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007052 mAppToken = appToken.appWindowToken;
7053
7054 mSurface = null;
7055 mRequestedWidth = 0;
7056 mRequestedHeight = 0;
7057 mLastRequestedWidth = 0;
7058 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007059 mXOffset = 0;
7060 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007061 mLayer = 0;
7062 mAnimLayer = 0;
7063 mLastLayer = 0;
7064 }
7065
7066 void attach() {
7067 if (localLOGV) Log.v(
7068 TAG, "Attaching " + this + " token=" + mToken
7069 + ", list=" + mToken.windows);
7070 mSession.windowAddedLocked();
7071 }
7072
7073 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7074 mHaveFrame = true;
7075
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007076 final Rect container = mContainingFrame;
7077 container.set(pf);
7078
7079 final Rect display = mDisplayFrame;
7080 display.set(df);
7081
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007082 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007083 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007084 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7085 display.intersect(mCompatibleScreenFrame);
7086 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007087 }
7088
7089 final int pw = container.right - container.left;
7090 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091
7092 int w,h;
7093 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7094 w = mAttrs.width < 0 ? pw : mAttrs.width;
7095 h = mAttrs.height< 0 ? ph : mAttrs.height;
7096 } else {
7097 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
7098 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
7099 }
Romain Guy06882f82009-06-10 13:36:04 -07007100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101 final Rect content = mContentFrame;
7102 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007104 final Rect visible = mVisibleFrame;
7105 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007107 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007108 final int fw = frame.width();
7109 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7112 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7113
7114 Gravity.apply(mAttrs.gravity, w, h, container,
7115 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7116 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7117
7118 //System.out.println("Out: " + mFrame);
7119
7120 // Now make sure the window fits in the overall display.
7121 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 // Make sure the content and visible frames are inside of the
7124 // final window frame.
7125 if (content.left < frame.left) content.left = frame.left;
7126 if (content.top < frame.top) content.top = frame.top;
7127 if (content.right > frame.right) content.right = frame.right;
7128 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7129 if (visible.left < frame.left) visible.left = frame.left;
7130 if (visible.top < frame.top) visible.top = frame.top;
7131 if (visible.right > frame.right) visible.right = frame.right;
7132 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007134 final Rect contentInsets = mContentInsets;
7135 contentInsets.left = content.left-frame.left;
7136 contentInsets.top = content.top-frame.top;
7137 contentInsets.right = frame.right-content.right;
7138 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007140 final Rect visibleInsets = mVisibleInsets;
7141 visibleInsets.left = visible.left-frame.left;
7142 visibleInsets.top = visible.top-frame.top;
7143 visibleInsets.right = frame.right-visible.right;
7144 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007145
Dianne Hackborn284ac932009-08-28 10:34:25 -07007146 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7147 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007148 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151 if (localLOGV) {
7152 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7153 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7154 Log.v(TAG, "Resolving (mRequestedWidth="
7155 + mRequestedWidth + ", mRequestedheight="
7156 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7157 + "): frame=" + mFrame.toShortString()
7158 + " ci=" + contentInsets.toShortString()
7159 + " vi=" + visibleInsets.toShortString());
7160 //}
7161 }
7162 }
Romain Guy06882f82009-06-10 13:36:04 -07007163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007164 public Rect getFrameLw() {
7165 return mFrame;
7166 }
7167
7168 public Rect getShownFrameLw() {
7169 return mShownFrame;
7170 }
7171
7172 public Rect getDisplayFrameLw() {
7173 return mDisplayFrame;
7174 }
7175
7176 public Rect getContentFrameLw() {
7177 return mContentFrame;
7178 }
7179
7180 public Rect getVisibleFrameLw() {
7181 return mVisibleFrame;
7182 }
7183
7184 public boolean getGivenInsetsPendingLw() {
7185 return mGivenInsetsPending;
7186 }
7187
7188 public Rect getGivenContentInsetsLw() {
7189 return mGivenContentInsets;
7190 }
Romain Guy06882f82009-06-10 13:36:04 -07007191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007192 public Rect getGivenVisibleInsetsLw() {
7193 return mGivenVisibleInsets;
7194 }
Romain Guy06882f82009-06-10 13:36:04 -07007195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007196 public WindowManager.LayoutParams getAttrs() {
7197 return mAttrs;
7198 }
7199
7200 public int getSurfaceLayer() {
7201 return mLayer;
7202 }
Romain Guy06882f82009-06-10 13:36:04 -07007203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007204 public IApplicationToken getAppToken() {
7205 return mAppToken != null ? mAppToken.appToken : null;
7206 }
7207
7208 public boolean hasAppShownWindows() {
7209 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7210 }
7211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007212 public void setAnimation(Animation anim) {
7213 if (localLOGV) Log.v(
7214 TAG, "Setting animation in " + this + ": " + anim);
7215 mAnimating = false;
7216 mLocalAnimating = false;
7217 mAnimation = anim;
7218 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7219 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7220 }
7221
7222 public void clearAnimation() {
7223 if (mAnimation != null) {
7224 mAnimating = true;
7225 mLocalAnimating = false;
7226 mAnimation = null;
7227 }
7228 }
Romain Guy06882f82009-06-10 13:36:04 -07007229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007230 Surface createSurfaceLocked() {
7231 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007232 mReportDestroySurface = false;
7233 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007234 mDrawPending = true;
7235 mCommitDrawPending = false;
7236 mReadyToShow = false;
7237 if (mAppToken != null) {
7238 mAppToken.allDrawn = false;
7239 }
7240
7241 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007242 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007243 flags |= Surface.PUSH_BUFFERS;
7244 }
7245
7246 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7247 flags |= Surface.SECURE;
7248 }
7249 if (DEBUG_VISIBILITY) Log.v(
7250 TAG, "Creating surface in session "
7251 + mSession.mSurfaceSession + " window " + this
7252 + " w=" + mFrame.width()
7253 + " h=" + mFrame.height() + " format="
7254 + mAttrs.format + " flags=" + flags);
7255
7256 int w = mFrame.width();
7257 int h = mFrame.height();
7258 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7259 // for a scaled surface, we always want the requested
7260 // size.
7261 w = mRequestedWidth;
7262 h = mRequestedHeight;
7263 }
7264
Romain Guy9825ec62009-10-01 00:58:09 -07007265 // Something is wrong and SurfaceFlinger will not like this,
7266 // try to revert to sane values
7267 if (w <= 0) w = 1;
7268 if (h <= 0) h = 1;
7269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 try {
7271 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007272 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007273 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007274 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7275 + mSurface + " IN SESSION "
7276 + mSession.mSurfaceSession
7277 + ": pid=" + mSession.mPid + " format="
7278 + mAttrs.format + " flags=0x"
7279 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 } catch (Surface.OutOfResourcesException e) {
7281 Log.w(TAG, "OutOfResourcesException creating surface");
7282 reclaimSomeSurfaceMemoryLocked(this, "create");
7283 return null;
7284 } catch (Exception e) {
7285 Log.e(TAG, "Exception creating surface", e);
7286 return null;
7287 }
Romain Guy06882f82009-06-10 13:36:04 -07007288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007289 if (localLOGV) Log.v(
7290 TAG, "Got surface: " + mSurface
7291 + ", set left=" + mFrame.left + " top=" + mFrame.top
7292 + ", animLayer=" + mAnimLayer);
7293 if (SHOW_TRANSACTIONS) {
7294 Log.i(TAG, ">>> OPEN TRANSACTION");
7295 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7296 + mAttrs.getTitle() + ") pos=(" +
7297 mFrame.left + "," + mFrame.top + ") (" +
7298 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7299 mAnimLayer + " HIDE");
7300 }
7301 Surface.openTransaction();
7302 try {
7303 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007304 mSurface.setPosition(mFrame.left + mXOffset,
7305 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007306 mSurface.setLayer(mAnimLayer);
7307 mSurface.hide();
7308 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007309 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7310 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 mSurface.setFlags(Surface.SURFACE_DITHER,
7312 Surface.SURFACE_DITHER);
7313 }
7314 } catch (RuntimeException e) {
7315 Log.w(TAG, "Error creating surface in " + w, e);
7316 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7317 }
7318 mLastHidden = true;
7319 } finally {
7320 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7321 Surface.closeTransaction();
7322 }
7323 if (localLOGV) Log.v(
7324 TAG, "Created surface " + this);
7325 }
7326 return mSurface;
7327 }
Romain Guy06882f82009-06-10 13:36:04 -07007328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 void destroySurfaceLocked() {
7330 // Window is no longer on-screen, so can no longer receive
7331 // key events... if we were waiting for it to finish
7332 // handling a key event, the wait is over!
7333 mKeyWaiter.finishedKey(mSession, mClient, true,
7334 KeyWaiter.RETURN_NOTHING);
7335 mKeyWaiter.releasePendingPointerLocked(mSession);
7336 mKeyWaiter.releasePendingTrackballLocked(mSession);
7337
7338 if (mAppToken != null && this == mAppToken.startingWindow) {
7339 mAppToken.startingDisplayed = false;
7340 }
Romain Guy06882f82009-06-10 13:36:04 -07007341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007342 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007343 mDrawPending = false;
7344 mCommitDrawPending = false;
7345 mReadyToShow = false;
7346
7347 int i = mChildWindows.size();
7348 while (i > 0) {
7349 i--;
7350 WindowState c = (WindowState)mChildWindows.get(i);
7351 c.mAttachedHidden = true;
7352 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007353
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007354 if (mReportDestroySurface) {
7355 mReportDestroySurface = false;
7356 mSurfacePendingDestroy = true;
7357 try {
7358 mClient.dispatchGetNewSurface();
7359 // We'll really destroy on the next time around.
7360 return;
7361 } catch (RemoteException e) {
7362 }
7363 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007366 if (DEBUG_VISIBILITY) {
7367 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007368 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007369 Log.w(TAG, "Window " + this + " destroying surface "
7370 + mSurface + ", session " + mSession, e);
7371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007372 if (SHOW_TRANSACTIONS) {
7373 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007374 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7376 + mAttrs.getTitle() + ")", ex);
7377 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007378 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007379 } catch (RuntimeException e) {
7380 Log.w(TAG, "Exception thrown when destroying Window " + this
7381 + " surface " + mSurface + " session " + mSession
7382 + ": " + e.toString());
7383 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 }
7387 }
7388
7389 boolean finishDrawingLocked() {
7390 if (mDrawPending) {
7391 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7392 TAG, "finishDrawingLocked: " + mSurface);
7393 mCommitDrawPending = true;
7394 mDrawPending = false;
7395 return true;
7396 }
7397 return false;
7398 }
7399
7400 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007401 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7403 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007404 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007405 }
7406 mCommitDrawPending = false;
7407 mReadyToShow = true;
7408 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7409 final AppWindowToken atoken = mAppToken;
7410 if (atoken == null || atoken.allDrawn || starting) {
7411 performShowLocked();
7412 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007413 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 }
7415
7416 // This must be called while inside a transaction.
7417 boolean performShowLocked() {
7418 if (DEBUG_VISIBILITY) {
7419 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007420 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007421 Log.v(TAG, "performShow on " + this
7422 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7423 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7424 }
7425 if (mReadyToShow && isReadyForDisplay()) {
7426 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7427 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7428 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7429 + " during animation: policyVis=" + mPolicyVisibility
7430 + " attHidden=" + mAttachedHidden
7431 + " tok.hiddenRequested="
7432 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007433 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007434 + (mAppToken != null ? mAppToken.hidden : false)
7435 + " animating=" + mAnimating
7436 + " tok animating="
7437 + (mAppToken != null ? mAppToken.animating : false));
7438 if (!showSurfaceRobustlyLocked(this)) {
7439 return false;
7440 }
7441 mLastAlpha = -1;
7442 mHasDrawn = true;
7443 mLastHidden = false;
7444 mReadyToShow = false;
7445 enableScreenIfNeededLocked();
7446
7447 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007449 int i = mChildWindows.size();
7450 while (i > 0) {
7451 i--;
7452 WindowState c = (WindowState)mChildWindows.get(i);
7453 if (c.mSurface != null && c.mAttachedHidden) {
7454 c.mAttachedHidden = false;
7455 c.performShowLocked();
Dianne Hackborn9b52a212009-12-11 14:51:35 -08007456 // It hadn't been shown, which means layout not
7457 // performed on it, so now we want to make sure to
7458 // do a layout. If called from within the transaction
7459 // loop, this will cause it to restart with a new
7460 // layout.
7461 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 }
7463 }
Romain Guy06882f82009-06-10 13:36:04 -07007464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 if (mAttrs.type != TYPE_APPLICATION_STARTING
7466 && mAppToken != null) {
7467 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007468
Dianne Hackborn248b1882009-09-16 16:46:44 -07007469 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007470 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7471 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007472 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007473 // If this initial window is animating, stop it -- we
7474 // will do an animation to reveal it from behind the
7475 // starting window, so there is no need for it to also
7476 // be doing its own stuff.
7477 if (mAnimation != null) {
7478 mAnimation = null;
7479 // Make sure we clean up the animation.
7480 mAnimating = true;
7481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 mFinishedStarting.add(mAppToken);
7483 mH.sendEmptyMessage(H.FINISHED_STARTING);
7484 }
7485 mAppToken.updateReportedVisibilityLocked();
7486 }
7487 }
7488 return true;
7489 }
Romain Guy06882f82009-06-10 13:36:04 -07007490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007491 // This must be called while inside a transaction. Returns true if
7492 // there is more animation to run.
7493 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7494 if (!mDisplayFrozen) {
7495 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007497 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7498 mHasTransformation = true;
7499 mHasLocalTransformation = true;
7500 if (!mLocalAnimating) {
7501 if (DEBUG_ANIM) Log.v(
7502 TAG, "Starting animation in " + this +
7503 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7504 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7505 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7506 mAnimation.setStartTime(currentTime);
7507 mLocalAnimating = true;
7508 mAnimating = true;
7509 }
7510 mTransformation.clear();
7511 final boolean more = mAnimation.getTransformation(
7512 currentTime, mTransformation);
7513 if (DEBUG_ANIM) Log.v(
7514 TAG, "Stepped animation in " + this +
7515 ": more=" + more + ", xform=" + mTransformation);
7516 if (more) {
7517 // we're not done!
7518 return true;
7519 }
7520 if (DEBUG_ANIM) Log.v(
7521 TAG, "Finished animation in " + this +
7522 " @ " + currentTime);
7523 mAnimation = null;
7524 //WindowManagerService.this.dump();
7525 }
7526 mHasLocalTransformation = false;
7527 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007528 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007529 // When our app token is animating, we kind-of pretend like
7530 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7531 // part of this check means that we will only do this if
7532 // our window is not currently exiting, or it is not
7533 // locally animating itself. The idea being that one that
7534 // is exiting and doing a local animation should be removed
7535 // once that animation is done.
7536 mAnimating = true;
7537 mHasTransformation = true;
7538 mTransformation.clear();
7539 return false;
7540 } else if (mHasTransformation) {
7541 // Little trick to get through the path below to act like
7542 // we have finished an animation.
7543 mAnimating = true;
7544 } else if (isAnimating()) {
7545 mAnimating = true;
7546 }
7547 } else if (mAnimation != null) {
7548 // If the display is frozen, and there is a pending animation,
7549 // clear it and make sure we run the cleanup code.
7550 mAnimating = true;
7551 mLocalAnimating = true;
7552 mAnimation = null;
7553 }
Romain Guy06882f82009-06-10 13:36:04 -07007554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007555 if (!mAnimating && !mLocalAnimating) {
7556 return false;
7557 }
7558
7559 if (DEBUG_ANIM) Log.v(
7560 TAG, "Animation done in " + this + ": exiting=" + mExiting
7561 + ", reportedVisible="
7562 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007564 mAnimating = false;
7565 mLocalAnimating = false;
7566 mAnimation = null;
7567 mAnimLayer = mLayer;
7568 if (mIsImWindow) {
7569 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007570 } else if (mIsWallpaper) {
7571 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007572 }
7573 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7574 + " anim layer: " + mAnimLayer);
7575 mHasTransformation = false;
7576 mHasLocalTransformation = false;
7577 mPolicyVisibility = mPolicyVisibilityAfterAnim;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007578 if (!mPolicyVisibility) {
7579 // Window is no longer visible -- make sure if we were waiting
7580 // for it to be displayed before enabling the display, that
7581 // we allow the display to be enabled now.
7582 enableScreenIfNeededLocked();
7583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007584 mTransformation.clear();
7585 if (mHasDrawn
7586 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7587 && mAppToken != null
7588 && mAppToken.firstWindowDrawn
7589 && mAppToken.startingData != null) {
7590 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7591 + mToken + ": first real window done animating");
7592 mFinishedStarting.add(mAppToken);
7593 mH.sendEmptyMessage(H.FINISHED_STARTING);
7594 }
Romain Guy06882f82009-06-10 13:36:04 -07007595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007596 finishExit();
7597
7598 if (mAppToken != null) {
7599 mAppToken.updateReportedVisibilityLocked();
7600 }
7601
7602 return false;
7603 }
7604
7605 void finishExit() {
7606 if (DEBUG_ANIM) Log.v(
7607 TAG, "finishExit in " + this
7608 + ": exiting=" + mExiting
7609 + " remove=" + mRemoveOnExit
7610 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 final int N = mChildWindows.size();
7613 for (int i=0; i<N; i++) {
7614 ((WindowState)mChildWindows.get(i)).finishExit();
7615 }
Romain Guy06882f82009-06-10 13:36:04 -07007616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007617 if (!mExiting) {
7618 return;
7619 }
Romain Guy06882f82009-06-10 13:36:04 -07007620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007621 if (isWindowAnimating()) {
7622 return;
7623 }
7624
7625 if (localLOGV) Log.v(
7626 TAG, "Exit animation finished in " + this
7627 + ": remove=" + mRemoveOnExit);
7628 if (mSurface != null) {
7629 mDestroySurface.add(this);
7630 mDestroying = true;
7631 if (SHOW_TRANSACTIONS) Log.i(
7632 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7633 try {
7634 mSurface.hide();
7635 } catch (RuntimeException e) {
7636 Log.w(TAG, "Error hiding surface in " + this, e);
7637 }
7638 mLastHidden = true;
7639 mKeyWaiter.releasePendingPointerLocked(mSession);
7640 }
7641 mExiting = false;
7642 if (mRemoveOnExit) {
7643 mPendingRemove.add(this);
7644 mRemoveOnExit = false;
7645 }
7646 }
Romain Guy06882f82009-06-10 13:36:04 -07007647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7649 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7650 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7651 if (dtdx < -.000001f || dtdx > .000001f) return false;
7652 if (dsdy < -.000001f || dsdy > .000001f) return false;
7653 return true;
7654 }
Romain Guy06882f82009-06-10 13:36:04 -07007655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007656 void computeShownFrameLocked() {
7657 final boolean selfTransformation = mHasLocalTransformation;
7658 Transformation attachedTransformation =
7659 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7660 ? mAttachedWindow.mTransformation : null;
7661 Transformation appTransformation =
7662 (mAppToken != null && mAppToken.hasTransformation)
7663 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007664
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007665 // Wallpapers are animated based on the "real" window they
7666 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007667 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007668 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007669 if (mWallpaperTarget.mHasLocalTransformation &&
7670 mWallpaperTarget.mAnimation != null &&
7671 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007672 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007673 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7674 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7675 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007676 }
7677 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007678 mWallpaperTarget.mAppToken.hasTransformation &&
7679 mWallpaperTarget.mAppToken.animation != null &&
7680 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007681 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007682 if (DEBUG_WALLPAPER && appTransformation != null) {
7683 Log.v(TAG, "WP target app xform: " + appTransformation);
7684 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007685 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007686 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007688 if (selfTransformation || attachedTransformation != null
7689 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007690 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007691 final Rect frame = mFrame;
7692 final float tmpFloats[] = mTmpFloats;
7693 final Matrix tmpMatrix = mTmpMatrix;
7694
7695 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007696 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007698 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007700 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007701 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007702 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007703 }
7704 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007705 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007706 }
7707
7708 // "convert" it into SurfaceFlinger's format
7709 // (a 2x2 matrix + an offset)
7710 // Here we must not transform the position of the surface
7711 // since it is already included in the transformation.
7712 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007714 tmpMatrix.getValues(tmpFloats);
7715 mDsDx = tmpFloats[Matrix.MSCALE_X];
7716 mDtDx = tmpFloats[Matrix.MSKEW_X];
7717 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7718 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007719 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7720 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007721 int w = frame.width();
7722 int h = frame.height();
7723 mShownFrame.set(x, y, x+w, y+h);
7724
7725 // Now set the alpha... but because our current hardware
7726 // can't do alpha transformation on a non-opaque surface,
7727 // turn it off if we are running an animation that is also
7728 // transforming since it is more important to have that
7729 // animation be smooth.
7730 mShownAlpha = mAlpha;
7731 if (!mLimitedAlphaCompositing
7732 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7733 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7734 && x == frame.left && y == frame.top))) {
7735 //Log.i(TAG, "Applying alpha transform");
7736 if (selfTransformation) {
7737 mShownAlpha *= mTransformation.getAlpha();
7738 }
7739 if (attachedTransformation != null) {
7740 mShownAlpha *= attachedTransformation.getAlpha();
7741 }
7742 if (appTransformation != null) {
7743 mShownAlpha *= appTransformation.getAlpha();
7744 }
7745 } else {
7746 //Log.i(TAG, "Not applying alpha transform");
7747 }
Romain Guy06882f82009-06-10 13:36:04 -07007748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007749 if (localLOGV) Log.v(
7750 TAG, "Continuing animation in " + this +
7751 ": " + mShownFrame +
7752 ", alpha=" + mTransformation.getAlpha());
7753 return;
7754 }
Romain Guy06882f82009-06-10 13:36:04 -07007755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007756 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007757 if (mXOffset != 0 || mYOffset != 0) {
7758 mShownFrame.offset(mXOffset, mYOffset);
7759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007760 mShownAlpha = mAlpha;
7761 mDsDx = 1;
7762 mDtDx = 0;
7763 mDsDy = 0;
7764 mDtDy = 1;
7765 }
Romain Guy06882f82009-06-10 13:36:04 -07007766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007767 /**
7768 * Is this window visible? It is not visible if there is no
7769 * surface, or we are in the process of running an exit animation
7770 * that will remove the surface, or its app token has been hidden.
7771 */
7772 public boolean isVisibleLw() {
7773 final AppWindowToken atoken = mAppToken;
7774 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7775 && (atoken == null || !atoken.hiddenRequested)
7776 && !mExiting && !mDestroying;
7777 }
7778
7779 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007780 * Like {@link #isVisibleLw}, but also counts a window that is currently
7781 * "hidden" behind the keyguard as visible. This allows us to apply
7782 * things like window flags that impact the keyguard.
7783 * XXX I am starting to think we need to have ANOTHER visibility flag
7784 * for this "hidden behind keyguard" state rather than overloading
7785 * mPolicyVisibility. Ungh.
7786 */
7787 public boolean isVisibleOrBehindKeyguardLw() {
7788 final AppWindowToken atoken = mAppToken;
7789 return mSurface != null && !mAttachedHidden
7790 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7791 && !mExiting && !mDestroying;
7792 }
7793
7794 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 * Is this window visible, ignoring its app token? It is not visible
7796 * if there is no surface, or we are in the process of running an exit animation
7797 * that will remove the surface.
7798 */
7799 public boolean isWinVisibleLw() {
7800 final AppWindowToken atoken = mAppToken;
7801 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7802 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7803 && !mExiting && !mDestroying;
7804 }
7805
7806 /**
7807 * The same as isVisible(), but follows the current hidden state of
7808 * the associated app token, not the pending requested hidden state.
7809 */
7810 boolean isVisibleNow() {
7811 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007812 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 }
7814
7815 /**
7816 * Same as isVisible(), but we also count it as visible between the
7817 * call to IWindowSession.add() and the first relayout().
7818 */
7819 boolean isVisibleOrAdding() {
7820 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007821 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7823 && mPolicyVisibility && !mAttachedHidden
7824 && (atoken == null || !atoken.hiddenRequested)
7825 && !mExiting && !mDestroying;
7826 }
7827
7828 /**
7829 * Is this window currently on-screen? It is on-screen either if it
7830 * is visible or it is currently running an animation before no longer
7831 * being visible.
7832 */
7833 boolean isOnScreen() {
7834 final AppWindowToken atoken = mAppToken;
7835 if (atoken != null) {
7836 return mSurface != null && mPolicyVisibility && !mDestroying
7837 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007838 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 } else {
7840 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007841 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007842 }
7843 }
Romain Guy06882f82009-06-10 13:36:04 -07007844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 /**
7846 * Like isOnScreen(), but we don't return true if the window is part
7847 * of a transition that has not yet been started.
7848 */
7849 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007850 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007851 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007852 return false;
7853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007854 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007855 final boolean animating = atoken != null
7856 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007857 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007858 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7859 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007860 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007861 }
7862
7863 /** Is the window or its container currently animating? */
7864 boolean isAnimating() {
7865 final WindowState attached = mAttachedWindow;
7866 final AppWindowToken atoken = mAppToken;
7867 return mAnimation != null
7868 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007869 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007870 (atoken.animation != null
7871 || atoken.inPendingTransaction));
7872 }
7873
7874 /** Is this window currently animating? */
7875 boolean isWindowAnimating() {
7876 return mAnimation != null;
7877 }
7878
7879 /**
7880 * Like isOnScreen, but returns false if the surface hasn't yet
7881 * been drawn.
7882 */
7883 public boolean isDisplayedLw() {
7884 final AppWindowToken atoken = mAppToken;
7885 return mSurface != null && mPolicyVisibility && !mDestroying
7886 && !mDrawPending && !mCommitDrawPending
7887 && ((!mAttachedHidden &&
7888 (atoken == null || !atoken.hiddenRequested))
7889 || mAnimating);
7890 }
7891
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007892 /**
7893 * Returns true if the window has a surface that it has drawn a
7894 * complete UI in to.
7895 */
7896 public boolean isDrawnLw() {
7897 final AppWindowToken atoken = mAppToken;
7898 return mSurface != null && !mDestroying
7899 && !mDrawPending && !mCommitDrawPending;
7900 }
7901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7903 boolean shownFrame, boolean onlyOpaque) {
7904 if (mSurface == null) {
7905 return false;
7906 }
7907 if (mAppToken != null && !mAppToken.appFullscreen) {
7908 return false;
7909 }
7910 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7911 return false;
7912 }
7913 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007914
7915 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7916 return frame.left <= mCompatibleScreenFrame.left &&
7917 frame.top <= mCompatibleScreenFrame.top &&
7918 frame.right >= mCompatibleScreenFrame.right &&
7919 frame.bottom >= mCompatibleScreenFrame.bottom;
7920 } else {
7921 return frame.left <= 0 && frame.top <= 0
7922 && frame.right >= screenWidth
7923 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 }
Romain Guy06882f82009-06-10 13:36:04 -07007926
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007927 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007928 * Return true if the window is opaque and fully drawn. This indicates
7929 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007930 */
7931 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007932 return (mAttrs.format == PixelFormat.OPAQUE
7933 || mAttrs.type == TYPE_WALLPAPER)
7934 && mSurface != null && mAnimation == null
7935 && (mAppToken == null || mAppToken.animation == null)
7936 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007937 }
7938
7939 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7940 return
7941 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007942 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7943 // only if it's visible
7944 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007945 // and only if the application fills the compatible screen
7946 mFrame.left <= mCompatibleScreenFrame.left &&
7947 mFrame.top <= mCompatibleScreenFrame.top &&
7948 mFrame.right >= mCompatibleScreenFrame.right &&
7949 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007950 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007951 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007952 }
7953
7954 boolean isFullscreen(int screenWidth, int screenHeight) {
7955 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007956 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007957 }
7958
7959 void removeLocked() {
7960 if (mAttachedWindow != null) {
7961 mAttachedWindow.mChildWindows.remove(this);
7962 }
7963 destroySurfaceLocked();
7964 mSession.windowRemovedLocked();
7965 try {
7966 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7967 } catch (RuntimeException e) {
7968 // Ignore if it has already been removed (usually because
7969 // we are doing this as part of processing a death note.)
7970 }
7971 }
7972
7973 private class DeathRecipient implements IBinder.DeathRecipient {
7974 public void binderDied() {
7975 try {
7976 synchronized(mWindowMap) {
7977 WindowState win = windowForClientLocked(mSession, mClient);
7978 Log.i(TAG, "WIN DEATH: " + win);
7979 if (win != null) {
7980 removeWindowLocked(mSession, win);
7981 }
7982 }
7983 } catch (IllegalArgumentException ex) {
7984 // This will happen if the window has already been
7985 // removed.
7986 }
7987 }
7988 }
7989
7990 /** Returns true if this window desires key events. */
7991 public final boolean canReceiveKeys() {
7992 return isVisibleOrAdding()
7993 && (mViewVisibility == View.VISIBLE)
7994 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7995 }
7996
7997 public boolean hasDrawnLw() {
7998 return mHasDrawn;
7999 }
8000
8001 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008002 return showLw(doAnimation, true);
8003 }
8004
8005 boolean showLw(boolean doAnimation, boolean requestAnim) {
8006 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8007 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008009 mPolicyVisibility = true;
8010 mPolicyVisibilityAfterAnim = true;
8011 if (doAnimation) {
8012 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8013 }
8014 if (requestAnim) {
8015 requestAnimationLocked(0);
8016 }
8017 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008018 }
8019
8020 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008021 return hideLw(doAnimation, true);
8022 }
8023
8024 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8026 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008027 if (!current) {
8028 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008030 if (doAnimation) {
8031 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8032 if (mAnimation == null) {
8033 doAnimation = false;
8034 }
8035 }
8036 if (doAnimation) {
8037 mPolicyVisibilityAfterAnim = false;
8038 } else {
8039 mPolicyVisibilityAfterAnim = false;
8040 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008041 // Window is no longer visible -- make sure if we were waiting
8042 // for it to be displayed before enabling the display, that
8043 // we allow the display to be enabled now.
8044 enableScreenIfNeededLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008045 }
8046 if (requestAnim) {
8047 requestAnimationLocked(0);
8048 }
8049 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008050 }
8051
8052 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008053 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07008054
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008055 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8056 pw.print(" mClient="); pw.println(mClient.asBinder());
8057 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8058 if (mAttachedWindow != null || mLayoutAttached) {
8059 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8060 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8061 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008062 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8063 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8064 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008065 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8066 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008067 }
8068 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8069 pw.print(" mSubLayer="); pw.print(mSubLayer);
8070 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8071 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8072 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8073 pw.print("="); pw.print(mAnimLayer);
8074 pw.print(" mLastLayer="); pw.println(mLastLayer);
8075 if (mSurface != null) {
8076 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8077 }
8078 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8079 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8080 if (mAppToken != null) {
8081 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8082 }
8083 if (mTargetAppToken != null) {
8084 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8085 }
8086 pw.print(prefix); pw.print("mViewVisibility=0x");
8087 pw.print(Integer.toHexString(mViewVisibility));
8088 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008089 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8090 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008091 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8092 pw.print(prefix); pw.print("mPolicyVisibility=");
8093 pw.print(mPolicyVisibility);
8094 pw.print(" mPolicyVisibilityAfterAnim=");
8095 pw.print(mPolicyVisibilityAfterAnim);
8096 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8097 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008098 if (!mRelayoutCalled) {
8099 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8100 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008101 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008102 pw.print(" h="); pw.println(mRequestedHeight);
8103 if (mXOffset != 0 || mYOffset != 0) {
8104 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8105 pw.print(" y="); pw.println(mYOffset);
8106 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008107 pw.print(prefix); pw.print("mGivenContentInsets=");
8108 mGivenContentInsets.printShortString(pw);
8109 pw.print(" mGivenVisibleInsets=");
8110 mGivenVisibleInsets.printShortString(pw);
8111 pw.println();
8112 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8113 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8114 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8115 }
8116 pw.print(prefix); pw.print("mShownFrame=");
8117 mShownFrame.printShortString(pw);
8118 pw.print(" last="); mLastShownFrame.printShortString(pw);
8119 pw.println();
8120 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8121 pw.print(" last="); mLastFrame.printShortString(pw);
8122 pw.println();
8123 pw.print(prefix); pw.print("mContainingFrame=");
8124 mContainingFrame.printShortString(pw);
8125 pw.print(" mDisplayFrame=");
8126 mDisplayFrame.printShortString(pw);
8127 pw.println();
8128 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8129 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8130 pw.println();
8131 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8132 pw.print(" last="); mLastContentInsets.printShortString(pw);
8133 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8134 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8135 pw.println();
8136 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8137 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8138 pw.print(" mAlpha="); pw.print(mAlpha);
8139 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8140 }
8141 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8142 || mAnimation != null) {
8143 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8144 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8145 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8146 pw.print(" mAnimation="); pw.println(mAnimation);
8147 }
8148 if (mHasTransformation || mHasLocalTransformation) {
8149 pw.print(prefix); pw.print("XForm: has=");
8150 pw.print(mHasTransformation);
8151 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8152 pw.print(" "); mTransformation.printShortString(pw);
8153 pw.println();
8154 }
8155 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8156 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8157 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8158 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8159 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8160 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8161 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8162 pw.print(" mDestroying="); pw.print(mDestroying);
8163 pw.print(" mRemoved="); pw.println(mRemoved);
8164 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008165 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008166 pw.print(prefix); pw.print("mOrientationChanging=");
8167 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008168 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8169 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008170 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008171 if (mHScale != 1 || mVScale != 1) {
8172 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8173 pw.print(" mVScale="); pw.println(mVScale);
8174 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008175 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008176 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8177 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8178 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008179 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8180 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8181 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008183 }
8184
8185 @Override
8186 public String toString() {
8187 return "Window{"
8188 + Integer.toHexString(System.identityHashCode(this))
8189 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8190 }
8191 }
Romain Guy06882f82009-06-10 13:36:04 -07008192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008193 // -------------------------------------------------------------
8194 // Window Token State
8195 // -------------------------------------------------------------
8196
8197 class WindowToken {
8198 // The actual token.
8199 final IBinder token;
8200
8201 // The type of window this token is for, as per WindowManager.LayoutParams.
8202 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008204 // Set if this token was explicitly added by a client, so should
8205 // not be removed when all windows are removed.
8206 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008207
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008208 // For printing.
8209 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211 // If this is an AppWindowToken, this is non-null.
8212 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214 // All of the windows associated with this token.
8215 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8216
8217 // Is key dispatching paused for this token?
8218 boolean paused = false;
8219
8220 // Should this token's windows be hidden?
8221 boolean hidden;
8222
8223 // Temporary for finding which tokens no longer have visible windows.
8224 boolean hasVisible;
8225
Dianne Hackborna8f60182009-09-01 19:01:50 -07008226 // Set to true when this token is in a pending transaction where it
8227 // will be shown.
8228 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008229
Dianne Hackborna8f60182009-09-01 19:01:50 -07008230 // Set to true when this token is in a pending transaction where it
8231 // will be hidden.
8232 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008233
Dianne Hackborna8f60182009-09-01 19:01:50 -07008234 // Set to true when this token is in a pending transaction where its
8235 // windows will be put to the bottom of the list.
8236 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008237
Dianne Hackborna8f60182009-09-01 19:01:50 -07008238 // Set to true when this token is in a pending transaction where its
8239 // windows will be put to the top of the list.
8240 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008242 WindowToken(IBinder _token, int type, boolean _explicit) {
8243 token = _token;
8244 windowType = type;
8245 explicit = _explicit;
8246 }
8247
8248 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008249 pw.print(prefix); pw.print("token="); pw.println(token);
8250 pw.print(prefix); pw.print("windows="); pw.println(windows);
8251 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8252 pw.print(" hidden="); pw.print(hidden);
8253 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008254 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8255 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8256 pw.print(" waitingToHide="); pw.print(waitingToHide);
8257 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8258 pw.print(" sendingToTop="); pw.println(sendingToTop);
8259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008260 }
8261
8262 @Override
8263 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008264 if (stringName == null) {
8265 StringBuilder sb = new StringBuilder();
8266 sb.append("WindowToken{");
8267 sb.append(Integer.toHexString(System.identityHashCode(this)));
8268 sb.append(" token="); sb.append(token); sb.append('}');
8269 stringName = sb.toString();
8270 }
8271 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008272 }
8273 };
8274
8275 class AppWindowToken extends WindowToken {
8276 // Non-null only for application tokens.
8277 final IApplicationToken appToken;
8278
8279 // All of the windows and child windows that are included in this
8280 // application token. Note this list is NOT sorted!
8281 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8282
8283 int groupId = -1;
8284 boolean appFullscreen;
8285 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008287 // These are used for determining when all windows associated with
8288 // an activity have been drawn, so they can be made visible together
8289 // at the same time.
8290 int lastTransactionSequence = mTransactionSequence-1;
8291 int numInterestingWindows;
8292 int numDrawnWindows;
8293 boolean inPendingTransaction;
8294 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008296 // Is this token going to be hidden in a little while? If so, it
8297 // won't be taken into account for setting the screen orientation.
8298 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008300 // Is this window's surface needed? This is almost like hidden, except
8301 // it will sometimes be true a little earlier: when the token has
8302 // been shown, but is still waiting for its app transition to execute
8303 // before making its windows shown.
8304 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008306 // Have we told the window clients to hide themselves?
8307 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008309 // Last visibility state we reported to the app token.
8310 boolean reportedVisible;
8311
8312 // Set to true when the token has been removed from the window mgr.
8313 boolean removed;
8314
8315 // Have we been asked to have this token keep the screen frozen?
8316 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008318 boolean animating;
8319 Animation animation;
8320 boolean hasTransformation;
8321 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 // Offset to the window of all layers in the token, for use by
8324 // AppWindowToken animations.
8325 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008327 // Information about an application starting window if displayed.
8328 StartingData startingData;
8329 WindowState startingWindow;
8330 View startingView;
8331 boolean startingDisplayed;
8332 boolean startingMoved;
8333 boolean firstWindowDrawn;
8334
8335 AppWindowToken(IApplicationToken _token) {
8336 super(_token.asBinder(),
8337 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8338 appWindowToken = this;
8339 appToken = _token;
8340 }
Romain Guy06882f82009-06-10 13:36:04 -07008341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008342 public void setAnimation(Animation anim) {
8343 if (localLOGV) Log.v(
8344 TAG, "Setting animation in " + this + ": " + anim);
8345 animation = anim;
8346 animating = false;
8347 anim.restrictDuration(MAX_ANIMATION_DURATION);
8348 anim.scaleCurrentDuration(mTransitionAnimationScale);
8349 int zorder = anim.getZAdjustment();
8350 int adj = 0;
8351 if (zorder == Animation.ZORDER_TOP) {
8352 adj = TYPE_LAYER_OFFSET;
8353 } else if (zorder == Animation.ZORDER_BOTTOM) {
8354 adj = -TYPE_LAYER_OFFSET;
8355 }
Romain Guy06882f82009-06-10 13:36:04 -07008356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 if (animLayerAdjustment != adj) {
8358 animLayerAdjustment = adj;
8359 updateLayers();
8360 }
8361 }
Romain Guy06882f82009-06-10 13:36:04 -07008362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008363 public void setDummyAnimation() {
8364 if (animation == null) {
8365 if (localLOGV) Log.v(
8366 TAG, "Setting dummy animation in " + this);
8367 animation = sDummyAnimation;
8368 }
8369 }
8370
8371 public void clearAnimation() {
8372 if (animation != null) {
8373 animation = null;
8374 animating = true;
8375 }
8376 }
Romain Guy06882f82009-06-10 13:36:04 -07008377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 void updateLayers() {
8379 final int N = allAppWindows.size();
8380 final int adj = animLayerAdjustment;
8381 for (int i=0; i<N; i++) {
8382 WindowState w = allAppWindows.get(i);
8383 w.mAnimLayer = w.mLayer + adj;
8384 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8385 + w.mAnimLayer);
8386 if (w == mInputMethodTarget) {
8387 setInputMethodAnimLayerAdjustment(adj);
8388 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008389 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008390 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 }
8393 }
Romain Guy06882f82009-06-10 13:36:04 -07008394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008395 void sendAppVisibilityToClients() {
8396 final int N = allAppWindows.size();
8397 for (int i=0; i<N; i++) {
8398 WindowState win = allAppWindows.get(i);
8399 if (win == startingWindow && clientHidden) {
8400 // Don't hide the starting window.
8401 continue;
8402 }
8403 try {
8404 if (DEBUG_VISIBILITY) Log.v(TAG,
8405 "Setting visibility of " + win + ": " + (!clientHidden));
8406 win.mClient.dispatchAppVisibility(!clientHidden);
8407 } catch (RemoteException e) {
8408 }
8409 }
8410 }
Romain Guy06882f82009-06-10 13:36:04 -07008411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008412 void showAllWindowsLocked() {
8413 final int NW = allAppWindows.size();
8414 for (int i=0; i<NW; i++) {
8415 WindowState w = allAppWindows.get(i);
8416 if (DEBUG_VISIBILITY) Log.v(TAG,
8417 "performing show on: " + w);
8418 w.performShowLocked();
8419 }
8420 }
Romain Guy06882f82009-06-10 13:36:04 -07008421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008422 // This must be called while inside a transaction.
8423 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
8424 if (!mDisplayFrozen) {
8425 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 if (animation == sDummyAnimation) {
8428 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008429 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 // when it is really time to animate, this will be set to
8431 // a real animation and the next call will execute normally.
8432 return false;
8433 }
Romain Guy06882f82009-06-10 13:36:04 -07008434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008435 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8436 if (!animating) {
8437 if (DEBUG_ANIM) Log.v(
8438 TAG, "Starting animation in " + this +
8439 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8440 + " scale=" + mTransitionAnimationScale
8441 + " allDrawn=" + allDrawn + " animating=" + animating);
8442 animation.initialize(dw, dh, dw, dh);
8443 animation.setStartTime(currentTime);
8444 animating = true;
8445 }
8446 transformation.clear();
8447 final boolean more = animation.getTransformation(
8448 currentTime, transformation);
8449 if (DEBUG_ANIM) Log.v(
8450 TAG, "Stepped animation in " + this +
8451 ": more=" + more + ", xform=" + transformation);
8452 if (more) {
8453 // we're done!
8454 hasTransformation = true;
8455 return true;
8456 }
8457 if (DEBUG_ANIM) Log.v(
8458 TAG, "Finished animation in " + this +
8459 " @ " + currentTime);
8460 animation = null;
8461 }
8462 } else if (animation != null) {
8463 // If the display is frozen, and there is a pending animation,
8464 // clear it and make sure we run the cleanup code.
8465 animating = true;
8466 animation = null;
8467 }
8468
8469 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008471 if (!animating) {
8472 return false;
8473 }
8474
8475 clearAnimation();
8476 animating = false;
8477 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8478 moveInputMethodWindowsIfNeededLocked(true);
8479 }
Romain Guy06882f82009-06-10 13:36:04 -07008480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008481 if (DEBUG_ANIM) Log.v(
8482 TAG, "Animation done in " + this
8483 + ": reportedVisible=" + reportedVisible);
8484
8485 transformation.clear();
8486 if (animLayerAdjustment != 0) {
8487 animLayerAdjustment = 0;
8488 updateLayers();
8489 }
Romain Guy06882f82009-06-10 13:36:04 -07008490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008491 final int N = windows.size();
8492 for (int i=0; i<N; i++) {
8493 ((WindowState)windows.get(i)).finishExit();
8494 }
8495 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008497 return false;
8498 }
8499
8500 void updateReportedVisibilityLocked() {
8501 if (appToken == null) {
8502 return;
8503 }
Romain Guy06882f82009-06-10 13:36:04 -07008504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008505 int numInteresting = 0;
8506 int numVisible = 0;
8507 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8510 final int N = allAppWindows.size();
8511 for (int i=0; i<N; i++) {
8512 WindowState win = allAppWindows.get(i);
8513 if (win == startingWindow || win.mAppFreezing) {
8514 continue;
8515 }
8516 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008517 Log.v(TAG, "Win " + win + ": isDrawn="
8518 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008520 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 Log.v(TAG, "Not displayed: s=" + win.mSurface
8522 + " pv=" + win.mPolicyVisibility
8523 + " dp=" + win.mDrawPending
8524 + " cdp=" + win.mCommitDrawPending
8525 + " ah=" + win.mAttachedHidden
8526 + " th="
8527 + (win.mAppToken != null
8528 ? win.mAppToken.hiddenRequested : false)
8529 + " a=" + win.mAnimating);
8530 }
8531 }
8532 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008533 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008534 if (!win.isAnimating()) {
8535 numVisible++;
8536 }
8537 nowGone = false;
8538 } else if (win.isAnimating()) {
8539 nowGone = false;
8540 }
8541 }
Romain Guy06882f82009-06-10 13:36:04 -07008542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8544 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8545 + numInteresting + " visible=" + numVisible);
8546 if (nowVisible != reportedVisible) {
8547 if (DEBUG_VISIBILITY) Log.v(
8548 TAG, "Visibility changed in " + this
8549 + ": vis=" + nowVisible);
8550 reportedVisible = nowVisible;
8551 Message m = mH.obtainMessage(
8552 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8553 nowVisible ? 1 : 0,
8554 nowGone ? 1 : 0,
8555 this);
8556 mH.sendMessage(m);
8557 }
8558 }
Romain Guy06882f82009-06-10 13:36:04 -07008559
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008560 WindowState findMainWindow() {
8561 int j = windows.size();
8562 while (j > 0) {
8563 j--;
8564 WindowState win = windows.get(j);
8565 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8566 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8567 return win;
8568 }
8569 }
8570 return null;
8571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 void dump(PrintWriter pw, String prefix) {
8574 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008575 if (appToken != null) {
8576 pw.print(prefix); pw.println("app=true");
8577 }
8578 if (allAppWindows.size() > 0) {
8579 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8580 }
8581 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008582 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008583 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8584 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8585 pw.print(" clientHidden="); pw.print(clientHidden);
8586 pw.print(" willBeHidden="); pw.print(willBeHidden);
8587 pw.print(" reportedVisible="); pw.println(reportedVisible);
8588 if (paused || freezingScreen) {
8589 pw.print(prefix); pw.print("paused="); pw.print(paused);
8590 pw.print(" freezingScreen="); pw.println(freezingScreen);
8591 }
8592 if (numInterestingWindows != 0 || numDrawnWindows != 0
8593 || inPendingTransaction || allDrawn) {
8594 pw.print(prefix); pw.print("numInterestingWindows=");
8595 pw.print(numInterestingWindows);
8596 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8597 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8598 pw.print(" allDrawn="); pw.println(allDrawn);
8599 }
8600 if (animating || animation != null) {
8601 pw.print(prefix); pw.print("animating="); pw.print(animating);
8602 pw.print(" animation="); pw.println(animation);
8603 }
8604 if (animLayerAdjustment != 0) {
8605 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8606 }
8607 if (hasTransformation) {
8608 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8609 pw.print(" transformation="); transformation.printShortString(pw);
8610 pw.println();
8611 }
8612 if (startingData != null || removed || firstWindowDrawn) {
8613 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8614 pw.print(" removed="); pw.print(removed);
8615 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8616 }
8617 if (startingWindow != null || startingView != null
8618 || startingDisplayed || startingMoved) {
8619 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8620 pw.print(" startingView="); pw.print(startingView);
8621 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8622 pw.print(" startingMoved"); pw.println(startingMoved);
8623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008624 }
8625
8626 @Override
8627 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008628 if (stringName == null) {
8629 StringBuilder sb = new StringBuilder();
8630 sb.append("AppWindowToken{");
8631 sb.append(Integer.toHexString(System.identityHashCode(this)));
8632 sb.append(" token="); sb.append(token); sb.append('}');
8633 stringName = sb.toString();
8634 }
8635 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008636 }
8637 }
Romain Guy06882f82009-06-10 13:36:04 -07008638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008639 // -------------------------------------------------------------
8640 // DummyAnimation
8641 // -------------------------------------------------------------
8642
8643 // This is an animation that does nothing: it just immediately finishes
8644 // itself every time it is called. It is used as a stub animation in cases
8645 // where we want to synchronize multiple things that may be animating.
8646 static final class DummyAnimation extends Animation {
8647 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8648 return false;
8649 }
8650 }
8651 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008653 // -------------------------------------------------------------
8654 // Async Handler
8655 // -------------------------------------------------------------
8656
8657 static final class StartingData {
8658 final String pkg;
8659 final int theme;
8660 final CharSequence nonLocalizedLabel;
8661 final int labelRes;
8662 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8665 int _labelRes, int _icon) {
8666 pkg = _pkg;
8667 theme = _theme;
8668 nonLocalizedLabel = _nonLocalizedLabel;
8669 labelRes = _labelRes;
8670 icon = _icon;
8671 }
8672 }
8673
8674 private final class H extends Handler {
8675 public static final int REPORT_FOCUS_CHANGE = 2;
8676 public static final int REPORT_LOSING_FOCUS = 3;
8677 public static final int ANIMATE = 4;
8678 public static final int ADD_STARTING = 5;
8679 public static final int REMOVE_STARTING = 6;
8680 public static final int FINISHED_STARTING = 7;
8681 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8683 public static final int HOLD_SCREEN_CHANGED = 12;
8684 public static final int APP_TRANSITION_TIMEOUT = 13;
8685 public static final int PERSIST_ANIMATION_SCALE = 14;
8686 public static final int FORCE_GC = 15;
8687 public static final int ENABLE_SCREEN = 16;
8688 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008689 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008691 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008693 public H() {
8694 }
Romain Guy06882f82009-06-10 13:36:04 -07008695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008696 @Override
8697 public void handleMessage(Message msg) {
8698 switch (msg.what) {
8699 case REPORT_FOCUS_CHANGE: {
8700 WindowState lastFocus;
8701 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008703 synchronized(mWindowMap) {
8704 lastFocus = mLastFocus;
8705 newFocus = mCurrentFocus;
8706 if (lastFocus == newFocus) {
8707 // Focus is not changing, so nothing to do.
8708 return;
8709 }
8710 mLastFocus = newFocus;
8711 //Log.i(TAG, "Focus moving from " + lastFocus
8712 // + " to " + newFocus);
8713 if (newFocus != null && lastFocus != null
8714 && !newFocus.isDisplayedLw()) {
8715 //Log.i(TAG, "Delaying loss of focus...");
8716 mLosingFocus.add(lastFocus);
8717 lastFocus = null;
8718 }
8719 }
8720
8721 if (lastFocus != newFocus) {
8722 //System.out.println("Changing focus from " + lastFocus
8723 // + " to " + newFocus);
8724 if (newFocus != null) {
8725 try {
8726 //Log.i(TAG, "Gaining focus: " + newFocus);
8727 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8728 } catch (RemoteException e) {
8729 // Ignore if process has died.
8730 }
8731 }
8732
8733 if (lastFocus != null) {
8734 try {
8735 //Log.i(TAG, "Losing focus: " + lastFocus);
8736 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8737 } catch (RemoteException e) {
8738 // Ignore if process has died.
8739 }
8740 }
8741 }
8742 } break;
8743
8744 case REPORT_LOSING_FOCUS: {
8745 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008747 synchronized(mWindowMap) {
8748 losers = mLosingFocus;
8749 mLosingFocus = new ArrayList<WindowState>();
8750 }
8751
8752 final int N = losers.size();
8753 for (int i=0; i<N; i++) {
8754 try {
8755 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8756 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8757 } catch (RemoteException e) {
8758 // Ignore if process has died.
8759 }
8760 }
8761 } break;
8762
8763 case ANIMATE: {
8764 synchronized(mWindowMap) {
8765 mAnimationPending = false;
8766 performLayoutAndPlaceSurfacesLocked();
8767 }
8768 } break;
8769
8770 case ADD_STARTING: {
8771 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8772 final StartingData sd = wtoken.startingData;
8773
8774 if (sd == null) {
8775 // Animation has been canceled... do nothing.
8776 return;
8777 }
Romain Guy06882f82009-06-10 13:36:04 -07008778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008779 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8780 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008782 View view = null;
8783 try {
8784 view = mPolicy.addStartingWindow(
8785 wtoken.token, sd.pkg,
8786 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8787 sd.icon);
8788 } catch (Exception e) {
8789 Log.w(TAG, "Exception when adding starting window", e);
8790 }
8791
8792 if (view != null) {
8793 boolean abort = false;
8794
8795 synchronized(mWindowMap) {
8796 if (wtoken.removed || wtoken.startingData == null) {
8797 // If the window was successfully added, then
8798 // we need to remove it.
8799 if (wtoken.startingWindow != null) {
8800 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8801 "Aborted starting " + wtoken
8802 + ": removed=" + wtoken.removed
8803 + " startingData=" + wtoken.startingData);
8804 wtoken.startingWindow = null;
8805 wtoken.startingData = null;
8806 abort = true;
8807 }
8808 } else {
8809 wtoken.startingView = view;
8810 }
8811 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8812 "Added starting " + wtoken
8813 + ": startingWindow="
8814 + wtoken.startingWindow + " startingView="
8815 + wtoken.startingView);
8816 }
8817
8818 if (abort) {
8819 try {
8820 mPolicy.removeStartingWindow(wtoken.token, view);
8821 } catch (Exception e) {
8822 Log.w(TAG, "Exception when removing starting window", e);
8823 }
8824 }
8825 }
8826 } break;
8827
8828 case REMOVE_STARTING: {
8829 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8830 IBinder token = null;
8831 View view = null;
8832 synchronized (mWindowMap) {
8833 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8834 + wtoken + ": startingWindow="
8835 + wtoken.startingWindow + " startingView="
8836 + wtoken.startingView);
8837 if (wtoken.startingWindow != null) {
8838 view = wtoken.startingView;
8839 token = wtoken.token;
8840 wtoken.startingData = null;
8841 wtoken.startingView = null;
8842 wtoken.startingWindow = null;
8843 }
8844 }
8845 if (view != null) {
8846 try {
8847 mPolicy.removeStartingWindow(token, view);
8848 } catch (Exception e) {
8849 Log.w(TAG, "Exception when removing starting window", e);
8850 }
8851 }
8852 } break;
8853
8854 case FINISHED_STARTING: {
8855 IBinder token = null;
8856 View view = null;
8857 while (true) {
8858 synchronized (mWindowMap) {
8859 final int N = mFinishedStarting.size();
8860 if (N <= 0) {
8861 break;
8862 }
8863 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8864
8865 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8866 "Finished starting " + wtoken
8867 + ": startingWindow=" + wtoken.startingWindow
8868 + " startingView=" + wtoken.startingView);
8869
8870 if (wtoken.startingWindow == null) {
8871 continue;
8872 }
8873
8874 view = wtoken.startingView;
8875 token = wtoken.token;
8876 wtoken.startingData = null;
8877 wtoken.startingView = null;
8878 wtoken.startingWindow = null;
8879 }
8880
8881 try {
8882 mPolicy.removeStartingWindow(token, view);
8883 } catch (Exception e) {
8884 Log.w(TAG, "Exception when removing starting window", e);
8885 }
8886 }
8887 } break;
8888
8889 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8890 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8891
8892 boolean nowVisible = msg.arg1 != 0;
8893 boolean nowGone = msg.arg2 != 0;
8894
8895 try {
8896 if (DEBUG_VISIBILITY) Log.v(
8897 TAG, "Reporting visible in " + wtoken
8898 + " visible=" + nowVisible
8899 + " gone=" + nowGone);
8900 if (nowVisible) {
8901 wtoken.appToken.windowsVisible();
8902 } else {
8903 wtoken.appToken.windowsGone();
8904 }
8905 } catch (RemoteException ex) {
8906 }
8907 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008909 case WINDOW_FREEZE_TIMEOUT: {
8910 synchronized (mWindowMap) {
8911 Log.w(TAG, "Window freeze timeout expired.");
8912 int i = mWindows.size();
8913 while (i > 0) {
8914 i--;
8915 WindowState w = (WindowState)mWindows.get(i);
8916 if (w.mOrientationChanging) {
8917 w.mOrientationChanging = false;
8918 Log.w(TAG, "Force clearing orientation change: " + w);
8919 }
8920 }
8921 performLayoutAndPlaceSurfacesLocked();
8922 }
8923 break;
8924 }
Romain Guy06882f82009-06-10 13:36:04 -07008925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008926 case HOLD_SCREEN_CHANGED: {
8927 Session oldHold;
8928 Session newHold;
8929 synchronized (mWindowMap) {
8930 oldHold = mLastReportedHold;
8931 newHold = (Session)msg.obj;
8932 mLastReportedHold = newHold;
8933 }
Romain Guy06882f82009-06-10 13:36:04 -07008934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008935 if (oldHold != newHold) {
8936 try {
8937 if (oldHold != null) {
8938 mBatteryStats.noteStopWakelock(oldHold.mUid,
8939 "window",
8940 BatteryStats.WAKE_TYPE_WINDOW);
8941 }
8942 if (newHold != null) {
8943 mBatteryStats.noteStartWakelock(newHold.mUid,
8944 "window",
8945 BatteryStats.WAKE_TYPE_WINDOW);
8946 }
8947 } catch (RemoteException e) {
8948 }
8949 }
8950 break;
8951 }
Romain Guy06882f82009-06-10 13:36:04 -07008952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008953 case APP_TRANSITION_TIMEOUT: {
8954 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008955 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008956 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8957 "*** APP TRANSITION TIMEOUT");
8958 mAppTransitionReady = true;
8959 mAppTransitionTimeout = true;
8960 performLayoutAndPlaceSurfacesLocked();
8961 }
8962 }
8963 break;
8964 }
Romain Guy06882f82009-06-10 13:36:04 -07008965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008966 case PERSIST_ANIMATION_SCALE: {
8967 Settings.System.putFloat(mContext.getContentResolver(),
8968 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8969 Settings.System.putFloat(mContext.getContentResolver(),
8970 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8971 break;
8972 }
Romain Guy06882f82009-06-10 13:36:04 -07008973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008974 case FORCE_GC: {
8975 synchronized(mWindowMap) {
8976 if (mAnimationPending) {
8977 // If we are animating, don't do the gc now but
8978 // delay a bit so we don't interrupt the animation.
8979 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8980 2000);
8981 return;
8982 }
8983 // If we are currently rotating the display, it will
8984 // schedule a new message when done.
8985 if (mDisplayFrozen) {
8986 return;
8987 }
8988 mFreezeGcPending = 0;
8989 }
8990 Runtime.getRuntime().gc();
8991 break;
8992 }
Romain Guy06882f82009-06-10 13:36:04 -07008993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008994 case ENABLE_SCREEN: {
8995 performEnableScreen();
8996 break;
8997 }
Romain Guy06882f82009-06-10 13:36:04 -07008998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008999 case APP_FREEZE_TIMEOUT: {
9000 synchronized (mWindowMap) {
9001 Log.w(TAG, "App freeze timeout expired.");
9002 int i = mAppTokens.size();
9003 while (i > 0) {
9004 i--;
9005 AppWindowToken tok = mAppTokens.get(i);
9006 if (tok.freezingScreen) {
9007 Log.w(TAG, "Force clearing freeze: " + tok);
9008 unsetAppFreezingScreenLocked(tok, true, true);
9009 }
9010 }
9011 }
9012 break;
9013 }
Romain Guy06882f82009-06-10 13:36:04 -07009014
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009015 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07009016 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009017 sendNewConfiguration();
9018 }
9019 break;
9020 }
Romain Guy06882f82009-06-10 13:36:04 -07009021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009022 }
9023 }
9024 }
9025
9026 // -------------------------------------------------------------
9027 // IWindowManager API
9028 // -------------------------------------------------------------
9029
9030 public IWindowSession openSession(IInputMethodClient client,
9031 IInputContext inputContext) {
9032 if (client == null) throw new IllegalArgumentException("null client");
9033 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9034 return new Session(client, inputContext);
9035 }
9036
9037 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9038 synchronized (mWindowMap) {
9039 // The focus for the client is the window immediately below
9040 // where we would place the input method window.
9041 int idx = findDesiredInputMethodWindowIndexLocked(false);
9042 WindowState imFocus;
9043 if (idx > 0) {
9044 imFocus = (WindowState)mWindows.get(idx-1);
9045 if (imFocus != null) {
9046 if (imFocus.mSession.mClient != null &&
9047 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9048 return true;
9049 }
9050 }
9051 }
9052 }
9053 return false;
9054 }
Romain Guy06882f82009-06-10 13:36:04 -07009055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009056 // -------------------------------------------------------------
9057 // Internals
9058 // -------------------------------------------------------------
9059
9060 final WindowState windowForClientLocked(Session session, IWindow client) {
9061 return windowForClientLocked(session, client.asBinder());
9062 }
Romain Guy06882f82009-06-10 13:36:04 -07009063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009064 final WindowState windowForClientLocked(Session session, IBinder client) {
9065 WindowState win = mWindowMap.get(client);
9066 if (localLOGV) Log.v(
9067 TAG, "Looking up client " + client + ": " + win);
9068 if (win == null) {
9069 RuntimeException ex = new RuntimeException();
9070 Log.w(TAG, "Requested window " + client + " does not exist", ex);
9071 return null;
9072 }
9073 if (session != null && win.mSession != session) {
9074 RuntimeException ex = new RuntimeException();
9075 Log.w(TAG, "Requested window " + client + " is in session " +
9076 win.mSession + ", not " + session, ex);
9077 return null;
9078 }
9079
9080 return win;
9081 }
9082
Dianne Hackborna8f60182009-09-01 19:01:50 -07009083 final void rebuildAppWindowListLocked() {
9084 int NW = mWindows.size();
9085 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009086 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009087 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009088
Dianne Hackborna8f60182009-09-01 19:01:50 -07009089 // First remove all existing app windows.
9090 i=0;
9091 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009092 WindowState w = (WindowState)mWindows.get(i);
9093 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009094 WindowState win = (WindowState)mWindows.remove(i);
9095 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
9096 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009097 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009098 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009099 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009100 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9101 && lastWallpaper == i-1) {
9102 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009103 }
9104 i++;
9105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009106
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009107 // The wallpaper window(s) typically live at the bottom of the stack,
9108 // so skip them before adding app tokens.
9109 lastWallpaper++;
9110 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009111
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009112 // First add all of the exiting app tokens... these are no longer
9113 // in the main app list, but still have windows shown. We put them
9114 // in the back because now that the animation is over we no longer
9115 // will care about them.
9116 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009117 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009118 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9119 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009120
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009121 // And add in the still active app tokens in Z order.
9122 NT = mAppTokens.size();
9123 for (int j=0; j<NT; j++) {
9124 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009126
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009127 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009128 if (i != numRemoved) {
9129 Log.w(TAG, "Rebuild removed " + numRemoved
9130 + " windows but added " + i);
9131 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009134 private final void assignLayersLocked() {
9135 int N = mWindows.size();
9136 int curBaseLayer = 0;
9137 int curLayer = 0;
9138 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009140 for (i=0; i<N; i++) {
9141 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009142 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9143 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009144 curLayer += WINDOW_LAYER_MULTIPLIER;
9145 w.mLayer = curLayer;
9146 } else {
9147 curBaseLayer = curLayer = w.mBaseLayer;
9148 w.mLayer = curLayer;
9149 }
9150 if (w.mTargetAppToken != null) {
9151 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9152 } else if (w.mAppToken != null) {
9153 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9154 } else {
9155 w.mAnimLayer = w.mLayer;
9156 }
9157 if (w.mIsImWindow) {
9158 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009159 } else if (w.mIsWallpaper) {
9160 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009161 }
9162 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9163 + w.mAnimLayer);
9164 //System.out.println(
9165 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9166 }
9167 }
9168
9169 private boolean mInLayout = false;
9170 private final void performLayoutAndPlaceSurfacesLocked() {
9171 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009172 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 throw new RuntimeException("Recursive call!");
9174 }
9175 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9176 return;
9177 }
9178
9179 boolean recoveringMemory = false;
9180 if (mForceRemoves != null) {
9181 recoveringMemory = true;
9182 // Wait a little it for things to settle down, and off we go.
9183 for (int i=0; i<mForceRemoves.size(); i++) {
9184 WindowState ws = mForceRemoves.get(i);
9185 Log.i(TAG, "Force removing: " + ws);
9186 removeWindowInnerLocked(ws.mSession, ws);
9187 }
9188 mForceRemoves = null;
9189 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9190 Object tmp = new Object();
9191 synchronized (tmp) {
9192 try {
9193 tmp.wait(250);
9194 } catch (InterruptedException e) {
9195 }
9196 }
9197 }
Romain Guy06882f82009-06-10 13:36:04 -07009198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009199 mInLayout = true;
9200 try {
9201 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009203 int i = mPendingRemove.size()-1;
9204 if (i >= 0) {
9205 while (i >= 0) {
9206 WindowState w = mPendingRemove.get(i);
9207 removeWindowInnerLocked(w.mSession, w);
9208 i--;
9209 }
9210 mPendingRemove.clear();
9211
9212 mInLayout = false;
9213 assignLayersLocked();
9214 mLayoutNeeded = true;
9215 performLayoutAndPlaceSurfacesLocked();
9216
9217 } else {
9218 mInLayout = false;
9219 if (mLayoutNeeded) {
9220 requestAnimationLocked(0);
9221 }
9222 }
9223 } catch (RuntimeException e) {
9224 mInLayout = false;
9225 Log.e(TAG, "Unhandled exception while layout out windows", e);
9226 }
9227 }
9228
9229 private final void performLayoutLockedInner() {
9230 final int dw = mDisplay.getWidth();
9231 final int dh = mDisplay.getHeight();
9232
9233 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009234 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009235 int i;
9236
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009237 if (DEBUG_LAYOUT) Log.v(TAG, "performLayout: needed="
9238 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009240 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009241
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009242 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009243 mPolicy.beginLayoutLw(dw, dh);
9244
9245 // First perform layout of any root windows (not attached
9246 // to another window).
9247 int topAttached = -1;
9248 for (i = N-1; i >= 0; i--) {
9249 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009250
9251 // Don't do layout of a window if it is not visible, or
9252 // soon won't be visible, to avoid wasting time and funky
9253 // changes while a window is animating away.
9254 final AppWindowToken atoken = win.mAppToken;
9255 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009256 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009257 || win.mRootToken.hidden
9258 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009259 || win.mAttachedHidden
9260 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009261
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009262 if (win.mLayoutAttached) {
9263 if (DEBUG_LAYOUT) Log.v(TAG, "First pass " + win
9264 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9265 + " mLayoutAttached=" + win.mLayoutAttached);
9266 if (DEBUG_LAYOUT && gone) Log.v(TAG, " (mViewVisibility="
9267 + win.mViewVisibility + " mRelayoutCalled="
9268 + win.mRelayoutCalled + " hidden="
9269 + win.mRootToken.hidden + " hiddenRequested="
9270 + (atoken != null && atoken.hiddenRequested)
9271 + " mAttachedHidden=" + win.mAttachedHidden);
9272 }
9273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009274 // If this view is GONE, then skip it -- keep the current
9275 // frame, and let the caller know so they can ignore it
9276 // if they want. (We do the normal layout for INVISIBLE
9277 // windows, since that means "perform layout as normal,
9278 // just don't display").
9279 if (!gone || !win.mHaveFrame) {
9280 if (!win.mLayoutAttached) {
9281 mPolicy.layoutWindowLw(win, win.mAttrs, null);
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009282 if (DEBUG_LAYOUT) Log.v(TAG, "-> mFrame="
9283 + win.mFrame + " mContainingFrame="
9284 + win.mContainingFrame + " mDisplayFrame="
9285 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009286 } else {
9287 if (topAttached < 0) topAttached = i;
9288 }
9289 }
9290 }
Romain Guy06882f82009-06-10 13:36:04 -07009291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009292 // Now perform layout of attached windows, which usually
9293 // depend on the position of the window they are attached to.
9294 // XXX does not deal with windows that are attached to windows
9295 // that are themselves attached.
9296 for (i = topAttached; i >= 0; i--) {
9297 WindowState win = (WindowState) mWindows.get(i);
9298
9299 // If this view is GONE, then skip it -- keep the current
9300 // frame, and let the caller know so they can ignore it
9301 // if they want. (We do the normal layout for INVISIBLE
9302 // windows, since that means "perform layout as normal,
9303 // just don't display").
9304 if (win.mLayoutAttached) {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009305 if (DEBUG_LAYOUT) Log.v(TAG, "Second pass " + win
9306 + " mHaveFrame=" + win.mHaveFrame
9307 + " mViewVisibility=" + win.mViewVisibility
9308 + " mRelayoutCalled=" + win.mRelayoutCalled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009309 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9310 || !win.mHaveFrame) {
9311 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009312 if (DEBUG_LAYOUT) Log.v(TAG, "-> mFrame="
9313 + win.mFrame + " mContainingFrame="
9314 + win.mContainingFrame + " mDisplayFrame="
9315 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009316 }
9317 }
9318 }
9319
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009320 int changes = mPolicy.finishLayoutLw();
9321 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9322 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9323 assignLayersLocked();
9324 }
9325 }
9326 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009327 mLayoutNeeded = false;
9328 } else if (repeats > 2) {
9329 Log.w(TAG, "Layout repeat aborted after too many iterations");
9330 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009331 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9332 Configuration newConfig = updateOrientationFromAppTokensLocked(
9333 null, null);
9334 if (newConfig != null) {
9335 mLayoutNeeded = true;
9336 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9337 }
9338 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009339 } else {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009340 if (DEBUG_LAYOUT) Log.v(TAG, "Repeating layout because changes=0x"
9341 + Integer.toHexString(changes));
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009342 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009343 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009344 if (DEBUG_LAYOUT) Log.v(TAG, "Computing new config from layout");
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009345 Configuration newConfig = updateOrientationFromAppTokensLocked(
9346 null, null);
9347 if (newConfig != null) {
9348 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9349 }
9350 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009352 }
9353 }
Romain Guy06882f82009-06-10 13:36:04 -07009354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009355 private final void performLayoutAndPlaceSurfacesLockedInner(
9356 boolean recoveringMemory) {
9357 final long currentTime = SystemClock.uptimeMillis();
9358 final int dw = mDisplay.getWidth();
9359 final int dh = mDisplay.getHeight();
9360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009361 int i;
9362
9363 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009364 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009366 if (mFxSession == null) {
9367 mFxSession = new SurfaceSession();
9368 }
Romain Guy06882f82009-06-10 13:36:04 -07009369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009370 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9371
9372 // Initialize state of exiting tokens.
9373 for (i=mExitingTokens.size()-1; i>=0; i--) {
9374 mExitingTokens.get(i).hasVisible = false;
9375 }
9376
9377 // Initialize state of exiting applications.
9378 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9379 mExitingAppTokens.get(i).hasVisible = false;
9380 }
9381
9382 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009383 boolean orientationChangeComplete = true;
9384 Session holdScreen = null;
9385 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009386 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009387 boolean focusDisplayed = false;
9388 boolean animating = false;
9389
9390 Surface.openTransaction();
9391 try {
9392 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009393 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394
9395 do {
9396 final int transactionSequence = ++mTransactionSequence;
9397
9398 // Update animations of all applications, including those
9399 // associated with exiting/removed apps
9400 boolean tokensAnimating = false;
9401 final int NAT = mAppTokens.size();
9402 for (i=0; i<NAT; i++) {
9403 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9404 tokensAnimating = true;
9405 }
9406 }
9407 final int NEAT = mExitingAppTokens.size();
9408 for (i=0; i<NEAT; i++) {
9409 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9410 tokensAnimating = true;
9411 }
9412 }
9413
9414 animating = tokensAnimating;
9415 restart = false;
9416
9417 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009418 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009419 boolean focusMayChange = false;
9420 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009421
9422 mPolicy.beginAnimationLw(dw, dh);
9423
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009424 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009426 for (i=N-1; i>=0; i--) {
9427 WindowState w = (WindowState)mWindows.get(i);
9428
9429 final WindowManager.LayoutParams attrs = w.mAttrs;
9430
9431 if (w.mSurface != null) {
9432 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009433 if (w.commitFinishDrawingLocked(currentTime)) {
9434 if ((w.mAttrs.flags
9435 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009436 if (DEBUG_WALLPAPER) Log.v(TAG,
9437 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009438 wallpaperMayChange = true;
9439 }
9440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009441
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009442 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009443 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9444 animating = true;
9445 //w.dump(" ");
9446 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009447 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9448 wallpaperMayChange = true;
9449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009450
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009451 if (mPolicy.doesForceHide(w, attrs)) {
9452 if (!wasAnimating && animating) {
9453 wallpaperForceHidingChanged = true;
9454 focusMayChange = true;
9455 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9456 forceHiding = true;
9457 }
9458 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9459 boolean changed;
9460 if (forceHiding) {
9461 changed = w.hideLw(false, false);
9462 } else {
9463 changed = w.showLw(false, false);
9464 if (changed && wallpaperForceHidingChanged
9465 && w.isReadyForDisplay()) {
9466 // Assume we will need to animate. If
9467 // we don't (because the wallpaper will
9468 // stay with the lock screen), then we will
9469 // clean up later.
9470 Animation a = mPolicy.createForceHideEnterAnimation();
9471 if (a != null) {
9472 w.setAnimation(a);
9473 }
9474 }
9475 }
9476 if (changed && (attrs.flags
9477 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9478 wallpaperMayChange = true;
9479 }
9480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009482 mPolicy.animatingWindowLw(w, attrs);
9483 }
9484
9485 final AppWindowToken atoken = w.mAppToken;
9486 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9487 if (atoken.lastTransactionSequence != transactionSequence) {
9488 atoken.lastTransactionSequence = transactionSequence;
9489 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9490 atoken.startingDisplayed = false;
9491 }
9492 if ((w.isOnScreen() || w.mAttrs.type
9493 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9494 && !w.mExiting && !w.mDestroying) {
9495 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009496 Log.v(TAG, "Eval win " + w + ": isDrawn="
9497 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009498 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009499 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009500 Log.v(TAG, "Not displayed: s=" + w.mSurface
9501 + " pv=" + w.mPolicyVisibility
9502 + " dp=" + w.mDrawPending
9503 + " cdp=" + w.mCommitDrawPending
9504 + " ah=" + w.mAttachedHidden
9505 + " th=" + atoken.hiddenRequested
9506 + " a=" + w.mAnimating);
9507 }
9508 }
9509 if (w != atoken.startingWindow) {
9510 if (!atoken.freezingScreen || !w.mAppFreezing) {
9511 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009512 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009513 atoken.numDrawnWindows++;
9514 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9515 "tokenMayBeDrawn: " + atoken
9516 + " freezingScreen=" + atoken.freezingScreen
9517 + " mAppFreezing=" + w.mAppFreezing);
9518 tokenMayBeDrawn = true;
9519 }
9520 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009521 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009522 atoken.startingDisplayed = true;
9523 }
9524 }
9525 } else if (w.mReadyToShow) {
9526 w.performShowLocked();
9527 }
9528 }
9529
9530 if (mPolicy.finishAnimationLw()) {
9531 restart = true;
9532 }
9533
9534 if (tokenMayBeDrawn) {
9535 // See if any windows have been drawn, so they (and others
9536 // associated with them) can now be shown.
9537 final int NT = mTokenList.size();
9538 for (i=0; i<NT; i++) {
9539 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9540 if (wtoken == null) {
9541 continue;
9542 }
9543 if (wtoken.freezingScreen) {
9544 int numInteresting = wtoken.numInterestingWindows;
9545 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9546 if (DEBUG_VISIBILITY) Log.v(TAG,
9547 "allDrawn: " + wtoken
9548 + " interesting=" + numInteresting
9549 + " drawn=" + wtoken.numDrawnWindows);
9550 wtoken.showAllWindowsLocked();
9551 unsetAppFreezingScreenLocked(wtoken, false, true);
9552 orientationChangeComplete = true;
9553 }
9554 } else if (!wtoken.allDrawn) {
9555 int numInteresting = wtoken.numInterestingWindows;
9556 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9557 if (DEBUG_VISIBILITY) Log.v(TAG,
9558 "allDrawn: " + wtoken
9559 + " interesting=" + numInteresting
9560 + " drawn=" + wtoken.numDrawnWindows);
9561 wtoken.allDrawn = true;
9562 restart = true;
9563
9564 // We can now show all of the drawn windows!
9565 if (!mOpeningApps.contains(wtoken)) {
9566 wtoken.showAllWindowsLocked();
9567 }
9568 }
9569 }
9570 }
9571 }
9572
9573 // If we are ready to perform an app transition, check through
9574 // all of the app tokens to be shown and see if they are ready
9575 // to go.
9576 if (mAppTransitionReady) {
9577 int NN = mOpeningApps.size();
9578 boolean goodToGo = true;
9579 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9580 "Checking " + NN + " opening apps (frozen="
9581 + mDisplayFrozen + " timeout="
9582 + mAppTransitionTimeout + ")...");
9583 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9584 // If the display isn't frozen, wait to do anything until
9585 // all of the apps are ready. Otherwise just go because
9586 // we'll unfreeze the display when everyone is ready.
9587 for (i=0; i<NN && goodToGo; i++) {
9588 AppWindowToken wtoken = mOpeningApps.get(i);
9589 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9590 "Check opening app" + wtoken + ": allDrawn="
9591 + wtoken.allDrawn + " startingDisplayed="
9592 + wtoken.startingDisplayed);
9593 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9594 && !wtoken.startingMoved) {
9595 goodToGo = false;
9596 }
9597 }
9598 }
9599 if (goodToGo) {
9600 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9601 int transit = mNextAppTransition;
9602 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009603 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009604 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009605 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009606 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009607 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009608 mAppTransitionTimeout = false;
9609 mStartingIconInTransition = false;
9610 mSkipAppTransitionAnimation = false;
9611
9612 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9613
Dianne Hackborna8f60182009-09-01 19:01:50 -07009614 // If there are applications waiting to come to the
9615 // top of the stack, now is the time to move their windows.
9616 // (Note that we don't do apps going to the bottom
9617 // here -- we want to keep their windows in the old
9618 // Z-order until the animation completes.)
9619 if (mToTopApps.size() > 0) {
9620 NN = mAppTokens.size();
9621 for (i=0; i<NN; i++) {
9622 AppWindowToken wtoken = mAppTokens.get(i);
9623 if (wtoken.sendingToTop) {
9624 wtoken.sendingToTop = false;
9625 moveAppWindowsLocked(wtoken, NN, false);
9626 }
9627 }
9628 mToTopApps.clear();
9629 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009630
Dianne Hackborn25994b42009-09-04 14:21:19 -07009631 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009632
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009633 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009634 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009635
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009636 // The top-most window will supply the layout params,
9637 // and we will determine it below.
9638 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009639 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009640 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009641
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009642 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009643 "New wallpaper target=" + mWallpaperTarget
9644 + ", lower target=" + mLowerWallpaperTarget
9645 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009646 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009647 // Do a first pass through the tokens for two
9648 // things:
9649 // (1) Determine if both the closing and opening
9650 // app token sets are wallpaper targets, in which
9651 // case special animations are needed
9652 // (since the wallpaper needs to stay static
9653 // behind them).
9654 // (2) Find the layout params of the top-most
9655 // application window in the tokens, which is
9656 // what will control the animation theme.
9657 final int NC = mClosingApps.size();
9658 NN = NC + mOpeningApps.size();
9659 for (i=0; i<NN; i++) {
9660 AppWindowToken wtoken;
9661 int mode;
9662 if (i < NC) {
9663 wtoken = mClosingApps.get(i);
9664 mode = 1;
9665 } else {
9666 wtoken = mOpeningApps.get(i-NC);
9667 mode = 2;
9668 }
9669 if (mLowerWallpaperTarget != null) {
9670 if (mLowerWallpaperTarget.mAppToken == wtoken
9671 || mUpperWallpaperTarget.mAppToken == wtoken) {
9672 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009673 }
9674 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009675 if (wtoken.appFullscreen) {
9676 WindowState ws = wtoken.findMainWindow();
9677 if (ws != null) {
9678 // If this is a compatibility mode
9679 // window, we will always use its anim.
9680 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9681 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009682 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009683 bestAnimLayer = Integer.MAX_VALUE;
9684 } else if (ws.mLayer > bestAnimLayer) {
9685 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009686 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009687 bestAnimLayer = ws.mLayer;
9688 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009689 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009690 }
9691 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009692
Dianne Hackborn25994b42009-09-04 14:21:19 -07009693 if (foundWallpapers == 3) {
9694 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9695 "Wallpaper animation!");
9696 switch (transit) {
9697 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9698 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9699 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9700 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9701 break;
9702 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9703 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9704 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9705 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9706 break;
9707 }
9708 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9709 "New transit: " + transit);
9710 } else if (oldWallpaper != null) {
9711 // We are transitioning from an activity with
9712 // a wallpaper to one without.
9713 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9714 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9715 "New transit away from wallpaper: " + transit);
9716 } else if (mWallpaperTarget != null) {
9717 // We are transitioning from an activity without
9718 // a wallpaper to now showing the wallpaper
9719 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9720 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9721 "New transit into wallpaper: " + transit);
9722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009723
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009724 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9725 mLastEnterAnimToken = animToken;
9726 mLastEnterAnimParams = animLp;
9727 } else if (mLastEnterAnimParams != null) {
9728 animLp = mLastEnterAnimParams;
9729 mLastEnterAnimToken = null;
9730 mLastEnterAnimParams = null;
9731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009733 NN = mOpeningApps.size();
9734 for (i=0; i<NN; i++) {
9735 AppWindowToken wtoken = mOpeningApps.get(i);
9736 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9737 "Now opening app" + wtoken);
9738 wtoken.reportedVisible = false;
9739 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009740 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009741 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009742 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009743 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009744 wtoken.showAllWindowsLocked();
9745 }
9746 NN = mClosingApps.size();
9747 for (i=0; i<NN; i++) {
9748 AppWindowToken wtoken = mClosingApps.get(i);
9749 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9750 "Now closing app" + wtoken);
9751 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009752 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009753 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009754 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009755 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009756 // Force the allDrawn flag, because we want to start
9757 // this guy's animations regardless of whether it's
9758 // gotten drawn.
9759 wtoken.allDrawn = true;
9760 }
9761
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009762 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009764 mOpeningApps.clear();
9765 mClosingApps.clear();
9766
9767 // This has changed the visibility of windows, so perform
9768 // a new layout to get them all up-to-date.
9769 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009770 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9771 assignLayersLocked();
9772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009773 performLayoutLockedInner();
9774 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009775 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009776
9777 restart = true;
9778 }
9779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009780
Dianne Hackborna8f60182009-09-01 19:01:50 -07009781 if (!animating && mAppTransitionRunning) {
9782 // We have finished the animation of an app transition. To do
9783 // this, we have delayed a lot of operations like showing and
9784 // hiding apps, moving apps in Z-order, etc. The app token list
9785 // reflects the correct Z-order, but the window list may now
9786 // be out of sync with it. So here we will just rebuild the
9787 // entire app window list. Fun!
9788 mAppTransitionRunning = false;
9789 // Clear information about apps that were moving.
9790 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009791
Dianne Hackborna8f60182009-09-01 19:01:50 -07009792 rebuildAppWindowListLocked();
9793 restart = true;
9794 moveInputMethodWindowsIfNeededLocked(false);
9795 wallpaperMayChange = true;
9796 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009797 // Since the window list has been rebuilt, focus might
9798 // have to be recomputed since the actual order of windows
9799 // might have changed again.
9800 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009802
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009803 int adjResult = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009804
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009805 if (wallpaperForceHidingChanged) {
9806 // At this point, there was a window with a wallpaper that
9807 // was force hiding other windows behind it, but now it
9808 // is going away. This may be simple -- just animate
9809 // away the wallpaper and its window -- or it may be
9810 // hard -- the wallpaper now needs to be shown behind
9811 // something that was hidden.
9812 WindowState oldWallpaper = mWallpaperTarget;
9813 adjResult = adjustWallpaperWindowsLocked();
9814 wallpaperMayChange = false;
9815 if (false) Log.v(TAG, "****** OLD: " + oldWallpaper
9816 + " NEW: " + mWallpaperTarget);
9817 if (mLowerWallpaperTarget == null) {
9818 // Whoops, we don't need a special wallpaper animation.
9819 // Clear them out.
9820 forceHiding = false;
9821 for (i=N-1; i>=0; i--) {
9822 WindowState w = (WindowState)mWindows.get(i);
9823 if (w.mSurface != null) {
9824 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009825 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9826 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009827 forceHiding = true;
9828 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9829 if (!w.mAnimating) {
9830 // We set the animation above so it
9831 // is not yet running.
9832 w.clearAnimation();
9833 }
9834 }
9835 }
9836 }
9837 }
9838 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009839
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009840 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009841 if (DEBUG_WALLPAPER) Log.v(TAG,
9842 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009843 adjResult = adjustWallpaperWindowsLocked();
9844 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009845
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009846 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9847 if (DEBUG_WALLPAPER) Log.v(TAG,
9848 "Wallpaper layer changed: assigning layers + relayout");
9849 restart = true;
9850 mLayoutNeeded = true;
9851 assignLayersLocked();
9852 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9853 if (DEBUG_WALLPAPER) Log.v(TAG,
9854 "Wallpaper visibility changed: relayout");
9855 restart = true;
9856 mLayoutNeeded = true;
9857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009858
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009859 if (focusMayChange) {
9860 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009861 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009862 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009863 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009864 }
9865
9866 if (mLayoutNeeded) {
9867 restart = true;
9868 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009869 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009871 } while (restart);
9872
9873 // THIRD LOOP: Update the surfaces of all windows.
9874
9875 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9876
9877 boolean obscured = false;
9878 boolean blurring = false;
9879 boolean dimming = false;
9880 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009881 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009882 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009884 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886 for (i=N-1; i>=0; i--) {
9887 WindowState w = (WindowState)mWindows.get(i);
9888
9889 boolean displayed = false;
9890 final WindowManager.LayoutParams attrs = w.mAttrs;
9891 final int attrFlags = attrs.flags;
9892
9893 if (w.mSurface != null) {
9894 w.computeShownFrameLocked();
9895 if (localLOGV) Log.v(
9896 TAG, "Placing surface #" + i + " " + w.mSurface
9897 + ": new=" + w.mShownFrame + ", old="
9898 + w.mLastShownFrame);
9899
9900 boolean resize;
9901 int width, height;
9902 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9903 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9904 w.mLastRequestedHeight != w.mRequestedHeight;
9905 // for a scaled surface, we just want to use
9906 // the requested size.
9907 width = w.mRequestedWidth;
9908 height = w.mRequestedHeight;
9909 w.mLastRequestedWidth = width;
9910 w.mLastRequestedHeight = height;
9911 w.mLastShownFrame.set(w.mShownFrame);
9912 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009913 if (SHOW_TRANSACTIONS) Log.i(
9914 TAG, " SURFACE " + w.mSurface
9915 + ": POS " + w.mShownFrame.left
9916 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009917 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9918 } catch (RuntimeException e) {
9919 Log.w(TAG, "Error positioning surface in " + w, e);
9920 if (!recoveringMemory) {
9921 reclaimSomeSurfaceMemoryLocked(w, "position");
9922 }
9923 }
9924 } else {
9925 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9926 width = w.mShownFrame.width();
9927 height = w.mShownFrame.height();
9928 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009929 }
9930
9931 if (resize) {
9932 if (width < 1) width = 1;
9933 if (height < 1) height = 1;
9934 if (w.mSurface != null) {
9935 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009936 if (SHOW_TRANSACTIONS) Log.i(
9937 TAG, " SURFACE " + w.mSurface + ": POS "
9938 + w.mShownFrame.left + ","
9939 + w.mShownFrame.top + " SIZE "
9940 + w.mShownFrame.width() + "x"
9941 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009942 w.mSurface.setSize(width, height);
9943 w.mSurface.setPosition(w.mShownFrame.left,
9944 w.mShownFrame.top);
9945 } catch (RuntimeException e) {
9946 // If something goes wrong with the surface (such
9947 // as running out of memory), don't take down the
9948 // entire system.
9949 Log.e(TAG, "Failure updating surface of " + w
9950 + "size=(" + width + "x" + height
9951 + "), pos=(" + w.mShownFrame.left
9952 + "," + w.mShownFrame.top + ")", e);
9953 if (!recoveringMemory) {
9954 reclaimSomeSurfaceMemoryLocked(w, "size");
9955 }
9956 }
9957 }
9958 }
9959 if (!w.mAppFreezing) {
9960 w.mContentInsetsChanged =
9961 !w.mLastContentInsets.equals(w.mContentInsets);
9962 w.mVisibleInsetsChanged =
9963 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009964 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009965 || w.mContentInsetsChanged
9966 || w.mVisibleInsetsChanged) {
9967 w.mLastFrame.set(w.mFrame);
9968 w.mLastContentInsets.set(w.mContentInsets);
9969 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009970 // If the screen is currently frozen, then keep
9971 // it frozen until this window draws at its new
9972 // orientation.
9973 if (mDisplayFrozen) {
9974 if (DEBUG_ORIENTATION) Log.v(TAG,
9975 "Resizing while display frozen: " + w);
9976 w.mOrientationChanging = true;
9977 if (mWindowsFreezingScreen) {
9978 mWindowsFreezingScreen = true;
9979 // XXX should probably keep timeout from
9980 // when we first froze the display.
9981 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9982 mH.sendMessageDelayed(mH.obtainMessage(
9983 H.WINDOW_FREEZE_TIMEOUT), 2000);
9984 }
9985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009986 // If the orientation is changing, then we need to
9987 // hold off on unfreezing the display until this
9988 // window has been redrawn; to do that, we need
9989 // to go through the process of getting informed
9990 // by the application when it has finished drawing.
9991 if (w.mOrientationChanging) {
9992 if (DEBUG_ORIENTATION) Log.v(TAG,
9993 "Orientation start waiting for draw in "
9994 + w + ", surface " + w.mSurface);
9995 w.mDrawPending = true;
9996 w.mCommitDrawPending = false;
9997 w.mReadyToShow = false;
9998 if (w.mAppToken != null) {
9999 w.mAppToken.allDrawn = false;
10000 }
10001 }
Romain Guy06882f82009-06-10 13:36:04 -070010002 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010003 "Resizing window " + w + " to " + w.mFrame);
10004 mResizingWindows.add(w);
10005 } else if (w.mOrientationChanging) {
10006 if (!w.mDrawPending && !w.mCommitDrawPending) {
10007 if (DEBUG_ORIENTATION) Log.v(TAG,
10008 "Orientation not waiting for draw in "
10009 + w + ", surface " + w.mSurface);
10010 w.mOrientationChanging = false;
10011 }
10012 }
10013 }
10014
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010015 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010016 if (!w.mLastHidden) {
10017 //dump();
10018 w.mLastHidden = true;
10019 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010020 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010021 if (w.mSurface != null) {
10022 try {
10023 w.mSurface.hide();
10024 } catch (RuntimeException e) {
10025 Log.w(TAG, "Exception hiding surface in " + w);
10026 }
10027 }
10028 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10029 }
10030 // If we are waiting for this window to handle an
10031 // orientation change, well, it is hidden, so
10032 // doesn't really matter. Note that this does
10033 // introduce a potential glitch if the window
10034 // becomes unhidden before it has drawn for the
10035 // new orientation.
10036 if (w.mOrientationChanging) {
10037 w.mOrientationChanging = false;
10038 if (DEBUG_ORIENTATION) Log.v(TAG,
10039 "Orientation change skips hidden " + w);
10040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010041 } else if (w.mLastLayer != w.mAnimLayer
10042 || w.mLastAlpha != w.mShownAlpha
10043 || w.mLastDsDx != w.mDsDx
10044 || w.mLastDtDx != w.mDtDx
10045 || w.mLastDsDy != w.mDsDy
10046 || w.mLastDtDy != w.mDtDy
10047 || w.mLastHScale != w.mHScale
10048 || w.mLastVScale != w.mVScale
10049 || w.mLastHidden) {
10050 displayed = true;
10051 w.mLastAlpha = w.mShownAlpha;
10052 w.mLastLayer = w.mAnimLayer;
10053 w.mLastDsDx = w.mDsDx;
10054 w.mLastDtDx = w.mDtDx;
10055 w.mLastDsDy = w.mDsDy;
10056 w.mLastDtDy = w.mDtDy;
10057 w.mLastHScale = w.mHScale;
10058 w.mLastVScale = w.mVScale;
10059 if (SHOW_TRANSACTIONS) Log.i(
10060 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010061 + w.mShownAlpha + " layer=" + w.mAnimLayer
10062 + " matrix=[" + (w.mDsDx*w.mHScale)
10063 + "," + (w.mDtDx*w.mVScale)
10064 + "][" + (w.mDsDy*w.mHScale)
10065 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010066 if (w.mSurface != null) {
10067 try {
10068 w.mSurface.setAlpha(w.mShownAlpha);
10069 w.mSurface.setLayer(w.mAnimLayer);
10070 w.mSurface.setMatrix(
10071 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10072 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10073 } catch (RuntimeException e) {
10074 Log.w(TAG, "Error updating surface in " + w, e);
10075 if (!recoveringMemory) {
10076 reclaimSomeSurfaceMemoryLocked(w, "update");
10077 }
10078 }
10079 }
10080
10081 if (w.mLastHidden && !w.mDrawPending
10082 && !w.mCommitDrawPending
10083 && !w.mReadyToShow) {
10084 if (SHOW_TRANSACTIONS) Log.i(
10085 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
10086 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
10087 + " during relayout");
10088 if (showSurfaceRobustlyLocked(w)) {
10089 w.mHasDrawn = true;
10090 w.mLastHidden = false;
10091 } else {
10092 w.mOrientationChanging = false;
10093 }
10094 }
10095 if (w.mSurface != null) {
10096 w.mToken.hasVisible = true;
10097 }
10098 } else {
10099 displayed = true;
10100 }
10101
10102 if (displayed) {
10103 if (!covered) {
10104 if (attrs.width == LayoutParams.FILL_PARENT
10105 && attrs.height == LayoutParams.FILL_PARENT) {
10106 covered = true;
10107 }
10108 }
10109 if (w.mOrientationChanging) {
10110 if (w.mDrawPending || w.mCommitDrawPending) {
10111 orientationChangeComplete = false;
10112 if (DEBUG_ORIENTATION) Log.v(TAG,
10113 "Orientation continue waiting for draw in " + w);
10114 } else {
10115 w.mOrientationChanging = false;
10116 if (DEBUG_ORIENTATION) Log.v(TAG,
10117 "Orientation change complete in " + w);
10118 }
10119 }
10120 w.mToken.hasVisible = true;
10121 }
10122 } else if (w.mOrientationChanging) {
10123 if (DEBUG_ORIENTATION) Log.v(TAG,
10124 "Orientation change skips hidden " + w);
10125 w.mOrientationChanging = false;
10126 }
10127
10128 final boolean canBeSeen = w.isDisplayedLw();
10129
10130 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10131 focusDisplayed = true;
10132 }
10133
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010134 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010136 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010137 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010138 if (w.mSurface != null) {
10139 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10140 holdScreen = w.mSession;
10141 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010142 if (!syswin && w.mAttrs.screenBrightness >= 0
10143 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010144 screenBrightness = w.mAttrs.screenBrightness;
10145 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010146 if (!syswin && w.mAttrs.buttonBrightness >= 0
10147 && buttonBrightness < 0) {
10148 buttonBrightness = w.mAttrs.buttonBrightness;
10149 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010150 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10151 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10152 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10153 syswin = true;
10154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010156
Dianne Hackborn25994b42009-09-04 14:21:19 -070010157 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10158 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010159 // This window completely covers everything behind it,
10160 // so we want to leave all of them as unblurred (for
10161 // performance reasons).
10162 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010163 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
10164 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010165 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010166 obscured = true;
10167 if (mBackgroundFillerSurface == null) {
10168 try {
10169 mBackgroundFillerSurface = new Surface(mFxSession, 0,
10170 0, dw, dh,
10171 PixelFormat.OPAQUE,
10172 Surface.FX_SURFACE_NORMAL);
10173 } catch (Exception e) {
10174 Log.e(TAG, "Exception creating filler surface", e);
10175 }
10176 }
10177 try {
10178 mBackgroundFillerSurface.setPosition(0, 0);
10179 mBackgroundFillerSurface.setSize(dw, dh);
10180 // Using the same layer as Dim because they will never be shown at the
10181 // same time.
10182 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10183 mBackgroundFillerSurface.show();
10184 } catch (RuntimeException e) {
10185 Log.e(TAG, "Exception showing filler surface");
10186 }
10187 backgroundFillerShown = true;
10188 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010189 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010190 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10191 if (localLOGV) Log.v(TAG, "Win " + w
10192 + ": blurring=" + blurring
10193 + " obscured=" + obscured
10194 + " displayed=" + displayed);
10195 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10196 if (!dimming) {
10197 //Log.i(TAG, "DIM BEHIND: " + w);
10198 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010199 if (mDimAnimator == null) {
10200 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010201 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010202 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010203 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010204 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010205 }
10206 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10207 if (!blurring) {
10208 //Log.i(TAG, "BLUR BEHIND: " + w);
10209 blurring = true;
10210 mBlurShown = true;
10211 if (mBlurSurface == null) {
10212 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10213 + mBlurSurface + ": CREATE");
10214 try {
Romain Guy06882f82009-06-10 13:36:04 -070010215 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010216 -1, 16, 16,
10217 PixelFormat.OPAQUE,
10218 Surface.FX_SURFACE_BLUR);
10219 } catch (Exception e) {
10220 Log.e(TAG, "Exception creating Blur surface", e);
10221 }
10222 }
10223 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10224 + mBlurSurface + ": SHOW pos=(0,0) (" +
10225 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10226 if (mBlurSurface != null) {
10227 mBlurSurface.setPosition(0, 0);
10228 mBlurSurface.setSize(dw, dh);
10229 try {
10230 mBlurSurface.show();
10231 } catch (RuntimeException e) {
10232 Log.w(TAG, "Failure showing blur surface", e);
10233 }
10234 }
10235 }
10236 mBlurSurface.setLayer(w.mAnimLayer-2);
10237 }
10238 }
10239 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010240
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010241 if (obscuredChanged && mWallpaperTarget == w) {
10242 // This is the wallpaper target and its obscured state
10243 // changed... make sure the current wallaper's visibility
10244 // has been updated accordingly.
10245 updateWallpaperVisibilityLocked();
10246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010248
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010249 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10250 mBackgroundFillerShown = false;
10251 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10252 try {
10253 mBackgroundFillerSurface.hide();
10254 } catch (RuntimeException e) {
10255 Log.e(TAG, "Exception hiding filler surface", e);
10256 }
10257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010258
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010259 if (mDimAnimator != null && mDimAnimator.mDimShown) {
10260 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010261 }
Romain Guy06882f82009-06-10 13:36:04 -070010262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010263 if (!blurring && mBlurShown) {
10264 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10265 + ": HIDE");
10266 try {
10267 mBlurSurface.hide();
10268 } catch (IllegalArgumentException e) {
10269 Log.w(TAG, "Illegal argument exception hiding blur surface");
10270 }
10271 mBlurShown = false;
10272 }
10273
10274 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10275 } catch (RuntimeException e) {
10276 Log.e(TAG, "Unhandled exception in Window Manager", e);
10277 }
10278
10279 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010281 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10282 "With display frozen, orientationChangeComplete="
10283 + orientationChangeComplete);
10284 if (orientationChangeComplete) {
10285 if (mWindowsFreezingScreen) {
10286 mWindowsFreezingScreen = false;
10287 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10288 }
10289 if (mAppsFreezingScreen == 0) {
10290 stopFreezingDisplayLocked();
10291 }
10292 }
Romain Guy06882f82009-06-10 13:36:04 -070010293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010294 i = mResizingWindows.size();
10295 if (i > 0) {
10296 do {
10297 i--;
10298 WindowState win = mResizingWindows.get(i);
10299 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010300 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10301 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 win.mClient.resized(win.mFrame.width(),
10303 win.mFrame.height(), win.mLastContentInsets,
10304 win.mLastVisibleInsets, win.mDrawPending);
10305 win.mContentInsetsChanged = false;
10306 win.mVisibleInsetsChanged = false;
10307 } catch (RemoteException e) {
10308 win.mOrientationChanging = false;
10309 }
10310 } while (i > 0);
10311 mResizingWindows.clear();
10312 }
Romain Guy06882f82009-06-10 13:36:04 -070010313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010314 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010315 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010316 i = mDestroySurface.size();
10317 if (i > 0) {
10318 do {
10319 i--;
10320 WindowState win = mDestroySurface.get(i);
10321 win.mDestroying = false;
10322 if (mInputMethodWindow == win) {
10323 mInputMethodWindow = null;
10324 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010325 if (win == mWallpaperTarget) {
10326 wallpaperDestroyed = true;
10327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 win.destroySurfaceLocked();
10329 } while (i > 0);
10330 mDestroySurface.clear();
10331 }
10332
10333 // Time to remove any exiting tokens?
10334 for (i=mExitingTokens.size()-1; i>=0; i--) {
10335 WindowToken token = mExitingTokens.get(i);
10336 if (!token.hasVisible) {
10337 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010338 if (token.windowType == TYPE_WALLPAPER) {
10339 mWallpaperTokens.remove(token);
10340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010341 }
10342 }
10343
10344 // Time to remove any exiting applications?
10345 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10346 AppWindowToken token = mExitingAppTokens.get(i);
10347 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010348 // Make sure there is no animation running on this token,
10349 // so any windows associated with it will be removed as
10350 // soon as their animations are complete
10351 token.animation = null;
10352 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010353 mAppTokens.remove(token);
10354 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010355 if (mLastEnterAnimToken == token) {
10356 mLastEnterAnimToken = null;
10357 mLastEnterAnimParams = null;
10358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010359 }
10360 }
10361
Dianne Hackborna8f60182009-09-01 19:01:50 -070010362 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010363
Dianne Hackborna8f60182009-09-01 19:01:50 -070010364 if (!animating && mAppTransitionRunning) {
10365 // We have finished the animation of an app transition. To do
10366 // this, we have delayed a lot of operations like showing and
10367 // hiding apps, moving apps in Z-order, etc. The app token list
10368 // reflects the correct Z-order, but the window list may now
10369 // be out of sync with it. So here we will just rebuild the
10370 // entire app window list. Fun!
10371 mAppTransitionRunning = false;
10372 needRelayout = true;
10373 rebuildAppWindowListLocked();
10374 // Clear information about apps that were moving.
10375 mToBottomApps.clear();
10376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010378 if (focusDisplayed) {
10379 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10380 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010381 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010382 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010383 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010384 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010385 requestAnimationLocked(0);
10386 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010387 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10388 }
10389 mQueue.setHoldScreenLocked(holdScreen != null);
10390 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10391 mPowerManager.setScreenBrightnessOverride(-1);
10392 } else {
10393 mPowerManager.setScreenBrightnessOverride((int)
10394 (screenBrightness * Power.BRIGHTNESS_ON));
10395 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010396 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10397 mPowerManager.setButtonBrightnessOverride(-1);
10398 } else {
10399 mPowerManager.setButtonBrightnessOverride((int)
10400 (buttonBrightness * Power.BRIGHTNESS_ON));
10401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010402 if (holdScreen != mHoldingScreenOn) {
10403 mHoldingScreenOn = holdScreen;
10404 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10405 mH.sendMessage(m);
10406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010407
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010408 if (mTurnOnScreen) {
10409 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10410 LocalPowerManager.BUTTON_EVENT, true);
10411 mTurnOnScreen = false;
10412 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010413
10414 // Check to see if we are now in a state where the screen should
10415 // be enabled, because the window obscured flags have changed.
10416 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 }
10418
10419 void requestAnimationLocked(long delay) {
10420 if (!mAnimationPending) {
10421 mAnimationPending = true;
10422 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10423 }
10424 }
Romain Guy06882f82009-06-10 13:36:04 -070010425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 /**
10427 * Have the surface flinger show a surface, robustly dealing with
10428 * error conditions. In particular, if there is not enough memory
10429 * to show the surface, then we will try to get rid of other surfaces
10430 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010431 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 * @return Returns true if the surface was successfully shown.
10433 */
10434 boolean showSurfaceRobustlyLocked(WindowState win) {
10435 try {
10436 if (win.mSurface != null) {
10437 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010438 if (win.mTurnOnScreen) {
10439 win.mTurnOnScreen = false;
10440 mTurnOnScreen = true;
10441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 }
10443 return true;
10444 } catch (RuntimeException e) {
10445 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10446 }
Romain Guy06882f82009-06-10 13:36:04 -070010447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010448 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010450 return false;
10451 }
Romain Guy06882f82009-06-10 13:36:04 -070010452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10454 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010455
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010456 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010457 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459 if (mForceRemoves == null) {
10460 mForceRemoves = new ArrayList<WindowState>();
10461 }
Romain Guy06882f82009-06-10 13:36:04 -070010462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010463 long callingIdentity = Binder.clearCallingIdentity();
10464 try {
10465 // There was some problem... first, do a sanity check of the
10466 // window list to make sure we haven't left any dangling surfaces
10467 // around.
10468 int N = mWindows.size();
10469 boolean leakedSurface = false;
10470 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10471 for (int i=0; i<N; i++) {
10472 WindowState ws = (WindowState)mWindows.get(i);
10473 if (ws.mSurface != null) {
10474 if (!mSessions.contains(ws.mSession)) {
10475 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10476 + ws + " surface=" + ws.mSurface
10477 + " token=" + win.mToken
10478 + " pid=" + ws.mSession.mPid
10479 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010480 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010481 ws.mSurface = null;
10482 mForceRemoves.add(ws);
10483 i--;
10484 N--;
10485 leakedSurface = true;
10486 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10487 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10488 + ws + " surface=" + ws.mSurface
10489 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010490 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 ws.mSurface = null;
10492 leakedSurface = true;
10493 }
10494 }
10495 }
Romain Guy06882f82009-06-10 13:36:04 -070010496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010497 boolean killedApps = false;
10498 if (!leakedSurface) {
10499 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10500 SparseIntArray pidCandidates = new SparseIntArray();
10501 for (int i=0; i<N; i++) {
10502 WindowState ws = (WindowState)mWindows.get(i);
10503 if (ws.mSurface != null) {
10504 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10505 }
10506 }
10507 if (pidCandidates.size() > 0) {
10508 int[] pids = new int[pidCandidates.size()];
10509 for (int i=0; i<pids.length; i++) {
10510 pids[i] = pidCandidates.keyAt(i);
10511 }
10512 try {
10513 if (mActivityManager.killPidsForMemory(pids)) {
10514 killedApps = true;
10515 }
10516 } catch (RemoteException e) {
10517 }
10518 }
10519 }
Romain Guy06882f82009-06-10 13:36:04 -070010520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010521 if (leakedSurface || killedApps) {
10522 // We managed to reclaim some memory, so get rid of the trouble
10523 // surface and ask the app to request another one.
10524 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10525 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010526 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010527 win.mSurface = null;
10528 }
Romain Guy06882f82009-06-10 13:36:04 -070010529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010530 try {
10531 win.mClient.dispatchGetNewSurface();
10532 } catch (RemoteException e) {
10533 }
10534 }
10535 } finally {
10536 Binder.restoreCallingIdentity(callingIdentity);
10537 }
10538 }
Romain Guy06882f82009-06-10 13:36:04 -070010539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010540 private boolean updateFocusedWindowLocked(int mode) {
10541 WindowState newFocus = computeFocusedWindowLocked();
10542 if (mCurrentFocus != newFocus) {
10543 // This check makes sure that we don't already have the focus
10544 // change message pending.
10545 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10546 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10547 if (localLOGV) Log.v(
10548 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10549 final WindowState oldFocus = mCurrentFocus;
10550 mCurrentFocus = newFocus;
10551 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 final WindowState imWindow = mInputMethodWindow;
10554 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010555 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010557 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10558 mLayoutNeeded = true;
10559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10561 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010562 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10563 // Client will do the layout, but we need to assign layers
10564 // for handleNewWindowLocked() below.
10565 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 }
10567 }
Romain Guy06882f82009-06-10 13:36:04 -070010568
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010569 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10570 mKeyWaiter.handleNewWindowLocked(newFocus);
10571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572 return true;
10573 }
10574 return false;
10575 }
10576
10577 private WindowState computeFocusedWindowLocked() {
10578 WindowState result = null;
10579 WindowState win;
10580
10581 int i = mWindows.size() - 1;
10582 int nextAppIndex = mAppTokens.size()-1;
10583 WindowToken nextApp = nextAppIndex >= 0
10584 ? mAppTokens.get(nextAppIndex) : null;
10585
10586 while (i >= 0) {
10587 win = (WindowState)mWindows.get(i);
10588
10589 if (localLOGV || DEBUG_FOCUS) Log.v(
10590 TAG, "Looking for focus: " + i
10591 + " = " + win
10592 + ", flags=" + win.mAttrs.flags
10593 + ", canReceive=" + win.canReceiveKeys());
10594
10595 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010597 // If this window's application has been removed, just skip it.
10598 if (thisApp != null && thisApp.removed) {
10599 i--;
10600 continue;
10601 }
Romain Guy06882f82009-06-10 13:36:04 -070010602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010603 // If there is a focused app, don't allow focus to go to any
10604 // windows below it. If this is an application window, step
10605 // through the app tokens until we find its app.
10606 if (thisApp != null && nextApp != null && thisApp != nextApp
10607 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10608 int origAppIndex = nextAppIndex;
10609 while (nextAppIndex > 0) {
10610 if (nextApp == mFocusedApp) {
10611 // Whoops, we are below the focused app... no focus
10612 // for you!
10613 if (localLOGV || DEBUG_FOCUS) Log.v(
10614 TAG, "Reached focused app: " + mFocusedApp);
10615 return null;
10616 }
10617 nextAppIndex--;
10618 nextApp = mAppTokens.get(nextAppIndex);
10619 if (nextApp == thisApp) {
10620 break;
10621 }
10622 }
10623 if (thisApp != nextApp) {
10624 // Uh oh, the app token doesn't exist! This shouldn't
10625 // happen, but if it does we can get totally hosed...
10626 // so restart at the original app.
10627 nextAppIndex = origAppIndex;
10628 nextApp = mAppTokens.get(nextAppIndex);
10629 }
10630 }
10631
10632 // Dispatch to this window if it is wants key events.
10633 if (win.canReceiveKeys()) {
10634 if (DEBUG_FOCUS) Log.v(
10635 TAG, "Found focus @ " + i + " = " + win);
10636 result = win;
10637 break;
10638 }
10639
10640 i--;
10641 }
10642
10643 return result;
10644 }
10645
10646 private void startFreezingDisplayLocked() {
10647 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010648 // Freezing the display also suspends key event delivery, to
10649 // keep events from going astray while the display is reconfigured.
10650 // If someone has changed orientation again while the screen is
10651 // still frozen, the events will continue to be blocked while the
10652 // successive orientation change is processed. To prevent spurious
10653 // ANRs, we reset the event dispatch timeout in this case.
10654 synchronized (mKeyWaiter) {
10655 mKeyWaiter.mWasFrozen = true;
10656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010657 return;
10658 }
Romain Guy06882f82009-06-10 13:36:04 -070010659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010660 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010662 long now = SystemClock.uptimeMillis();
10663 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10664 if (mFreezeGcPending != 0) {
10665 if (now > (mFreezeGcPending+1000)) {
10666 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10667 mH.removeMessages(H.FORCE_GC);
10668 Runtime.getRuntime().gc();
10669 mFreezeGcPending = now;
10670 }
10671 } else {
10672 mFreezeGcPending = now;
10673 }
Romain Guy06882f82009-06-10 13:36:04 -070010674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010676 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10677 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010678 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010679 mAppTransitionReady = true;
10680 }
Romain Guy06882f82009-06-10 13:36:04 -070010681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010682 if (PROFILE_ORIENTATION) {
10683 File file = new File("/data/system/frozen");
10684 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10685 }
10686 Surface.freezeDisplay(0);
10687 }
Romain Guy06882f82009-06-10 13:36:04 -070010688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 private void stopFreezingDisplayLocked() {
10690 if (!mDisplayFrozen) {
10691 return;
10692 }
Romain Guy06882f82009-06-10 13:36:04 -070010693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010694 mDisplayFrozen = false;
10695 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10696 if (PROFILE_ORIENTATION) {
10697 Debug.stopMethodTracing();
10698 }
10699 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010700
Chris Tate2ad63a92009-03-25 17:36:48 -070010701 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10702 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010703 synchronized (mKeyWaiter) {
10704 mKeyWaiter.mWasFrozen = true;
10705 mKeyWaiter.notifyAll();
10706 }
10707
10708 // A little kludge: a lot could have happened while the
10709 // display was frozen, so now that we are coming back we
10710 // do a gc so that any remote references the system
10711 // processes holds on others can be released if they are
10712 // no longer needed.
10713 mH.removeMessages(H.FORCE_GC);
10714 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10715 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010717 mScreenFrozenLock.release();
10718 }
Romain Guy06882f82009-06-10 13:36:04 -070010719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010720 @Override
10721 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10722 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10723 != PackageManager.PERMISSION_GRANTED) {
10724 pw.println("Permission Denial: can't dump WindowManager from from pid="
10725 + Binder.getCallingPid()
10726 + ", uid=" + Binder.getCallingUid());
10727 return;
10728 }
Romain Guy06882f82009-06-10 13:36:04 -070010729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010730 synchronized(mWindowMap) {
10731 pw.println("Current Window Manager state:");
10732 for (int i=mWindows.size()-1; i>=0; i--) {
10733 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010734 pw.print(" Window #"); pw.print(i); pw.print(' ');
10735 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010736 w.dump(pw, " ");
10737 }
10738 if (mInputMethodDialogs.size() > 0) {
10739 pw.println(" ");
10740 pw.println(" Input method dialogs:");
10741 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10742 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010743 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010744 }
10745 }
10746 if (mPendingRemove.size() > 0) {
10747 pw.println(" ");
10748 pw.println(" Remove pending for:");
10749 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10750 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010751 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10752 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010753 w.dump(pw, " ");
10754 }
10755 }
10756 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10757 pw.println(" ");
10758 pw.println(" Windows force removing:");
10759 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10760 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010761 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10762 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010763 w.dump(pw, " ");
10764 }
10765 }
10766 if (mDestroySurface.size() > 0) {
10767 pw.println(" ");
10768 pw.println(" Windows waiting to destroy their surface:");
10769 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10770 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010771 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10772 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010773 w.dump(pw, " ");
10774 }
10775 }
10776 if (mLosingFocus.size() > 0) {
10777 pw.println(" ");
10778 pw.println(" Windows losing focus:");
10779 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10780 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010781 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10782 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010783 w.dump(pw, " ");
10784 }
10785 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010786 if (mResizingWindows.size() > 0) {
10787 pw.println(" ");
10788 pw.println(" Windows waiting to resize:");
10789 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10790 WindowState w = mResizingWindows.get(i);
10791 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10792 pw.print(w); pw.println(":");
10793 w.dump(pw, " ");
10794 }
10795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010796 if (mSessions.size() > 0) {
10797 pw.println(" ");
10798 pw.println(" All active sessions:");
10799 Iterator<Session> it = mSessions.iterator();
10800 while (it.hasNext()) {
10801 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010802 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010803 s.dump(pw, " ");
10804 }
10805 }
10806 if (mTokenMap.size() > 0) {
10807 pw.println(" ");
10808 pw.println(" All tokens:");
10809 Iterator<WindowToken> it = mTokenMap.values().iterator();
10810 while (it.hasNext()) {
10811 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010812 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010813 token.dump(pw, " ");
10814 }
10815 }
10816 if (mTokenList.size() > 0) {
10817 pw.println(" ");
10818 pw.println(" Window token list:");
10819 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010820 pw.print(" #"); pw.print(i); pw.print(": ");
10821 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010822 }
10823 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010824 if (mWallpaperTokens.size() > 0) {
10825 pw.println(" ");
10826 pw.println(" Wallpaper tokens:");
10827 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10828 WindowToken token = mWallpaperTokens.get(i);
10829 pw.print(" Wallpaper #"); pw.print(i);
10830 pw.print(' '); pw.print(token); pw.println(':');
10831 token.dump(pw, " ");
10832 }
10833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010834 if (mAppTokens.size() > 0) {
10835 pw.println(" ");
10836 pw.println(" Application tokens in Z order:");
10837 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010838 pw.print(" App #"); pw.print(i); pw.print(": ");
10839 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010840 }
10841 }
10842 if (mFinishedStarting.size() > 0) {
10843 pw.println(" ");
10844 pw.println(" Finishing start of application tokens:");
10845 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10846 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010847 pw.print(" Finished Starting #"); pw.print(i);
10848 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010849 token.dump(pw, " ");
10850 }
10851 }
10852 if (mExitingTokens.size() > 0) {
10853 pw.println(" ");
10854 pw.println(" Exiting tokens:");
10855 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10856 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010857 pw.print(" Exiting #"); pw.print(i);
10858 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859 token.dump(pw, " ");
10860 }
10861 }
10862 if (mExitingAppTokens.size() > 0) {
10863 pw.println(" ");
10864 pw.println(" Exiting application tokens:");
10865 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10866 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010867 pw.print(" Exiting App #"); pw.print(i);
10868 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 token.dump(pw, " ");
10870 }
10871 }
10872 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010873 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10874 pw.print(" mLastFocus="); pw.println(mLastFocus);
10875 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10876 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10877 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010878 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010879 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10880 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10881 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10882 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010883 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10884 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10885 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10886 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10887 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010888 if (mDimAnimator != null) {
10889 mDimAnimator.printTo(pw);
10890 } else {
10891 pw.print( " no DimAnimator ");
10892 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010893 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010894 pw.print(mInputMethodAnimLayerAdjustment);
10895 pw.print(" mWallpaperAnimLayerAdjustment=");
10896 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010897 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10898 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010899 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10900 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10901 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10902 pw.print(" mRotation="); pw.print(mRotation);
10903 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10904 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10905 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10906 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10907 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10908 pw.print(" mNextAppTransition=0x");
10909 pw.print(Integer.toHexString(mNextAppTransition));
10910 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010911 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010912 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010913 if (mNextAppTransitionPackage != null) {
10914 pw.print(" mNextAppTransitionPackage=");
10915 pw.print(mNextAppTransitionPackage);
10916 pw.print(", mNextAppTransitionEnter=0x");
10917 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10918 pw.print(", mNextAppTransitionExit=0x");
10919 pw.print(Integer.toHexString(mNextAppTransitionExit));
10920 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010921 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10922 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010923 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10924 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10925 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10926 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010927 if (mOpeningApps.size() > 0) {
10928 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10929 }
10930 if (mClosingApps.size() > 0) {
10931 pw.print(" mClosingApps="); pw.println(mClosingApps);
10932 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010933 if (mToTopApps.size() > 0) {
10934 pw.print(" mToTopApps="); pw.println(mToTopApps);
10935 }
10936 if (mToBottomApps.size() > 0) {
10937 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10938 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010939 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10940 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010941 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010942 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10943 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10944 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10945 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10946 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10947 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010948 }
10949 }
10950
10951 public void monitor() {
10952 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050010953 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010954 synchronized (mKeyWaiter) { }
10955 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010956
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010957 public void virtualKeyFeedback(KeyEvent event) {
10958 mPolicy.keyFeedbackFromInput(event);
10959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010960
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010961 /**
10962 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010963 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010964 */
10965 private static class DimAnimator {
10966 Surface mDimSurface;
10967 boolean mDimShown = false;
10968 float mDimCurrentAlpha;
10969 float mDimTargetAlpha;
10970 float mDimDeltaPerMs;
10971 long mLastDimAnimTime;
10972
10973 DimAnimator (SurfaceSession session) {
10974 if (mDimSurface == null) {
10975 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10976 + mDimSurface + ": CREATE");
10977 try {
10978 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10979 Surface.FX_SURFACE_DIM);
10980 } catch (Exception e) {
10981 Log.e(TAG, "Exception creating Dim surface", e);
10982 }
10983 }
10984 }
10985
10986 /**
10987 * Show the dim surface.
10988 */
10989 void show(int dw, int dh) {
10990 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10991 dw + "x" + dh + ")");
10992 mDimShown = true;
10993 try {
10994 mDimSurface.setPosition(0, 0);
10995 mDimSurface.setSize(dw, dh);
10996 mDimSurface.show();
10997 } catch (RuntimeException e) {
10998 Log.w(TAG, "Failure showing dim surface", e);
10999 }
11000 }
11001
11002 /**
11003 * Set's the dim surface's layer and update dim parameters that will be used in
11004 * {@link updateSurface} after all windows are examined.
11005 */
11006 void updateParameters(WindowState w, long currentTime) {
11007 mDimSurface.setLayer(w.mAnimLayer-1);
11008
11009 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011010 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11011 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011012 if (mDimTargetAlpha != target) {
11013 // If the desired dim level has changed, then
11014 // start an animation to it.
11015 mLastDimAnimTime = currentTime;
11016 long duration = (w.mAnimating && w.mAnimation != null)
11017 ? w.mAnimation.computeDurationHint()
11018 : DEFAULT_DIM_DURATION;
11019 if (target > mDimTargetAlpha) {
11020 // This is happening behind the activity UI,
11021 // so we can make it run a little longer to
11022 // give a stronger impression without disrupting
11023 // the user.
11024 duration *= DIM_DURATION_MULTIPLIER;
11025 }
11026 if (duration < 1) {
11027 // Don't divide by zero
11028 duration = 1;
11029 }
11030 mDimTargetAlpha = target;
11031 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11032 }
11033 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011034
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011035 /**
11036 * Updating the surface's alpha. Returns true if the animation continues, or returns
11037 * false when the animation is finished and the dim surface is hidden.
11038 */
11039 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11040 if (!dimming) {
11041 if (mDimTargetAlpha != 0) {
11042 mLastDimAnimTime = currentTime;
11043 mDimTargetAlpha = 0;
11044 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11045 }
11046 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011047
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011048 boolean animating = false;
11049 if (mLastDimAnimTime != 0) {
11050 mDimCurrentAlpha += mDimDeltaPerMs
11051 * (currentTime-mLastDimAnimTime);
11052 boolean more = true;
11053 if (displayFrozen) {
11054 // If the display is frozen, there is no reason to animate.
11055 more = false;
11056 } else if (mDimDeltaPerMs > 0) {
11057 if (mDimCurrentAlpha > mDimTargetAlpha) {
11058 more = false;
11059 }
11060 } else if (mDimDeltaPerMs < 0) {
11061 if (mDimCurrentAlpha < mDimTargetAlpha) {
11062 more = false;
11063 }
11064 } else {
11065 more = false;
11066 }
11067
11068 // Do we need to continue animating?
11069 if (more) {
11070 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11071 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11072 mLastDimAnimTime = currentTime;
11073 mDimSurface.setAlpha(mDimCurrentAlpha);
11074 animating = true;
11075 } else {
11076 mDimCurrentAlpha = mDimTargetAlpha;
11077 mLastDimAnimTime = 0;
11078 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11079 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11080 mDimSurface.setAlpha(mDimCurrentAlpha);
11081 if (!dimming) {
11082 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11083 + ": HIDE");
11084 try {
11085 mDimSurface.hide();
11086 } catch (RuntimeException e) {
11087 Log.w(TAG, "Illegal argument exception hiding dim surface");
11088 }
11089 mDimShown = false;
11090 }
11091 }
11092 }
11093 return animating;
11094 }
11095
11096 public void printTo(PrintWriter pw) {
11097 pw.print(" mDimShown="); pw.print(mDimShown);
11098 pw.print(" current="); pw.print(mDimCurrentAlpha);
11099 pw.print(" target="); pw.print(mDimTargetAlpha);
11100 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11101 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11102 }
11103 }
11104
11105 /**
11106 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11107 * This is used for opening/closing transition for apps in compatible mode.
11108 */
11109 private static class FadeInOutAnimation extends Animation {
11110 int mWidth;
11111 boolean mFadeIn;
11112
11113 public FadeInOutAnimation(boolean fadeIn) {
11114 setInterpolator(new AccelerateInterpolator());
11115 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11116 mFadeIn = fadeIn;
11117 }
11118
11119 @Override
11120 protected void applyTransformation(float interpolatedTime, Transformation t) {
11121 float x = interpolatedTime;
11122 if (!mFadeIn) {
11123 x = 1.0f - x; // reverse the interpolation for fade out
11124 }
11125 if (x < 0.5) {
11126 // move the window out of the screen.
11127 t.getMatrix().setTranslate(mWidth, 0);
11128 } else {
11129 t.getMatrix().setTranslate(0, 0);// show
11130 t.setAlpha((x - 0.5f) * 2);
11131 }
11132 }
11133
11134 @Override
11135 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11136 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11137 mWidth = width;
11138 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011139
11140 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011141 public int getZAdjustment() {
11142 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011143 }
11144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011145}