blob: 7fcf900231c6c05a33d598edaff24c24d4f95a09 [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
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001776 void dispatchPointerElsewhereLocked(WindowState srcWin, WindowState relWin,
1777 MotionEvent pointer, long eventTime, boolean skipped) {
1778 if (relWin != null) {
1779 mPolicy.dispatchedPointerEventLw(pointer, relWin.mFrame.left, relWin.mFrame.top);
1780 } else {
1781 mPolicy.dispatchedPointerEventLw(pointer, 0, 0);
1782 }
1783
1784 // If we sent an initial down to the wallpaper, then continue
1785 // sending events until the final up.
1786 if (mSendingPointersToWallpaper) {
1787 if (skipped) {
1788 Log.i(TAG, "Sending skipped pointer to wallpaper!");
1789 }
1790 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1791
1792 // If we are on top of the wallpaper, then the wallpaper also
1793 // gets to see this movement.
1794 } else if (srcWin != null
1795 && pointer.getAction() == MotionEvent.ACTION_DOWN
1796 && mWallpaperTarget == srcWin
1797 && srcWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
1798 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1799 }
1800 }
1801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 public int addWindow(Session session, IWindow client,
1803 WindowManager.LayoutParams attrs, int viewVisibility,
1804 Rect outContentInsets) {
1805 int res = mPolicy.checkAddPermission(attrs);
1806 if (res != WindowManagerImpl.ADD_OKAY) {
1807 return res;
1808 }
Romain Guy06882f82009-06-10 13:36:04 -07001809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 boolean reportNewConfig = false;
1811 WindowState attachedWindow = null;
1812 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 synchronized(mWindowMap) {
1815 // Instantiating a Display requires talking with the simulator,
1816 // so don't do it until we know the system is mostly up and
1817 // running.
1818 if (mDisplay == null) {
1819 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1820 mDisplay = wm.getDefaultDisplay();
1821 mQueue.setDisplay(mDisplay);
1822 reportNewConfig = true;
1823 }
Romain Guy06882f82009-06-10 13:36:04 -07001824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 if (mWindowMap.containsKey(client.asBinder())) {
1826 Log.w(TAG, "Window " + client + " is already added");
1827 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1828 }
1829
1830 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001831 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 if (attachedWindow == null) {
1833 Log.w(TAG, "Attempted to add window with token that is not a window: "
1834 + attrs.token + ". Aborting.");
1835 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1836 }
1837 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1838 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1839 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1840 + attrs.token + ". Aborting.");
1841 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1842 }
1843 }
1844
1845 boolean addToken = false;
1846 WindowToken token = mTokenMap.get(attrs.token);
1847 if (token == null) {
1848 if (attrs.type >= FIRST_APPLICATION_WINDOW
1849 && attrs.type <= LAST_APPLICATION_WINDOW) {
1850 Log.w(TAG, "Attempted to add application window with unknown token "
1851 + attrs.token + ". Aborting.");
1852 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1853 }
1854 if (attrs.type == TYPE_INPUT_METHOD) {
1855 Log.w(TAG, "Attempted to add input method window with unknown token "
1856 + attrs.token + ". Aborting.");
1857 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1858 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001859 if (attrs.type == TYPE_WALLPAPER) {
1860 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1861 + attrs.token + ". Aborting.");
1862 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 token = new WindowToken(attrs.token, -1, false);
1865 addToken = true;
1866 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1867 && attrs.type <= LAST_APPLICATION_WINDOW) {
1868 AppWindowToken atoken = token.appWindowToken;
1869 if (atoken == null) {
1870 Log.w(TAG, "Attempted to add window with non-application token "
1871 + token + ". Aborting.");
1872 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1873 } else if (atoken.removed) {
1874 Log.w(TAG, "Attempted to add window with exiting application token "
1875 + token + ". Aborting.");
1876 return WindowManagerImpl.ADD_APP_EXITING;
1877 }
1878 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1879 // No need for this guy!
1880 if (localLOGV) Log.v(
1881 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1882 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1883 }
1884 } else if (attrs.type == TYPE_INPUT_METHOD) {
1885 if (token.windowType != TYPE_INPUT_METHOD) {
1886 Log.w(TAG, "Attempted to add input method window with bad token "
1887 + attrs.token + ". Aborting.");
1888 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1889 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001890 } else if (attrs.type == TYPE_WALLPAPER) {
1891 if (token.windowType != TYPE_WALLPAPER) {
1892 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1893 + attrs.token + ". Aborting.");
1894 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 }
1897
1898 win = new WindowState(session, client, token,
1899 attachedWindow, attrs, viewVisibility);
1900 if (win.mDeathRecipient == null) {
1901 // Client has apparently died, so there is no reason to
1902 // continue.
1903 Log.w(TAG, "Adding window client " + client.asBinder()
1904 + " that is dead, aborting.");
1905 return WindowManagerImpl.ADD_APP_EXITING;
1906 }
1907
1908 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 res = mPolicy.prepareAddWindowLw(win, attrs);
1911 if (res != WindowManagerImpl.ADD_OKAY) {
1912 return res;
1913 }
1914
1915 // From now on, no exceptions or errors allowed!
1916
1917 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 if (addToken) {
1922 mTokenMap.put(attrs.token, token);
1923 mTokenList.add(token);
1924 }
1925 win.attach();
1926 mWindowMap.put(client.asBinder(), win);
1927
1928 if (attrs.type == TYPE_APPLICATION_STARTING &&
1929 token.appWindowToken != null) {
1930 token.appWindowToken.startingWindow = win;
1931 }
1932
1933 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 if (attrs.type == TYPE_INPUT_METHOD) {
1936 mInputMethodWindow = win;
1937 addInputMethodWindowToListLocked(win);
1938 imMayMove = false;
1939 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1940 mInputMethodDialogs.add(win);
1941 addWindowToListInOrderLocked(win, true);
1942 adjustInputMethodDialogsLocked();
1943 imMayMove = false;
1944 } else {
1945 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001946 if (attrs.type == TYPE_WALLPAPER) {
1947 mLastWallpaperTimeoutTime = 0;
1948 adjustWallpaperWindowsLocked();
1949 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001950 adjustWallpaperWindowsLocked();
1951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
Romain Guy06882f82009-06-10 13:36:04 -07001953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 if (mInTouchMode) {
1959 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1960 }
1961 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1962 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1963 }
Romain Guy06882f82009-06-10 13:36:04 -07001964
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001965 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001967 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1968 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 imMayMove = false;
1970 }
1971 }
Romain Guy06882f82009-06-10 13:36:04 -07001972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001974 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
Romain Guy06882f82009-06-10 13:36:04 -07001976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 assignLayersLocked();
1978 // Don't do layout here, the window must call
1979 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 //dump();
1982
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001983 if (focusChanged) {
1984 if (mCurrentFocus != null) {
1985 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1986 }
1987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 if (localLOGV) Log.v(
1989 TAG, "New client " + client.asBinder()
1990 + ": window=" + win);
1991 }
1992
1993 // sendNewConfiguration() checks caller permissions so we must call it with
1994 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1995 // identity anyway, so it's safe to just clear & restore around this whole
1996 // block.
1997 final long origId = Binder.clearCallingIdentity();
1998 if (reportNewConfig) {
1999 sendNewConfiguration();
2000 } else {
2001 // Update Orientation after adding a window, only if the window needs to be
2002 // displayed right away
2003 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002004 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 sendNewConfiguration();
2006 }
2007 }
2008 }
2009 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 return res;
2012 }
Romain Guy06882f82009-06-10 13:36:04 -07002013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 public void removeWindow(Session session, IWindow client) {
2015 synchronized(mWindowMap) {
2016 WindowState win = windowForClientLocked(session, client);
2017 if (win == null) {
2018 return;
2019 }
2020 removeWindowLocked(session, win);
2021 }
2022 }
Romain Guy06882f82009-06-10 13:36:04 -07002023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 public void removeWindowLocked(Session session, WindowState win) {
2025
2026 if (localLOGV || DEBUG_FOCUS) Log.v(
2027 TAG, "Remove " + win + " client="
2028 + Integer.toHexString(System.identityHashCode(
2029 win.mClient.asBinder()))
2030 + ", surface=" + win.mSurface);
2031
2032 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 if (DEBUG_APP_TRANSITIONS) Log.v(
2035 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2036 + " mExiting=" + win.mExiting
2037 + " isAnimating=" + win.isAnimating()
2038 + " app-animation="
2039 + (win.mAppToken != null ? win.mAppToken.animation : null)
2040 + " inPendingTransaction="
2041 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2042 + " mDisplayFrozen=" + mDisplayFrozen);
2043 // Visibility of the removed window. Will be used later to update orientation later on.
2044 boolean wasVisible = false;
2045 // First, see if we need to run an animation. If we do, we have
2046 // to hold off on removing the window until the animation is done.
2047 // If the display is frozen, just remove immediately, since the
2048 // animation wouldn't be seen.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002049 if (win.mSurface != null && !mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 // If we are not currently running the exit animation, we
2051 // need to see about starting one.
2052 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2055 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2056 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2057 }
2058 // Try starting an animation.
2059 if (applyAnimationLocked(win, transit, false)) {
2060 win.mExiting = true;
2061 }
2062 }
2063 if (win.mExiting || win.isAnimating()) {
2064 // The exit animation is running... wait for it!
2065 //Log.i(TAG, "*** Running exit animation...");
2066 win.mExiting = true;
2067 win.mRemoveOnExit = true;
2068 mLayoutNeeded = true;
2069 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2070 performLayoutAndPlaceSurfacesLocked();
2071 if (win.mAppToken != null) {
2072 win.mAppToken.updateReportedVisibilityLocked();
2073 }
2074 //dump();
2075 Binder.restoreCallingIdentity(origId);
2076 return;
2077 }
2078 }
2079
2080 removeWindowInnerLocked(session, win);
2081 // Removing a visible window will effect the computed orientation
2082 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002083 if (wasVisible && computeForcedAppOrientationLocked()
2084 != mForcedAppOrientation) {
2085 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
2087 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2088 Binder.restoreCallingIdentity(origId);
2089 }
Romain Guy06882f82009-06-10 13:36:04 -07002090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002092 mKeyWaiter.finishedKey(session, win.mClient, true,
2093 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2095 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 if (mInputMethodTarget == win) {
2100 moveInputMethodWindowsIfNeededLocked(false);
2101 }
Romain Guy06882f82009-06-10 13:36:04 -07002102
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002103 if (false) {
2104 RuntimeException e = new RuntimeException("here");
2105 e.fillInStackTrace();
2106 Log.w(TAG, "Removing window " + win, e);
2107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 mPolicy.removeWindowLw(win);
2110 win.removeLocked();
2111
2112 mWindowMap.remove(win.mClient.asBinder());
2113 mWindows.remove(win);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002114 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115
2116 if (mInputMethodWindow == win) {
2117 mInputMethodWindow = null;
2118 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2119 mInputMethodDialogs.remove(win);
2120 }
Romain Guy06882f82009-06-10 13:36:04 -07002121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 final WindowToken token = win.mToken;
2123 final AppWindowToken atoken = win.mAppToken;
2124 token.windows.remove(win);
2125 if (atoken != null) {
2126 atoken.allAppWindows.remove(win);
2127 }
2128 if (localLOGV) Log.v(
2129 TAG, "**** Removing window " + win + ": count="
2130 + token.windows.size());
2131 if (token.windows.size() == 0) {
2132 if (!token.explicit) {
2133 mTokenMap.remove(token.token);
2134 mTokenList.remove(token);
2135 } else if (atoken != null) {
2136 atoken.firstWindowDrawn = false;
2137 }
2138 }
2139
2140 if (atoken != null) {
2141 if (atoken.startingWindow == win) {
2142 atoken.startingWindow = null;
2143 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2144 // If this is the last window and we had requested a starting
2145 // transition window, well there is no point now.
2146 atoken.startingData = null;
2147 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2148 // If this is the last window except for a starting transition
2149 // window, we need to get rid of the starting transition.
2150 if (DEBUG_STARTING_WINDOW) {
2151 Log.v(TAG, "Schedule remove starting " + token
2152 + ": no more real windows");
2153 }
2154 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2155 mH.sendMessage(m);
2156 }
2157 }
Romain Guy06882f82009-06-10 13:36:04 -07002158
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002159 if (win.mAttrs.type == TYPE_WALLPAPER) {
2160 mLastWallpaperTimeoutTime = 0;
2161 adjustWallpaperWindowsLocked();
2162 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002163 adjustWallpaperWindowsLocked();
2164 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 if (!mInLayout) {
2167 assignLayersLocked();
2168 mLayoutNeeded = true;
2169 performLayoutAndPlaceSurfacesLocked();
2170 if (win.mAppToken != null) {
2171 win.mAppToken.updateReportedVisibilityLocked();
2172 }
2173 }
2174 }
2175
2176 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2177 long origId = Binder.clearCallingIdentity();
2178 try {
2179 synchronized (mWindowMap) {
2180 WindowState w = windowForClientLocked(session, client);
2181 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002182 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 Surface.openTransaction();
2184 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002185 if (SHOW_TRANSACTIONS) Log.i(
2186 TAG, " SURFACE " + w.mSurface
2187 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 w.mSurface.setTransparentRegionHint(region);
2189 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002190 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 Surface.closeTransaction();
2192 }
2193 }
2194 }
2195 } finally {
2196 Binder.restoreCallingIdentity(origId);
2197 }
2198 }
2199
2200 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002201 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 Rect visibleInsets) {
2203 long origId = Binder.clearCallingIdentity();
2204 try {
2205 synchronized (mWindowMap) {
2206 WindowState w = windowForClientLocked(session, client);
2207 if (w != null) {
2208 w.mGivenInsetsPending = false;
2209 w.mGivenContentInsets.set(contentInsets);
2210 w.mGivenVisibleInsets.set(visibleInsets);
2211 w.mTouchableInsets = touchableInsets;
2212 mLayoutNeeded = true;
2213 performLayoutAndPlaceSurfacesLocked();
2214 }
2215 }
2216 } finally {
2217 Binder.restoreCallingIdentity(origId);
2218 }
2219 }
Romain Guy06882f82009-06-10 13:36:04 -07002220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 public void getWindowDisplayFrame(Session session, IWindow client,
2222 Rect outDisplayFrame) {
2223 synchronized(mWindowMap) {
2224 WindowState win = windowForClientLocked(session, client);
2225 if (win == null) {
2226 outDisplayFrame.setEmpty();
2227 return;
2228 }
2229 outDisplayFrame.set(win.mDisplayFrame);
2230 }
2231 }
2232
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002233 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2234 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002235 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2236 window.mWallpaperX = x;
2237 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002238 window.mWallpaperXStep = xStep;
2239 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002240 if (updateWallpaperOffsetLocked(window, true)) {
2241 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002242 }
2243 }
2244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002245
Dianne Hackborn75804932009-10-20 20:15:20 -07002246 void wallpaperCommandComplete(IBinder window, Bundle result) {
2247 synchronized (mWindowMap) {
2248 if (mWaitingOnWallpaper != null &&
2249 mWaitingOnWallpaper.mClient.asBinder() == window) {
2250 mWaitingOnWallpaper = null;
2251 mWindowMap.notifyAll();
2252 }
2253 }
2254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002255
Dianne Hackborn75804932009-10-20 20:15:20 -07002256 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2257 String action, int x, int y, int z, Bundle extras, boolean sync) {
2258 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2259 || window == mUpperWallpaperTarget) {
2260 boolean doWait = sync;
2261 int curTokenIndex = mWallpaperTokens.size();
2262 while (curTokenIndex > 0) {
2263 curTokenIndex--;
2264 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2265 int curWallpaperIndex = token.windows.size();
2266 while (curWallpaperIndex > 0) {
2267 curWallpaperIndex--;
2268 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2269 try {
2270 wallpaper.mClient.dispatchWallpaperCommand(action,
2271 x, y, z, extras, sync);
2272 // We only want to be synchronous with one wallpaper.
2273 sync = false;
2274 } catch (RemoteException e) {
2275 }
2276 }
2277 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002278
Dianne Hackborn75804932009-10-20 20:15:20 -07002279 if (doWait) {
2280 // XXX Need to wait for result.
2281 }
2282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002283
Dianne Hackborn75804932009-10-20 20:15:20 -07002284 return null;
2285 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 public int relayoutWindow(Session session, IWindow client,
2288 WindowManager.LayoutParams attrs, int requestedWidth,
2289 int requestedHeight, int viewVisibility, boolean insetsPending,
2290 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2291 Surface outSurface) {
2292 boolean displayed = false;
2293 boolean inTouchMode;
2294 Configuration newConfig = null;
2295 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 synchronized(mWindowMap) {
2298 WindowState win = windowForClientLocked(session, client);
2299 if (win == null) {
2300 return 0;
2301 }
2302 win.mRequestedWidth = requestedWidth;
2303 win.mRequestedHeight = requestedHeight;
2304
2305 if (attrs != null) {
2306 mPolicy.adjustWindowParamsLw(attrs);
2307 }
Romain Guy06882f82009-06-10 13:36:04 -07002308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 int attrChanges = 0;
2310 int flagChanges = 0;
2311 if (attrs != null) {
2312 flagChanges = win.mAttrs.flags ^= attrs.flags;
2313 attrChanges = win.mAttrs.copyFrom(attrs);
2314 }
2315
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002316 if (DEBUG_LAYOUT) Log.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317
2318 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2319 win.mAlpha = attrs.alpha;
2320 }
2321
2322 final boolean scaledWindow =
2323 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2324
2325 if (scaledWindow) {
2326 // requested{Width|Height} Surface's physical size
2327 // attrs.{width|height} Size on screen
2328 win.mHScale = (attrs.width != requestedWidth) ?
2329 (attrs.width / (float)requestedWidth) : 1.0f;
2330 win.mVScale = (attrs.height != requestedHeight) ?
2331 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002332 } else {
2333 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 }
2335
2336 boolean imMayMove = (flagChanges&(
2337 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2338 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 boolean focusMayChange = win.mViewVisibility != viewVisibility
2341 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2342 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002343
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002344 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2345 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 win.mRelayoutCalled = true;
2348 final int oldVisibility = win.mViewVisibility;
2349 win.mViewVisibility = viewVisibility;
2350 if (viewVisibility == View.VISIBLE &&
2351 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2352 displayed = !win.isVisibleLw();
2353 if (win.mExiting) {
2354 win.mExiting = false;
2355 win.mAnimation = null;
2356 }
2357 if (win.mDestroying) {
2358 win.mDestroying = false;
2359 mDestroySurface.remove(win);
2360 }
2361 if (oldVisibility == View.GONE) {
2362 win.mEnterAnimationPending = true;
2363 }
2364 if (displayed && win.mSurface != null && !win.mDrawPending
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002365 && !win.mCommitDrawPending && !mDisplayFrozen
2366 && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 applyEnterAnimationLocked(win);
2368 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002369 if (displayed && (win.mAttrs.flags
2370 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2371 win.mTurnOnScreen = true;
2372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2374 // To change the format, we need to re-build the surface.
2375 win.destroySurfaceLocked();
2376 displayed = true;
2377 }
2378 try {
2379 Surface surface = win.createSurfaceLocked();
2380 if (surface != null) {
2381 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002382 win.mReportDestroySurface = false;
2383 win.mSurfacePendingDestroy = false;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002384 if (SHOW_TRANSACTIONS) Log.i(TAG,
2385 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002387 // For some reason there isn't a surface. Clear the
2388 // caller's object so they see the same state.
2389 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 }
2391 } catch (Exception e) {
2392 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002393 + client + " (" + win.mAttrs.getTitle() + ")",
2394 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 Binder.restoreCallingIdentity(origId);
2396 return 0;
2397 }
2398 if (displayed) {
2399 focusMayChange = true;
2400 }
2401 if (win.mAttrs.type == TYPE_INPUT_METHOD
2402 && mInputMethodWindow == null) {
2403 mInputMethodWindow = win;
2404 imMayMove = true;
2405 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002406 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2407 && win.mAppToken != null
2408 && win.mAppToken.startingWindow != null) {
2409 // Special handling of starting window over the base
2410 // window of the app: propagate lock screen flags to it,
2411 // to provide the correct semantics while starting.
2412 final int mask =
2413 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Mike Lockwoodef731622010-01-27 17:51:34 -05002414 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
2415 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
Dianne Hackborn558947c2009-12-18 16:02:50 -08002416 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2417 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 } else {
2420 win.mEnterAnimationPending = false;
2421 if (win.mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002422 if (DEBUG_VISIBILITY) Log.i(TAG, "Relayout invis " + win
2423 + ": mExiting=" + win.mExiting
2424 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 // If we are not currently running the exit animation, we
2426 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002427 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 // Try starting an animation; if there isn't one, we
2429 // can destroy the surface right away.
2430 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2431 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2432 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2433 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002434 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002436 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 win.mExiting = true;
2438 mKeyWaiter.finishedKey(session, client, true,
2439 KeyWaiter.RETURN_NOTHING);
2440 } else if (win.isAnimating()) {
2441 // Currently in a hide animation... turn this into
2442 // an exit.
2443 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002444 } else if (win == mWallpaperTarget) {
2445 // If the wallpaper is currently behind this
2446 // window, we need to change both of them inside
2447 // of a transaction to avoid artifacts.
2448 win.mExiting = true;
2449 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 } else {
2451 if (mInputMethodWindow == win) {
2452 mInputMethodWindow = null;
2453 }
2454 win.destroySurfaceLocked();
2455 }
2456 }
2457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002458
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002459 if (win.mSurface == null || (win.getAttrs().flags
2460 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2461 || win.mSurfacePendingDestroy) {
2462 // We are being called from a local process, which
2463 // means outSurface holds its current surface. Ensure the
2464 // surface object is cleared, but we don't want it actually
2465 // destroyed at this point.
2466 win.mSurfacePendingDestroy = false;
2467 outSurface.release();
2468 if (DEBUG_VISIBILITY) Log.i(TAG, "Releasing surface in: " + win);
2469 } else if (win.mSurface != null) {
2470 if (DEBUG_VISIBILITY) Log.i(TAG,
2471 "Keeping surface, will report destroy: " + win);
2472 win.mReportDestroySurface = true;
2473 outSurface.copyFrom(win.mSurface);
2474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 }
2476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 if (focusMayChange) {
2478 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2479 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 imMayMove = false;
2481 }
2482 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2483 }
Romain Guy06882f82009-06-10 13:36:04 -07002484
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002485 // updateFocusedWindowLocked() already assigned layers so we only need to
2486 // reassign them at this point if the IM window state gets shuffled
2487 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002490 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2491 // Little hack here -- we -should- be able to rely on the
2492 // function to return true if the IME has moved and needs
2493 // its layer recomputed. However, if the IME was hidden
2494 // and isn't actually moved in the list, its layer may be
2495 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 assignLayers = true;
2497 }
2498 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002499 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002500 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002501 assignLayers = true;
2502 }
2503 }
Romain Guy06882f82009-06-10 13:36:04 -07002504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 mLayoutNeeded = true;
2506 win.mGivenInsetsPending = insetsPending;
2507 if (assignLayers) {
2508 assignLayersLocked();
2509 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002510 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002512 if (displayed && win.mIsWallpaper) {
2513 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002514 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 if (win.mAppToken != null) {
2517 win.mAppToken.updateReportedVisibilityLocked();
2518 }
2519 outFrame.set(win.mFrame);
2520 outContentInsets.set(win.mContentInsets);
2521 outVisibleInsets.set(win.mVisibleInsets);
2522 if (localLOGV) Log.v(
2523 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002524 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 + ", requestedHeight=" + requestedHeight
2526 + ", viewVisibility=" + viewVisibility
2527 + "\nRelayout returning frame=" + outFrame
2528 + ", surface=" + outSurface);
2529
2530 if (localLOGV || DEBUG_FOCUS) Log.v(
2531 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2532
2533 inTouchMode = mInTouchMode;
2534 }
2535
2536 if (newConfig != null) {
2537 sendNewConfiguration();
2538 }
Romain Guy06882f82009-06-10 13:36:04 -07002539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2543 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2544 }
2545
2546 public void finishDrawingWindow(Session session, IWindow client) {
2547 final long origId = Binder.clearCallingIdentity();
2548 synchronized(mWindowMap) {
2549 WindowState win = windowForClientLocked(session, client);
2550 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002551 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2552 adjustWallpaperWindowsLocked();
2553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 mLayoutNeeded = true;
2555 performLayoutAndPlaceSurfacesLocked();
2556 }
2557 }
2558 Binder.restoreCallingIdentity(origId);
2559 }
2560
2561 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2562 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2563 + (lp != null ? lp.packageName : null)
2564 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2565 if (lp != null && lp.windowAnimations != 0) {
2566 // If this is a system resource, don't try to load it from the
2567 // application resources. It is nice to avoid loading application
2568 // resources if we can.
2569 String packageName = lp.packageName != null ? lp.packageName : "android";
2570 int resId = lp.windowAnimations;
2571 if ((resId&0xFF000000) == 0x01000000) {
2572 packageName = "android";
2573 }
2574 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2575 + packageName);
2576 return AttributeCache.instance().get(packageName, resId,
2577 com.android.internal.R.styleable.WindowAnimation);
2578 }
2579 return null;
2580 }
Romain Guy06882f82009-06-10 13:36:04 -07002581
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002582 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
2583 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2584 + packageName + " resId=0x" + Integer.toHexString(resId));
2585 if (packageName != null) {
2586 if ((resId&0xFF000000) == 0x01000000) {
2587 packageName = "android";
2588 }
2589 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2590 + packageName);
2591 return AttributeCache.instance().get(packageName, resId,
2592 com.android.internal.R.styleable.WindowAnimation);
2593 }
2594 return null;
2595 }
2596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 private void applyEnterAnimationLocked(WindowState win) {
2598 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2599 if (win.mEnterAnimationPending) {
2600 win.mEnterAnimationPending = false;
2601 transit = WindowManagerPolicy.TRANSIT_ENTER;
2602 }
2603
2604 applyAnimationLocked(win, transit, true);
2605 }
2606
2607 private boolean applyAnimationLocked(WindowState win,
2608 int transit, boolean isEntrance) {
2609 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2610 // If we are trying to apply an animation, but already running
2611 // an animation of the same type, then just leave that one alone.
2612 return true;
2613 }
Romain Guy06882f82009-06-10 13:36:04 -07002614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 // Only apply an animation if the display isn't frozen. If it is
2616 // frozen, there is no reason to animate and it can cause strange
2617 // artifacts when we unfreeze the display if some different animation
2618 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002619 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 int anim = mPolicy.selectAnimationLw(win, transit);
2621 int attr = -1;
2622 Animation a = null;
2623 if (anim != 0) {
2624 a = AnimationUtils.loadAnimation(mContext, anim);
2625 } else {
2626 switch (transit) {
2627 case WindowManagerPolicy.TRANSIT_ENTER:
2628 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2629 break;
2630 case WindowManagerPolicy.TRANSIT_EXIT:
2631 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2632 break;
2633 case WindowManagerPolicy.TRANSIT_SHOW:
2634 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2635 break;
2636 case WindowManagerPolicy.TRANSIT_HIDE:
2637 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2638 break;
2639 }
2640 if (attr >= 0) {
2641 a = loadAnimation(win.mAttrs, attr);
2642 }
2643 }
2644 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2645 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2646 + " mAnimation=" + win.mAnimation
2647 + " isEntrance=" + isEntrance);
2648 if (a != null) {
2649 if (DEBUG_ANIM) {
2650 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002651 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2653 }
2654 win.setAnimation(a);
2655 win.mAnimationIsEntrance = isEntrance;
2656 }
2657 } else {
2658 win.clearAnimation();
2659 }
2660
2661 return win.mAnimation != null;
2662 }
2663
2664 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2665 int anim = 0;
2666 Context context = mContext;
2667 if (animAttr >= 0) {
2668 AttributeCache.Entry ent = getCachedAnimations(lp);
2669 if (ent != null) {
2670 context = ent.context;
2671 anim = ent.array.getResourceId(animAttr, 0);
2672 }
2673 }
2674 if (anim != 0) {
2675 return AnimationUtils.loadAnimation(context, anim);
2676 }
2677 return null;
2678 }
Romain Guy06882f82009-06-10 13:36:04 -07002679
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002680 private Animation loadAnimation(String packageName, int resId) {
2681 int anim = 0;
2682 Context context = mContext;
2683 if (resId >= 0) {
2684 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2685 if (ent != null) {
2686 context = ent.context;
2687 anim = resId;
2688 }
2689 }
2690 if (anim != 0) {
2691 return AnimationUtils.loadAnimation(context, anim);
2692 }
2693 return null;
2694 }
2695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 private boolean applyAnimationLocked(AppWindowToken wtoken,
2697 WindowManager.LayoutParams lp, int transit, boolean enter) {
2698 // Only apply an animation if the display isn't frozen. If it is
2699 // frozen, there is no reason to animate and it can cause strange
2700 // artifacts when we unfreeze the display if some different animation
2701 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002702 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002703 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002704 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002705 a = new FadeInOutAnimation(enter);
2706 if (DEBUG_ANIM) Log.v(TAG,
2707 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002708 } else if (mNextAppTransitionPackage != null) {
2709 a = loadAnimation(mNextAppTransitionPackage, enter ?
2710 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002711 } else {
2712 int animAttr = 0;
2713 switch (transit) {
2714 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2715 animAttr = enter
2716 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2717 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2718 break;
2719 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2720 animAttr = enter
2721 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2722 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2723 break;
2724 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2725 animAttr = enter
2726 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2727 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2728 break;
2729 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2730 animAttr = enter
2731 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2732 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2733 break;
2734 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2735 animAttr = enter
2736 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2737 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2738 break;
2739 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2740 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002741 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002742 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2743 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002744 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002745 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002746 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2747 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002748 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002749 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002750 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002751 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2752 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2753 break;
2754 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2755 animAttr = enter
2756 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2757 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2758 break;
2759 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2760 animAttr = enter
2761 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2762 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002763 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002764 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002765 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002766 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2767 + " anim=" + a
2768 + " animAttr=0x" + Integer.toHexString(animAttr)
2769 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 if (a != null) {
2772 if (DEBUG_ANIM) {
2773 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002774 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2776 }
2777 wtoken.setAnimation(a);
2778 }
2779 } else {
2780 wtoken.clearAnimation();
2781 }
2782
2783 return wtoken.animation != null;
2784 }
2785
2786 // -------------------------------------------------------------
2787 // Application Window Tokens
2788 // -------------------------------------------------------------
2789
2790 public void validateAppTokens(List tokens) {
2791 int v = tokens.size()-1;
2792 int m = mAppTokens.size()-1;
2793 while (v >= 0 && m >= 0) {
2794 AppWindowToken wtoken = mAppTokens.get(m);
2795 if (wtoken.removed) {
2796 m--;
2797 continue;
2798 }
2799 if (tokens.get(v) != wtoken.token) {
2800 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2801 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2802 }
2803 v--;
2804 m--;
2805 }
2806 while (v >= 0) {
2807 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2808 v--;
2809 }
2810 while (m >= 0) {
2811 AppWindowToken wtoken = mAppTokens.get(m);
2812 if (!wtoken.removed) {
2813 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2814 }
2815 m--;
2816 }
2817 }
2818
2819 boolean checkCallingPermission(String permission, String func) {
2820 // Quick check: if the calling permission is me, it's all okay.
2821 if (Binder.getCallingPid() == Process.myPid()) {
2822 return true;
2823 }
Romain Guy06882f82009-06-10 13:36:04 -07002824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 if (mContext.checkCallingPermission(permission)
2826 == PackageManager.PERMISSION_GRANTED) {
2827 return true;
2828 }
2829 String msg = "Permission Denial: " + func + " from pid="
2830 + Binder.getCallingPid()
2831 + ", uid=" + Binder.getCallingUid()
2832 + " requires " + permission;
2833 Log.w(TAG, msg);
2834 return false;
2835 }
Romain Guy06882f82009-06-10 13:36:04 -07002836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 AppWindowToken findAppWindowToken(IBinder token) {
2838 WindowToken wtoken = mTokenMap.get(token);
2839 if (wtoken == null) {
2840 return null;
2841 }
2842 return wtoken.appWindowToken;
2843 }
Romain Guy06882f82009-06-10 13:36:04 -07002844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 public void addWindowToken(IBinder token, int type) {
2846 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2847 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002848 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 }
Romain Guy06882f82009-06-10 13:36:04 -07002850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 synchronized(mWindowMap) {
2852 WindowToken wtoken = mTokenMap.get(token);
2853 if (wtoken != null) {
2854 Log.w(TAG, "Attempted to add existing input method token: " + token);
2855 return;
2856 }
2857 wtoken = new WindowToken(token, type, true);
2858 mTokenMap.put(token, wtoken);
2859 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002860 if (type == TYPE_WALLPAPER) {
2861 mWallpaperTokens.add(wtoken);
2862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 }
2864 }
Romain Guy06882f82009-06-10 13:36:04 -07002865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 public void removeWindowToken(IBinder token) {
2867 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2868 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002869 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 }
2871
2872 final long origId = Binder.clearCallingIdentity();
2873 synchronized(mWindowMap) {
2874 WindowToken wtoken = mTokenMap.remove(token);
2875 mTokenList.remove(wtoken);
2876 if (wtoken != null) {
2877 boolean delayed = false;
2878 if (!wtoken.hidden) {
2879 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 final int N = wtoken.windows.size();
2882 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 for (int i=0; i<N; i++) {
2885 WindowState win = wtoken.windows.get(i);
2886
2887 if (win.isAnimating()) {
2888 delayed = true;
2889 }
Romain Guy06882f82009-06-10 13:36:04 -07002890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 if (win.isVisibleNow()) {
2892 applyAnimationLocked(win,
2893 WindowManagerPolicy.TRANSIT_EXIT, false);
2894 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2895 KeyWaiter.RETURN_NOTHING);
2896 changed = true;
2897 }
2898 }
2899
2900 if (changed) {
2901 mLayoutNeeded = true;
2902 performLayoutAndPlaceSurfacesLocked();
2903 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2904 }
Romain Guy06882f82009-06-10 13:36:04 -07002905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 if (delayed) {
2907 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002908 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2909 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 }
2911 }
Romain Guy06882f82009-06-10 13:36:04 -07002912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 } else {
2914 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2915 }
2916 }
2917 Binder.restoreCallingIdentity(origId);
2918 }
2919
2920 public void addAppToken(int addPos, IApplicationToken token,
2921 int groupId, int requestedOrientation, boolean fullscreen) {
2922 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2923 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002924 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 }
Romain Guy06882f82009-06-10 13:36:04 -07002926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 synchronized(mWindowMap) {
2928 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2929 if (wtoken != null) {
2930 Log.w(TAG, "Attempted to add existing app token: " + token);
2931 return;
2932 }
2933 wtoken = new AppWindowToken(token);
2934 wtoken.groupId = groupId;
2935 wtoken.appFullscreen = fullscreen;
2936 wtoken.requestedOrientation = requestedOrientation;
2937 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002938 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 mTokenMap.put(token.asBinder(), wtoken);
2940 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 // Application tokens start out hidden.
2943 wtoken.hidden = true;
2944 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 //dump();
2947 }
2948 }
Romain Guy06882f82009-06-10 13:36:04 -07002949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 public void setAppGroupId(IBinder token, int groupId) {
2951 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2952 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002953 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 }
2955
2956 synchronized(mWindowMap) {
2957 AppWindowToken wtoken = findAppWindowToken(token);
2958 if (wtoken == null) {
2959 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2960 return;
2961 }
2962 wtoken.groupId = groupId;
2963 }
2964 }
Romain Guy06882f82009-06-10 13:36:04 -07002965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 public int getOrientationFromWindowsLocked() {
2967 int pos = mWindows.size() - 1;
2968 while (pos >= 0) {
2969 WindowState wtoken = (WindowState) mWindows.get(pos);
2970 pos--;
2971 if (wtoken.mAppToken != null) {
2972 // We hit an application window. so the orientation will be determined by the
2973 // app window. No point in continuing further.
2974 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2975 }
2976 if (!wtoken.isVisibleLw()) {
2977 continue;
2978 }
2979 int req = wtoken.mAttrs.screenOrientation;
2980 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2981 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2982 continue;
2983 } else {
2984 return req;
2985 }
2986 }
2987 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2988 }
Romain Guy06882f82009-06-10 13:36:04 -07002989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 public int getOrientationFromAppTokensLocked() {
2991 int pos = mAppTokens.size() - 1;
2992 int curGroup = 0;
2993 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002994 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002996 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 while (pos >= 0) {
2998 AppWindowToken wtoken = mAppTokens.get(pos);
2999 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07003000 // if we're about to tear down this window and not seek for
3001 // the behind activity, don't use it for orientation
3002 if (!findingBehind
3003 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003004 continue;
3005 }
3006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 if (!haveGroup) {
3008 // We ignore any hidden applications on the top.
3009 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
3010 continue;
3011 }
3012 haveGroup = true;
3013 curGroup = wtoken.groupId;
3014 lastOrientation = wtoken.requestedOrientation;
3015 } else if (curGroup != wtoken.groupId) {
3016 // If we have hit a new application group, and the bottom
3017 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08003018 // the orientation behind it, and the last app was
3019 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08003021 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
3022 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 return lastOrientation;
3024 }
3025 }
3026 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07003027 // If this application is fullscreen, and didn't explicitly say
3028 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08003030 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07003031 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07003032 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 return or;
3034 }
3035 // If this application has requested an explicit orientation,
3036 // then use it.
3037 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3038 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3039 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3040 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3041 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3042 return or;
3043 }
Owen Lin3413b892009-05-01 17:12:32 -07003044 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 }
3046 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3047 }
Romain Guy06882f82009-06-10 13:36:04 -07003048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003050 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003051 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3052 "updateOrientationFromAppTokens()")) {
3053 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 Configuration config;
3057 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003058 config = updateOrientationFromAppTokensUnchecked(currentConfig,
3059 freezeThisOneIfNeeded);
3060 Binder.restoreCallingIdentity(ident);
3061 return config;
3062 }
3063
3064 Configuration updateOrientationFromAppTokensUnchecked(
3065 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
3066 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003068 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003069 if (config != null) {
3070 mLayoutNeeded = true;
3071 performLayoutAndPlaceSurfacesLocked();
3072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 return config;
3075 }
Romain Guy06882f82009-06-10 13:36:04 -07003076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 /*
3078 * The orientation is computed from non-application windows first. If none of
3079 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003080 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3082 * android.os.IBinder)
3083 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003084 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07003085 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 long ident = Binder.clearCallingIdentity();
3088 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003089 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 if (req != mForcedAppOrientation) {
3092 changed = true;
3093 mForcedAppOrientation = req;
3094 //send a message to Policy indicating orientation change to take
3095 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003096 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 }
Romain Guy06882f82009-06-10 13:36:04 -07003098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 if (changed) {
3100 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07003101 WindowManagerPolicy.USE_LAST_ROTATION,
3102 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 if (changed) {
3104 if (freezeThisOneIfNeeded != null) {
3105 AppWindowToken wtoken = findAppWindowToken(
3106 freezeThisOneIfNeeded);
3107 if (wtoken != null) {
3108 startAppFreezingScreenLocked(wtoken,
3109 ActivityInfo.CONFIG_ORIENTATION);
3110 }
3111 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003112 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 }
3114 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003115
3116 // No obvious action we need to take, but if our current
3117 // state mismatches the activity maanager's, update it
3118 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003119 mTempConfiguration.setToDefaults();
3120 if (computeNewConfigurationLocked(mTempConfiguration)) {
3121 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003122 return new Configuration(mTempConfiguration);
3123 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003124 }
3125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 } finally {
3127 Binder.restoreCallingIdentity(ident);
3128 }
Romain Guy06882f82009-06-10 13:36:04 -07003129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 return null;
3131 }
Romain Guy06882f82009-06-10 13:36:04 -07003132
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003133 int computeForcedAppOrientationLocked() {
3134 int req = getOrientationFromWindowsLocked();
3135 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3136 req = getOrientationFromAppTokensLocked();
3137 }
3138 return req;
3139 }
Romain Guy06882f82009-06-10 13:36:04 -07003140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3142 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3143 "setAppOrientation()")) {
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 }
Romain Guy06882f82009-06-10 13:36:04 -07003146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 synchronized(mWindowMap) {
3148 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3149 if (wtoken == null) {
3150 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
3151 return;
3152 }
Romain Guy06882f82009-06-10 13:36:04 -07003153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 wtoken.requestedOrientation = requestedOrientation;
3155 }
3156 }
Romain Guy06882f82009-06-10 13:36:04 -07003157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 public int getAppOrientation(IApplicationToken token) {
3159 synchronized(mWindowMap) {
3160 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3161 if (wtoken == null) {
3162 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3163 }
Romain Guy06882f82009-06-10 13:36:04 -07003164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 return wtoken.requestedOrientation;
3166 }
3167 }
Romain Guy06882f82009-06-10 13:36:04 -07003168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3170 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3171 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003172 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 }
3174
3175 synchronized(mWindowMap) {
3176 boolean changed = false;
3177 if (token == null) {
3178 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
3179 changed = mFocusedApp != null;
3180 mFocusedApp = null;
3181 mKeyWaiter.tickle();
3182 } else {
3183 AppWindowToken newFocus = findAppWindowToken(token);
3184 if (newFocus == null) {
3185 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
3186 return;
3187 }
3188 changed = mFocusedApp != newFocus;
3189 mFocusedApp = newFocus;
3190 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
3191 mKeyWaiter.tickle();
3192 }
3193
3194 if (moveFocusNow && changed) {
3195 final long origId = Binder.clearCallingIdentity();
3196 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3197 Binder.restoreCallingIdentity(origId);
3198 }
3199 }
3200 }
3201
3202 public void prepareAppTransition(int transit) {
3203 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3204 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003205 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 }
Romain Guy06882f82009-06-10 13:36:04 -07003207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 synchronized(mWindowMap) {
3209 if (DEBUG_APP_TRANSITIONS) Log.v(
3210 TAG, "Prepare app transition: transit=" + transit
3211 + " mNextAppTransition=" + mNextAppTransition);
3212 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003213 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3214 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003216 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3217 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3218 // Opening a new task always supersedes a close for the anim.
3219 mNextAppTransition = transit;
3220 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3221 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3222 // Opening a new activity always supersedes a close for the anim.
3223 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 }
3225 mAppTransitionReady = false;
3226 mAppTransitionTimeout = false;
3227 mStartingIconInTransition = false;
3228 mSkipAppTransitionAnimation = false;
3229 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3230 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3231 5000);
3232 }
3233 }
3234 }
3235
3236 public int getPendingAppTransition() {
3237 return mNextAppTransition;
3238 }
Romain Guy06882f82009-06-10 13:36:04 -07003239
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003240 public void overridePendingAppTransition(String packageName,
3241 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003242 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003243 mNextAppTransitionPackage = packageName;
3244 mNextAppTransitionEnter = enterAnim;
3245 mNextAppTransitionExit = exitAnim;
3246 }
3247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 public void executeAppTransition() {
3250 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3251 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003252 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 }
Romain Guy06882f82009-06-10 13:36:04 -07003254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003256 if (DEBUG_APP_TRANSITIONS) {
3257 RuntimeException e = new RuntimeException("here");
3258 e.fillInStackTrace();
3259 Log.w(TAG, "Execute app transition: mNextAppTransition="
3260 + mNextAppTransition, e);
3261 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003262 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 mAppTransitionReady = true;
3264 final long origId = Binder.clearCallingIdentity();
3265 performLayoutAndPlaceSurfacesLocked();
3266 Binder.restoreCallingIdentity(origId);
3267 }
3268 }
3269 }
3270
3271 public void setAppStartingWindow(IBinder token, String pkg,
3272 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3273 IBinder transferFrom, boolean createIfNeeded) {
3274 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3275 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003276 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 }
3278
3279 synchronized(mWindowMap) {
3280 if (DEBUG_STARTING_WINDOW) Log.v(
3281 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3282 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 AppWindowToken wtoken = findAppWindowToken(token);
3285 if (wtoken == null) {
3286 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3287 return;
3288 }
3289
3290 // If the display is frozen, we won't do anything until the
3291 // actual window is displayed so there is no reason to put in
3292 // the starting window.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08003293 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 return;
3295 }
Romain Guy06882f82009-06-10 13:36:04 -07003296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 if (wtoken.startingData != null) {
3298 return;
3299 }
Romain Guy06882f82009-06-10 13:36:04 -07003300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 if (transferFrom != null) {
3302 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3303 if (ttoken != null) {
3304 WindowState startingWindow = ttoken.startingWindow;
3305 if (startingWindow != null) {
3306 if (mStartingIconInTransition) {
3307 // In this case, the starting icon has already
3308 // been displayed, so start letting windows get
3309 // shown immediately without any more transitions.
3310 mSkipAppTransitionAnimation = true;
3311 }
3312 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3313 "Moving existing starting from " + ttoken
3314 + " to " + wtoken);
3315 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 // Transfer the starting window over to the new
3318 // token.
3319 wtoken.startingData = ttoken.startingData;
3320 wtoken.startingView = ttoken.startingView;
3321 wtoken.startingWindow = startingWindow;
3322 ttoken.startingData = null;
3323 ttoken.startingView = null;
3324 ttoken.startingWindow = null;
3325 ttoken.startingMoved = true;
3326 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003327 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 startingWindow.mAppToken = wtoken;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003329 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3330 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 mWindows.remove(startingWindow);
3332 ttoken.windows.remove(startingWindow);
3333 ttoken.allAppWindows.remove(startingWindow);
3334 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 // Propagate other interesting state between the
3337 // tokens. If the old token is displayed, we should
3338 // immediately force the new one to be displayed. If
3339 // it is animating, we need to move that animation to
3340 // the new one.
3341 if (ttoken.allDrawn) {
3342 wtoken.allDrawn = true;
3343 }
3344 if (ttoken.firstWindowDrawn) {
3345 wtoken.firstWindowDrawn = true;
3346 }
3347 if (!ttoken.hidden) {
3348 wtoken.hidden = false;
3349 wtoken.hiddenRequested = false;
3350 wtoken.willBeHidden = false;
3351 }
3352 if (wtoken.clientHidden != ttoken.clientHidden) {
3353 wtoken.clientHidden = ttoken.clientHidden;
3354 wtoken.sendAppVisibilityToClients();
3355 }
3356 if (ttoken.animation != null) {
3357 wtoken.animation = ttoken.animation;
3358 wtoken.animating = ttoken.animating;
3359 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3360 ttoken.animation = null;
3361 ttoken.animLayerAdjustment = 0;
3362 wtoken.updateLayers();
3363 ttoken.updateLayers();
3364 }
Romain Guy06882f82009-06-10 13:36:04 -07003365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 mLayoutNeeded = true;
3368 performLayoutAndPlaceSurfacesLocked();
3369 Binder.restoreCallingIdentity(origId);
3370 return;
3371 } else if (ttoken.startingData != null) {
3372 // The previous app was getting ready to show a
3373 // starting window, but hasn't yet done so. Steal it!
3374 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3375 "Moving pending starting from " + ttoken
3376 + " to " + wtoken);
3377 wtoken.startingData = ttoken.startingData;
3378 ttoken.startingData = null;
3379 ttoken.startingMoved = true;
3380 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3381 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3382 // want to process the message ASAP, before any other queued
3383 // messages.
3384 mH.sendMessageAtFrontOfQueue(m);
3385 return;
3386 }
3387 }
3388 }
3389
3390 // There is no existing starting window, and the caller doesn't
3391 // want us to create one, so that's it!
3392 if (!createIfNeeded) {
3393 return;
3394 }
Romain Guy06882f82009-06-10 13:36:04 -07003395
Dianne Hackborn284ac932009-08-28 10:34:25 -07003396 // If this is a translucent or wallpaper window, then don't
3397 // show a starting window -- the current effect (a full-screen
3398 // opaque starting window that fades away to the real contents
3399 // when it is ready) does not work for this.
3400 if (theme != 0) {
3401 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3402 com.android.internal.R.styleable.Window);
3403 if (ent.array.getBoolean(
3404 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3405 return;
3406 }
3407 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003408 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3409 return;
3410 }
3411 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003412 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3413 return;
3414 }
3415 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 mStartingIconInTransition = true;
3418 wtoken.startingData = new StartingData(
3419 pkg, theme, nonLocalizedLabel,
3420 labelRes, icon);
3421 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3422 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3423 // want to process the message ASAP, before any other queued
3424 // messages.
3425 mH.sendMessageAtFrontOfQueue(m);
3426 }
3427 }
3428
3429 public void setAppWillBeHidden(IBinder token) {
3430 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3431 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003432 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 }
3434
3435 AppWindowToken wtoken;
3436
3437 synchronized(mWindowMap) {
3438 wtoken = findAppWindowToken(token);
3439 if (wtoken == null) {
3440 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3441 return;
3442 }
3443 wtoken.willBeHidden = true;
3444 }
3445 }
Romain Guy06882f82009-06-10 13:36:04 -07003446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3448 boolean visible, int transit, boolean performLayout) {
3449 boolean delayed = false;
3450
3451 if (wtoken.clientHidden == visible) {
3452 wtoken.clientHidden = !visible;
3453 wtoken.sendAppVisibilityToClients();
3454 }
Romain Guy06882f82009-06-10 13:36:04 -07003455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 wtoken.willBeHidden = false;
3457 if (wtoken.hidden == visible) {
3458 final int N = wtoken.allAppWindows.size();
3459 boolean changed = false;
3460 if (DEBUG_APP_TRANSITIONS) Log.v(
3461 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3462 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003465
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003466 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 if (wtoken.animation == sDummyAnimation) {
3468 wtoken.animation = null;
3469 }
3470 applyAnimationLocked(wtoken, lp, transit, visible);
3471 changed = true;
3472 if (wtoken.animation != null) {
3473 delayed = runningAppAnimation = true;
3474 }
3475 }
Romain Guy06882f82009-06-10 13:36:04 -07003476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 for (int i=0; i<N; i++) {
3478 WindowState win = wtoken.allAppWindows.get(i);
3479 if (win == wtoken.startingWindow) {
3480 continue;
3481 }
3482
3483 if (win.isAnimating()) {
3484 delayed = true;
3485 }
Romain Guy06882f82009-06-10 13:36:04 -07003486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3488 //win.dump(" ");
3489 if (visible) {
3490 if (!win.isVisibleNow()) {
3491 if (!runningAppAnimation) {
3492 applyAnimationLocked(win,
3493 WindowManagerPolicy.TRANSIT_ENTER, true);
3494 }
3495 changed = true;
3496 }
3497 } else if (win.isVisibleNow()) {
3498 if (!runningAppAnimation) {
3499 applyAnimationLocked(win,
3500 WindowManagerPolicy.TRANSIT_EXIT, false);
3501 }
3502 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3503 KeyWaiter.RETURN_NOTHING);
3504 changed = true;
3505 }
3506 }
3507
3508 wtoken.hidden = wtoken.hiddenRequested = !visible;
3509 if (!visible) {
3510 unsetAppFreezingScreenLocked(wtoken, true, true);
3511 } else {
3512 // If we are being set visible, and the starting window is
3513 // not yet displayed, then make sure it doesn't get displayed.
3514 WindowState swin = wtoken.startingWindow;
3515 if (swin != null && (swin.mDrawPending
3516 || swin.mCommitDrawPending)) {
3517 swin.mPolicyVisibility = false;
3518 swin.mPolicyVisibilityAfterAnim = false;
3519 }
3520 }
Romain Guy06882f82009-06-10 13:36:04 -07003521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3523 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3524 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003525
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003526 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003528 if (performLayout) {
3529 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3530 performLayoutAndPlaceSurfacesLocked();
3531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 }
3533 }
3534
3535 if (wtoken.animation != null) {
3536 delayed = true;
3537 }
Romain Guy06882f82009-06-10 13:36:04 -07003538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 return delayed;
3540 }
3541
3542 public void setAppVisibility(IBinder token, boolean visible) {
3543 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3544 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003545 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 }
3547
3548 AppWindowToken wtoken;
3549
3550 synchronized(mWindowMap) {
3551 wtoken = findAppWindowToken(token);
3552 if (wtoken == null) {
3553 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3554 return;
3555 }
3556
3557 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3558 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003559 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3561 + "): mNextAppTransition=" + mNextAppTransition
3562 + " hidden=" + wtoken.hidden
3563 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3564 }
Romain Guy06882f82009-06-10 13:36:04 -07003565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 // If we are preparing an app transition, then delay changing
3567 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003568 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 // Already in requested state, don't do anything more.
3570 if (wtoken.hiddenRequested != visible) {
3571 return;
3572 }
3573 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 if (DEBUG_APP_TRANSITIONS) Log.v(
3576 TAG, "Setting dummy animation on: " + wtoken);
3577 wtoken.setDummyAnimation();
3578 mOpeningApps.remove(wtoken);
3579 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003580 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 wtoken.inPendingTransaction = true;
3582 if (visible) {
3583 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 wtoken.startingDisplayed = false;
3585 wtoken.startingMoved = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003586
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003587 // If the token is currently hidden (should be the
3588 // common case), then we need to set up to wait for
3589 // its windows to be ready.
3590 if (wtoken.hidden) {
3591 wtoken.allDrawn = false;
3592 wtoken.waitingToShow = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003593
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003594 if (wtoken.clientHidden) {
3595 // In the case where we are making an app visible
3596 // but holding off for a transition, we still need
3597 // to tell the client to make its windows visible so
3598 // they get drawn. Otherwise, we will wait on
3599 // performing the transition until all windows have
3600 // been drawn, they never will be, and we are sad.
3601 wtoken.clientHidden = false;
3602 wtoken.sendAppVisibilityToClients();
3603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604 }
3605 } else {
3606 mClosingApps.add(wtoken);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003607
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003608 // If the token is currently visible (should be the
3609 // common case), then set up to wait for it to be hidden.
3610 if (!wtoken.hidden) {
3611 wtoken.waitingToHide = true;
3612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 }
3614 return;
3615 }
Romain Guy06882f82009-06-10 13:36:04 -07003616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003618 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 wtoken.updateReportedVisibilityLocked();
3620 Binder.restoreCallingIdentity(origId);
3621 }
3622 }
3623
3624 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3625 boolean unfreezeSurfaceNow, boolean force) {
3626 if (wtoken.freezingScreen) {
3627 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3628 + " force=" + force);
3629 final int N = wtoken.allAppWindows.size();
3630 boolean unfrozeWindows = false;
3631 for (int i=0; i<N; i++) {
3632 WindowState w = wtoken.allAppWindows.get(i);
3633 if (w.mAppFreezing) {
3634 w.mAppFreezing = false;
3635 if (w.mSurface != null && !w.mOrientationChanging) {
3636 w.mOrientationChanging = true;
3637 }
3638 unfrozeWindows = true;
3639 }
3640 }
3641 if (force || unfrozeWindows) {
3642 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3643 wtoken.freezingScreen = false;
3644 mAppsFreezingScreen--;
3645 }
3646 if (unfreezeSurfaceNow) {
3647 if (unfrozeWindows) {
3648 mLayoutNeeded = true;
3649 performLayoutAndPlaceSurfacesLocked();
3650 }
3651 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3652 stopFreezingDisplayLocked();
3653 }
3654 }
3655 }
3656 }
Romain Guy06882f82009-06-10 13:36:04 -07003657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3659 int configChanges) {
3660 if (DEBUG_ORIENTATION) {
3661 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003662 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 Log.i(TAG, "Set freezing of " + wtoken.appToken
3664 + ": hidden=" + wtoken.hidden + " freezing="
3665 + wtoken.freezingScreen, e);
3666 }
3667 if (!wtoken.hiddenRequested) {
3668 if (!wtoken.freezingScreen) {
3669 wtoken.freezingScreen = true;
3670 mAppsFreezingScreen++;
3671 if (mAppsFreezingScreen == 1) {
3672 startFreezingDisplayLocked();
3673 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3674 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3675 5000);
3676 }
3677 }
3678 final int N = wtoken.allAppWindows.size();
3679 for (int i=0; i<N; i++) {
3680 WindowState w = wtoken.allAppWindows.get(i);
3681 w.mAppFreezing = true;
3682 }
3683 }
3684 }
Romain Guy06882f82009-06-10 13:36:04 -07003685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 public void startAppFreezingScreen(IBinder token, int configChanges) {
3687 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3688 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003689 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 }
3691
3692 synchronized(mWindowMap) {
3693 if (configChanges == 0 && !mDisplayFrozen) {
3694 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3695 return;
3696 }
Romain Guy06882f82009-06-10 13:36:04 -07003697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 AppWindowToken wtoken = findAppWindowToken(token);
3699 if (wtoken == null || wtoken.appToken == null) {
3700 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3701 return;
3702 }
3703 final long origId = Binder.clearCallingIdentity();
3704 startAppFreezingScreenLocked(wtoken, configChanges);
3705 Binder.restoreCallingIdentity(origId);
3706 }
3707 }
Romain Guy06882f82009-06-10 13:36:04 -07003708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 public void stopAppFreezingScreen(IBinder token, boolean force) {
3710 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3711 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003712 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 }
3714
3715 synchronized(mWindowMap) {
3716 AppWindowToken wtoken = findAppWindowToken(token);
3717 if (wtoken == null || wtoken.appToken == null) {
3718 return;
3719 }
3720 final long origId = Binder.clearCallingIdentity();
3721 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3722 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3723 unsetAppFreezingScreenLocked(wtoken, true, force);
3724 Binder.restoreCallingIdentity(origId);
3725 }
3726 }
Romain Guy06882f82009-06-10 13:36:04 -07003727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 public void removeAppToken(IBinder token) {
3729 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3730 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003731 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 }
3733
3734 AppWindowToken wtoken = null;
3735 AppWindowToken startingToken = null;
3736 boolean delayed = false;
3737
3738 final long origId = Binder.clearCallingIdentity();
3739 synchronized(mWindowMap) {
3740 WindowToken basewtoken = mTokenMap.remove(token);
3741 mTokenList.remove(basewtoken);
3742 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3743 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003744 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 wtoken.inPendingTransaction = false;
3746 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003747 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 if (mClosingApps.contains(wtoken)) {
3749 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003750 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003752 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 delayed = true;
3754 }
3755 if (DEBUG_APP_TRANSITIONS) Log.v(
3756 TAG, "Removing app " + wtoken + " delayed=" + delayed
3757 + " animation=" + wtoken.animation
3758 + " animating=" + wtoken.animating);
3759 if (delayed) {
3760 // set the token aside because it has an active animation to be finished
3761 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003762 } else {
3763 // Make sure there is no animation running on this token,
3764 // so any windows associated with it will be removed as
3765 // soon as their animations are complete
3766 wtoken.animation = null;
3767 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 }
3769 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003770 if (mLastEnterAnimToken == wtoken) {
3771 mLastEnterAnimToken = null;
3772 mLastEnterAnimParams = null;
3773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 wtoken.removed = true;
3775 if (wtoken.startingData != null) {
3776 startingToken = wtoken;
3777 }
3778 unsetAppFreezingScreenLocked(wtoken, true, true);
3779 if (mFocusedApp == wtoken) {
3780 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3781 mFocusedApp = null;
3782 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3783 mKeyWaiter.tickle();
3784 }
3785 } else {
3786 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3787 }
Romain Guy06882f82009-06-10 13:36:04 -07003788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 if (!delayed && wtoken != null) {
3790 wtoken.updateReportedVisibilityLocked();
3791 }
3792 }
3793 Binder.restoreCallingIdentity(origId);
3794
3795 if (startingToken != null) {
3796 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3797 + startingToken + ": app token removed");
3798 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3799 mH.sendMessage(m);
3800 }
3801 }
3802
3803 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3804 final int NW = token.windows.size();
3805 for (int i=0; i<NW; i++) {
3806 WindowState win = token.windows.get(i);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003807 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 mWindows.remove(win);
3809 int j = win.mChildWindows.size();
3810 while (j > 0) {
3811 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003812 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3813 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3814 "Tmp removing child window " + cwin);
3815 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 }
3817 }
3818 return NW > 0;
3819 }
3820
3821 void dumpAppTokensLocked() {
3822 for (int i=mAppTokens.size()-1; i>=0; i--) {
3823 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3824 }
3825 }
Romain Guy06882f82009-06-10 13:36:04 -07003826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 void dumpWindowsLocked() {
3828 for (int i=mWindows.size()-1; i>=0; i--) {
3829 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3830 }
3831 }
Romain Guy06882f82009-06-10 13:36:04 -07003832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 private int findWindowOffsetLocked(int tokenPos) {
3834 final int NW = mWindows.size();
3835
3836 if (tokenPos >= mAppTokens.size()) {
3837 int i = NW;
3838 while (i > 0) {
3839 i--;
3840 WindowState win = (WindowState)mWindows.get(i);
3841 if (win.getAppToken() != null) {
3842 return i+1;
3843 }
3844 }
3845 }
3846
3847 while (tokenPos > 0) {
3848 // Find the first app token below the new position that has
3849 // a window displayed.
3850 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3851 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3852 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003853 if (wtoken.sendingToBottom) {
3854 if (DEBUG_REORDER) Log.v(TAG,
3855 "Skipping token -- currently sending to bottom");
3856 tokenPos--;
3857 continue;
3858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 int i = wtoken.windows.size();
3860 while (i > 0) {
3861 i--;
3862 WindowState win = wtoken.windows.get(i);
3863 int j = win.mChildWindows.size();
3864 while (j > 0) {
3865 j--;
3866 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003867 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 for (int pos=NW-1; pos>=0; pos--) {
3869 if (mWindows.get(pos) == cwin) {
3870 if (DEBUG_REORDER) Log.v(TAG,
3871 "Found child win @" + (pos+1));
3872 return pos+1;
3873 }
3874 }
3875 }
3876 }
3877 for (int pos=NW-1; pos>=0; pos--) {
3878 if (mWindows.get(pos) == win) {
3879 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3880 return pos+1;
3881 }
3882 }
3883 }
3884 tokenPos--;
3885 }
3886
3887 return 0;
3888 }
3889
3890 private final int reAddWindowLocked(int index, WindowState win) {
3891 final int NCW = win.mChildWindows.size();
3892 boolean added = false;
3893 for (int j=0; j<NCW; j++) {
3894 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3895 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003896 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3897 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 mWindows.add(index, win);
3899 index++;
3900 added = true;
3901 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003902 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3903 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 mWindows.add(index, cwin);
3905 index++;
3906 }
3907 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003908 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3909 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003910 mWindows.add(index, win);
3911 index++;
3912 }
3913 return index;
3914 }
Romain Guy06882f82009-06-10 13:36:04 -07003915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3917 final int NW = token.windows.size();
3918 for (int i=0; i<NW; i++) {
3919 index = reAddWindowLocked(index, token.windows.get(i));
3920 }
3921 return index;
3922 }
3923
3924 public void moveAppToken(int index, IBinder token) {
3925 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3926 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003927 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 }
3929
3930 synchronized(mWindowMap) {
3931 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3932 if (DEBUG_REORDER) dumpAppTokensLocked();
3933 final AppWindowToken wtoken = findAppWindowToken(token);
3934 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3935 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3936 + token + " (" + wtoken + ")");
3937 return;
3938 }
3939 mAppTokens.add(index, wtoken);
3940 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3941 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 final long origId = Binder.clearCallingIdentity();
3944 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3945 if (DEBUG_REORDER) dumpWindowsLocked();
3946 if (tmpRemoveAppWindowsLocked(wtoken)) {
3947 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3948 if (DEBUG_REORDER) dumpWindowsLocked();
3949 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3950 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3951 if (DEBUG_REORDER) dumpWindowsLocked();
3952 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 mLayoutNeeded = true;
3954 performLayoutAndPlaceSurfacesLocked();
3955 }
3956 Binder.restoreCallingIdentity(origId);
3957 }
3958 }
3959
3960 private void removeAppTokensLocked(List<IBinder> tokens) {
3961 // XXX This should be done more efficiently!
3962 // (take advantage of the fact that both lists should be
3963 // ordered in the same way.)
3964 int N = tokens.size();
3965 for (int i=0; i<N; i++) {
3966 IBinder token = tokens.get(i);
3967 final AppWindowToken wtoken = findAppWindowToken(token);
3968 if (!mAppTokens.remove(wtoken)) {
3969 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3970 + token + " (" + wtoken + ")");
3971 i--;
3972 N--;
3973 }
3974 }
3975 }
3976
Dianne Hackborna8f60182009-09-01 19:01:50 -07003977 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3978 boolean updateFocusAndLayout) {
3979 // First remove all of the windows from the list.
3980 tmpRemoveAppWindowsLocked(wtoken);
3981
3982 // Where to start adding?
3983 int pos = findWindowOffsetLocked(tokenPos);
3984
3985 // And now add them back at the correct place.
3986 pos = reAddAppWindowsLocked(pos, wtoken);
3987
3988 if (updateFocusAndLayout) {
3989 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3990 assignLayersLocked();
3991 }
3992 mLayoutNeeded = true;
3993 performLayoutAndPlaceSurfacesLocked();
3994 }
3995 }
3996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3998 // First remove all of the windows from the list.
3999 final int N = tokens.size();
4000 int i;
4001 for (i=0; i<N; i++) {
4002 WindowToken token = mTokenMap.get(tokens.get(i));
4003 if (token != null) {
4004 tmpRemoveAppWindowsLocked(token);
4005 }
4006 }
4007
4008 // Where to start adding?
4009 int pos = findWindowOffsetLocked(tokenPos);
4010
4011 // And now add them back at the correct place.
4012 for (i=0; i<N; i++) {
4013 WindowToken token = mTokenMap.get(tokens.get(i));
4014 if (token != null) {
4015 pos = reAddAppWindowsLocked(pos, token);
4016 }
4017 }
4018
Dianne Hackborna8f60182009-09-01 19:01:50 -07004019 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4020 assignLayersLocked();
4021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 mLayoutNeeded = true;
4023 performLayoutAndPlaceSurfacesLocked();
4024
4025 //dump();
4026 }
4027
4028 public void moveAppTokensToTop(List<IBinder> tokens) {
4029 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4030 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004031 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 }
4033
4034 final long origId = Binder.clearCallingIdentity();
4035 synchronized(mWindowMap) {
4036 removeAppTokensLocked(tokens);
4037 final int N = tokens.size();
4038 for (int i=0; i<N; i++) {
4039 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4040 if (wt != null) {
4041 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004042 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004043 mToTopApps.remove(wt);
4044 mToBottomApps.remove(wt);
4045 mToTopApps.add(wt);
4046 wt.sendingToBottom = false;
4047 wt.sendingToTop = true;
4048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 }
4050 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004051
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004052 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004053 moveAppWindowsLocked(tokens, mAppTokens.size());
4054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 }
4056 Binder.restoreCallingIdentity(origId);
4057 }
4058
4059 public void moveAppTokensToBottom(List<IBinder> tokens) {
4060 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4061 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004062 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 }
4064
4065 final long origId = Binder.clearCallingIdentity();
4066 synchronized(mWindowMap) {
4067 removeAppTokensLocked(tokens);
4068 final int N = tokens.size();
4069 int pos = 0;
4070 for (int i=0; i<N; i++) {
4071 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4072 if (wt != null) {
4073 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004074 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004075 mToTopApps.remove(wt);
4076 mToBottomApps.remove(wt);
4077 mToBottomApps.add(i, wt);
4078 wt.sendingToTop = false;
4079 wt.sendingToBottom = true;
4080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 pos++;
4082 }
4083 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004084
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004085 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004086 moveAppWindowsLocked(tokens, 0);
4087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 }
4089 Binder.restoreCallingIdentity(origId);
4090 }
4091
4092 // -------------------------------------------------------------
4093 // Misc IWindowSession methods
4094 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004097 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 != PackageManager.PERMISSION_GRANTED) {
4099 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4100 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004101 synchronized (mKeyguardTokenWatcher) {
4102 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004104 }
4105
4106 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004107 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 != PackageManager.PERMISSION_GRANTED) {
4109 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4110 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004111 synchronized (mKeyguardTokenWatcher) {
4112 mKeyguardTokenWatcher.release(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113
Mike Lockwood983ee092009-11-22 01:42:24 -05004114 if (!mKeyguardTokenWatcher.isAcquired()) {
4115 // If we are the last one to reenable the keyguard wait until
4116 // we have actaully finished reenabling until returning.
4117 // It is possible that reenableKeyguard() can be called before
4118 // the previous disableKeyguard() is handled, in which case
4119 // neither mKeyguardTokenWatcher.acquired() or released() would
4120 // be called. In that case mKeyguardDisabled will be false here
4121 // and we have nothing to wait for.
4122 while (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 try {
Mike Lockwood983ee092009-11-22 01:42:24 -05004124 mKeyguardTokenWatcher.wait();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 } catch (InterruptedException e) {
4126 Thread.currentThread().interrupt();
4127 }
4128 }
4129 }
4130 }
4131 }
4132
4133 /**
4134 * @see android.app.KeyguardManager#exitKeyguardSecurely
4135 */
4136 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004137 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 != PackageManager.PERMISSION_GRANTED) {
4139 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4140 }
4141 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4142 public void onKeyguardExitResult(boolean success) {
4143 try {
4144 callback.onKeyguardExitResult(success);
4145 } catch (RemoteException e) {
4146 // Client has died, we don't care.
4147 }
4148 }
4149 });
4150 }
4151
4152 public boolean inKeyguardRestrictedInputMode() {
4153 return mPolicy.inKeyguardRestrictedKeyInputMode();
4154 }
Romain Guy06882f82009-06-10 13:36:04 -07004155
Dianne Hackbornffa42482009-09-23 22:20:11 -07004156 public void closeSystemDialogs(String reason) {
4157 synchronized(mWindowMap) {
4158 for (int i=mWindows.size()-1; i>=0; i--) {
4159 WindowState w = (WindowState)mWindows.get(i);
4160 if (w.mSurface != null) {
4161 try {
4162 w.mClient.closeSystemDialogs(reason);
4163 } catch (RemoteException e) {
4164 }
4165 }
4166 }
4167 }
4168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 static float fixScale(float scale) {
4171 if (scale < 0) scale = 0;
4172 else if (scale > 20) scale = 20;
4173 return Math.abs(scale);
4174 }
Romain Guy06882f82009-06-10 13:36:04 -07004175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 public void setAnimationScale(int which, float scale) {
4177 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4178 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004179 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 }
4181
4182 if (scale < 0) scale = 0;
4183 else if (scale > 20) scale = 20;
4184 scale = Math.abs(scale);
4185 switch (which) {
4186 case 0: mWindowAnimationScale = fixScale(scale); break;
4187 case 1: mTransitionAnimationScale = fixScale(scale); break;
4188 }
Romain Guy06882f82009-06-10 13:36:04 -07004189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 // Persist setting
4191 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4192 }
Romain Guy06882f82009-06-10 13:36:04 -07004193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 public void setAnimationScales(float[] scales) {
4195 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4196 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004197 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 }
4199
4200 if (scales != null) {
4201 if (scales.length >= 1) {
4202 mWindowAnimationScale = fixScale(scales[0]);
4203 }
4204 if (scales.length >= 2) {
4205 mTransitionAnimationScale = fixScale(scales[1]);
4206 }
4207 }
Romain Guy06882f82009-06-10 13:36:04 -07004208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 // Persist setting
4210 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4211 }
Romain Guy06882f82009-06-10 13:36:04 -07004212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 public float getAnimationScale(int which) {
4214 switch (which) {
4215 case 0: return mWindowAnimationScale;
4216 case 1: return mTransitionAnimationScale;
4217 }
4218 return 0;
4219 }
Romain Guy06882f82009-06-10 13:36:04 -07004220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 public float[] getAnimationScales() {
4222 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4223 }
Romain Guy06882f82009-06-10 13:36:04 -07004224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 public int getSwitchState(int sw) {
4226 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4227 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004228 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 }
4230 return KeyInputQueue.getSwitchState(sw);
4231 }
Romain Guy06882f82009-06-10 13:36:04 -07004232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 public int getSwitchStateForDevice(int devid, int sw) {
4234 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4235 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004236 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 }
4238 return KeyInputQueue.getSwitchState(devid, sw);
4239 }
Romain Guy06882f82009-06-10 13:36:04 -07004240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 public int getScancodeState(int sw) {
4242 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4243 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004244 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004246 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 }
Romain Guy06882f82009-06-10 13:36:04 -07004248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 public int getScancodeStateForDevice(int devid, int sw) {
4250 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4251 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004252 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004254 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 }
Romain Guy06882f82009-06-10 13:36:04 -07004256
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004257 public int getTrackballScancodeState(int sw) {
4258 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4259 "getTrackballScancodeState()")) {
4260 throw new SecurityException("Requires READ_INPUT_STATE permission");
4261 }
4262 return mQueue.getTrackballScancodeState(sw);
4263 }
4264
4265 public int getDPadScancodeState(int sw) {
4266 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4267 "getDPadScancodeState()")) {
4268 throw new SecurityException("Requires READ_INPUT_STATE permission");
4269 }
4270 return mQueue.getDPadScancodeState(sw);
4271 }
4272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004273 public int getKeycodeState(int sw) {
4274 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4275 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004276 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004278 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 }
Romain Guy06882f82009-06-10 13:36:04 -07004280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 public int getKeycodeStateForDevice(int devid, int sw) {
4282 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4283 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004284 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004286 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 }
Romain Guy06882f82009-06-10 13:36:04 -07004288
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004289 public int getTrackballKeycodeState(int sw) {
4290 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4291 "getTrackballKeycodeState()")) {
4292 throw new SecurityException("Requires READ_INPUT_STATE permission");
4293 }
4294 return mQueue.getTrackballKeycodeState(sw);
4295 }
4296
4297 public int getDPadKeycodeState(int sw) {
4298 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4299 "getDPadKeycodeState()")) {
4300 throw new SecurityException("Requires READ_INPUT_STATE permission");
4301 }
4302 return mQueue.getDPadKeycodeState(sw);
4303 }
4304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4306 return KeyInputQueue.hasKeys(keycodes, keyExists);
4307 }
Romain Guy06882f82009-06-10 13:36:04 -07004308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 public void enableScreenAfterBoot() {
4310 synchronized(mWindowMap) {
4311 if (mSystemBooted) {
4312 return;
4313 }
4314 mSystemBooted = true;
4315 }
Romain Guy06882f82009-06-10 13:36:04 -07004316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 performEnableScreen();
4318 }
Romain Guy06882f82009-06-10 13:36:04 -07004319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 public void enableScreenIfNeededLocked() {
4321 if (mDisplayEnabled) {
4322 return;
4323 }
4324 if (!mSystemBooted) {
4325 return;
4326 }
4327 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4328 }
Romain Guy06882f82009-06-10 13:36:04 -07004329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 public void performEnableScreen() {
4331 synchronized(mWindowMap) {
4332 if (mDisplayEnabled) {
4333 return;
4334 }
4335 if (!mSystemBooted) {
4336 return;
4337 }
Romain Guy06882f82009-06-10 13:36:04 -07004338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 // Don't enable the screen until all existing windows
4340 // have been drawn.
4341 final int N = mWindows.size();
4342 for (int i=0; i<N; i++) {
4343 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08004344 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 return;
4346 }
4347 }
Romain Guy06882f82009-06-10 13:36:04 -07004348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 mDisplayEnabled = true;
4350 if (false) {
4351 Log.i(TAG, "ENABLING SCREEN!");
4352 StringWriter sw = new StringWriter();
4353 PrintWriter pw = new PrintWriter(sw);
4354 this.dump(null, pw, null);
4355 Log.i(TAG, sw.toString());
4356 }
4357 try {
4358 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4359 if (surfaceFlinger != null) {
4360 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4361 Parcel data = Parcel.obtain();
4362 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4363 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4364 data, null, 0);
4365 data.recycle();
4366 }
4367 } catch (RemoteException ex) {
4368 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4369 }
4370 }
Romain Guy06882f82009-06-10 13:36:04 -07004371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004375 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4376 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 }
Romain Guy06882f82009-06-10 13:36:04 -07004378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 public void setInTouchMode(boolean mode) {
4380 synchronized(mWindowMap) {
4381 mInTouchMode = mode;
4382 }
4383 }
4384
Romain Guy06882f82009-06-10 13:36:04 -07004385 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004386 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004388 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004389 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 }
4391
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004392 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 }
Romain Guy06882f82009-06-10 13:36:04 -07004394
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004395 public void setRotationUnchecked(int rotation,
4396 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 if(DEBUG_ORIENTATION) Log.v(TAG,
4398 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 long origId = Binder.clearCallingIdentity();
4401 boolean changed;
4402 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004403 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 }
Romain Guy06882f82009-06-10 13:36:04 -07004405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 if (changed) {
4407 sendNewConfiguration();
4408 synchronized(mWindowMap) {
4409 mLayoutNeeded = true;
4410 performLayoutAndPlaceSurfacesLocked();
4411 }
4412 } else if (alwaysSendConfiguration) {
4413 //update configuration ignoring orientation change
4414 sendNewConfiguration();
4415 }
Romain Guy06882f82009-06-10 13:36:04 -07004416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 Binder.restoreCallingIdentity(origId);
4418 }
Romain Guy06882f82009-06-10 13:36:04 -07004419
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004420 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 boolean changed;
4422 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4423 rotation = mRequestedRotation;
4424 } else {
4425 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004426 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 }
4428 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004429 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 mRotation, mDisplayEnabled);
4431 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4432 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004435 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 "Rotation changed to " + rotation
4437 + " from " + mRotation
4438 + " (forceApp=" + mForcedAppOrientation
4439 + ", req=" + mRequestedRotation + ")");
4440 mRotation = rotation;
4441 mWindowsFreezingScreen = true;
4442 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4443 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4444 2000);
4445 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004446 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 mQueue.setOrientation(rotation);
4448 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004449 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 }
4451 for (int i=mWindows.size()-1; i>=0; i--) {
4452 WindowState w = (WindowState)mWindows.get(i);
4453 if (w.mSurface != null) {
4454 w.mOrientationChanging = true;
4455 }
4456 }
4457 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4458 try {
4459 mRotationWatchers.get(i).onRotationChanged(rotation);
4460 } catch (RemoteException e) {
4461 }
4462 }
4463 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 return changed;
4466 }
Romain Guy06882f82009-06-10 13:36:04 -07004467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 public int getRotation() {
4469 return mRotation;
4470 }
4471
4472 public int watchRotation(IRotationWatcher watcher) {
4473 final IBinder watcherBinder = watcher.asBinder();
4474 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4475 public void binderDied() {
4476 synchronized (mWindowMap) {
4477 for (int i=0; i<mRotationWatchers.size(); i++) {
4478 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004479 IRotationWatcher removed = mRotationWatchers.remove(i);
4480 if (removed != null) {
4481 removed.asBinder().unlinkToDeath(this, 0);
4482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 i--;
4484 }
4485 }
4486 }
4487 }
4488 };
Romain Guy06882f82009-06-10 13:36:04 -07004489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004490 synchronized (mWindowMap) {
4491 try {
4492 watcher.asBinder().linkToDeath(dr, 0);
4493 mRotationWatchers.add(watcher);
4494 } catch (RemoteException e) {
4495 // Client died, no cleanup needed.
4496 }
Romain Guy06882f82009-06-10 13:36:04 -07004497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 return mRotation;
4499 }
4500 }
4501
4502 /**
4503 * Starts the view server on the specified port.
4504 *
4505 * @param port The port to listener to.
4506 *
4507 * @return True if the server was successfully started, false otherwise.
4508 *
4509 * @see com.android.server.ViewServer
4510 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4511 */
4512 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004513 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 return false;
4515 }
4516
4517 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4518 return false;
4519 }
4520
4521 if (port < 1024) {
4522 return false;
4523 }
4524
4525 if (mViewServer != null) {
4526 if (!mViewServer.isRunning()) {
4527 try {
4528 return mViewServer.start();
4529 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004530 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531 }
4532 }
4533 return false;
4534 }
4535
4536 try {
4537 mViewServer = new ViewServer(this, port);
4538 return mViewServer.start();
4539 } catch (IOException e) {
4540 Log.w(TAG, "View server did not start");
4541 }
4542 return false;
4543 }
4544
Romain Guy06882f82009-06-10 13:36:04 -07004545 private boolean isSystemSecure() {
4546 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4547 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4548 }
4549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 /**
4551 * Stops the view server if it exists.
4552 *
4553 * @return True if the server stopped, false if it wasn't started or
4554 * couldn't be stopped.
4555 *
4556 * @see com.android.server.ViewServer
4557 */
4558 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004559 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004560 return false;
4561 }
4562
4563 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4564 return false;
4565 }
4566
4567 if (mViewServer != null) {
4568 return mViewServer.stop();
4569 }
4570 return false;
4571 }
4572
4573 /**
4574 * Indicates whether the view server is running.
4575 *
4576 * @return True if the server is running, false otherwise.
4577 *
4578 * @see com.android.server.ViewServer
4579 */
4580 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004581 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 return false;
4583 }
4584
4585 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4586 return false;
4587 }
4588
4589 return mViewServer != null && mViewServer.isRunning();
4590 }
4591
4592 /**
4593 * Lists all availble windows in the system. The listing is written in the
4594 * specified Socket's output stream with the following syntax:
4595 * windowHashCodeInHexadecimal windowName
4596 * Each line of the ouput represents a different window.
4597 *
4598 * @param client The remote client to send the listing to.
4599 * @return False if an error occured, true otherwise.
4600 */
4601 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004602 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 return false;
4604 }
4605
4606 boolean result = true;
4607
4608 Object[] windows;
4609 synchronized (mWindowMap) {
4610 windows = new Object[mWindows.size()];
4611 //noinspection unchecked
4612 windows = mWindows.toArray(windows);
4613 }
4614
4615 BufferedWriter out = null;
4616
4617 // Any uncaught exception will crash the system process
4618 try {
4619 OutputStream clientStream = client.getOutputStream();
4620 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4621
4622 final int count = windows.length;
4623 for (int i = 0; i < count; i++) {
4624 final WindowState w = (WindowState) windows[i];
4625 out.write(Integer.toHexString(System.identityHashCode(w)));
4626 out.write(' ');
4627 out.append(w.mAttrs.getTitle());
4628 out.write('\n');
4629 }
4630
4631 out.write("DONE.\n");
4632 out.flush();
4633 } catch (Exception e) {
4634 result = false;
4635 } finally {
4636 if (out != null) {
4637 try {
4638 out.close();
4639 } catch (IOException e) {
4640 result = false;
4641 }
4642 }
4643 }
4644
4645 return result;
4646 }
4647
4648 /**
4649 * Sends a command to a target window. The result of the command, if any, will be
4650 * written in the output stream of the specified socket.
4651 *
4652 * The parameters must follow this syntax:
4653 * windowHashcode extra
4654 *
4655 * Where XX is the length in characeters of the windowTitle.
4656 *
4657 * The first parameter is the target window. The window with the specified hashcode
4658 * will be the target. If no target can be found, nothing happens. The extra parameters
4659 * will be delivered to the target window and as parameters to the command itself.
4660 *
4661 * @param client The remote client to sent the result, if any, to.
4662 * @param command The command to execute.
4663 * @param parameters The command parameters.
4664 *
4665 * @return True if the command was successfully delivered, false otherwise. This does
4666 * not indicate whether the command itself was successful.
4667 */
4668 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004669 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 return false;
4671 }
4672
4673 boolean success = true;
4674 Parcel data = null;
4675 Parcel reply = null;
4676
4677 // Any uncaught exception will crash the system process
4678 try {
4679 // Find the hashcode of the window
4680 int index = parameters.indexOf(' ');
4681 if (index == -1) {
4682 index = parameters.length();
4683 }
4684 final String code = parameters.substring(0, index);
Romain Guy236092a2009-12-14 15:31:48 -08004685 int hashCode = (int) Long.parseLong(code, 16);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686
4687 // Extract the command's parameter after the window description
4688 if (index < parameters.length()) {
4689 parameters = parameters.substring(index + 1);
4690 } else {
4691 parameters = "";
4692 }
4693
4694 final WindowManagerService.WindowState window = findWindow(hashCode);
4695 if (window == null) {
4696 return false;
4697 }
4698
4699 data = Parcel.obtain();
4700 data.writeInterfaceToken("android.view.IWindow");
4701 data.writeString(command);
4702 data.writeString(parameters);
4703 data.writeInt(1);
4704 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4705
4706 reply = Parcel.obtain();
4707
4708 final IBinder binder = window.mClient.asBinder();
4709 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4710 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4711
4712 reply.readException();
4713
4714 } catch (Exception e) {
4715 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4716 success = false;
4717 } finally {
4718 if (data != null) {
4719 data.recycle();
4720 }
4721 if (reply != null) {
4722 reply.recycle();
4723 }
4724 }
4725
4726 return success;
4727 }
4728
4729 private WindowState findWindow(int hashCode) {
4730 if (hashCode == -1) {
4731 return getFocusedWindow();
4732 }
4733
4734 synchronized (mWindowMap) {
4735 final ArrayList windows = mWindows;
4736 final int count = windows.size();
4737
4738 for (int i = 0; i < count; i++) {
4739 WindowState w = (WindowState) windows.get(i);
4740 if (System.identityHashCode(w) == hashCode) {
4741 return w;
4742 }
4743 }
4744 }
4745
4746 return null;
4747 }
4748
4749 /*
4750 * Instruct the Activity Manager to fetch the current configuration and broadcast
4751 * that to config-changed listeners if appropriate.
4752 */
4753 void sendNewConfiguration() {
4754 try {
4755 mActivityManager.updateConfiguration(null);
4756 } catch (RemoteException e) {
4757 }
4758 }
Romain Guy06882f82009-06-10 13:36:04 -07004759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 public Configuration computeNewConfiguration() {
4761 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004762 return computeNewConfigurationLocked();
4763 }
4764 }
Romain Guy06882f82009-06-10 13:36:04 -07004765
Dianne Hackbornc485a602009-03-24 22:39:49 -07004766 Configuration computeNewConfigurationLocked() {
4767 Configuration config = new Configuration();
4768 if (!computeNewConfigurationLocked(config)) {
4769 return null;
4770 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004771 return config;
4772 }
Romain Guy06882f82009-06-10 13:36:04 -07004773
Dianne Hackbornc485a602009-03-24 22:39:49 -07004774 boolean computeNewConfigurationLocked(Configuration config) {
4775 if (mDisplay == null) {
4776 return false;
4777 }
4778 mQueue.getInputConfiguration(config);
4779 final int dw = mDisplay.getWidth();
4780 final int dh = mDisplay.getHeight();
4781 int orientation = Configuration.ORIENTATION_SQUARE;
4782 if (dw < dh) {
4783 orientation = Configuration.ORIENTATION_PORTRAIT;
4784 } else if (dw > dh) {
4785 orientation = Configuration.ORIENTATION_LANDSCAPE;
4786 }
4787 config.orientation = orientation;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004788
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004789 DisplayMetrics dm = new DisplayMetrics();
4790 mDisplay.getMetrics(dm);
4791 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4792
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004793 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004794 // Note we only do this once because at this point we don't
4795 // expect the screen to change in this way at runtime, and want
4796 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004797 int longSize = dw;
4798 int shortSize = dh;
4799 if (longSize < shortSize) {
4800 int tmp = longSize;
4801 longSize = shortSize;
4802 shortSize = tmp;
4803 }
4804 longSize = (int)(longSize/dm.density);
4805 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004806
Dianne Hackborn723738c2009-06-25 19:48:04 -07004807 // These semi-magic numbers define our compatibility modes for
4808 // applications with different screens. Don't change unless you
4809 // make sure to test lots and lots of apps!
4810 if (longSize < 470) {
4811 // This is shorter than an HVGA normal density screen (which
4812 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004813 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4814 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004815 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004816 // Is this a large screen?
4817 if (longSize > 640 && shortSize >= 480) {
4818 // VGA or larger screens at medium density are the point
4819 // at which we consider it to be a large screen.
4820 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4821 } else {
4822 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004823
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004824 // If this screen is wider than normal HVGA, or taller
4825 // than FWVGA, then for old apps we want to run in size
4826 // compatibility mode.
4827 if (shortSize > 321 || longSize > 570) {
4828 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4829 }
4830 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004831
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004832 // Is this a long screen?
4833 if (((longSize*3)/5) >= (shortSize-1)) {
4834 // Anything wider than WVGA (5:3) is considering to be long.
4835 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4836 } else {
4837 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4838 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004839 }
4840 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004841 config.screenLayout = mScreenLayout;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004842
Dianne Hackbornc485a602009-03-24 22:39:49 -07004843 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4844 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4845 mPolicy.adjustConfigurationLw(config);
4846 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847 }
Romain Guy06882f82009-06-10 13:36:04 -07004848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004849 // -------------------------------------------------------------
4850 // Input Events and Focus Management
4851 // -------------------------------------------------------------
4852
4853 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004854 long curTime = SystemClock.uptimeMillis();
4855
Michael Chane10de972009-05-18 11:24:50 -07004856 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004857 if (mLastTouchEventType == eventType &&
4858 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4859 return;
4860 }
4861 mLastUserActivityCallTime = curTime;
4862 mLastTouchEventType = eventType;
4863 }
4864
4865 if (targetWin == null
4866 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4867 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 }
4869 }
4870
4871 // tells if it's a cheek event or not -- this function is stateful
4872 private static final int EVENT_NONE = 0;
4873 private static final int EVENT_UNKNOWN = 0;
4874 private static final int EVENT_CHEEK = 0;
4875 private static final int EVENT_IGNORE_DURATION = 300; // ms
4876 private static final float CHEEK_THRESHOLD = 0.6f;
4877 private int mEventState = EVENT_NONE;
4878 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 private int eventType(MotionEvent ev) {
4881 float size = ev.getSize();
4882 switch (ev.getAction()) {
4883 case MotionEvent.ACTION_DOWN:
4884 mEventSize = size;
4885 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4886 case MotionEvent.ACTION_UP:
4887 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004888 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004889 case MotionEvent.ACTION_MOVE:
4890 final int N = ev.getHistorySize();
4891 if (size > mEventSize) mEventSize = size;
4892 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4893 for (int i=0; i<N; i++) {
4894 size = ev.getHistoricalSize(i);
4895 if (size > mEventSize) mEventSize = size;
4896 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4897 }
4898 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4899 return TOUCH_EVENT;
4900 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004901 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 }
4903 default:
4904 // not good
4905 return OTHER_EVENT;
4906 }
4907 }
4908
4909 /**
4910 * @return Returns true if event was dispatched, false if it was dropped for any reason
4911 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004912 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004913 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4914 "dispatchPointer " + ev);
4915
Michael Chan53071d62009-05-13 17:29:48 -07004916 if (MEASURE_LATENCY) {
4917 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4918 }
4919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004920 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004921 ev, true, false, pid, uid);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004922
Michael Chan53071d62009-05-13 17:29:48 -07004923 if (MEASURE_LATENCY) {
4924 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4925 }
4926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 if (action == MotionEvent.ACTION_UP) {
4930 // let go of our target
4931 mKeyWaiter.mMotionTarget = null;
4932 mPowerManager.logPointerUpEvent();
4933 } else if (action == MotionEvent.ACTION_DOWN) {
4934 mPowerManager.logPointerDownEvent();
4935 }
4936
4937 if (targetObj == null) {
4938 // In this case we are either dropping the event, or have received
4939 // a move or up without a down. It is common to receive move
4940 // events in such a way, since this means the user is moving the
4941 // pointer without actually pressing down. All other cases should
4942 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004943 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4945 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08004946 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08004947 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08004948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004949 if (qev != null) {
4950 mQueue.recycleEvent(qev);
4951 }
4952 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004953 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004954 }
4955 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004956 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08004957 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08004958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 if (qev != null) {
4960 mQueue.recycleEvent(qev);
4961 }
4962 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004963 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004964 }
Romain Guy06882f82009-06-10 13:36:04 -07004965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004968 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004969 final long eventTimeNano = ev.getEventTimeNano();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004971 //Log.i(TAG, "Sending " + ev + " to " + target);
4972
4973 if (uid != 0 && uid != target.mSession.mUid) {
4974 if (mContext.checkPermission(
4975 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4976 != PackageManager.PERMISSION_GRANTED) {
4977 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4978 + pid + " uid " + uid + " to window " + target
4979 + " owned by uid " + target.mSession.mUid);
4980 if (qev != null) {
4981 mQueue.recycleEvent(qev);
4982 }
4983 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004984 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 }
4986 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004987
Michael Chan53071d62009-05-13 17:29:48 -07004988 if (MEASURE_LATENCY) {
4989 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4990 }
4991
Romain Guy06882f82009-06-10 13:36:04 -07004992 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004993 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4994 //target wants to ignore fat touch events
4995 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4996 //explicit flag to return without processing event further
4997 boolean returnFlag = false;
4998 if((action == MotionEvent.ACTION_DOWN)) {
4999 mFatTouch = false;
5000 if(cheekPress) {
5001 mFatTouch = true;
5002 returnFlag = true;
5003 }
5004 } else {
5005 if(action == MotionEvent.ACTION_UP) {
5006 if(mFatTouch) {
5007 //earlier even was invalid doesnt matter if current up is cheekpress or not
5008 mFatTouch = false;
5009 returnFlag = true;
5010 } else if(cheekPress) {
5011 //cancel the earlier event
5012 ev.setAction(MotionEvent.ACTION_CANCEL);
5013 action = MotionEvent.ACTION_CANCEL;
5014 }
5015 } else if(action == MotionEvent.ACTION_MOVE) {
5016 if(mFatTouch) {
5017 //two cases here
5018 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07005019 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 returnFlag = true;
5021 } else if(cheekPress) {
5022 //valid down followed by invalid moves
5023 //an invalid move have to cancel earlier action
5024 ev.setAction(MotionEvent.ACTION_CANCEL);
5025 action = MotionEvent.ACTION_CANCEL;
5026 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
5027 //note that the subsequent invalid moves will not get here
5028 mFatTouch = true;
5029 }
5030 }
5031 } //else if action
5032 if(returnFlag) {
5033 //recycle que, ev
5034 if (qev != null) {
5035 mQueue.recycleEvent(qev);
5036 }
5037 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005038 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005039 }
5040 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005041
Michael Chan9f028e62009-08-04 17:37:46 -07005042 // Enable this for testing the "right" value
5043 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005044 int max_events_per_sec = 35;
5045 try {
5046 max_events_per_sec = Integer.parseInt(SystemProperties
5047 .get("windowsmgr.max_events_per_sec"));
5048 if (max_events_per_sec < 1) {
5049 max_events_per_sec = 35;
5050 }
5051 } catch (NumberFormatException e) {
5052 }
5053 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5054 }
5055
5056 /*
5057 * Throttle events to minimize CPU usage when there's a flood of events
5058 * e.g. constant contact with the screen
5059 */
5060 if (action == MotionEvent.ACTION_MOVE) {
5061 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5062 long now = SystemClock.uptimeMillis();
5063 if (now < nextEventTime) {
5064 try {
5065 Thread.sleep(nextEventTime - now);
5066 } catch (InterruptedException e) {
5067 }
5068 mLastTouchEventTime = nextEventTime;
5069 } else {
5070 mLastTouchEventTime = now;
5071 }
5072 }
5073
Michael Chan53071d62009-05-13 17:29:48 -07005074 if (MEASURE_LATENCY) {
5075 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5076 }
5077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005078 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005079 if (!target.isVisibleLw()) {
5080 // During this motion dispatch, the target window has become
5081 // invisible.
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005082 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), false);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005083 if (qev != null) {
5084 mQueue.recycleEvent(qev);
5085 }
5086 ev.recycle();
5087 return INJECT_SUCCEEDED;
5088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5091 mKeyWaiter.bindTargetWindowLocked(target,
5092 KeyWaiter.RETURN_PENDING_POINTER, qev);
5093 ev = null;
5094 } else {
5095 if (action == MotionEvent.ACTION_DOWN) {
5096 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5097 if (out != null) {
5098 MotionEvent oev = MotionEvent.obtain(ev);
5099 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5100 do {
5101 final Rect frame = out.mFrame;
5102 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5103 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005104 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 } catch (android.os.RemoteException e) {
5106 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
5107 }
5108 oev.offsetLocation((float)frame.left, (float)frame.top);
5109 out = out.mNextOutsideTouch;
5110 } while (out != null);
5111 mKeyWaiter.mOutsideTouchTargets = null;
5112 }
5113 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005114
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005115 dispatchPointerElsewhereLocked(target, null, ev, ev.getEventTime(), false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005116
Dianne Hackborn6adba242009-11-10 11:10:09 -08005117 final Rect frame = target.mFrame;
5118 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5119 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 }
5121 }
Romain Guy06882f82009-06-10 13:36:04 -07005122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 // finally offset the event to the target's coordinate system and
5124 // dispatch the event.
5125 try {
5126 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
5127 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
5128 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005129
Michael Chan53071d62009-05-13 17:29:48 -07005130 if (MEASURE_LATENCY) {
5131 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5132 }
5133
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005134 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005135
5136 if (MEASURE_LATENCY) {
5137 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5138 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005139 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005140 } catch (android.os.RemoteException e) {
5141 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
5142 mKeyWaiter.mMotionTarget = null;
5143 try {
5144 removeWindow(target.mSession, target.mClient);
5145 } catch (java.util.NoSuchElementException ex) {
5146 // This will happen if the window has already been
5147 // removed.
5148 }
5149 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005150 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005151 }
Romain Guy06882f82009-06-10 13:36:04 -07005152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153 /**
5154 * @return Returns true if event was dispatched, false if it was dropped for any reason
5155 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005156 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 if (DEBUG_INPUT) Log.v(
5158 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005161 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 if (focusObj == null) {
5163 Log.w(TAG, "No focus window, dropping trackball: " + ev);
5164 if (qev != null) {
5165 mQueue.recycleEvent(qev);
5166 }
5167 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005168 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 }
5170 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5171 if (qev != null) {
5172 mQueue.recycleEvent(qev);
5173 }
5174 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005175 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 }
Romain Guy06882f82009-06-10 13:36:04 -07005177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005178 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 if (uid != 0 && uid != focus.mSession.mUid) {
5181 if (mContext.checkPermission(
5182 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5183 != PackageManager.PERMISSION_GRANTED) {
5184 Log.w(TAG, "Permission denied: injecting key event from pid "
5185 + pid + " uid " + uid + " to window " + focus
5186 + " owned by uid " + focus.mSession.mUid);
5187 if (qev != null) {
5188 mQueue.recycleEvent(qev);
5189 }
5190 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005191 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 }
5193 }
Romain Guy06882f82009-06-10 13:36:04 -07005194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 synchronized(mWindowMap) {
5198 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5199 mKeyWaiter.bindTargetWindowLocked(focus,
5200 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5201 // We don't deliver movement events to the client, we hold
5202 // them and wait for them to call back.
5203 ev = null;
5204 } else {
5205 mKeyWaiter.bindTargetWindowLocked(focus);
5206 }
5207 }
Romain Guy06882f82009-06-10 13:36:04 -07005208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005210 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005211 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 } catch (android.os.RemoteException e) {
5213 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5214 try {
5215 removeWindow(focus.mSession, focus.mClient);
5216 } catch (java.util.NoSuchElementException ex) {
5217 // This will happen if the window has already been
5218 // removed.
5219 }
5220 }
Romain Guy06882f82009-06-10 13:36:04 -07005221
Dianne Hackborncfaef692009-06-15 14:24:44 -07005222 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 }
Romain Guy06882f82009-06-10 13:36:04 -07005224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 /**
5226 * @return Returns true if event was dispatched, false if it was dropped for any reason
5227 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005228 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
5230
5231 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005232 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 if (focusObj == null) {
5234 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005235 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 }
5237 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005238 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 }
Romain Guy06882f82009-06-10 13:36:04 -07005240
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005241 // Okay we have finished waiting for the last event to be processed.
5242 // First off, if this is a repeat event, check to see if there is
5243 // a corresponding up event in the queue. If there is, we will
5244 // just drop the repeat, because it makes no sense to repeat after
5245 // the user has released a key. (This is especially important for
5246 // long presses.)
5247 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5248 return INJECT_SUCCEEDED;
5249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005251 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 if (DEBUG_INPUT) Log.v(
5254 TAG, "Dispatching to " + focus + ": " + event);
5255
5256 if (uid != 0 && uid != focus.mSession.mUid) {
5257 if (mContext.checkPermission(
5258 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5259 != PackageManager.PERMISSION_GRANTED) {
5260 Log.w(TAG, "Permission denied: injecting key event from pid "
5261 + pid + " uid " + uid + " to window " + focus
5262 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005263 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 }
5265 }
Romain Guy06882f82009-06-10 13:36:04 -07005266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 synchronized(mWindowMap) {
5268 mKeyWaiter.bindTargetWindowLocked(focus);
5269 }
5270
5271 // NOSHIP extra state logging
5272 mKeyWaiter.recordDispatchState(event, focus);
5273 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 try {
5276 if (DEBUG_INPUT || DEBUG_FOCUS) {
5277 Log.v(TAG, "Delivering key " + event.getKeyCode()
5278 + " to " + focus);
5279 }
5280 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005281 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005282 } catch (android.os.RemoteException e) {
5283 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
5284 try {
5285 removeWindow(focus.mSession, focus.mClient);
5286 } catch (java.util.NoSuchElementException ex) {
5287 // This will happen if the window has already been
5288 // removed.
5289 }
5290 }
Romain Guy06882f82009-06-10 13:36:04 -07005291
Dianne Hackborncfaef692009-06-15 14:24:44 -07005292 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005293 }
Romain Guy06882f82009-06-10 13:36:04 -07005294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 public void pauseKeyDispatching(IBinder _token) {
5296 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5297 "pauseKeyDispatching()")) {
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.pauseDispatchingLocked(token);
5305 }
5306 }
5307 }
5308
5309 public void resumeKeyDispatching(IBinder _token) {
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 WindowToken token = mTokenMap.get(_token);
5317 if (token != null) {
5318 mKeyWaiter.resumeDispatchingLocked(token);
5319 }
5320 }
5321 }
5322
5323 public void setEventDispatching(boolean enabled) {
5324 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5325 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005326 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005327 }
5328
5329 synchronized (mWindowMap) {
5330 mKeyWaiter.setEventDispatchingLocked(enabled);
5331 }
5332 }
Romain Guy06882f82009-06-10 13:36:04 -07005333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 /**
5335 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005336 *
5337 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 * {@link SystemClock#uptimeMillis()} as the timebase.)
5339 * @param sync If true, wait for the event to be completed before returning to the caller.
5340 * @return Returns true if event was dispatched, false if it was dropped for any reason
5341 */
5342 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5343 long downTime = ev.getDownTime();
5344 long eventTime = ev.getEventTime();
5345
5346 int action = ev.getAction();
5347 int code = ev.getKeyCode();
5348 int repeatCount = ev.getRepeatCount();
5349 int metaState = ev.getMetaState();
5350 int deviceId = ev.getDeviceId();
5351 int scancode = ev.getScanCode();
5352
5353 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5354 if (downTime == 0) downTime = eventTime;
5355
5356 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005357 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005358
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005359 final int pid = Binder.getCallingPid();
5360 final int uid = Binder.getCallingUid();
5361 final long ident = Binder.clearCallingIdentity();
5362 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005364 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005366 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005367 switch (result) {
5368 case INJECT_NO_PERMISSION:
5369 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305370 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005371 case INJECT_SUCCEEDED:
5372 return true;
5373 }
5374 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 }
5376
5377 /**
5378 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005379 *
5380 * @param ev A motion event describing the pointer (touch) action. (As noted in
5381 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 * {@link SystemClock#uptimeMillis()} as the timebase.)
5383 * @param sync If true, wait for the event to be completed before returning to the caller.
5384 * @return Returns true if event was dispatched, false if it was dropped for any reason
5385 */
5386 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005387 final int pid = Binder.getCallingPid();
5388 final int uid = Binder.getCallingUid();
5389 final long ident = Binder.clearCallingIdentity();
5390 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005392 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005394 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005395 switch (result) {
5396 case INJECT_NO_PERMISSION:
5397 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305398 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005399 case INJECT_SUCCEEDED:
5400 return true;
5401 }
5402 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 }
Romain Guy06882f82009-06-10 13:36:04 -07005404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 /**
5406 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005407 *
5408 * @param ev A motion event describing the trackball action. (As noted in
5409 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 * {@link SystemClock#uptimeMillis()} as the timebase.)
5411 * @param sync If true, wait for the event to be completed before returning to the caller.
5412 * @return Returns true if event was dispatched, false if it was dropped for any reason
5413 */
5414 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005415 final int pid = Binder.getCallingPid();
5416 final int uid = Binder.getCallingUid();
5417 final long ident = Binder.clearCallingIdentity();
5418 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005420 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005422 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005423 switch (result) {
5424 case INJECT_NO_PERMISSION:
5425 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305426 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005427 case INJECT_SUCCEEDED:
5428 return true;
5429 }
5430 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 }
Romain Guy06882f82009-06-10 13:36:04 -07005432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 private WindowState getFocusedWindow() {
5434 synchronized (mWindowMap) {
5435 return getFocusedWindowLocked();
5436 }
5437 }
5438
5439 private WindowState getFocusedWindowLocked() {
5440 return mCurrentFocus;
5441 }
Romain Guy06882f82009-06-10 13:36:04 -07005442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005443 /**
5444 * This class holds the state for dispatching key events. This state
5445 * is protected by the KeyWaiter instance, NOT by the window lock. You
5446 * can be holding the main window lock while acquire the KeyWaiter lock,
5447 * but not the other way around.
5448 */
5449 final class KeyWaiter {
5450 // NOSHIP debugging
5451 public class DispatchState {
5452 private KeyEvent event;
5453 private WindowState focus;
5454 private long time;
5455 private WindowState lastWin;
5456 private IBinder lastBinder;
5457 private boolean finished;
5458 private boolean gotFirstWindow;
5459 private boolean eventDispatching;
5460 private long timeToSwitch;
5461 private boolean wasFrozen;
5462 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005463 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5466 focus = theFocus;
5467 event = theEvent;
5468 time = System.currentTimeMillis();
5469 // snapshot KeyWaiter state
5470 lastWin = mLastWin;
5471 lastBinder = mLastBinder;
5472 finished = mFinished;
5473 gotFirstWindow = mGotFirstWindow;
5474 eventDispatching = mEventDispatching;
5475 timeToSwitch = mTimeToSwitch;
5476 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005477 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 // cache the paused state at ctor time as well
5479 if (theFocus == null || theFocus.mToken == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005480 focusPaused = false;
5481 } else {
5482 focusPaused = theFocus.mToken.paused;
5483 }
5484 }
Romain Guy06882f82009-06-10 13:36:04 -07005485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 public String toString() {
5487 return "{{" + event + " to " + focus + " @ " + time
5488 + " lw=" + lastWin + " lb=" + lastBinder
5489 + " fin=" + finished + " gfw=" + gotFirstWindow
5490 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005491 + " wf=" + wasFrozen + " fp=" + focusPaused
Christopher Tate46d45252010-02-09 15:48:57 -08005492 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 }
5494 };
5495 private DispatchState mDispatchState = null;
5496 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5497 mDispatchState = new DispatchState(theEvent, theFocus);
5498 }
5499 // END NOSHIP
5500
5501 public static final int RETURN_NOTHING = 0;
5502 public static final int RETURN_PENDING_POINTER = 1;
5503 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005505 final Object SKIP_TARGET_TOKEN = new Object();
5506 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 private WindowState mLastWin = null;
5509 private IBinder mLastBinder = null;
5510 private boolean mFinished = true;
5511 private boolean mGotFirstWindow = false;
5512 private boolean mEventDispatching = true;
5513 private long mTimeToSwitch = 0;
5514 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005516 // Target of Motion events
5517 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005519 // Windows above the target who would like to receive an "outside"
5520 // touch event for any down events outside of them.
5521 WindowState mOutsideTouchTargets;
5522
5523 /**
5524 * Wait for the last event dispatch to complete, then find the next
5525 * target that should receive the given event and wait for that one
5526 * to be ready to receive it.
5527 */
5528 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5529 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005530 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 long startTime = SystemClock.uptimeMillis();
5532 long keyDispatchingTimeout = 5 * 1000;
5533 long waitedFor = 0;
5534
5535 while (true) {
5536 // Figure out which window we care about. It is either the
5537 // last window we are waiting to have process the event or,
5538 // if none, then the next window we think the event should go
5539 // to. Note: we retrieve mLastWin outside of the lock, so
5540 // it may change before we lock. Thus we must check it again.
5541 WindowState targetWin = mLastWin;
5542 boolean targetIsNew = targetWin == null;
5543 if (DEBUG_INPUT) Log.v(
5544 TAG, "waitForLastKey: mFinished=" + mFinished +
5545 ", mLastWin=" + mLastWin);
5546 if (targetIsNew) {
5547 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005548 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005549 if (target == SKIP_TARGET_TOKEN) {
5550 // The user has pressed a special key, and we are
5551 // dropping all pending events before it.
5552 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5553 + " " + nextMotion);
5554 return null;
5555 }
5556 if (target == CONSUMED_EVENT_TOKEN) {
5557 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5558 + " " + nextMotion);
5559 return target;
5560 }
5561 targetWin = (WindowState)target;
5562 }
Romain Guy06882f82009-06-10 13:36:04 -07005563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 // Now: is it okay to send the next event to this window?
5567 synchronized (this) {
5568 // First: did we come here based on the last window not
5569 // being null, but it changed by the time we got here?
5570 // If so, try again.
5571 if (!targetIsNew && mLastWin == null) {
5572 continue;
5573 }
Romain Guy06882f82009-06-10 13:36:04 -07005574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005575 // We never dispatch events if not finished with the
5576 // last one, or the display is frozen.
5577 if (mFinished && !mDisplayFrozen) {
5578 // If event dispatching is disabled, then we
5579 // just consume the events.
5580 if (!mEventDispatching) {
5581 if (DEBUG_INPUT) Log.v(TAG,
5582 "Skipping event; dispatching disabled: "
5583 + nextKey + " " + nextMotion);
5584 return null;
5585 }
5586 if (targetWin != null) {
5587 // If this is a new target, and that target is not
5588 // paused or unresponsive, then all looks good to
5589 // handle the event.
5590 if (targetIsNew && !targetWin.mToken.paused) {
5591 return targetWin;
5592 }
Romain Guy06882f82009-06-10 13:36:04 -07005593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 // If we didn't find a target window, and there is no
5595 // focused app window, then just eat the events.
5596 } else if (mFocusedApp == null) {
5597 if (DEBUG_INPUT) Log.v(TAG,
5598 "Skipping event; no focused app: "
5599 + nextKey + " " + nextMotion);
5600 return null;
5601 }
5602 }
Romain Guy06882f82009-06-10 13:36:04 -07005603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 if (DEBUG_INPUT) Log.v(
5605 TAG, "Waiting for last key in " + mLastBinder
5606 + " target=" + targetWin
5607 + " mFinished=" + mFinished
5608 + " mDisplayFrozen=" + mDisplayFrozen
5609 + " targetIsNew=" + targetIsNew
5610 + " paused="
5611 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005612 + " mFocusedApp=" + mFocusedApp
5613 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 targetApp = targetWin != null
5616 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 long curTimeout = keyDispatchingTimeout;
5619 if (mTimeToSwitch != 0) {
5620 long now = SystemClock.uptimeMillis();
5621 if (mTimeToSwitch <= now) {
5622 // If an app switch key has been pressed, and we have
5623 // waited too long for the current app to finish
5624 // processing keys, then wait no more!
Christopher Tate136b1f92010-02-11 17:51:24 -08005625 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 continue;
5627 }
5628 long switchTimeout = mTimeToSwitch - now;
5629 if (curTimeout > switchTimeout) {
5630 curTimeout = switchTimeout;
5631 }
5632 }
Romain Guy06882f82009-06-10 13:36:04 -07005633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005634 try {
5635 // after that continue
5636 // processing keys, so we don't get stuck.
5637 if (DEBUG_INPUT) Log.v(
5638 TAG, "Waiting for key dispatch: " + curTimeout);
5639 wait(curTimeout);
5640 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5641 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005642 + startTime + " switchTime=" + mTimeToSwitch
5643 + " target=" + targetWin + " mLW=" + mLastWin
5644 + " mLB=" + mLastBinder + " fin=" + mFinished
5645 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 } catch (InterruptedException e) {
5647 }
5648 }
5649
5650 // If we were frozen during configuration change, restart the
5651 // timeout checks from now; otherwise look at whether we timed
5652 // out before awakening.
5653 if (mWasFrozen) {
5654 waitedFor = 0;
5655 mWasFrozen = false;
5656 } else {
5657 waitedFor = SystemClock.uptimeMillis() - startTime;
5658 }
5659
5660 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5661 IApplicationToken at = null;
5662 synchronized (this) {
5663 Log.w(TAG, "Key dispatching timed out sending to " +
5664 (targetWin != null ? targetWin.mAttrs.getTitle()
Ken Shirriff8200b202010-02-04 13:34:37 -08005665 : "<null>: no window ready for key dispatch"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 // NOSHIP debugging
Ken Shirriff8200b202010-02-04 13:34:37 -08005667 Log.w(TAG, "Previous dispatch state: " + mDispatchState);
5668 Log.w(TAG, "Current dispatch state: " +
5669 new DispatchState(nextKey, targetWin));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670 // END NOSHIP
5671 //dump();
5672 if (targetWin != null) {
5673 at = targetWin.getAppToken();
5674 } else if (targetApp != null) {
5675 at = targetApp.appToken;
5676 }
5677 }
5678
5679 boolean abort = true;
5680 if (at != null) {
5681 try {
5682 long timeout = at.getKeyDispatchingTimeout();
5683 if (timeout > waitedFor) {
5684 // we did not wait the proper amount of time for this application.
5685 // set the timeout to be the real timeout and wait again.
5686 keyDispatchingTimeout = timeout - waitedFor;
5687 continue;
5688 } else {
5689 abort = at.keyDispatchingTimedOut();
5690 }
5691 } catch (RemoteException ex) {
5692 }
5693 }
5694
5695 synchronized (this) {
5696 if (abort && (mLastWin == targetWin || targetWin == null)) {
5697 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005698 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 if (DEBUG_INPUT) Log.v(TAG,
5700 "Window " + mLastWin +
5701 " timed out on key input");
5702 if (mLastWin.mToken.paused) {
5703 Log.w(TAG, "Un-pausing dispatching to this window");
5704 mLastWin.mToken.paused = false;
5705 }
5706 }
5707 if (mMotionTarget == targetWin) {
5708 mMotionTarget = null;
5709 }
5710 mLastWin = null;
5711 mLastBinder = null;
5712 if (failIfTimeout || targetWin == null) {
5713 return null;
5714 }
5715 } else {
5716 Log.w(TAG, "Continuing to wait for key to be dispatched");
5717 startTime = SystemClock.uptimeMillis();
5718 }
5719 }
5720 }
5721 }
5722 }
Romain Guy06882f82009-06-10 13:36:04 -07005723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005725 MotionEvent nextMotion, boolean isPointerEvent,
5726 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 if (nextKey != null) {
5730 // Find the target window for a normal key event.
5731 final int keycode = nextKey.getKeyCode();
5732 final int repeatCount = nextKey.getRepeatCount();
5733 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5734 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005736 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005737 if (callingUid == 0 ||
5738 mContext.checkPermission(
5739 android.Manifest.permission.INJECT_EVENTS,
5740 callingPid, callingUid)
5741 == PackageManager.PERMISSION_GRANTED) {
5742 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005743 nextKey.getMetaState(), down, repeatCount,
5744 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 Log.w(TAG, "Event timeout during app switch: dropping "
5747 + nextKey);
5748 return SKIP_TARGET_TOKEN;
5749 }
Romain Guy06882f82009-06-10 13:36:04 -07005750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005753 WindowState focus = null;
5754 synchronized(mWindowMap) {
5755 focus = getFocusedWindowLocked();
5756 }
Romain Guy06882f82009-06-10 13:36:04 -07005757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005759
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005760 if (callingUid == 0 ||
5761 (focus != null && callingUid == focus.mSession.mUid) ||
5762 mContext.checkPermission(
5763 android.Manifest.permission.INJECT_EVENTS,
5764 callingPid, callingUid)
5765 == PackageManager.PERMISSION_GRANTED) {
5766 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005767 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005768 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005769 return CONSUMED_EVENT_TOKEN;
5770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771 }
Romain Guy06882f82009-06-10 13:36:04 -07005772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 } else if (!isPointerEvent) {
5776 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5777 if (!dispatch) {
5778 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5779 + nextMotion);
5780 return SKIP_TARGET_TOKEN;
5781 }
Romain Guy06882f82009-06-10 13:36:04 -07005782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 WindowState focus = null;
5784 synchronized(mWindowMap) {
5785 focus = getFocusedWindowLocked();
5786 }
Romain Guy06882f82009-06-10 13:36:04 -07005787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5789 return focus;
5790 }
Romain Guy06882f82009-06-10 13:36:04 -07005791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 if (nextMotion == null) {
5793 return SKIP_TARGET_TOKEN;
5794 }
Romain Guy06882f82009-06-10 13:36:04 -07005795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5797 KeyEvent.KEYCODE_UNKNOWN);
5798 if (!dispatch) {
5799 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5800 + nextMotion);
5801 return SKIP_TARGET_TOKEN;
5802 }
Romain Guy06882f82009-06-10 13:36:04 -07005803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804 // Find the target window for a pointer event.
5805 int action = nextMotion.getAction();
5806 final float xf = nextMotion.getX();
5807 final float yf = nextMotion.getY();
5808 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 final boolean screenWasOff = qev != null
5811 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005813 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005815 synchronized(mWindowMap) {
5816 synchronized (this) {
5817 if (action == MotionEvent.ACTION_DOWN) {
5818 if (mMotionTarget != null) {
5819 // this is weird, we got a pen down, but we thought it was
5820 // already down!
5821 // XXX: We should probably send an ACTION_UP to the current
5822 // target.
5823 Log.w(TAG, "Pointer down received while already down in: "
5824 + mMotionTarget);
5825 mMotionTarget = null;
5826 }
Romain Guy06882f82009-06-10 13:36:04 -07005827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 // ACTION_DOWN is special, because we need to lock next events to
5829 // the window we'll land onto.
5830 final int x = (int)xf;
5831 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005833 final ArrayList windows = mWindows;
5834 final int N = windows.size();
5835 WindowState topErrWindow = null;
5836 final Rect tmpRect = mTempRect;
5837 for (int i=N-1; i>=0; i--) {
5838 WindowState child = (WindowState)windows.get(i);
5839 //Log.i(TAG, "Checking dispatch to: " + child);
5840 final int flags = child.mAttrs.flags;
5841 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5842 if (topErrWindow == null) {
5843 topErrWindow = child;
5844 }
5845 }
5846 if (!child.isVisibleLw()) {
5847 //Log.i(TAG, "Not visible!");
5848 continue;
5849 }
5850 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5851 //Log.i(TAG, "Not touchable!");
5852 if ((flags & WindowManager.LayoutParams
5853 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5854 child.mNextOutsideTouch = mOutsideTouchTargets;
5855 mOutsideTouchTargets = child;
5856 }
5857 continue;
5858 }
5859 tmpRect.set(child.mFrame);
5860 if (child.mTouchableInsets == ViewTreeObserver
5861 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5862 // The touch is inside of the window if it is
5863 // inside the frame, AND the content part of that
5864 // frame that was given by the application.
5865 tmpRect.left += child.mGivenContentInsets.left;
5866 tmpRect.top += child.mGivenContentInsets.top;
5867 tmpRect.right -= child.mGivenContentInsets.right;
5868 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5869 } else if (child.mTouchableInsets == ViewTreeObserver
5870 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5871 // The touch is inside of the window if it is
5872 // inside the frame, AND the visible part of that
5873 // frame that was given by the application.
5874 tmpRect.left += child.mGivenVisibleInsets.left;
5875 tmpRect.top += child.mGivenVisibleInsets.top;
5876 tmpRect.right -= child.mGivenVisibleInsets.right;
5877 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5878 }
5879 final int touchFlags = flags &
5880 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5881 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5882 if (tmpRect.contains(x, y) || touchFlags == 0) {
5883 //Log.i(TAG, "Using this target!");
5884 if (!screenWasOff || (flags &
5885 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5886 mMotionTarget = child;
5887 } else {
5888 //Log.i(TAG, "Waking, skip!");
5889 mMotionTarget = null;
5890 }
5891 break;
5892 }
Romain Guy06882f82009-06-10 13:36:04 -07005893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 if ((flags & WindowManager.LayoutParams
5895 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5896 child.mNextOutsideTouch = mOutsideTouchTargets;
5897 mOutsideTouchTargets = child;
5898 //Log.i(TAG, "Adding to outside target list: " + child);
5899 }
5900 }
5901
5902 // if there's an error window but it's not accepting
5903 // focus (typically because it is not yet visible) just
5904 // wait for it -- any other focused window may in fact
5905 // be in ANR state.
5906 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5907 mMotionTarget = null;
5908 }
5909 }
Romain Guy06882f82009-06-10 13:36:04 -07005910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 target = mMotionTarget;
5912 }
5913 }
Romain Guy06882f82009-06-10 13:36:04 -07005914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 // Pointer events are a little different -- if there isn't a
5918 // target found for any event, then just drop it.
5919 return target != null ? target : SKIP_TARGET_TOKEN;
5920 }
Romain Guy06882f82009-06-10 13:36:04 -07005921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922 boolean checkShouldDispatchKey(int keycode) {
5923 synchronized (this) {
5924 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5925 mTimeToSwitch = 0;
5926 return true;
5927 }
5928 if (mTimeToSwitch != 0
5929 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5930 return false;
5931 }
5932 return true;
5933 }
5934 }
Romain Guy06882f82009-06-10 13:36:04 -07005935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 void bindTargetWindowLocked(WindowState win,
5937 int pendingWhat, QueuedEvent pendingMotion) {
5938 synchronized (this) {
5939 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5940 }
5941 }
Romain Guy06882f82009-06-10 13:36:04 -07005942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943 void bindTargetWindowLocked(WindowState win) {
5944 synchronized (this) {
5945 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5946 }
5947 }
5948
5949 void bindTargetWindowLockedLocked(WindowState win,
5950 int pendingWhat, QueuedEvent pendingMotion) {
5951 mLastWin = win;
5952 mLastBinder = win.mClient.asBinder();
5953 mFinished = false;
5954 if (pendingMotion != null) {
5955 final Session s = win.mSession;
5956 if (pendingWhat == RETURN_PENDING_POINTER) {
5957 releasePendingPointerLocked(s);
5958 s.mPendingPointerMove = pendingMotion;
5959 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005960 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 "bindTargetToWindow " + s.mPendingPointerMove);
5962 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5963 releasePendingTrackballLocked(s);
5964 s.mPendingTrackballMove = pendingMotion;
5965 s.mPendingTrackballWindow = win;
5966 }
5967 }
5968 }
Romain Guy06882f82009-06-10 13:36:04 -07005969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 void releasePendingPointerLocked(Session s) {
5971 if (DEBUG_INPUT) Log.v(TAG,
5972 "releasePendingPointer " + s.mPendingPointerMove);
5973 if (s.mPendingPointerMove != null) {
5974 mQueue.recycleEvent(s.mPendingPointerMove);
5975 s.mPendingPointerMove = null;
5976 }
5977 }
Romain Guy06882f82009-06-10 13:36:04 -07005978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005979 void releasePendingTrackballLocked(Session s) {
5980 if (s.mPendingTrackballMove != null) {
5981 mQueue.recycleEvent(s.mPendingTrackballMove);
5982 s.mPendingTrackballMove = null;
5983 }
5984 }
Romain Guy06882f82009-06-10 13:36:04 -07005985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005986 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5987 int returnWhat) {
5988 if (DEBUG_INPUT) Log.v(
5989 TAG, "finishedKey: client=" + client + ", force=" + force);
5990
5991 if (client == null) {
5992 return null;
5993 }
5994
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005995 MotionEvent res = null;
5996 QueuedEvent qev = null;
5997 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005999 synchronized (this) {
6000 if (DEBUG_INPUT) Log.v(
6001 TAG, "finishedKey: client=" + client.asBinder()
6002 + ", force=" + force + ", last=" + mLastBinder
6003 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
6004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 if (returnWhat == RETURN_PENDING_POINTER) {
6006 qev = session.mPendingPointerMove;
6007 win = session.mPendingPointerWindow;
6008 session.mPendingPointerMove = null;
6009 session.mPendingPointerWindow = null;
6010 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
6011 qev = session.mPendingTrackballMove;
6012 win = session.mPendingTrackballWindow;
6013 session.mPendingTrackballMove = null;
6014 session.mPendingTrackballWindow = null;
6015 }
Romain Guy06882f82009-06-10 13:36:04 -07006016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 if (mLastBinder == client.asBinder()) {
6018 if (DEBUG_INPUT) Log.v(
6019 TAG, "finishedKey: last paused="
6020 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6021 if (mLastWin != null && (!mLastWin.mToken.paused || force
6022 || !mEventDispatching)) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006023 doFinishedKeyLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 } else {
6025 // Make sure to wake up anyone currently waiting to
6026 // dispatch a key, so they can re-evaluate their
6027 // current situation.
6028 mFinished = true;
6029 notifyAll();
6030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 }
Romain Guy06882f82009-06-10 13:36:04 -07006032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006034 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006035 if (DEBUG_INPUT) Log.v(TAG,
6036 "Returning pending motion: " + res);
6037 mQueue.recycleEvent(qev);
6038 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6039 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006043
Christopher Tate2624fbc2009-12-11 12:11:31 -08006044 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6045 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08006046 dispatchPointerElsewhereLocked(win, win, res, res.getEventTime(), false);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006047 }
6048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006049
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006050 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 }
6052
6053 void tickle() {
6054 synchronized (this) {
6055 notifyAll();
6056 }
6057 }
Romain Guy06882f82009-06-10 13:36:04 -07006058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006059 void handleNewWindowLocked(WindowState newWindow) {
6060 if (!newWindow.canReceiveKeys()) {
6061 return;
6062 }
6063 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006064 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006065 TAG, "New key dispatch window: win="
6066 + newWindow.mClient.asBinder()
6067 + ", last=" + mLastBinder
6068 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6069 + "), finished=" + mFinished + ", paused="
6070 + newWindow.mToken.paused);
6071
6072 // Displaying a window implicitly causes dispatching to
6073 // be unpaused. (This is to protect against bugs if someone
6074 // pauses dispatching but forgets to resume.)
6075 newWindow.mToken.paused = false;
6076
6077 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078
6079 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
6080 if (DEBUG_INPUT) Log.v(TAG,
6081 "New SYSTEM_ERROR window; resetting state");
6082 mLastWin = null;
6083 mLastBinder = null;
6084 mMotionTarget = null;
6085 mFinished = true;
6086 } else if (mLastWin != null) {
6087 // If the new window is above the window we are
6088 // waiting on, then stop waiting and let key dispatching
6089 // start on the new guy.
6090 if (DEBUG_INPUT) Log.v(
6091 TAG, "Last win layer=" + mLastWin.mLayer
6092 + ", new win layer=" + newWindow.mLayer);
6093 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006094 // The new window is above the old; finish pending input to the last
6095 // window and start directing it to the new one.
6096 mLastWin.mToken.paused = false;
Christopher Tate136b1f92010-02-11 17:51:24 -08006097 doFinishedKeyLocked(false); // does a notifyAll()
6098 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 }
6100 }
6101
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006102 // Now that we've put a new window state in place, make the event waiter
6103 // take notice and retarget its attentions.
6104 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006105 }
6106 }
6107
6108 void pauseDispatchingLocked(WindowToken token) {
6109 synchronized (this)
6110 {
6111 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
6112 token.paused = true;
6113
6114 /*
6115 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6116 mPaused = true;
6117 } else {
6118 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07006119 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006120 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07006121 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07006123 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006124 }
6125 }
6126 */
6127 }
6128 }
6129
6130 void resumeDispatchingLocked(WindowToken token) {
6131 synchronized (this) {
6132 if (token.paused) {
6133 if (DEBUG_INPUT) Log.v(
6134 TAG, "Resuming WindowToken " + token
6135 + ", last=" + mLastBinder
6136 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6137 + "), finished=" + mFinished + ", paused="
6138 + token.paused);
6139 token.paused = false;
6140 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006141 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006142 } else {
6143 notifyAll();
6144 }
6145 }
6146 }
6147 }
6148
6149 void setEventDispatchingLocked(boolean enabled) {
6150 synchronized (this) {
6151 mEventDispatching = enabled;
6152 notifyAll();
6153 }
6154 }
Romain Guy06882f82009-06-10 13:36:04 -07006155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 void appSwitchComing() {
6157 synchronized (this) {
6158 // Don't wait for more than .5 seconds for app to finish
6159 // processing the pending events.
6160 long now = SystemClock.uptimeMillis() + 500;
6161 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
6162 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6163 mTimeToSwitch = now;
6164 }
6165 notifyAll();
6166 }
6167 }
Romain Guy06882f82009-06-10 13:36:04 -07006168
Christopher Tate136b1f92010-02-11 17:51:24 -08006169 private final void doFinishedKeyLocked(boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006170 if (mLastWin != null) {
6171 releasePendingPointerLocked(mLastWin.mSession);
6172 releasePendingTrackballLocked(mLastWin.mSession);
6173 }
Romain Guy06882f82009-06-10 13:36:04 -07006174
Christopher Tate136b1f92010-02-11 17:51:24 -08006175 if (force || mLastWin == null || !mLastWin.mToken.paused
6176 || !mLastWin.isVisibleLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 // If the current window has been paused, we aren't -really-
6178 // finished... so let the waiters still wait.
6179 mLastWin = null;
6180 mLastBinder = null;
6181 }
6182 mFinished = true;
6183 notifyAll();
6184 }
6185 }
6186
6187 private class KeyQ extends KeyInputQueue
6188 implements KeyInputQueue.FilterCallback {
6189 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006191 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006192 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006193 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6194 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6195 "KEEP_SCREEN_ON_FLAG");
6196 mHoldingScreen.setReferenceCounted(false);
6197 }
6198
6199 @Override
6200 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6201 if (mPolicy.preprocessInputEventTq(event)) {
6202 return true;
6203 }
Romain Guy06882f82009-06-10 13:36:04 -07006204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 switch (event.type) {
6206 case RawInputEvent.EV_KEY: {
6207 // XXX begin hack
6208 if (DEBUG) {
6209 if (event.keycode == KeyEvent.KEYCODE_G) {
6210 if (event.value != 0) {
6211 // G down
6212 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6213 }
6214 return false;
6215 }
6216 if (event.keycode == KeyEvent.KEYCODE_D) {
6217 if (event.value != 0) {
6218 //dump();
6219 }
6220 return false;
6221 }
6222 }
6223 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006224
Charles Mendis322591c2009-10-29 11:06:59 -07006225 boolean screenIsOff = !mPowerManager.isScreenOn();
6226 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006227 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6230 mPowerManager.goToSleep(event.when);
6231 }
6232
6233 if (screenIsOff) {
6234 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6235 }
6236 if (screenIsDim) {
6237 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6238 }
6239 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6240 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006241 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006242 }
Romain Guy06882f82009-06-10 13:36:04 -07006243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6245 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6246 filterQueue(this);
6247 mKeyWaiter.appSwitchComing();
6248 }
6249 return true;
6250 } else {
6251 return false;
6252 }
6253 }
Romain Guy06882f82009-06-10 13:36:04 -07006254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006256 boolean screenIsOff = !mPowerManager.isScreenOn();
6257 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 if (screenIsOff) {
6259 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6260 device.classes, event)) {
6261 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6262 return false;
6263 }
6264 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6265 }
6266 if (screenIsDim) {
6267 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6268 }
6269 return true;
6270 }
Romain Guy06882f82009-06-10 13:36:04 -07006271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006273 boolean screenIsOff = !mPowerManager.isScreenOn();
6274 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006275 if (screenIsOff) {
6276 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6277 device.classes, event)) {
6278 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
6279 return false;
6280 }
6281 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6282 }
6283 if (screenIsDim) {
6284 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6285 }
6286 return true;
6287 }
Romain Guy06882f82009-06-10 13:36:04 -07006288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006289 default:
6290 return true;
6291 }
6292 }
6293
6294 public int filterEvent(QueuedEvent ev) {
6295 switch (ev.classType) {
6296 case RawInputEvent.CLASS_KEYBOARD:
6297 KeyEvent ke = (KeyEvent)ev.event;
6298 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
6299 Log.w(TAG, "Dropping movement key during app switch: "
6300 + ke.getKeyCode() + ", action=" + ke.getAction());
6301 return FILTER_REMOVE;
6302 }
6303 return FILTER_ABORT;
6304 default:
6305 return FILTER_KEEP;
6306 }
6307 }
Romain Guy06882f82009-06-10 13:36:04 -07006308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309 /**
6310 * Must be called with the main window manager lock held.
6311 */
6312 void setHoldScreenLocked(boolean holding) {
6313 boolean state = mHoldingScreen.isHeld();
6314 if (holding != state) {
6315 if (holding) {
6316 mHoldingScreen.acquire();
6317 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006318 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 mHoldingScreen.release();
6320 }
6321 }
6322 }
Michael Chan53071d62009-05-13 17:29:48 -07006323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324
6325 public boolean detectSafeMode() {
6326 mSafeMode = mPolicy.detectSafeMode();
6327 return mSafeMode;
6328 }
Romain Guy06882f82009-06-10 13:36:04 -07006329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006330 public void systemReady() {
6331 mPolicy.systemReady();
6332 }
Romain Guy06882f82009-06-10 13:36:04 -07006333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 private final class InputDispatcherThread extends Thread {
6335 // Time to wait when there is nothing to do: 9999 seconds.
6336 static final int LONG_WAIT=9999*1000;
6337
6338 public InputDispatcherThread() {
6339 super("InputDispatcher");
6340 }
Romain Guy06882f82009-06-10 13:36:04 -07006341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342 @Override
6343 public void run() {
6344 while (true) {
6345 try {
6346 process();
6347 } catch (Exception e) {
6348 Log.e(TAG, "Exception in input dispatcher", e);
6349 }
6350 }
6351 }
Romain Guy06882f82009-06-10 13:36:04 -07006352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 private void process() {
6354 android.os.Process.setThreadPriority(
6355 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 // The last key event we saw
6358 KeyEvent lastKey = null;
6359
6360 // Last keydown time for auto-repeating keys
6361 long lastKeyTime = SystemClock.uptimeMillis();
6362 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006363 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006364
Romain Guy06882f82009-06-10 13:36:04 -07006365 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 int keyRepeatCount = 0;
6367
6368 // Need to report that configuration has changed?
6369 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006371 while (true) {
6372 long curTime = SystemClock.uptimeMillis();
6373
6374 if (DEBUG_INPUT) Log.v(
6375 TAG, "Waiting for next key: now=" + curTime
6376 + ", repeat @ " + nextKeyTime);
6377
6378 // Retrieve next event, waiting only as long as the next
6379 // repeat timeout. If the configuration has changed, then
6380 // don't wait at all -- we'll report the change as soon as
6381 // we have processed all events.
6382 QueuedEvent ev = mQueue.getEvent(
6383 (int)((!configChanged && curTime < nextKeyTime)
6384 ? (nextKeyTime-curTime) : 0));
6385
6386 if (DEBUG_INPUT && ev != null) Log.v(
6387 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6388
Michael Chan53071d62009-05-13 17:29:48 -07006389 if (MEASURE_LATENCY) {
6390 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6391 }
6392
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006393 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6394 // cancel key repeat at the request of the policy.
6395 lastKey = null;
6396 downTime = 0;
6397 lastKeyTime = curTime;
6398 nextKeyTime = curTime + LONG_WAIT;
6399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006400 try {
6401 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006402 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 int eventType;
6404 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6405 eventType = eventType((MotionEvent)ev.event);
6406 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6407 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6408 eventType = LocalPowerManager.BUTTON_EVENT;
6409 } else {
6410 eventType = LocalPowerManager.OTHER_EVENT;
6411 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006412 try {
Michael Chan53071d62009-05-13 17:29:48 -07006413 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006414 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006415 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006416 mBatteryStats.noteInputEvent();
6417 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006418 } catch (RemoteException e) {
6419 // Ignore
6420 }
Michael Chane10de972009-05-18 11:24:50 -07006421
Mike Lockwood5db42402009-11-30 14:51:51 -05006422 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6423 // do not wake screen in this case
6424 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006425 && eventType != LONG_TOUCH_EVENT
6426 && eventType != CHEEK_EVENT) {
6427 mPowerManager.userActivity(curTime, false,
6428 eventType, false);
6429 } else if (mLastTouchEventType != eventType
6430 || (curTime - mLastUserActivityCallTime)
6431 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6432 mLastUserActivityCallTime = curTime;
6433 mLastTouchEventType = eventType;
6434 mPowerManager.userActivity(curTime, false,
6435 eventType, false);
6436 }
6437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 switch (ev.classType) {
6439 case RawInputEvent.CLASS_KEYBOARD:
6440 KeyEvent ke = (KeyEvent)ev.event;
6441 if (ke.isDown()) {
6442 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006443 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 keyRepeatCount = 0;
6445 lastKeyTime = curTime;
6446 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006447 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 if (DEBUG_INPUT) Log.v(
6449 TAG, "Received key down: first repeat @ "
6450 + nextKeyTime);
6451 } else {
6452 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006453 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 // Arbitrary long timeout.
6455 lastKeyTime = curTime;
6456 nextKeyTime = curTime + LONG_WAIT;
6457 if (DEBUG_INPUT) Log.v(
6458 TAG, "Received key up: ignore repeat @ "
6459 + nextKeyTime);
6460 }
6461 dispatchKey((KeyEvent)ev.event, 0, 0);
6462 mQueue.recycleEvent(ev);
6463 break;
6464 case RawInputEvent.CLASS_TOUCHSCREEN:
6465 //Log.i(TAG, "Read next event " + ev);
6466 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6467 break;
6468 case RawInputEvent.CLASS_TRACKBALL:
6469 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6470 break;
6471 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6472 configChanged = true;
6473 break;
6474 default:
6475 mQueue.recycleEvent(ev);
6476 break;
6477 }
Romain Guy06882f82009-06-10 13:36:04 -07006478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 } else if (configChanged) {
6480 configChanged = false;
6481 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006483 } else if (lastKey != null) {
6484 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006486 // Timeout occurred while key was down. If it is at or
6487 // past the key repeat time, dispatch the repeat.
6488 if (DEBUG_INPUT) Log.v(
6489 TAG, "Key timeout: repeat=" + nextKeyTime
6490 + ", now=" + curTime);
6491 if (curTime < nextKeyTime) {
6492 continue;
6493 }
Romain Guy06882f82009-06-10 13:36:04 -07006494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 lastKeyTime = nextKeyTime;
6496 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6497 keyRepeatCount++;
6498 if (DEBUG_INPUT) Log.v(
6499 TAG, "Key repeat: count=" + keyRepeatCount
6500 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006501 KeyEvent newEvent;
6502 if (downTime != 0 && (downTime
6503 + ViewConfiguration.getLongPressTimeout())
6504 <= curTime) {
6505 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6506 curTime, keyRepeatCount,
6507 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6508 downTime = 0;
6509 } else {
6510 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6511 curTime, keyRepeatCount);
6512 }
6513 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006515 } else {
6516 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006518 lastKeyTime = curTime;
6519 nextKeyTime = curTime + LONG_WAIT;
6520 }
Romain Guy06882f82009-06-10 13:36:04 -07006521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006522 } catch (Exception e) {
6523 Log.e(TAG,
6524 "Input thread received uncaught exception: " + e, e);
6525 }
6526 }
6527 }
6528 }
6529
6530 // -------------------------------------------------------------
6531 // Client Session State
6532 // -------------------------------------------------------------
6533
6534 private final class Session extends IWindowSession.Stub
6535 implements IBinder.DeathRecipient {
6536 final IInputMethodClient mClient;
6537 final IInputContext mInputContext;
6538 final int mUid;
6539 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006540 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006541 SurfaceSession mSurfaceSession;
6542 int mNumWindow = 0;
6543 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006545 /**
6546 * Current pointer move event being dispatched to client window... must
6547 * hold key lock to access.
6548 */
6549 QueuedEvent mPendingPointerMove;
6550 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006552 /**
6553 * Current trackball move event being dispatched to client window... must
6554 * hold key lock to access.
6555 */
6556 QueuedEvent mPendingTrackballMove;
6557 WindowState mPendingTrackballWindow;
6558
6559 public Session(IInputMethodClient client, IInputContext inputContext) {
6560 mClient = client;
6561 mInputContext = inputContext;
6562 mUid = Binder.getCallingUid();
6563 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006564 StringBuilder sb = new StringBuilder();
6565 sb.append("Session{");
6566 sb.append(Integer.toHexString(System.identityHashCode(this)));
6567 sb.append(" uid ");
6568 sb.append(mUid);
6569 sb.append("}");
6570 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006572 synchronized (mWindowMap) {
6573 if (mInputMethodManager == null && mHaveInputMethods) {
6574 IBinder b = ServiceManager.getService(
6575 Context.INPUT_METHOD_SERVICE);
6576 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6577 }
6578 }
6579 long ident = Binder.clearCallingIdentity();
6580 try {
6581 // Note: it is safe to call in to the input method manager
6582 // here because we are not holding our lock.
6583 if (mInputMethodManager != null) {
6584 mInputMethodManager.addClient(client, inputContext,
6585 mUid, mPid);
6586 } else {
6587 client.setUsingInputMethod(false);
6588 }
6589 client.asBinder().linkToDeath(this, 0);
6590 } catch (RemoteException e) {
6591 // The caller has died, so we can just forget about this.
6592 try {
6593 if (mInputMethodManager != null) {
6594 mInputMethodManager.removeClient(client);
6595 }
6596 } catch (RemoteException ee) {
6597 }
6598 } finally {
6599 Binder.restoreCallingIdentity(ident);
6600 }
6601 }
Romain Guy06882f82009-06-10 13:36:04 -07006602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006603 @Override
6604 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6605 throws RemoteException {
6606 try {
6607 return super.onTransact(code, data, reply, flags);
6608 } catch (RuntimeException e) {
6609 // Log all 'real' exceptions thrown to the caller
6610 if (!(e instanceof SecurityException)) {
6611 Log.e(TAG, "Window Session Crash", e);
6612 }
6613 throw e;
6614 }
6615 }
6616
6617 public void binderDied() {
6618 // Note: it is safe to call in to the input method manager
6619 // here because we are not holding our lock.
6620 try {
6621 if (mInputMethodManager != null) {
6622 mInputMethodManager.removeClient(mClient);
6623 }
6624 } catch (RemoteException e) {
6625 }
6626 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006627 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 mClientDead = true;
6629 killSessionLocked();
6630 }
6631 }
6632
6633 public int add(IWindow window, WindowManager.LayoutParams attrs,
6634 int viewVisibility, Rect outContentInsets) {
6635 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6636 }
Romain Guy06882f82009-06-10 13:36:04 -07006637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638 public void remove(IWindow window) {
6639 removeWindow(this, window);
6640 }
Romain Guy06882f82009-06-10 13:36:04 -07006641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006642 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6643 int requestedWidth, int requestedHeight, int viewFlags,
6644 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6645 Rect outVisibleInsets, Surface outSurface) {
6646 return relayoutWindow(this, window, attrs,
6647 requestedWidth, requestedHeight, viewFlags, insetsPending,
6648 outFrame, outContentInsets, outVisibleInsets, outSurface);
6649 }
Romain Guy06882f82009-06-10 13:36:04 -07006650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651 public void setTransparentRegion(IWindow window, Region region) {
6652 setTransparentRegionWindow(this, window, region);
6653 }
Romain Guy06882f82009-06-10 13:36:04 -07006654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006655 public void setInsets(IWindow window, int touchableInsets,
6656 Rect contentInsets, Rect visibleInsets) {
6657 setInsetsWindow(this, window, touchableInsets, contentInsets,
6658 visibleInsets);
6659 }
Romain Guy06882f82009-06-10 13:36:04 -07006660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6662 getWindowDisplayFrame(this, window, outDisplayFrame);
6663 }
Romain Guy06882f82009-06-10 13:36:04 -07006664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006665 public void finishDrawing(IWindow window) {
6666 if (localLOGV) Log.v(
6667 TAG, "IWindow finishDrawing called for " + window);
6668 finishDrawingWindow(this, window);
6669 }
6670
6671 public void finishKey(IWindow window) {
6672 if (localLOGV) Log.v(
6673 TAG, "IWindow finishKey called for " + window);
6674 mKeyWaiter.finishedKey(this, window, false,
6675 KeyWaiter.RETURN_NOTHING);
6676 }
6677
6678 public MotionEvent getPendingPointerMove(IWindow window) {
6679 if (localLOGV) Log.v(
6680 TAG, "IWindow getPendingMotionEvent called for " + window);
6681 return mKeyWaiter.finishedKey(this, window, false,
6682 KeyWaiter.RETURN_PENDING_POINTER);
6683 }
Romain Guy06882f82009-06-10 13:36:04 -07006684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 public MotionEvent getPendingTrackballMove(IWindow window) {
6686 if (localLOGV) Log.v(
6687 TAG, "IWindow getPendingMotionEvent called for " + window);
6688 return mKeyWaiter.finishedKey(this, window, false,
6689 KeyWaiter.RETURN_PENDING_TRACKBALL);
6690 }
6691
6692 public void setInTouchMode(boolean mode) {
6693 synchronized(mWindowMap) {
6694 mInTouchMode = mode;
6695 }
6696 }
6697
6698 public boolean getInTouchMode() {
6699 synchronized(mWindowMap) {
6700 return mInTouchMode;
6701 }
6702 }
6703
6704 public boolean performHapticFeedback(IWindow window, int effectId,
6705 boolean always) {
6706 synchronized(mWindowMap) {
6707 long ident = Binder.clearCallingIdentity();
6708 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006709 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006710 windowForClientLocked(this, window), effectId, always);
6711 } finally {
6712 Binder.restoreCallingIdentity(ident);
6713 }
6714 }
6715 }
Romain Guy06882f82009-06-10 13:36:04 -07006716
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006717 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006718 synchronized(mWindowMap) {
6719 long ident = Binder.clearCallingIdentity();
6720 try {
6721 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006722 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006723 } finally {
6724 Binder.restoreCallingIdentity(ident);
6725 }
6726 }
6727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006728
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006729 public void wallpaperOffsetsComplete(IBinder window) {
6730 WindowManagerService.this.wallpaperOffsetsComplete(window);
6731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006732
Dianne Hackborn75804932009-10-20 20:15:20 -07006733 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6734 int z, Bundle extras, boolean sync) {
6735 synchronized(mWindowMap) {
6736 long ident = Binder.clearCallingIdentity();
6737 try {
6738 return sendWindowWallpaperCommandLocked(
6739 windowForClientLocked(this, window),
6740 action, x, y, z, extras, sync);
6741 } finally {
6742 Binder.restoreCallingIdentity(ident);
6743 }
6744 }
6745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006746
Dianne Hackborn75804932009-10-20 20:15:20 -07006747 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6748 WindowManagerService.this.wallpaperCommandComplete(window, result);
6749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006751 void windowAddedLocked() {
6752 if (mSurfaceSession == null) {
6753 if (localLOGV) Log.v(
6754 TAG, "First window added to " + this + ", creating SurfaceSession");
6755 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006756 if (SHOW_TRANSACTIONS) Log.i(
6757 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006758 mSessions.add(this);
6759 }
6760 mNumWindow++;
6761 }
6762
6763 void windowRemovedLocked() {
6764 mNumWindow--;
6765 killSessionLocked();
6766 }
Romain Guy06882f82009-06-10 13:36:04 -07006767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 void killSessionLocked() {
6769 if (mNumWindow <= 0 && mClientDead) {
6770 mSessions.remove(this);
6771 if (mSurfaceSession != null) {
6772 if (localLOGV) Log.v(
6773 TAG, "Last window removed from " + this
6774 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006775 if (SHOW_TRANSACTIONS) Log.i(
6776 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777 try {
6778 mSurfaceSession.kill();
6779 } catch (Exception e) {
6780 Log.w(TAG, "Exception thrown when killing surface session "
6781 + mSurfaceSession + " in session " + this
6782 + ": " + e.toString());
6783 }
6784 mSurfaceSession = null;
6785 }
6786 }
6787 }
Romain Guy06882f82009-06-10 13:36:04 -07006788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006789 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006790 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6791 pw.print(" mClientDead="); pw.print(mClientDead);
6792 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6793 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6794 pw.print(prefix);
6795 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6796 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6797 }
6798 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6799 pw.print(prefix);
6800 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6801 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 }
6804
6805 @Override
6806 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006807 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006808 }
6809 }
6810
6811 // -------------------------------------------------------------
6812 // Client Window State
6813 // -------------------------------------------------------------
6814
6815 private final class WindowState implements WindowManagerPolicy.WindowState {
6816 final Session mSession;
6817 final IWindow mClient;
6818 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006819 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 AppWindowToken mAppToken;
6821 AppWindowToken mTargetAppToken;
6822 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6823 final DeathRecipient mDeathRecipient;
6824 final WindowState mAttachedWindow;
6825 final ArrayList mChildWindows = new ArrayList();
6826 final int mBaseLayer;
6827 final int mSubLayer;
6828 final boolean mLayoutAttached;
6829 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006830 final boolean mIsWallpaper;
6831 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832 int mViewVisibility;
6833 boolean mPolicyVisibility = true;
6834 boolean mPolicyVisibilityAfterAnim = true;
6835 boolean mAppFreezing;
6836 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006837 boolean mReportDestroySurface;
6838 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839 boolean mAttachedHidden; // is our parent window hidden?
6840 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006841 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 int mRequestedWidth;
6843 int mRequestedHeight;
6844 int mLastRequestedWidth;
6845 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846 int mLayer;
6847 int mAnimLayer;
6848 int mLastLayer;
6849 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006850 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006851 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006852
6853 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855 // Actual frame shown on-screen (may be modified by animation)
6856 final Rect mShownFrame = new Rect();
6857 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859 /**
6860 * Insets that determine the actually visible area
6861 */
6862 final Rect mVisibleInsets = new Rect();
6863 final Rect mLastVisibleInsets = new Rect();
6864 boolean mVisibleInsetsChanged;
6865
6866 /**
6867 * Insets that are covered by system windows
6868 */
6869 final Rect mContentInsets = new Rect();
6870 final Rect mLastContentInsets = new Rect();
6871 boolean mContentInsetsChanged;
6872
6873 /**
6874 * Set to true if we are waiting for this window to receive its
6875 * given internal insets before laying out other windows based on it.
6876 */
6877 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879 /**
6880 * These are the content insets that were given during layout for
6881 * this window, to be applied to windows behind it.
6882 */
6883 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006885 /**
6886 * These are the visible insets that were given during layout for
6887 * this window, to be applied to windows behind it.
6888 */
6889 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006891 /**
6892 * Flag indicating whether the touchable region should be adjusted by
6893 * the visible insets; if false the area outside the visible insets is
6894 * NOT touchable, so we must use those to adjust the frame during hit
6895 * tests.
6896 */
6897 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899 // Current transformation being applied.
6900 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6901 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6902 float mHScale=1, mVScale=1;
6903 float mLastHScale=1, mLastVScale=1;
6904 final Matrix mTmpMatrix = new Matrix();
6905
6906 // "Real" frame that the application sees.
6907 final Rect mFrame = new Rect();
6908 final Rect mLastFrame = new Rect();
6909
6910 final Rect mContainingFrame = new Rect();
6911 final Rect mDisplayFrame = new Rect();
6912 final Rect mContentFrame = new Rect();
6913 final Rect mVisibleFrame = new Rect();
6914
6915 float mShownAlpha = 1;
6916 float mAlpha = 1;
6917 float mLastAlpha = 1;
6918
6919 // Set to true if, when the window gets displayed, it should perform
6920 // an enter animation.
6921 boolean mEnterAnimationPending;
6922
6923 // Currently running animation.
6924 boolean mAnimating;
6925 boolean mLocalAnimating;
6926 Animation mAnimation;
6927 boolean mAnimationIsEntrance;
6928 boolean mHasTransformation;
6929 boolean mHasLocalTransformation;
6930 final Transformation mTransformation = new Transformation();
6931
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006932 // If a window showing a wallpaper: the requested offset for the
6933 // wallpaper; if a wallpaper window: the currently applied offset.
6934 float mWallpaperX = -1;
6935 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006936
6937 // If a window showing a wallpaper: what fraction of the offset
6938 // range corresponds to a full virtual screen.
6939 float mWallpaperXStep = -1;
6940 float mWallpaperYStep = -1;
6941
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006942 // Wallpaper windows: pixels offset based on above variables.
6943 int mXOffset;
6944 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 // This is set after IWindowSession.relayout() has been called at
6947 // least once for the window. It allows us to detect the situation
6948 // where we don't yet have a surface, but should have one soon, so
6949 // we can give the window focus before waiting for the relayout.
6950 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 // This is set after the Surface has been created but before the
6953 // window has been drawn. During this time the surface is hidden.
6954 boolean mDrawPending;
6955
6956 // This is set after the window has finished drawing for the first
6957 // time but before its surface is shown. The surface will be
6958 // displayed when the next layout is run.
6959 boolean mCommitDrawPending;
6960
6961 // This is set during the time after the window's drawing has been
6962 // committed, and before its surface is actually shown. It is used
6963 // to delay showing the surface until all windows in a token are ready
6964 // to be shown.
6965 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 // Set when the window has been shown in the screen the first time.
6968 boolean mHasDrawn;
6969
6970 // Currently running an exit animation?
6971 boolean mExiting;
6972
6973 // Currently on the mDestroySurface list?
6974 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 // Completely remove from window manager after exit animation?
6977 boolean mRemoveOnExit;
6978
6979 // Set when the orientation is changing and this window has not yet
6980 // been updated for the new orientation.
6981 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 // Is this window now (or just being) removed?
6984 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 WindowState(Session s, IWindow c, WindowToken token,
6987 WindowState attachedWindow, WindowManager.LayoutParams a,
6988 int viewVisibility) {
6989 mSession = s;
6990 mClient = c;
6991 mToken = token;
6992 mAttrs.copyFrom(a);
6993 mViewVisibility = viewVisibility;
6994 DeathRecipient deathRecipient = new DeathRecipient();
6995 mAlpha = a.alpha;
6996 if (localLOGV) Log.v(
6997 TAG, "Window " + this + " client=" + c.asBinder()
6998 + " token=" + token + " (" + mAttrs.token + ")");
6999 try {
7000 c.asBinder().linkToDeath(deathRecipient, 0);
7001 } catch (RemoteException e) {
7002 mDeathRecipient = null;
7003 mAttachedWindow = null;
7004 mLayoutAttached = false;
7005 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007006 mIsWallpaper = false;
7007 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 mBaseLayer = 0;
7009 mSubLayer = 0;
7010 return;
7011 }
7012 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007014 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7015 mAttrs.type <= LAST_SUB_WINDOW)) {
7016 // The multiplier here is to reserve space for multiple
7017 // windows in the same type layer.
7018 mBaseLayer = mPolicy.windowTypeToLayerLw(
7019 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7020 + TYPE_LAYER_OFFSET;
7021 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7022 mAttachedWindow = attachedWindow;
7023 mAttachedWindow.mChildWindows.add(this);
7024 mLayoutAttached = mAttrs.type !=
7025 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7026 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7027 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007028 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7029 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007030 } else {
7031 // The multiplier here is to reserve space for multiple
7032 // windows in the same type layer.
7033 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7034 * TYPE_LAYER_MULTIPLIER
7035 + TYPE_LAYER_OFFSET;
7036 mSubLayer = 0;
7037 mAttachedWindow = null;
7038 mLayoutAttached = false;
7039 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7040 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007041 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7042 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007043 }
7044
7045 WindowState appWin = this;
7046 while (appWin.mAttachedWindow != null) {
7047 appWin = mAttachedWindow;
7048 }
7049 WindowToken appToken = appWin.mToken;
7050 while (appToken.appWindowToken == null) {
7051 WindowToken parent = mTokenMap.get(appToken.token);
7052 if (parent == null || appToken == parent) {
7053 break;
7054 }
7055 appToken = parent;
7056 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007057 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007058 mAppToken = appToken.appWindowToken;
7059
7060 mSurface = null;
7061 mRequestedWidth = 0;
7062 mRequestedHeight = 0;
7063 mLastRequestedWidth = 0;
7064 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007065 mXOffset = 0;
7066 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 mLayer = 0;
7068 mAnimLayer = 0;
7069 mLastLayer = 0;
7070 }
7071
7072 void attach() {
7073 if (localLOGV) Log.v(
7074 TAG, "Attaching " + this + " token=" + mToken
7075 + ", list=" + mToken.windows);
7076 mSession.windowAddedLocked();
7077 }
7078
7079 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7080 mHaveFrame = true;
7081
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007082 final Rect container = mContainingFrame;
7083 container.set(pf);
7084
7085 final Rect display = mDisplayFrame;
7086 display.set(df);
7087
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007088 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007089 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007090 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7091 display.intersect(mCompatibleScreenFrame);
7092 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007093 }
7094
7095 final int pw = container.right - container.left;
7096 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007097
7098 int w,h;
7099 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7100 w = mAttrs.width < 0 ? pw : mAttrs.width;
7101 h = mAttrs.height< 0 ? ph : mAttrs.height;
7102 } else {
Romain Guy980a9382010-01-08 15:06:28 -08007103 w = mAttrs.width == mAttrs.MATCH_PARENT ? pw : mRequestedWidth;
7104 h = mAttrs.height== mAttrs.MATCH_PARENT ? ph : mRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007105 }
Romain Guy06882f82009-06-10 13:36:04 -07007106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007107 final Rect content = mContentFrame;
7108 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 final Rect visible = mVisibleFrame;
7111 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007114 final int fw = frame.width();
7115 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7118 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7119
7120 Gravity.apply(mAttrs.gravity, w, h, container,
7121 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7122 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7123
7124 //System.out.println("Out: " + mFrame);
7125
7126 // Now make sure the window fits in the overall display.
7127 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 // Make sure the content and visible frames are inside of the
7130 // final window frame.
7131 if (content.left < frame.left) content.left = frame.left;
7132 if (content.top < frame.top) content.top = frame.top;
7133 if (content.right > frame.right) content.right = frame.right;
7134 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7135 if (visible.left < frame.left) visible.left = frame.left;
7136 if (visible.top < frame.top) visible.top = frame.top;
7137 if (visible.right > frame.right) visible.right = frame.right;
7138 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007140 final Rect contentInsets = mContentInsets;
7141 contentInsets.left = content.left-frame.left;
7142 contentInsets.top = content.top-frame.top;
7143 contentInsets.right = frame.right-content.right;
7144 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146 final Rect visibleInsets = mVisibleInsets;
7147 visibleInsets.left = visible.left-frame.left;
7148 visibleInsets.top = visible.top-frame.top;
7149 visibleInsets.right = frame.right-visible.right;
7150 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007151
Dianne Hackborn284ac932009-08-28 10:34:25 -07007152 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7153 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007154 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007155 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007157 if (localLOGV) {
7158 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7159 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
7160 Log.v(TAG, "Resolving (mRequestedWidth="
7161 + mRequestedWidth + ", mRequestedheight="
7162 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7163 + "): frame=" + mFrame.toShortString()
7164 + " ci=" + contentInsets.toShortString()
7165 + " vi=" + visibleInsets.toShortString());
7166 //}
7167 }
7168 }
Romain Guy06882f82009-06-10 13:36:04 -07007169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007170 public Rect getFrameLw() {
7171 return mFrame;
7172 }
7173
7174 public Rect getShownFrameLw() {
7175 return mShownFrame;
7176 }
7177
7178 public Rect getDisplayFrameLw() {
7179 return mDisplayFrame;
7180 }
7181
7182 public Rect getContentFrameLw() {
7183 return mContentFrame;
7184 }
7185
7186 public Rect getVisibleFrameLw() {
7187 return mVisibleFrame;
7188 }
7189
7190 public boolean getGivenInsetsPendingLw() {
7191 return mGivenInsetsPending;
7192 }
7193
7194 public Rect getGivenContentInsetsLw() {
7195 return mGivenContentInsets;
7196 }
Romain Guy06882f82009-06-10 13:36:04 -07007197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007198 public Rect getGivenVisibleInsetsLw() {
7199 return mGivenVisibleInsets;
7200 }
Romain Guy06882f82009-06-10 13:36:04 -07007201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007202 public WindowManager.LayoutParams getAttrs() {
7203 return mAttrs;
7204 }
7205
7206 public int getSurfaceLayer() {
7207 return mLayer;
7208 }
Romain Guy06882f82009-06-10 13:36:04 -07007209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 public IApplicationToken getAppToken() {
7211 return mAppToken != null ? mAppToken.appToken : null;
7212 }
7213
7214 public boolean hasAppShownWindows() {
7215 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7216 }
7217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007218 public void setAnimation(Animation anim) {
7219 if (localLOGV) Log.v(
7220 TAG, "Setting animation in " + this + ": " + anim);
7221 mAnimating = false;
7222 mLocalAnimating = false;
7223 mAnimation = anim;
7224 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7225 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7226 }
7227
7228 public void clearAnimation() {
7229 if (mAnimation != null) {
7230 mAnimating = true;
7231 mLocalAnimating = false;
7232 mAnimation = null;
7233 }
7234 }
Romain Guy06882f82009-06-10 13:36:04 -07007235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007236 Surface createSurfaceLocked() {
7237 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007238 mReportDestroySurface = false;
7239 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007240 mDrawPending = true;
7241 mCommitDrawPending = false;
7242 mReadyToShow = false;
7243 if (mAppToken != null) {
7244 mAppToken.allDrawn = false;
7245 }
7246
7247 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007248 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007249 flags |= Surface.PUSH_BUFFERS;
7250 }
7251
7252 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7253 flags |= Surface.SECURE;
7254 }
7255 if (DEBUG_VISIBILITY) Log.v(
7256 TAG, "Creating surface in session "
7257 + mSession.mSurfaceSession + " window " + this
7258 + " w=" + mFrame.width()
7259 + " h=" + mFrame.height() + " format="
7260 + mAttrs.format + " flags=" + flags);
7261
7262 int w = mFrame.width();
7263 int h = mFrame.height();
7264 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7265 // for a scaled surface, we always want the requested
7266 // size.
7267 w = mRequestedWidth;
7268 h = mRequestedHeight;
7269 }
7270
Romain Guy9825ec62009-10-01 00:58:09 -07007271 // Something is wrong and SurfaceFlinger will not like this,
7272 // try to revert to sane values
7273 if (w <= 0) w = 1;
7274 if (h <= 0) h = 1;
7275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 try {
7277 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007278 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007280 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
7281 + mSurface + " IN SESSION "
7282 + mSession.mSurfaceSession
7283 + ": pid=" + mSession.mPid + " format="
7284 + mAttrs.format + " flags=0x"
7285 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 } catch (Surface.OutOfResourcesException e) {
7287 Log.w(TAG, "OutOfResourcesException creating surface");
7288 reclaimSomeSurfaceMemoryLocked(this, "create");
7289 return null;
7290 } catch (Exception e) {
7291 Log.e(TAG, "Exception creating surface", e);
7292 return null;
7293 }
Romain Guy06882f82009-06-10 13:36:04 -07007294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007295 if (localLOGV) Log.v(
7296 TAG, "Got surface: " + mSurface
7297 + ", set left=" + mFrame.left + " top=" + mFrame.top
7298 + ", animLayer=" + mAnimLayer);
7299 if (SHOW_TRANSACTIONS) {
7300 Log.i(TAG, ">>> OPEN TRANSACTION");
7301 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
7302 + mAttrs.getTitle() + ") pos=(" +
7303 mFrame.left + "," + mFrame.top + ") (" +
7304 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7305 mAnimLayer + " HIDE");
7306 }
7307 Surface.openTransaction();
7308 try {
7309 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007310 mSurface.setPosition(mFrame.left + mXOffset,
7311 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 mSurface.setLayer(mAnimLayer);
7313 mSurface.hide();
7314 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007315 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
7316 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 mSurface.setFlags(Surface.SURFACE_DITHER,
7318 Surface.SURFACE_DITHER);
7319 }
7320 } catch (RuntimeException e) {
7321 Log.w(TAG, "Error creating surface in " + w, e);
7322 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7323 }
7324 mLastHidden = true;
7325 } finally {
7326 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
7327 Surface.closeTransaction();
7328 }
7329 if (localLOGV) Log.v(
7330 TAG, "Created surface " + this);
7331 }
7332 return mSurface;
7333 }
Romain Guy06882f82009-06-10 13:36:04 -07007334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007335 void destroySurfaceLocked() {
7336 // Window is no longer on-screen, so can no longer receive
7337 // key events... if we were waiting for it to finish
7338 // handling a key event, the wait is over!
7339 mKeyWaiter.finishedKey(mSession, mClient, true,
7340 KeyWaiter.RETURN_NOTHING);
7341 mKeyWaiter.releasePendingPointerLocked(mSession);
7342 mKeyWaiter.releasePendingTrackballLocked(mSession);
7343
7344 if (mAppToken != null && this == mAppToken.startingWindow) {
7345 mAppToken.startingDisplayed = false;
7346 }
Romain Guy06882f82009-06-10 13:36:04 -07007347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007348 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007349 mDrawPending = false;
7350 mCommitDrawPending = false;
7351 mReadyToShow = false;
7352
7353 int i = mChildWindows.size();
7354 while (i > 0) {
7355 i--;
7356 WindowState c = (WindowState)mChildWindows.get(i);
7357 c.mAttachedHidden = true;
7358 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007359
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007360 if (mReportDestroySurface) {
7361 mReportDestroySurface = false;
7362 mSurfacePendingDestroy = true;
7363 try {
7364 mClient.dispatchGetNewSurface();
7365 // We'll really destroy on the next time around.
7366 return;
7367 } catch (RemoteException e) {
7368 }
7369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007371 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007372 if (DEBUG_VISIBILITY) {
7373 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007374 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007375 Log.w(TAG, "Window " + this + " destroying surface "
7376 + mSurface + ", session " + mSession, e);
7377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007378 if (SHOW_TRANSACTIONS) {
7379 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007380 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007381 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
7382 + mAttrs.getTitle() + ")", ex);
7383 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007384 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 } catch (RuntimeException e) {
7386 Log.w(TAG, "Exception thrown when destroying Window " + this
7387 + " surface " + mSurface + " session " + mSession
7388 + ": " + e.toString());
7389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007391 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 }
7393 }
7394
7395 boolean finishDrawingLocked() {
7396 if (mDrawPending) {
7397 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
7398 TAG, "finishDrawingLocked: " + mSurface);
7399 mCommitDrawPending = true;
7400 mDrawPending = false;
7401 return true;
7402 }
7403 return false;
7404 }
7405
7406 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007407 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007408 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7409 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007410 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 }
7412 mCommitDrawPending = false;
7413 mReadyToShow = true;
7414 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7415 final AppWindowToken atoken = mAppToken;
7416 if (atoken == null || atoken.allDrawn || starting) {
7417 performShowLocked();
7418 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007419 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007420 }
7421
7422 // This must be called while inside a transaction.
7423 boolean performShowLocked() {
7424 if (DEBUG_VISIBILITY) {
7425 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007426 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007427 Log.v(TAG, "performShow on " + this
7428 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7429 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7430 }
7431 if (mReadyToShow && isReadyForDisplay()) {
7432 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7433 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7434 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7435 + " during animation: policyVis=" + mPolicyVisibility
7436 + " attHidden=" + mAttachedHidden
7437 + " tok.hiddenRequested="
7438 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007439 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007440 + (mAppToken != null ? mAppToken.hidden : false)
7441 + " animating=" + mAnimating
7442 + " tok animating="
7443 + (mAppToken != null ? mAppToken.animating : false));
7444 if (!showSurfaceRobustlyLocked(this)) {
7445 return false;
7446 }
7447 mLastAlpha = -1;
7448 mHasDrawn = true;
7449 mLastHidden = false;
7450 mReadyToShow = false;
7451 enableScreenIfNeededLocked();
7452
7453 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007455 int i = mChildWindows.size();
7456 while (i > 0) {
7457 i--;
7458 WindowState c = (WindowState)mChildWindows.get(i);
7459 if (c.mSurface != null && c.mAttachedHidden) {
7460 c.mAttachedHidden = false;
7461 c.performShowLocked();
Dianne Hackborn9b52a212009-12-11 14:51:35 -08007462 // It hadn't been shown, which means layout not
7463 // performed on it, so now we want to make sure to
7464 // do a layout. If called from within the transaction
7465 // loop, this will cause it to restart with a new
7466 // layout.
7467 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007468 }
7469 }
Romain Guy06882f82009-06-10 13:36:04 -07007470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007471 if (mAttrs.type != TYPE_APPLICATION_STARTING
7472 && mAppToken != null) {
7473 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007474
Dianne Hackborn248b1882009-09-16 16:46:44 -07007475 if (mAppToken.startingData != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007476 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Log.v(TAG,
7477 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007478 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007479 // If this initial window is animating, stop it -- we
7480 // will do an animation to reveal it from behind the
7481 // starting window, so there is no need for it to also
7482 // be doing its own stuff.
7483 if (mAnimation != null) {
7484 mAnimation = null;
7485 // Make sure we clean up the animation.
7486 mAnimating = true;
7487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007488 mFinishedStarting.add(mAppToken);
7489 mH.sendEmptyMessage(H.FINISHED_STARTING);
7490 }
7491 mAppToken.updateReportedVisibilityLocked();
7492 }
7493 }
7494 return true;
7495 }
Romain Guy06882f82009-06-10 13:36:04 -07007496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007497 // This must be called while inside a transaction. Returns true if
7498 // there is more animation to run.
7499 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08007500 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007501 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7504 mHasTransformation = true;
7505 mHasLocalTransformation = true;
7506 if (!mLocalAnimating) {
7507 if (DEBUG_ANIM) Log.v(
7508 TAG, "Starting animation in " + this +
7509 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7510 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7511 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7512 mAnimation.setStartTime(currentTime);
7513 mLocalAnimating = true;
7514 mAnimating = true;
7515 }
7516 mTransformation.clear();
7517 final boolean more = mAnimation.getTransformation(
7518 currentTime, mTransformation);
7519 if (DEBUG_ANIM) Log.v(
7520 TAG, "Stepped animation in " + this +
7521 ": more=" + more + ", xform=" + mTransformation);
7522 if (more) {
7523 // we're not done!
7524 return true;
7525 }
7526 if (DEBUG_ANIM) Log.v(
7527 TAG, "Finished animation in " + this +
7528 " @ " + currentTime);
7529 mAnimation = null;
7530 //WindowManagerService.this.dump();
7531 }
7532 mHasLocalTransformation = false;
7533 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007534 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007535 // When our app token is animating, we kind-of pretend like
7536 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7537 // part of this check means that we will only do this if
7538 // our window is not currently exiting, or it is not
7539 // locally animating itself. The idea being that one that
7540 // is exiting and doing a local animation should be removed
7541 // once that animation is done.
7542 mAnimating = true;
7543 mHasTransformation = true;
7544 mTransformation.clear();
7545 return false;
7546 } else if (mHasTransformation) {
7547 // Little trick to get through the path below to act like
7548 // we have finished an animation.
7549 mAnimating = true;
7550 } else if (isAnimating()) {
7551 mAnimating = true;
7552 }
7553 } else if (mAnimation != null) {
7554 // If the display is frozen, and there is a pending animation,
7555 // clear it and make sure we run the cleanup code.
7556 mAnimating = true;
7557 mLocalAnimating = true;
7558 mAnimation = null;
7559 }
Romain Guy06882f82009-06-10 13:36:04 -07007560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007561 if (!mAnimating && !mLocalAnimating) {
7562 return false;
7563 }
7564
7565 if (DEBUG_ANIM) Log.v(
7566 TAG, "Animation done in " + this + ": exiting=" + mExiting
7567 + ", reportedVisible="
7568 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 mAnimating = false;
7571 mLocalAnimating = false;
7572 mAnimation = null;
7573 mAnimLayer = mLayer;
7574 if (mIsImWindow) {
7575 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007576 } else if (mIsWallpaper) {
7577 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007578 }
7579 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7580 + " anim layer: " + mAnimLayer);
7581 mHasTransformation = false;
7582 mHasLocalTransformation = false;
7583 mPolicyVisibility = mPolicyVisibilityAfterAnim;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007584 if (!mPolicyVisibility) {
7585 // Window is no longer visible -- make sure if we were waiting
7586 // for it to be displayed before enabling the display, that
7587 // we allow the display to be enabled now.
7588 enableScreenIfNeededLocked();
7589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007590 mTransformation.clear();
7591 if (mHasDrawn
7592 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7593 && mAppToken != null
7594 && mAppToken.firstWindowDrawn
7595 && mAppToken.startingData != null) {
7596 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7597 + mToken + ": first real window done animating");
7598 mFinishedStarting.add(mAppToken);
7599 mH.sendEmptyMessage(H.FINISHED_STARTING);
7600 }
Romain Guy06882f82009-06-10 13:36:04 -07007601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007602 finishExit();
7603
7604 if (mAppToken != null) {
7605 mAppToken.updateReportedVisibilityLocked();
7606 }
7607
7608 return false;
7609 }
7610
7611 void finishExit() {
7612 if (DEBUG_ANIM) Log.v(
7613 TAG, "finishExit in " + this
7614 + ": exiting=" + mExiting
7615 + " remove=" + mRemoveOnExit
7616 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 final int N = mChildWindows.size();
7619 for (int i=0; i<N; i++) {
7620 ((WindowState)mChildWindows.get(i)).finishExit();
7621 }
Romain Guy06882f82009-06-10 13:36:04 -07007622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007623 if (!mExiting) {
7624 return;
7625 }
Romain Guy06882f82009-06-10 13:36:04 -07007626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007627 if (isWindowAnimating()) {
7628 return;
7629 }
7630
7631 if (localLOGV) Log.v(
7632 TAG, "Exit animation finished in " + this
7633 + ": remove=" + mRemoveOnExit);
7634 if (mSurface != null) {
7635 mDestroySurface.add(this);
7636 mDestroying = true;
7637 if (SHOW_TRANSACTIONS) Log.i(
7638 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7639 try {
7640 mSurface.hide();
7641 } catch (RuntimeException e) {
7642 Log.w(TAG, "Error hiding surface in " + this, e);
7643 }
7644 mLastHidden = true;
7645 mKeyWaiter.releasePendingPointerLocked(mSession);
7646 }
7647 mExiting = false;
7648 if (mRemoveOnExit) {
7649 mPendingRemove.add(this);
7650 mRemoveOnExit = false;
7651 }
7652 }
Romain Guy06882f82009-06-10 13:36:04 -07007653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007654 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7655 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7656 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7657 if (dtdx < -.000001f || dtdx > .000001f) return false;
7658 if (dsdy < -.000001f || dsdy > .000001f) return false;
7659 return true;
7660 }
Romain Guy06882f82009-06-10 13:36:04 -07007661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007662 void computeShownFrameLocked() {
7663 final boolean selfTransformation = mHasLocalTransformation;
7664 Transformation attachedTransformation =
7665 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7666 ? mAttachedWindow.mTransformation : null;
7667 Transformation appTransformation =
7668 (mAppToken != null && mAppToken.hasTransformation)
7669 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007670
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007671 // Wallpapers are animated based on the "real" window they
7672 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007673 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007674 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007675 if (mWallpaperTarget.mHasLocalTransformation &&
7676 mWallpaperTarget.mAnimation != null &&
7677 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007678 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007679 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7680 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7681 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007682 }
7683 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007684 mWallpaperTarget.mAppToken.hasTransformation &&
7685 mWallpaperTarget.mAppToken.animation != null &&
7686 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007687 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007688 if (DEBUG_WALLPAPER && appTransformation != null) {
7689 Log.v(TAG, "WP target app xform: " + appTransformation);
7690 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007691 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007692 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 if (selfTransformation || attachedTransformation != null
7695 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007696 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 final Rect frame = mFrame;
7698 final float tmpFloats[] = mTmpFloats;
7699 final Matrix tmpMatrix = mTmpMatrix;
7700
7701 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007702 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007703 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007704 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007705 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007706 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007707 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007708 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709 }
7710 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007711 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007712 }
7713
7714 // "convert" it into SurfaceFlinger's format
7715 // (a 2x2 matrix + an offset)
7716 // Here we must not transform the position of the surface
7717 // since it is already included in the transformation.
7718 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007720 tmpMatrix.getValues(tmpFloats);
7721 mDsDx = tmpFloats[Matrix.MSCALE_X];
7722 mDtDx = tmpFloats[Matrix.MSKEW_X];
7723 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7724 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007725 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7726 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007727 int w = frame.width();
7728 int h = frame.height();
7729 mShownFrame.set(x, y, x+w, y+h);
7730
7731 // Now set the alpha... but because our current hardware
7732 // can't do alpha transformation on a non-opaque surface,
7733 // turn it off if we are running an animation that is also
7734 // transforming since it is more important to have that
7735 // animation be smooth.
7736 mShownAlpha = mAlpha;
7737 if (!mLimitedAlphaCompositing
7738 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7739 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7740 && x == frame.left && y == frame.top))) {
7741 //Log.i(TAG, "Applying alpha transform");
7742 if (selfTransformation) {
7743 mShownAlpha *= mTransformation.getAlpha();
7744 }
7745 if (attachedTransformation != null) {
7746 mShownAlpha *= attachedTransformation.getAlpha();
7747 }
7748 if (appTransformation != null) {
7749 mShownAlpha *= appTransformation.getAlpha();
7750 }
7751 } else {
7752 //Log.i(TAG, "Not applying alpha transform");
7753 }
Romain Guy06882f82009-06-10 13:36:04 -07007754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007755 if (localLOGV) Log.v(
7756 TAG, "Continuing animation in " + this +
7757 ": " + mShownFrame +
7758 ", alpha=" + mTransformation.getAlpha());
7759 return;
7760 }
Romain Guy06882f82009-06-10 13:36:04 -07007761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007763 if (mXOffset != 0 || mYOffset != 0) {
7764 mShownFrame.offset(mXOffset, mYOffset);
7765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 mShownAlpha = mAlpha;
7767 mDsDx = 1;
7768 mDtDx = 0;
7769 mDsDy = 0;
7770 mDtDy = 1;
7771 }
Romain Guy06882f82009-06-10 13:36:04 -07007772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 /**
7774 * Is this window visible? It is not visible if there is no
7775 * surface, or we are in the process of running an exit animation
7776 * that will remove the surface, or its app token has been hidden.
7777 */
7778 public boolean isVisibleLw() {
7779 final AppWindowToken atoken = mAppToken;
7780 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7781 && (atoken == null || !atoken.hiddenRequested)
7782 && !mExiting && !mDestroying;
7783 }
7784
7785 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007786 * Like {@link #isVisibleLw}, but also counts a window that is currently
7787 * "hidden" behind the keyguard as visible. This allows us to apply
7788 * things like window flags that impact the keyguard.
7789 * XXX I am starting to think we need to have ANOTHER visibility flag
7790 * for this "hidden behind keyguard" state rather than overloading
7791 * mPolicyVisibility. Ungh.
7792 */
7793 public boolean isVisibleOrBehindKeyguardLw() {
7794 final AppWindowToken atoken = mAppToken;
7795 return mSurface != null && !mAttachedHidden
7796 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7797 && !mExiting && !mDestroying;
7798 }
7799
7800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 * Is this window visible, ignoring its app token? It is not visible
7802 * if there is no surface, or we are in the process of running an exit animation
7803 * that will remove the surface.
7804 */
7805 public boolean isWinVisibleLw() {
7806 final AppWindowToken atoken = mAppToken;
7807 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7808 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7809 && !mExiting && !mDestroying;
7810 }
7811
7812 /**
7813 * The same as isVisible(), but follows the current hidden state of
7814 * the associated app token, not the pending requested hidden state.
7815 */
7816 boolean isVisibleNow() {
7817 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007818 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 }
7820
7821 /**
7822 * Same as isVisible(), but we also count it as visible between the
7823 * call to IWindowSession.add() and the first relayout().
7824 */
7825 boolean isVisibleOrAdding() {
7826 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007827 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7829 && mPolicyVisibility && !mAttachedHidden
7830 && (atoken == null || !atoken.hiddenRequested)
7831 && !mExiting && !mDestroying;
7832 }
7833
7834 /**
7835 * Is this window currently on-screen? It is on-screen either if it
7836 * is visible or it is currently running an animation before no longer
7837 * being visible.
7838 */
7839 boolean isOnScreen() {
7840 final AppWindowToken atoken = mAppToken;
7841 if (atoken != null) {
7842 return mSurface != null && mPolicyVisibility && !mDestroying
7843 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007844 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 } else {
7846 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007847 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 }
7849 }
Romain Guy06882f82009-06-10 13:36:04 -07007850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007851 /**
7852 * Like isOnScreen(), but we don't return true if the window is part
7853 * of a transition that has not yet been started.
7854 */
7855 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007856 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007857 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007858 return false;
7859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007861 final boolean animating = atoken != null
7862 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007863 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007864 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7865 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007866 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 }
7868
7869 /** Is the window or its container currently animating? */
7870 boolean isAnimating() {
7871 final WindowState attached = mAttachedWindow;
7872 final AppWindowToken atoken = mAppToken;
7873 return mAnimation != null
7874 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007875 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007876 (atoken.animation != null
7877 || atoken.inPendingTransaction));
7878 }
7879
7880 /** Is this window currently animating? */
7881 boolean isWindowAnimating() {
7882 return mAnimation != null;
7883 }
7884
7885 /**
7886 * Like isOnScreen, but returns false if the surface hasn't yet
7887 * been drawn.
7888 */
7889 public boolean isDisplayedLw() {
7890 final AppWindowToken atoken = mAppToken;
7891 return mSurface != null && mPolicyVisibility && !mDestroying
7892 && !mDrawPending && !mCommitDrawPending
7893 && ((!mAttachedHidden &&
7894 (atoken == null || !atoken.hiddenRequested))
7895 || mAnimating);
7896 }
7897
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007898 /**
7899 * Returns true if the window has a surface that it has drawn a
7900 * complete UI in to.
7901 */
7902 public boolean isDrawnLw() {
7903 final AppWindowToken atoken = mAppToken;
7904 return mSurface != null && !mDestroying
7905 && !mDrawPending && !mCommitDrawPending;
7906 }
7907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007908 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7909 boolean shownFrame, boolean onlyOpaque) {
7910 if (mSurface == null) {
7911 return false;
7912 }
7913 if (mAppToken != null && !mAppToken.appFullscreen) {
7914 return false;
7915 }
7916 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7917 return false;
7918 }
7919 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007920
7921 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7922 return frame.left <= mCompatibleScreenFrame.left &&
7923 frame.top <= mCompatibleScreenFrame.top &&
7924 frame.right >= mCompatibleScreenFrame.right &&
7925 frame.bottom >= mCompatibleScreenFrame.bottom;
7926 } else {
7927 return frame.left <= 0 && frame.top <= 0
7928 && frame.right >= screenWidth
7929 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931 }
Romain Guy06882f82009-06-10 13:36:04 -07007932
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007933 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007934 * Return true if the window is opaque and fully drawn. This indicates
7935 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007936 */
7937 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007938 return (mAttrs.format == PixelFormat.OPAQUE
7939 || mAttrs.type == TYPE_WALLPAPER)
7940 && mSurface != null && mAnimation == null
7941 && (mAppToken == null || mAppToken.animation == null)
7942 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007943 }
7944
7945 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7946 return
7947 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007948 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7949 // only if it's visible
7950 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007951 // and only if the application fills the compatible screen
7952 mFrame.left <= mCompatibleScreenFrame.left &&
7953 mFrame.top <= mCompatibleScreenFrame.top &&
7954 mFrame.right >= mCompatibleScreenFrame.right &&
7955 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007956 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007957 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007958 }
7959
7960 boolean isFullscreen(int screenWidth, int screenHeight) {
7961 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007962 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007963 }
7964
7965 void removeLocked() {
7966 if (mAttachedWindow != null) {
7967 mAttachedWindow.mChildWindows.remove(this);
7968 }
7969 destroySurfaceLocked();
7970 mSession.windowRemovedLocked();
7971 try {
7972 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7973 } catch (RuntimeException e) {
7974 // Ignore if it has already been removed (usually because
7975 // we are doing this as part of processing a death note.)
7976 }
7977 }
7978
7979 private class DeathRecipient implements IBinder.DeathRecipient {
7980 public void binderDied() {
7981 try {
7982 synchronized(mWindowMap) {
7983 WindowState win = windowForClientLocked(mSession, mClient);
7984 Log.i(TAG, "WIN DEATH: " + win);
7985 if (win != null) {
7986 removeWindowLocked(mSession, win);
7987 }
7988 }
7989 } catch (IllegalArgumentException ex) {
7990 // This will happen if the window has already been
7991 // removed.
7992 }
7993 }
7994 }
7995
7996 /** Returns true if this window desires key events. */
7997 public final boolean canReceiveKeys() {
7998 return isVisibleOrAdding()
7999 && (mViewVisibility == View.VISIBLE)
8000 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8001 }
8002
8003 public boolean hasDrawnLw() {
8004 return mHasDrawn;
8005 }
8006
8007 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008008 return showLw(doAnimation, true);
8009 }
8010
8011 boolean showLw(boolean doAnimation, boolean requestAnim) {
8012 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8013 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008014 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008015 mPolicyVisibility = true;
8016 mPolicyVisibilityAfterAnim = true;
8017 if (doAnimation) {
8018 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8019 }
8020 if (requestAnim) {
8021 requestAnimationLocked(0);
8022 }
8023 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008024 }
8025
8026 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008027 return hideLw(doAnimation, true);
8028 }
8029
8030 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008031 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8032 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008033 if (!current) {
8034 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008035 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008036 if (doAnimation) {
8037 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8038 if (mAnimation == null) {
8039 doAnimation = false;
8040 }
8041 }
8042 if (doAnimation) {
8043 mPolicyVisibilityAfterAnim = false;
8044 } else {
8045 mPolicyVisibilityAfterAnim = false;
8046 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008047 // Window is no longer visible -- make sure if we were waiting
8048 // for it to be displayed before enabling the display, that
8049 // we allow the display to be enabled now.
8050 enableScreenIfNeededLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008051 }
8052 if (requestAnim) {
8053 requestAnimationLocked(0);
8054 }
8055 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056 }
8057
8058 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008059 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07008060
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008061 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8062 pw.print(" mClient="); pw.println(mClient.asBinder());
8063 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8064 if (mAttachedWindow != null || mLayoutAttached) {
8065 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8066 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8067 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008068 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8069 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8070 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008071 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8072 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008073 }
8074 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8075 pw.print(" mSubLayer="); pw.print(mSubLayer);
8076 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8077 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8078 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8079 pw.print("="); pw.print(mAnimLayer);
8080 pw.print(" mLastLayer="); pw.println(mLastLayer);
8081 if (mSurface != null) {
8082 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8083 }
8084 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8085 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8086 if (mAppToken != null) {
8087 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8088 }
8089 if (mTargetAppToken != null) {
8090 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8091 }
8092 pw.print(prefix); pw.print("mViewVisibility=0x");
8093 pw.print(Integer.toHexString(mViewVisibility));
8094 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008095 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8096 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008097 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8098 pw.print(prefix); pw.print("mPolicyVisibility=");
8099 pw.print(mPolicyVisibility);
8100 pw.print(" mPolicyVisibilityAfterAnim=");
8101 pw.print(mPolicyVisibilityAfterAnim);
8102 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8103 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008104 if (!mRelayoutCalled) {
8105 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8106 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008107 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008108 pw.print(" h="); pw.println(mRequestedHeight);
8109 if (mXOffset != 0 || mYOffset != 0) {
8110 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8111 pw.print(" y="); pw.println(mYOffset);
8112 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008113 pw.print(prefix); pw.print("mGivenContentInsets=");
8114 mGivenContentInsets.printShortString(pw);
8115 pw.print(" mGivenVisibleInsets=");
8116 mGivenVisibleInsets.printShortString(pw);
8117 pw.println();
8118 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8119 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8120 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8121 }
8122 pw.print(prefix); pw.print("mShownFrame=");
8123 mShownFrame.printShortString(pw);
8124 pw.print(" last="); mLastShownFrame.printShortString(pw);
8125 pw.println();
8126 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8127 pw.print(" last="); mLastFrame.printShortString(pw);
8128 pw.println();
8129 pw.print(prefix); pw.print("mContainingFrame=");
8130 mContainingFrame.printShortString(pw);
8131 pw.print(" mDisplayFrame=");
8132 mDisplayFrame.printShortString(pw);
8133 pw.println();
8134 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8135 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8136 pw.println();
8137 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8138 pw.print(" last="); mLastContentInsets.printShortString(pw);
8139 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8140 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8141 pw.println();
8142 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8143 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8144 pw.print(" mAlpha="); pw.print(mAlpha);
8145 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8146 }
8147 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8148 || mAnimation != null) {
8149 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8150 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8151 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8152 pw.print(" mAnimation="); pw.println(mAnimation);
8153 }
8154 if (mHasTransformation || mHasLocalTransformation) {
8155 pw.print(prefix); pw.print("XForm: has=");
8156 pw.print(mHasTransformation);
8157 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8158 pw.print(" "); mTransformation.printShortString(pw);
8159 pw.println();
8160 }
8161 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8162 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8163 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8164 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8165 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8166 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8167 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8168 pw.print(" mDestroying="); pw.print(mDestroying);
8169 pw.print(" mRemoved="); pw.println(mRemoved);
8170 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008171 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008172 pw.print(prefix); pw.print("mOrientationChanging=");
8173 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008174 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8175 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008176 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008177 if (mHScale != 1 || mVScale != 1) {
8178 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8179 pw.print(" mVScale="); pw.println(mVScale);
8180 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008181 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008182 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8183 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8184 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008185 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8186 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8187 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008189 }
8190
8191 @Override
8192 public String toString() {
8193 return "Window{"
8194 + Integer.toHexString(System.identityHashCode(this))
8195 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8196 }
8197 }
Romain Guy06882f82009-06-10 13:36:04 -07008198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008199 // -------------------------------------------------------------
8200 // Window Token State
8201 // -------------------------------------------------------------
8202
8203 class WindowToken {
8204 // The actual token.
8205 final IBinder token;
8206
8207 // The type of window this token is for, as per WindowManager.LayoutParams.
8208 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 // Set if this token was explicitly added by a client, so should
8211 // not be removed when all windows are removed.
8212 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008213
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008214 // For printing.
8215 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008217 // If this is an AppWindowToken, this is non-null.
8218 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008220 // All of the windows associated with this token.
8221 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8222
8223 // Is key dispatching paused for this token?
8224 boolean paused = false;
8225
8226 // Should this token's windows be hidden?
8227 boolean hidden;
8228
8229 // Temporary for finding which tokens no longer have visible windows.
8230 boolean hasVisible;
8231
Dianne Hackborna8f60182009-09-01 19:01:50 -07008232 // Set to true when this token is in a pending transaction where it
8233 // will be shown.
8234 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008235
Dianne Hackborna8f60182009-09-01 19:01:50 -07008236 // Set to true when this token is in a pending transaction where it
8237 // will be hidden.
8238 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008239
Dianne Hackborna8f60182009-09-01 19:01:50 -07008240 // Set to true when this token is in a pending transaction where its
8241 // windows will be put to the bottom of the list.
8242 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008243
Dianne Hackborna8f60182009-09-01 19:01:50 -07008244 // Set to true when this token is in a pending transaction where its
8245 // windows will be put to the top of the list.
8246 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008248 WindowToken(IBinder _token, int type, boolean _explicit) {
8249 token = _token;
8250 windowType = type;
8251 explicit = _explicit;
8252 }
8253
8254 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008255 pw.print(prefix); pw.print("token="); pw.println(token);
8256 pw.print(prefix); pw.print("windows="); pw.println(windows);
8257 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8258 pw.print(" hidden="); pw.print(hidden);
8259 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008260 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8261 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8262 pw.print(" waitingToHide="); pw.print(waitingToHide);
8263 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8264 pw.print(" sendingToTop="); pw.println(sendingToTop);
8265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008266 }
8267
8268 @Override
8269 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008270 if (stringName == null) {
8271 StringBuilder sb = new StringBuilder();
8272 sb.append("WindowToken{");
8273 sb.append(Integer.toHexString(System.identityHashCode(this)));
8274 sb.append(" token="); sb.append(token); sb.append('}');
8275 stringName = sb.toString();
8276 }
8277 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008278 }
8279 };
8280
8281 class AppWindowToken extends WindowToken {
8282 // Non-null only for application tokens.
8283 final IApplicationToken appToken;
8284
8285 // All of the windows and child windows that are included in this
8286 // application token. Note this list is NOT sorted!
8287 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8288
8289 int groupId = -1;
8290 boolean appFullscreen;
8291 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008293 // These are used for determining when all windows associated with
8294 // an activity have been drawn, so they can be made visible together
8295 // at the same time.
8296 int lastTransactionSequence = mTransactionSequence-1;
8297 int numInterestingWindows;
8298 int numDrawnWindows;
8299 boolean inPendingTransaction;
8300 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008302 // Is this token going to be hidden in a little while? If so, it
8303 // won't be taken into account for setting the screen orientation.
8304 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008306 // Is this window's surface needed? This is almost like hidden, except
8307 // it will sometimes be true a little earlier: when the token has
8308 // been shown, but is still waiting for its app transition to execute
8309 // before making its windows shown.
8310 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008312 // Have we told the window clients to hide themselves?
8313 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008315 // Last visibility state we reported to the app token.
8316 boolean reportedVisible;
8317
8318 // Set to true when the token has been removed from the window mgr.
8319 boolean removed;
8320
8321 // Have we been asked to have this token keep the screen frozen?
8322 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008324 boolean animating;
8325 Animation animation;
8326 boolean hasTransformation;
8327 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008329 // Offset to the window of all layers in the token, for use by
8330 // AppWindowToken animations.
8331 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008333 // Information about an application starting window if displayed.
8334 StartingData startingData;
8335 WindowState startingWindow;
8336 View startingView;
8337 boolean startingDisplayed;
8338 boolean startingMoved;
8339 boolean firstWindowDrawn;
8340
8341 AppWindowToken(IApplicationToken _token) {
8342 super(_token.asBinder(),
8343 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8344 appWindowToken = this;
8345 appToken = _token;
8346 }
Romain Guy06882f82009-06-10 13:36:04 -07008347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348 public void setAnimation(Animation anim) {
8349 if (localLOGV) Log.v(
8350 TAG, "Setting animation in " + this + ": " + anim);
8351 animation = anim;
8352 animating = false;
8353 anim.restrictDuration(MAX_ANIMATION_DURATION);
8354 anim.scaleCurrentDuration(mTransitionAnimationScale);
8355 int zorder = anim.getZAdjustment();
8356 int adj = 0;
8357 if (zorder == Animation.ZORDER_TOP) {
8358 adj = TYPE_LAYER_OFFSET;
8359 } else if (zorder == Animation.ZORDER_BOTTOM) {
8360 adj = -TYPE_LAYER_OFFSET;
8361 }
Romain Guy06882f82009-06-10 13:36:04 -07008362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008363 if (animLayerAdjustment != adj) {
8364 animLayerAdjustment = adj;
8365 updateLayers();
8366 }
8367 }
Romain Guy06882f82009-06-10 13:36:04 -07008368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008369 public void setDummyAnimation() {
8370 if (animation == null) {
8371 if (localLOGV) Log.v(
8372 TAG, "Setting dummy animation in " + this);
8373 animation = sDummyAnimation;
8374 }
8375 }
8376
8377 public void clearAnimation() {
8378 if (animation != null) {
8379 animation = null;
8380 animating = true;
8381 }
8382 }
Romain Guy06882f82009-06-10 13:36:04 -07008383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384 void updateLayers() {
8385 final int N = allAppWindows.size();
8386 final int adj = animLayerAdjustment;
8387 for (int i=0; i<N; i++) {
8388 WindowState w = allAppWindows.get(i);
8389 w.mAnimLayer = w.mLayer + adj;
8390 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
8391 + w.mAnimLayer);
8392 if (w == mInputMethodTarget) {
8393 setInputMethodAnimLayerAdjustment(adj);
8394 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008395 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008396 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 }
8399 }
Romain Guy06882f82009-06-10 13:36:04 -07008400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008401 void sendAppVisibilityToClients() {
8402 final int N = allAppWindows.size();
8403 for (int i=0; i<N; i++) {
8404 WindowState win = allAppWindows.get(i);
8405 if (win == startingWindow && clientHidden) {
8406 // Don't hide the starting window.
8407 continue;
8408 }
8409 try {
8410 if (DEBUG_VISIBILITY) Log.v(TAG,
8411 "Setting visibility of " + win + ": " + (!clientHidden));
8412 win.mClient.dispatchAppVisibility(!clientHidden);
8413 } catch (RemoteException e) {
8414 }
8415 }
8416 }
Romain Guy06882f82009-06-10 13:36:04 -07008417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008418 void showAllWindowsLocked() {
8419 final int NW = allAppWindows.size();
8420 for (int i=0; i<NW; i++) {
8421 WindowState w = allAppWindows.get(i);
8422 if (DEBUG_VISIBILITY) Log.v(TAG,
8423 "performing show on: " + w);
8424 w.performShowLocked();
8425 }
8426 }
Romain Guy06882f82009-06-10 13:36:04 -07008427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008428 // This must be called while inside a transaction.
8429 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08008430 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008431 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008433 if (animation == sDummyAnimation) {
8434 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008435 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008436 // when it is really time to animate, this will be set to
8437 // a real animation and the next call will execute normally.
8438 return false;
8439 }
Romain Guy06882f82009-06-10 13:36:04 -07008440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008441 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8442 if (!animating) {
8443 if (DEBUG_ANIM) Log.v(
8444 TAG, "Starting animation in " + this +
8445 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8446 + " scale=" + mTransitionAnimationScale
8447 + " allDrawn=" + allDrawn + " animating=" + animating);
8448 animation.initialize(dw, dh, dw, dh);
8449 animation.setStartTime(currentTime);
8450 animating = true;
8451 }
8452 transformation.clear();
8453 final boolean more = animation.getTransformation(
8454 currentTime, transformation);
8455 if (DEBUG_ANIM) Log.v(
8456 TAG, "Stepped animation in " + this +
8457 ": more=" + more + ", xform=" + transformation);
8458 if (more) {
8459 // we're done!
8460 hasTransformation = true;
8461 return true;
8462 }
8463 if (DEBUG_ANIM) Log.v(
8464 TAG, "Finished animation in " + this +
8465 " @ " + currentTime);
8466 animation = null;
8467 }
8468 } else if (animation != null) {
8469 // If the display is frozen, and there is a pending animation,
8470 // clear it and make sure we run the cleanup code.
8471 animating = true;
8472 animation = null;
8473 }
8474
8475 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008477 if (!animating) {
8478 return false;
8479 }
8480
8481 clearAnimation();
8482 animating = false;
8483 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8484 moveInputMethodWindowsIfNeededLocked(true);
8485 }
Romain Guy06882f82009-06-10 13:36:04 -07008486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008487 if (DEBUG_ANIM) Log.v(
8488 TAG, "Animation done in " + this
8489 + ": reportedVisible=" + reportedVisible);
8490
8491 transformation.clear();
8492 if (animLayerAdjustment != 0) {
8493 animLayerAdjustment = 0;
8494 updateLayers();
8495 }
Romain Guy06882f82009-06-10 13:36:04 -07008496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008497 final int N = windows.size();
8498 for (int i=0; i<N; i++) {
8499 ((WindowState)windows.get(i)).finishExit();
8500 }
8501 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008503 return false;
8504 }
8505
8506 void updateReportedVisibilityLocked() {
8507 if (appToken == null) {
8508 return;
8509 }
Romain Guy06882f82009-06-10 13:36:04 -07008510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008511 int numInteresting = 0;
8512 int numVisible = 0;
8513 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008515 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8516 final int N = allAppWindows.size();
8517 for (int i=0; i<N; i++) {
8518 WindowState win = allAppWindows.get(i);
Dianne Hackborn6cf67fa2009-12-21 16:46:34 -08008519 if (win == startingWindow || win.mAppFreezing
8520 || win.mViewVisibility != View.VISIBLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 continue;
8522 }
8523 if (DEBUG_VISIBILITY) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008524 Log.v(TAG, "Win " + win + ": isDrawn="
8525 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008526 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008527 if (!win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008528 Log.v(TAG, "Not displayed: s=" + win.mSurface
8529 + " pv=" + win.mPolicyVisibility
8530 + " dp=" + win.mDrawPending
8531 + " cdp=" + win.mCommitDrawPending
8532 + " ah=" + win.mAttachedHidden
8533 + " th="
8534 + (win.mAppToken != null
8535 ? win.mAppToken.hiddenRequested : false)
8536 + " a=" + win.mAnimating);
8537 }
8538 }
8539 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008540 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008541 if (!win.isAnimating()) {
8542 numVisible++;
8543 }
8544 nowGone = false;
8545 } else if (win.isAnimating()) {
8546 nowGone = false;
8547 }
8548 }
Romain Guy06882f82009-06-10 13:36:04 -07008549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008550 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8551 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8552 + numInteresting + " visible=" + numVisible);
8553 if (nowVisible != reportedVisible) {
8554 if (DEBUG_VISIBILITY) Log.v(
8555 TAG, "Visibility changed in " + this
8556 + ": vis=" + nowVisible);
8557 reportedVisible = nowVisible;
8558 Message m = mH.obtainMessage(
8559 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8560 nowVisible ? 1 : 0,
8561 nowGone ? 1 : 0,
8562 this);
8563 mH.sendMessage(m);
8564 }
8565 }
Romain Guy06882f82009-06-10 13:36:04 -07008566
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008567 WindowState findMainWindow() {
8568 int j = windows.size();
8569 while (j > 0) {
8570 j--;
8571 WindowState win = windows.get(j);
8572 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8573 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8574 return win;
8575 }
8576 }
8577 return null;
8578 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008580 void dump(PrintWriter pw, String prefix) {
8581 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008582 if (appToken != null) {
8583 pw.print(prefix); pw.println("app=true");
8584 }
8585 if (allAppWindows.size() > 0) {
8586 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8587 }
8588 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008589 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008590 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8591 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8592 pw.print(" clientHidden="); pw.print(clientHidden);
8593 pw.print(" willBeHidden="); pw.print(willBeHidden);
8594 pw.print(" reportedVisible="); pw.println(reportedVisible);
8595 if (paused || freezingScreen) {
8596 pw.print(prefix); pw.print("paused="); pw.print(paused);
8597 pw.print(" freezingScreen="); pw.println(freezingScreen);
8598 }
8599 if (numInterestingWindows != 0 || numDrawnWindows != 0
8600 || inPendingTransaction || allDrawn) {
8601 pw.print(prefix); pw.print("numInterestingWindows=");
8602 pw.print(numInterestingWindows);
8603 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8604 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8605 pw.print(" allDrawn="); pw.println(allDrawn);
8606 }
8607 if (animating || animation != null) {
8608 pw.print(prefix); pw.print("animating="); pw.print(animating);
8609 pw.print(" animation="); pw.println(animation);
8610 }
8611 if (animLayerAdjustment != 0) {
8612 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8613 }
8614 if (hasTransformation) {
8615 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8616 pw.print(" transformation="); transformation.printShortString(pw);
8617 pw.println();
8618 }
8619 if (startingData != null || removed || firstWindowDrawn) {
8620 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8621 pw.print(" removed="); pw.print(removed);
8622 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8623 }
8624 if (startingWindow != null || startingView != null
8625 || startingDisplayed || startingMoved) {
8626 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8627 pw.print(" startingView="); pw.print(startingView);
8628 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8629 pw.print(" startingMoved"); pw.println(startingMoved);
8630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008631 }
8632
8633 @Override
8634 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008635 if (stringName == null) {
8636 StringBuilder sb = new StringBuilder();
8637 sb.append("AppWindowToken{");
8638 sb.append(Integer.toHexString(System.identityHashCode(this)));
8639 sb.append(" token="); sb.append(token); sb.append('}');
8640 stringName = sb.toString();
8641 }
8642 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 }
8644 }
Romain Guy06882f82009-06-10 13:36:04 -07008645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008646 // -------------------------------------------------------------
8647 // DummyAnimation
8648 // -------------------------------------------------------------
8649
8650 // This is an animation that does nothing: it just immediately finishes
8651 // itself every time it is called. It is used as a stub animation in cases
8652 // where we want to synchronize multiple things that may be animating.
8653 static final class DummyAnimation extends Animation {
8654 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8655 return false;
8656 }
8657 }
8658 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008660 // -------------------------------------------------------------
8661 // Async Handler
8662 // -------------------------------------------------------------
8663
8664 static final class StartingData {
8665 final String pkg;
8666 final int theme;
8667 final CharSequence nonLocalizedLabel;
8668 final int labelRes;
8669 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008671 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8672 int _labelRes, int _icon) {
8673 pkg = _pkg;
8674 theme = _theme;
8675 nonLocalizedLabel = _nonLocalizedLabel;
8676 labelRes = _labelRes;
8677 icon = _icon;
8678 }
8679 }
8680
8681 private final class H extends Handler {
8682 public static final int REPORT_FOCUS_CHANGE = 2;
8683 public static final int REPORT_LOSING_FOCUS = 3;
8684 public static final int ANIMATE = 4;
8685 public static final int ADD_STARTING = 5;
8686 public static final int REMOVE_STARTING = 6;
8687 public static final int FINISHED_STARTING = 7;
8688 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008689 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8690 public static final int HOLD_SCREEN_CHANGED = 12;
8691 public static final int APP_TRANSITION_TIMEOUT = 13;
8692 public static final int PERSIST_ANIMATION_SCALE = 14;
8693 public static final int FORCE_GC = 15;
8694 public static final int ENABLE_SCREEN = 16;
8695 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008696 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008698 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008700 public H() {
8701 }
Romain Guy06882f82009-06-10 13:36:04 -07008702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008703 @Override
8704 public void handleMessage(Message msg) {
8705 switch (msg.what) {
8706 case REPORT_FOCUS_CHANGE: {
8707 WindowState lastFocus;
8708 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008710 synchronized(mWindowMap) {
8711 lastFocus = mLastFocus;
8712 newFocus = mCurrentFocus;
8713 if (lastFocus == newFocus) {
8714 // Focus is not changing, so nothing to do.
8715 return;
8716 }
8717 mLastFocus = newFocus;
8718 //Log.i(TAG, "Focus moving from " + lastFocus
8719 // + " to " + newFocus);
8720 if (newFocus != null && lastFocus != null
8721 && !newFocus.isDisplayedLw()) {
8722 //Log.i(TAG, "Delaying loss of focus...");
8723 mLosingFocus.add(lastFocus);
8724 lastFocus = null;
8725 }
8726 }
8727
8728 if (lastFocus != newFocus) {
8729 //System.out.println("Changing focus from " + lastFocus
8730 // + " to " + newFocus);
8731 if (newFocus != null) {
8732 try {
8733 //Log.i(TAG, "Gaining focus: " + newFocus);
8734 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8735 } catch (RemoteException e) {
8736 // Ignore if process has died.
8737 }
8738 }
8739
8740 if (lastFocus != null) {
8741 try {
8742 //Log.i(TAG, "Losing focus: " + lastFocus);
8743 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8744 } catch (RemoteException e) {
8745 // Ignore if process has died.
8746 }
8747 }
8748 }
8749 } break;
8750
8751 case REPORT_LOSING_FOCUS: {
8752 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008754 synchronized(mWindowMap) {
8755 losers = mLosingFocus;
8756 mLosingFocus = new ArrayList<WindowState>();
8757 }
8758
8759 final int N = losers.size();
8760 for (int i=0; i<N; i++) {
8761 try {
8762 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8763 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8764 } catch (RemoteException e) {
8765 // Ignore if process has died.
8766 }
8767 }
8768 } break;
8769
8770 case ANIMATE: {
8771 synchronized(mWindowMap) {
8772 mAnimationPending = false;
8773 performLayoutAndPlaceSurfacesLocked();
8774 }
8775 } break;
8776
8777 case ADD_STARTING: {
8778 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8779 final StartingData sd = wtoken.startingData;
8780
8781 if (sd == null) {
8782 // Animation has been canceled... do nothing.
8783 return;
8784 }
Romain Guy06882f82009-06-10 13:36:04 -07008785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008786 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8787 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008789 View view = null;
8790 try {
8791 view = mPolicy.addStartingWindow(
8792 wtoken.token, sd.pkg,
8793 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8794 sd.icon);
8795 } catch (Exception e) {
8796 Log.w(TAG, "Exception when adding starting window", e);
8797 }
8798
8799 if (view != null) {
8800 boolean abort = false;
8801
8802 synchronized(mWindowMap) {
8803 if (wtoken.removed || wtoken.startingData == null) {
8804 // If the window was successfully added, then
8805 // we need to remove it.
8806 if (wtoken.startingWindow != null) {
8807 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8808 "Aborted starting " + wtoken
8809 + ": removed=" + wtoken.removed
8810 + " startingData=" + wtoken.startingData);
8811 wtoken.startingWindow = null;
8812 wtoken.startingData = null;
8813 abort = true;
8814 }
8815 } else {
8816 wtoken.startingView = view;
8817 }
8818 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8819 "Added starting " + wtoken
8820 + ": startingWindow="
8821 + wtoken.startingWindow + " startingView="
8822 + wtoken.startingView);
8823 }
8824
8825 if (abort) {
8826 try {
8827 mPolicy.removeStartingWindow(wtoken.token, view);
8828 } catch (Exception e) {
8829 Log.w(TAG, "Exception when removing starting window", e);
8830 }
8831 }
8832 }
8833 } break;
8834
8835 case REMOVE_STARTING: {
8836 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8837 IBinder token = null;
8838 View view = null;
8839 synchronized (mWindowMap) {
8840 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8841 + wtoken + ": startingWindow="
8842 + wtoken.startingWindow + " startingView="
8843 + wtoken.startingView);
8844 if (wtoken.startingWindow != null) {
8845 view = wtoken.startingView;
8846 token = wtoken.token;
8847 wtoken.startingData = null;
8848 wtoken.startingView = null;
8849 wtoken.startingWindow = null;
8850 }
8851 }
8852 if (view != null) {
8853 try {
8854 mPolicy.removeStartingWindow(token, view);
8855 } catch (Exception e) {
8856 Log.w(TAG, "Exception when removing starting window", e);
8857 }
8858 }
8859 } break;
8860
8861 case FINISHED_STARTING: {
8862 IBinder token = null;
8863 View view = null;
8864 while (true) {
8865 synchronized (mWindowMap) {
8866 final int N = mFinishedStarting.size();
8867 if (N <= 0) {
8868 break;
8869 }
8870 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8871
8872 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8873 "Finished starting " + wtoken
8874 + ": startingWindow=" + wtoken.startingWindow
8875 + " startingView=" + wtoken.startingView);
8876
8877 if (wtoken.startingWindow == null) {
8878 continue;
8879 }
8880
8881 view = wtoken.startingView;
8882 token = wtoken.token;
8883 wtoken.startingData = null;
8884 wtoken.startingView = null;
8885 wtoken.startingWindow = null;
8886 }
8887
8888 try {
8889 mPolicy.removeStartingWindow(token, view);
8890 } catch (Exception e) {
8891 Log.w(TAG, "Exception when removing starting window", e);
8892 }
8893 }
8894 } break;
8895
8896 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8897 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8898
8899 boolean nowVisible = msg.arg1 != 0;
8900 boolean nowGone = msg.arg2 != 0;
8901
8902 try {
8903 if (DEBUG_VISIBILITY) Log.v(
8904 TAG, "Reporting visible in " + wtoken
8905 + " visible=" + nowVisible
8906 + " gone=" + nowGone);
8907 if (nowVisible) {
8908 wtoken.appToken.windowsVisible();
8909 } else {
8910 wtoken.appToken.windowsGone();
8911 }
8912 } catch (RemoteException ex) {
8913 }
8914 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 case WINDOW_FREEZE_TIMEOUT: {
8917 synchronized (mWindowMap) {
8918 Log.w(TAG, "Window freeze timeout expired.");
8919 int i = mWindows.size();
8920 while (i > 0) {
8921 i--;
8922 WindowState w = (WindowState)mWindows.get(i);
8923 if (w.mOrientationChanging) {
8924 w.mOrientationChanging = false;
8925 Log.w(TAG, "Force clearing orientation change: " + w);
8926 }
8927 }
8928 performLayoutAndPlaceSurfacesLocked();
8929 }
8930 break;
8931 }
Romain Guy06882f82009-06-10 13:36:04 -07008932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008933 case HOLD_SCREEN_CHANGED: {
8934 Session oldHold;
8935 Session newHold;
8936 synchronized (mWindowMap) {
8937 oldHold = mLastReportedHold;
8938 newHold = (Session)msg.obj;
8939 mLastReportedHold = newHold;
8940 }
Romain Guy06882f82009-06-10 13:36:04 -07008941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008942 if (oldHold != newHold) {
8943 try {
8944 if (oldHold != null) {
8945 mBatteryStats.noteStopWakelock(oldHold.mUid,
8946 "window",
8947 BatteryStats.WAKE_TYPE_WINDOW);
8948 }
8949 if (newHold != null) {
8950 mBatteryStats.noteStartWakelock(newHold.mUid,
8951 "window",
8952 BatteryStats.WAKE_TYPE_WINDOW);
8953 }
8954 } catch (RemoteException e) {
8955 }
8956 }
8957 break;
8958 }
Romain Guy06882f82009-06-10 13:36:04 -07008959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008960 case APP_TRANSITION_TIMEOUT: {
8961 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008962 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008963 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8964 "*** APP TRANSITION TIMEOUT");
8965 mAppTransitionReady = true;
8966 mAppTransitionTimeout = true;
8967 performLayoutAndPlaceSurfacesLocked();
8968 }
8969 }
8970 break;
8971 }
Romain Guy06882f82009-06-10 13:36:04 -07008972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008973 case PERSIST_ANIMATION_SCALE: {
8974 Settings.System.putFloat(mContext.getContentResolver(),
8975 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8976 Settings.System.putFloat(mContext.getContentResolver(),
8977 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8978 break;
8979 }
Romain Guy06882f82009-06-10 13:36:04 -07008980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008981 case FORCE_GC: {
8982 synchronized(mWindowMap) {
8983 if (mAnimationPending) {
8984 // If we are animating, don't do the gc now but
8985 // delay a bit so we don't interrupt the animation.
8986 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8987 2000);
8988 return;
8989 }
8990 // If we are currently rotating the display, it will
8991 // schedule a new message when done.
8992 if (mDisplayFrozen) {
8993 return;
8994 }
8995 mFreezeGcPending = 0;
8996 }
8997 Runtime.getRuntime().gc();
8998 break;
8999 }
Romain Guy06882f82009-06-10 13:36:04 -07009000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009001 case ENABLE_SCREEN: {
9002 performEnableScreen();
9003 break;
9004 }
Romain Guy06882f82009-06-10 13:36:04 -07009005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009006 case APP_FREEZE_TIMEOUT: {
9007 synchronized (mWindowMap) {
9008 Log.w(TAG, "App freeze timeout expired.");
9009 int i = mAppTokens.size();
9010 while (i > 0) {
9011 i--;
9012 AppWindowToken tok = mAppTokens.get(i);
9013 if (tok.freezingScreen) {
9014 Log.w(TAG, "Force clearing freeze: " + tok);
9015 unsetAppFreezingScreenLocked(tok, true, true);
9016 }
9017 }
9018 }
9019 break;
9020 }
Romain Guy06882f82009-06-10 13:36:04 -07009021
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009022 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07009023 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009024 sendNewConfiguration();
9025 }
9026 break;
9027 }
Romain Guy06882f82009-06-10 13:36:04 -07009028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009029 }
9030 }
9031 }
9032
9033 // -------------------------------------------------------------
9034 // IWindowManager API
9035 // -------------------------------------------------------------
9036
9037 public IWindowSession openSession(IInputMethodClient client,
9038 IInputContext inputContext) {
9039 if (client == null) throw new IllegalArgumentException("null client");
9040 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9041 return new Session(client, inputContext);
9042 }
9043
9044 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9045 synchronized (mWindowMap) {
9046 // The focus for the client is the window immediately below
9047 // where we would place the input method window.
9048 int idx = findDesiredInputMethodWindowIndexLocked(false);
9049 WindowState imFocus;
9050 if (idx > 0) {
9051 imFocus = (WindowState)mWindows.get(idx-1);
9052 if (imFocus != null) {
9053 if (imFocus.mSession.mClient != null &&
9054 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9055 return true;
9056 }
9057 }
9058 }
9059 }
9060 return false;
9061 }
Romain Guy06882f82009-06-10 13:36:04 -07009062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009063 // -------------------------------------------------------------
9064 // Internals
9065 // -------------------------------------------------------------
9066
9067 final WindowState windowForClientLocked(Session session, IWindow client) {
9068 return windowForClientLocked(session, client.asBinder());
9069 }
Romain Guy06882f82009-06-10 13:36:04 -07009070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009071 final WindowState windowForClientLocked(Session session, IBinder client) {
9072 WindowState win = mWindowMap.get(client);
9073 if (localLOGV) Log.v(
9074 TAG, "Looking up client " + client + ": " + win);
9075 if (win == null) {
9076 RuntimeException ex = new RuntimeException();
9077 Log.w(TAG, "Requested window " + client + " does not exist", ex);
9078 return null;
9079 }
9080 if (session != null && win.mSession != session) {
9081 RuntimeException ex = new RuntimeException();
9082 Log.w(TAG, "Requested window " + client + " is in session " +
9083 win.mSession + ", not " + session, ex);
9084 return null;
9085 }
9086
9087 return win;
9088 }
9089
Dianne Hackborna8f60182009-09-01 19:01:50 -07009090 final void rebuildAppWindowListLocked() {
9091 int NW = mWindows.size();
9092 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009093 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009094 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009095
Dianne Hackborna8f60182009-09-01 19:01:50 -07009096 // First remove all existing app windows.
9097 i=0;
9098 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009099 WindowState w = (WindowState)mWindows.get(i);
9100 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009101 WindowState win = (WindowState)mWindows.remove(i);
9102 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
9103 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009104 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009105 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009106 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009107 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9108 && lastWallpaper == i-1) {
9109 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009110 }
9111 i++;
9112 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009113
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009114 // The wallpaper window(s) typically live at the bottom of the stack,
9115 // so skip them before adding app tokens.
9116 lastWallpaper++;
9117 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009118
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009119 // First add all of the exiting app tokens... these are no longer
9120 // in the main app list, but still have windows shown. We put them
9121 // in the back because now that the animation is over we no longer
9122 // will care about them.
9123 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009124 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009125 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009127
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009128 // And add in the still active app tokens in Z order.
9129 NT = mAppTokens.size();
9130 for (int j=0; j<NT; j++) {
9131 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009133
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009134 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009135 if (i != numRemoved) {
9136 Log.w(TAG, "Rebuild removed " + numRemoved
9137 + " windows but added " + i);
9138 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009139 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009141 private final void assignLayersLocked() {
9142 int N = mWindows.size();
9143 int curBaseLayer = 0;
9144 int curLayer = 0;
9145 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009147 for (i=0; i<N; i++) {
9148 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009149 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9150 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009151 curLayer += WINDOW_LAYER_MULTIPLIER;
9152 w.mLayer = curLayer;
9153 } else {
9154 curBaseLayer = curLayer = w.mBaseLayer;
9155 w.mLayer = curLayer;
9156 }
9157 if (w.mTargetAppToken != null) {
9158 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9159 } else if (w.mAppToken != null) {
9160 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9161 } else {
9162 w.mAnimLayer = w.mLayer;
9163 }
9164 if (w.mIsImWindow) {
9165 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009166 } else if (w.mIsWallpaper) {
9167 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009168 }
9169 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
9170 + w.mAnimLayer);
9171 //System.out.println(
9172 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9173 }
9174 }
9175
9176 private boolean mInLayout = false;
9177 private final void performLayoutAndPlaceSurfacesLocked() {
9178 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009179 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009180 throw new RuntimeException("Recursive call!");
9181 }
9182 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
9183 return;
9184 }
9185
9186 boolean recoveringMemory = false;
9187 if (mForceRemoves != null) {
9188 recoveringMemory = true;
9189 // Wait a little it for things to settle down, and off we go.
9190 for (int i=0; i<mForceRemoves.size(); i++) {
9191 WindowState ws = mForceRemoves.get(i);
9192 Log.i(TAG, "Force removing: " + ws);
9193 removeWindowInnerLocked(ws.mSession, ws);
9194 }
9195 mForceRemoves = null;
9196 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
9197 Object tmp = new Object();
9198 synchronized (tmp) {
9199 try {
9200 tmp.wait(250);
9201 } catch (InterruptedException e) {
9202 }
9203 }
9204 }
Romain Guy06882f82009-06-10 13:36:04 -07009205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009206 mInLayout = true;
9207 try {
9208 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009210 int i = mPendingRemove.size()-1;
9211 if (i >= 0) {
9212 while (i >= 0) {
9213 WindowState w = mPendingRemove.get(i);
9214 removeWindowInnerLocked(w.mSession, w);
9215 i--;
9216 }
9217 mPendingRemove.clear();
9218
9219 mInLayout = false;
9220 assignLayersLocked();
9221 mLayoutNeeded = true;
9222 performLayoutAndPlaceSurfacesLocked();
9223
9224 } else {
9225 mInLayout = false;
9226 if (mLayoutNeeded) {
9227 requestAnimationLocked(0);
9228 }
9229 }
9230 } catch (RuntimeException e) {
9231 mInLayout = false;
9232 Log.e(TAG, "Unhandled exception while layout out windows", e);
9233 }
9234 }
9235
9236 private final void performLayoutLockedInner() {
9237 final int dw = mDisplay.getWidth();
9238 final int dh = mDisplay.getHeight();
9239
9240 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009241 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009242 int i;
9243
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009244 if (DEBUG_LAYOUT) Log.v(TAG, "performLayout: needed="
9245 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009247 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009248
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009249 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009250 mPolicy.beginLayoutLw(dw, dh);
9251
9252 // First perform layout of any root windows (not attached
9253 // to another window).
9254 int topAttached = -1;
9255 for (i = N-1; i >= 0; i--) {
9256 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009257
9258 // Don't do layout of a window if it is not visible, or
9259 // soon won't be visible, to avoid wasting time and funky
9260 // changes while a window is animating away.
9261 final AppWindowToken atoken = win.mAppToken;
9262 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009263 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009264 || win.mRootToken.hidden
9265 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009266 || win.mAttachedHidden
9267 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009268
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009269 if (win.mLayoutAttached) {
9270 if (DEBUG_LAYOUT) Log.v(TAG, "First pass " + win
9271 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9272 + " mLayoutAttached=" + win.mLayoutAttached);
9273 if (DEBUG_LAYOUT && gone) Log.v(TAG, " (mViewVisibility="
9274 + win.mViewVisibility + " mRelayoutCalled="
9275 + win.mRelayoutCalled + " hidden="
9276 + win.mRootToken.hidden + " hiddenRequested="
9277 + (atoken != null && atoken.hiddenRequested)
9278 + " mAttachedHidden=" + win.mAttachedHidden);
9279 }
9280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009281 // If this view is GONE, then skip it -- keep the current
9282 // frame, and let the caller know so they can ignore it
9283 // if they want. (We do the normal layout for INVISIBLE
9284 // windows, since that means "perform layout as normal,
9285 // just don't display").
9286 if (!gone || !win.mHaveFrame) {
9287 if (!win.mLayoutAttached) {
9288 mPolicy.layoutWindowLw(win, win.mAttrs, null);
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009289 if (DEBUG_LAYOUT) Log.v(TAG, "-> mFrame="
9290 + win.mFrame + " mContainingFrame="
9291 + win.mContainingFrame + " mDisplayFrame="
9292 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293 } else {
9294 if (topAttached < 0) topAttached = i;
9295 }
9296 }
9297 }
Romain Guy06882f82009-06-10 13:36:04 -07009298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009299 // Now perform layout of attached windows, which usually
9300 // depend on the position of the window they are attached to.
9301 // XXX does not deal with windows that are attached to windows
9302 // that are themselves attached.
9303 for (i = topAttached; i >= 0; i--) {
9304 WindowState win = (WindowState) mWindows.get(i);
9305
9306 // If this view is GONE, then skip it -- keep the current
9307 // frame, and let the caller know so they can ignore it
9308 // if they want. (We do the normal layout for INVISIBLE
9309 // windows, since that means "perform layout as normal,
9310 // just don't display").
9311 if (win.mLayoutAttached) {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009312 if (DEBUG_LAYOUT) Log.v(TAG, "Second pass " + win
9313 + " mHaveFrame=" + win.mHaveFrame
9314 + " mViewVisibility=" + win.mViewVisibility
9315 + " mRelayoutCalled=" + win.mRelayoutCalled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009316 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9317 || !win.mHaveFrame) {
9318 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009319 if (DEBUG_LAYOUT) Log.v(TAG, "-> mFrame="
9320 + win.mFrame + " mContainingFrame="
9321 + win.mContainingFrame + " mDisplayFrame="
9322 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009323 }
9324 }
9325 }
9326
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009327 int changes = mPolicy.finishLayoutLw();
9328 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9329 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9330 assignLayersLocked();
9331 }
9332 }
9333 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009334 mLayoutNeeded = false;
9335 } else if (repeats > 2) {
9336 Log.w(TAG, "Layout repeat aborted after too many iterations");
9337 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009338 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9339 Configuration newConfig = updateOrientationFromAppTokensLocked(
9340 null, null);
9341 if (newConfig != null) {
9342 mLayoutNeeded = true;
9343 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9344 }
9345 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009346 } else {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009347 if (DEBUG_LAYOUT) Log.v(TAG, "Repeating layout because changes=0x"
9348 + Integer.toHexString(changes));
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009349 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009350 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009351 if (DEBUG_LAYOUT) Log.v(TAG, "Computing new config from layout");
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009352 Configuration newConfig = updateOrientationFromAppTokensLocked(
9353 null, null);
9354 if (newConfig != null) {
9355 mH.sendEmptyMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION);
9356 }
9357 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009359 }
9360 }
Romain Guy06882f82009-06-10 13:36:04 -07009361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009362 private final void performLayoutAndPlaceSurfacesLockedInner(
9363 boolean recoveringMemory) {
9364 final long currentTime = SystemClock.uptimeMillis();
9365 final int dw = mDisplay.getWidth();
9366 final int dh = mDisplay.getHeight();
9367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009368 int i;
9369
9370 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009371 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009373 if (mFxSession == null) {
9374 mFxSession = new SurfaceSession();
9375 }
Romain Guy06882f82009-06-10 13:36:04 -07009376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009377 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
9378
9379 // Initialize state of exiting tokens.
9380 for (i=mExitingTokens.size()-1; i>=0; i--) {
9381 mExitingTokens.get(i).hasVisible = false;
9382 }
9383
9384 // Initialize state of exiting applications.
9385 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9386 mExitingAppTokens.get(i).hasVisible = false;
9387 }
9388
9389 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009390 boolean orientationChangeComplete = true;
9391 Session holdScreen = null;
9392 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009393 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394 boolean focusDisplayed = false;
9395 boolean animating = false;
9396
9397 Surface.openTransaction();
9398 try {
9399 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009400 boolean forceHiding = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009401 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009402
9403 do {
9404 final int transactionSequence = ++mTransactionSequence;
9405
9406 // Update animations of all applications, including those
9407 // associated with exiting/removed apps
9408 boolean tokensAnimating = false;
9409 final int NAT = mAppTokens.size();
9410 for (i=0; i<NAT; i++) {
9411 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9412 tokensAnimating = true;
9413 }
9414 }
9415 final int NEAT = mExitingAppTokens.size();
9416 for (i=0; i<NEAT; i++) {
9417 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9418 tokensAnimating = true;
9419 }
9420 }
9421
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009422 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "*** ANIM STEP: seq="
9423 + transactionSequence + " tokensAnimating="
9424 + tokensAnimating);
9425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009426 animating = tokensAnimating;
9427 restart = false;
9428
9429 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009430 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009431 boolean focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009432
9433 mPolicy.beginAnimationLw(dw, dh);
9434
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009435 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009437 for (i=N-1; i>=0; i--) {
9438 WindowState w = (WindowState)mWindows.get(i);
9439
9440 final WindowManager.LayoutParams attrs = w.mAttrs;
9441
9442 if (w.mSurface != null) {
9443 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009444 if (w.commitFinishDrawingLocked(currentTime)) {
9445 if ((w.mAttrs.flags
9446 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009447 if (DEBUG_WALLPAPER) Log.v(TAG,
9448 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009449 wallpaperMayChange = true;
9450 }
9451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009452
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009453 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009454 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9455 animating = true;
9456 //w.dump(" ");
9457 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009458 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9459 wallpaperMayChange = true;
9460 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009461
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009462 if (mPolicy.doesForceHide(w, attrs)) {
9463 if (!wasAnimating && animating) {
9464 wallpaperForceHidingChanged = true;
9465 focusMayChange = true;
9466 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9467 forceHiding = true;
9468 }
9469 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9470 boolean changed;
9471 if (forceHiding) {
9472 changed = w.hideLw(false, false);
9473 } else {
9474 changed = w.showLw(false, false);
9475 if (changed && wallpaperForceHidingChanged
9476 && w.isReadyForDisplay()) {
9477 // Assume we will need to animate. If
9478 // we don't (because the wallpaper will
9479 // stay with the lock screen), then we will
9480 // clean up later.
9481 Animation a = mPolicy.createForceHideEnterAnimation();
9482 if (a != null) {
9483 w.setAnimation(a);
9484 }
9485 }
9486 }
9487 if (changed && (attrs.flags
9488 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9489 wallpaperMayChange = true;
9490 }
9491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009493 mPolicy.animatingWindowLw(w, attrs);
9494 }
9495
9496 final AppWindowToken atoken = w.mAppToken;
9497 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9498 if (atoken.lastTransactionSequence != transactionSequence) {
9499 atoken.lastTransactionSequence = transactionSequence;
9500 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9501 atoken.startingDisplayed = false;
9502 }
9503 if ((w.isOnScreen() || w.mAttrs.type
9504 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9505 && !w.mExiting && !w.mDestroying) {
9506 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009507 Log.v(TAG, "Eval win " + w + ": isDrawn="
9508 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009509 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009510 if (!w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009511 Log.v(TAG, "Not displayed: s=" + w.mSurface
9512 + " pv=" + w.mPolicyVisibility
9513 + " dp=" + w.mDrawPending
9514 + " cdp=" + w.mCommitDrawPending
9515 + " ah=" + w.mAttachedHidden
9516 + " th=" + atoken.hiddenRequested
9517 + " a=" + w.mAnimating);
9518 }
9519 }
9520 if (w != atoken.startingWindow) {
9521 if (!atoken.freezingScreen || !w.mAppFreezing) {
9522 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009523 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009524 atoken.numDrawnWindows++;
9525 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
9526 "tokenMayBeDrawn: " + atoken
9527 + " freezingScreen=" + atoken.freezingScreen
9528 + " mAppFreezing=" + w.mAppFreezing);
9529 tokenMayBeDrawn = true;
9530 }
9531 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009532 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009533 atoken.startingDisplayed = true;
9534 }
9535 }
9536 } else if (w.mReadyToShow) {
9537 w.performShowLocked();
9538 }
9539 }
9540
9541 if (mPolicy.finishAnimationLw()) {
9542 restart = true;
9543 }
9544
9545 if (tokenMayBeDrawn) {
9546 // See if any windows have been drawn, so they (and others
9547 // associated with them) can now be shown.
9548 final int NT = mTokenList.size();
9549 for (i=0; i<NT; i++) {
9550 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9551 if (wtoken == null) {
9552 continue;
9553 }
9554 if (wtoken.freezingScreen) {
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.showAllWindowsLocked();
9562 unsetAppFreezingScreenLocked(wtoken, false, true);
9563 orientationChangeComplete = true;
9564 }
9565 } else if (!wtoken.allDrawn) {
9566 int numInteresting = wtoken.numInterestingWindows;
9567 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
9568 if (DEBUG_VISIBILITY) Log.v(TAG,
9569 "allDrawn: " + wtoken
9570 + " interesting=" + numInteresting
9571 + " drawn=" + wtoken.numDrawnWindows);
9572 wtoken.allDrawn = true;
9573 restart = true;
9574
9575 // We can now show all of the drawn windows!
9576 if (!mOpeningApps.contains(wtoken)) {
9577 wtoken.showAllWindowsLocked();
9578 }
9579 }
9580 }
9581 }
9582 }
9583
9584 // If we are ready to perform an app transition, check through
9585 // all of the app tokens to be shown and see if they are ready
9586 // to go.
9587 if (mAppTransitionReady) {
9588 int NN = mOpeningApps.size();
9589 boolean goodToGo = true;
9590 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9591 "Checking " + NN + " opening apps (frozen="
9592 + mDisplayFrozen + " timeout="
9593 + mAppTransitionTimeout + ")...");
9594 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9595 // If the display isn't frozen, wait to do anything until
9596 // all of the apps are ready. Otherwise just go because
9597 // we'll unfreeze the display when everyone is ready.
9598 for (i=0; i<NN && goodToGo; i++) {
9599 AppWindowToken wtoken = mOpeningApps.get(i);
9600 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9601 "Check opening app" + wtoken + ": allDrawn="
9602 + wtoken.allDrawn + " startingDisplayed="
9603 + wtoken.startingDisplayed);
9604 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9605 && !wtoken.startingMoved) {
9606 goodToGo = false;
9607 }
9608 }
9609 }
9610 if (goodToGo) {
9611 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9612 int transit = mNextAppTransition;
9613 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009614 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009615 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009616 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009617 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009618 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009619 mAppTransitionTimeout = false;
9620 mStartingIconInTransition = false;
9621 mSkipAppTransitionAnimation = false;
9622
9623 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9624
Dianne Hackborna8f60182009-09-01 19:01:50 -07009625 // If there are applications waiting to come to the
9626 // top of the stack, now is the time to move their windows.
9627 // (Note that we don't do apps going to the bottom
9628 // here -- we want to keep their windows in the old
9629 // Z-order until the animation completes.)
9630 if (mToTopApps.size() > 0) {
9631 NN = mAppTokens.size();
9632 for (i=0; i<NN; i++) {
9633 AppWindowToken wtoken = mAppTokens.get(i);
9634 if (wtoken.sendingToTop) {
9635 wtoken.sendingToTop = false;
9636 moveAppWindowsLocked(wtoken, NN, false);
9637 }
9638 }
9639 mToTopApps.clear();
9640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009641
Dianne Hackborn25994b42009-09-04 14:21:19 -07009642 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009643
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009644 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009645 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009646
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009647 // The top-most window will supply the layout params,
9648 // and we will determine it below.
9649 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009650 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009651 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009652
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009653 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009654 "New wallpaper target=" + mWallpaperTarget
9655 + ", lower target=" + mLowerWallpaperTarget
9656 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009657 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009658 // Do a first pass through the tokens for two
9659 // things:
9660 // (1) Determine if both the closing and opening
9661 // app token sets are wallpaper targets, in which
9662 // case special animations are needed
9663 // (since the wallpaper needs to stay static
9664 // behind them).
9665 // (2) Find the layout params of the top-most
9666 // application window in the tokens, which is
9667 // what will control the animation theme.
9668 final int NC = mClosingApps.size();
9669 NN = NC + mOpeningApps.size();
9670 for (i=0; i<NN; i++) {
9671 AppWindowToken wtoken;
9672 int mode;
9673 if (i < NC) {
9674 wtoken = mClosingApps.get(i);
9675 mode = 1;
9676 } else {
9677 wtoken = mOpeningApps.get(i-NC);
9678 mode = 2;
9679 }
9680 if (mLowerWallpaperTarget != null) {
9681 if (mLowerWallpaperTarget.mAppToken == wtoken
9682 || mUpperWallpaperTarget.mAppToken == wtoken) {
9683 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009684 }
9685 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009686 if (wtoken.appFullscreen) {
9687 WindowState ws = wtoken.findMainWindow();
9688 if (ws != null) {
9689 // If this is a compatibility mode
9690 // window, we will always use its anim.
9691 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9692 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009693 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009694 bestAnimLayer = Integer.MAX_VALUE;
9695 } else if (ws.mLayer > bestAnimLayer) {
9696 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009697 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009698 bestAnimLayer = ws.mLayer;
9699 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009700 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009701 }
9702 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009703
Dianne Hackborn25994b42009-09-04 14:21:19 -07009704 if (foundWallpapers == 3) {
9705 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9706 "Wallpaper animation!");
9707 switch (transit) {
9708 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9709 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9710 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9711 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9712 break;
9713 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9714 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9715 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9716 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9717 break;
9718 }
9719 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9720 "New transit: " + transit);
9721 } else if (oldWallpaper != null) {
9722 // We are transitioning from an activity with
9723 // a wallpaper to one without.
9724 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9725 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9726 "New transit away from wallpaper: " + transit);
9727 } else if (mWallpaperTarget != null) {
9728 // We are transitioning from an activity without
9729 // a wallpaper to now showing the wallpaper
9730 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9731 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9732 "New transit into wallpaper: " + transit);
9733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009734
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009735 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9736 mLastEnterAnimToken = animToken;
9737 mLastEnterAnimParams = animLp;
9738 } else if (mLastEnterAnimParams != null) {
9739 animLp = mLastEnterAnimParams;
9740 mLastEnterAnimToken = null;
9741 mLastEnterAnimParams = null;
9742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009743
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009744 // If all closing windows are obscured, then there is
9745 // no need to do an animation. This is the case, for
9746 // example, when this transition is being done behind
9747 // the lock screen.
9748 if (!mPolicy.allowAppAnimationsLw()) {
9749 animLp = null;
9750 }
9751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009752 NN = mOpeningApps.size();
9753 for (i=0; i<NN; i++) {
9754 AppWindowToken wtoken = mOpeningApps.get(i);
9755 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9756 "Now opening app" + wtoken);
9757 wtoken.reportedVisible = false;
9758 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009759 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009760 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009761 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009762 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009763 wtoken.showAllWindowsLocked();
9764 }
9765 NN = mClosingApps.size();
9766 for (i=0; i<NN; i++) {
9767 AppWindowToken wtoken = mClosingApps.get(i);
9768 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9769 "Now closing app" + wtoken);
9770 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009771 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009772 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009773 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009774 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009775 // Force the allDrawn flag, because we want to start
9776 // this guy's animations regardless of whether it's
9777 // gotten drawn.
9778 wtoken.allDrawn = true;
9779 }
9780
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009781 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009783 mOpeningApps.clear();
9784 mClosingApps.clear();
9785
9786 // This has changed the visibility of windows, so perform
9787 // a new layout to get them all up-to-date.
9788 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009789 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9790 assignLayersLocked();
9791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009792 performLayoutLockedInner();
9793 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009794 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009795
9796 restart = true;
9797 }
9798 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009799
Dianne Hackborna8f60182009-09-01 19:01:50 -07009800 if (!animating && mAppTransitionRunning) {
9801 // We have finished the animation of an app transition. To do
9802 // this, we have delayed a lot of operations like showing and
9803 // hiding apps, moving apps in Z-order, etc. The app token list
9804 // reflects the correct Z-order, but the window list may now
9805 // be out of sync with it. So here we will just rebuild the
9806 // entire app window list. Fun!
9807 mAppTransitionRunning = false;
9808 // Clear information about apps that were moving.
9809 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009810
Dianne Hackborna8f60182009-09-01 19:01:50 -07009811 rebuildAppWindowListLocked();
9812 restart = true;
9813 moveInputMethodWindowsIfNeededLocked(false);
9814 wallpaperMayChange = true;
9815 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009816 // Since the window list has been rebuilt, focus might
9817 // have to be recomputed since the actual order of windows
9818 // might have changed again.
9819 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009820 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009821
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009822 int adjResult = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009823
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009824 if (wallpaperForceHidingChanged && !restart && !mAppTransitionReady) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009825 // At this point, there was a window with a wallpaper that
9826 // was force hiding other windows behind it, but now it
9827 // is going away. This may be simple -- just animate
9828 // away the wallpaper and its window -- or it may be
9829 // hard -- the wallpaper now needs to be shown behind
9830 // something that was hidden.
9831 WindowState oldWallpaper = mWallpaperTarget;
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009832 if (mLowerWallpaperTarget != null
9833 && mLowerWallpaperTarget.mAppToken != null) {
9834 if (DEBUG_WALLPAPER) Log.v(TAG,
9835 "wallpaperForceHiding changed with lower="
9836 + mLowerWallpaperTarget);
9837 if (DEBUG_WALLPAPER) Log.v(TAG,
9838 "hidden=" + mLowerWallpaperTarget.mAppToken.hidden +
9839 " hiddenRequested=" + mLowerWallpaperTarget.mAppToken.hiddenRequested);
9840 if (mLowerWallpaperTarget.mAppToken.hidden) {
9841 // The lower target has become hidden before we
9842 // actually started the animation... let's completely
9843 // re-evaluate everything.
9844 mLowerWallpaperTarget = mUpperWallpaperTarget = null;
9845 restart = true;
9846 }
9847 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009848 adjResult = adjustWallpaperWindowsLocked();
9849 wallpaperMayChange = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009850 wallpaperForceHidingChanged = false;
9851 if (DEBUG_WALLPAPER) Log.v(TAG, "****** OLD: " + oldWallpaper
9852 + " NEW: " + mWallpaperTarget
9853 + " LOWER: " + mLowerWallpaperTarget);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009854 if (mLowerWallpaperTarget == null) {
9855 // Whoops, we don't need a special wallpaper animation.
9856 // Clear them out.
9857 forceHiding = false;
9858 for (i=N-1; i>=0; i--) {
9859 WindowState w = (WindowState)mWindows.get(i);
9860 if (w.mSurface != null) {
9861 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009862 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
9863 if (DEBUG_FOCUS) Log.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009864 forceHiding = true;
9865 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9866 if (!w.mAnimating) {
9867 // We set the animation above so it
9868 // is not yet running.
9869 w.clearAnimation();
9870 }
9871 }
9872 }
9873 }
9874 }
9875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009876
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009877 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009878 if (DEBUG_WALLPAPER) Log.v(TAG,
9879 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009880 adjResult = adjustWallpaperWindowsLocked();
9881 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009882
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009883 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9884 if (DEBUG_WALLPAPER) Log.v(TAG,
9885 "Wallpaper layer changed: assigning layers + relayout");
9886 restart = true;
9887 mLayoutNeeded = true;
9888 assignLayersLocked();
9889 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9890 if (DEBUG_WALLPAPER) Log.v(TAG,
9891 "Wallpaper visibility changed: relayout");
9892 restart = true;
9893 mLayoutNeeded = true;
9894 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009895
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009896 if (focusMayChange) {
9897 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009898 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009899 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009900 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009901 }
9902
9903 if (mLayoutNeeded) {
9904 restart = true;
9905 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009906 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009907
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009908 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "*** ANIM STEP: restart="
9909 + restart);
9910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 } while (restart);
9912
9913 // THIRD LOOP: Update the surfaces of all windows.
9914
9915 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9916
9917 boolean obscured = false;
9918 boolean blurring = false;
9919 boolean dimming = false;
9920 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009921 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009922 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009923
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009924 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 for (i=N-1; i>=0; i--) {
9927 WindowState w = (WindowState)mWindows.get(i);
9928
9929 boolean displayed = false;
9930 final WindowManager.LayoutParams attrs = w.mAttrs;
9931 final int attrFlags = attrs.flags;
9932
9933 if (w.mSurface != null) {
9934 w.computeShownFrameLocked();
9935 if (localLOGV) Log.v(
9936 TAG, "Placing surface #" + i + " " + w.mSurface
9937 + ": new=" + w.mShownFrame + ", old="
9938 + w.mLastShownFrame);
9939
9940 boolean resize;
9941 int width, height;
9942 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9943 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9944 w.mLastRequestedHeight != w.mRequestedHeight;
9945 // for a scaled surface, we just want to use
9946 // the requested size.
9947 width = w.mRequestedWidth;
9948 height = w.mRequestedHeight;
9949 w.mLastRequestedWidth = width;
9950 w.mLastRequestedHeight = height;
9951 w.mLastShownFrame.set(w.mShownFrame);
9952 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009953 if (SHOW_TRANSACTIONS) Log.i(
9954 TAG, " SURFACE " + w.mSurface
9955 + ": POS " + w.mShownFrame.left
9956 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009957 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9958 } catch (RuntimeException e) {
9959 Log.w(TAG, "Error positioning surface in " + w, e);
9960 if (!recoveringMemory) {
9961 reclaimSomeSurfaceMemoryLocked(w, "position");
9962 }
9963 }
9964 } else {
9965 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9966 width = w.mShownFrame.width();
9967 height = w.mShownFrame.height();
9968 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009969 }
9970
9971 if (resize) {
9972 if (width < 1) width = 1;
9973 if (height < 1) height = 1;
9974 if (w.mSurface != null) {
9975 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009976 if (SHOW_TRANSACTIONS) Log.i(
9977 TAG, " SURFACE " + w.mSurface + ": POS "
9978 + w.mShownFrame.left + ","
9979 + w.mShownFrame.top + " SIZE "
9980 + w.mShownFrame.width() + "x"
9981 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009982 w.mSurface.setSize(width, height);
9983 w.mSurface.setPosition(w.mShownFrame.left,
9984 w.mShownFrame.top);
9985 } catch (RuntimeException e) {
9986 // If something goes wrong with the surface (such
9987 // as running out of memory), don't take down the
9988 // entire system.
9989 Log.e(TAG, "Failure updating surface of " + w
9990 + "size=(" + width + "x" + height
9991 + "), pos=(" + w.mShownFrame.left
9992 + "," + w.mShownFrame.top + ")", e);
9993 if (!recoveringMemory) {
9994 reclaimSomeSurfaceMemoryLocked(w, "size");
9995 }
9996 }
9997 }
9998 }
9999 if (!w.mAppFreezing) {
10000 w.mContentInsetsChanged =
10001 !w.mLastContentInsets.equals(w.mContentInsets);
10002 w.mVisibleInsetsChanged =
10003 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -070010004 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005 || w.mContentInsetsChanged
10006 || w.mVisibleInsetsChanged) {
10007 w.mLastFrame.set(w.mFrame);
10008 w.mLastContentInsets.set(w.mContentInsets);
10009 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010010 // If the screen is currently frozen, then keep
10011 // it frozen until this window draws at its new
10012 // orientation.
10013 if (mDisplayFrozen) {
10014 if (DEBUG_ORIENTATION) Log.v(TAG,
10015 "Resizing while display frozen: " + w);
10016 w.mOrientationChanging = true;
10017 if (mWindowsFreezingScreen) {
10018 mWindowsFreezingScreen = true;
10019 // XXX should probably keep timeout from
10020 // when we first froze the display.
10021 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10022 mH.sendMessageDelayed(mH.obtainMessage(
10023 H.WINDOW_FREEZE_TIMEOUT), 2000);
10024 }
10025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 // If the orientation is changing, then we need to
10027 // hold off on unfreezing the display until this
10028 // window has been redrawn; to do that, we need
10029 // to go through the process of getting informed
10030 // by the application when it has finished drawing.
10031 if (w.mOrientationChanging) {
10032 if (DEBUG_ORIENTATION) Log.v(TAG,
10033 "Orientation start waiting for draw in "
10034 + w + ", surface " + w.mSurface);
10035 w.mDrawPending = true;
10036 w.mCommitDrawPending = false;
10037 w.mReadyToShow = false;
10038 if (w.mAppToken != null) {
10039 w.mAppToken.allDrawn = false;
10040 }
10041 }
Romain Guy06882f82009-06-10 13:36:04 -070010042 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010043 "Resizing window " + w + " to " + w.mFrame);
10044 mResizingWindows.add(w);
10045 } else if (w.mOrientationChanging) {
10046 if (!w.mDrawPending && !w.mCommitDrawPending) {
10047 if (DEBUG_ORIENTATION) Log.v(TAG,
10048 "Orientation not waiting for draw in "
10049 + w + ", surface " + w.mSurface);
10050 w.mOrientationChanging = false;
10051 }
10052 }
10053 }
10054
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010055 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010056 if (!w.mLastHidden) {
10057 //dump();
10058 w.mLastHidden = true;
10059 if (SHOW_TRANSACTIONS) Log.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010060 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010061 if (w.mSurface != null) {
10062 try {
10063 w.mSurface.hide();
10064 } catch (RuntimeException e) {
10065 Log.w(TAG, "Exception hiding surface in " + w);
10066 }
10067 }
10068 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10069 }
10070 // If we are waiting for this window to handle an
10071 // orientation change, well, it is hidden, so
10072 // doesn't really matter. Note that this does
10073 // introduce a potential glitch if the window
10074 // becomes unhidden before it has drawn for the
10075 // new orientation.
10076 if (w.mOrientationChanging) {
10077 w.mOrientationChanging = false;
10078 if (DEBUG_ORIENTATION) Log.v(TAG,
10079 "Orientation change skips hidden " + w);
10080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 } else if (w.mLastLayer != w.mAnimLayer
10082 || w.mLastAlpha != w.mShownAlpha
10083 || w.mLastDsDx != w.mDsDx
10084 || w.mLastDtDx != w.mDtDx
10085 || w.mLastDsDy != w.mDsDy
10086 || w.mLastDtDy != w.mDtDy
10087 || w.mLastHScale != w.mHScale
10088 || w.mLastVScale != w.mVScale
10089 || w.mLastHidden) {
10090 displayed = true;
10091 w.mLastAlpha = w.mShownAlpha;
10092 w.mLastLayer = w.mAnimLayer;
10093 w.mLastDsDx = w.mDsDx;
10094 w.mLastDtDx = w.mDtDx;
10095 w.mLastDsDy = w.mDsDy;
10096 w.mLastDtDy = w.mDtDy;
10097 w.mLastHScale = w.mHScale;
10098 w.mLastVScale = w.mVScale;
10099 if (SHOW_TRANSACTIONS) Log.i(
10100 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010101 + w.mShownAlpha + " layer=" + w.mAnimLayer
10102 + " matrix=[" + (w.mDsDx*w.mHScale)
10103 + "," + (w.mDtDx*w.mVScale)
10104 + "][" + (w.mDsDy*w.mHScale)
10105 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010106 if (w.mSurface != null) {
10107 try {
10108 w.mSurface.setAlpha(w.mShownAlpha);
10109 w.mSurface.setLayer(w.mAnimLayer);
10110 w.mSurface.setMatrix(
10111 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10112 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10113 } catch (RuntimeException e) {
10114 Log.w(TAG, "Error updating surface in " + w, e);
10115 if (!recoveringMemory) {
10116 reclaimSomeSurfaceMemoryLocked(w, "update");
10117 }
10118 }
10119 }
10120
10121 if (w.mLastHidden && !w.mDrawPending
10122 && !w.mCommitDrawPending
10123 && !w.mReadyToShow) {
10124 if (SHOW_TRANSACTIONS) Log.i(
10125 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
10126 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
10127 + " during relayout");
10128 if (showSurfaceRobustlyLocked(w)) {
10129 w.mHasDrawn = true;
10130 w.mLastHidden = false;
10131 } else {
10132 w.mOrientationChanging = false;
10133 }
10134 }
10135 if (w.mSurface != null) {
10136 w.mToken.hasVisible = true;
10137 }
10138 } else {
10139 displayed = true;
10140 }
10141
10142 if (displayed) {
10143 if (!covered) {
Romain Guy980a9382010-01-08 15:06:28 -080010144 if (attrs.width == LayoutParams.MATCH_PARENT
10145 && attrs.height == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 covered = true;
10147 }
10148 }
10149 if (w.mOrientationChanging) {
10150 if (w.mDrawPending || w.mCommitDrawPending) {
10151 orientationChangeComplete = false;
10152 if (DEBUG_ORIENTATION) Log.v(TAG,
10153 "Orientation continue waiting for draw in " + w);
10154 } else {
10155 w.mOrientationChanging = false;
10156 if (DEBUG_ORIENTATION) Log.v(TAG,
10157 "Orientation change complete in " + w);
10158 }
10159 }
10160 w.mToken.hasVisible = true;
10161 }
10162 } else if (w.mOrientationChanging) {
10163 if (DEBUG_ORIENTATION) Log.v(TAG,
10164 "Orientation change skips hidden " + w);
10165 w.mOrientationChanging = false;
10166 }
10167
10168 final boolean canBeSeen = w.isDisplayedLw();
10169
10170 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10171 focusDisplayed = true;
10172 }
10173
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010174 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010176 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010177 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010178 if (w.mSurface != null) {
10179 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10180 holdScreen = w.mSession;
10181 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010182 if (!syswin && w.mAttrs.screenBrightness >= 0
10183 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010184 screenBrightness = w.mAttrs.screenBrightness;
10185 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010186 if (!syswin && w.mAttrs.buttonBrightness >= 0
10187 && buttonBrightness < 0) {
10188 buttonBrightness = w.mAttrs.buttonBrightness;
10189 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010190 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10191 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10192 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10193 syswin = true;
10194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010196
Dianne Hackborn25994b42009-09-04 14:21:19 -070010197 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10198 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010199 // This window completely covers everything behind it,
10200 // so we want to leave all of them as unblurred (for
10201 // performance reasons).
10202 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010203 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
10204 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010205 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010206 obscured = true;
10207 if (mBackgroundFillerSurface == null) {
10208 try {
10209 mBackgroundFillerSurface = new Surface(mFxSession, 0,
10210 0, dw, dh,
10211 PixelFormat.OPAQUE,
10212 Surface.FX_SURFACE_NORMAL);
10213 } catch (Exception e) {
10214 Log.e(TAG, "Exception creating filler surface", e);
10215 }
10216 }
10217 try {
10218 mBackgroundFillerSurface.setPosition(0, 0);
10219 mBackgroundFillerSurface.setSize(dw, dh);
10220 // Using the same layer as Dim because they will never be shown at the
10221 // same time.
10222 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10223 mBackgroundFillerSurface.show();
10224 } catch (RuntimeException e) {
10225 Log.e(TAG, "Exception showing filler surface");
10226 }
10227 backgroundFillerShown = true;
10228 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010229 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010230 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
10231 if (localLOGV) Log.v(TAG, "Win " + w
10232 + ": blurring=" + blurring
10233 + " obscured=" + obscured
10234 + " displayed=" + displayed);
10235 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10236 if (!dimming) {
10237 //Log.i(TAG, "DIM BEHIND: " + w);
10238 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010239 if (mDimAnimator == null) {
10240 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010242 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010243 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010244 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010245 }
10246 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10247 if (!blurring) {
10248 //Log.i(TAG, "BLUR BEHIND: " + w);
10249 blurring = true;
10250 mBlurShown = true;
10251 if (mBlurSurface == null) {
10252 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10253 + mBlurSurface + ": CREATE");
10254 try {
Romain Guy06882f82009-06-10 13:36:04 -070010255 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010256 -1, 16, 16,
10257 PixelFormat.OPAQUE,
10258 Surface.FX_SURFACE_BLUR);
10259 } catch (Exception e) {
10260 Log.e(TAG, "Exception creating Blur surface", e);
10261 }
10262 }
10263 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
10264 + mBlurSurface + ": SHOW pos=(0,0) (" +
10265 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10266 if (mBlurSurface != null) {
10267 mBlurSurface.setPosition(0, 0);
10268 mBlurSurface.setSize(dw, dh);
10269 try {
10270 mBlurSurface.show();
10271 } catch (RuntimeException e) {
10272 Log.w(TAG, "Failure showing blur surface", e);
10273 }
10274 }
10275 }
10276 mBlurSurface.setLayer(w.mAnimLayer-2);
10277 }
10278 }
10279 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010280
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010281 if (obscuredChanged && mWallpaperTarget == w) {
10282 // This is the wallpaper target and its obscured state
10283 // changed... make sure the current wallaper's visibility
10284 // has been updated accordingly.
10285 updateWallpaperVisibilityLocked();
10286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010288
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010289 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10290 mBackgroundFillerShown = false;
10291 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
10292 try {
10293 mBackgroundFillerSurface.hide();
10294 } catch (RuntimeException e) {
10295 Log.e(TAG, "Exception hiding filler surface", e);
10296 }
10297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010299 if (mDimAnimator != null && mDimAnimator.mDimShown) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010300 animating |= mDimAnimator.updateSurface(dimming, currentTime,
10301 mDisplayFrozen || !mPolicy.isScreenOn());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 }
Romain Guy06882f82009-06-10 13:36:04 -070010303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010304 if (!blurring && mBlurShown) {
10305 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
10306 + ": HIDE");
10307 try {
10308 mBlurSurface.hide();
10309 } catch (IllegalArgumentException e) {
10310 Log.w(TAG, "Illegal argument exception hiding blur surface");
10311 }
10312 mBlurShown = false;
10313 }
10314
10315 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
10316 } catch (RuntimeException e) {
10317 Log.e(TAG, "Unhandled exception in Window Manager", e);
10318 }
10319
10320 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010322 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
10323 "With display frozen, orientationChangeComplete="
10324 + orientationChangeComplete);
10325 if (orientationChangeComplete) {
10326 if (mWindowsFreezingScreen) {
10327 mWindowsFreezingScreen = false;
10328 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10329 }
10330 if (mAppsFreezingScreen == 0) {
10331 stopFreezingDisplayLocked();
10332 }
10333 }
Romain Guy06882f82009-06-10 13:36:04 -070010334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 i = mResizingWindows.size();
10336 if (i > 0) {
10337 do {
10338 i--;
10339 WindowState win = mResizingWindows.get(i);
10340 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010341 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
10342 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010343 win.mClient.resized(win.mFrame.width(),
10344 win.mFrame.height(), win.mLastContentInsets,
10345 win.mLastVisibleInsets, win.mDrawPending);
10346 win.mContentInsetsChanged = false;
10347 win.mVisibleInsetsChanged = false;
10348 } catch (RemoteException e) {
10349 win.mOrientationChanging = false;
10350 }
10351 } while (i > 0);
10352 mResizingWindows.clear();
10353 }
Romain Guy06882f82009-06-10 13:36:04 -070010354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010355 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010356 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010357 i = mDestroySurface.size();
10358 if (i > 0) {
10359 do {
10360 i--;
10361 WindowState win = mDestroySurface.get(i);
10362 win.mDestroying = false;
10363 if (mInputMethodWindow == win) {
10364 mInputMethodWindow = null;
10365 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010366 if (win == mWallpaperTarget) {
10367 wallpaperDestroyed = true;
10368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010369 win.destroySurfaceLocked();
10370 } while (i > 0);
10371 mDestroySurface.clear();
10372 }
10373
10374 // Time to remove any exiting tokens?
10375 for (i=mExitingTokens.size()-1; i>=0; i--) {
10376 WindowToken token = mExitingTokens.get(i);
10377 if (!token.hasVisible) {
10378 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010379 if (token.windowType == TYPE_WALLPAPER) {
10380 mWallpaperTokens.remove(token);
10381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 }
10383 }
10384
10385 // Time to remove any exiting applications?
10386 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10387 AppWindowToken token = mExitingAppTokens.get(i);
10388 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010389 // Make sure there is no animation running on this token,
10390 // so any windows associated with it will be removed as
10391 // soon as their animations are complete
10392 token.animation = null;
10393 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 mAppTokens.remove(token);
10395 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010396 if (mLastEnterAnimToken == token) {
10397 mLastEnterAnimToken = null;
10398 mLastEnterAnimParams = null;
10399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010400 }
10401 }
10402
Dianne Hackborna8f60182009-09-01 19:01:50 -070010403 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010404
Dianne Hackborna8f60182009-09-01 19:01:50 -070010405 if (!animating && mAppTransitionRunning) {
10406 // We have finished the animation of an app transition. To do
10407 // this, we have delayed a lot of operations like showing and
10408 // hiding apps, moving apps in Z-order, etc. The app token list
10409 // reflects the correct Z-order, but the window list may now
10410 // be out of sync with it. So here we will just rebuild the
10411 // entire app window list. Fun!
10412 mAppTransitionRunning = false;
10413 needRelayout = true;
10414 rebuildAppWindowListLocked();
10415 // Clear information about apps that were moving.
10416 mToBottomApps.clear();
10417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010419 if (focusDisplayed) {
10420 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10421 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010422 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010423 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010424 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010425 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010426 requestAnimationLocked(0);
10427 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010428 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10429 }
10430 mQueue.setHoldScreenLocked(holdScreen != null);
10431 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10432 mPowerManager.setScreenBrightnessOverride(-1);
10433 } else {
10434 mPowerManager.setScreenBrightnessOverride((int)
10435 (screenBrightness * Power.BRIGHTNESS_ON));
10436 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010437 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10438 mPowerManager.setButtonBrightnessOverride(-1);
10439 } else {
10440 mPowerManager.setButtonBrightnessOverride((int)
10441 (buttonBrightness * Power.BRIGHTNESS_ON));
10442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010443 if (holdScreen != mHoldingScreenOn) {
10444 mHoldingScreenOn = holdScreen;
10445 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10446 mH.sendMessage(m);
10447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010448
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010449 if (mTurnOnScreen) {
10450 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10451 LocalPowerManager.BUTTON_EVENT, true);
10452 mTurnOnScreen = false;
10453 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010454
10455 // Check to see if we are now in a state where the screen should
10456 // be enabled, because the window obscured flags have changed.
10457 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010458 }
10459
10460 void requestAnimationLocked(long delay) {
10461 if (!mAnimationPending) {
10462 mAnimationPending = true;
10463 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10464 }
10465 }
Romain Guy06882f82009-06-10 13:36:04 -070010466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010467 /**
10468 * Have the surface flinger show a surface, robustly dealing with
10469 * error conditions. In particular, if there is not enough memory
10470 * to show the surface, then we will try to get rid of other surfaces
10471 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010472 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010473 * @return Returns true if the surface was successfully shown.
10474 */
10475 boolean showSurfaceRobustlyLocked(WindowState win) {
10476 try {
10477 if (win.mSurface != null) {
10478 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010479 if (win.mTurnOnScreen) {
10480 win.mTurnOnScreen = false;
10481 mTurnOnScreen = true;
10482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010483 }
10484 return true;
10485 } catch (RuntimeException e) {
10486 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
10487 }
Romain Guy06882f82009-06-10 13:36:04 -070010488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010489 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 return false;
10492 }
Romain Guy06882f82009-06-10 13:36:04 -070010493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010494 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10495 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010496
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010497 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010500 if (mForceRemoves == null) {
10501 mForceRemoves = new ArrayList<WindowState>();
10502 }
Romain Guy06882f82009-06-10 13:36:04 -070010503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010504 long callingIdentity = Binder.clearCallingIdentity();
10505 try {
10506 // There was some problem... first, do a sanity check of the
10507 // window list to make sure we haven't left any dangling surfaces
10508 // around.
10509 int N = mWindows.size();
10510 boolean leakedSurface = false;
10511 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
10512 for (int i=0; i<N; i++) {
10513 WindowState ws = (WindowState)mWindows.get(i);
10514 if (ws.mSurface != null) {
10515 if (!mSessions.contains(ws.mSession)) {
10516 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
10517 + ws + " surface=" + ws.mSurface
10518 + " token=" + win.mToken
10519 + " pid=" + ws.mSession.mPid
10520 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010521 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 ws.mSurface = null;
10523 mForceRemoves.add(ws);
10524 i--;
10525 N--;
10526 leakedSurface = true;
10527 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
10528 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
10529 + ws + " surface=" + ws.mSurface
10530 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010531 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010532 ws.mSurface = null;
10533 leakedSurface = true;
10534 }
10535 }
10536 }
Romain Guy06882f82009-06-10 13:36:04 -070010537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010538 boolean killedApps = false;
10539 if (!leakedSurface) {
10540 Log.w(TAG, "No leaked surfaces; killing applicatons!");
10541 SparseIntArray pidCandidates = new SparseIntArray();
10542 for (int i=0; i<N; i++) {
10543 WindowState ws = (WindowState)mWindows.get(i);
10544 if (ws.mSurface != null) {
10545 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10546 }
10547 }
10548 if (pidCandidates.size() > 0) {
10549 int[] pids = new int[pidCandidates.size()];
10550 for (int i=0; i<pids.length; i++) {
10551 pids[i] = pidCandidates.keyAt(i);
10552 }
10553 try {
10554 if (mActivityManager.killPidsForMemory(pids)) {
10555 killedApps = true;
10556 }
10557 } catch (RemoteException e) {
10558 }
10559 }
10560 }
Romain Guy06882f82009-06-10 13:36:04 -070010561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010562 if (leakedSurface || killedApps) {
10563 // We managed to reclaim some memory, so get rid of the trouble
10564 // surface and ask the app to request another one.
10565 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
10566 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010567 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010568 win.mSurface = null;
10569 }
Romain Guy06882f82009-06-10 13:36:04 -070010570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010571 try {
10572 win.mClient.dispatchGetNewSurface();
10573 } catch (RemoteException e) {
10574 }
10575 }
10576 } finally {
10577 Binder.restoreCallingIdentity(callingIdentity);
10578 }
10579 }
Romain Guy06882f82009-06-10 13:36:04 -070010580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010581 private boolean updateFocusedWindowLocked(int mode) {
10582 WindowState newFocus = computeFocusedWindowLocked();
10583 if (mCurrentFocus != newFocus) {
10584 // This check makes sure that we don't already have the focus
10585 // change message pending.
10586 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10587 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
10588 if (localLOGV) Log.v(
10589 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10590 final WindowState oldFocus = mCurrentFocus;
10591 mCurrentFocus = newFocus;
10592 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 final WindowState imWindow = mInputMethodWindow;
10595 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010596 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010597 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010598 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10599 mLayoutNeeded = true;
10600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010601 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10602 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010603 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10604 // Client will do the layout, but we need to assign layers
10605 // for handleNewWindowLocked() below.
10606 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 }
10608 }
Romain Guy06882f82009-06-10 13:36:04 -070010609
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010610 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10611 mKeyWaiter.handleNewWindowLocked(newFocus);
10612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010613 return true;
10614 }
10615 return false;
10616 }
10617
10618 private WindowState computeFocusedWindowLocked() {
10619 WindowState result = null;
10620 WindowState win;
10621
10622 int i = mWindows.size() - 1;
10623 int nextAppIndex = mAppTokens.size()-1;
10624 WindowToken nextApp = nextAppIndex >= 0
10625 ? mAppTokens.get(nextAppIndex) : null;
10626
10627 while (i >= 0) {
10628 win = (WindowState)mWindows.get(i);
10629
10630 if (localLOGV || DEBUG_FOCUS) Log.v(
10631 TAG, "Looking for focus: " + i
10632 + " = " + win
10633 + ", flags=" + win.mAttrs.flags
10634 + ", canReceive=" + win.canReceiveKeys());
10635
10636 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010638 // If this window's application has been removed, just skip it.
10639 if (thisApp != null && thisApp.removed) {
10640 i--;
10641 continue;
10642 }
Romain Guy06882f82009-06-10 13:36:04 -070010643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 // If there is a focused app, don't allow focus to go to any
10645 // windows below it. If this is an application window, step
10646 // through the app tokens until we find its app.
10647 if (thisApp != null && nextApp != null && thisApp != nextApp
10648 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10649 int origAppIndex = nextAppIndex;
10650 while (nextAppIndex > 0) {
10651 if (nextApp == mFocusedApp) {
10652 // Whoops, we are below the focused app... no focus
10653 // for you!
10654 if (localLOGV || DEBUG_FOCUS) Log.v(
10655 TAG, "Reached focused app: " + mFocusedApp);
10656 return null;
10657 }
10658 nextAppIndex--;
10659 nextApp = mAppTokens.get(nextAppIndex);
10660 if (nextApp == thisApp) {
10661 break;
10662 }
10663 }
10664 if (thisApp != nextApp) {
10665 // Uh oh, the app token doesn't exist! This shouldn't
10666 // happen, but if it does we can get totally hosed...
10667 // so restart at the original app.
10668 nextAppIndex = origAppIndex;
10669 nextApp = mAppTokens.get(nextAppIndex);
10670 }
10671 }
10672
10673 // Dispatch to this window if it is wants key events.
10674 if (win.canReceiveKeys()) {
10675 if (DEBUG_FOCUS) Log.v(
10676 TAG, "Found focus @ " + i + " = " + win);
10677 result = win;
10678 break;
10679 }
10680
10681 i--;
10682 }
10683
10684 return result;
10685 }
10686
10687 private void startFreezingDisplayLocked() {
10688 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010689 // Freezing the display also suspends key event delivery, to
10690 // keep events from going astray while the display is reconfigured.
10691 // If someone has changed orientation again while the screen is
10692 // still frozen, the events will continue to be blocked while the
10693 // successive orientation change is processed. To prevent spurious
10694 // ANRs, we reset the event dispatch timeout in this case.
10695 synchronized (mKeyWaiter) {
10696 mKeyWaiter.mWasFrozen = true;
10697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698 return;
10699 }
Romain Guy06882f82009-06-10 13:36:04 -070010700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010701 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010703 long now = SystemClock.uptimeMillis();
10704 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10705 if (mFreezeGcPending != 0) {
10706 if (now > (mFreezeGcPending+1000)) {
10707 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10708 mH.removeMessages(H.FORCE_GC);
10709 Runtime.getRuntime().gc();
10710 mFreezeGcPending = now;
10711 }
10712 } else {
10713 mFreezeGcPending = now;
10714 }
Romain Guy06882f82009-06-10 13:36:04 -070010715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010716 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010717 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10718 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010719 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010720 mAppTransitionReady = true;
10721 }
Romain Guy06882f82009-06-10 13:36:04 -070010722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010723 if (PROFILE_ORIENTATION) {
10724 File file = new File("/data/system/frozen");
10725 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10726 }
10727 Surface.freezeDisplay(0);
10728 }
Romain Guy06882f82009-06-10 13:36:04 -070010729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010730 private void stopFreezingDisplayLocked() {
10731 if (!mDisplayFrozen) {
10732 return;
10733 }
Romain Guy06882f82009-06-10 13:36:04 -070010734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 mDisplayFrozen = false;
10736 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10737 if (PROFILE_ORIENTATION) {
10738 Debug.stopMethodTracing();
10739 }
10740 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010741
Chris Tate2ad63a92009-03-25 17:36:48 -070010742 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10743 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010744 synchronized (mKeyWaiter) {
10745 mKeyWaiter.mWasFrozen = true;
10746 mKeyWaiter.notifyAll();
10747 }
10748
10749 // A little kludge: a lot could have happened while the
10750 // display was frozen, so now that we are coming back we
10751 // do a gc so that any remote references the system
10752 // processes holds on others can be released if they are
10753 // no longer needed.
10754 mH.removeMessages(H.FORCE_GC);
10755 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10756 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010758 mScreenFrozenLock.release();
10759 }
Romain Guy06882f82009-06-10 13:36:04 -070010760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 @Override
10762 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10763 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10764 != PackageManager.PERMISSION_GRANTED) {
10765 pw.println("Permission Denial: can't dump WindowManager from from pid="
10766 + Binder.getCallingPid()
10767 + ", uid=" + Binder.getCallingUid());
10768 return;
10769 }
Romain Guy06882f82009-06-10 13:36:04 -070010770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 synchronized(mWindowMap) {
10772 pw.println("Current Window Manager state:");
10773 for (int i=mWindows.size()-1; i>=0; i--) {
10774 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010775 pw.print(" Window #"); pw.print(i); pw.print(' ');
10776 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010777 w.dump(pw, " ");
10778 }
10779 if (mInputMethodDialogs.size() > 0) {
10780 pw.println(" ");
10781 pw.println(" Input method dialogs:");
10782 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10783 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010784 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010785 }
10786 }
10787 if (mPendingRemove.size() > 0) {
10788 pw.println(" ");
10789 pw.println(" Remove pending for:");
10790 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10791 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010792 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10793 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 w.dump(pw, " ");
10795 }
10796 }
10797 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10798 pw.println(" ");
10799 pw.println(" Windows force removing:");
10800 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10801 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010802 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10803 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010804 w.dump(pw, " ");
10805 }
10806 }
10807 if (mDestroySurface.size() > 0) {
10808 pw.println(" ");
10809 pw.println(" Windows waiting to destroy their surface:");
10810 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10811 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010812 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10813 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010814 w.dump(pw, " ");
10815 }
10816 }
10817 if (mLosingFocus.size() > 0) {
10818 pw.println(" ");
10819 pw.println(" Windows losing focus:");
10820 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10821 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010822 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10823 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010824 w.dump(pw, " ");
10825 }
10826 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010827 if (mResizingWindows.size() > 0) {
10828 pw.println(" ");
10829 pw.println(" Windows waiting to resize:");
10830 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10831 WindowState w = mResizingWindows.get(i);
10832 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10833 pw.print(w); pw.println(":");
10834 w.dump(pw, " ");
10835 }
10836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010837 if (mSessions.size() > 0) {
10838 pw.println(" ");
10839 pw.println(" All active sessions:");
10840 Iterator<Session> it = mSessions.iterator();
10841 while (it.hasNext()) {
10842 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010843 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010844 s.dump(pw, " ");
10845 }
10846 }
10847 if (mTokenMap.size() > 0) {
10848 pw.println(" ");
10849 pw.println(" All tokens:");
10850 Iterator<WindowToken> it = mTokenMap.values().iterator();
10851 while (it.hasNext()) {
10852 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010853 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010854 token.dump(pw, " ");
10855 }
10856 }
10857 if (mTokenList.size() > 0) {
10858 pw.println(" ");
10859 pw.println(" Window token list:");
10860 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010861 pw.print(" #"); pw.print(i); pw.print(": ");
10862 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010863 }
10864 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010865 if (mWallpaperTokens.size() > 0) {
10866 pw.println(" ");
10867 pw.println(" Wallpaper tokens:");
10868 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10869 WindowToken token = mWallpaperTokens.get(i);
10870 pw.print(" Wallpaper #"); pw.print(i);
10871 pw.print(' '); pw.print(token); pw.println(':');
10872 token.dump(pw, " ");
10873 }
10874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010875 if (mAppTokens.size() > 0) {
10876 pw.println(" ");
10877 pw.println(" Application tokens in Z order:");
10878 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010879 pw.print(" App #"); pw.print(i); pw.print(": ");
10880 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010881 }
10882 }
10883 if (mFinishedStarting.size() > 0) {
10884 pw.println(" ");
10885 pw.println(" Finishing start of application tokens:");
10886 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10887 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010888 pw.print(" Finished Starting #"); pw.print(i);
10889 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010890 token.dump(pw, " ");
10891 }
10892 }
10893 if (mExitingTokens.size() > 0) {
10894 pw.println(" ");
10895 pw.println(" Exiting tokens:");
10896 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10897 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010898 pw.print(" Exiting #"); pw.print(i);
10899 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010900 token.dump(pw, " ");
10901 }
10902 }
10903 if (mExitingAppTokens.size() > 0) {
10904 pw.println(" ");
10905 pw.println(" Exiting application tokens:");
10906 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10907 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010908 pw.print(" Exiting App #"); pw.print(i);
10909 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010910 token.dump(pw, " ");
10911 }
10912 }
10913 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010914 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10915 pw.print(" mLastFocus="); pw.println(mLastFocus);
10916 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10917 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10918 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010919 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010920 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10921 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10922 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10923 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010924 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10925 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10926 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10927 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10928 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010929 if (mDimAnimator != null) {
10930 mDimAnimator.printTo(pw);
10931 } else {
10932 pw.print( " no DimAnimator ");
10933 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010934 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010935 pw.print(mInputMethodAnimLayerAdjustment);
10936 pw.print(" mWallpaperAnimLayerAdjustment=");
10937 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010938 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10939 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010940 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10941 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10942 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10943 pw.print(" mRotation="); pw.print(mRotation);
10944 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10945 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10946 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10947 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10948 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10949 pw.print(" mNextAppTransition=0x");
10950 pw.print(Integer.toHexString(mNextAppTransition));
10951 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010952 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010953 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010954 if (mNextAppTransitionPackage != null) {
10955 pw.print(" mNextAppTransitionPackage=");
10956 pw.print(mNextAppTransitionPackage);
10957 pw.print(", mNextAppTransitionEnter=0x");
10958 pw.print(Integer.toHexString(mNextAppTransitionEnter));
10959 pw.print(", mNextAppTransitionExit=0x");
10960 pw.print(Integer.toHexString(mNextAppTransitionExit));
10961 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010962 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10963 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010964 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
10965 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
10966 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
10967 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010968 if (mOpeningApps.size() > 0) {
10969 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10970 }
10971 if (mClosingApps.size() > 0) {
10972 pw.print(" mClosingApps="); pw.println(mClosingApps);
10973 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010974 if (mToTopApps.size() > 0) {
10975 pw.print(" mToTopApps="); pw.println(mToTopApps);
10976 }
10977 if (mToBottomApps.size() > 0) {
10978 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10979 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010980 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10981 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010982 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010983 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10984 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10985 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10986 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10987 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10988 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010989 }
10990 }
10991
10992 public void monitor() {
10993 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050010994 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010995 synchronized (mKeyWaiter) { }
10996 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010997
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010998 public void virtualKeyFeedback(KeyEvent event) {
10999 mPolicy.keyFeedbackFromInput(event);
11000 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011001
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011002 /**
11003 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011004 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011005 */
11006 private static class DimAnimator {
11007 Surface mDimSurface;
11008 boolean mDimShown = false;
11009 float mDimCurrentAlpha;
11010 float mDimTargetAlpha;
11011 float mDimDeltaPerMs;
11012 long mLastDimAnimTime;
11013
11014 DimAnimator (SurfaceSession session) {
11015 if (mDimSurface == null) {
11016 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11017 + mDimSurface + ": CREATE");
11018 try {
11019 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
11020 Surface.FX_SURFACE_DIM);
11021 } catch (Exception e) {
11022 Log.e(TAG, "Exception creating Dim surface", e);
11023 }
11024 }
11025 }
11026
11027 /**
11028 * Show the dim surface.
11029 */
11030 void show(int dw, int dh) {
11031 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
11032 dw + "x" + dh + ")");
11033 mDimShown = true;
11034 try {
11035 mDimSurface.setPosition(0, 0);
11036 mDimSurface.setSize(dw, dh);
11037 mDimSurface.show();
11038 } catch (RuntimeException e) {
11039 Log.w(TAG, "Failure showing dim surface", e);
11040 }
11041 }
11042
11043 /**
11044 * Set's the dim surface's layer and update dim parameters that will be used in
11045 * {@link updateSurface} after all windows are examined.
11046 */
11047 void updateParameters(WindowState w, long currentTime) {
11048 mDimSurface.setLayer(w.mAnimLayer-1);
11049
11050 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011051 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11052 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011053 if (mDimTargetAlpha != target) {
11054 // If the desired dim level has changed, then
11055 // start an animation to it.
11056 mLastDimAnimTime = currentTime;
11057 long duration = (w.mAnimating && w.mAnimation != null)
11058 ? w.mAnimation.computeDurationHint()
11059 : DEFAULT_DIM_DURATION;
11060 if (target > mDimTargetAlpha) {
11061 // This is happening behind the activity UI,
11062 // so we can make it run a little longer to
11063 // give a stronger impression without disrupting
11064 // the user.
11065 duration *= DIM_DURATION_MULTIPLIER;
11066 }
11067 if (duration < 1) {
11068 // Don't divide by zero
11069 duration = 1;
11070 }
11071 mDimTargetAlpha = target;
11072 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11073 }
11074 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011075
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011076 /**
11077 * Updating the surface's alpha. Returns true if the animation continues, or returns
11078 * false when the animation is finished and the dim surface is hidden.
11079 */
11080 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11081 if (!dimming) {
11082 if (mDimTargetAlpha != 0) {
11083 mLastDimAnimTime = currentTime;
11084 mDimTargetAlpha = 0;
11085 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11086 }
11087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011088
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011089 boolean animating = false;
11090 if (mLastDimAnimTime != 0) {
11091 mDimCurrentAlpha += mDimDeltaPerMs
11092 * (currentTime-mLastDimAnimTime);
11093 boolean more = true;
11094 if (displayFrozen) {
11095 // If the display is frozen, there is no reason to animate.
11096 more = false;
11097 } else if (mDimDeltaPerMs > 0) {
11098 if (mDimCurrentAlpha > mDimTargetAlpha) {
11099 more = false;
11100 }
11101 } else if (mDimDeltaPerMs < 0) {
11102 if (mDimCurrentAlpha < mDimTargetAlpha) {
11103 more = false;
11104 }
11105 } else {
11106 more = false;
11107 }
11108
11109 // Do we need to continue animating?
11110 if (more) {
11111 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11112 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11113 mLastDimAnimTime = currentTime;
11114 mDimSurface.setAlpha(mDimCurrentAlpha);
11115 animating = true;
11116 } else {
11117 mDimCurrentAlpha = mDimTargetAlpha;
11118 mLastDimAnimTime = 0;
11119 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
11120 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11121 mDimSurface.setAlpha(mDimCurrentAlpha);
11122 if (!dimming) {
11123 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
11124 + ": HIDE");
11125 try {
11126 mDimSurface.hide();
11127 } catch (RuntimeException e) {
11128 Log.w(TAG, "Illegal argument exception hiding dim surface");
11129 }
11130 mDimShown = false;
11131 }
11132 }
11133 }
11134 return animating;
11135 }
11136
11137 public void printTo(PrintWriter pw) {
11138 pw.print(" mDimShown="); pw.print(mDimShown);
11139 pw.print(" current="); pw.print(mDimCurrentAlpha);
11140 pw.print(" target="); pw.print(mDimTargetAlpha);
11141 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11142 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11143 }
11144 }
11145
11146 /**
11147 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11148 * This is used for opening/closing transition for apps in compatible mode.
11149 */
11150 private static class FadeInOutAnimation extends Animation {
11151 int mWidth;
11152 boolean mFadeIn;
11153
11154 public FadeInOutAnimation(boolean fadeIn) {
11155 setInterpolator(new AccelerateInterpolator());
11156 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11157 mFadeIn = fadeIn;
11158 }
11159
11160 @Override
11161 protected void applyTransformation(float interpolatedTime, Transformation t) {
11162 float x = interpolatedTime;
11163 if (!mFadeIn) {
11164 x = 1.0f - x; // reverse the interpolation for fade out
11165 }
11166 if (x < 0.5) {
11167 // move the window out of the screen.
11168 t.getMatrix().setTranslate(mWidth, 0);
11169 } else {
11170 t.getMatrix().setTranslate(0, 0);// show
11171 t.setAlpha((x - 0.5f) * 2);
11172 }
11173 }
11174
11175 @Override
11176 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11177 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11178 mWidth = width;
11179 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011180
11181 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011182 public int getZAdjustment() {
11183 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011184 }
11185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011186}