blob: a00a756f2cd1859d92adc1de538e84e45155cb41 [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;
66import android.os.Debug;
67import android.os.Handler;
68import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070069import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.LocalPowerManager;
71import android.os.Looper;
72import android.os.Message;
73import android.os.Parcel;
74import android.os.ParcelFileDescriptor;
75import android.os.Power;
76import android.os.PowerManager;
77import android.os.Process;
78import android.os.RemoteException;
79import android.os.ServiceManager;
80import android.os.SystemClock;
81import android.os.SystemProperties;
82import android.os.TokenWatcher;
83import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070084import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.util.EventLog;
86import android.util.Log;
87import android.util.SparseIntArray;
88import android.view.Display;
89import android.view.Gravity;
90import android.view.IApplicationToken;
91import android.view.IOnKeyguardExitResult;
92import android.view.IRotationWatcher;
93import android.view.IWindow;
94import android.view.IWindowManager;
95import android.view.IWindowSession;
96import android.view.KeyEvent;
97import android.view.MotionEvent;
98import android.view.RawInputEvent;
99import android.view.Surface;
100import android.view.SurfaceSession;
101import android.view.View;
102import android.view.ViewTreeObserver;
103import android.view.WindowManager;
104import android.view.WindowManagerImpl;
105import android.view.WindowManagerPolicy;
106import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700107import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.animation.Animation;
109import android.view.animation.AnimationUtils;
110import android.view.animation.Transformation;
111
112import java.io.BufferedWriter;
113import java.io.File;
114import java.io.FileDescriptor;
115import java.io.IOException;
116import java.io.OutputStream;
117import java.io.OutputStreamWriter;
118import java.io.PrintWriter;
119import java.io.StringWriter;
120import java.net.Socket;
121import java.util.ArrayList;
122import java.util.HashMap;
123import java.util.HashSet;
124import java.util.Iterator;
125import java.util.List;
126
127/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700128public class WindowManagerService extends IWindowManager.Stub
129 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 static final String TAG = "WindowManager";
131 static final boolean DEBUG = false;
132 static final boolean DEBUG_FOCUS = false;
133 static final boolean DEBUG_ANIM = false;
134 static final boolean DEBUG_LAYERS = false;
135 static final boolean DEBUG_INPUT = false;
136 static final boolean DEBUG_INPUT_METHOD = false;
137 static final boolean DEBUG_VISIBILITY = false;
138 static final boolean DEBUG_ORIENTATION = false;
139 static final boolean DEBUG_APP_TRANSITIONS = false;
140 static final boolean DEBUG_STARTING_WINDOW = false;
141 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700142 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700144 static final boolean MEASURE_LATENCY = false;
145 static private LatencyTimer lt;
146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final boolean PROFILE_ORIENTATION = false;
148 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700149 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /** How long to wait for first key repeat, in milliseconds */
154 static final int KEY_REPEAT_FIRST_DELAY = 750;
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;
195
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.
226 */
227 private boolean mWaitingUntilKeyguardReenabled = false;
228
229
230 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
231 new Handler(), "WindowManagerService.mKeyguardDisabled") {
232 public void acquired() {
233 mPolicy.enableKeyguard(false);
234 }
235 public void released() {
236 synchronized (mKeyguardDisabled) {
237 mPolicy.enableKeyguard(true);
238 mWaitingUntilKeyguardReenabled = false;
239 mKeyguardDisabled.notifyAll();
240 }
241 }
242 };
243
244 final Context mContext;
245
246 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
251
252 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * All currently active sessions with clients.
258 */
259 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
262 * Mapping from an IWindow IBinder to the server's Window object.
263 * This is also used as the lock for all of our state.
264 */
265 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
266
267 /**
268 * Mapping from a token IBinder to a WindowToken object.
269 */
270 final HashMap<IBinder, WindowToken> mTokenMap =
271 new HashMap<IBinder, WindowToken>();
272
273 /**
274 * The same tokens as mTokenMap, stored in a list for efficient iteration
275 * over them.
276 */
277 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 /**
280 * Window tokens that are in the process of exiting, but still
281 * on screen for animations.
282 */
283 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
284
285 /**
286 * Z-ordered (bottom-most first) list of all application tokens, for
287 * controlling the ordering of windows in different applications. This
288 * contains WindowToken objects.
289 */
290 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
291
292 /**
293 * Application tokens that are in the process of exiting, but still
294 * on screen for animations.
295 */
296 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
297
298 /**
299 * List of window tokens that have finished starting their application,
300 * and now need to have the policy remove their windows.
301 */
302 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
303
304 /**
305 * Z-ordered (bottom-most first) list of all Window objects.
306 */
307 final ArrayList mWindows = new ArrayList();
308
309 /**
310 * Windows that are being resized. Used so we can tell the client about
311 * the resize after closing the transaction in which we resized the
312 * underlying surface.
313 */
314 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
315
316 /**
317 * Windows whose animations have ended and now must be removed.
318 */
319 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
320
321 /**
322 * Windows whose surface should be destroyed.
323 */
324 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
325
326 /**
327 * Windows that have lost input focus and are waiting for the new
328 * focus window to be displayed before they are told about this.
329 */
330 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
331
332 /**
333 * This is set when we have run out of memory, and will either be an empty
334 * list or contain windows that need to be force removed.
335 */
336 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700341 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 Surface mBlurSurface;
343 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 final float[] mTmpFloats = new float[9];
348
349 boolean mSafeMode;
350 boolean mDisplayEnabled = false;
351 boolean mSystemBooted = false;
352 int mRotation = 0;
353 int mRequestedRotation = 0;
354 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700355 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 ArrayList<IRotationWatcher> mRotationWatchers
357 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 boolean mLayoutNeeded = true;
360 boolean mAnimationPending = false;
361 boolean mDisplayFrozen = false;
362 boolean mWindowsFreezingScreen = false;
363 long mFreezeGcPending = 0;
364 int mAppsFreezingScreen = 0;
365
366 // This is held as long as we have the screen frozen, to give us time to
367 // perform a rotation animation when turning off shows the lock screen which
368 // changes the orientation.
369 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 // State management of app transitions. When we are preparing for a
372 // transition, mNextAppTransition will be the kind of transition to
373 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
374 // mOpeningApps and mClosingApps are the lists of tokens that will be
375 // made visible or hidden at the next transition.
376 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
377 boolean mAppTransitionReady = false;
378 boolean mAppTransitionTimeout = false;
379 boolean mStartingIconInTransition = false;
380 boolean mSkipAppTransitionAnimation = false;
381 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
382 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 //flag to detect fat touch events
385 boolean mFatTouch = false;
386 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 H mH = new H();
389
390 WindowState mCurrentFocus = null;
391 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 // This just indicates the window the input method is on top of, not
394 // necessarily the window its input is going to.
395 WindowState mInputMethodTarget = null;
396 WindowState mUpcomingInputMethodTarget = null;
397 boolean mInputMethodTargetWaitingAnim;
398 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 WindowState mInputMethodWindow = null;
401 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
402
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700403 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
404
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700405 // If non-null, this is the currently visible window that is associated
406 // with the wallpaper.
407 WindowState mWallpaperTarget = null;
408 int mWallpaperAnimLayerAdjustment;
409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 AppWindowToken mFocusedApp = null;
411
412 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 float mWindowAnimationScale = 1.0f;
415 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 final KeyWaiter mKeyWaiter = new KeyWaiter();
418 final KeyQ mQueue;
419 final InputDispatcherThread mInputThread;
420
421 // Who is holding the screen on.
422 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 /**
425 * Whether the UI is currently running in touch mode (not showing
426 * navigational focus because the user is directly pressing the screen).
427 */
428 boolean mInTouchMode = false;
429
430 private ViewServer mViewServer;
431
432 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700433
Dianne Hackbornc485a602009-03-24 22:39:49 -0700434 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700435 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700436
437 // The frame use to limit the size of the app running in compatibility mode.
438 Rect mCompatibleScreenFrame = new Rect();
439 // The surface used to fill the outer rim of the app running in compatibility mode.
440 Surface mBackgroundFillerSurface = null;
441 boolean mBackgroundFillerShown = false;
442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 public static WindowManagerService main(Context context,
444 PowerManagerService pm, boolean haveInputMethods) {
445 WMThread thr = new WMThread(context, pm, haveInputMethods);
446 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 synchronized (thr) {
449 while (thr.mService == null) {
450 try {
451 thr.wait();
452 } catch (InterruptedException e) {
453 }
454 }
455 }
Romain Guy06882f82009-06-10 13:36:04 -0700456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 return thr.mService;
458 }
Romain Guy06882f82009-06-10 13:36:04 -0700459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 static class WMThread extends Thread {
461 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 private final Context mContext;
464 private final PowerManagerService mPM;
465 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 public WMThread(Context context, PowerManagerService pm,
468 boolean haveInputMethods) {
469 super("WindowManager");
470 mContext = context;
471 mPM = pm;
472 mHaveInputMethods = haveInputMethods;
473 }
Romain Guy06882f82009-06-10 13:36:04 -0700474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 public void run() {
476 Looper.prepare();
477 WindowManagerService s = new WindowManagerService(mContext, mPM,
478 mHaveInputMethods);
479 android.os.Process.setThreadPriority(
480 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 synchronized (this) {
483 mService = s;
484 notifyAll();
485 }
Romain Guy06882f82009-06-10 13:36:04 -0700486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 Looper.loop();
488 }
489 }
490
491 static class PolicyThread extends Thread {
492 private final WindowManagerPolicy mPolicy;
493 private final WindowManagerService mService;
494 private final Context mContext;
495 private final PowerManagerService mPM;
496 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 public PolicyThread(WindowManagerPolicy policy,
499 WindowManagerService service, Context context,
500 PowerManagerService pm) {
501 super("WindowManagerPolicy");
502 mPolicy = policy;
503 mService = service;
504 mContext = context;
505 mPM = pm;
506 }
Romain Guy06882f82009-06-10 13:36:04 -0700507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 public void run() {
509 Looper.prepare();
510 //Looper.myLooper().setMessageLogging(new LogPrinter(
511 // Log.VERBOSE, "WindowManagerPolicy"));
512 android.os.Process.setThreadPriority(
513 android.os.Process.THREAD_PRIORITY_FOREGROUND);
514 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 synchronized (this) {
517 mRunning = true;
518 notifyAll();
519 }
Romain Guy06882f82009-06-10 13:36:04 -0700520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 Looper.loop();
522 }
523 }
524
525 private WindowManagerService(Context context, PowerManagerService pm,
526 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700527 if (MEASURE_LATENCY) {
528 lt = new LatencyTimer(100, 1000);
529 }
530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 mContext = context;
532 mHaveInputMethods = haveInputMethods;
533 mLimitedAlphaCompositing = context.getResources().getBoolean(
534 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 mPowerManager = pm;
537 mPowerManager.setPolicy(mPolicy);
538 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
539 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
540 "SCREEN_FROZEN");
541 mScreenFrozenLock.setReferenceCounted(false);
542
543 mActivityManager = ActivityManagerNative.getDefault();
544 mBatteryStats = BatteryStatsService.getService();
545
546 // Get persisted window scale setting
547 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
548 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
549 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
550 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700551
Michael Chan9f028e62009-08-04 17:37:46 -0700552 int max_events_per_sec = 35;
553 try {
554 max_events_per_sec = Integer.parseInt(SystemProperties
555 .get("windowsmgr.max_events_per_sec"));
556 if (max_events_per_sec < 1) {
557 max_events_per_sec = 35;
558 }
559 } catch (NumberFormatException e) {
560 }
561 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 mQueue = new KeyQ();
564
565 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
568 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 synchronized (thr) {
571 while (!thr.mRunning) {
572 try {
573 thr.wait();
574 } catch (InterruptedException e) {
575 }
576 }
577 }
Romain Guy06882f82009-06-10 13:36:04 -0700578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 // Add ourself to the Watchdog monitors.
582 Watchdog.getInstance().addMonitor(this);
583 }
584
585 @Override
586 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
587 throws RemoteException {
588 try {
589 return super.onTransact(code, data, reply, flags);
590 } catch (RuntimeException e) {
591 // The window manager only throws security exceptions, so let's
592 // log all others.
593 if (!(e instanceof SecurityException)) {
594 Log.e(TAG, "Window Manager Crash", e);
595 }
596 throw e;
597 }
598 }
599
600 private void placeWindowAfter(Object pos, WindowState window) {
601 final int i = mWindows.indexOf(pos);
602 if (localLOGV || DEBUG_FOCUS) Log.v(
603 TAG, "Adding window " + window + " at "
604 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
605 mWindows.add(i+1, window);
606 }
607
608 private void placeWindowBefore(Object pos, WindowState window) {
609 final int i = mWindows.indexOf(pos);
610 if (localLOGV || DEBUG_FOCUS) Log.v(
611 TAG, "Adding window " + window + " at "
612 + i + " of " + mWindows.size() + " (before " + pos + ")");
613 mWindows.add(i, window);
614 }
615
616 //This method finds out the index of a window that has the same app token as
617 //win. used for z ordering the windows in mWindows
618 private int findIdxBasedOnAppTokens(WindowState win) {
619 //use a local variable to cache mWindows
620 ArrayList localmWindows = mWindows;
621 int jmax = localmWindows.size();
622 if(jmax == 0) {
623 return -1;
624 }
625 for(int j = (jmax-1); j >= 0; j--) {
626 WindowState wentry = (WindowState)localmWindows.get(j);
627 if(wentry.mAppToken == win.mAppToken) {
628 return j;
629 }
630 }
631 return -1;
632 }
Romain Guy06882f82009-06-10 13:36:04 -0700633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
635 final IWindow client = win.mClient;
636 final WindowToken token = win.mToken;
637 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 final int N = localmWindows.size();
640 final WindowState attached = win.mAttachedWindow;
641 int i;
642 if (attached == null) {
643 int tokenWindowsPos = token.windows.size();
644 if (token.appWindowToken != null) {
645 int index = tokenWindowsPos-1;
646 if (index >= 0) {
647 // If this application has existing windows, we
648 // simply place the new window on top of them... but
649 // keep the starting window on top.
650 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
651 // Base windows go behind everything else.
652 placeWindowBefore(token.windows.get(0), win);
653 tokenWindowsPos = 0;
654 } else {
655 AppWindowToken atoken = win.mAppToken;
656 if (atoken != null &&
657 token.windows.get(index) == atoken.startingWindow) {
658 placeWindowBefore(token.windows.get(index), win);
659 tokenWindowsPos--;
660 } else {
661 int newIdx = findIdxBasedOnAppTokens(win);
662 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700663 //there is a window above this one associated with the same
664 //apptoken note that the window could be a floating window
665 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 //windows associated with this token.
667 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 }
670 }
671 } else {
672 if (localLOGV) Log.v(
673 TAG, "Figuring out where to add app window "
674 + client.asBinder() + " (token=" + token + ")");
675 // Figure out where the window should go, based on the
676 // order of applications.
677 final int NA = mAppTokens.size();
678 Object pos = null;
679 for (i=NA-1; i>=0; i--) {
680 AppWindowToken t = mAppTokens.get(i);
681 if (t == token) {
682 i--;
683 break;
684 }
685 if (t.windows.size() > 0) {
686 pos = t.windows.get(0);
687 }
688 }
689 // We now know the index into the apps. If we found
690 // an app window above, that gives us the position; else
691 // we need to look some more.
692 if (pos != null) {
693 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700694 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 mTokenMap.get(((WindowState)pos).mClient.asBinder());
696 if (atoken != null) {
697 final int NC = atoken.windows.size();
698 if (NC > 0) {
699 WindowState bottom = atoken.windows.get(0);
700 if (bottom.mSubLayer < 0) {
701 pos = bottom;
702 }
703 }
704 }
705 placeWindowBefore(pos, win);
706 } else {
707 while (i >= 0) {
708 AppWindowToken t = mAppTokens.get(i);
709 final int NW = t.windows.size();
710 if (NW > 0) {
711 pos = t.windows.get(NW-1);
712 break;
713 }
714 i--;
715 }
716 if (pos != null) {
717 // Move in front of any windows attached to this
718 // one.
719 WindowToken atoken =
720 mTokenMap.get(((WindowState)pos).mClient.asBinder());
721 if (atoken != null) {
722 final int NC = atoken.windows.size();
723 if (NC > 0) {
724 WindowState top = atoken.windows.get(NC-1);
725 if (top.mSubLayer >= 0) {
726 pos = top;
727 }
728 }
729 }
730 placeWindowAfter(pos, win);
731 } else {
732 // Just search for the start of this layer.
733 final int myLayer = win.mBaseLayer;
734 for (i=0; i<N; i++) {
735 WindowState w = (WindowState)localmWindows.get(i);
736 if (w.mBaseLayer > myLayer) {
737 break;
738 }
739 }
740 if (localLOGV || DEBUG_FOCUS) Log.v(
741 TAG, "Adding window " + win + " at "
742 + i + " of " + N);
743 localmWindows.add(i, win);
744 }
745 }
746 }
747 } else {
748 // Figure out where window should go, based on layer.
749 final int myLayer = win.mBaseLayer;
750 for (i=N-1; i>=0; i--) {
751 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
752 i++;
753 break;
754 }
755 }
756 if (i < 0) i = 0;
757 if (localLOGV || DEBUG_FOCUS) Log.v(
758 TAG, "Adding window " + win + " at "
759 + i + " of " + N);
760 localmWindows.add(i, win);
761 }
762 if (addToToken) {
763 token.windows.add(tokenWindowsPos, win);
764 }
765
766 } else {
767 // Figure out this window's ordering relative to the window
768 // it is attached to.
769 final int NA = token.windows.size();
770 final int sublayer = win.mSubLayer;
771 int largestSublayer = Integer.MIN_VALUE;
772 WindowState windowWithLargestSublayer = null;
773 for (i=0; i<NA; i++) {
774 WindowState w = token.windows.get(i);
775 final int wSublayer = w.mSubLayer;
776 if (wSublayer >= largestSublayer) {
777 largestSublayer = wSublayer;
778 windowWithLargestSublayer = w;
779 }
780 if (sublayer < 0) {
781 // For negative sublayers, we go below all windows
782 // in the same sublayer.
783 if (wSublayer >= sublayer) {
784 if (addToToken) {
785 token.windows.add(i, win);
786 }
787 placeWindowBefore(
788 wSublayer >= 0 ? attached : w, win);
789 break;
790 }
791 } else {
792 // For positive sublayers, we go above all windows
793 // in the same sublayer.
794 if (wSublayer > sublayer) {
795 if (addToToken) {
796 token.windows.add(i, win);
797 }
798 placeWindowBefore(w, win);
799 break;
800 }
801 }
802 }
803 if (i >= NA) {
804 if (addToToken) {
805 token.windows.add(win);
806 }
807 if (sublayer < 0) {
808 placeWindowBefore(attached, win);
809 } else {
810 placeWindowAfter(largestSublayer >= 0
811 ? windowWithLargestSublayer
812 : attached,
813 win);
814 }
815 }
816 }
Romain Guy06882f82009-06-10 13:36:04 -0700817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 if (win.mAppToken != null && addToToken) {
819 win.mAppToken.allAppWindows.add(win);
820 }
821 }
Romain Guy06882f82009-06-10 13:36:04 -0700822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 static boolean canBeImeTarget(WindowState w) {
824 final int fl = w.mAttrs.flags
825 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
826 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
827 return w.isVisibleOrAdding();
828 }
829 return false;
830 }
Romain Guy06882f82009-06-10 13:36:04 -0700831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
833 final ArrayList localmWindows = mWindows;
834 final int N = localmWindows.size();
835 WindowState w = null;
836 int i = N;
837 while (i > 0) {
838 i--;
839 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
842 // + Integer.toHexString(w.mAttrs.flags));
843 if (canBeImeTarget(w)) {
844 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 // Yet more tricksyness! If this window is a "starting"
847 // window, we do actually want to be on top of it, but
848 // it is not -really- where input will go. So if the caller
849 // is not actually looking to move the IME, look down below
850 // for a real window to target...
851 if (!willMove
852 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
853 && i > 0) {
854 WindowState wb = (WindowState)localmWindows.get(i-1);
855 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
856 i--;
857 w = wb;
858 }
859 }
860 break;
861 }
862 }
Romain Guy06882f82009-06-10 13:36:04 -0700863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
867 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 if (willMove && w != null) {
870 final WindowState curTarget = mInputMethodTarget;
871 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 // Now some fun for dealing with window animations that
874 // modify the Z order. We need to look at all windows below
875 // the current target that are in this app, finding the highest
876 // visible one in layering.
877 AppWindowToken token = curTarget.mAppToken;
878 WindowState highestTarget = null;
879 int highestPos = 0;
880 if (token.animating || token.animation != null) {
881 int pos = 0;
882 pos = localmWindows.indexOf(curTarget);
883 while (pos >= 0) {
884 WindowState win = (WindowState)localmWindows.get(pos);
885 if (win.mAppToken != token) {
886 break;
887 }
888 if (!win.mRemoved) {
889 if (highestTarget == null || win.mAnimLayer >
890 highestTarget.mAnimLayer) {
891 highestTarget = win;
892 highestPos = pos;
893 }
894 }
895 pos--;
896 }
897 }
Romain Guy06882f82009-06-10 13:36:04 -0700898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700900 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 + mNextAppTransition + " " + highestTarget
902 + " animating=" + highestTarget.isAnimating()
903 + " layer=" + highestTarget.mAnimLayer
904 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
907 // If we are currently setting up for an animation,
908 // hold everything until we can find out what will happen.
909 mInputMethodTargetWaitingAnim = true;
910 mInputMethodTarget = highestTarget;
911 return highestPos + 1;
912 } else if (highestTarget.isAnimating() &&
913 highestTarget.mAnimLayer > w.mAnimLayer) {
914 // If the window we are currently targeting is involved
915 // with an animation, and it is on top of the next target
916 // we will be over, then hold off on moving until
917 // that is done.
918 mInputMethodTarget = highestTarget;
919 return highestPos + 1;
920 }
921 }
922 }
923 }
Romain Guy06882f82009-06-10 13:36:04 -0700924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 //Log.i(TAG, "Placing input method @" + (i+1));
926 if (w != null) {
927 if (willMove) {
928 RuntimeException e = new RuntimeException();
929 e.fillInStackTrace();
930 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
931 + mInputMethodTarget + " to " + w, e);
932 mInputMethodTarget = w;
933 if (w.mAppToken != null) {
934 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
935 } else {
936 setInputMethodAnimLayerAdjustment(0);
937 }
938 }
939 return i+1;
940 }
941 if (willMove) {
942 RuntimeException e = new RuntimeException();
943 e.fillInStackTrace();
944 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
945 + mInputMethodTarget + " to null", e);
946 mInputMethodTarget = null;
947 setInputMethodAnimLayerAdjustment(0);
948 }
949 return -1;
950 }
Romain Guy06882f82009-06-10 13:36:04 -0700951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 void addInputMethodWindowToListLocked(WindowState win) {
953 int pos = findDesiredInputMethodWindowIndexLocked(true);
954 if (pos >= 0) {
955 win.mTargetAppToken = mInputMethodTarget.mAppToken;
956 mWindows.add(pos, win);
957 moveInputMethodDialogsLocked(pos+1);
958 return;
959 }
960 win.mTargetAppToken = null;
961 addWindowToListInOrderLocked(win, true);
962 moveInputMethodDialogsLocked(pos);
963 }
Romain Guy06882f82009-06-10 13:36:04 -0700964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 void setInputMethodAnimLayerAdjustment(int adj) {
966 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
967 mInputMethodAnimLayerAdjustment = adj;
968 WindowState imw = mInputMethodWindow;
969 if (imw != null) {
970 imw.mAnimLayer = imw.mLayer + adj;
971 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
972 + " anim layer: " + imw.mAnimLayer);
973 int wi = imw.mChildWindows.size();
974 while (wi > 0) {
975 wi--;
976 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
977 cw.mAnimLayer = cw.mLayer + adj;
978 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
979 + " anim layer: " + cw.mAnimLayer);
980 }
981 }
982 int di = mInputMethodDialogs.size();
983 while (di > 0) {
984 di --;
985 imw = mInputMethodDialogs.get(di);
986 imw.mAnimLayer = imw.mLayer + adj;
987 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
988 + " anim layer: " + imw.mAnimLayer);
989 }
990 }
Romain Guy06882f82009-06-10 13:36:04 -0700991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
993 int wpos = mWindows.indexOf(win);
994 if (wpos >= 0) {
995 if (wpos < interestingPos) interestingPos--;
996 mWindows.remove(wpos);
997 int NC = win.mChildWindows.size();
998 while (NC > 0) {
999 NC--;
1000 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1001 int cpos = mWindows.indexOf(cw);
1002 if (cpos >= 0) {
1003 if (cpos < interestingPos) interestingPos--;
1004 mWindows.remove(cpos);
1005 }
1006 }
1007 }
1008 return interestingPos;
1009 }
Romain Guy06882f82009-06-10 13:36:04 -07001010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 private void reAddWindowToListInOrderLocked(WindowState win) {
1012 addWindowToListInOrderLocked(win, false);
1013 // This is a hack to get all of the child windows added as well
1014 // at the right position. Child windows should be rare and
1015 // this case should be rare, so it shouldn't be that big a deal.
1016 int wpos = mWindows.indexOf(win);
1017 if (wpos >= 0) {
1018 mWindows.remove(wpos);
1019 reAddWindowLocked(wpos, win);
1020 }
1021 }
Romain Guy06882f82009-06-10 13:36:04 -07001022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 void logWindowList(String prefix) {
1024 int N = mWindows.size();
1025 while (N > 0) {
1026 N--;
1027 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1028 }
1029 }
Romain Guy06882f82009-06-10 13:36:04 -07001030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 void moveInputMethodDialogsLocked(int pos) {
1032 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 final int N = dialogs.size();
1035 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1036 for (int i=0; i<N; i++) {
1037 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1038 }
1039 if (DEBUG_INPUT_METHOD) {
1040 Log.v(TAG, "Window list w/pos=" + pos);
1041 logWindowList(" ");
1042 }
Romain Guy06882f82009-06-10 13:36:04 -07001043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 if (pos >= 0) {
1045 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1046 if (pos < mWindows.size()) {
1047 WindowState wp = (WindowState)mWindows.get(pos);
1048 if (wp == mInputMethodWindow) {
1049 pos++;
1050 }
1051 }
1052 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1053 for (int i=0; i<N; i++) {
1054 WindowState win = dialogs.get(i);
1055 win.mTargetAppToken = targetAppToken;
1056 pos = reAddWindowLocked(pos, win);
1057 }
1058 if (DEBUG_INPUT_METHOD) {
1059 Log.v(TAG, "Final window list:");
1060 logWindowList(" ");
1061 }
1062 return;
1063 }
1064 for (int i=0; i<N; i++) {
1065 WindowState win = dialogs.get(i);
1066 win.mTargetAppToken = null;
1067 reAddWindowToListInOrderLocked(win);
1068 if (DEBUG_INPUT_METHOD) {
1069 Log.v(TAG, "No IM target, final list:");
1070 logWindowList(" ");
1071 }
1072 }
1073 }
Romain Guy06882f82009-06-10 13:36:04 -07001074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1076 final WindowState imWin = mInputMethodWindow;
1077 final int DN = mInputMethodDialogs.size();
1078 if (imWin == null && DN == 0) {
1079 return false;
1080 }
Romain Guy06882f82009-06-10 13:36:04 -07001081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1083 if (imPos >= 0) {
1084 // In this case, the input method windows are to be placed
1085 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 // First check to see if the input method windows are already
1088 // located here, and contiguous.
1089 final int N = mWindows.size();
1090 WindowState firstImWin = imPos < N
1091 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 // Figure out the actual input method window that should be
1094 // at the bottom of their stack.
1095 WindowState baseImWin = imWin != null
1096 ? imWin : mInputMethodDialogs.get(0);
1097 if (baseImWin.mChildWindows.size() > 0) {
1098 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1099 if (cw.mSubLayer < 0) baseImWin = cw;
1100 }
Romain Guy06882f82009-06-10 13:36:04 -07001101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 if (firstImWin == baseImWin) {
1103 // The windows haven't moved... but are they still contiguous?
1104 // First find the top IM window.
1105 int pos = imPos+1;
1106 while (pos < N) {
1107 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1108 break;
1109 }
1110 pos++;
1111 }
1112 pos++;
1113 // Now there should be no more input method windows above.
1114 while (pos < N) {
1115 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1116 break;
1117 }
1118 pos++;
1119 }
1120 if (pos >= N) {
1121 // All is good!
1122 return false;
1123 }
1124 }
Romain Guy06882f82009-06-10 13:36:04 -07001125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 if (imWin != null) {
1127 if (DEBUG_INPUT_METHOD) {
1128 Log.v(TAG, "Moving IM from " + imPos);
1129 logWindowList(" ");
1130 }
1131 imPos = tmpRemoveWindowLocked(imPos, imWin);
1132 if (DEBUG_INPUT_METHOD) {
1133 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1134 logWindowList(" ");
1135 }
1136 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1137 reAddWindowLocked(imPos, imWin);
1138 if (DEBUG_INPUT_METHOD) {
1139 Log.v(TAG, "List after moving IM to " + imPos + ":");
1140 logWindowList(" ");
1141 }
1142 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1143 } else {
1144 moveInputMethodDialogsLocked(imPos);
1145 }
Romain Guy06882f82009-06-10 13:36:04 -07001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 } else {
1148 // In this case, the input method windows go in a fixed layer,
1149 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 if (imWin != null) {
1152 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1153 tmpRemoveWindowLocked(0, imWin);
1154 imWin.mTargetAppToken = null;
1155 reAddWindowToListInOrderLocked(imWin);
1156 if (DEBUG_INPUT_METHOD) {
1157 Log.v(TAG, "List with no IM target:");
1158 logWindowList(" ");
1159 }
1160 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1161 } else {
1162 moveInputMethodDialogsLocked(-1);;
1163 }
Romain Guy06882f82009-06-10 13:36:04 -07001164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
Romain Guy06882f82009-06-10 13:36:04 -07001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 if (needAssignLayers) {
1168 assignLayersLocked();
1169 }
Romain Guy06882f82009-06-10 13:36:04 -07001170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 return true;
1172 }
Romain Guy06882f82009-06-10 13:36:04 -07001173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 void adjustInputMethodDialogsLocked() {
1175 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1176 }
Romain Guy06882f82009-06-10 13:36:04 -07001177
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001178 boolean adjustWallpaperWindowsLocked() {
1179 boolean changed = false;
1180
1181 // First find top-most window that has asked to be on top of the
1182 // wallpaper; all wallpapers go behind it.
1183 final ArrayList localmWindows = mWindows;
1184 int N = localmWindows.size();
1185 WindowState w = null;
1186 int i = N;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001187 boolean visible = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001188 while (i > 0) {
1189 i--;
1190 w = (WindowState)localmWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001191 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
1192 && !w.mDrawPending && !w.mCommitDrawPending) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001193 visible = true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001194 break;
1195 }
1196 }
1197
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001198 if (!visible) w = null;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001199 if (DEBUG_WALLPAPER && mWallpaperTarget != w) {
1200 Log.v(TAG, "New wallpaper target: " + w);
1201 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001202 mWallpaperTarget = w;
1203
1204 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001205 // The window is visible to the compositor... but is it visible
1206 // to the user? That is what the wallpaper cares about.
1207 visible = !w.mObscured;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001208 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001209
1210 // If the wallpaper target is animating, we may need to copy
1211 // its layer adjustment.
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001212 mWallpaperAnimLayerAdjustment = w.mAppToken != null
1213 ? w.mAppToken.animLayerAdjustment : 0;
1214
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001215 // Now w is the window we are supposed to be behind... but we
1216 // need to be sure to also be behind any of its attached windows,
1217 // AND any starting window associated with it.
1218 while (i > 0) {
1219 WindowState wb = (WindowState)localmWindows.get(i-1);
1220 if (wb.mAttachedWindow != w &&
1221 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
1222 wb.mToken != w.mToken)) {
1223 // This window is not related to the previous one in any
1224 // interesting way, so stop here.
1225 break;
1226 }
1227 w = wb;
1228 i--;
1229 }
1230 }
1231
1232 // Okay i is the position immediately above the wallpaper. Look at
1233 // what is below it for later.
1234 w = i > 0 ? (WindowState)localmWindows.get(i-1) : null;
1235
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001236 final int dw = mDisplay.getWidth();
1237 final int dh = mDisplay.getHeight();
1238
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001239 // Start stepping backwards from here, ensuring that our wallpaper windows
1240 // are correctly placed.
1241 int curTokenIndex = mWallpaperTokens.size();
1242 while (curTokenIndex > 0) {
1243 curTokenIndex--;
1244 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1245 int curWallpaperIndex = token.windows.size();
1246 while (curWallpaperIndex > 0) {
1247 curWallpaperIndex--;
1248 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001249
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001250 if (visible) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001251 updateWallpaperOffsetLocked(mWallpaperTarget,
1252 wallpaper, dw, dh);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001253 }
1254
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001255 // First, make sure the client has the current visibility
1256 // state.
1257 if (wallpaper.mWallpaperVisible != visible) {
1258 wallpaper.mWallpaperVisible = visible;
1259 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001260 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001261 "Setting visibility of wallpaper " + wallpaper
1262 + ": " + visible);
1263 wallpaper.mClient.dispatchAppVisibility(visible);
1264 } catch (RemoteException e) {
1265 }
1266 }
1267
1268 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001269 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1270 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001271
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001272 // First, if this window is at the current index, then all
1273 // is well.
1274 if (wallpaper == w) {
1275 i--;
1276 w = i > 0 ? (WindowState)localmWindows.get(i-1) : null;
1277 continue;
1278 }
1279
1280 // The window didn't match... the current wallpaper window,
1281 // wherever it is, is in the wrong place, so make sure it is
1282 // not in the list.
1283 int oldIndex = localmWindows.indexOf(wallpaper);
1284 if (oldIndex >= 0) {
1285 localmWindows.remove(oldIndex);
1286 if (oldIndex < i) {
1287 i--;
1288 }
1289 }
1290
1291 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001292 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
1293 + " from " + oldIndex + " to " + i);
1294
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001295 localmWindows.add(i, wallpaper);
1296 changed = true;
1297 }
1298 }
1299
1300 return changed;
1301 }
1302
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001303 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001304 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1305 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001306 mWallpaperAnimLayerAdjustment = adj;
1307 int curTokenIndex = mWallpaperTokens.size();
1308 while (curTokenIndex > 0) {
1309 curTokenIndex--;
1310 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1311 int curWallpaperIndex = token.windows.size();
1312 while (curWallpaperIndex > 0) {
1313 curWallpaperIndex--;
1314 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1315 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001316 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1317 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001318 }
1319 }
1320 }
1321
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001322 boolean updateWallpaperOffsetLocked(WindowState target,
1323 WindowState wallpaperWin, int dw, int dh) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001324 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001325 boolean rawChanged = false;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001326 if (target.mWallpaperX >= 0) {
1327 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1328 int offset = availw > 0 ? -(int)(availw*target.mWallpaperX+.5f) : 0;
1329 changed = wallpaperWin.mXOffset != offset;
1330 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001331 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1332 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001333 wallpaperWin.mXOffset = offset;
1334 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001335 if (wallpaperWin.mWallpaperX != target.mWallpaperX) {
1336 wallpaperWin.mWallpaperX = target.mWallpaperX;
1337 rawChanged = true;
1338 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001339 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001340
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001341 if (target.mWallpaperY >= 0) {
1342 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1343 int offset = availh > 0 ? -(int)(availh*target.mWallpaperY+.5f) : 0;
1344 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001345 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1346 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001347 changed = true;
1348 wallpaperWin.mYOffset = offset;
1349 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001350 if (wallpaperWin.mWallpaperY != target.mWallpaperY) {
1351 wallpaperWin.mWallpaperY = target.mWallpaperY;
1352 rawChanged = true;
1353 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001354 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001355
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001356 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001357 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001358 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1359 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1360 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001361 wallpaperWin.mClient.dispatchWallpaperOffsets(
1362 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY);
1363 } catch (RemoteException e) {
1364 }
1365 }
1366
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001367 return changed;
1368 }
1369
1370 boolean updateWallpaperOffsetLocked() {
1371 final int dw = mDisplay.getWidth();
1372 final int dh = mDisplay.getHeight();
1373
1374 boolean changed = false;
1375
1376 WindowState target = mWallpaperTarget;
1377 if (target != null) {
1378 int curTokenIndex = mWallpaperTokens.size();
1379 while (curTokenIndex > 0) {
1380 curTokenIndex--;
1381 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1382 int curWallpaperIndex = token.windows.size();
1383 while (curWallpaperIndex > 0) {
1384 curWallpaperIndex--;
1385 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1386 if (updateWallpaperOffsetLocked(target, wallpaper, dw, dh)) {
1387 wallpaper.computeShownFrameLocked();
1388 changed = true;
1389 }
1390 }
1391 }
1392 }
1393
1394 return changed;
1395 }
1396
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001397 void updateWallpaperVisibilityLocked() {
1398 final boolean visible = mWallpaperTarget != null
1399 && !mWallpaperTarget.mObscured;
1400 final int dw = mDisplay.getWidth();
1401 final int dh = mDisplay.getHeight();
1402
1403 int curTokenIndex = mWallpaperTokens.size();
1404 while (curTokenIndex > 0) {
1405 curTokenIndex--;
1406 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1407 int curWallpaperIndex = token.windows.size();
1408 while (curWallpaperIndex > 0) {
1409 curWallpaperIndex--;
1410 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1411 if (visible) {
1412 updateWallpaperOffsetLocked(mWallpaperTarget,
1413 wallpaper, dw, dh);
1414 }
1415
1416 if (wallpaper.mWallpaperVisible != visible) {
1417 wallpaper.mWallpaperVisible = visible;
1418 try {
1419 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
1420 "Setting visibility of wallpaper " + wallpaper
1421 + ": " + visible);
1422 wallpaper.mClient.dispatchAppVisibility(visible);
1423 } catch (RemoteException e) {
1424 }
1425 }
1426 }
1427 }
1428 }
1429
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001430 void sendPointerToWallpaperLocked(WindowState srcWin,
1431 MotionEvent pointer, long eventTime) {
1432 int curTokenIndex = mWallpaperTokens.size();
1433 while (curTokenIndex > 0) {
1434 curTokenIndex--;
1435 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1436 int curWallpaperIndex = token.windows.size();
1437 while (curWallpaperIndex > 0) {
1438 curWallpaperIndex--;
1439 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1440 if ((wallpaper.mAttrs.flags &
1441 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1442 continue;
1443 }
1444 try {
1445 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1446 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1447 srcWin.mFrame.top-wallpaper.mFrame.top);
1448 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1449 } catch (RemoteException e) {
1450 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1451 }
1452 }
1453 }
1454 }
1455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 public int addWindow(Session session, IWindow client,
1457 WindowManager.LayoutParams attrs, int viewVisibility,
1458 Rect outContentInsets) {
1459 int res = mPolicy.checkAddPermission(attrs);
1460 if (res != WindowManagerImpl.ADD_OKAY) {
1461 return res;
1462 }
Romain Guy06882f82009-06-10 13:36:04 -07001463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 boolean reportNewConfig = false;
1465 WindowState attachedWindow = null;
1466 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 synchronized(mWindowMap) {
1469 // Instantiating a Display requires talking with the simulator,
1470 // so don't do it until we know the system is mostly up and
1471 // running.
1472 if (mDisplay == null) {
1473 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1474 mDisplay = wm.getDefaultDisplay();
1475 mQueue.setDisplay(mDisplay);
1476 reportNewConfig = true;
1477 }
Romain Guy06882f82009-06-10 13:36:04 -07001478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 if (mWindowMap.containsKey(client.asBinder())) {
1480 Log.w(TAG, "Window " + client + " is already added");
1481 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1482 }
1483
1484 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001485 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 if (attachedWindow == null) {
1487 Log.w(TAG, "Attempted to add window with token that is not a window: "
1488 + attrs.token + ". Aborting.");
1489 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1490 }
1491 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1492 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1493 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1494 + attrs.token + ". Aborting.");
1495 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1496 }
1497 }
1498
1499 boolean addToken = false;
1500 WindowToken token = mTokenMap.get(attrs.token);
1501 if (token == null) {
1502 if (attrs.type >= FIRST_APPLICATION_WINDOW
1503 && attrs.type <= LAST_APPLICATION_WINDOW) {
1504 Log.w(TAG, "Attempted to add application window with unknown token "
1505 + attrs.token + ". Aborting.");
1506 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1507 }
1508 if (attrs.type == TYPE_INPUT_METHOD) {
1509 Log.w(TAG, "Attempted to add input method window with unknown token "
1510 + attrs.token + ". Aborting.");
1511 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1512 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001513 if (attrs.type == TYPE_WALLPAPER) {
1514 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1515 + attrs.token + ". Aborting.");
1516 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 token = new WindowToken(attrs.token, -1, false);
1519 addToken = true;
1520 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1521 && attrs.type <= LAST_APPLICATION_WINDOW) {
1522 AppWindowToken atoken = token.appWindowToken;
1523 if (atoken == null) {
1524 Log.w(TAG, "Attempted to add window with non-application token "
1525 + token + ". Aborting.");
1526 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1527 } else if (atoken.removed) {
1528 Log.w(TAG, "Attempted to add window with exiting application token "
1529 + token + ". Aborting.");
1530 return WindowManagerImpl.ADD_APP_EXITING;
1531 }
1532 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1533 // No need for this guy!
1534 if (localLOGV) Log.v(
1535 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1536 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1537 }
1538 } else if (attrs.type == TYPE_INPUT_METHOD) {
1539 if (token.windowType != TYPE_INPUT_METHOD) {
1540 Log.w(TAG, "Attempted to add input method window with bad token "
1541 + attrs.token + ". Aborting.");
1542 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1543 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001544 } else if (attrs.type == TYPE_WALLPAPER) {
1545 if (token.windowType != TYPE_WALLPAPER) {
1546 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1547 + attrs.token + ". Aborting.");
1548 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 }
1551
1552 win = new WindowState(session, client, token,
1553 attachedWindow, attrs, viewVisibility);
1554 if (win.mDeathRecipient == null) {
1555 // Client has apparently died, so there is no reason to
1556 // continue.
1557 Log.w(TAG, "Adding window client " + client.asBinder()
1558 + " that is dead, aborting.");
1559 return WindowManagerImpl.ADD_APP_EXITING;
1560 }
1561
1562 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 res = mPolicy.prepareAddWindowLw(win, attrs);
1565 if (res != WindowManagerImpl.ADD_OKAY) {
1566 return res;
1567 }
1568
1569 // From now on, no exceptions or errors allowed!
1570
1571 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 if (addToken) {
1576 mTokenMap.put(attrs.token, token);
1577 mTokenList.add(token);
1578 }
1579 win.attach();
1580 mWindowMap.put(client.asBinder(), win);
1581
1582 if (attrs.type == TYPE_APPLICATION_STARTING &&
1583 token.appWindowToken != null) {
1584 token.appWindowToken.startingWindow = win;
1585 }
1586
1587 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 if (attrs.type == TYPE_INPUT_METHOD) {
1590 mInputMethodWindow = win;
1591 addInputMethodWindowToListLocked(win);
1592 imMayMove = false;
1593 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1594 mInputMethodDialogs.add(win);
1595 addWindowToListInOrderLocked(win, true);
1596 adjustInputMethodDialogsLocked();
1597 imMayMove = false;
1598 } else {
1599 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001600 if (attrs.type == TYPE_WALLPAPER ||
1601 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1602 adjustWallpaperWindowsLocked();
1603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 }
Romain Guy06882f82009-06-10 13:36:04 -07001605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 if (mInTouchMode) {
1611 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1612 }
1613 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1614 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1615 }
Romain Guy06882f82009-06-10 13:36:04 -07001616
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001617 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001619 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1620 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 imMayMove = false;
1622 }
1623 }
Romain Guy06882f82009-06-10 13:36:04 -07001624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001626 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 }
Romain Guy06882f82009-06-10 13:36:04 -07001628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 assignLayersLocked();
1630 // Don't do layout here, the window must call
1631 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 //dump();
1634
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001635 if (focusChanged) {
1636 if (mCurrentFocus != null) {
1637 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1638 }
1639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 if (localLOGV) Log.v(
1641 TAG, "New client " + client.asBinder()
1642 + ": window=" + win);
1643 }
1644
1645 // sendNewConfiguration() checks caller permissions so we must call it with
1646 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1647 // identity anyway, so it's safe to just clear & restore around this whole
1648 // block.
1649 final long origId = Binder.clearCallingIdentity();
1650 if (reportNewConfig) {
1651 sendNewConfiguration();
1652 } else {
1653 // Update Orientation after adding a window, only if the window needs to be
1654 // displayed right away
1655 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001656 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 sendNewConfiguration();
1658 }
1659 }
1660 }
1661 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 return res;
1664 }
Romain Guy06882f82009-06-10 13:36:04 -07001665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 public void removeWindow(Session session, IWindow client) {
1667 synchronized(mWindowMap) {
1668 WindowState win = windowForClientLocked(session, client);
1669 if (win == null) {
1670 return;
1671 }
1672 removeWindowLocked(session, win);
1673 }
1674 }
Romain Guy06882f82009-06-10 13:36:04 -07001675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 public void removeWindowLocked(Session session, WindowState win) {
1677
1678 if (localLOGV || DEBUG_FOCUS) Log.v(
1679 TAG, "Remove " + win + " client="
1680 + Integer.toHexString(System.identityHashCode(
1681 win.mClient.asBinder()))
1682 + ", surface=" + win.mSurface);
1683
1684 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 if (DEBUG_APP_TRANSITIONS) Log.v(
1687 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1688 + " mExiting=" + win.mExiting
1689 + " isAnimating=" + win.isAnimating()
1690 + " app-animation="
1691 + (win.mAppToken != null ? win.mAppToken.animation : null)
1692 + " inPendingTransaction="
1693 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1694 + " mDisplayFrozen=" + mDisplayFrozen);
1695 // Visibility of the removed window. Will be used later to update orientation later on.
1696 boolean wasVisible = false;
1697 // First, see if we need to run an animation. If we do, we have
1698 // to hold off on removing the window until the animation is done.
1699 // If the display is frozen, just remove immediately, since the
1700 // animation wouldn't be seen.
1701 if (win.mSurface != null && !mDisplayFrozen) {
1702 // If we are not currently running the exit animation, we
1703 // need to see about starting one.
1704 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1707 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1708 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1709 }
1710 // Try starting an animation.
1711 if (applyAnimationLocked(win, transit, false)) {
1712 win.mExiting = true;
1713 }
1714 }
1715 if (win.mExiting || win.isAnimating()) {
1716 // The exit animation is running... wait for it!
1717 //Log.i(TAG, "*** Running exit animation...");
1718 win.mExiting = true;
1719 win.mRemoveOnExit = true;
1720 mLayoutNeeded = true;
1721 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1722 performLayoutAndPlaceSurfacesLocked();
1723 if (win.mAppToken != null) {
1724 win.mAppToken.updateReportedVisibilityLocked();
1725 }
1726 //dump();
1727 Binder.restoreCallingIdentity(origId);
1728 return;
1729 }
1730 }
1731
1732 removeWindowInnerLocked(session, win);
1733 // Removing a visible window will effect the computed orientation
1734 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001735 if (wasVisible && computeForcedAppOrientationLocked()
1736 != mForcedAppOrientation) {
1737 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 }
1739 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1740 Binder.restoreCallingIdentity(origId);
1741 }
Romain Guy06882f82009-06-10 13:36:04 -07001742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001744 mKeyWaiter.finishedKey(session, win.mClient, true,
1745 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1747 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 if (mInputMethodTarget == win) {
1752 moveInputMethodWindowsIfNeededLocked(false);
1753 }
Romain Guy06882f82009-06-10 13:36:04 -07001754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 mPolicy.removeWindowLw(win);
1756 win.removeLocked();
1757
1758 mWindowMap.remove(win.mClient.asBinder());
1759 mWindows.remove(win);
1760
1761 if (mInputMethodWindow == win) {
1762 mInputMethodWindow = null;
1763 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1764 mInputMethodDialogs.remove(win);
1765 }
Romain Guy06882f82009-06-10 13:36:04 -07001766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 final WindowToken token = win.mToken;
1768 final AppWindowToken atoken = win.mAppToken;
1769 token.windows.remove(win);
1770 if (atoken != null) {
1771 atoken.allAppWindows.remove(win);
1772 }
1773 if (localLOGV) Log.v(
1774 TAG, "**** Removing window " + win + ": count="
1775 + token.windows.size());
1776 if (token.windows.size() == 0) {
1777 if (!token.explicit) {
1778 mTokenMap.remove(token.token);
1779 mTokenList.remove(token);
1780 } else if (atoken != null) {
1781 atoken.firstWindowDrawn = false;
1782 }
1783 }
1784
1785 if (atoken != null) {
1786 if (atoken.startingWindow == win) {
1787 atoken.startingWindow = null;
1788 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1789 // If this is the last window and we had requested a starting
1790 // transition window, well there is no point now.
1791 atoken.startingData = null;
1792 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1793 // If this is the last window except for a starting transition
1794 // window, we need to get rid of the starting transition.
1795 if (DEBUG_STARTING_WINDOW) {
1796 Log.v(TAG, "Schedule remove starting " + token
1797 + ": no more real windows");
1798 }
1799 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1800 mH.sendMessage(m);
1801 }
1802 }
Romain Guy06882f82009-06-10 13:36:04 -07001803
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001804 if (win.mAttrs.type == TYPE_WALLPAPER ||
1805 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1806 adjustWallpaperWindowsLocked();
1807 }
1808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 if (!mInLayout) {
1810 assignLayersLocked();
1811 mLayoutNeeded = true;
1812 performLayoutAndPlaceSurfacesLocked();
1813 if (win.mAppToken != null) {
1814 win.mAppToken.updateReportedVisibilityLocked();
1815 }
1816 }
1817 }
1818
1819 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1820 long origId = Binder.clearCallingIdentity();
1821 try {
1822 synchronized (mWindowMap) {
1823 WindowState w = windowForClientLocked(session, client);
1824 if ((w != null) && (w.mSurface != null)) {
1825 Surface.openTransaction();
1826 try {
1827 w.mSurface.setTransparentRegionHint(region);
1828 } finally {
1829 Surface.closeTransaction();
1830 }
1831 }
1832 }
1833 } finally {
1834 Binder.restoreCallingIdentity(origId);
1835 }
1836 }
1837
1838 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001839 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 Rect visibleInsets) {
1841 long origId = Binder.clearCallingIdentity();
1842 try {
1843 synchronized (mWindowMap) {
1844 WindowState w = windowForClientLocked(session, client);
1845 if (w != null) {
1846 w.mGivenInsetsPending = false;
1847 w.mGivenContentInsets.set(contentInsets);
1848 w.mGivenVisibleInsets.set(visibleInsets);
1849 w.mTouchableInsets = touchableInsets;
1850 mLayoutNeeded = true;
1851 performLayoutAndPlaceSurfacesLocked();
1852 }
1853 }
1854 } finally {
1855 Binder.restoreCallingIdentity(origId);
1856 }
1857 }
Romain Guy06882f82009-06-10 13:36:04 -07001858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 public void getWindowDisplayFrame(Session session, IWindow client,
1860 Rect outDisplayFrame) {
1861 synchronized(mWindowMap) {
1862 WindowState win = windowForClientLocked(session, client);
1863 if (win == null) {
1864 outDisplayFrame.setEmpty();
1865 return;
1866 }
1867 outDisplayFrame.set(win.mDisplayFrame);
1868 }
1869 }
1870
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001871 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
1872 if (window.mWallpaperX != x || window.mWallpaperY != y) {
1873 window.mWallpaperX = x;
1874 window.mWallpaperY = y;
1875
1876 if (mWallpaperTarget == window) {
1877 if (updateWallpaperOffsetLocked()) {
1878 performLayoutAndPlaceSurfacesLocked();
1879 }
1880 }
1881 }
1882 }
1883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 public int relayoutWindow(Session session, IWindow client,
1885 WindowManager.LayoutParams attrs, int requestedWidth,
1886 int requestedHeight, int viewVisibility, boolean insetsPending,
1887 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1888 Surface outSurface) {
1889 boolean displayed = false;
1890 boolean inTouchMode;
1891 Configuration newConfig = null;
1892 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 synchronized(mWindowMap) {
1895 WindowState win = windowForClientLocked(session, client);
1896 if (win == null) {
1897 return 0;
1898 }
1899 win.mRequestedWidth = requestedWidth;
1900 win.mRequestedHeight = requestedHeight;
1901
1902 if (attrs != null) {
1903 mPolicy.adjustWindowParamsLw(attrs);
1904 }
Romain Guy06882f82009-06-10 13:36:04 -07001905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 int attrChanges = 0;
1907 int flagChanges = 0;
1908 if (attrs != null) {
1909 flagChanges = win.mAttrs.flags ^= attrs.flags;
1910 attrChanges = win.mAttrs.copyFrom(attrs);
1911 }
1912
1913 if (localLOGV) Log.v(
1914 TAG, "Relayout given client " + client.asBinder()
1915 + " (" + win.mAttrs.getTitle() + ")");
1916
1917
1918 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1919 win.mAlpha = attrs.alpha;
1920 }
1921
1922 final boolean scaledWindow =
1923 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1924
1925 if (scaledWindow) {
1926 // requested{Width|Height} Surface's physical size
1927 // attrs.{width|height} Size on screen
1928 win.mHScale = (attrs.width != requestedWidth) ?
1929 (attrs.width / (float)requestedWidth) : 1.0f;
1930 win.mVScale = (attrs.height != requestedHeight) ?
1931 (attrs.height / (float)requestedHeight) : 1.0f;
1932 }
1933
1934 boolean imMayMove = (flagChanges&(
1935 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1936 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 boolean focusMayChange = win.mViewVisibility != viewVisibility
1939 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1940 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001941
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001942 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
1943 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
1944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 win.mRelayoutCalled = true;
1946 final int oldVisibility = win.mViewVisibility;
1947 win.mViewVisibility = viewVisibility;
1948 if (viewVisibility == View.VISIBLE &&
1949 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1950 displayed = !win.isVisibleLw();
1951 if (win.mExiting) {
1952 win.mExiting = false;
1953 win.mAnimation = null;
1954 }
1955 if (win.mDestroying) {
1956 win.mDestroying = false;
1957 mDestroySurface.remove(win);
1958 }
1959 if (oldVisibility == View.GONE) {
1960 win.mEnterAnimationPending = true;
1961 }
1962 if (displayed && win.mSurface != null && !win.mDrawPending
1963 && !win.mCommitDrawPending && !mDisplayFrozen) {
1964 applyEnterAnimationLocked(win);
1965 }
1966 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1967 // To change the format, we need to re-build the surface.
1968 win.destroySurfaceLocked();
1969 displayed = true;
1970 }
1971 try {
1972 Surface surface = win.createSurfaceLocked();
1973 if (surface != null) {
1974 outSurface.copyFrom(surface);
1975 } else {
1976 outSurface.clear();
1977 }
1978 } catch (Exception e) {
1979 Log.w(TAG, "Exception thrown when creating surface for client "
1980 + client + " (" + win.mAttrs.getTitle() + ")",
1981 e);
1982 Binder.restoreCallingIdentity(origId);
1983 return 0;
1984 }
1985 if (displayed) {
1986 focusMayChange = true;
1987 }
1988 if (win.mAttrs.type == TYPE_INPUT_METHOD
1989 && mInputMethodWindow == null) {
1990 mInputMethodWindow = win;
1991 imMayMove = true;
1992 }
1993 } else {
1994 win.mEnterAnimationPending = false;
1995 if (win.mSurface != null) {
1996 // If we are not currently running the exit animation, we
1997 // need to see about starting one.
1998 if (!win.mExiting) {
1999 // Try starting an animation; if there isn't one, we
2000 // can destroy the surface right away.
2001 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2002 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2003 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2004 }
2005 if (win.isWinVisibleLw() &&
2006 applyAnimationLocked(win, transit, false)) {
2007 win.mExiting = true;
2008 mKeyWaiter.finishedKey(session, client, true,
2009 KeyWaiter.RETURN_NOTHING);
2010 } else if (win.isAnimating()) {
2011 // Currently in a hide animation... turn this into
2012 // an exit.
2013 win.mExiting = true;
2014 } else {
2015 if (mInputMethodWindow == win) {
2016 mInputMethodWindow = null;
2017 }
2018 win.destroySurfaceLocked();
2019 }
2020 }
2021 }
2022 outSurface.clear();
2023 }
2024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 if (focusMayChange) {
2026 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2027 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 imMayMove = false;
2029 }
2030 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2031 }
Romain Guy06882f82009-06-10 13:36:04 -07002032
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002033 // updateFocusedWindowLocked() already assigned layers so we only need to
2034 // reassign them at this point if the IM window state gets shuffled
2035 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 if (imMayMove) {
2038 if (moveInputMethodWindowsIfNeededLocked(false)) {
2039 assignLayers = true;
2040 }
2041 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002042 if (wallpaperMayMove) {
2043 if (adjustWallpaperWindowsLocked()) {
2044 assignLayers = true;
2045 }
2046 }
Romain Guy06882f82009-06-10 13:36:04 -07002047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 mLayoutNeeded = true;
2049 win.mGivenInsetsPending = insetsPending;
2050 if (assignLayers) {
2051 assignLayersLocked();
2052 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002053 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 performLayoutAndPlaceSurfacesLocked();
2055 if (win.mAppToken != null) {
2056 win.mAppToken.updateReportedVisibilityLocked();
2057 }
2058 outFrame.set(win.mFrame);
2059 outContentInsets.set(win.mContentInsets);
2060 outVisibleInsets.set(win.mVisibleInsets);
2061 if (localLOGV) Log.v(
2062 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002063 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 + ", requestedHeight=" + requestedHeight
2065 + ", viewVisibility=" + viewVisibility
2066 + "\nRelayout returning frame=" + outFrame
2067 + ", surface=" + outSurface);
2068
2069 if (localLOGV || DEBUG_FOCUS) Log.v(
2070 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2071
2072 inTouchMode = mInTouchMode;
2073 }
2074
2075 if (newConfig != null) {
2076 sendNewConfiguration();
2077 }
Romain Guy06882f82009-06-10 13:36:04 -07002078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2082 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2083 }
2084
2085 public void finishDrawingWindow(Session session, IWindow client) {
2086 final long origId = Binder.clearCallingIdentity();
2087 synchronized(mWindowMap) {
2088 WindowState win = windowForClientLocked(session, client);
2089 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002090 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2091 adjustWallpaperWindowsLocked();
2092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 mLayoutNeeded = true;
2094 performLayoutAndPlaceSurfacesLocked();
2095 }
2096 }
2097 Binder.restoreCallingIdentity(origId);
2098 }
2099
2100 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2101 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2102 + (lp != null ? lp.packageName : null)
2103 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2104 if (lp != null && lp.windowAnimations != 0) {
2105 // If this is a system resource, don't try to load it from the
2106 // application resources. It is nice to avoid loading application
2107 // resources if we can.
2108 String packageName = lp.packageName != null ? lp.packageName : "android";
2109 int resId = lp.windowAnimations;
2110 if ((resId&0xFF000000) == 0x01000000) {
2111 packageName = "android";
2112 }
2113 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2114 + packageName);
2115 return AttributeCache.instance().get(packageName, resId,
2116 com.android.internal.R.styleable.WindowAnimation);
2117 }
2118 return null;
2119 }
Romain Guy06882f82009-06-10 13:36:04 -07002120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 private void applyEnterAnimationLocked(WindowState win) {
2122 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2123 if (win.mEnterAnimationPending) {
2124 win.mEnterAnimationPending = false;
2125 transit = WindowManagerPolicy.TRANSIT_ENTER;
2126 }
2127
2128 applyAnimationLocked(win, transit, true);
2129 }
2130
2131 private boolean applyAnimationLocked(WindowState win,
2132 int transit, boolean isEntrance) {
2133 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2134 // If we are trying to apply an animation, but already running
2135 // an animation of the same type, then just leave that one alone.
2136 return true;
2137 }
Romain Guy06882f82009-06-10 13:36:04 -07002138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 // Only apply an animation if the display isn't frozen. If it is
2140 // frozen, there is no reason to animate and it can cause strange
2141 // artifacts when we unfreeze the display if some different animation
2142 // is running.
2143 if (!mDisplayFrozen) {
2144 int anim = mPolicy.selectAnimationLw(win, transit);
2145 int attr = -1;
2146 Animation a = null;
2147 if (anim != 0) {
2148 a = AnimationUtils.loadAnimation(mContext, anim);
2149 } else {
2150 switch (transit) {
2151 case WindowManagerPolicy.TRANSIT_ENTER:
2152 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2153 break;
2154 case WindowManagerPolicy.TRANSIT_EXIT:
2155 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2156 break;
2157 case WindowManagerPolicy.TRANSIT_SHOW:
2158 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2159 break;
2160 case WindowManagerPolicy.TRANSIT_HIDE:
2161 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2162 break;
2163 }
2164 if (attr >= 0) {
2165 a = loadAnimation(win.mAttrs, attr);
2166 }
2167 }
2168 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2169 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2170 + " mAnimation=" + win.mAnimation
2171 + " isEntrance=" + isEntrance);
2172 if (a != null) {
2173 if (DEBUG_ANIM) {
2174 RuntimeException e = new RuntimeException();
2175 e.fillInStackTrace();
2176 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2177 }
2178 win.setAnimation(a);
2179 win.mAnimationIsEntrance = isEntrance;
2180 }
2181 } else {
2182 win.clearAnimation();
2183 }
2184
2185 return win.mAnimation != null;
2186 }
2187
2188 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2189 int anim = 0;
2190 Context context = mContext;
2191 if (animAttr >= 0) {
2192 AttributeCache.Entry ent = getCachedAnimations(lp);
2193 if (ent != null) {
2194 context = ent.context;
2195 anim = ent.array.getResourceId(animAttr, 0);
2196 }
2197 }
2198 if (anim != 0) {
2199 return AnimationUtils.loadAnimation(context, anim);
2200 }
2201 return null;
2202 }
Romain Guy06882f82009-06-10 13:36:04 -07002203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 private boolean applyAnimationLocked(AppWindowToken wtoken,
2205 WindowManager.LayoutParams lp, int transit, boolean enter) {
2206 // Only apply an animation if the display isn't frozen. If it is
2207 // frozen, there is no reason to animate and it can cause strange
2208 // artifacts when we unfreeze the display if some different animation
2209 // is running.
2210 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002211 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002212 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002213 a = new FadeInOutAnimation(enter);
2214 if (DEBUG_ANIM) Log.v(TAG,
2215 "applying FadeInOutAnimation for a window in compatibility mode");
2216 } else {
2217 int animAttr = 0;
2218 switch (transit) {
2219 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2220 animAttr = enter
2221 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2222 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2223 break;
2224 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2225 animAttr = enter
2226 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2227 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2228 break;
2229 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2230 animAttr = enter
2231 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2232 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2233 break;
2234 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2235 animAttr = enter
2236 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2237 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2238 break;
2239 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2240 animAttr = enter
2241 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2242 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2243 break;
2244 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2245 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002246 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002247 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2248 break;
2249 }
2250 a = loadAnimation(lp, animAttr);
2251 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2252 + " anim=" + a
2253 + " animAttr=0x" + Integer.toHexString(animAttr)
2254 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 if (a != null) {
2257 if (DEBUG_ANIM) {
2258 RuntimeException e = new RuntimeException();
2259 e.fillInStackTrace();
2260 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2261 }
2262 wtoken.setAnimation(a);
2263 }
2264 } else {
2265 wtoken.clearAnimation();
2266 }
2267
2268 return wtoken.animation != null;
2269 }
2270
2271 // -------------------------------------------------------------
2272 // Application Window Tokens
2273 // -------------------------------------------------------------
2274
2275 public void validateAppTokens(List tokens) {
2276 int v = tokens.size()-1;
2277 int m = mAppTokens.size()-1;
2278 while (v >= 0 && m >= 0) {
2279 AppWindowToken wtoken = mAppTokens.get(m);
2280 if (wtoken.removed) {
2281 m--;
2282 continue;
2283 }
2284 if (tokens.get(v) != wtoken.token) {
2285 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2286 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2287 }
2288 v--;
2289 m--;
2290 }
2291 while (v >= 0) {
2292 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2293 v--;
2294 }
2295 while (m >= 0) {
2296 AppWindowToken wtoken = mAppTokens.get(m);
2297 if (!wtoken.removed) {
2298 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2299 }
2300 m--;
2301 }
2302 }
2303
2304 boolean checkCallingPermission(String permission, String func) {
2305 // Quick check: if the calling permission is me, it's all okay.
2306 if (Binder.getCallingPid() == Process.myPid()) {
2307 return true;
2308 }
Romain Guy06882f82009-06-10 13:36:04 -07002309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 if (mContext.checkCallingPermission(permission)
2311 == PackageManager.PERMISSION_GRANTED) {
2312 return true;
2313 }
2314 String msg = "Permission Denial: " + func + " from pid="
2315 + Binder.getCallingPid()
2316 + ", uid=" + Binder.getCallingUid()
2317 + " requires " + permission;
2318 Log.w(TAG, msg);
2319 return false;
2320 }
Romain Guy06882f82009-06-10 13:36:04 -07002321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 AppWindowToken findAppWindowToken(IBinder token) {
2323 WindowToken wtoken = mTokenMap.get(token);
2324 if (wtoken == null) {
2325 return null;
2326 }
2327 return wtoken.appWindowToken;
2328 }
Romain Guy06882f82009-06-10 13:36:04 -07002329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 public void addWindowToken(IBinder token, int type) {
2331 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2332 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002333 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 }
Romain Guy06882f82009-06-10 13:36:04 -07002335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 synchronized(mWindowMap) {
2337 WindowToken wtoken = mTokenMap.get(token);
2338 if (wtoken != null) {
2339 Log.w(TAG, "Attempted to add existing input method token: " + token);
2340 return;
2341 }
2342 wtoken = new WindowToken(token, type, true);
2343 mTokenMap.put(token, wtoken);
2344 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002345 if (type == TYPE_WALLPAPER) {
2346 mWallpaperTokens.add(wtoken);
2347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 }
2349 }
Romain Guy06882f82009-06-10 13:36:04 -07002350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 public void removeWindowToken(IBinder token) {
2352 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2353 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002354 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 }
2356
2357 final long origId = Binder.clearCallingIdentity();
2358 synchronized(mWindowMap) {
2359 WindowToken wtoken = mTokenMap.remove(token);
2360 mTokenList.remove(wtoken);
2361 if (wtoken != null) {
2362 boolean delayed = false;
2363 if (!wtoken.hidden) {
2364 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 final int N = wtoken.windows.size();
2367 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 for (int i=0; i<N; i++) {
2370 WindowState win = wtoken.windows.get(i);
2371
2372 if (win.isAnimating()) {
2373 delayed = true;
2374 }
Romain Guy06882f82009-06-10 13:36:04 -07002375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 if (win.isVisibleNow()) {
2377 applyAnimationLocked(win,
2378 WindowManagerPolicy.TRANSIT_EXIT, false);
2379 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2380 KeyWaiter.RETURN_NOTHING);
2381 changed = true;
2382 }
2383 }
2384
2385 if (changed) {
2386 mLayoutNeeded = true;
2387 performLayoutAndPlaceSurfacesLocked();
2388 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2389 }
Romain Guy06882f82009-06-10 13:36:04 -07002390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 if (delayed) {
2392 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002393 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2394 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 }
2396 }
Romain Guy06882f82009-06-10 13:36:04 -07002397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 } else {
2399 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2400 }
2401 }
2402 Binder.restoreCallingIdentity(origId);
2403 }
2404
2405 public void addAppToken(int addPos, IApplicationToken token,
2406 int groupId, int requestedOrientation, boolean fullscreen) {
2407 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2408 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002409 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 }
Romain Guy06882f82009-06-10 13:36:04 -07002411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 synchronized(mWindowMap) {
2413 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2414 if (wtoken != null) {
2415 Log.w(TAG, "Attempted to add existing app token: " + token);
2416 return;
2417 }
2418 wtoken = new AppWindowToken(token);
2419 wtoken.groupId = groupId;
2420 wtoken.appFullscreen = fullscreen;
2421 wtoken.requestedOrientation = requestedOrientation;
2422 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002423 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 mTokenMap.put(token.asBinder(), wtoken);
2425 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 // Application tokens start out hidden.
2428 wtoken.hidden = true;
2429 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 //dump();
2432 }
2433 }
Romain Guy06882f82009-06-10 13:36:04 -07002434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 public void setAppGroupId(IBinder token, int groupId) {
2436 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2437 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002438 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 }
2440
2441 synchronized(mWindowMap) {
2442 AppWindowToken wtoken = findAppWindowToken(token);
2443 if (wtoken == null) {
2444 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2445 return;
2446 }
2447 wtoken.groupId = groupId;
2448 }
2449 }
Romain Guy06882f82009-06-10 13:36:04 -07002450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 public int getOrientationFromWindowsLocked() {
2452 int pos = mWindows.size() - 1;
2453 while (pos >= 0) {
2454 WindowState wtoken = (WindowState) mWindows.get(pos);
2455 pos--;
2456 if (wtoken.mAppToken != null) {
2457 // We hit an application window. so the orientation will be determined by the
2458 // app window. No point in continuing further.
2459 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2460 }
2461 if (!wtoken.isVisibleLw()) {
2462 continue;
2463 }
2464 int req = wtoken.mAttrs.screenOrientation;
2465 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2466 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2467 continue;
2468 } else {
2469 return req;
2470 }
2471 }
2472 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2473 }
Romain Guy06882f82009-06-10 13:36:04 -07002474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 public int getOrientationFromAppTokensLocked() {
2476 int pos = mAppTokens.size() - 1;
2477 int curGroup = 0;
2478 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002479 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002481 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 while (pos >= 0) {
2483 AppWindowToken wtoken = mAppTokens.get(pos);
2484 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002485 // if we're about to tear down this window and not seek for
2486 // the behind activity, don't use it for orientation
2487 if (!findingBehind
2488 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002489 continue;
2490 }
2491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 if (!haveGroup) {
2493 // We ignore any hidden applications on the top.
2494 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2495 continue;
2496 }
2497 haveGroup = true;
2498 curGroup = wtoken.groupId;
2499 lastOrientation = wtoken.requestedOrientation;
2500 } else if (curGroup != wtoken.groupId) {
2501 // If we have hit a new application group, and the bottom
2502 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002503 // the orientation behind it, and the last app was
2504 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002506 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2507 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 return lastOrientation;
2509 }
2510 }
2511 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002512 // If this application is fullscreen, and didn't explicitly say
2513 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002515 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002516 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002517 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 return or;
2519 }
2520 // If this application has requested an explicit orientation,
2521 // then use it.
2522 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2523 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2524 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2525 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2526 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2527 return or;
2528 }
Owen Lin3413b892009-05-01 17:12:32 -07002529 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 }
2531 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2532 }
Romain Guy06882f82009-06-10 13:36:04 -07002533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002535 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002536 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2537 "updateOrientationFromAppTokens()")) {
2538 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2539 }
2540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 Configuration config;
2542 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002543 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2544 freezeThisOneIfNeeded);
2545 Binder.restoreCallingIdentity(ident);
2546 return config;
2547 }
2548
2549 Configuration updateOrientationFromAppTokensUnchecked(
2550 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2551 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002553 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 }
2555 if (config != null) {
2556 mLayoutNeeded = true;
2557 performLayoutAndPlaceSurfacesLocked();
2558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 return config;
2560 }
Romain Guy06882f82009-06-10 13:36:04 -07002561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 /*
2563 * The orientation is computed from non-application windows first. If none of
2564 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002565 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2567 * android.os.IBinder)
2568 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002569 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002570 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 long ident = Binder.clearCallingIdentity();
2573 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002574 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 if (req != mForcedAppOrientation) {
2577 changed = true;
2578 mForcedAppOrientation = req;
2579 //send a message to Policy indicating orientation change to take
2580 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002581 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 }
Romain Guy06882f82009-06-10 13:36:04 -07002583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 if (changed) {
2585 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002586 WindowManagerPolicy.USE_LAST_ROTATION,
2587 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 if (changed) {
2589 if (freezeThisOneIfNeeded != null) {
2590 AppWindowToken wtoken = findAppWindowToken(
2591 freezeThisOneIfNeeded);
2592 if (wtoken != null) {
2593 startAppFreezingScreenLocked(wtoken,
2594 ActivityInfo.CONFIG_ORIENTATION);
2595 }
2596 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002597 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 }
2599 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002600
2601 // No obvious action we need to take, but if our current
2602 // state mismatches the activity maanager's, update it
2603 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002604 mTempConfiguration.setToDefaults();
2605 if (computeNewConfigurationLocked(mTempConfiguration)) {
2606 if (appConfig.diff(mTempConfiguration) != 0) {
2607 Log.i(TAG, "Config changed: " + mTempConfiguration);
2608 return new Configuration(mTempConfiguration);
2609 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002610 }
2611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 } finally {
2613 Binder.restoreCallingIdentity(ident);
2614 }
Romain Guy06882f82009-06-10 13:36:04 -07002615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 return null;
2617 }
Romain Guy06882f82009-06-10 13:36:04 -07002618
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002619 int computeForcedAppOrientationLocked() {
2620 int req = getOrientationFromWindowsLocked();
2621 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2622 req = getOrientationFromAppTokensLocked();
2623 }
2624 return req;
2625 }
Romain Guy06882f82009-06-10 13:36:04 -07002626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2628 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2629 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002630 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 }
Romain Guy06882f82009-06-10 13:36:04 -07002632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 synchronized(mWindowMap) {
2634 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2635 if (wtoken == null) {
2636 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2637 return;
2638 }
Romain Guy06882f82009-06-10 13:36:04 -07002639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 wtoken.requestedOrientation = requestedOrientation;
2641 }
2642 }
Romain Guy06882f82009-06-10 13:36:04 -07002643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 public int getAppOrientation(IApplicationToken token) {
2645 synchronized(mWindowMap) {
2646 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2647 if (wtoken == null) {
2648 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2649 }
Romain Guy06882f82009-06-10 13:36:04 -07002650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 return wtoken.requestedOrientation;
2652 }
2653 }
Romain Guy06882f82009-06-10 13:36:04 -07002654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2656 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2657 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002658 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 }
2660
2661 synchronized(mWindowMap) {
2662 boolean changed = false;
2663 if (token == null) {
2664 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2665 changed = mFocusedApp != null;
2666 mFocusedApp = null;
2667 mKeyWaiter.tickle();
2668 } else {
2669 AppWindowToken newFocus = findAppWindowToken(token);
2670 if (newFocus == null) {
2671 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2672 return;
2673 }
2674 changed = mFocusedApp != newFocus;
2675 mFocusedApp = newFocus;
2676 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2677 mKeyWaiter.tickle();
2678 }
2679
2680 if (moveFocusNow && changed) {
2681 final long origId = Binder.clearCallingIdentity();
2682 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2683 Binder.restoreCallingIdentity(origId);
2684 }
2685 }
2686 }
2687
2688 public void prepareAppTransition(int transit) {
2689 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2690 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002691 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 }
Romain Guy06882f82009-06-10 13:36:04 -07002693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 synchronized(mWindowMap) {
2695 if (DEBUG_APP_TRANSITIONS) Log.v(
2696 TAG, "Prepare app transition: transit=" + transit
2697 + " mNextAppTransition=" + mNextAppTransition);
2698 if (!mDisplayFrozen) {
2699 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2700 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002701 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2702 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2703 // Opening a new task always supersedes a close for the anim.
2704 mNextAppTransition = transit;
2705 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2706 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2707 // Opening a new activity always supersedes a close for the anim.
2708 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 }
2710 mAppTransitionReady = false;
2711 mAppTransitionTimeout = false;
2712 mStartingIconInTransition = false;
2713 mSkipAppTransitionAnimation = false;
2714 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2715 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2716 5000);
2717 }
2718 }
2719 }
2720
2721 public int getPendingAppTransition() {
2722 return mNextAppTransition;
2723 }
Romain Guy06882f82009-06-10 13:36:04 -07002724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 public void executeAppTransition() {
2726 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2727 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002728 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 }
Romain Guy06882f82009-06-10 13:36:04 -07002730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 synchronized(mWindowMap) {
2732 if (DEBUG_APP_TRANSITIONS) Log.v(
2733 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2734 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2735 mAppTransitionReady = true;
2736 final long origId = Binder.clearCallingIdentity();
2737 performLayoutAndPlaceSurfacesLocked();
2738 Binder.restoreCallingIdentity(origId);
2739 }
2740 }
2741 }
2742
2743 public void setAppStartingWindow(IBinder token, String pkg,
2744 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2745 IBinder transferFrom, boolean createIfNeeded) {
2746 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2747 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002748 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 }
2750
2751 synchronized(mWindowMap) {
2752 if (DEBUG_STARTING_WINDOW) Log.v(
2753 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2754 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 AppWindowToken wtoken = findAppWindowToken(token);
2757 if (wtoken == null) {
2758 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2759 return;
2760 }
2761
2762 // If the display is frozen, we won't do anything until the
2763 // actual window is displayed so there is no reason to put in
2764 // the starting window.
2765 if (mDisplayFrozen) {
2766 return;
2767 }
Romain Guy06882f82009-06-10 13:36:04 -07002768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 if (wtoken.startingData != null) {
2770 return;
2771 }
Romain Guy06882f82009-06-10 13:36:04 -07002772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 if (transferFrom != null) {
2774 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2775 if (ttoken != null) {
2776 WindowState startingWindow = ttoken.startingWindow;
2777 if (startingWindow != null) {
2778 if (mStartingIconInTransition) {
2779 // In this case, the starting icon has already
2780 // been displayed, so start letting windows get
2781 // shown immediately without any more transitions.
2782 mSkipAppTransitionAnimation = true;
2783 }
2784 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2785 "Moving existing starting from " + ttoken
2786 + " to " + wtoken);
2787 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 // Transfer the starting window over to the new
2790 // token.
2791 wtoken.startingData = ttoken.startingData;
2792 wtoken.startingView = ttoken.startingView;
2793 wtoken.startingWindow = startingWindow;
2794 ttoken.startingData = null;
2795 ttoken.startingView = null;
2796 ttoken.startingWindow = null;
2797 ttoken.startingMoved = true;
2798 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002799 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 startingWindow.mAppToken = wtoken;
2801 mWindows.remove(startingWindow);
2802 ttoken.windows.remove(startingWindow);
2803 ttoken.allAppWindows.remove(startingWindow);
2804 addWindowToListInOrderLocked(startingWindow, true);
2805 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 // Propagate other interesting state between the
2808 // tokens. If the old token is displayed, we should
2809 // immediately force the new one to be displayed. If
2810 // it is animating, we need to move that animation to
2811 // the new one.
2812 if (ttoken.allDrawn) {
2813 wtoken.allDrawn = true;
2814 }
2815 if (ttoken.firstWindowDrawn) {
2816 wtoken.firstWindowDrawn = true;
2817 }
2818 if (!ttoken.hidden) {
2819 wtoken.hidden = false;
2820 wtoken.hiddenRequested = false;
2821 wtoken.willBeHidden = false;
2822 }
2823 if (wtoken.clientHidden != ttoken.clientHidden) {
2824 wtoken.clientHidden = ttoken.clientHidden;
2825 wtoken.sendAppVisibilityToClients();
2826 }
2827 if (ttoken.animation != null) {
2828 wtoken.animation = ttoken.animation;
2829 wtoken.animating = ttoken.animating;
2830 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2831 ttoken.animation = null;
2832 ttoken.animLayerAdjustment = 0;
2833 wtoken.updateLayers();
2834 ttoken.updateLayers();
2835 }
Romain Guy06882f82009-06-10 13:36:04 -07002836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 mLayoutNeeded = true;
2839 performLayoutAndPlaceSurfacesLocked();
2840 Binder.restoreCallingIdentity(origId);
2841 return;
2842 } else if (ttoken.startingData != null) {
2843 // The previous app was getting ready to show a
2844 // starting window, but hasn't yet done so. Steal it!
2845 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2846 "Moving pending starting from " + ttoken
2847 + " to " + wtoken);
2848 wtoken.startingData = ttoken.startingData;
2849 ttoken.startingData = null;
2850 ttoken.startingMoved = true;
2851 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2852 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2853 // want to process the message ASAP, before any other queued
2854 // messages.
2855 mH.sendMessageAtFrontOfQueue(m);
2856 return;
2857 }
2858 }
2859 }
2860
2861 // There is no existing starting window, and the caller doesn't
2862 // want us to create one, so that's it!
2863 if (!createIfNeeded) {
2864 return;
2865 }
Romain Guy06882f82009-06-10 13:36:04 -07002866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 mStartingIconInTransition = true;
2868 wtoken.startingData = new StartingData(
2869 pkg, theme, nonLocalizedLabel,
2870 labelRes, icon);
2871 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2872 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2873 // want to process the message ASAP, before any other queued
2874 // messages.
2875 mH.sendMessageAtFrontOfQueue(m);
2876 }
2877 }
2878
2879 public void setAppWillBeHidden(IBinder token) {
2880 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2881 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002882 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 }
2884
2885 AppWindowToken wtoken;
2886
2887 synchronized(mWindowMap) {
2888 wtoken = findAppWindowToken(token);
2889 if (wtoken == null) {
2890 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2891 return;
2892 }
2893 wtoken.willBeHidden = true;
2894 }
2895 }
Romain Guy06882f82009-06-10 13:36:04 -07002896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2898 boolean visible, int transit, boolean performLayout) {
2899 boolean delayed = false;
2900
2901 if (wtoken.clientHidden == visible) {
2902 wtoken.clientHidden = !visible;
2903 wtoken.sendAppVisibilityToClients();
2904 }
Romain Guy06882f82009-06-10 13:36:04 -07002905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 wtoken.willBeHidden = false;
2907 if (wtoken.hidden == visible) {
2908 final int N = wtoken.allAppWindows.size();
2909 boolean changed = false;
2910 if (DEBUG_APP_TRANSITIONS) Log.v(
2911 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2912 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2917 if (wtoken.animation == sDummyAnimation) {
2918 wtoken.animation = null;
2919 }
2920 applyAnimationLocked(wtoken, lp, transit, visible);
2921 changed = true;
2922 if (wtoken.animation != null) {
2923 delayed = runningAppAnimation = true;
2924 }
2925 }
Romain Guy06882f82009-06-10 13:36:04 -07002926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 for (int i=0; i<N; i++) {
2928 WindowState win = wtoken.allAppWindows.get(i);
2929 if (win == wtoken.startingWindow) {
2930 continue;
2931 }
2932
2933 if (win.isAnimating()) {
2934 delayed = true;
2935 }
Romain Guy06882f82009-06-10 13:36:04 -07002936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2938 //win.dump(" ");
2939 if (visible) {
2940 if (!win.isVisibleNow()) {
2941 if (!runningAppAnimation) {
2942 applyAnimationLocked(win,
2943 WindowManagerPolicy.TRANSIT_ENTER, true);
2944 }
2945 changed = true;
2946 }
2947 } else if (win.isVisibleNow()) {
2948 if (!runningAppAnimation) {
2949 applyAnimationLocked(win,
2950 WindowManagerPolicy.TRANSIT_EXIT, false);
2951 }
2952 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2953 KeyWaiter.RETURN_NOTHING);
2954 changed = true;
2955 }
2956 }
2957
2958 wtoken.hidden = wtoken.hiddenRequested = !visible;
2959 if (!visible) {
2960 unsetAppFreezingScreenLocked(wtoken, true, true);
2961 } else {
2962 // If we are being set visible, and the starting window is
2963 // not yet displayed, then make sure it doesn't get displayed.
2964 WindowState swin = wtoken.startingWindow;
2965 if (swin != null && (swin.mDrawPending
2966 || swin.mCommitDrawPending)) {
2967 swin.mPolicyVisibility = false;
2968 swin.mPolicyVisibilityAfterAnim = false;
2969 }
2970 }
Romain Guy06882f82009-06-10 13:36:04 -07002971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2973 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2974 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 if (changed && performLayout) {
2977 mLayoutNeeded = true;
2978 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 performLayoutAndPlaceSurfacesLocked();
2980 }
2981 }
2982
2983 if (wtoken.animation != null) {
2984 delayed = true;
2985 }
Romain Guy06882f82009-06-10 13:36:04 -07002986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 return delayed;
2988 }
2989
2990 public void setAppVisibility(IBinder token, boolean visible) {
2991 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2992 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002993 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 }
2995
2996 AppWindowToken wtoken;
2997
2998 synchronized(mWindowMap) {
2999 wtoken = findAppWindowToken(token);
3000 if (wtoken == null) {
3001 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3002 return;
3003 }
3004
3005 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3006 RuntimeException e = new RuntimeException();
3007 e.fillInStackTrace();
3008 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3009 + "): mNextAppTransition=" + mNextAppTransition
3010 + " hidden=" + wtoken.hidden
3011 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3012 }
Romain Guy06882f82009-06-10 13:36:04 -07003013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 // If we are preparing an app transition, then delay changing
3015 // the visibility of this token until we execute that transition.
3016 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3017 // Already in requested state, don't do anything more.
3018 if (wtoken.hiddenRequested != visible) {
3019 return;
3020 }
3021 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 if (DEBUG_APP_TRANSITIONS) Log.v(
3024 TAG, "Setting dummy animation on: " + wtoken);
3025 wtoken.setDummyAnimation();
3026 mOpeningApps.remove(wtoken);
3027 mClosingApps.remove(wtoken);
3028 wtoken.inPendingTransaction = true;
3029 if (visible) {
3030 mOpeningApps.add(wtoken);
3031 wtoken.allDrawn = false;
3032 wtoken.startingDisplayed = false;
3033 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07003034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 if (wtoken.clientHidden) {
3036 // In the case where we are making an app visible
3037 // but holding off for a transition, we still need
3038 // to tell the client to make its windows visible so
3039 // they get drawn. Otherwise, we will wait on
3040 // performing the transition until all windows have
3041 // been drawn, they never will be, and we are sad.
3042 wtoken.clientHidden = false;
3043 wtoken.sendAppVisibilityToClients();
3044 }
3045 } else {
3046 mClosingApps.add(wtoken);
3047 }
3048 return;
3049 }
Romain Guy06882f82009-06-10 13:36:04 -07003050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 final long origId = Binder.clearCallingIdentity();
3052 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
3053 wtoken.updateReportedVisibilityLocked();
3054 Binder.restoreCallingIdentity(origId);
3055 }
3056 }
3057
3058 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3059 boolean unfreezeSurfaceNow, boolean force) {
3060 if (wtoken.freezingScreen) {
3061 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3062 + " force=" + force);
3063 final int N = wtoken.allAppWindows.size();
3064 boolean unfrozeWindows = false;
3065 for (int i=0; i<N; i++) {
3066 WindowState w = wtoken.allAppWindows.get(i);
3067 if (w.mAppFreezing) {
3068 w.mAppFreezing = false;
3069 if (w.mSurface != null && !w.mOrientationChanging) {
3070 w.mOrientationChanging = true;
3071 }
3072 unfrozeWindows = true;
3073 }
3074 }
3075 if (force || unfrozeWindows) {
3076 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3077 wtoken.freezingScreen = false;
3078 mAppsFreezingScreen--;
3079 }
3080 if (unfreezeSurfaceNow) {
3081 if (unfrozeWindows) {
3082 mLayoutNeeded = true;
3083 performLayoutAndPlaceSurfacesLocked();
3084 }
3085 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3086 stopFreezingDisplayLocked();
3087 }
3088 }
3089 }
3090 }
Romain Guy06882f82009-06-10 13:36:04 -07003091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3093 int configChanges) {
3094 if (DEBUG_ORIENTATION) {
3095 RuntimeException e = new RuntimeException();
3096 e.fillInStackTrace();
3097 Log.i(TAG, "Set freezing of " + wtoken.appToken
3098 + ": hidden=" + wtoken.hidden + " freezing="
3099 + wtoken.freezingScreen, e);
3100 }
3101 if (!wtoken.hiddenRequested) {
3102 if (!wtoken.freezingScreen) {
3103 wtoken.freezingScreen = true;
3104 mAppsFreezingScreen++;
3105 if (mAppsFreezingScreen == 1) {
3106 startFreezingDisplayLocked();
3107 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3108 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3109 5000);
3110 }
3111 }
3112 final int N = wtoken.allAppWindows.size();
3113 for (int i=0; i<N; i++) {
3114 WindowState w = wtoken.allAppWindows.get(i);
3115 w.mAppFreezing = true;
3116 }
3117 }
3118 }
Romain Guy06882f82009-06-10 13:36:04 -07003119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 public void startAppFreezingScreen(IBinder token, int configChanges) {
3121 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3122 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003123 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 }
3125
3126 synchronized(mWindowMap) {
3127 if (configChanges == 0 && !mDisplayFrozen) {
3128 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3129 return;
3130 }
Romain Guy06882f82009-06-10 13:36:04 -07003131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 AppWindowToken wtoken = findAppWindowToken(token);
3133 if (wtoken == null || wtoken.appToken == null) {
3134 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3135 return;
3136 }
3137 final long origId = Binder.clearCallingIdentity();
3138 startAppFreezingScreenLocked(wtoken, configChanges);
3139 Binder.restoreCallingIdentity(origId);
3140 }
3141 }
Romain Guy06882f82009-06-10 13:36:04 -07003142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 public void stopAppFreezingScreen(IBinder token, boolean force) {
3144 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3145 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003146 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
3148
3149 synchronized(mWindowMap) {
3150 AppWindowToken wtoken = findAppWindowToken(token);
3151 if (wtoken == null || wtoken.appToken == null) {
3152 return;
3153 }
3154 final long origId = Binder.clearCallingIdentity();
3155 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3156 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3157 unsetAppFreezingScreenLocked(wtoken, true, force);
3158 Binder.restoreCallingIdentity(origId);
3159 }
3160 }
Romain Guy06882f82009-06-10 13:36:04 -07003161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 public void removeAppToken(IBinder token) {
3163 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3164 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003165 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 }
3167
3168 AppWindowToken wtoken = null;
3169 AppWindowToken startingToken = null;
3170 boolean delayed = false;
3171
3172 final long origId = Binder.clearCallingIdentity();
3173 synchronized(mWindowMap) {
3174 WindowToken basewtoken = mTokenMap.remove(token);
3175 mTokenList.remove(basewtoken);
3176 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3177 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3178 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3179 wtoken.inPendingTransaction = false;
3180 mOpeningApps.remove(wtoken);
3181 if (mClosingApps.contains(wtoken)) {
3182 delayed = true;
3183 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3184 mClosingApps.add(wtoken);
3185 delayed = true;
3186 }
3187 if (DEBUG_APP_TRANSITIONS) Log.v(
3188 TAG, "Removing app " + wtoken + " delayed=" + delayed
3189 + " animation=" + wtoken.animation
3190 + " animating=" + wtoken.animating);
3191 if (delayed) {
3192 // set the token aside because it has an active animation to be finished
3193 mExitingAppTokens.add(wtoken);
3194 }
3195 mAppTokens.remove(wtoken);
3196 wtoken.removed = true;
3197 if (wtoken.startingData != null) {
3198 startingToken = wtoken;
3199 }
3200 unsetAppFreezingScreenLocked(wtoken, true, true);
3201 if (mFocusedApp == wtoken) {
3202 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3203 mFocusedApp = null;
3204 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3205 mKeyWaiter.tickle();
3206 }
3207 } else {
3208 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3209 }
Romain Guy06882f82009-06-10 13:36:04 -07003210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 if (!delayed && wtoken != null) {
3212 wtoken.updateReportedVisibilityLocked();
3213 }
3214 }
3215 Binder.restoreCallingIdentity(origId);
3216
3217 if (startingToken != null) {
3218 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3219 + startingToken + ": app token removed");
3220 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3221 mH.sendMessage(m);
3222 }
3223 }
3224
3225 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3226 final int NW = token.windows.size();
3227 for (int i=0; i<NW; i++) {
3228 WindowState win = token.windows.get(i);
3229 mWindows.remove(win);
3230 int j = win.mChildWindows.size();
3231 while (j > 0) {
3232 j--;
3233 mWindows.remove(win.mChildWindows.get(j));
3234 }
3235 }
3236 return NW > 0;
3237 }
3238
3239 void dumpAppTokensLocked() {
3240 for (int i=mAppTokens.size()-1; i>=0; i--) {
3241 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3242 }
3243 }
Romain Guy06882f82009-06-10 13:36:04 -07003244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 void dumpWindowsLocked() {
3246 for (int i=mWindows.size()-1; i>=0; i--) {
3247 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3248 }
3249 }
Romain Guy06882f82009-06-10 13:36:04 -07003250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 private int findWindowOffsetLocked(int tokenPos) {
3252 final int NW = mWindows.size();
3253
3254 if (tokenPos >= mAppTokens.size()) {
3255 int i = NW;
3256 while (i > 0) {
3257 i--;
3258 WindowState win = (WindowState)mWindows.get(i);
3259 if (win.getAppToken() != null) {
3260 return i+1;
3261 }
3262 }
3263 }
3264
3265 while (tokenPos > 0) {
3266 // Find the first app token below the new position that has
3267 // a window displayed.
3268 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3269 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3270 + tokenPos + " -- " + wtoken.token);
3271 int i = wtoken.windows.size();
3272 while (i > 0) {
3273 i--;
3274 WindowState win = wtoken.windows.get(i);
3275 int j = win.mChildWindows.size();
3276 while (j > 0) {
3277 j--;
3278 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3279 if (cwin.mSubLayer >= 0 ) {
3280 for (int pos=NW-1; pos>=0; pos--) {
3281 if (mWindows.get(pos) == cwin) {
3282 if (DEBUG_REORDER) Log.v(TAG,
3283 "Found child win @" + (pos+1));
3284 return pos+1;
3285 }
3286 }
3287 }
3288 }
3289 for (int pos=NW-1; pos>=0; pos--) {
3290 if (mWindows.get(pos) == win) {
3291 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3292 return pos+1;
3293 }
3294 }
3295 }
3296 tokenPos--;
3297 }
3298
3299 return 0;
3300 }
3301
3302 private final int reAddWindowLocked(int index, WindowState win) {
3303 final int NCW = win.mChildWindows.size();
3304 boolean added = false;
3305 for (int j=0; j<NCW; j++) {
3306 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3307 if (!added && cwin.mSubLayer >= 0) {
3308 mWindows.add(index, win);
3309 index++;
3310 added = true;
3311 }
3312 mWindows.add(index, cwin);
3313 index++;
3314 }
3315 if (!added) {
3316 mWindows.add(index, win);
3317 index++;
3318 }
3319 return index;
3320 }
Romain Guy06882f82009-06-10 13:36:04 -07003321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3323 final int NW = token.windows.size();
3324 for (int i=0; i<NW; i++) {
3325 index = reAddWindowLocked(index, token.windows.get(i));
3326 }
3327 return index;
3328 }
3329
3330 public void moveAppToken(int index, IBinder token) {
3331 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3332 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003333 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 }
3335
3336 synchronized(mWindowMap) {
3337 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3338 if (DEBUG_REORDER) dumpAppTokensLocked();
3339 final AppWindowToken wtoken = findAppWindowToken(token);
3340 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3341 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3342 + token + " (" + wtoken + ")");
3343 return;
3344 }
3345 mAppTokens.add(index, wtoken);
3346 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3347 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 final long origId = Binder.clearCallingIdentity();
3350 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3351 if (DEBUG_REORDER) dumpWindowsLocked();
3352 if (tmpRemoveAppWindowsLocked(wtoken)) {
3353 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3354 if (DEBUG_REORDER) dumpWindowsLocked();
3355 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3356 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3357 if (DEBUG_REORDER) dumpWindowsLocked();
3358 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 mLayoutNeeded = true;
3360 performLayoutAndPlaceSurfacesLocked();
3361 }
3362 Binder.restoreCallingIdentity(origId);
3363 }
3364 }
3365
3366 private void removeAppTokensLocked(List<IBinder> tokens) {
3367 // XXX This should be done more efficiently!
3368 // (take advantage of the fact that both lists should be
3369 // ordered in the same way.)
3370 int N = tokens.size();
3371 for (int i=0; i<N; i++) {
3372 IBinder token = tokens.get(i);
3373 final AppWindowToken wtoken = findAppWindowToken(token);
3374 if (!mAppTokens.remove(wtoken)) {
3375 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3376 + token + " (" + wtoken + ")");
3377 i--;
3378 N--;
3379 }
3380 }
3381 }
3382
3383 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3384 // First remove all of the windows from the list.
3385 final int N = tokens.size();
3386 int i;
3387 for (i=0; i<N; i++) {
3388 WindowToken token = mTokenMap.get(tokens.get(i));
3389 if (token != null) {
3390 tmpRemoveAppWindowsLocked(token);
3391 }
3392 }
3393
3394 // Where to start adding?
3395 int pos = findWindowOffsetLocked(tokenPos);
3396
3397 // And now add them back at the correct place.
3398 for (i=0; i<N; i++) {
3399 WindowToken token = mTokenMap.get(tokens.get(i));
3400 if (token != null) {
3401 pos = reAddAppWindowsLocked(pos, token);
3402 }
3403 }
3404
3405 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 mLayoutNeeded = true;
3407 performLayoutAndPlaceSurfacesLocked();
3408
3409 //dump();
3410 }
3411
3412 public void moveAppTokensToTop(List<IBinder> tokens) {
3413 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3414 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003415 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 }
3417
3418 final long origId = Binder.clearCallingIdentity();
3419 synchronized(mWindowMap) {
3420 removeAppTokensLocked(tokens);
3421 final int N = tokens.size();
3422 for (int i=0; i<N; i++) {
3423 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3424 if (wt != null) {
3425 mAppTokens.add(wt);
3426 }
3427 }
3428 moveAppWindowsLocked(tokens, mAppTokens.size());
3429 }
3430 Binder.restoreCallingIdentity(origId);
3431 }
3432
3433 public void moveAppTokensToBottom(List<IBinder> tokens) {
3434 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3435 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003436 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 }
3438
3439 final long origId = Binder.clearCallingIdentity();
3440 synchronized(mWindowMap) {
3441 removeAppTokensLocked(tokens);
3442 final int N = tokens.size();
3443 int pos = 0;
3444 for (int i=0; i<N; i++) {
3445 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3446 if (wt != null) {
3447 mAppTokens.add(pos, wt);
3448 pos++;
3449 }
3450 }
3451 moveAppWindowsLocked(tokens, 0);
3452 }
3453 Binder.restoreCallingIdentity(origId);
3454 }
3455
3456 // -------------------------------------------------------------
3457 // Misc IWindowSession methods
3458 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 public void disableKeyguard(IBinder token, String tag) {
3461 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3462 != PackageManager.PERMISSION_GRANTED) {
3463 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3464 }
3465 mKeyguardDisabled.acquire(token, tag);
3466 }
3467
3468 public void reenableKeyguard(IBinder token) {
3469 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3470 != PackageManager.PERMISSION_GRANTED) {
3471 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3472 }
3473 synchronized (mKeyguardDisabled) {
3474 mKeyguardDisabled.release(token);
3475
3476 if (!mKeyguardDisabled.isAcquired()) {
3477 // if we are the last one to reenable the keyguard wait until
3478 // we have actaully finished reenabling until returning
3479 mWaitingUntilKeyguardReenabled = true;
3480 while (mWaitingUntilKeyguardReenabled) {
3481 try {
3482 mKeyguardDisabled.wait();
3483 } catch (InterruptedException e) {
3484 Thread.currentThread().interrupt();
3485 }
3486 }
3487 }
3488 }
3489 }
3490
3491 /**
3492 * @see android.app.KeyguardManager#exitKeyguardSecurely
3493 */
3494 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3495 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3496 != PackageManager.PERMISSION_GRANTED) {
3497 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3498 }
3499 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3500 public void onKeyguardExitResult(boolean success) {
3501 try {
3502 callback.onKeyguardExitResult(success);
3503 } catch (RemoteException e) {
3504 // Client has died, we don't care.
3505 }
3506 }
3507 });
3508 }
3509
3510 public boolean inKeyguardRestrictedInputMode() {
3511 return mPolicy.inKeyguardRestrictedKeyInputMode();
3512 }
Romain Guy06882f82009-06-10 13:36:04 -07003513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 static float fixScale(float scale) {
3515 if (scale < 0) scale = 0;
3516 else if (scale > 20) scale = 20;
3517 return Math.abs(scale);
3518 }
Romain Guy06882f82009-06-10 13:36:04 -07003519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 public void setAnimationScale(int which, float scale) {
3521 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3522 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003523 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 }
3525
3526 if (scale < 0) scale = 0;
3527 else if (scale > 20) scale = 20;
3528 scale = Math.abs(scale);
3529 switch (which) {
3530 case 0: mWindowAnimationScale = fixScale(scale); break;
3531 case 1: mTransitionAnimationScale = fixScale(scale); break;
3532 }
Romain Guy06882f82009-06-10 13:36:04 -07003533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 // Persist setting
3535 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3536 }
Romain Guy06882f82009-06-10 13:36:04 -07003537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 public void setAnimationScales(float[] scales) {
3539 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3540 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003541 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 }
3543
3544 if (scales != null) {
3545 if (scales.length >= 1) {
3546 mWindowAnimationScale = fixScale(scales[0]);
3547 }
3548 if (scales.length >= 2) {
3549 mTransitionAnimationScale = fixScale(scales[1]);
3550 }
3551 }
Romain Guy06882f82009-06-10 13:36:04 -07003552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 // Persist setting
3554 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3555 }
Romain Guy06882f82009-06-10 13:36:04 -07003556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 public float getAnimationScale(int which) {
3558 switch (which) {
3559 case 0: return mWindowAnimationScale;
3560 case 1: return mTransitionAnimationScale;
3561 }
3562 return 0;
3563 }
Romain Guy06882f82009-06-10 13:36:04 -07003564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 public float[] getAnimationScales() {
3566 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3567 }
Romain Guy06882f82009-06-10 13:36:04 -07003568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 public int getSwitchState(int sw) {
3570 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3571 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003572 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 }
3574 return KeyInputQueue.getSwitchState(sw);
3575 }
Romain Guy06882f82009-06-10 13:36:04 -07003576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 public int getSwitchStateForDevice(int devid, int sw) {
3578 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3579 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003580 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 }
3582 return KeyInputQueue.getSwitchState(devid, sw);
3583 }
Romain Guy06882f82009-06-10 13:36:04 -07003584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 public int getScancodeState(int sw) {
3586 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3587 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003588 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 }
3590 return KeyInputQueue.getScancodeState(sw);
3591 }
Romain Guy06882f82009-06-10 13:36:04 -07003592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 public int getScancodeStateForDevice(int devid, int sw) {
3594 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3595 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003596 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003597 }
3598 return KeyInputQueue.getScancodeState(devid, sw);
3599 }
Romain Guy06882f82009-06-10 13:36:04 -07003600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 public int getKeycodeState(int sw) {
3602 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3603 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003604 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 }
3606 return KeyInputQueue.getKeycodeState(sw);
3607 }
Romain Guy06882f82009-06-10 13:36:04 -07003608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 public int getKeycodeStateForDevice(int devid, int sw) {
3610 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3611 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003612 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 }
3614 return KeyInputQueue.getKeycodeState(devid, sw);
3615 }
Romain Guy06882f82009-06-10 13:36:04 -07003616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3618 return KeyInputQueue.hasKeys(keycodes, keyExists);
3619 }
Romain Guy06882f82009-06-10 13:36:04 -07003620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 public void enableScreenAfterBoot() {
3622 synchronized(mWindowMap) {
3623 if (mSystemBooted) {
3624 return;
3625 }
3626 mSystemBooted = true;
3627 }
Romain Guy06882f82009-06-10 13:36:04 -07003628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003629 performEnableScreen();
3630 }
Romain Guy06882f82009-06-10 13:36:04 -07003631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 public void enableScreenIfNeededLocked() {
3633 if (mDisplayEnabled) {
3634 return;
3635 }
3636 if (!mSystemBooted) {
3637 return;
3638 }
3639 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3640 }
Romain Guy06882f82009-06-10 13:36:04 -07003641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003642 public void performEnableScreen() {
3643 synchronized(mWindowMap) {
3644 if (mDisplayEnabled) {
3645 return;
3646 }
3647 if (!mSystemBooted) {
3648 return;
3649 }
Romain Guy06882f82009-06-10 13:36:04 -07003650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 // Don't enable the screen until all existing windows
3652 // have been drawn.
3653 final int N = mWindows.size();
3654 for (int i=0; i<N; i++) {
3655 WindowState w = (WindowState)mWindows.get(i);
3656 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3657 return;
3658 }
3659 }
Romain Guy06882f82009-06-10 13:36:04 -07003660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 mDisplayEnabled = true;
3662 if (false) {
3663 Log.i(TAG, "ENABLING SCREEN!");
3664 StringWriter sw = new StringWriter();
3665 PrintWriter pw = new PrintWriter(sw);
3666 this.dump(null, pw, null);
3667 Log.i(TAG, sw.toString());
3668 }
3669 try {
3670 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3671 if (surfaceFlinger != null) {
3672 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3673 Parcel data = Parcel.obtain();
3674 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3675 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3676 data, null, 0);
3677 data.recycle();
3678 }
3679 } catch (RemoteException ex) {
3680 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3681 }
3682 }
Romain Guy06882f82009-06-10 13:36:04 -07003683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003687 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3688 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 }
Romain Guy06882f82009-06-10 13:36:04 -07003690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 public void setInTouchMode(boolean mode) {
3692 synchronized(mWindowMap) {
3693 mInTouchMode = mode;
3694 }
3695 }
3696
Romain Guy06882f82009-06-10 13:36:04 -07003697 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003698 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003700 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003701 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 }
3703
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003704 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 }
Romain Guy06882f82009-06-10 13:36:04 -07003706
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003707 public void setRotationUnchecked(int rotation,
3708 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 if(DEBUG_ORIENTATION) Log.v(TAG,
3710 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 long origId = Binder.clearCallingIdentity();
3713 boolean changed;
3714 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003715 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 }
Romain Guy06882f82009-06-10 13:36:04 -07003717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 if (changed) {
3719 sendNewConfiguration();
3720 synchronized(mWindowMap) {
3721 mLayoutNeeded = true;
3722 performLayoutAndPlaceSurfacesLocked();
3723 }
3724 } else if (alwaysSendConfiguration) {
3725 //update configuration ignoring orientation change
3726 sendNewConfiguration();
3727 }
Romain Guy06882f82009-06-10 13:36:04 -07003728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 Binder.restoreCallingIdentity(origId);
3730 }
Romain Guy06882f82009-06-10 13:36:04 -07003731
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003732 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 boolean changed;
3734 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3735 rotation = mRequestedRotation;
3736 } else {
3737 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003738 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 }
3740 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003741 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 mRotation, mDisplayEnabled);
3743 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3744 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003747 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 "Rotation changed to " + rotation
3749 + " from " + mRotation
3750 + " (forceApp=" + mForcedAppOrientation
3751 + ", req=" + mRequestedRotation + ")");
3752 mRotation = rotation;
3753 mWindowsFreezingScreen = true;
3754 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3755 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3756 2000);
3757 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003758 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 mQueue.setOrientation(rotation);
3760 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003761 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 }
3763 for (int i=mWindows.size()-1; i>=0; i--) {
3764 WindowState w = (WindowState)mWindows.get(i);
3765 if (w.mSurface != null) {
3766 w.mOrientationChanging = true;
3767 }
3768 }
3769 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3770 try {
3771 mRotationWatchers.get(i).onRotationChanged(rotation);
3772 } catch (RemoteException e) {
3773 }
3774 }
3775 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 return changed;
3778 }
Romain Guy06882f82009-06-10 13:36:04 -07003779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780 public int getRotation() {
3781 return mRotation;
3782 }
3783
3784 public int watchRotation(IRotationWatcher watcher) {
3785 final IBinder watcherBinder = watcher.asBinder();
3786 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3787 public void binderDied() {
3788 synchronized (mWindowMap) {
3789 for (int i=0; i<mRotationWatchers.size(); i++) {
3790 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003791 IRotationWatcher removed = mRotationWatchers.remove(i);
3792 if (removed != null) {
3793 removed.asBinder().unlinkToDeath(this, 0);
3794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003795 i--;
3796 }
3797 }
3798 }
3799 }
3800 };
Romain Guy06882f82009-06-10 13:36:04 -07003801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 synchronized (mWindowMap) {
3803 try {
3804 watcher.asBinder().linkToDeath(dr, 0);
3805 mRotationWatchers.add(watcher);
3806 } catch (RemoteException e) {
3807 // Client died, no cleanup needed.
3808 }
Romain Guy06882f82009-06-10 13:36:04 -07003809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 return mRotation;
3811 }
3812 }
3813
3814 /**
3815 * Starts the view server on the specified port.
3816 *
3817 * @param port The port to listener to.
3818 *
3819 * @return True if the server was successfully started, false otherwise.
3820 *
3821 * @see com.android.server.ViewServer
3822 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3823 */
3824 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003825 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 return false;
3827 }
3828
3829 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3830 return false;
3831 }
3832
3833 if (port < 1024) {
3834 return false;
3835 }
3836
3837 if (mViewServer != null) {
3838 if (!mViewServer.isRunning()) {
3839 try {
3840 return mViewServer.start();
3841 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003842 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003843 }
3844 }
3845 return false;
3846 }
3847
3848 try {
3849 mViewServer = new ViewServer(this, port);
3850 return mViewServer.start();
3851 } catch (IOException e) {
3852 Log.w(TAG, "View server did not start");
3853 }
3854 return false;
3855 }
3856
Romain Guy06882f82009-06-10 13:36:04 -07003857 private boolean isSystemSecure() {
3858 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3859 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3860 }
3861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 /**
3863 * Stops the view server if it exists.
3864 *
3865 * @return True if the server stopped, false if it wasn't started or
3866 * couldn't be stopped.
3867 *
3868 * @see com.android.server.ViewServer
3869 */
3870 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003871 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 return false;
3873 }
3874
3875 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3876 return false;
3877 }
3878
3879 if (mViewServer != null) {
3880 return mViewServer.stop();
3881 }
3882 return false;
3883 }
3884
3885 /**
3886 * Indicates whether the view server is running.
3887 *
3888 * @return True if the server is running, false otherwise.
3889 *
3890 * @see com.android.server.ViewServer
3891 */
3892 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003893 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 return false;
3895 }
3896
3897 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3898 return false;
3899 }
3900
3901 return mViewServer != null && mViewServer.isRunning();
3902 }
3903
3904 /**
3905 * Lists all availble windows in the system. The listing is written in the
3906 * specified Socket's output stream with the following syntax:
3907 * windowHashCodeInHexadecimal windowName
3908 * Each line of the ouput represents a different window.
3909 *
3910 * @param client The remote client to send the listing to.
3911 * @return False if an error occured, true otherwise.
3912 */
3913 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003914 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 return false;
3916 }
3917
3918 boolean result = true;
3919
3920 Object[] windows;
3921 synchronized (mWindowMap) {
3922 windows = new Object[mWindows.size()];
3923 //noinspection unchecked
3924 windows = mWindows.toArray(windows);
3925 }
3926
3927 BufferedWriter out = null;
3928
3929 // Any uncaught exception will crash the system process
3930 try {
3931 OutputStream clientStream = client.getOutputStream();
3932 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3933
3934 final int count = windows.length;
3935 for (int i = 0; i < count; i++) {
3936 final WindowState w = (WindowState) windows[i];
3937 out.write(Integer.toHexString(System.identityHashCode(w)));
3938 out.write(' ');
3939 out.append(w.mAttrs.getTitle());
3940 out.write('\n');
3941 }
3942
3943 out.write("DONE.\n");
3944 out.flush();
3945 } catch (Exception e) {
3946 result = false;
3947 } finally {
3948 if (out != null) {
3949 try {
3950 out.close();
3951 } catch (IOException e) {
3952 result = false;
3953 }
3954 }
3955 }
3956
3957 return result;
3958 }
3959
3960 /**
3961 * Sends a command to a target window. The result of the command, if any, will be
3962 * written in the output stream of the specified socket.
3963 *
3964 * The parameters must follow this syntax:
3965 * windowHashcode extra
3966 *
3967 * Where XX is the length in characeters of the windowTitle.
3968 *
3969 * The first parameter is the target window. The window with the specified hashcode
3970 * will be the target. If no target can be found, nothing happens. The extra parameters
3971 * will be delivered to the target window and as parameters to the command itself.
3972 *
3973 * @param client The remote client to sent the result, if any, to.
3974 * @param command The command to execute.
3975 * @param parameters The command parameters.
3976 *
3977 * @return True if the command was successfully delivered, false otherwise. This does
3978 * not indicate whether the command itself was successful.
3979 */
3980 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003981 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 return false;
3983 }
3984
3985 boolean success = true;
3986 Parcel data = null;
3987 Parcel reply = null;
3988
3989 // Any uncaught exception will crash the system process
3990 try {
3991 // Find the hashcode of the window
3992 int index = parameters.indexOf(' ');
3993 if (index == -1) {
3994 index = parameters.length();
3995 }
3996 final String code = parameters.substring(0, index);
3997 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3998
3999 // Extract the command's parameter after the window description
4000 if (index < parameters.length()) {
4001 parameters = parameters.substring(index + 1);
4002 } else {
4003 parameters = "";
4004 }
4005
4006 final WindowManagerService.WindowState window = findWindow(hashCode);
4007 if (window == null) {
4008 return false;
4009 }
4010
4011 data = Parcel.obtain();
4012 data.writeInterfaceToken("android.view.IWindow");
4013 data.writeString(command);
4014 data.writeString(parameters);
4015 data.writeInt(1);
4016 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4017
4018 reply = Parcel.obtain();
4019
4020 final IBinder binder = window.mClient.asBinder();
4021 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4022 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4023
4024 reply.readException();
4025
4026 } catch (Exception e) {
4027 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4028 success = false;
4029 } finally {
4030 if (data != null) {
4031 data.recycle();
4032 }
4033 if (reply != null) {
4034 reply.recycle();
4035 }
4036 }
4037
4038 return success;
4039 }
4040
4041 private WindowState findWindow(int hashCode) {
4042 if (hashCode == -1) {
4043 return getFocusedWindow();
4044 }
4045
4046 synchronized (mWindowMap) {
4047 final ArrayList windows = mWindows;
4048 final int count = windows.size();
4049
4050 for (int i = 0; i < count; i++) {
4051 WindowState w = (WindowState) windows.get(i);
4052 if (System.identityHashCode(w) == hashCode) {
4053 return w;
4054 }
4055 }
4056 }
4057
4058 return null;
4059 }
4060
4061 /*
4062 * Instruct the Activity Manager to fetch the current configuration and broadcast
4063 * that to config-changed listeners if appropriate.
4064 */
4065 void sendNewConfiguration() {
4066 try {
4067 mActivityManager.updateConfiguration(null);
4068 } catch (RemoteException e) {
4069 }
4070 }
Romain Guy06882f82009-06-10 13:36:04 -07004071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 public Configuration computeNewConfiguration() {
4073 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004074 return computeNewConfigurationLocked();
4075 }
4076 }
Romain Guy06882f82009-06-10 13:36:04 -07004077
Dianne Hackbornc485a602009-03-24 22:39:49 -07004078 Configuration computeNewConfigurationLocked() {
4079 Configuration config = new Configuration();
4080 if (!computeNewConfigurationLocked(config)) {
4081 return null;
4082 }
4083 Log.i(TAG, "Config changed: " + config);
4084 long now = SystemClock.uptimeMillis();
4085 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
4086 if (mFreezeGcPending != 0) {
4087 if (now > (mFreezeGcPending+1000)) {
4088 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
4089 mH.removeMessages(H.FORCE_GC);
4090 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 mFreezeGcPending = now;
4092 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004093 } else {
4094 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004096 return config;
4097 }
Romain Guy06882f82009-06-10 13:36:04 -07004098
Dianne Hackbornc485a602009-03-24 22:39:49 -07004099 boolean computeNewConfigurationLocked(Configuration config) {
4100 if (mDisplay == null) {
4101 return false;
4102 }
4103 mQueue.getInputConfiguration(config);
4104 final int dw = mDisplay.getWidth();
4105 final int dh = mDisplay.getHeight();
4106 int orientation = Configuration.ORIENTATION_SQUARE;
4107 if (dw < dh) {
4108 orientation = Configuration.ORIENTATION_PORTRAIT;
4109 } else if (dw > dh) {
4110 orientation = Configuration.ORIENTATION_LANDSCAPE;
4111 }
4112 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004113
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004114 DisplayMetrics dm = new DisplayMetrics();
4115 mDisplay.getMetrics(dm);
4116 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4117
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004118 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004119 // Note we only do this once because at this point we don't
4120 // expect the screen to change in this way at runtime, and want
4121 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004122 int longSize = dw;
4123 int shortSize = dh;
4124 if (longSize < shortSize) {
4125 int tmp = longSize;
4126 longSize = shortSize;
4127 shortSize = tmp;
4128 }
4129 longSize = (int)(longSize/dm.density);
4130 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004131
Dianne Hackborn723738c2009-06-25 19:48:04 -07004132 // These semi-magic numbers define our compatibility modes for
4133 // applications with different screens. Don't change unless you
4134 // make sure to test lots and lots of apps!
4135 if (longSize < 470) {
4136 // This is shorter than an HVGA normal density screen (which
4137 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004138 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4139 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004140 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004141 // Is this a large screen?
4142 if (longSize > 640 && shortSize >= 480) {
4143 // VGA or larger screens at medium density are the point
4144 // at which we consider it to be a large screen.
4145 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4146 } else {
4147 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4148
4149 // If this screen is wider than normal HVGA, or taller
4150 // than FWVGA, then for old apps we want to run in size
4151 // compatibility mode.
4152 if (shortSize > 321 || longSize > 570) {
4153 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4154 }
4155 }
4156
4157 // Is this a long screen?
4158 if (((longSize*3)/5) >= (shortSize-1)) {
4159 // Anything wider than WVGA (5:3) is considering to be long.
4160 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4161 } else {
4162 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4163 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004164 }
4165 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004166 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004167
Dianne Hackbornc485a602009-03-24 22:39:49 -07004168 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4169 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4170 mPolicy.adjustConfigurationLw(config);
4171 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 }
Romain Guy06882f82009-06-10 13:36:04 -07004173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 // -------------------------------------------------------------
4175 // Input Events and Focus Management
4176 // -------------------------------------------------------------
4177
4178 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004179 long curTime = SystemClock.uptimeMillis();
4180
Michael Chane10de972009-05-18 11:24:50 -07004181 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004182 if (mLastTouchEventType == eventType &&
4183 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4184 return;
4185 }
4186 mLastUserActivityCallTime = curTime;
4187 mLastTouchEventType = eventType;
4188 }
4189
4190 if (targetWin == null
4191 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4192 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 }
4194 }
4195
4196 // tells if it's a cheek event or not -- this function is stateful
4197 private static final int EVENT_NONE = 0;
4198 private static final int EVENT_UNKNOWN = 0;
4199 private static final int EVENT_CHEEK = 0;
4200 private static final int EVENT_IGNORE_DURATION = 300; // ms
4201 private static final float CHEEK_THRESHOLD = 0.6f;
4202 private int mEventState = EVENT_NONE;
4203 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 private int eventType(MotionEvent ev) {
4206 float size = ev.getSize();
4207 switch (ev.getAction()) {
4208 case MotionEvent.ACTION_DOWN:
4209 mEventSize = size;
4210 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4211 case MotionEvent.ACTION_UP:
4212 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004213 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 case MotionEvent.ACTION_MOVE:
4215 final int N = ev.getHistorySize();
4216 if (size > mEventSize) mEventSize = size;
4217 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4218 for (int i=0; i<N; i++) {
4219 size = ev.getHistoricalSize(i);
4220 if (size > mEventSize) mEventSize = size;
4221 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4222 }
4223 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4224 return TOUCH_EVENT;
4225 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004226 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
4228 default:
4229 // not good
4230 return OTHER_EVENT;
4231 }
4232 }
4233
4234 /**
4235 * @return Returns true if event was dispatched, false if it was dropped for any reason
4236 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004237 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004238 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4239 "dispatchPointer " + ev);
4240
Michael Chan53071d62009-05-13 17:29:48 -07004241 if (MEASURE_LATENCY) {
4242 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4243 }
4244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004246 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247
Michael Chan53071d62009-05-13 17:29:48 -07004248 if (MEASURE_LATENCY) {
4249 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4250 }
4251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 if (action == MotionEvent.ACTION_UP) {
4255 // let go of our target
4256 mKeyWaiter.mMotionTarget = null;
4257 mPowerManager.logPointerUpEvent();
4258 } else if (action == MotionEvent.ACTION_DOWN) {
4259 mPowerManager.logPointerDownEvent();
4260 }
4261
4262 if (targetObj == null) {
4263 // In this case we are either dropping the event, or have received
4264 // a move or up without a down. It is common to receive move
4265 // events in such a way, since this means the user is moving the
4266 // pointer without actually pressing down. All other cases should
4267 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004268 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4270 }
4271 if (qev != null) {
4272 mQueue.recycleEvent(qev);
4273 }
4274 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004275 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 }
4277 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4278 if (qev != null) {
4279 mQueue.recycleEvent(qev);
4280 }
4281 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004282 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 }
Romain Guy06882f82009-06-10 13:36:04 -07004284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004288 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289
4290 //Log.i(TAG, "Sending " + ev + " to " + target);
4291
4292 if (uid != 0 && uid != target.mSession.mUid) {
4293 if (mContext.checkPermission(
4294 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4295 != PackageManager.PERMISSION_GRANTED) {
4296 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4297 + pid + " uid " + uid + " to window " + target
4298 + " owned by uid " + target.mSession.mUid);
4299 if (qev != null) {
4300 mQueue.recycleEvent(qev);
4301 }
4302 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004303 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304 }
4305 }
4306
Michael Chan53071d62009-05-13 17:29:48 -07004307 if (MEASURE_LATENCY) {
4308 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4309 }
4310
Romain Guy06882f82009-06-10 13:36:04 -07004311 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4313 //target wants to ignore fat touch events
4314 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4315 //explicit flag to return without processing event further
4316 boolean returnFlag = false;
4317 if((action == MotionEvent.ACTION_DOWN)) {
4318 mFatTouch = false;
4319 if(cheekPress) {
4320 mFatTouch = true;
4321 returnFlag = true;
4322 }
4323 } else {
4324 if(action == MotionEvent.ACTION_UP) {
4325 if(mFatTouch) {
4326 //earlier even was invalid doesnt matter if current up is cheekpress or not
4327 mFatTouch = false;
4328 returnFlag = true;
4329 } else if(cheekPress) {
4330 //cancel the earlier event
4331 ev.setAction(MotionEvent.ACTION_CANCEL);
4332 action = MotionEvent.ACTION_CANCEL;
4333 }
4334 } else if(action == MotionEvent.ACTION_MOVE) {
4335 if(mFatTouch) {
4336 //two cases here
4337 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004338 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 returnFlag = true;
4340 } else if(cheekPress) {
4341 //valid down followed by invalid moves
4342 //an invalid move have to cancel earlier action
4343 ev.setAction(MotionEvent.ACTION_CANCEL);
4344 action = MotionEvent.ACTION_CANCEL;
4345 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4346 //note that the subsequent invalid moves will not get here
4347 mFatTouch = true;
4348 }
4349 }
4350 } //else if action
4351 if(returnFlag) {
4352 //recycle que, ev
4353 if (qev != null) {
4354 mQueue.recycleEvent(qev);
4355 }
4356 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004357 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 }
4359 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004360
Michael Chan9f028e62009-08-04 17:37:46 -07004361 // Enable this for testing the "right" value
4362 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004363 int max_events_per_sec = 35;
4364 try {
4365 max_events_per_sec = Integer.parseInt(SystemProperties
4366 .get("windowsmgr.max_events_per_sec"));
4367 if (max_events_per_sec < 1) {
4368 max_events_per_sec = 35;
4369 }
4370 } catch (NumberFormatException e) {
4371 }
4372 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4373 }
4374
4375 /*
4376 * Throttle events to minimize CPU usage when there's a flood of events
4377 * e.g. constant contact with the screen
4378 */
4379 if (action == MotionEvent.ACTION_MOVE) {
4380 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4381 long now = SystemClock.uptimeMillis();
4382 if (now < nextEventTime) {
4383 try {
4384 Thread.sleep(nextEventTime - now);
4385 } catch (InterruptedException e) {
4386 }
4387 mLastTouchEventTime = nextEventTime;
4388 } else {
4389 mLastTouchEventTime = now;
4390 }
4391 }
4392
Michael Chan53071d62009-05-13 17:29:48 -07004393 if (MEASURE_LATENCY) {
4394 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4395 }
4396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 synchronized(mWindowMap) {
4398 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4399 mKeyWaiter.bindTargetWindowLocked(target,
4400 KeyWaiter.RETURN_PENDING_POINTER, qev);
4401 ev = null;
4402 } else {
4403 if (action == MotionEvent.ACTION_DOWN) {
4404 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4405 if (out != null) {
4406 MotionEvent oev = MotionEvent.obtain(ev);
4407 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4408 do {
4409 final Rect frame = out.mFrame;
4410 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4411 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004412 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 } catch (android.os.RemoteException e) {
4414 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4415 }
4416 oev.offsetLocation((float)frame.left, (float)frame.top);
4417 out = out.mNextOutsideTouch;
4418 } while (out != null);
4419 mKeyWaiter.mOutsideTouchTargets = null;
4420 }
4421 }
4422 final Rect frame = target.mFrame;
4423 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4424 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004425
4426 // If we are on top of the wallpaper, then the wallpaper also
4427 // gets to see this movement.
4428 if (mWallpaperTarget == target) {
4429 sendPointerToWallpaperLocked(target, ev, eventTime);
4430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 }
4432 }
Romain Guy06882f82009-06-10 13:36:04 -07004433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 // finally offset the event to the target's coordinate system and
4435 // dispatch the event.
4436 try {
4437 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4438 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4439 }
Michael Chan53071d62009-05-13 17:29:48 -07004440
4441 if (MEASURE_LATENCY) {
4442 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4443 }
4444
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004445 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004446
4447 if (MEASURE_LATENCY) {
4448 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4449 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004450 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 } catch (android.os.RemoteException e) {
4452 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4453 mKeyWaiter.mMotionTarget = null;
4454 try {
4455 removeWindow(target.mSession, target.mClient);
4456 } catch (java.util.NoSuchElementException ex) {
4457 // This will happen if the window has already been
4458 // removed.
4459 }
4460 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004461 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 }
Romain Guy06882f82009-06-10 13:36:04 -07004463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 /**
4465 * @return Returns true if event was dispatched, false if it was dropped for any reason
4466 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004467 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 if (DEBUG_INPUT) Log.v(
4469 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004472 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 if (focusObj == null) {
4474 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4475 if (qev != null) {
4476 mQueue.recycleEvent(qev);
4477 }
4478 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004479 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004480 }
4481 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4482 if (qev != null) {
4483 mQueue.recycleEvent(qev);
4484 }
4485 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004486 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 }
Romain Guy06882f82009-06-10 13:36:04 -07004488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004491 if (uid != 0 && uid != focus.mSession.mUid) {
4492 if (mContext.checkPermission(
4493 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4494 != PackageManager.PERMISSION_GRANTED) {
4495 Log.w(TAG, "Permission denied: injecting key event from pid "
4496 + pid + " uid " + uid + " to window " + focus
4497 + " owned by uid " + focus.mSession.mUid);
4498 if (qev != null) {
4499 mQueue.recycleEvent(qev);
4500 }
4501 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004502 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 }
4504 }
Romain Guy06882f82009-06-10 13:36:04 -07004505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 synchronized(mWindowMap) {
4509 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4510 mKeyWaiter.bindTargetWindowLocked(focus,
4511 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4512 // We don't deliver movement events to the client, we hold
4513 // them and wait for them to call back.
4514 ev = null;
4515 } else {
4516 mKeyWaiter.bindTargetWindowLocked(focus);
4517 }
4518 }
Romain Guy06882f82009-06-10 13:36:04 -07004519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004521 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004522 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 } catch (android.os.RemoteException e) {
4524 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4525 try {
4526 removeWindow(focus.mSession, focus.mClient);
4527 } catch (java.util.NoSuchElementException ex) {
4528 // This will happen if the window has already been
4529 // removed.
4530 }
4531 }
Romain Guy06882f82009-06-10 13:36:04 -07004532
Dianne Hackborncfaef692009-06-15 14:24:44 -07004533 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 }
Romain Guy06882f82009-06-10 13:36:04 -07004535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 /**
4537 * @return Returns true if event was dispatched, false if it was dropped for any reason
4538 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004539 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4541
4542 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004543 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 if (focusObj == null) {
4545 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004546 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 }
4548 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004549 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 }
Romain Guy06882f82009-06-10 13:36:04 -07004551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 if (DEBUG_INPUT) Log.v(
4555 TAG, "Dispatching to " + focus + ": " + event);
4556
4557 if (uid != 0 && uid != focus.mSession.mUid) {
4558 if (mContext.checkPermission(
4559 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4560 != PackageManager.PERMISSION_GRANTED) {
4561 Log.w(TAG, "Permission denied: injecting key event from pid "
4562 + pid + " uid " + uid + " to window " + focus
4563 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004564 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 }
4566 }
Romain Guy06882f82009-06-10 13:36:04 -07004567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 synchronized(mWindowMap) {
4569 mKeyWaiter.bindTargetWindowLocked(focus);
4570 }
4571
4572 // NOSHIP extra state logging
4573 mKeyWaiter.recordDispatchState(event, focus);
4574 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 try {
4577 if (DEBUG_INPUT || DEBUG_FOCUS) {
4578 Log.v(TAG, "Delivering key " + event.getKeyCode()
4579 + " to " + focus);
4580 }
4581 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004582 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004583 } catch (android.os.RemoteException e) {
4584 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4585 try {
4586 removeWindow(focus.mSession, focus.mClient);
4587 } catch (java.util.NoSuchElementException ex) {
4588 // This will happen if the window has already been
4589 // removed.
4590 }
4591 }
Romain Guy06882f82009-06-10 13:36:04 -07004592
Dianne Hackborncfaef692009-06-15 14:24:44 -07004593 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 }
Romain Guy06882f82009-06-10 13:36:04 -07004595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 public void pauseKeyDispatching(IBinder _token) {
4597 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4598 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004599 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004600 }
4601
4602 synchronized (mWindowMap) {
4603 WindowToken token = mTokenMap.get(_token);
4604 if (token != null) {
4605 mKeyWaiter.pauseDispatchingLocked(token);
4606 }
4607 }
4608 }
4609
4610 public void resumeKeyDispatching(IBinder _token) {
4611 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4612 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004613 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 }
4615
4616 synchronized (mWindowMap) {
4617 WindowToken token = mTokenMap.get(_token);
4618 if (token != null) {
4619 mKeyWaiter.resumeDispatchingLocked(token);
4620 }
4621 }
4622 }
4623
4624 public void setEventDispatching(boolean enabled) {
4625 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4626 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004627 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004628 }
4629
4630 synchronized (mWindowMap) {
4631 mKeyWaiter.setEventDispatchingLocked(enabled);
4632 }
4633 }
Romain Guy06882f82009-06-10 13:36:04 -07004634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 /**
4636 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004637 *
4638 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639 * {@link SystemClock#uptimeMillis()} as the timebase.)
4640 * @param sync If true, wait for the event to be completed before returning to the caller.
4641 * @return Returns true if event was dispatched, false if it was dropped for any reason
4642 */
4643 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4644 long downTime = ev.getDownTime();
4645 long eventTime = ev.getEventTime();
4646
4647 int action = ev.getAction();
4648 int code = ev.getKeyCode();
4649 int repeatCount = ev.getRepeatCount();
4650 int metaState = ev.getMetaState();
4651 int deviceId = ev.getDeviceId();
4652 int scancode = ev.getScanCode();
4653
4654 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4655 if (downTime == 0) downTime = eventTime;
4656
4657 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004658 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004659
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004660 final int pid = Binder.getCallingPid();
4661 final int uid = Binder.getCallingUid();
4662 final long ident = Binder.clearCallingIdentity();
4663 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004665 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004667 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004668 switch (result) {
4669 case INJECT_NO_PERMISSION:
4670 throw new SecurityException(
4671 "Injecting to another application requires INJECT_EVENT permission");
4672 case INJECT_SUCCEEDED:
4673 return true;
4674 }
4675 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004676 }
4677
4678 /**
4679 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004680 *
4681 * @param ev A motion event describing the pointer (touch) action. (As noted in
4682 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 * {@link SystemClock#uptimeMillis()} as the timebase.)
4684 * @param sync If true, wait for the event to be completed before returning to the caller.
4685 * @return Returns true if event was dispatched, false if it was dropped for any reason
4686 */
4687 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004688 final int pid = Binder.getCallingPid();
4689 final int uid = Binder.getCallingUid();
4690 final long ident = Binder.clearCallingIdentity();
4691 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004693 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004695 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004696 switch (result) {
4697 case INJECT_NO_PERMISSION:
4698 throw new SecurityException(
4699 "Injecting to another application requires INJECT_EVENT permission");
4700 case INJECT_SUCCEEDED:
4701 return true;
4702 }
4703 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004704 }
Romain Guy06882f82009-06-10 13:36:04 -07004705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004706 /**
4707 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004708 *
4709 * @param ev A motion event describing the trackball action. (As noted in
4710 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 * {@link SystemClock#uptimeMillis()} as the timebase.)
4712 * @param sync If true, wait for the event to be completed before returning to the caller.
4713 * @return Returns true if event was dispatched, false if it was dropped for any reason
4714 */
4715 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004716 final int pid = Binder.getCallingPid();
4717 final int uid = Binder.getCallingUid();
4718 final long ident = Binder.clearCallingIdentity();
4719 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004721 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004723 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004724 switch (result) {
4725 case INJECT_NO_PERMISSION:
4726 throw new SecurityException(
4727 "Injecting to another application requires INJECT_EVENT permission");
4728 case INJECT_SUCCEEDED:
4729 return true;
4730 }
4731 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004732 }
Romain Guy06882f82009-06-10 13:36:04 -07004733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 private WindowState getFocusedWindow() {
4735 synchronized (mWindowMap) {
4736 return getFocusedWindowLocked();
4737 }
4738 }
4739
4740 private WindowState getFocusedWindowLocked() {
4741 return mCurrentFocus;
4742 }
Romain Guy06882f82009-06-10 13:36:04 -07004743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004744 /**
4745 * This class holds the state for dispatching key events. This state
4746 * is protected by the KeyWaiter instance, NOT by the window lock. You
4747 * can be holding the main window lock while acquire the KeyWaiter lock,
4748 * but not the other way around.
4749 */
4750 final class KeyWaiter {
4751 // NOSHIP debugging
4752 public class DispatchState {
4753 private KeyEvent event;
4754 private WindowState focus;
4755 private long time;
4756 private WindowState lastWin;
4757 private IBinder lastBinder;
4758 private boolean finished;
4759 private boolean gotFirstWindow;
4760 private boolean eventDispatching;
4761 private long timeToSwitch;
4762 private boolean wasFrozen;
4763 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004764 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004766 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4767 focus = theFocus;
4768 event = theEvent;
4769 time = System.currentTimeMillis();
4770 // snapshot KeyWaiter state
4771 lastWin = mLastWin;
4772 lastBinder = mLastBinder;
4773 finished = mFinished;
4774 gotFirstWindow = mGotFirstWindow;
4775 eventDispatching = mEventDispatching;
4776 timeToSwitch = mTimeToSwitch;
4777 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004778 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004779 // cache the paused state at ctor time as well
4780 if (theFocus == null || theFocus.mToken == null) {
4781 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4782 focusPaused = false;
4783 } else {
4784 focusPaused = theFocus.mToken.paused;
4785 }
4786 }
Romain Guy06882f82009-06-10 13:36:04 -07004787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004788 public String toString() {
4789 return "{{" + event + " to " + focus + " @ " + time
4790 + " lw=" + lastWin + " lb=" + lastBinder
4791 + " fin=" + finished + " gfw=" + gotFirstWindow
4792 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004793 + " wf=" + wasFrozen + " fp=" + focusPaused
4794 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004795 }
4796 };
4797 private DispatchState mDispatchState = null;
4798 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4799 mDispatchState = new DispatchState(theEvent, theFocus);
4800 }
4801 // END NOSHIP
4802
4803 public static final int RETURN_NOTHING = 0;
4804 public static final int RETURN_PENDING_POINTER = 1;
4805 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 final Object SKIP_TARGET_TOKEN = new Object();
4808 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004810 private WindowState mLastWin = null;
4811 private IBinder mLastBinder = null;
4812 private boolean mFinished = true;
4813 private boolean mGotFirstWindow = false;
4814 private boolean mEventDispatching = true;
4815 private long mTimeToSwitch = 0;
4816 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 // Target of Motion events
4819 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 // Windows above the target who would like to receive an "outside"
4822 // touch event for any down events outside of them.
4823 WindowState mOutsideTouchTargets;
4824
4825 /**
4826 * Wait for the last event dispatch to complete, then find the next
4827 * target that should receive the given event and wait for that one
4828 * to be ready to receive it.
4829 */
4830 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4831 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004832 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 long startTime = SystemClock.uptimeMillis();
4834 long keyDispatchingTimeout = 5 * 1000;
4835 long waitedFor = 0;
4836
4837 while (true) {
4838 // Figure out which window we care about. It is either the
4839 // last window we are waiting to have process the event or,
4840 // if none, then the next window we think the event should go
4841 // to. Note: we retrieve mLastWin outside of the lock, so
4842 // it may change before we lock. Thus we must check it again.
4843 WindowState targetWin = mLastWin;
4844 boolean targetIsNew = targetWin == null;
4845 if (DEBUG_INPUT) Log.v(
4846 TAG, "waitForLastKey: mFinished=" + mFinished +
4847 ", mLastWin=" + mLastWin);
4848 if (targetIsNew) {
4849 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004850 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004851 if (target == SKIP_TARGET_TOKEN) {
4852 // The user has pressed a special key, and we are
4853 // dropping all pending events before it.
4854 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4855 + " " + nextMotion);
4856 return null;
4857 }
4858 if (target == CONSUMED_EVENT_TOKEN) {
4859 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4860 + " " + nextMotion);
4861 return target;
4862 }
4863 targetWin = (WindowState)target;
4864 }
Romain Guy06882f82009-06-10 13:36:04 -07004865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 // Now: is it okay to send the next event to this window?
4869 synchronized (this) {
4870 // First: did we come here based on the last window not
4871 // being null, but it changed by the time we got here?
4872 // If so, try again.
4873 if (!targetIsNew && mLastWin == null) {
4874 continue;
4875 }
Romain Guy06882f82009-06-10 13:36:04 -07004876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 // We never dispatch events if not finished with the
4878 // last one, or the display is frozen.
4879 if (mFinished && !mDisplayFrozen) {
4880 // If event dispatching is disabled, then we
4881 // just consume the events.
4882 if (!mEventDispatching) {
4883 if (DEBUG_INPUT) Log.v(TAG,
4884 "Skipping event; dispatching disabled: "
4885 + nextKey + " " + nextMotion);
4886 return null;
4887 }
4888 if (targetWin != null) {
4889 // If this is a new target, and that target is not
4890 // paused or unresponsive, then all looks good to
4891 // handle the event.
4892 if (targetIsNew && !targetWin.mToken.paused) {
4893 return targetWin;
4894 }
Romain Guy06882f82009-06-10 13:36:04 -07004895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896 // If we didn't find a target window, and there is no
4897 // focused app window, then just eat the events.
4898 } else if (mFocusedApp == null) {
4899 if (DEBUG_INPUT) Log.v(TAG,
4900 "Skipping event; no focused app: "
4901 + nextKey + " " + nextMotion);
4902 return null;
4903 }
4904 }
Romain Guy06882f82009-06-10 13:36:04 -07004905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004906 if (DEBUG_INPUT) Log.v(
4907 TAG, "Waiting for last key in " + mLastBinder
4908 + " target=" + targetWin
4909 + " mFinished=" + mFinished
4910 + " mDisplayFrozen=" + mDisplayFrozen
4911 + " targetIsNew=" + targetIsNew
4912 + " paused="
4913 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004914 + " mFocusedApp=" + mFocusedApp
4915 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004917 targetApp = targetWin != null
4918 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004920 long curTimeout = keyDispatchingTimeout;
4921 if (mTimeToSwitch != 0) {
4922 long now = SystemClock.uptimeMillis();
4923 if (mTimeToSwitch <= now) {
4924 // If an app switch key has been pressed, and we have
4925 // waited too long for the current app to finish
4926 // processing keys, then wait no more!
4927 doFinishedKeyLocked(true);
4928 continue;
4929 }
4930 long switchTimeout = mTimeToSwitch - now;
4931 if (curTimeout > switchTimeout) {
4932 curTimeout = switchTimeout;
4933 }
4934 }
Romain Guy06882f82009-06-10 13:36:04 -07004935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004936 try {
4937 // after that continue
4938 // processing keys, so we don't get stuck.
4939 if (DEBUG_INPUT) Log.v(
4940 TAG, "Waiting for key dispatch: " + curTimeout);
4941 wait(curTimeout);
4942 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4943 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004944 + startTime + " switchTime=" + mTimeToSwitch
4945 + " target=" + targetWin + " mLW=" + mLastWin
4946 + " mLB=" + mLastBinder + " fin=" + mFinished
4947 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004948 } catch (InterruptedException e) {
4949 }
4950 }
4951
4952 // If we were frozen during configuration change, restart the
4953 // timeout checks from now; otherwise look at whether we timed
4954 // out before awakening.
4955 if (mWasFrozen) {
4956 waitedFor = 0;
4957 mWasFrozen = false;
4958 } else {
4959 waitedFor = SystemClock.uptimeMillis() - startTime;
4960 }
4961
4962 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4963 IApplicationToken at = null;
4964 synchronized (this) {
4965 Log.w(TAG, "Key dispatching timed out sending to " +
4966 (targetWin != null ? targetWin.mAttrs.getTitle()
4967 : "<null>"));
4968 // NOSHIP debugging
4969 Log.w(TAG, "Dispatch state: " + mDispatchState);
4970 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4971 // END NOSHIP
4972 //dump();
4973 if (targetWin != null) {
4974 at = targetWin.getAppToken();
4975 } else if (targetApp != null) {
4976 at = targetApp.appToken;
4977 }
4978 }
4979
4980 boolean abort = true;
4981 if (at != null) {
4982 try {
4983 long timeout = at.getKeyDispatchingTimeout();
4984 if (timeout > waitedFor) {
4985 // we did not wait the proper amount of time for this application.
4986 // set the timeout to be the real timeout and wait again.
4987 keyDispatchingTimeout = timeout - waitedFor;
4988 continue;
4989 } else {
4990 abort = at.keyDispatchingTimedOut();
4991 }
4992 } catch (RemoteException ex) {
4993 }
4994 }
4995
4996 synchronized (this) {
4997 if (abort && (mLastWin == targetWin || targetWin == null)) {
4998 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004999 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 if (DEBUG_INPUT) Log.v(TAG,
5001 "Window " + mLastWin +
5002 " timed out on key input");
5003 if (mLastWin.mToken.paused) {
5004 Log.w(TAG, "Un-pausing dispatching to this window");
5005 mLastWin.mToken.paused = false;
5006 }
5007 }
5008 if (mMotionTarget == targetWin) {
5009 mMotionTarget = null;
5010 }
5011 mLastWin = null;
5012 mLastBinder = null;
5013 if (failIfTimeout || targetWin == null) {
5014 return null;
5015 }
5016 } else {
5017 Log.w(TAG, "Continuing to wait for key to be dispatched");
5018 startTime = SystemClock.uptimeMillis();
5019 }
5020 }
5021 }
5022 }
5023 }
Romain Guy06882f82009-06-10 13:36:04 -07005024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005025 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005026 MotionEvent nextMotion, boolean isPointerEvent,
5027 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005030 if (nextKey != null) {
5031 // Find the target window for a normal key event.
5032 final int keycode = nextKey.getKeyCode();
5033 final int repeatCount = nextKey.getRepeatCount();
5034 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5035 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005038 if (callingUid == 0 ||
5039 mContext.checkPermission(
5040 android.Manifest.permission.INJECT_EVENTS,
5041 callingPid, callingUid)
5042 == PackageManager.PERMISSION_GRANTED) {
5043 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005044 nextKey.getMetaState(), down, repeatCount,
5045 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047 Log.w(TAG, "Event timeout during app switch: dropping "
5048 + nextKey);
5049 return SKIP_TARGET_TOKEN;
5050 }
Romain Guy06882f82009-06-10 13:36:04 -07005051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005054 WindowState focus = null;
5055 synchronized(mWindowMap) {
5056 focus = getFocusedWindowLocked();
5057 }
Romain Guy06882f82009-06-10 13:36:04 -07005058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005060
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005061 if (callingUid == 0 ||
5062 (focus != null && callingUid == focus.mSession.mUid) ||
5063 mContext.checkPermission(
5064 android.Manifest.permission.INJECT_EVENTS,
5065 callingPid, callingUid)
5066 == PackageManager.PERMISSION_GRANTED) {
5067 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005068 keycode, nextKey.getMetaState(), down, repeatCount,
5069 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005070 return CONSUMED_EVENT_TOKEN;
5071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 }
Romain Guy06882f82009-06-10 13:36:04 -07005073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 } else if (!isPointerEvent) {
5077 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5078 if (!dispatch) {
5079 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5080 + nextMotion);
5081 return SKIP_TARGET_TOKEN;
5082 }
Romain Guy06882f82009-06-10 13:36:04 -07005083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005084 WindowState focus = null;
5085 synchronized(mWindowMap) {
5086 focus = getFocusedWindowLocked();
5087 }
Romain Guy06882f82009-06-10 13:36:04 -07005088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5090 return focus;
5091 }
Romain Guy06882f82009-06-10 13:36:04 -07005092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005093 if (nextMotion == null) {
5094 return SKIP_TARGET_TOKEN;
5095 }
Romain Guy06882f82009-06-10 13:36:04 -07005096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5098 KeyEvent.KEYCODE_UNKNOWN);
5099 if (!dispatch) {
5100 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5101 + nextMotion);
5102 return SKIP_TARGET_TOKEN;
5103 }
Romain Guy06882f82009-06-10 13:36:04 -07005104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 // Find the target window for a pointer event.
5106 int action = nextMotion.getAction();
5107 final float xf = nextMotion.getX();
5108 final float yf = nextMotion.getY();
5109 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111 final boolean screenWasOff = qev != null
5112 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 synchronized(mWindowMap) {
5117 synchronized (this) {
5118 if (action == MotionEvent.ACTION_DOWN) {
5119 if (mMotionTarget != null) {
5120 // this is weird, we got a pen down, but we thought it was
5121 // already down!
5122 // XXX: We should probably send an ACTION_UP to the current
5123 // target.
5124 Log.w(TAG, "Pointer down received while already down in: "
5125 + mMotionTarget);
5126 mMotionTarget = null;
5127 }
Romain Guy06882f82009-06-10 13:36:04 -07005128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 // ACTION_DOWN is special, because we need to lock next events to
5130 // the window we'll land onto.
5131 final int x = (int)xf;
5132 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005134 final ArrayList windows = mWindows;
5135 final int N = windows.size();
5136 WindowState topErrWindow = null;
5137 final Rect tmpRect = mTempRect;
5138 for (int i=N-1; i>=0; i--) {
5139 WindowState child = (WindowState)windows.get(i);
5140 //Log.i(TAG, "Checking dispatch to: " + child);
5141 final int flags = child.mAttrs.flags;
5142 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5143 if (topErrWindow == null) {
5144 topErrWindow = child;
5145 }
5146 }
5147 if (!child.isVisibleLw()) {
5148 //Log.i(TAG, "Not visible!");
5149 continue;
5150 }
5151 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5152 //Log.i(TAG, "Not touchable!");
5153 if ((flags & WindowManager.LayoutParams
5154 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5155 child.mNextOutsideTouch = mOutsideTouchTargets;
5156 mOutsideTouchTargets = child;
5157 }
5158 continue;
5159 }
5160 tmpRect.set(child.mFrame);
5161 if (child.mTouchableInsets == ViewTreeObserver
5162 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5163 // The touch is inside of the window if it is
5164 // inside the frame, AND the content part of that
5165 // frame that was given by the application.
5166 tmpRect.left += child.mGivenContentInsets.left;
5167 tmpRect.top += child.mGivenContentInsets.top;
5168 tmpRect.right -= child.mGivenContentInsets.right;
5169 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5170 } else if (child.mTouchableInsets == ViewTreeObserver
5171 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5172 // The touch is inside of the window if it is
5173 // inside the frame, AND the visible part of that
5174 // frame that was given by the application.
5175 tmpRect.left += child.mGivenVisibleInsets.left;
5176 tmpRect.top += child.mGivenVisibleInsets.top;
5177 tmpRect.right -= child.mGivenVisibleInsets.right;
5178 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5179 }
5180 final int touchFlags = flags &
5181 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5182 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5183 if (tmpRect.contains(x, y) || touchFlags == 0) {
5184 //Log.i(TAG, "Using this target!");
5185 if (!screenWasOff || (flags &
5186 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5187 mMotionTarget = child;
5188 } else {
5189 //Log.i(TAG, "Waking, skip!");
5190 mMotionTarget = null;
5191 }
5192 break;
5193 }
Romain Guy06882f82009-06-10 13:36:04 -07005194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 if ((flags & WindowManager.LayoutParams
5196 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5197 child.mNextOutsideTouch = mOutsideTouchTargets;
5198 mOutsideTouchTargets = child;
5199 //Log.i(TAG, "Adding to outside target list: " + child);
5200 }
5201 }
5202
5203 // if there's an error window but it's not accepting
5204 // focus (typically because it is not yet visible) just
5205 // wait for it -- any other focused window may in fact
5206 // be in ANR state.
5207 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5208 mMotionTarget = null;
5209 }
5210 }
Romain Guy06882f82009-06-10 13:36:04 -07005211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 target = mMotionTarget;
5213 }
5214 }
Romain Guy06882f82009-06-10 13:36:04 -07005215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 // Pointer events are a little different -- if there isn't a
5219 // target found for any event, then just drop it.
5220 return target != null ? target : SKIP_TARGET_TOKEN;
5221 }
Romain Guy06882f82009-06-10 13:36:04 -07005222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 boolean checkShouldDispatchKey(int keycode) {
5224 synchronized (this) {
5225 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5226 mTimeToSwitch = 0;
5227 return true;
5228 }
5229 if (mTimeToSwitch != 0
5230 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5231 return false;
5232 }
5233 return true;
5234 }
5235 }
Romain Guy06882f82009-06-10 13:36:04 -07005236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 void bindTargetWindowLocked(WindowState win,
5238 int pendingWhat, QueuedEvent pendingMotion) {
5239 synchronized (this) {
5240 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5241 }
5242 }
Romain Guy06882f82009-06-10 13:36:04 -07005243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 void bindTargetWindowLocked(WindowState win) {
5245 synchronized (this) {
5246 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5247 }
5248 }
5249
5250 void bindTargetWindowLockedLocked(WindowState win,
5251 int pendingWhat, QueuedEvent pendingMotion) {
5252 mLastWin = win;
5253 mLastBinder = win.mClient.asBinder();
5254 mFinished = false;
5255 if (pendingMotion != null) {
5256 final Session s = win.mSession;
5257 if (pendingWhat == RETURN_PENDING_POINTER) {
5258 releasePendingPointerLocked(s);
5259 s.mPendingPointerMove = pendingMotion;
5260 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005261 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 "bindTargetToWindow " + s.mPendingPointerMove);
5263 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5264 releasePendingTrackballLocked(s);
5265 s.mPendingTrackballMove = pendingMotion;
5266 s.mPendingTrackballWindow = win;
5267 }
5268 }
5269 }
Romain Guy06882f82009-06-10 13:36:04 -07005270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 void releasePendingPointerLocked(Session s) {
5272 if (DEBUG_INPUT) Log.v(TAG,
5273 "releasePendingPointer " + s.mPendingPointerMove);
5274 if (s.mPendingPointerMove != null) {
5275 mQueue.recycleEvent(s.mPendingPointerMove);
5276 s.mPendingPointerMove = null;
5277 }
5278 }
Romain Guy06882f82009-06-10 13:36:04 -07005279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005280 void releasePendingTrackballLocked(Session s) {
5281 if (s.mPendingTrackballMove != null) {
5282 mQueue.recycleEvent(s.mPendingTrackballMove);
5283 s.mPendingTrackballMove = null;
5284 }
5285 }
Romain Guy06882f82009-06-10 13:36:04 -07005286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005287 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5288 int returnWhat) {
5289 if (DEBUG_INPUT) Log.v(
5290 TAG, "finishedKey: client=" + client + ", force=" + force);
5291
5292 if (client == null) {
5293 return null;
5294 }
5295
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005296 MotionEvent res = null;
5297 QueuedEvent qev = null;
5298 WindowState win = null;
5299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 synchronized (this) {
5301 if (DEBUG_INPUT) Log.v(
5302 TAG, "finishedKey: client=" + client.asBinder()
5303 + ", force=" + force + ", last=" + mLastBinder
5304 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 if (returnWhat == RETURN_PENDING_POINTER) {
5307 qev = session.mPendingPointerMove;
5308 win = session.mPendingPointerWindow;
5309 session.mPendingPointerMove = null;
5310 session.mPendingPointerWindow = null;
5311 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5312 qev = session.mPendingTrackballMove;
5313 win = session.mPendingTrackballWindow;
5314 session.mPendingTrackballMove = null;
5315 session.mPendingTrackballWindow = null;
5316 }
Romain Guy06882f82009-06-10 13:36:04 -07005317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 if (mLastBinder == client.asBinder()) {
5319 if (DEBUG_INPUT) Log.v(
5320 TAG, "finishedKey: last paused="
5321 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5322 if (mLastWin != null && (!mLastWin.mToken.paused || force
5323 || !mEventDispatching)) {
5324 doFinishedKeyLocked(false);
5325 } else {
5326 // Make sure to wake up anyone currently waiting to
5327 // dispatch a key, so they can re-evaluate their
5328 // current situation.
5329 mFinished = true;
5330 notifyAll();
5331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 }
Romain Guy06882f82009-06-10 13:36:04 -07005333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005335 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 if (DEBUG_INPUT) Log.v(TAG,
5337 "Returning pending motion: " + res);
5338 mQueue.recycleEvent(qev);
5339 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5340 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005344
5345 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5346 synchronized (mWindowMap) {
5347 if (mWallpaperTarget == win) {
5348 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5349 }
5350 }
5351 }
5352
5353 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 }
5355
5356 void tickle() {
5357 synchronized (this) {
5358 notifyAll();
5359 }
5360 }
Romain Guy06882f82009-06-10 13:36:04 -07005361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 void handleNewWindowLocked(WindowState newWindow) {
5363 if (!newWindow.canReceiveKeys()) {
5364 return;
5365 }
5366 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005367 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 TAG, "New key dispatch window: win="
5369 + newWindow.mClient.asBinder()
5370 + ", last=" + mLastBinder
5371 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5372 + "), finished=" + mFinished + ", paused="
5373 + newWindow.mToken.paused);
5374
5375 // Displaying a window implicitly causes dispatching to
5376 // be unpaused. (This is to protect against bugs if someone
5377 // pauses dispatching but forgets to resume.)
5378 newWindow.mToken.paused = false;
5379
5380 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381
5382 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5383 if (DEBUG_INPUT) Log.v(TAG,
5384 "New SYSTEM_ERROR window; resetting state");
5385 mLastWin = null;
5386 mLastBinder = null;
5387 mMotionTarget = null;
5388 mFinished = true;
5389 } else if (mLastWin != null) {
5390 // If the new window is above the window we are
5391 // waiting on, then stop waiting and let key dispatching
5392 // start on the new guy.
5393 if (DEBUG_INPUT) Log.v(
5394 TAG, "Last win layer=" + mLastWin.mLayer
5395 + ", new win layer=" + newWindow.mLayer);
5396 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005397 // The new window is above the old; finish pending input to the last
5398 // window and start directing it to the new one.
5399 mLastWin.mToken.paused = false;
5400 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005402 // Either the new window is lower, so there is no need to wake key waiters,
5403 // or we just finished key input to the previous window, which implicitly
5404 // notified the key waiters. In both cases, we don't need to issue the
5405 // notification here.
5406 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 }
5408
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005409 // Now that we've put a new window state in place, make the event waiter
5410 // take notice and retarget its attentions.
5411 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 }
5413 }
5414
5415 void pauseDispatchingLocked(WindowToken token) {
5416 synchronized (this)
5417 {
5418 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5419 token.paused = true;
5420
5421 /*
5422 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5423 mPaused = true;
5424 } else {
5425 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005426 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005427 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005428 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005429 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005430 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 }
5432 }
5433 */
5434 }
5435 }
5436
5437 void resumeDispatchingLocked(WindowToken token) {
5438 synchronized (this) {
5439 if (token.paused) {
5440 if (DEBUG_INPUT) Log.v(
5441 TAG, "Resuming WindowToken " + token
5442 + ", last=" + mLastBinder
5443 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5444 + "), finished=" + mFinished + ", paused="
5445 + token.paused);
5446 token.paused = false;
5447 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5448 doFinishedKeyLocked(true);
5449 } else {
5450 notifyAll();
5451 }
5452 }
5453 }
5454 }
5455
5456 void setEventDispatchingLocked(boolean enabled) {
5457 synchronized (this) {
5458 mEventDispatching = enabled;
5459 notifyAll();
5460 }
5461 }
Romain Guy06882f82009-06-10 13:36:04 -07005462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 void appSwitchComing() {
5464 synchronized (this) {
5465 // Don't wait for more than .5 seconds for app to finish
5466 // processing the pending events.
5467 long now = SystemClock.uptimeMillis() + 500;
5468 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5469 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5470 mTimeToSwitch = now;
5471 }
5472 notifyAll();
5473 }
5474 }
Romain Guy06882f82009-06-10 13:36:04 -07005475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 private final void doFinishedKeyLocked(boolean doRecycle) {
5477 if (mLastWin != null) {
5478 releasePendingPointerLocked(mLastWin.mSession);
5479 releasePendingTrackballLocked(mLastWin.mSession);
5480 }
Romain Guy06882f82009-06-10 13:36:04 -07005481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 if (mLastWin == null || !mLastWin.mToken.paused
5483 || !mLastWin.isVisibleLw()) {
5484 // If the current window has been paused, we aren't -really-
5485 // finished... so let the waiters still wait.
5486 mLastWin = null;
5487 mLastBinder = null;
5488 }
5489 mFinished = true;
5490 notifyAll();
5491 }
5492 }
5493
5494 private class KeyQ extends KeyInputQueue
5495 implements KeyInputQueue.FilterCallback {
5496 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005499 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5501 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5502 "KEEP_SCREEN_ON_FLAG");
5503 mHoldingScreen.setReferenceCounted(false);
5504 }
5505
5506 @Override
5507 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5508 if (mPolicy.preprocessInputEventTq(event)) {
5509 return true;
5510 }
Romain Guy06882f82009-06-10 13:36:04 -07005511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 switch (event.type) {
5513 case RawInputEvent.EV_KEY: {
5514 // XXX begin hack
5515 if (DEBUG) {
5516 if (event.keycode == KeyEvent.KEYCODE_G) {
5517 if (event.value != 0) {
5518 // G down
5519 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5520 }
5521 return false;
5522 }
5523 if (event.keycode == KeyEvent.KEYCODE_D) {
5524 if (event.value != 0) {
5525 //dump();
5526 }
5527 return false;
5528 }
5529 }
5530 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 boolean screenIsOff = !mPowerManager.screenIsOn();
5533 boolean screenIsDim = !mPowerManager.screenIsBright();
5534 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5537 mPowerManager.goToSleep(event.when);
5538 }
5539
5540 if (screenIsOff) {
5541 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5542 }
5543 if (screenIsDim) {
5544 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5545 }
5546 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5547 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005548 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005549 }
Romain Guy06882f82009-06-10 13:36:04 -07005550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5552 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5553 filterQueue(this);
5554 mKeyWaiter.appSwitchComing();
5555 }
5556 return true;
5557 } else {
5558 return false;
5559 }
5560 }
Romain Guy06882f82009-06-10 13:36:04 -07005561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562 case RawInputEvent.EV_REL: {
5563 boolean screenIsOff = !mPowerManager.screenIsOn();
5564 boolean screenIsDim = !mPowerManager.screenIsBright();
5565 if (screenIsOff) {
5566 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5567 device.classes, event)) {
5568 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5569 return false;
5570 }
5571 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5572 }
5573 if (screenIsDim) {
5574 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5575 }
5576 return true;
5577 }
Romain Guy06882f82009-06-10 13:36:04 -07005578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 case RawInputEvent.EV_ABS: {
5580 boolean screenIsOff = !mPowerManager.screenIsOn();
5581 boolean screenIsDim = !mPowerManager.screenIsBright();
5582 if (screenIsOff) {
5583 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5584 device.classes, event)) {
5585 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5586 return false;
5587 }
5588 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5589 }
5590 if (screenIsDim) {
5591 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5592 }
5593 return true;
5594 }
Romain Guy06882f82009-06-10 13:36:04 -07005595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 default:
5597 return true;
5598 }
5599 }
5600
5601 public int filterEvent(QueuedEvent ev) {
5602 switch (ev.classType) {
5603 case RawInputEvent.CLASS_KEYBOARD:
5604 KeyEvent ke = (KeyEvent)ev.event;
5605 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5606 Log.w(TAG, "Dropping movement key during app switch: "
5607 + ke.getKeyCode() + ", action=" + ke.getAction());
5608 return FILTER_REMOVE;
5609 }
5610 return FILTER_ABORT;
5611 default:
5612 return FILTER_KEEP;
5613 }
5614 }
Romain Guy06882f82009-06-10 13:36:04 -07005615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616 /**
5617 * Must be called with the main window manager lock held.
5618 */
5619 void setHoldScreenLocked(boolean holding) {
5620 boolean state = mHoldingScreen.isHeld();
5621 if (holding != state) {
5622 if (holding) {
5623 mHoldingScreen.acquire();
5624 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005625 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 mHoldingScreen.release();
5627 }
5628 }
5629 }
Michael Chan53071d62009-05-13 17:29:48 -07005630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005631
5632 public boolean detectSafeMode() {
5633 mSafeMode = mPolicy.detectSafeMode();
5634 return mSafeMode;
5635 }
Romain Guy06882f82009-06-10 13:36:04 -07005636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005637 public void systemReady() {
5638 mPolicy.systemReady();
5639 }
Romain Guy06882f82009-06-10 13:36:04 -07005640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005641 private final class InputDispatcherThread extends Thread {
5642 // Time to wait when there is nothing to do: 9999 seconds.
5643 static final int LONG_WAIT=9999*1000;
5644
5645 public InputDispatcherThread() {
5646 super("InputDispatcher");
5647 }
Romain Guy06882f82009-06-10 13:36:04 -07005648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649 @Override
5650 public void run() {
5651 while (true) {
5652 try {
5653 process();
5654 } catch (Exception e) {
5655 Log.e(TAG, "Exception in input dispatcher", e);
5656 }
5657 }
5658 }
Romain Guy06882f82009-06-10 13:36:04 -07005659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005660 private void process() {
5661 android.os.Process.setThreadPriority(
5662 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 // The last key event we saw
5665 KeyEvent lastKey = null;
5666
5667 // Last keydown time for auto-repeating keys
5668 long lastKeyTime = SystemClock.uptimeMillis();
5669 long nextKeyTime = lastKeyTime+LONG_WAIT;
5670
Romain Guy06882f82009-06-10 13:36:04 -07005671 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672 int keyRepeatCount = 0;
5673
5674 // Need to report that configuration has changed?
5675 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 while (true) {
5678 long curTime = SystemClock.uptimeMillis();
5679
5680 if (DEBUG_INPUT) Log.v(
5681 TAG, "Waiting for next key: now=" + curTime
5682 + ", repeat @ " + nextKeyTime);
5683
5684 // Retrieve next event, waiting only as long as the next
5685 // repeat timeout. If the configuration has changed, then
5686 // don't wait at all -- we'll report the change as soon as
5687 // we have processed all events.
5688 QueuedEvent ev = mQueue.getEvent(
5689 (int)((!configChanged && curTime < nextKeyTime)
5690 ? (nextKeyTime-curTime) : 0));
5691
5692 if (DEBUG_INPUT && ev != null) Log.v(
5693 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5694
Michael Chan53071d62009-05-13 17:29:48 -07005695 if (MEASURE_LATENCY) {
5696 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5697 }
5698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 try {
5700 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005701 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 int eventType;
5703 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5704 eventType = eventType((MotionEvent)ev.event);
5705 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5706 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5707 eventType = LocalPowerManager.BUTTON_EVENT;
5708 } else {
5709 eventType = LocalPowerManager.OTHER_EVENT;
5710 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005711 try {
Michael Chan53071d62009-05-13 17:29:48 -07005712 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005713 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005714 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005715 mBatteryStats.noteInputEvent();
5716 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005717 } catch (RemoteException e) {
5718 // Ignore
5719 }
Michael Chane10de972009-05-18 11:24:50 -07005720
5721 if (eventType != TOUCH_EVENT
5722 && eventType != LONG_TOUCH_EVENT
5723 && eventType != CHEEK_EVENT) {
5724 mPowerManager.userActivity(curTime, false,
5725 eventType, false);
5726 } else if (mLastTouchEventType != eventType
5727 || (curTime - mLastUserActivityCallTime)
5728 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5729 mLastUserActivityCallTime = curTime;
5730 mLastTouchEventType = eventType;
5731 mPowerManager.userActivity(curTime, false,
5732 eventType, false);
5733 }
5734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 switch (ev.classType) {
5736 case RawInputEvent.CLASS_KEYBOARD:
5737 KeyEvent ke = (KeyEvent)ev.event;
5738 if (ke.isDown()) {
5739 lastKey = ke;
5740 keyRepeatCount = 0;
5741 lastKeyTime = curTime;
5742 nextKeyTime = lastKeyTime
5743 + KEY_REPEAT_FIRST_DELAY;
5744 if (DEBUG_INPUT) Log.v(
5745 TAG, "Received key down: first repeat @ "
5746 + nextKeyTime);
5747 } else {
5748 lastKey = null;
5749 // Arbitrary long timeout.
5750 lastKeyTime = curTime;
5751 nextKeyTime = curTime + LONG_WAIT;
5752 if (DEBUG_INPUT) Log.v(
5753 TAG, "Received key up: ignore repeat @ "
5754 + nextKeyTime);
5755 }
5756 dispatchKey((KeyEvent)ev.event, 0, 0);
5757 mQueue.recycleEvent(ev);
5758 break;
5759 case RawInputEvent.CLASS_TOUCHSCREEN:
5760 //Log.i(TAG, "Read next event " + ev);
5761 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5762 break;
5763 case RawInputEvent.CLASS_TRACKBALL:
5764 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5765 break;
5766 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5767 configChanged = true;
5768 break;
5769 default:
5770 mQueue.recycleEvent(ev);
5771 break;
5772 }
Romain Guy06882f82009-06-10 13:36:04 -07005773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 } else if (configChanged) {
5775 configChanged = false;
5776 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005778 } else if (lastKey != null) {
5779 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 // Timeout occurred while key was down. If it is at or
5782 // past the key repeat time, dispatch the repeat.
5783 if (DEBUG_INPUT) Log.v(
5784 TAG, "Key timeout: repeat=" + nextKeyTime
5785 + ", now=" + curTime);
5786 if (curTime < nextKeyTime) {
5787 continue;
5788 }
Romain Guy06882f82009-06-10 13:36:04 -07005789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 lastKeyTime = nextKeyTime;
5791 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5792 keyRepeatCount++;
5793 if (DEBUG_INPUT) Log.v(
5794 TAG, "Key repeat: count=" + keyRepeatCount
5795 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005796 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 } else {
5799 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005801 lastKeyTime = curTime;
5802 nextKeyTime = curTime + LONG_WAIT;
5803 }
Romain Guy06882f82009-06-10 13:36:04 -07005804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 } catch (Exception e) {
5806 Log.e(TAG,
5807 "Input thread received uncaught exception: " + e, e);
5808 }
5809 }
5810 }
5811 }
5812
5813 // -------------------------------------------------------------
5814 // Client Session State
5815 // -------------------------------------------------------------
5816
5817 private final class Session extends IWindowSession.Stub
5818 implements IBinder.DeathRecipient {
5819 final IInputMethodClient mClient;
5820 final IInputContext mInputContext;
5821 final int mUid;
5822 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005823 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005824 SurfaceSession mSurfaceSession;
5825 int mNumWindow = 0;
5826 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 /**
5829 * Current pointer move event being dispatched to client window... must
5830 * hold key lock to access.
5831 */
5832 QueuedEvent mPendingPointerMove;
5833 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835 /**
5836 * Current trackball move event being dispatched to client window... must
5837 * hold key lock to access.
5838 */
5839 QueuedEvent mPendingTrackballMove;
5840 WindowState mPendingTrackballWindow;
5841
5842 public Session(IInputMethodClient client, IInputContext inputContext) {
5843 mClient = client;
5844 mInputContext = inputContext;
5845 mUid = Binder.getCallingUid();
5846 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005847 StringBuilder sb = new StringBuilder();
5848 sb.append("Session{");
5849 sb.append(Integer.toHexString(System.identityHashCode(this)));
5850 sb.append(" uid ");
5851 sb.append(mUid);
5852 sb.append("}");
5853 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 synchronized (mWindowMap) {
5856 if (mInputMethodManager == null && mHaveInputMethods) {
5857 IBinder b = ServiceManager.getService(
5858 Context.INPUT_METHOD_SERVICE);
5859 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5860 }
5861 }
5862 long ident = Binder.clearCallingIdentity();
5863 try {
5864 // Note: it is safe to call in to the input method manager
5865 // here because we are not holding our lock.
5866 if (mInputMethodManager != null) {
5867 mInputMethodManager.addClient(client, inputContext,
5868 mUid, mPid);
5869 } else {
5870 client.setUsingInputMethod(false);
5871 }
5872 client.asBinder().linkToDeath(this, 0);
5873 } catch (RemoteException e) {
5874 // The caller has died, so we can just forget about this.
5875 try {
5876 if (mInputMethodManager != null) {
5877 mInputMethodManager.removeClient(client);
5878 }
5879 } catch (RemoteException ee) {
5880 }
5881 } finally {
5882 Binder.restoreCallingIdentity(ident);
5883 }
5884 }
Romain Guy06882f82009-06-10 13:36:04 -07005885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 @Override
5887 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5888 throws RemoteException {
5889 try {
5890 return super.onTransact(code, data, reply, flags);
5891 } catch (RuntimeException e) {
5892 // Log all 'real' exceptions thrown to the caller
5893 if (!(e instanceof SecurityException)) {
5894 Log.e(TAG, "Window Session Crash", e);
5895 }
5896 throw e;
5897 }
5898 }
5899
5900 public void binderDied() {
5901 // Note: it is safe to call in to the input method manager
5902 // here because we are not holding our lock.
5903 try {
5904 if (mInputMethodManager != null) {
5905 mInputMethodManager.removeClient(mClient);
5906 }
5907 } catch (RemoteException e) {
5908 }
5909 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005910 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 mClientDead = true;
5912 killSessionLocked();
5913 }
5914 }
5915
5916 public int add(IWindow window, WindowManager.LayoutParams attrs,
5917 int viewVisibility, Rect outContentInsets) {
5918 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5919 }
Romain Guy06882f82009-06-10 13:36:04 -07005920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005921 public void remove(IWindow window) {
5922 removeWindow(this, window);
5923 }
Romain Guy06882f82009-06-10 13:36:04 -07005924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5926 int requestedWidth, int requestedHeight, int viewFlags,
5927 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5928 Rect outVisibleInsets, Surface outSurface) {
5929 return relayoutWindow(this, window, attrs,
5930 requestedWidth, requestedHeight, viewFlags, insetsPending,
5931 outFrame, outContentInsets, outVisibleInsets, outSurface);
5932 }
Romain Guy06882f82009-06-10 13:36:04 -07005933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 public void setTransparentRegion(IWindow window, Region region) {
5935 setTransparentRegionWindow(this, window, region);
5936 }
Romain Guy06882f82009-06-10 13:36:04 -07005937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005938 public void setInsets(IWindow window, int touchableInsets,
5939 Rect contentInsets, Rect visibleInsets) {
5940 setInsetsWindow(this, window, touchableInsets, contentInsets,
5941 visibleInsets);
5942 }
Romain Guy06882f82009-06-10 13:36:04 -07005943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5945 getWindowDisplayFrame(this, window, outDisplayFrame);
5946 }
Romain Guy06882f82009-06-10 13:36:04 -07005947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005948 public void finishDrawing(IWindow window) {
5949 if (localLOGV) Log.v(
5950 TAG, "IWindow finishDrawing called for " + window);
5951 finishDrawingWindow(this, window);
5952 }
5953
5954 public void finishKey(IWindow window) {
5955 if (localLOGV) Log.v(
5956 TAG, "IWindow finishKey called for " + window);
5957 mKeyWaiter.finishedKey(this, window, false,
5958 KeyWaiter.RETURN_NOTHING);
5959 }
5960
5961 public MotionEvent getPendingPointerMove(IWindow window) {
5962 if (localLOGV) Log.v(
5963 TAG, "IWindow getPendingMotionEvent called for " + window);
5964 return mKeyWaiter.finishedKey(this, window, false,
5965 KeyWaiter.RETURN_PENDING_POINTER);
5966 }
Romain Guy06882f82009-06-10 13:36:04 -07005967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005968 public MotionEvent getPendingTrackballMove(IWindow window) {
5969 if (localLOGV) Log.v(
5970 TAG, "IWindow getPendingMotionEvent called for " + window);
5971 return mKeyWaiter.finishedKey(this, window, false,
5972 KeyWaiter.RETURN_PENDING_TRACKBALL);
5973 }
5974
5975 public void setInTouchMode(boolean mode) {
5976 synchronized(mWindowMap) {
5977 mInTouchMode = mode;
5978 }
5979 }
5980
5981 public boolean getInTouchMode() {
5982 synchronized(mWindowMap) {
5983 return mInTouchMode;
5984 }
5985 }
5986
5987 public boolean performHapticFeedback(IWindow window, int effectId,
5988 boolean always) {
5989 synchronized(mWindowMap) {
5990 long ident = Binder.clearCallingIdentity();
5991 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005992 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 windowForClientLocked(this, window), effectId, always);
5994 } finally {
5995 Binder.restoreCallingIdentity(ident);
5996 }
5997 }
5998 }
Romain Guy06882f82009-06-10 13:36:04 -07005999
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006000 public void setWallpaperPosition(IBinder window, float x, float y) {
6001 synchronized(mWindowMap) {
6002 long ident = Binder.clearCallingIdentity();
6003 try {
6004 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6005 x, y);
6006 } finally {
6007 Binder.restoreCallingIdentity(ident);
6008 }
6009 }
6010 }
6011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 void windowAddedLocked() {
6013 if (mSurfaceSession == null) {
6014 if (localLOGV) Log.v(
6015 TAG, "First window added to " + this + ", creating SurfaceSession");
6016 mSurfaceSession = new SurfaceSession();
6017 mSessions.add(this);
6018 }
6019 mNumWindow++;
6020 }
6021
6022 void windowRemovedLocked() {
6023 mNumWindow--;
6024 killSessionLocked();
6025 }
Romain Guy06882f82009-06-10 13:36:04 -07006026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006027 void killSessionLocked() {
6028 if (mNumWindow <= 0 && mClientDead) {
6029 mSessions.remove(this);
6030 if (mSurfaceSession != null) {
6031 if (localLOGV) Log.v(
6032 TAG, "Last window removed from " + this
6033 + ", destroying " + mSurfaceSession);
6034 try {
6035 mSurfaceSession.kill();
6036 } catch (Exception e) {
6037 Log.w(TAG, "Exception thrown when killing surface session "
6038 + mSurfaceSession + " in session " + this
6039 + ": " + e.toString());
6040 }
6041 mSurfaceSession = null;
6042 }
6043 }
6044 }
Romain Guy06882f82009-06-10 13:36:04 -07006045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006046 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006047 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6048 pw.print(" mClientDead="); pw.print(mClientDead);
6049 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6050 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6051 pw.print(prefix);
6052 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6053 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6054 }
6055 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6056 pw.print(prefix);
6057 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6058 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006060 }
6061
6062 @Override
6063 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006064 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006065 }
6066 }
6067
6068 // -------------------------------------------------------------
6069 // Client Window State
6070 // -------------------------------------------------------------
6071
6072 private final class WindowState implements WindowManagerPolicy.WindowState {
6073 final Session mSession;
6074 final IWindow mClient;
6075 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006076 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006077 AppWindowToken mAppToken;
6078 AppWindowToken mTargetAppToken;
6079 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6080 final DeathRecipient mDeathRecipient;
6081 final WindowState mAttachedWindow;
6082 final ArrayList mChildWindows = new ArrayList();
6083 final int mBaseLayer;
6084 final int mSubLayer;
6085 final boolean mLayoutAttached;
6086 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006087 final boolean mIsWallpaper;
6088 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006089 int mViewVisibility;
6090 boolean mPolicyVisibility = true;
6091 boolean mPolicyVisibilityAfterAnim = true;
6092 boolean mAppFreezing;
6093 Surface mSurface;
6094 boolean mAttachedHidden; // is our parent window hidden?
6095 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006096 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006097 int mRequestedWidth;
6098 int mRequestedHeight;
6099 int mLastRequestedWidth;
6100 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 int mLayer;
6102 int mAnimLayer;
6103 int mLastLayer;
6104 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006105 boolean mObscured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006106
6107 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 // Actual frame shown on-screen (may be modified by animation)
6110 final Rect mShownFrame = new Rect();
6111 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006113 /**
6114 * Insets that determine the actually visible area
6115 */
6116 final Rect mVisibleInsets = new Rect();
6117 final Rect mLastVisibleInsets = new Rect();
6118 boolean mVisibleInsetsChanged;
6119
6120 /**
6121 * Insets that are covered by system windows
6122 */
6123 final Rect mContentInsets = new Rect();
6124 final Rect mLastContentInsets = new Rect();
6125 boolean mContentInsetsChanged;
6126
6127 /**
6128 * Set to true if we are waiting for this window to receive its
6129 * given internal insets before laying out other windows based on it.
6130 */
6131 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 /**
6134 * These are the content insets that were given during layout for
6135 * this window, to be applied to windows behind it.
6136 */
6137 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 /**
6140 * These are the visible insets that were given during layout for
6141 * this window, to be applied to windows behind it.
6142 */
6143 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 /**
6146 * Flag indicating whether the touchable region should be adjusted by
6147 * the visible insets; if false the area outside the visible insets is
6148 * NOT touchable, so we must use those to adjust the frame during hit
6149 * tests.
6150 */
6151 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 // Current transformation being applied.
6154 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6155 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6156 float mHScale=1, mVScale=1;
6157 float mLastHScale=1, mLastVScale=1;
6158 final Matrix mTmpMatrix = new Matrix();
6159
6160 // "Real" frame that the application sees.
6161 final Rect mFrame = new Rect();
6162 final Rect mLastFrame = new Rect();
6163
6164 final Rect mContainingFrame = new Rect();
6165 final Rect mDisplayFrame = new Rect();
6166 final Rect mContentFrame = new Rect();
6167 final Rect mVisibleFrame = new Rect();
6168
6169 float mShownAlpha = 1;
6170 float mAlpha = 1;
6171 float mLastAlpha = 1;
6172
6173 // Set to true if, when the window gets displayed, it should perform
6174 // an enter animation.
6175 boolean mEnterAnimationPending;
6176
6177 // Currently running animation.
6178 boolean mAnimating;
6179 boolean mLocalAnimating;
6180 Animation mAnimation;
6181 boolean mAnimationIsEntrance;
6182 boolean mHasTransformation;
6183 boolean mHasLocalTransformation;
6184 final Transformation mTransformation = new Transformation();
6185
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006186 // If a window showing a wallpaper: the requested offset for the
6187 // wallpaper; if a wallpaper window: the currently applied offset.
6188 float mWallpaperX = -1;
6189 float mWallpaperY = -1;
6190
6191 // Wallpaper windows: pixels offset based on above variables.
6192 int mXOffset;
6193 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195 // This is set after IWindowSession.relayout() has been called at
6196 // least once for the window. It allows us to detect the situation
6197 // where we don't yet have a surface, but should have one soon, so
6198 // we can give the window focus before waiting for the relayout.
6199 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 // This is set after the Surface has been created but before the
6202 // window has been drawn. During this time the surface is hidden.
6203 boolean mDrawPending;
6204
6205 // This is set after the window has finished drawing for the first
6206 // time but before its surface is shown. The surface will be
6207 // displayed when the next layout is run.
6208 boolean mCommitDrawPending;
6209
6210 // This is set during the time after the window's drawing has been
6211 // committed, and before its surface is actually shown. It is used
6212 // to delay showing the surface until all windows in a token are ready
6213 // to be shown.
6214 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006216 // Set when the window has been shown in the screen the first time.
6217 boolean mHasDrawn;
6218
6219 // Currently running an exit animation?
6220 boolean mExiting;
6221
6222 // Currently on the mDestroySurface list?
6223 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 // Completely remove from window manager after exit animation?
6226 boolean mRemoveOnExit;
6227
6228 // Set when the orientation is changing and this window has not yet
6229 // been updated for the new orientation.
6230 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006232 // Is this window now (or just being) removed?
6233 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006235 WindowState(Session s, IWindow c, WindowToken token,
6236 WindowState attachedWindow, WindowManager.LayoutParams a,
6237 int viewVisibility) {
6238 mSession = s;
6239 mClient = c;
6240 mToken = token;
6241 mAttrs.copyFrom(a);
6242 mViewVisibility = viewVisibility;
6243 DeathRecipient deathRecipient = new DeathRecipient();
6244 mAlpha = a.alpha;
6245 if (localLOGV) Log.v(
6246 TAG, "Window " + this + " client=" + c.asBinder()
6247 + " token=" + token + " (" + mAttrs.token + ")");
6248 try {
6249 c.asBinder().linkToDeath(deathRecipient, 0);
6250 } catch (RemoteException e) {
6251 mDeathRecipient = null;
6252 mAttachedWindow = null;
6253 mLayoutAttached = false;
6254 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006255 mIsWallpaper = false;
6256 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 mBaseLayer = 0;
6258 mSubLayer = 0;
6259 return;
6260 }
6261 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006263 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6264 mAttrs.type <= LAST_SUB_WINDOW)) {
6265 // The multiplier here is to reserve space for multiple
6266 // windows in the same type layer.
6267 mBaseLayer = mPolicy.windowTypeToLayerLw(
6268 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6269 + TYPE_LAYER_OFFSET;
6270 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6271 mAttachedWindow = attachedWindow;
6272 mAttachedWindow.mChildWindows.add(this);
6273 mLayoutAttached = mAttrs.type !=
6274 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6275 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6276 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006277 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6278 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 } else {
6280 // The multiplier here is to reserve space for multiple
6281 // windows in the same type layer.
6282 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6283 * TYPE_LAYER_MULTIPLIER
6284 + TYPE_LAYER_OFFSET;
6285 mSubLayer = 0;
6286 mAttachedWindow = null;
6287 mLayoutAttached = false;
6288 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6289 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006290 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6291 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 }
6293
6294 WindowState appWin = this;
6295 while (appWin.mAttachedWindow != null) {
6296 appWin = mAttachedWindow;
6297 }
6298 WindowToken appToken = appWin.mToken;
6299 while (appToken.appWindowToken == null) {
6300 WindowToken parent = mTokenMap.get(appToken.token);
6301 if (parent == null || appToken == parent) {
6302 break;
6303 }
6304 appToken = parent;
6305 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006306 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006307 mAppToken = appToken.appWindowToken;
6308
6309 mSurface = null;
6310 mRequestedWidth = 0;
6311 mRequestedHeight = 0;
6312 mLastRequestedWidth = 0;
6313 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006314 mXOffset = 0;
6315 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 mLayer = 0;
6317 mAnimLayer = 0;
6318 mLastLayer = 0;
6319 }
6320
6321 void attach() {
6322 if (localLOGV) Log.v(
6323 TAG, "Attaching " + this + " token=" + mToken
6324 + ", list=" + mToken.windows);
6325 mSession.windowAddedLocked();
6326 }
6327
6328 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6329 mHaveFrame = true;
6330
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006331 final Rect container = mContainingFrame;
6332 container.set(pf);
6333
6334 final Rect display = mDisplayFrame;
6335 display.set(df);
6336
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006337 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006338 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006339 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6340 display.intersect(mCompatibleScreenFrame);
6341 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006342 }
6343
6344 final int pw = container.right - container.left;
6345 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006346
6347 int w,h;
6348 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6349 w = mAttrs.width < 0 ? pw : mAttrs.width;
6350 h = mAttrs.height< 0 ? ph : mAttrs.height;
6351 } else {
6352 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6353 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6354 }
Romain Guy06882f82009-06-10 13:36:04 -07006355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 final Rect content = mContentFrame;
6357 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006359 final Rect visible = mVisibleFrame;
6360 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006362 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006363 final int fw = frame.width();
6364 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6367 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6368
6369 Gravity.apply(mAttrs.gravity, w, h, container,
6370 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6371 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6372
6373 //System.out.println("Out: " + mFrame);
6374
6375 // Now make sure the window fits in the overall display.
6376 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 // Make sure the content and visible frames are inside of the
6379 // final window frame.
6380 if (content.left < frame.left) content.left = frame.left;
6381 if (content.top < frame.top) content.top = frame.top;
6382 if (content.right > frame.right) content.right = frame.right;
6383 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6384 if (visible.left < frame.left) visible.left = frame.left;
6385 if (visible.top < frame.top) visible.top = frame.top;
6386 if (visible.right > frame.right) visible.right = frame.right;
6387 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006389 final Rect contentInsets = mContentInsets;
6390 contentInsets.left = content.left-frame.left;
6391 contentInsets.top = content.top-frame.top;
6392 contentInsets.right = frame.right-content.right;
6393 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 final Rect visibleInsets = mVisibleInsets;
6396 visibleInsets.left = visible.left-frame.left;
6397 visibleInsets.top = visible.top-frame.top;
6398 visibleInsets.right = frame.right-visible.right;
6399 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006400
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006401 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())
6402 && mWallpaperTarget != null) {
6403 updateWallpaperOffsetLocked(mWallpaperTarget, this,
6404 mDisplay.getWidth(), mDisplay.getHeight());
6405 }
6406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 if (localLOGV) {
6408 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6409 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6410 Log.v(TAG, "Resolving (mRequestedWidth="
6411 + mRequestedWidth + ", mRequestedheight="
6412 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6413 + "): frame=" + mFrame.toShortString()
6414 + " ci=" + contentInsets.toShortString()
6415 + " vi=" + visibleInsets.toShortString());
6416 //}
6417 }
6418 }
Romain Guy06882f82009-06-10 13:36:04 -07006419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 public Rect getFrameLw() {
6421 return mFrame;
6422 }
6423
6424 public Rect getShownFrameLw() {
6425 return mShownFrame;
6426 }
6427
6428 public Rect getDisplayFrameLw() {
6429 return mDisplayFrame;
6430 }
6431
6432 public Rect getContentFrameLw() {
6433 return mContentFrame;
6434 }
6435
6436 public Rect getVisibleFrameLw() {
6437 return mVisibleFrame;
6438 }
6439
6440 public boolean getGivenInsetsPendingLw() {
6441 return mGivenInsetsPending;
6442 }
6443
6444 public Rect getGivenContentInsetsLw() {
6445 return mGivenContentInsets;
6446 }
Romain Guy06882f82009-06-10 13:36:04 -07006447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 public Rect getGivenVisibleInsetsLw() {
6449 return mGivenVisibleInsets;
6450 }
Romain Guy06882f82009-06-10 13:36:04 -07006451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006452 public WindowManager.LayoutParams getAttrs() {
6453 return mAttrs;
6454 }
6455
6456 public int getSurfaceLayer() {
6457 return mLayer;
6458 }
Romain Guy06882f82009-06-10 13:36:04 -07006459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006460 public IApplicationToken getAppToken() {
6461 return mAppToken != null ? mAppToken.appToken : null;
6462 }
6463
6464 public boolean hasAppShownWindows() {
6465 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6466 }
6467
6468 public boolean hasAppStartingIcon() {
6469 return mAppToken != null ? (mAppToken.startingData != null) : false;
6470 }
6471
6472 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6473 return mAppToken != null ? mAppToken.startingWindow : null;
6474 }
6475
6476 public void setAnimation(Animation anim) {
6477 if (localLOGV) Log.v(
6478 TAG, "Setting animation in " + this + ": " + anim);
6479 mAnimating = false;
6480 mLocalAnimating = false;
6481 mAnimation = anim;
6482 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6483 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6484 }
6485
6486 public void clearAnimation() {
6487 if (mAnimation != null) {
6488 mAnimating = true;
6489 mLocalAnimating = false;
6490 mAnimation = null;
6491 }
6492 }
Romain Guy06882f82009-06-10 13:36:04 -07006493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 Surface createSurfaceLocked() {
6495 if (mSurface == null) {
6496 mDrawPending = true;
6497 mCommitDrawPending = false;
6498 mReadyToShow = false;
6499 if (mAppToken != null) {
6500 mAppToken.allDrawn = false;
6501 }
6502
6503 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006504 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 flags |= Surface.PUSH_BUFFERS;
6506 }
6507
6508 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6509 flags |= Surface.SECURE;
6510 }
6511 if (DEBUG_VISIBILITY) Log.v(
6512 TAG, "Creating surface in session "
6513 + mSession.mSurfaceSession + " window " + this
6514 + " w=" + mFrame.width()
6515 + " h=" + mFrame.height() + " format="
6516 + mAttrs.format + " flags=" + flags);
6517
6518 int w = mFrame.width();
6519 int h = mFrame.height();
6520 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6521 // for a scaled surface, we always want the requested
6522 // size.
6523 w = mRequestedWidth;
6524 h = mRequestedHeight;
6525 }
6526
6527 try {
6528 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006529 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006530 0, w, h, mAttrs.format, flags);
6531 } catch (Surface.OutOfResourcesException e) {
6532 Log.w(TAG, "OutOfResourcesException creating surface");
6533 reclaimSomeSurfaceMemoryLocked(this, "create");
6534 return null;
6535 } catch (Exception e) {
6536 Log.e(TAG, "Exception creating surface", e);
6537 return null;
6538 }
Romain Guy06882f82009-06-10 13:36:04 -07006539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 if (localLOGV) Log.v(
6541 TAG, "Got surface: " + mSurface
6542 + ", set left=" + mFrame.left + " top=" + mFrame.top
6543 + ", animLayer=" + mAnimLayer);
6544 if (SHOW_TRANSACTIONS) {
6545 Log.i(TAG, ">>> OPEN TRANSACTION");
6546 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6547 + mAttrs.getTitle() + ") pos=(" +
6548 mFrame.left + "," + mFrame.top + ") (" +
6549 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6550 mAnimLayer + " HIDE");
6551 }
6552 Surface.openTransaction();
6553 try {
6554 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006555 mSurface.setPosition(mFrame.left + mXOffset,
6556 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006557 mSurface.setLayer(mAnimLayer);
6558 mSurface.hide();
6559 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6560 mSurface.setFlags(Surface.SURFACE_DITHER,
6561 Surface.SURFACE_DITHER);
6562 }
6563 } catch (RuntimeException e) {
6564 Log.w(TAG, "Error creating surface in " + w, e);
6565 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6566 }
6567 mLastHidden = true;
6568 } finally {
6569 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6570 Surface.closeTransaction();
6571 }
6572 if (localLOGV) Log.v(
6573 TAG, "Created surface " + this);
6574 }
6575 return mSurface;
6576 }
Romain Guy06882f82009-06-10 13:36:04 -07006577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006578 void destroySurfaceLocked() {
6579 // Window is no longer on-screen, so can no longer receive
6580 // key events... if we were waiting for it to finish
6581 // handling a key event, the wait is over!
6582 mKeyWaiter.finishedKey(mSession, mClient, true,
6583 KeyWaiter.RETURN_NOTHING);
6584 mKeyWaiter.releasePendingPointerLocked(mSession);
6585 mKeyWaiter.releasePendingTrackballLocked(mSession);
6586
6587 if (mAppToken != null && this == mAppToken.startingWindow) {
6588 mAppToken.startingDisplayed = false;
6589 }
Romain Guy06882f82009-06-10 13:36:04 -07006590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 if (localLOGV) Log.v(
6592 TAG, "Window " + this
6593 + " destroying surface " + mSurface + ", session " + mSession);
6594 if (mSurface != null) {
6595 try {
6596 if (SHOW_TRANSACTIONS) {
6597 RuntimeException ex = new RuntimeException();
6598 ex.fillInStackTrace();
6599 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6600 + mAttrs.getTitle() + ")", ex);
6601 }
6602 mSurface.clear();
6603 } catch (RuntimeException e) {
6604 Log.w(TAG, "Exception thrown when destroying Window " + this
6605 + " surface " + mSurface + " session " + mSession
6606 + ": " + e.toString());
6607 }
6608 mSurface = null;
6609 mDrawPending = false;
6610 mCommitDrawPending = false;
6611 mReadyToShow = false;
6612
6613 int i = mChildWindows.size();
6614 while (i > 0) {
6615 i--;
6616 WindowState c = (WindowState)mChildWindows.get(i);
6617 c.mAttachedHidden = true;
6618 }
6619 }
6620 }
6621
6622 boolean finishDrawingLocked() {
6623 if (mDrawPending) {
6624 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6625 TAG, "finishDrawingLocked: " + mSurface);
6626 mCommitDrawPending = true;
6627 mDrawPending = false;
6628 return true;
6629 }
6630 return false;
6631 }
6632
6633 // This must be called while inside a transaction.
6634 void commitFinishDrawingLocked(long currentTime) {
6635 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6636 if (!mCommitDrawPending) {
6637 return;
6638 }
6639 mCommitDrawPending = false;
6640 mReadyToShow = true;
6641 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6642 final AppWindowToken atoken = mAppToken;
6643 if (atoken == null || atoken.allDrawn || starting) {
6644 performShowLocked();
6645 }
6646 }
6647
6648 // This must be called while inside a transaction.
6649 boolean performShowLocked() {
6650 if (DEBUG_VISIBILITY) {
6651 RuntimeException e = new RuntimeException();
6652 e.fillInStackTrace();
6653 Log.v(TAG, "performShow on " + this
6654 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6655 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6656 }
6657 if (mReadyToShow && isReadyForDisplay()) {
6658 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6659 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6660 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6661 + " during animation: policyVis=" + mPolicyVisibility
6662 + " attHidden=" + mAttachedHidden
6663 + " tok.hiddenRequested="
6664 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6665 + " tok.idden="
6666 + (mAppToken != null ? mAppToken.hidden : false)
6667 + " animating=" + mAnimating
6668 + " tok animating="
6669 + (mAppToken != null ? mAppToken.animating : false));
6670 if (!showSurfaceRobustlyLocked(this)) {
6671 return false;
6672 }
6673 mLastAlpha = -1;
6674 mHasDrawn = true;
6675 mLastHidden = false;
6676 mReadyToShow = false;
6677 enableScreenIfNeededLocked();
6678
6679 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006681 int i = mChildWindows.size();
6682 while (i > 0) {
6683 i--;
6684 WindowState c = (WindowState)mChildWindows.get(i);
6685 if (c.mSurface != null && c.mAttachedHidden) {
6686 c.mAttachedHidden = false;
6687 c.performShowLocked();
6688 }
6689 }
Romain Guy06882f82009-06-10 13:36:04 -07006690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006691 if (mAttrs.type != TYPE_APPLICATION_STARTING
6692 && mAppToken != null) {
6693 mAppToken.firstWindowDrawn = true;
6694 if (mAnimation == null && mAppToken.startingData != null) {
6695 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6696 + mToken
6697 + ": first real window is shown, no animation");
6698 mFinishedStarting.add(mAppToken);
6699 mH.sendEmptyMessage(H.FINISHED_STARTING);
6700 }
6701 mAppToken.updateReportedVisibilityLocked();
6702 }
6703 }
6704 return true;
6705 }
Romain Guy06882f82009-06-10 13:36:04 -07006706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006707 // This must be called while inside a transaction. Returns true if
6708 // there is more animation to run.
6709 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6710 if (!mDisplayFrozen) {
6711 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006713 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6714 mHasTransformation = true;
6715 mHasLocalTransformation = true;
6716 if (!mLocalAnimating) {
6717 if (DEBUG_ANIM) Log.v(
6718 TAG, "Starting animation in " + this +
6719 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6720 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6721 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6722 mAnimation.setStartTime(currentTime);
6723 mLocalAnimating = true;
6724 mAnimating = true;
6725 }
6726 mTransformation.clear();
6727 final boolean more = mAnimation.getTransformation(
6728 currentTime, mTransformation);
6729 if (DEBUG_ANIM) Log.v(
6730 TAG, "Stepped animation in " + this +
6731 ": more=" + more + ", xform=" + mTransformation);
6732 if (more) {
6733 // we're not done!
6734 return true;
6735 }
6736 if (DEBUG_ANIM) Log.v(
6737 TAG, "Finished animation in " + this +
6738 " @ " + currentTime);
6739 mAnimation = null;
6740 //WindowManagerService.this.dump();
6741 }
6742 mHasLocalTransformation = false;
6743 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6744 && mAppToken.hasTransformation) {
6745 // When our app token is animating, we kind-of pretend like
6746 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6747 // part of this check means that we will only do this if
6748 // our window is not currently exiting, or it is not
6749 // locally animating itself. The idea being that one that
6750 // is exiting and doing a local animation should be removed
6751 // once that animation is done.
6752 mAnimating = true;
6753 mHasTransformation = true;
6754 mTransformation.clear();
6755 return false;
6756 } else if (mHasTransformation) {
6757 // Little trick to get through the path below to act like
6758 // we have finished an animation.
6759 mAnimating = true;
6760 } else if (isAnimating()) {
6761 mAnimating = true;
6762 }
6763 } else if (mAnimation != null) {
6764 // If the display is frozen, and there is a pending animation,
6765 // clear it and make sure we run the cleanup code.
6766 mAnimating = true;
6767 mLocalAnimating = true;
6768 mAnimation = null;
6769 }
Romain Guy06882f82009-06-10 13:36:04 -07006770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 if (!mAnimating && !mLocalAnimating) {
6772 return false;
6773 }
6774
6775 if (DEBUG_ANIM) Log.v(
6776 TAG, "Animation done in " + this + ": exiting=" + mExiting
6777 + ", reportedVisible="
6778 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006780 mAnimating = false;
6781 mLocalAnimating = false;
6782 mAnimation = null;
6783 mAnimLayer = mLayer;
6784 if (mIsImWindow) {
6785 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006786 } else if (mIsWallpaper) {
6787 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 }
6789 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6790 + " anim layer: " + mAnimLayer);
6791 mHasTransformation = false;
6792 mHasLocalTransformation = false;
6793 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6794 mTransformation.clear();
6795 if (mHasDrawn
6796 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6797 && mAppToken != null
6798 && mAppToken.firstWindowDrawn
6799 && mAppToken.startingData != null) {
6800 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6801 + mToken + ": first real window done animating");
6802 mFinishedStarting.add(mAppToken);
6803 mH.sendEmptyMessage(H.FINISHED_STARTING);
6804 }
Romain Guy06882f82009-06-10 13:36:04 -07006805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006806 finishExit();
6807
6808 if (mAppToken != null) {
6809 mAppToken.updateReportedVisibilityLocked();
6810 }
6811
6812 return false;
6813 }
6814
6815 void finishExit() {
6816 if (DEBUG_ANIM) Log.v(
6817 TAG, "finishExit in " + this
6818 + ": exiting=" + mExiting
6819 + " remove=" + mRemoveOnExit
6820 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 final int N = mChildWindows.size();
6823 for (int i=0; i<N; i++) {
6824 ((WindowState)mChildWindows.get(i)).finishExit();
6825 }
Romain Guy06882f82009-06-10 13:36:04 -07006826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006827 if (!mExiting) {
6828 return;
6829 }
Romain Guy06882f82009-06-10 13:36:04 -07006830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006831 if (isWindowAnimating()) {
6832 return;
6833 }
6834
6835 if (localLOGV) Log.v(
6836 TAG, "Exit animation finished in " + this
6837 + ": remove=" + mRemoveOnExit);
6838 if (mSurface != null) {
6839 mDestroySurface.add(this);
6840 mDestroying = true;
6841 if (SHOW_TRANSACTIONS) Log.i(
6842 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6843 try {
6844 mSurface.hide();
6845 } catch (RuntimeException e) {
6846 Log.w(TAG, "Error hiding surface in " + this, e);
6847 }
6848 mLastHidden = true;
6849 mKeyWaiter.releasePendingPointerLocked(mSession);
6850 }
6851 mExiting = false;
6852 if (mRemoveOnExit) {
6853 mPendingRemove.add(this);
6854 mRemoveOnExit = false;
6855 }
6856 }
Romain Guy06882f82009-06-10 13:36:04 -07006857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006858 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6859 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6860 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6861 if (dtdx < -.000001f || dtdx > .000001f) return false;
6862 if (dsdy < -.000001f || dsdy > .000001f) return false;
6863 return true;
6864 }
Romain Guy06882f82009-06-10 13:36:04 -07006865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 void computeShownFrameLocked() {
6867 final boolean selfTransformation = mHasLocalTransformation;
6868 Transformation attachedTransformation =
6869 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6870 ? mAttachedWindow.mTransformation : null;
6871 Transformation appTransformation =
6872 (mAppToken != null && mAppToken.hasTransformation)
6873 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006874
6875 // Wallpapers are animated based on the "real" window they
6876 // are currently targeting.
6877 if (mAttrs.type == TYPE_WALLPAPER && mWallpaperTarget != null) {
6878 if (mWallpaperTarget.mHasLocalTransformation) {
6879 attachedTransformation = mWallpaperTarget.mTransformation;
6880 }
6881 if (mWallpaperTarget.mAppToken != null &&
6882 mWallpaperTarget.mAppToken.hasTransformation) {
6883 appTransformation = mWallpaperTarget.mAppToken.transformation;
6884 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006885 if (DEBUG_WALLPAPER && attachedTransformation != null) {
6886 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
6887 }
6888 if (DEBUG_WALLPAPER && appTransformation != null) {
6889 Log.v(TAG, "WP target app xform: " + appTransformation);
6890 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006891 }
6892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 if (selfTransformation || attachedTransformation != null
6894 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006895 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896 final Rect frame = mFrame;
6897 final float tmpFloats[] = mTmpFloats;
6898 final Matrix tmpMatrix = mTmpMatrix;
6899
6900 // Compute the desired transformation.
6901 tmpMatrix.setTranslate(frame.left, frame.top);
6902 if (selfTransformation) {
6903 tmpMatrix.preConcat(mTransformation.getMatrix());
6904 }
6905 if (attachedTransformation != null) {
6906 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6907 }
6908 if (appTransformation != null) {
6909 tmpMatrix.preConcat(appTransformation.getMatrix());
6910 }
6911
6912 // "convert" it into SurfaceFlinger's format
6913 // (a 2x2 matrix + an offset)
6914 // Here we must not transform the position of the surface
6915 // since it is already included in the transformation.
6916 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918 tmpMatrix.getValues(tmpFloats);
6919 mDsDx = tmpFloats[Matrix.MSCALE_X];
6920 mDtDx = tmpFloats[Matrix.MSKEW_X];
6921 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6922 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006923 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
6924 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 int w = frame.width();
6926 int h = frame.height();
6927 mShownFrame.set(x, y, x+w, y+h);
6928
6929 // Now set the alpha... but because our current hardware
6930 // can't do alpha transformation on a non-opaque surface,
6931 // turn it off if we are running an animation that is also
6932 // transforming since it is more important to have that
6933 // animation be smooth.
6934 mShownAlpha = mAlpha;
6935 if (!mLimitedAlphaCompositing
6936 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6937 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6938 && x == frame.left && y == frame.top))) {
6939 //Log.i(TAG, "Applying alpha transform");
6940 if (selfTransformation) {
6941 mShownAlpha *= mTransformation.getAlpha();
6942 }
6943 if (attachedTransformation != null) {
6944 mShownAlpha *= attachedTransformation.getAlpha();
6945 }
6946 if (appTransformation != null) {
6947 mShownAlpha *= appTransformation.getAlpha();
6948 }
6949 } else {
6950 //Log.i(TAG, "Not applying alpha transform");
6951 }
Romain Guy06882f82009-06-10 13:36:04 -07006952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 if (localLOGV) Log.v(
6954 TAG, "Continuing animation in " + this +
6955 ": " + mShownFrame +
6956 ", alpha=" + mTransformation.getAlpha());
6957 return;
6958 }
Romain Guy06882f82009-06-10 13:36:04 -07006959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006960 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006961 if (mXOffset != 0 || mYOffset != 0) {
6962 mShownFrame.offset(mXOffset, mYOffset);
6963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006964 mShownAlpha = mAlpha;
6965 mDsDx = 1;
6966 mDtDx = 0;
6967 mDsDy = 0;
6968 mDtDy = 1;
6969 }
Romain Guy06882f82009-06-10 13:36:04 -07006970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006971 /**
6972 * Is this window visible? It is not visible if there is no
6973 * surface, or we are in the process of running an exit animation
6974 * that will remove the surface, or its app token has been hidden.
6975 */
6976 public boolean isVisibleLw() {
6977 final AppWindowToken atoken = mAppToken;
6978 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6979 && (atoken == null || !atoken.hiddenRequested)
6980 && !mExiting && !mDestroying;
6981 }
6982
6983 /**
6984 * Is this window visible, ignoring its app token? It is not visible
6985 * if there is no surface, or we are in the process of running an exit animation
6986 * that will remove the surface.
6987 */
6988 public boolean isWinVisibleLw() {
6989 final AppWindowToken atoken = mAppToken;
6990 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6991 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6992 && !mExiting && !mDestroying;
6993 }
6994
6995 /**
6996 * The same as isVisible(), but follows the current hidden state of
6997 * the associated app token, not the pending requested hidden state.
6998 */
6999 boolean isVisibleNow() {
7000 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007001 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 }
7003
7004 /**
7005 * Same as isVisible(), but we also count it as visible between the
7006 * call to IWindowSession.add() and the first relayout().
7007 */
7008 boolean isVisibleOrAdding() {
7009 final AppWindowToken atoken = mAppToken;
7010 return (mSurface != null
7011 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7012 && mPolicyVisibility && !mAttachedHidden
7013 && (atoken == null || !atoken.hiddenRequested)
7014 && !mExiting && !mDestroying;
7015 }
7016
7017 /**
7018 * Is this window currently on-screen? It is on-screen either if it
7019 * is visible or it is currently running an animation before no longer
7020 * being visible.
7021 */
7022 boolean isOnScreen() {
7023 final AppWindowToken atoken = mAppToken;
7024 if (atoken != null) {
7025 return mSurface != null && mPolicyVisibility && !mDestroying
7026 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007027 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007028 } else {
7029 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007030 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 }
7032 }
Romain Guy06882f82009-06-10 13:36:04 -07007033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007034 /**
7035 * Like isOnScreen(), but we don't return true if the window is part
7036 * of a transition that has not yet been started.
7037 */
7038 boolean isReadyForDisplay() {
7039 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007040 final boolean animating = atoken != null
7041 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007042 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007043 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7044 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007045 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007046 }
7047
7048 /** Is the window or its container currently animating? */
7049 boolean isAnimating() {
7050 final WindowState attached = mAttachedWindow;
7051 final AppWindowToken atoken = mAppToken;
7052 return mAnimation != null
7053 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007054 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007055 (atoken.animation != null
7056 || atoken.inPendingTransaction));
7057 }
7058
7059 /** Is this window currently animating? */
7060 boolean isWindowAnimating() {
7061 return mAnimation != null;
7062 }
7063
7064 /**
7065 * Like isOnScreen, but returns false if the surface hasn't yet
7066 * been drawn.
7067 */
7068 public boolean isDisplayedLw() {
7069 final AppWindowToken atoken = mAppToken;
7070 return mSurface != null && mPolicyVisibility && !mDestroying
7071 && !mDrawPending && !mCommitDrawPending
7072 && ((!mAttachedHidden &&
7073 (atoken == null || !atoken.hiddenRequested))
7074 || mAnimating);
7075 }
7076
7077 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7078 boolean shownFrame, boolean onlyOpaque) {
7079 if (mSurface == null) {
7080 return false;
7081 }
7082 if (mAppToken != null && !mAppToken.appFullscreen) {
7083 return false;
7084 }
7085 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7086 return false;
7087 }
7088 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007089
7090 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7091 return frame.left <= mCompatibleScreenFrame.left &&
7092 frame.top <= mCompatibleScreenFrame.top &&
7093 frame.right >= mCompatibleScreenFrame.right &&
7094 frame.bottom >= mCompatibleScreenFrame.bottom;
7095 } else {
7096 return frame.left <= 0 && frame.top <= 0
7097 && frame.right >= screenWidth
7098 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 }
Romain Guy06882f82009-06-10 13:36:04 -07007101
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007102 /**
7103 * Return true if the window is opaque and fully drawn.
7104 */
7105 boolean isOpaqueDrawn() {
7106 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
7107 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
7108 }
7109
7110 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7111 return
7112 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007113 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7114 // only if it's visible
7115 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007116 // and only if the application fills the compatible screen
7117 mFrame.left <= mCompatibleScreenFrame.left &&
7118 mFrame.top <= mCompatibleScreenFrame.top &&
7119 mFrame.right >= mCompatibleScreenFrame.right &&
7120 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007121 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007122 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007123 }
7124
7125 boolean isFullscreen(int screenWidth, int screenHeight) {
7126 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007127 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 }
7129
7130 void removeLocked() {
7131 if (mAttachedWindow != null) {
7132 mAttachedWindow.mChildWindows.remove(this);
7133 }
7134 destroySurfaceLocked();
7135 mSession.windowRemovedLocked();
7136 try {
7137 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7138 } catch (RuntimeException e) {
7139 // Ignore if it has already been removed (usually because
7140 // we are doing this as part of processing a death note.)
7141 }
7142 }
7143
7144 private class DeathRecipient implements IBinder.DeathRecipient {
7145 public void binderDied() {
7146 try {
7147 synchronized(mWindowMap) {
7148 WindowState win = windowForClientLocked(mSession, mClient);
7149 Log.i(TAG, "WIN DEATH: " + win);
7150 if (win != null) {
7151 removeWindowLocked(mSession, win);
7152 }
7153 }
7154 } catch (IllegalArgumentException ex) {
7155 // This will happen if the window has already been
7156 // removed.
7157 }
7158 }
7159 }
7160
7161 /** Returns true if this window desires key events. */
7162 public final boolean canReceiveKeys() {
7163 return isVisibleOrAdding()
7164 && (mViewVisibility == View.VISIBLE)
7165 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7166 }
7167
7168 public boolean hasDrawnLw() {
7169 return mHasDrawn;
7170 }
7171
7172 public boolean showLw(boolean doAnimation) {
7173 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7174 mPolicyVisibility = true;
7175 mPolicyVisibilityAfterAnim = true;
7176 if (doAnimation) {
7177 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7178 }
7179 requestAnimationLocked(0);
7180 return true;
7181 }
7182 return false;
7183 }
7184
7185 public boolean hideLw(boolean doAnimation) {
7186 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7187 : mPolicyVisibility;
7188 if (current) {
7189 if (doAnimation) {
7190 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7191 if (mAnimation == null) {
7192 doAnimation = false;
7193 }
7194 }
7195 if (doAnimation) {
7196 mPolicyVisibilityAfterAnim = false;
7197 } else {
7198 mPolicyVisibilityAfterAnim = false;
7199 mPolicyVisibility = false;
7200 }
7201 requestAnimationLocked(0);
7202 return true;
7203 }
7204 return false;
7205 }
7206
7207 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007208 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007209
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007210 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7211 pw.print(" mClient="); pw.println(mClient.asBinder());
7212 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7213 if (mAttachedWindow != null || mLayoutAttached) {
7214 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7215 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7216 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007217 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7218 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7219 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007220 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7221 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007222 }
7223 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7224 pw.print(" mSubLayer="); pw.print(mSubLayer);
7225 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7226 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7227 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7228 pw.print("="); pw.print(mAnimLayer);
7229 pw.print(" mLastLayer="); pw.println(mLastLayer);
7230 if (mSurface != null) {
7231 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7232 }
7233 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7234 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7235 if (mAppToken != null) {
7236 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7237 }
7238 if (mTargetAppToken != null) {
7239 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7240 }
7241 pw.print(prefix); pw.print("mViewVisibility=0x");
7242 pw.print(Integer.toHexString(mViewVisibility));
7243 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007244 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7245 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007246 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7247 pw.print(prefix); pw.print("mPolicyVisibility=");
7248 pw.print(mPolicyVisibility);
7249 pw.print(" mPolicyVisibilityAfterAnim=");
7250 pw.print(mPolicyVisibilityAfterAnim);
7251 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7252 }
7253 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007254 pw.print(" h="); pw.println(mRequestedHeight);
7255 if (mXOffset != 0 || mYOffset != 0) {
7256 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7257 pw.print(" y="); pw.println(mYOffset);
7258 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007259 pw.print(prefix); pw.print("mGivenContentInsets=");
7260 mGivenContentInsets.printShortString(pw);
7261 pw.print(" mGivenVisibleInsets=");
7262 mGivenVisibleInsets.printShortString(pw);
7263 pw.println();
7264 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7265 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7266 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7267 }
7268 pw.print(prefix); pw.print("mShownFrame=");
7269 mShownFrame.printShortString(pw);
7270 pw.print(" last="); mLastShownFrame.printShortString(pw);
7271 pw.println();
7272 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7273 pw.print(" last="); mLastFrame.printShortString(pw);
7274 pw.println();
7275 pw.print(prefix); pw.print("mContainingFrame=");
7276 mContainingFrame.printShortString(pw);
7277 pw.print(" mDisplayFrame=");
7278 mDisplayFrame.printShortString(pw);
7279 pw.println();
7280 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7281 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7282 pw.println();
7283 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7284 pw.print(" last="); mLastContentInsets.printShortString(pw);
7285 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7286 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7287 pw.println();
7288 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7289 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7290 pw.print(" mAlpha="); pw.print(mAlpha);
7291 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7292 }
7293 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7294 || mAnimation != null) {
7295 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7296 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7297 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7298 pw.print(" mAnimation="); pw.println(mAnimation);
7299 }
7300 if (mHasTransformation || mHasLocalTransformation) {
7301 pw.print(prefix); pw.print("XForm: has=");
7302 pw.print(mHasTransformation);
7303 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7304 pw.print(" "); mTransformation.printShortString(pw);
7305 pw.println();
7306 }
7307 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7308 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7309 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7310 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7311 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7312 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7313 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7314 pw.print(" mDestroying="); pw.print(mDestroying);
7315 pw.print(" mRemoved="); pw.println(mRemoved);
7316 }
7317 if (mOrientationChanging || mAppFreezing) {
7318 pw.print(prefix); pw.print("mOrientationChanging=");
7319 pw.print(mOrientationChanging);
7320 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7321 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007322 if (mHScale != 1 || mVScale != 1) {
7323 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7324 pw.print(" mVScale="); pw.println(mVScale);
7325 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007326 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007327 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7328 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007330 }
7331
7332 @Override
7333 public String toString() {
7334 return "Window{"
7335 + Integer.toHexString(System.identityHashCode(this))
7336 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7337 }
7338 }
Romain Guy06882f82009-06-10 13:36:04 -07007339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007340 // -------------------------------------------------------------
7341 // Window Token State
7342 // -------------------------------------------------------------
7343
7344 class WindowToken {
7345 // The actual token.
7346 final IBinder token;
7347
7348 // The type of window this token is for, as per WindowManager.LayoutParams.
7349 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007351 // Set if this token was explicitly added by a client, so should
7352 // not be removed when all windows are removed.
7353 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007354
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007355 // For printing.
7356 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007358 // If this is an AppWindowToken, this is non-null.
7359 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007361 // All of the windows associated with this token.
7362 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7363
7364 // Is key dispatching paused for this token?
7365 boolean paused = false;
7366
7367 // Should this token's windows be hidden?
7368 boolean hidden;
7369
7370 // Temporary for finding which tokens no longer have visible windows.
7371 boolean hasVisible;
7372
7373 WindowToken(IBinder _token, int type, boolean _explicit) {
7374 token = _token;
7375 windowType = type;
7376 explicit = _explicit;
7377 }
7378
7379 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007380 pw.print(prefix); pw.print("token="); pw.println(token);
7381 pw.print(prefix); pw.print("windows="); pw.println(windows);
7382 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7383 pw.print(" hidden="); pw.print(hidden);
7384 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 }
7386
7387 @Override
7388 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007389 if (stringName == null) {
7390 StringBuilder sb = new StringBuilder();
7391 sb.append("WindowToken{");
7392 sb.append(Integer.toHexString(System.identityHashCode(this)));
7393 sb.append(" token="); sb.append(token); sb.append('}');
7394 stringName = sb.toString();
7395 }
7396 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397 }
7398 };
7399
7400 class AppWindowToken extends WindowToken {
7401 // Non-null only for application tokens.
7402 final IApplicationToken appToken;
7403
7404 // All of the windows and child windows that are included in this
7405 // application token. Note this list is NOT sorted!
7406 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7407
7408 int groupId = -1;
7409 boolean appFullscreen;
7410 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412 // These are used for determining when all windows associated with
7413 // an activity have been drawn, so they can be made visible together
7414 // at the same time.
7415 int lastTransactionSequence = mTransactionSequence-1;
7416 int numInterestingWindows;
7417 int numDrawnWindows;
7418 boolean inPendingTransaction;
7419 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007421 // Is this token going to be hidden in a little while? If so, it
7422 // won't be taken into account for setting the screen orientation.
7423 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 // Is this window's surface needed? This is almost like hidden, except
7426 // it will sometimes be true a little earlier: when the token has
7427 // been shown, but is still waiting for its app transition to execute
7428 // before making its windows shown.
7429 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007431 // Have we told the window clients to hide themselves?
7432 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007434 // Last visibility state we reported to the app token.
7435 boolean reportedVisible;
7436
7437 // Set to true when the token has been removed from the window mgr.
7438 boolean removed;
7439
7440 // Have we been asked to have this token keep the screen frozen?
7441 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007443 boolean animating;
7444 Animation animation;
7445 boolean hasTransformation;
7446 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 // Offset to the window of all layers in the token, for use by
7449 // AppWindowToken animations.
7450 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007452 // Information about an application starting window if displayed.
7453 StartingData startingData;
7454 WindowState startingWindow;
7455 View startingView;
7456 boolean startingDisplayed;
7457 boolean startingMoved;
7458 boolean firstWindowDrawn;
7459
7460 AppWindowToken(IApplicationToken _token) {
7461 super(_token.asBinder(),
7462 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7463 appWindowToken = this;
7464 appToken = _token;
7465 }
Romain Guy06882f82009-06-10 13:36:04 -07007466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007467 public void setAnimation(Animation anim) {
7468 if (localLOGV) Log.v(
7469 TAG, "Setting animation in " + this + ": " + anim);
7470 animation = anim;
7471 animating = false;
7472 anim.restrictDuration(MAX_ANIMATION_DURATION);
7473 anim.scaleCurrentDuration(mTransitionAnimationScale);
7474 int zorder = anim.getZAdjustment();
7475 int adj = 0;
7476 if (zorder == Animation.ZORDER_TOP) {
7477 adj = TYPE_LAYER_OFFSET;
7478 } else if (zorder == Animation.ZORDER_BOTTOM) {
7479 adj = -TYPE_LAYER_OFFSET;
7480 }
Romain Guy06882f82009-06-10 13:36:04 -07007481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 if (animLayerAdjustment != adj) {
7483 animLayerAdjustment = adj;
7484 updateLayers();
7485 }
7486 }
Romain Guy06882f82009-06-10 13:36:04 -07007487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007488 public void setDummyAnimation() {
7489 if (animation == null) {
7490 if (localLOGV) Log.v(
7491 TAG, "Setting dummy animation in " + this);
7492 animation = sDummyAnimation;
7493 }
7494 }
7495
7496 public void clearAnimation() {
7497 if (animation != null) {
7498 animation = null;
7499 animating = true;
7500 }
7501 }
Romain Guy06882f82009-06-10 13:36:04 -07007502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 void updateLayers() {
7504 final int N = allAppWindows.size();
7505 final int adj = animLayerAdjustment;
7506 for (int i=0; i<N; i++) {
7507 WindowState w = allAppWindows.get(i);
7508 w.mAnimLayer = w.mLayer + adj;
7509 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7510 + w.mAnimLayer);
7511 if (w == mInputMethodTarget) {
7512 setInputMethodAnimLayerAdjustment(adj);
7513 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007514 if (w == mWallpaperTarget) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007515 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007517 }
7518 }
Romain Guy06882f82009-06-10 13:36:04 -07007519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 void sendAppVisibilityToClients() {
7521 final int N = allAppWindows.size();
7522 for (int i=0; i<N; i++) {
7523 WindowState win = allAppWindows.get(i);
7524 if (win == startingWindow && clientHidden) {
7525 // Don't hide the starting window.
7526 continue;
7527 }
7528 try {
7529 if (DEBUG_VISIBILITY) Log.v(TAG,
7530 "Setting visibility of " + win + ": " + (!clientHidden));
7531 win.mClient.dispatchAppVisibility(!clientHidden);
7532 } catch (RemoteException e) {
7533 }
7534 }
7535 }
Romain Guy06882f82009-06-10 13:36:04 -07007536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007537 void showAllWindowsLocked() {
7538 final int NW = allAppWindows.size();
7539 for (int i=0; i<NW; i++) {
7540 WindowState w = allAppWindows.get(i);
7541 if (DEBUG_VISIBILITY) Log.v(TAG,
7542 "performing show on: " + w);
7543 w.performShowLocked();
7544 }
7545 }
Romain Guy06882f82009-06-10 13:36:04 -07007546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007547 // This must be called while inside a transaction.
7548 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7549 if (!mDisplayFrozen) {
7550 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007552 if (animation == sDummyAnimation) {
7553 // This guy is going to animate, but not yet. For now count
7554 // it is not animating for purposes of scheduling transactions;
7555 // when it is really time to animate, this will be set to
7556 // a real animation and the next call will execute normally.
7557 return false;
7558 }
Romain Guy06882f82009-06-10 13:36:04 -07007559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007560 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7561 if (!animating) {
7562 if (DEBUG_ANIM) Log.v(
7563 TAG, "Starting animation in " + this +
7564 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7565 + " scale=" + mTransitionAnimationScale
7566 + " allDrawn=" + allDrawn + " animating=" + animating);
7567 animation.initialize(dw, dh, dw, dh);
7568 animation.setStartTime(currentTime);
7569 animating = true;
7570 }
7571 transformation.clear();
7572 final boolean more = animation.getTransformation(
7573 currentTime, transformation);
7574 if (DEBUG_ANIM) Log.v(
7575 TAG, "Stepped animation in " + this +
7576 ": more=" + more + ", xform=" + transformation);
7577 if (more) {
7578 // we're done!
7579 hasTransformation = true;
7580 return true;
7581 }
7582 if (DEBUG_ANIM) Log.v(
7583 TAG, "Finished animation in " + this +
7584 " @ " + currentTime);
7585 animation = null;
7586 }
7587 } else if (animation != null) {
7588 // If the display is frozen, and there is a pending animation,
7589 // clear it and make sure we run the cleanup code.
7590 animating = true;
7591 animation = null;
7592 }
7593
7594 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007596 if (!animating) {
7597 return false;
7598 }
7599
7600 clearAnimation();
7601 animating = false;
7602 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7603 moveInputMethodWindowsIfNeededLocked(true);
7604 }
Romain Guy06882f82009-06-10 13:36:04 -07007605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 if (DEBUG_ANIM) Log.v(
7607 TAG, "Animation done in " + this
7608 + ": reportedVisible=" + reportedVisible);
7609
7610 transformation.clear();
7611 if (animLayerAdjustment != 0) {
7612 animLayerAdjustment = 0;
7613 updateLayers();
7614 }
Romain Guy06882f82009-06-10 13:36:04 -07007615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007616 final int N = windows.size();
7617 for (int i=0; i<N; i++) {
7618 ((WindowState)windows.get(i)).finishExit();
7619 }
7620 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007622 return false;
7623 }
7624
7625 void updateReportedVisibilityLocked() {
7626 if (appToken == null) {
7627 return;
7628 }
Romain Guy06882f82009-06-10 13:36:04 -07007629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007630 int numInteresting = 0;
7631 int numVisible = 0;
7632 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7635 final int N = allAppWindows.size();
7636 for (int i=0; i<N; i++) {
7637 WindowState win = allAppWindows.get(i);
7638 if (win == startingWindow || win.mAppFreezing) {
7639 continue;
7640 }
7641 if (DEBUG_VISIBILITY) {
7642 Log.v(TAG, "Win " + win + ": isDisplayed="
7643 + win.isDisplayedLw()
7644 + ", isAnimating=" + win.isAnimating());
7645 if (!win.isDisplayedLw()) {
7646 Log.v(TAG, "Not displayed: s=" + win.mSurface
7647 + " pv=" + win.mPolicyVisibility
7648 + " dp=" + win.mDrawPending
7649 + " cdp=" + win.mCommitDrawPending
7650 + " ah=" + win.mAttachedHidden
7651 + " th="
7652 + (win.mAppToken != null
7653 ? win.mAppToken.hiddenRequested : false)
7654 + " a=" + win.mAnimating);
7655 }
7656 }
7657 numInteresting++;
7658 if (win.isDisplayedLw()) {
7659 if (!win.isAnimating()) {
7660 numVisible++;
7661 }
7662 nowGone = false;
7663 } else if (win.isAnimating()) {
7664 nowGone = false;
7665 }
7666 }
Romain Guy06882f82009-06-10 13:36:04 -07007667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007668 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7669 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7670 + numInteresting + " visible=" + numVisible);
7671 if (nowVisible != reportedVisible) {
7672 if (DEBUG_VISIBILITY) Log.v(
7673 TAG, "Visibility changed in " + this
7674 + ": vis=" + nowVisible);
7675 reportedVisible = nowVisible;
7676 Message m = mH.obtainMessage(
7677 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7678 nowVisible ? 1 : 0,
7679 nowGone ? 1 : 0,
7680 this);
7681 mH.sendMessage(m);
7682 }
7683 }
Romain Guy06882f82009-06-10 13:36:04 -07007684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007685 void dump(PrintWriter pw, String prefix) {
7686 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007687 if (appToken != null) {
7688 pw.print(prefix); pw.println("app=true");
7689 }
7690 if (allAppWindows.size() > 0) {
7691 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7692 }
7693 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7694 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7695 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7696 pw.print(" clientHidden="); pw.print(clientHidden);
7697 pw.print(" willBeHidden="); pw.print(willBeHidden);
7698 pw.print(" reportedVisible="); pw.println(reportedVisible);
7699 if (paused || freezingScreen) {
7700 pw.print(prefix); pw.print("paused="); pw.print(paused);
7701 pw.print(" freezingScreen="); pw.println(freezingScreen);
7702 }
7703 if (numInterestingWindows != 0 || numDrawnWindows != 0
7704 || inPendingTransaction || allDrawn) {
7705 pw.print(prefix); pw.print("numInterestingWindows=");
7706 pw.print(numInterestingWindows);
7707 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7708 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7709 pw.print(" allDrawn="); pw.println(allDrawn);
7710 }
7711 if (animating || animation != null) {
7712 pw.print(prefix); pw.print("animating="); pw.print(animating);
7713 pw.print(" animation="); pw.println(animation);
7714 }
7715 if (animLayerAdjustment != 0) {
7716 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7717 }
7718 if (hasTransformation) {
7719 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7720 pw.print(" transformation="); transformation.printShortString(pw);
7721 pw.println();
7722 }
7723 if (startingData != null || removed || firstWindowDrawn) {
7724 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7725 pw.print(" removed="); pw.print(removed);
7726 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7727 }
7728 if (startingWindow != null || startingView != null
7729 || startingDisplayed || startingMoved) {
7730 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7731 pw.print(" startingView="); pw.print(startingView);
7732 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7733 pw.print(" startingMoved"); pw.println(startingMoved);
7734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007735 }
7736
7737 @Override
7738 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007739 if (stringName == null) {
7740 StringBuilder sb = new StringBuilder();
7741 sb.append("AppWindowToken{");
7742 sb.append(Integer.toHexString(System.identityHashCode(this)));
7743 sb.append(" token="); sb.append(token); sb.append('}');
7744 stringName = sb.toString();
7745 }
7746 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007747 }
7748 }
Romain Guy06882f82009-06-10 13:36:04 -07007749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007750 public static WindowManager.LayoutParams findAnimations(
7751 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007752 ArrayList<AppWindowToken> openingTokenList1,
7753 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007755
7756 // First, check if there is a compatible window in opening/closing
7757 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 WindowManager.LayoutParams animParams = null;
7759 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007760 animParams = findCompatibleWindowParams(openingTokenList1);
7761 if (animParams == null) {
7762 animParams = findCompatibleWindowParams(closingTokenList2);
7763 }
7764 if (animParams != null) {
7765 return animParams;
7766 }
7767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007768 //Log.i(TAG, "Looking for animations...");
7769 for (int i=order.size()-1; i>=0; i--) {
7770 AppWindowToken wtoken = order.get(i);
7771 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007772 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 int j = wtoken.windows.size();
7774 while (j > 0) {
7775 j--;
7776 WindowState win = wtoken.windows.get(j);
7777 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7778 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7779 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7780 //Log.i(TAG, "Found base or application window, done!");
7781 if (wtoken.appFullscreen) {
7782 return win.mAttrs;
7783 }
7784 if (animSrc < 2) {
7785 animParams = win.mAttrs;
7786 animSrc = 2;
7787 }
7788 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7789 //Log.i(TAG, "Found normal window, we may use this...");
7790 animParams = win.mAttrs;
7791 animSrc = 1;
7792 }
7793 }
7794 }
7795 }
Romain Guy06882f82009-06-10 13:36:04 -07007796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007797 return animParams;
7798 }
Romain Guy06882f82009-06-10 13:36:04 -07007799
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007800 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7801 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7802 AppWindowToken wtoken = tokenList.get(appCount);
7803 // Just checking one window is sufficient as all windows have the compatible flag
7804 // if the application is in compatibility mode.
7805 if (wtoken.windows.size() > 0) {
7806 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7807 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7808 return params;
7809 }
7810 }
7811 }
7812 return null;
7813 }
7814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007815 // -------------------------------------------------------------
7816 // DummyAnimation
7817 // -------------------------------------------------------------
7818
7819 // This is an animation that does nothing: it just immediately finishes
7820 // itself every time it is called. It is used as a stub animation in cases
7821 // where we want to synchronize multiple things that may be animating.
7822 static final class DummyAnimation extends Animation {
7823 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7824 return false;
7825 }
7826 }
7827 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 // -------------------------------------------------------------
7830 // Async Handler
7831 // -------------------------------------------------------------
7832
7833 static final class StartingData {
7834 final String pkg;
7835 final int theme;
7836 final CharSequence nonLocalizedLabel;
7837 final int labelRes;
7838 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7841 int _labelRes, int _icon) {
7842 pkg = _pkg;
7843 theme = _theme;
7844 nonLocalizedLabel = _nonLocalizedLabel;
7845 labelRes = _labelRes;
7846 icon = _icon;
7847 }
7848 }
7849
7850 private final class H extends Handler {
7851 public static final int REPORT_FOCUS_CHANGE = 2;
7852 public static final int REPORT_LOSING_FOCUS = 3;
7853 public static final int ANIMATE = 4;
7854 public static final int ADD_STARTING = 5;
7855 public static final int REMOVE_STARTING = 6;
7856 public static final int FINISHED_STARTING = 7;
7857 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007858 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7859 public static final int HOLD_SCREEN_CHANGED = 12;
7860 public static final int APP_TRANSITION_TIMEOUT = 13;
7861 public static final int PERSIST_ANIMATION_SCALE = 14;
7862 public static final int FORCE_GC = 15;
7863 public static final int ENABLE_SCREEN = 16;
7864 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007865 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 public H() {
7870 }
Romain Guy06882f82009-06-10 13:36:04 -07007871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 @Override
7873 public void handleMessage(Message msg) {
7874 switch (msg.what) {
7875 case REPORT_FOCUS_CHANGE: {
7876 WindowState lastFocus;
7877 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 synchronized(mWindowMap) {
7880 lastFocus = mLastFocus;
7881 newFocus = mCurrentFocus;
7882 if (lastFocus == newFocus) {
7883 // Focus is not changing, so nothing to do.
7884 return;
7885 }
7886 mLastFocus = newFocus;
7887 //Log.i(TAG, "Focus moving from " + lastFocus
7888 // + " to " + newFocus);
7889 if (newFocus != null && lastFocus != null
7890 && !newFocus.isDisplayedLw()) {
7891 //Log.i(TAG, "Delaying loss of focus...");
7892 mLosingFocus.add(lastFocus);
7893 lastFocus = null;
7894 }
7895 }
7896
7897 if (lastFocus != newFocus) {
7898 //System.out.println("Changing focus from " + lastFocus
7899 // + " to " + newFocus);
7900 if (newFocus != null) {
7901 try {
7902 //Log.i(TAG, "Gaining focus: " + newFocus);
7903 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7904 } catch (RemoteException e) {
7905 // Ignore if process has died.
7906 }
7907 }
7908
7909 if (lastFocus != null) {
7910 try {
7911 //Log.i(TAG, "Losing focus: " + lastFocus);
7912 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7913 } catch (RemoteException e) {
7914 // Ignore if process has died.
7915 }
7916 }
7917 }
7918 } break;
7919
7920 case REPORT_LOSING_FOCUS: {
7921 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 synchronized(mWindowMap) {
7924 losers = mLosingFocus;
7925 mLosingFocus = new ArrayList<WindowState>();
7926 }
7927
7928 final int N = losers.size();
7929 for (int i=0; i<N; i++) {
7930 try {
7931 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7932 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7933 } catch (RemoteException e) {
7934 // Ignore if process has died.
7935 }
7936 }
7937 } break;
7938
7939 case ANIMATE: {
7940 synchronized(mWindowMap) {
7941 mAnimationPending = false;
7942 performLayoutAndPlaceSurfacesLocked();
7943 }
7944 } break;
7945
7946 case ADD_STARTING: {
7947 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7948 final StartingData sd = wtoken.startingData;
7949
7950 if (sd == null) {
7951 // Animation has been canceled... do nothing.
7952 return;
7953 }
Romain Guy06882f82009-06-10 13:36:04 -07007954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007955 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7956 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007958 View view = null;
7959 try {
7960 view = mPolicy.addStartingWindow(
7961 wtoken.token, sd.pkg,
7962 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7963 sd.icon);
7964 } catch (Exception e) {
7965 Log.w(TAG, "Exception when adding starting window", e);
7966 }
7967
7968 if (view != null) {
7969 boolean abort = false;
7970
7971 synchronized(mWindowMap) {
7972 if (wtoken.removed || wtoken.startingData == null) {
7973 // If the window was successfully added, then
7974 // we need to remove it.
7975 if (wtoken.startingWindow != null) {
7976 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7977 "Aborted starting " + wtoken
7978 + ": removed=" + wtoken.removed
7979 + " startingData=" + wtoken.startingData);
7980 wtoken.startingWindow = null;
7981 wtoken.startingData = null;
7982 abort = true;
7983 }
7984 } else {
7985 wtoken.startingView = view;
7986 }
7987 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7988 "Added starting " + wtoken
7989 + ": startingWindow="
7990 + wtoken.startingWindow + " startingView="
7991 + wtoken.startingView);
7992 }
7993
7994 if (abort) {
7995 try {
7996 mPolicy.removeStartingWindow(wtoken.token, view);
7997 } catch (Exception e) {
7998 Log.w(TAG, "Exception when removing starting window", e);
7999 }
8000 }
8001 }
8002 } break;
8003
8004 case REMOVE_STARTING: {
8005 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8006 IBinder token = null;
8007 View view = null;
8008 synchronized (mWindowMap) {
8009 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8010 + wtoken + ": startingWindow="
8011 + wtoken.startingWindow + " startingView="
8012 + wtoken.startingView);
8013 if (wtoken.startingWindow != null) {
8014 view = wtoken.startingView;
8015 token = wtoken.token;
8016 wtoken.startingData = null;
8017 wtoken.startingView = null;
8018 wtoken.startingWindow = null;
8019 }
8020 }
8021 if (view != null) {
8022 try {
8023 mPolicy.removeStartingWindow(token, view);
8024 } catch (Exception e) {
8025 Log.w(TAG, "Exception when removing starting window", e);
8026 }
8027 }
8028 } break;
8029
8030 case FINISHED_STARTING: {
8031 IBinder token = null;
8032 View view = null;
8033 while (true) {
8034 synchronized (mWindowMap) {
8035 final int N = mFinishedStarting.size();
8036 if (N <= 0) {
8037 break;
8038 }
8039 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8040
8041 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8042 "Finished starting " + wtoken
8043 + ": startingWindow=" + wtoken.startingWindow
8044 + " startingView=" + wtoken.startingView);
8045
8046 if (wtoken.startingWindow == null) {
8047 continue;
8048 }
8049
8050 view = wtoken.startingView;
8051 token = wtoken.token;
8052 wtoken.startingData = null;
8053 wtoken.startingView = null;
8054 wtoken.startingWindow = null;
8055 }
8056
8057 try {
8058 mPolicy.removeStartingWindow(token, view);
8059 } catch (Exception e) {
8060 Log.w(TAG, "Exception when removing starting window", e);
8061 }
8062 }
8063 } break;
8064
8065 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8066 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8067
8068 boolean nowVisible = msg.arg1 != 0;
8069 boolean nowGone = msg.arg2 != 0;
8070
8071 try {
8072 if (DEBUG_VISIBILITY) Log.v(
8073 TAG, "Reporting visible in " + wtoken
8074 + " visible=" + nowVisible
8075 + " gone=" + nowGone);
8076 if (nowVisible) {
8077 wtoken.appToken.windowsVisible();
8078 } else {
8079 wtoken.appToken.windowsGone();
8080 }
8081 } catch (RemoteException ex) {
8082 }
8083 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008085 case WINDOW_FREEZE_TIMEOUT: {
8086 synchronized (mWindowMap) {
8087 Log.w(TAG, "Window freeze timeout expired.");
8088 int i = mWindows.size();
8089 while (i > 0) {
8090 i--;
8091 WindowState w = (WindowState)mWindows.get(i);
8092 if (w.mOrientationChanging) {
8093 w.mOrientationChanging = false;
8094 Log.w(TAG, "Force clearing orientation change: " + w);
8095 }
8096 }
8097 performLayoutAndPlaceSurfacesLocked();
8098 }
8099 break;
8100 }
Romain Guy06882f82009-06-10 13:36:04 -07008101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008102 case HOLD_SCREEN_CHANGED: {
8103 Session oldHold;
8104 Session newHold;
8105 synchronized (mWindowMap) {
8106 oldHold = mLastReportedHold;
8107 newHold = (Session)msg.obj;
8108 mLastReportedHold = newHold;
8109 }
Romain Guy06882f82009-06-10 13:36:04 -07008110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 if (oldHold != newHold) {
8112 try {
8113 if (oldHold != null) {
8114 mBatteryStats.noteStopWakelock(oldHold.mUid,
8115 "window",
8116 BatteryStats.WAKE_TYPE_WINDOW);
8117 }
8118 if (newHold != null) {
8119 mBatteryStats.noteStartWakelock(newHold.mUid,
8120 "window",
8121 BatteryStats.WAKE_TYPE_WINDOW);
8122 }
8123 } catch (RemoteException e) {
8124 }
8125 }
8126 break;
8127 }
Romain Guy06882f82009-06-10 13:36:04 -07008128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 case APP_TRANSITION_TIMEOUT: {
8130 synchronized (mWindowMap) {
8131 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8132 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8133 "*** APP TRANSITION TIMEOUT");
8134 mAppTransitionReady = true;
8135 mAppTransitionTimeout = true;
8136 performLayoutAndPlaceSurfacesLocked();
8137 }
8138 }
8139 break;
8140 }
Romain Guy06882f82009-06-10 13:36:04 -07008141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008142 case PERSIST_ANIMATION_SCALE: {
8143 Settings.System.putFloat(mContext.getContentResolver(),
8144 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8145 Settings.System.putFloat(mContext.getContentResolver(),
8146 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8147 break;
8148 }
Romain Guy06882f82009-06-10 13:36:04 -07008149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008150 case FORCE_GC: {
8151 synchronized(mWindowMap) {
8152 if (mAnimationPending) {
8153 // If we are animating, don't do the gc now but
8154 // delay a bit so we don't interrupt the animation.
8155 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8156 2000);
8157 return;
8158 }
8159 // If we are currently rotating the display, it will
8160 // schedule a new message when done.
8161 if (mDisplayFrozen) {
8162 return;
8163 }
8164 mFreezeGcPending = 0;
8165 }
8166 Runtime.getRuntime().gc();
8167 break;
8168 }
Romain Guy06882f82009-06-10 13:36:04 -07008169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008170 case ENABLE_SCREEN: {
8171 performEnableScreen();
8172 break;
8173 }
Romain Guy06882f82009-06-10 13:36:04 -07008174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175 case APP_FREEZE_TIMEOUT: {
8176 synchronized (mWindowMap) {
8177 Log.w(TAG, "App freeze timeout expired.");
8178 int i = mAppTokens.size();
8179 while (i > 0) {
8180 i--;
8181 AppWindowToken tok = mAppTokens.get(i);
8182 if (tok.freezingScreen) {
8183 Log.w(TAG, "Force clearing freeze: " + tok);
8184 unsetAppFreezingScreenLocked(tok, true, true);
8185 }
8186 }
8187 }
8188 break;
8189 }
Romain Guy06882f82009-06-10 13:36:04 -07008190
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008191 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008192 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008193 sendNewConfiguration();
8194 }
8195 break;
8196 }
Romain Guy06882f82009-06-10 13:36:04 -07008197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 }
8199 }
8200 }
8201
8202 // -------------------------------------------------------------
8203 // IWindowManager API
8204 // -------------------------------------------------------------
8205
8206 public IWindowSession openSession(IInputMethodClient client,
8207 IInputContext inputContext) {
8208 if (client == null) throw new IllegalArgumentException("null client");
8209 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8210 return new Session(client, inputContext);
8211 }
8212
8213 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8214 synchronized (mWindowMap) {
8215 // The focus for the client is the window immediately below
8216 // where we would place the input method window.
8217 int idx = findDesiredInputMethodWindowIndexLocked(false);
8218 WindowState imFocus;
8219 if (idx > 0) {
8220 imFocus = (WindowState)mWindows.get(idx-1);
8221 if (imFocus != null) {
8222 if (imFocus.mSession.mClient != null &&
8223 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8224 return true;
8225 }
8226 }
8227 }
8228 }
8229 return false;
8230 }
Romain Guy06882f82009-06-10 13:36:04 -07008231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008232 // -------------------------------------------------------------
8233 // Internals
8234 // -------------------------------------------------------------
8235
8236 final WindowState windowForClientLocked(Session session, IWindow client) {
8237 return windowForClientLocked(session, client.asBinder());
8238 }
Romain Guy06882f82009-06-10 13:36:04 -07008239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008240 final WindowState windowForClientLocked(Session session, IBinder client) {
8241 WindowState win = mWindowMap.get(client);
8242 if (localLOGV) Log.v(
8243 TAG, "Looking up client " + client + ": " + win);
8244 if (win == null) {
8245 RuntimeException ex = new RuntimeException();
8246 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8247 return null;
8248 }
8249 if (session != null && win.mSession != session) {
8250 RuntimeException ex = new RuntimeException();
8251 Log.w(TAG, "Requested window " + client + " is in session " +
8252 win.mSession + ", not " + session, ex);
8253 return null;
8254 }
8255
8256 return win;
8257 }
8258
8259 private final void assignLayersLocked() {
8260 int N = mWindows.size();
8261 int curBaseLayer = 0;
8262 int curLayer = 0;
8263 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008265 for (i=0; i<N; i++) {
8266 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008267 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8268 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008269 curLayer += WINDOW_LAYER_MULTIPLIER;
8270 w.mLayer = curLayer;
8271 } else {
8272 curBaseLayer = curLayer = w.mBaseLayer;
8273 w.mLayer = curLayer;
8274 }
8275 if (w.mTargetAppToken != null) {
8276 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8277 } else if (w.mAppToken != null) {
8278 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8279 } else {
8280 w.mAnimLayer = w.mLayer;
8281 }
8282 if (w.mIsImWindow) {
8283 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008284 } else if (w.mIsWallpaper) {
8285 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008286 }
8287 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8288 + w.mAnimLayer);
8289 //System.out.println(
8290 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8291 }
8292 }
8293
8294 private boolean mInLayout = false;
8295 private final void performLayoutAndPlaceSurfacesLocked() {
8296 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008297 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008298 throw new RuntimeException("Recursive call!");
8299 }
8300 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8301 return;
8302 }
8303
8304 boolean recoveringMemory = false;
8305 if (mForceRemoves != null) {
8306 recoveringMemory = true;
8307 // Wait a little it for things to settle down, and off we go.
8308 for (int i=0; i<mForceRemoves.size(); i++) {
8309 WindowState ws = mForceRemoves.get(i);
8310 Log.i(TAG, "Force removing: " + ws);
8311 removeWindowInnerLocked(ws.mSession, ws);
8312 }
8313 mForceRemoves = null;
8314 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8315 Object tmp = new Object();
8316 synchronized (tmp) {
8317 try {
8318 tmp.wait(250);
8319 } catch (InterruptedException e) {
8320 }
8321 }
8322 }
Romain Guy06882f82009-06-10 13:36:04 -07008323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008324 mInLayout = true;
8325 try {
8326 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 int i = mPendingRemove.size()-1;
8329 if (i >= 0) {
8330 while (i >= 0) {
8331 WindowState w = mPendingRemove.get(i);
8332 removeWindowInnerLocked(w.mSession, w);
8333 i--;
8334 }
8335 mPendingRemove.clear();
8336
8337 mInLayout = false;
8338 assignLayersLocked();
8339 mLayoutNeeded = true;
8340 performLayoutAndPlaceSurfacesLocked();
8341
8342 } else {
8343 mInLayout = false;
8344 if (mLayoutNeeded) {
8345 requestAnimationLocked(0);
8346 }
8347 }
8348 } catch (RuntimeException e) {
8349 mInLayout = false;
8350 Log.e(TAG, "Unhandled exception while layout out windows", e);
8351 }
8352 }
8353
8354 private final void performLayoutLockedInner() {
8355 final int dw = mDisplay.getWidth();
8356 final int dh = mDisplay.getHeight();
8357
8358 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008359 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008360 int i;
8361
8362 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008363
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008364 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 mPolicy.beginLayoutLw(dw, dh);
8366
8367 // First perform layout of any root windows (not attached
8368 // to another window).
8369 int topAttached = -1;
8370 for (i = N-1; i >= 0; i--) {
8371 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008372
8373 // Don't do layout of a window if it is not visible, or
8374 // soon won't be visible, to avoid wasting time and funky
8375 // changes while a window is animating away.
8376 final AppWindowToken atoken = win.mAppToken;
8377 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008379 || win.mRootToken.hidden
8380 || (atoken != null && atoken.hiddenRequested)
8381 || !win.mPolicyVisibility
8382 || win.mAttachedHidden
8383 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384
8385 // If this view is GONE, then skip it -- keep the current
8386 // frame, and let the caller know so they can ignore it
8387 // if they want. (We do the normal layout for INVISIBLE
8388 // windows, since that means "perform layout as normal,
8389 // just don't display").
8390 if (!gone || !win.mHaveFrame) {
8391 if (!win.mLayoutAttached) {
8392 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8393 } else {
8394 if (topAttached < 0) topAttached = i;
8395 }
8396 }
8397 }
Romain Guy06882f82009-06-10 13:36:04 -07008398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008399 // Now perform layout of attached windows, which usually
8400 // depend on the position of the window they are attached to.
8401 // XXX does not deal with windows that are attached to windows
8402 // that are themselves attached.
8403 for (i = topAttached; i >= 0; i--) {
8404 WindowState win = (WindowState) mWindows.get(i);
8405
8406 // If this view is GONE, then skip it -- keep the current
8407 // frame, and let the caller know so they can ignore it
8408 // if they want. (We do the normal layout for INVISIBLE
8409 // windows, since that means "perform layout as normal,
8410 // just don't display").
8411 if (win.mLayoutAttached) {
8412 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8413 || !win.mHaveFrame) {
8414 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8415 }
8416 }
8417 }
8418
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008419 if (!mPolicy.finishLayoutLw()) {
8420 mLayoutNeeded = false;
8421 } else if (repeats > 2) {
8422 Log.w(TAG, "Layout repeat aborted after too many iterations");
8423 mLayoutNeeded = false;
8424 } else {
8425 repeats++;
8426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 }
8428 }
Romain Guy06882f82009-06-10 13:36:04 -07008429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 private final void performLayoutAndPlaceSurfacesLockedInner(
8431 boolean recoveringMemory) {
8432 final long currentTime = SystemClock.uptimeMillis();
8433 final int dw = mDisplay.getWidth();
8434 final int dh = mDisplay.getHeight();
8435
8436 final int N = mWindows.size();
8437 int i;
8438
8439 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008440 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008442 if (mFxSession == null) {
8443 mFxSession = new SurfaceSession();
8444 }
Romain Guy06882f82009-06-10 13:36:04 -07008445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8447
8448 // Initialize state of exiting tokens.
8449 for (i=mExitingTokens.size()-1; i>=0; i--) {
8450 mExitingTokens.get(i).hasVisible = false;
8451 }
8452
8453 // Initialize state of exiting applications.
8454 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8455 mExitingAppTokens.get(i).hasVisible = false;
8456 }
8457
8458 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 boolean orientationChangeComplete = true;
8460 Session holdScreen = null;
8461 float screenBrightness = -1;
8462 boolean focusDisplayed = false;
8463 boolean animating = false;
8464
8465 Surface.openTransaction();
8466 try {
8467 boolean restart;
8468
8469 do {
8470 final int transactionSequence = ++mTransactionSequence;
8471
8472 // Update animations of all applications, including those
8473 // associated with exiting/removed apps
8474 boolean tokensAnimating = false;
8475 final int NAT = mAppTokens.size();
8476 for (i=0; i<NAT; i++) {
8477 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8478 tokensAnimating = true;
8479 }
8480 }
8481 final int NEAT = mExitingAppTokens.size();
8482 for (i=0; i<NEAT; i++) {
8483 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8484 tokensAnimating = true;
8485 }
8486 }
8487
8488 animating = tokensAnimating;
8489 restart = false;
8490
8491 boolean tokenMayBeDrawn = false;
8492
8493 mPolicy.beginAnimationLw(dw, dh);
8494
8495 for (i=N-1; i>=0; i--) {
8496 WindowState w = (WindowState)mWindows.get(i);
8497
8498 final WindowManager.LayoutParams attrs = w.mAttrs;
8499
8500 if (w.mSurface != null) {
8501 // Execute animation.
8502 w.commitFinishDrawingLocked(currentTime);
8503 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8504 animating = true;
8505 //w.dump(" ");
8506 }
8507
8508 mPolicy.animatingWindowLw(w, attrs);
8509 }
8510
8511 final AppWindowToken atoken = w.mAppToken;
8512 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8513 if (atoken.lastTransactionSequence != transactionSequence) {
8514 atoken.lastTransactionSequence = transactionSequence;
8515 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8516 atoken.startingDisplayed = false;
8517 }
8518 if ((w.isOnScreen() || w.mAttrs.type
8519 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8520 && !w.mExiting && !w.mDestroying) {
8521 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8522 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8523 + w.isDisplayedLw()
8524 + ", isAnimating=" + w.isAnimating());
8525 if (!w.isDisplayedLw()) {
8526 Log.v(TAG, "Not displayed: s=" + w.mSurface
8527 + " pv=" + w.mPolicyVisibility
8528 + " dp=" + w.mDrawPending
8529 + " cdp=" + w.mCommitDrawPending
8530 + " ah=" + w.mAttachedHidden
8531 + " th=" + atoken.hiddenRequested
8532 + " a=" + w.mAnimating);
8533 }
8534 }
8535 if (w != atoken.startingWindow) {
8536 if (!atoken.freezingScreen || !w.mAppFreezing) {
8537 atoken.numInterestingWindows++;
8538 if (w.isDisplayedLw()) {
8539 atoken.numDrawnWindows++;
8540 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8541 "tokenMayBeDrawn: " + atoken
8542 + " freezingScreen=" + atoken.freezingScreen
8543 + " mAppFreezing=" + w.mAppFreezing);
8544 tokenMayBeDrawn = true;
8545 }
8546 }
8547 } else if (w.isDisplayedLw()) {
8548 atoken.startingDisplayed = true;
8549 }
8550 }
8551 } else if (w.mReadyToShow) {
8552 w.performShowLocked();
8553 }
8554 }
8555
8556 if (mPolicy.finishAnimationLw()) {
8557 restart = true;
8558 }
8559
8560 if (tokenMayBeDrawn) {
8561 // See if any windows have been drawn, so they (and others
8562 // associated with them) can now be shown.
8563 final int NT = mTokenList.size();
8564 for (i=0; i<NT; i++) {
8565 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8566 if (wtoken == null) {
8567 continue;
8568 }
8569 if (wtoken.freezingScreen) {
8570 int numInteresting = wtoken.numInterestingWindows;
8571 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8572 if (DEBUG_VISIBILITY) Log.v(TAG,
8573 "allDrawn: " + wtoken
8574 + " interesting=" + numInteresting
8575 + " drawn=" + wtoken.numDrawnWindows);
8576 wtoken.showAllWindowsLocked();
8577 unsetAppFreezingScreenLocked(wtoken, false, true);
8578 orientationChangeComplete = true;
8579 }
8580 } else if (!wtoken.allDrawn) {
8581 int numInteresting = wtoken.numInterestingWindows;
8582 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8583 if (DEBUG_VISIBILITY) Log.v(TAG,
8584 "allDrawn: " + wtoken
8585 + " interesting=" + numInteresting
8586 + " drawn=" + wtoken.numDrawnWindows);
8587 wtoken.allDrawn = true;
8588 restart = true;
8589
8590 // We can now show all of the drawn windows!
8591 if (!mOpeningApps.contains(wtoken)) {
8592 wtoken.showAllWindowsLocked();
8593 }
8594 }
8595 }
8596 }
8597 }
8598
8599 // If we are ready to perform an app transition, check through
8600 // all of the app tokens to be shown and see if they are ready
8601 // to go.
8602 if (mAppTransitionReady) {
8603 int NN = mOpeningApps.size();
8604 boolean goodToGo = true;
8605 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8606 "Checking " + NN + " opening apps (frozen="
8607 + mDisplayFrozen + " timeout="
8608 + mAppTransitionTimeout + ")...");
8609 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8610 // If the display isn't frozen, wait to do anything until
8611 // all of the apps are ready. Otherwise just go because
8612 // we'll unfreeze the display when everyone is ready.
8613 for (i=0; i<NN && goodToGo; i++) {
8614 AppWindowToken wtoken = mOpeningApps.get(i);
8615 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8616 "Check opening app" + wtoken + ": allDrawn="
8617 + wtoken.allDrawn + " startingDisplayed="
8618 + wtoken.startingDisplayed);
8619 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8620 && !wtoken.startingMoved) {
8621 goodToGo = false;
8622 }
8623 }
8624 }
8625 if (goodToGo) {
8626 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8627 int transit = mNextAppTransition;
8628 if (mSkipAppTransitionAnimation) {
8629 transit = WindowManagerPolicy.TRANSIT_NONE;
8630 }
8631 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8632 mAppTransitionReady = false;
8633 mAppTransitionTimeout = false;
8634 mStartingIconInTransition = false;
8635 mSkipAppTransitionAnimation = false;
8636
8637 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8638
8639 // We need to figure out which animation to use...
8640 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8641 mOpeningApps, mClosingApps);
8642
8643 NN = mOpeningApps.size();
8644 for (i=0; i<NN; i++) {
8645 AppWindowToken wtoken = mOpeningApps.get(i);
8646 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8647 "Now opening app" + wtoken);
8648 wtoken.reportedVisible = false;
8649 wtoken.inPendingTransaction = false;
8650 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8651 wtoken.updateReportedVisibilityLocked();
8652 wtoken.showAllWindowsLocked();
8653 }
8654 NN = mClosingApps.size();
8655 for (i=0; i<NN; i++) {
8656 AppWindowToken wtoken = mClosingApps.get(i);
8657 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8658 "Now closing app" + wtoken);
8659 wtoken.inPendingTransaction = false;
8660 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8661 wtoken.updateReportedVisibilityLocked();
8662 // Force the allDrawn flag, because we want to start
8663 // this guy's animations regardless of whether it's
8664 // gotten drawn.
8665 wtoken.allDrawn = true;
8666 }
8667
8668 mOpeningApps.clear();
8669 mClosingApps.clear();
8670
8671 // This has changed the visibility of windows, so perform
8672 // a new layout to get them all up-to-date.
8673 mLayoutNeeded = true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008674 adjustWallpaperWindowsLocked();
Dianne Hackborn20583ff2009-07-27 21:51:05 -07008675 if (!moveInputMethodWindowsIfNeededLocked(true)) {
8676 assignLayersLocked();
8677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 performLayoutLockedInner();
8679 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8680
8681 restart = true;
8682 }
8683 }
8684 } while (restart);
8685
8686 // THIRD LOOP: Update the surfaces of all windows.
8687
8688 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8689
8690 boolean obscured = false;
8691 boolean blurring = false;
8692 boolean dimming = false;
8693 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008694 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008695 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008696
8697 for (i=N-1; i>=0; i--) {
8698 WindowState w = (WindowState)mWindows.get(i);
8699
8700 boolean displayed = false;
8701 final WindowManager.LayoutParams attrs = w.mAttrs;
8702 final int attrFlags = attrs.flags;
8703
8704 if (w.mSurface != null) {
8705 w.computeShownFrameLocked();
8706 if (localLOGV) Log.v(
8707 TAG, "Placing surface #" + i + " " + w.mSurface
8708 + ": new=" + w.mShownFrame + ", old="
8709 + w.mLastShownFrame);
8710
8711 boolean resize;
8712 int width, height;
8713 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8714 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8715 w.mLastRequestedHeight != w.mRequestedHeight;
8716 // for a scaled surface, we just want to use
8717 // the requested size.
8718 width = w.mRequestedWidth;
8719 height = w.mRequestedHeight;
8720 w.mLastRequestedWidth = width;
8721 w.mLastRequestedHeight = height;
8722 w.mLastShownFrame.set(w.mShownFrame);
8723 try {
8724 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8725 } catch (RuntimeException e) {
8726 Log.w(TAG, "Error positioning surface in " + w, e);
8727 if (!recoveringMemory) {
8728 reclaimSomeSurfaceMemoryLocked(w, "position");
8729 }
8730 }
8731 } else {
8732 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8733 width = w.mShownFrame.width();
8734 height = w.mShownFrame.height();
8735 w.mLastShownFrame.set(w.mShownFrame);
8736 if (resize) {
8737 if (SHOW_TRANSACTIONS) Log.i(
8738 TAG, " SURFACE " + w.mSurface + ": ("
8739 + w.mShownFrame.left + ","
8740 + w.mShownFrame.top + ") ("
8741 + w.mShownFrame.width() + "x"
8742 + w.mShownFrame.height() + ")");
8743 }
8744 }
8745
8746 if (resize) {
8747 if (width < 1) width = 1;
8748 if (height < 1) height = 1;
8749 if (w.mSurface != null) {
8750 try {
8751 w.mSurface.setSize(width, height);
8752 w.mSurface.setPosition(w.mShownFrame.left,
8753 w.mShownFrame.top);
8754 } catch (RuntimeException e) {
8755 // If something goes wrong with the surface (such
8756 // as running out of memory), don't take down the
8757 // entire system.
8758 Log.e(TAG, "Failure updating surface of " + w
8759 + "size=(" + width + "x" + height
8760 + "), pos=(" + w.mShownFrame.left
8761 + "," + w.mShownFrame.top + ")", e);
8762 if (!recoveringMemory) {
8763 reclaimSomeSurfaceMemoryLocked(w, "size");
8764 }
8765 }
8766 }
8767 }
8768 if (!w.mAppFreezing) {
8769 w.mContentInsetsChanged =
8770 !w.mLastContentInsets.equals(w.mContentInsets);
8771 w.mVisibleInsetsChanged =
8772 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008773 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008774 || w.mContentInsetsChanged
8775 || w.mVisibleInsetsChanged) {
8776 w.mLastFrame.set(w.mFrame);
8777 w.mLastContentInsets.set(w.mContentInsets);
8778 w.mLastVisibleInsets.set(w.mVisibleInsets);
8779 // If the orientation is changing, then we need to
8780 // hold off on unfreezing the display until this
8781 // window has been redrawn; to do that, we need
8782 // to go through the process of getting informed
8783 // by the application when it has finished drawing.
8784 if (w.mOrientationChanging) {
8785 if (DEBUG_ORIENTATION) Log.v(TAG,
8786 "Orientation start waiting for draw in "
8787 + w + ", surface " + w.mSurface);
8788 w.mDrawPending = true;
8789 w.mCommitDrawPending = false;
8790 w.mReadyToShow = false;
8791 if (w.mAppToken != null) {
8792 w.mAppToken.allDrawn = false;
8793 }
8794 }
Romain Guy06882f82009-06-10 13:36:04 -07008795 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008796 "Resizing window " + w + " to " + w.mFrame);
8797 mResizingWindows.add(w);
8798 } else if (w.mOrientationChanging) {
8799 if (!w.mDrawPending && !w.mCommitDrawPending) {
8800 if (DEBUG_ORIENTATION) Log.v(TAG,
8801 "Orientation not waiting for draw in "
8802 + w + ", surface " + w.mSurface);
8803 w.mOrientationChanging = false;
8804 }
8805 }
8806 }
8807
8808 if (w.mAttachedHidden) {
8809 if (!w.mLastHidden) {
8810 //dump();
8811 w.mLastHidden = true;
8812 if (SHOW_TRANSACTIONS) Log.i(
8813 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8814 if (w.mSurface != null) {
8815 try {
8816 w.mSurface.hide();
8817 } catch (RuntimeException e) {
8818 Log.w(TAG, "Exception hiding surface in " + w);
8819 }
8820 }
8821 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8822 }
8823 // If we are waiting for this window to handle an
8824 // orientation change, well, it is hidden, so
8825 // doesn't really matter. Note that this does
8826 // introduce a potential glitch if the window
8827 // becomes unhidden before it has drawn for the
8828 // new orientation.
8829 if (w.mOrientationChanging) {
8830 w.mOrientationChanging = false;
8831 if (DEBUG_ORIENTATION) Log.v(TAG,
8832 "Orientation change skips hidden " + w);
8833 }
8834 } else if (!w.isReadyForDisplay()) {
8835 if (!w.mLastHidden) {
8836 //dump();
8837 w.mLastHidden = true;
8838 if (SHOW_TRANSACTIONS) Log.i(
8839 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8840 if (w.mSurface != null) {
8841 try {
8842 w.mSurface.hide();
8843 } catch (RuntimeException e) {
8844 Log.w(TAG, "Exception exception hiding surface in " + w);
8845 }
8846 }
8847 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8848 }
8849 // If we are waiting for this window to handle an
8850 // orientation change, well, it is hidden, so
8851 // doesn't really matter. Note that this does
8852 // introduce a potential glitch if the window
8853 // becomes unhidden before it has drawn for the
8854 // new orientation.
8855 if (w.mOrientationChanging) {
8856 w.mOrientationChanging = false;
8857 if (DEBUG_ORIENTATION) Log.v(TAG,
8858 "Orientation change skips hidden " + w);
8859 }
8860 } else if (w.mLastLayer != w.mAnimLayer
8861 || w.mLastAlpha != w.mShownAlpha
8862 || w.mLastDsDx != w.mDsDx
8863 || w.mLastDtDx != w.mDtDx
8864 || w.mLastDsDy != w.mDsDy
8865 || w.mLastDtDy != w.mDtDy
8866 || w.mLastHScale != w.mHScale
8867 || w.mLastVScale != w.mVScale
8868 || w.mLastHidden) {
8869 displayed = true;
8870 w.mLastAlpha = w.mShownAlpha;
8871 w.mLastLayer = w.mAnimLayer;
8872 w.mLastDsDx = w.mDsDx;
8873 w.mLastDtDx = w.mDtDx;
8874 w.mLastDsDy = w.mDsDy;
8875 w.mLastDtDy = w.mDtDy;
8876 w.mLastHScale = w.mHScale;
8877 w.mLastVScale = w.mVScale;
8878 if (SHOW_TRANSACTIONS) Log.i(
8879 TAG, " SURFACE " + w.mSurface + ": alpha="
8880 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8881 if (w.mSurface != null) {
8882 try {
8883 w.mSurface.setAlpha(w.mShownAlpha);
8884 w.mSurface.setLayer(w.mAnimLayer);
8885 w.mSurface.setMatrix(
8886 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8887 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8888 } catch (RuntimeException e) {
8889 Log.w(TAG, "Error updating surface in " + w, e);
8890 if (!recoveringMemory) {
8891 reclaimSomeSurfaceMemoryLocked(w, "update");
8892 }
8893 }
8894 }
8895
8896 if (w.mLastHidden && !w.mDrawPending
8897 && !w.mCommitDrawPending
8898 && !w.mReadyToShow) {
8899 if (SHOW_TRANSACTIONS) Log.i(
8900 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8901 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8902 + " during relayout");
8903 if (showSurfaceRobustlyLocked(w)) {
8904 w.mHasDrawn = true;
8905 w.mLastHidden = false;
8906 } else {
8907 w.mOrientationChanging = false;
8908 }
8909 }
8910 if (w.mSurface != null) {
8911 w.mToken.hasVisible = true;
8912 }
8913 } else {
8914 displayed = true;
8915 }
8916
8917 if (displayed) {
8918 if (!covered) {
8919 if (attrs.width == LayoutParams.FILL_PARENT
8920 && attrs.height == LayoutParams.FILL_PARENT) {
8921 covered = true;
8922 }
8923 }
8924 if (w.mOrientationChanging) {
8925 if (w.mDrawPending || w.mCommitDrawPending) {
8926 orientationChangeComplete = false;
8927 if (DEBUG_ORIENTATION) Log.v(TAG,
8928 "Orientation continue waiting for draw in " + w);
8929 } else {
8930 w.mOrientationChanging = false;
8931 if (DEBUG_ORIENTATION) Log.v(TAG,
8932 "Orientation change complete in " + w);
8933 }
8934 }
8935 w.mToken.hasVisible = true;
8936 }
8937 } else if (w.mOrientationChanging) {
8938 if (DEBUG_ORIENTATION) Log.v(TAG,
8939 "Orientation change skips hidden " + w);
8940 w.mOrientationChanging = false;
8941 }
8942
8943 final boolean canBeSeen = w.isDisplayedLw();
8944
8945 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8946 focusDisplayed = true;
8947 }
8948
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07008949 final boolean obscuredChanged = w.mObscured != obscured;
8950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008951 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008952 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008953 if (w.mSurface != null) {
8954 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8955 holdScreen = w.mSession;
8956 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008957 if (!syswin && w.mAttrs.screenBrightness >= 0
8958 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008959 screenBrightness = w.mAttrs.screenBrightness;
8960 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008961 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8962 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8963 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8964 syswin = true;
8965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008966 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008967
8968 boolean opaqueDrawn = w.isOpaqueDrawn();
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008969 if ((opaqueDrawn && w.isFullscreen(dw, dh))
8970 || attrs.type == TYPE_WALLPAPER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008971 // This window completely covers everything behind it,
8972 // so we want to leave all of them as unblurred (for
8973 // performance reasons).
8974 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008975 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8976 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008977 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008978 obscured = true;
8979 if (mBackgroundFillerSurface == null) {
8980 try {
8981 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8982 0, dw, dh,
8983 PixelFormat.OPAQUE,
8984 Surface.FX_SURFACE_NORMAL);
8985 } catch (Exception e) {
8986 Log.e(TAG, "Exception creating filler surface", e);
8987 }
8988 }
8989 try {
8990 mBackgroundFillerSurface.setPosition(0, 0);
8991 mBackgroundFillerSurface.setSize(dw, dh);
8992 // Using the same layer as Dim because they will never be shown at the
8993 // same time.
8994 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8995 mBackgroundFillerSurface.show();
8996 } catch (RuntimeException e) {
8997 Log.e(TAG, "Exception showing filler surface");
8998 }
8999 backgroundFillerShown = true;
9000 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009001 } else if (canBeSeen && !obscured &&
9002 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9003 if (localLOGV) Log.v(TAG, "Win " + w
9004 + ": blurring=" + blurring
9005 + " obscured=" + obscured
9006 + " displayed=" + displayed);
9007 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9008 if (!dimming) {
9009 //Log.i(TAG, "DIM BEHIND: " + w);
9010 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009011 if (mDimAnimator == null) {
9012 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009013 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009014 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009015 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009016 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009017 }
9018 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9019 if (!blurring) {
9020 //Log.i(TAG, "BLUR BEHIND: " + w);
9021 blurring = true;
9022 mBlurShown = true;
9023 if (mBlurSurface == null) {
9024 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9025 + mBlurSurface + ": CREATE");
9026 try {
Romain Guy06882f82009-06-10 13:36:04 -07009027 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 -1, 16, 16,
9029 PixelFormat.OPAQUE,
9030 Surface.FX_SURFACE_BLUR);
9031 } catch (Exception e) {
9032 Log.e(TAG, "Exception creating Blur surface", e);
9033 }
9034 }
9035 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9036 + mBlurSurface + ": SHOW pos=(0,0) (" +
9037 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9038 if (mBlurSurface != null) {
9039 mBlurSurface.setPosition(0, 0);
9040 mBlurSurface.setSize(dw, dh);
9041 try {
9042 mBlurSurface.show();
9043 } catch (RuntimeException e) {
9044 Log.w(TAG, "Failure showing blur surface", e);
9045 }
9046 }
9047 }
9048 mBlurSurface.setLayer(w.mAnimLayer-2);
9049 }
9050 }
9051 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009052
9053 if (obscuredChanged && mWallpaperTarget == w) {
9054 // This is the wallpaper target and its obscured state
9055 // changed... make sure the current wallaper's visibility
9056 // has been updated accordingly.
9057 updateWallpaperVisibilityLocked();
9058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009059 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009060
9061 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9062 mBackgroundFillerShown = false;
9063 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9064 try {
9065 mBackgroundFillerSurface.hide();
9066 } catch (RuntimeException e) {
9067 Log.e(TAG, "Exception hiding filler surface", e);
9068 }
9069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009070
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009071 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9072 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009073 }
Romain Guy06882f82009-06-10 13:36:04 -07009074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009075 if (!blurring && mBlurShown) {
9076 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9077 + ": HIDE");
9078 try {
9079 mBlurSurface.hide();
9080 } catch (IllegalArgumentException e) {
9081 Log.w(TAG, "Illegal argument exception hiding blur surface");
9082 }
9083 mBlurShown = false;
9084 }
9085
9086 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9087 } catch (RuntimeException e) {
9088 Log.e(TAG, "Unhandled exception in Window Manager", e);
9089 }
9090
9091 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009093 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9094 "With display frozen, orientationChangeComplete="
9095 + orientationChangeComplete);
9096 if (orientationChangeComplete) {
9097 if (mWindowsFreezingScreen) {
9098 mWindowsFreezingScreen = false;
9099 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9100 }
9101 if (mAppsFreezingScreen == 0) {
9102 stopFreezingDisplayLocked();
9103 }
9104 }
Romain Guy06882f82009-06-10 13:36:04 -07009105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009106 i = mResizingWindows.size();
9107 if (i > 0) {
9108 do {
9109 i--;
9110 WindowState win = mResizingWindows.get(i);
9111 try {
9112 win.mClient.resized(win.mFrame.width(),
9113 win.mFrame.height(), win.mLastContentInsets,
9114 win.mLastVisibleInsets, win.mDrawPending);
9115 win.mContentInsetsChanged = false;
9116 win.mVisibleInsetsChanged = false;
9117 } catch (RemoteException e) {
9118 win.mOrientationChanging = false;
9119 }
9120 } while (i > 0);
9121 mResizingWindows.clear();
9122 }
Romain Guy06882f82009-06-10 13:36:04 -07009123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009124 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009125 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009126 i = mDestroySurface.size();
9127 if (i > 0) {
9128 do {
9129 i--;
9130 WindowState win = mDestroySurface.get(i);
9131 win.mDestroying = false;
9132 if (mInputMethodWindow == win) {
9133 mInputMethodWindow = null;
9134 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009135 if (win == mWallpaperTarget) {
9136 wallpaperDestroyed = true;
9137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009138 win.destroySurfaceLocked();
9139 } while (i > 0);
9140 mDestroySurface.clear();
9141 }
9142
9143 // Time to remove any exiting tokens?
9144 for (i=mExitingTokens.size()-1; i>=0; i--) {
9145 WindowToken token = mExitingTokens.get(i);
9146 if (!token.hasVisible) {
9147 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009148 if (token.windowType == TYPE_WALLPAPER) {
9149 mWallpaperTokens.remove(token);
9150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009151 }
9152 }
9153
9154 // Time to remove any exiting applications?
9155 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9156 AppWindowToken token = mExitingAppTokens.get(i);
9157 if (!token.hasVisible && !mClosingApps.contains(token)) {
9158 mAppTokens.remove(token);
9159 mExitingAppTokens.remove(i);
9160 }
9161 }
9162
9163 if (focusDisplayed) {
9164 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9165 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009166 if (wallpaperDestroyed) {
9167 wallpaperDestroyed = adjustWallpaperWindowsLocked();
9168 }
9169 if (wallpaperDestroyed) {
9170 requestAnimationLocked(0);
9171 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009172 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9173 }
9174 mQueue.setHoldScreenLocked(holdScreen != null);
9175 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9176 mPowerManager.setScreenBrightnessOverride(-1);
9177 } else {
9178 mPowerManager.setScreenBrightnessOverride((int)
9179 (screenBrightness * Power.BRIGHTNESS_ON));
9180 }
9181 if (holdScreen != mHoldingScreenOn) {
9182 mHoldingScreenOn = holdScreen;
9183 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9184 mH.sendMessage(m);
9185 }
9186 }
9187
9188 void requestAnimationLocked(long delay) {
9189 if (!mAnimationPending) {
9190 mAnimationPending = true;
9191 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9192 }
9193 }
Romain Guy06882f82009-06-10 13:36:04 -07009194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009195 /**
9196 * Have the surface flinger show a surface, robustly dealing with
9197 * error conditions. In particular, if there is not enough memory
9198 * to show the surface, then we will try to get rid of other surfaces
9199 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009200 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009201 * @return Returns true if the surface was successfully shown.
9202 */
9203 boolean showSurfaceRobustlyLocked(WindowState win) {
9204 try {
9205 if (win.mSurface != null) {
9206 win.mSurface.show();
9207 }
9208 return true;
9209 } catch (RuntimeException e) {
9210 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9211 }
Romain Guy06882f82009-06-10 13:36:04 -07009212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009213 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215 return false;
9216 }
Romain Guy06882f82009-06-10 13:36:04 -07009217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009218 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9219 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009221 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9222 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009224 if (mForceRemoves == null) {
9225 mForceRemoves = new ArrayList<WindowState>();
9226 }
Romain Guy06882f82009-06-10 13:36:04 -07009227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 long callingIdentity = Binder.clearCallingIdentity();
9229 try {
9230 // There was some problem... first, do a sanity check of the
9231 // window list to make sure we haven't left any dangling surfaces
9232 // around.
9233 int N = mWindows.size();
9234 boolean leakedSurface = false;
9235 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9236 for (int i=0; i<N; i++) {
9237 WindowState ws = (WindowState)mWindows.get(i);
9238 if (ws.mSurface != null) {
9239 if (!mSessions.contains(ws.mSession)) {
9240 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9241 + ws + " surface=" + ws.mSurface
9242 + " token=" + win.mToken
9243 + " pid=" + ws.mSession.mPid
9244 + " uid=" + ws.mSession.mUid);
9245 ws.mSurface.clear();
9246 ws.mSurface = null;
9247 mForceRemoves.add(ws);
9248 i--;
9249 N--;
9250 leakedSurface = true;
9251 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9252 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9253 + ws + " surface=" + ws.mSurface
9254 + " token=" + win.mAppToken);
9255 ws.mSurface.clear();
9256 ws.mSurface = null;
9257 leakedSurface = true;
9258 }
9259 }
9260 }
Romain Guy06882f82009-06-10 13:36:04 -07009261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009262 boolean killedApps = false;
9263 if (!leakedSurface) {
9264 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9265 SparseIntArray pidCandidates = new SparseIntArray();
9266 for (int i=0; i<N; i++) {
9267 WindowState ws = (WindowState)mWindows.get(i);
9268 if (ws.mSurface != null) {
9269 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9270 }
9271 }
9272 if (pidCandidates.size() > 0) {
9273 int[] pids = new int[pidCandidates.size()];
9274 for (int i=0; i<pids.length; i++) {
9275 pids[i] = pidCandidates.keyAt(i);
9276 }
9277 try {
9278 if (mActivityManager.killPidsForMemory(pids)) {
9279 killedApps = true;
9280 }
9281 } catch (RemoteException e) {
9282 }
9283 }
9284 }
Romain Guy06882f82009-06-10 13:36:04 -07009285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009286 if (leakedSurface || killedApps) {
9287 // We managed to reclaim some memory, so get rid of the trouble
9288 // surface and ask the app to request another one.
9289 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9290 if (surface != null) {
9291 surface.clear();
9292 win.mSurface = null;
9293 }
Romain Guy06882f82009-06-10 13:36:04 -07009294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 try {
9296 win.mClient.dispatchGetNewSurface();
9297 } catch (RemoteException e) {
9298 }
9299 }
9300 } finally {
9301 Binder.restoreCallingIdentity(callingIdentity);
9302 }
9303 }
Romain Guy06882f82009-06-10 13:36:04 -07009304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009305 private boolean updateFocusedWindowLocked(int mode) {
9306 WindowState newFocus = computeFocusedWindowLocked();
9307 if (mCurrentFocus != newFocus) {
9308 // This check makes sure that we don't already have the focus
9309 // change message pending.
9310 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9311 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9312 if (localLOGV) Log.v(
9313 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9314 final WindowState oldFocus = mCurrentFocus;
9315 mCurrentFocus = newFocus;
9316 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009318 final WindowState imWindow = mInputMethodWindow;
9319 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009320 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009321 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009322 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9323 mLayoutNeeded = true;
9324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009325 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9326 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009327 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9328 // Client will do the layout, but we need to assign layers
9329 // for handleNewWindowLocked() below.
9330 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 }
9332 }
Romain Guy06882f82009-06-10 13:36:04 -07009333
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009334 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9335 mKeyWaiter.handleNewWindowLocked(newFocus);
9336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009337 return true;
9338 }
9339 return false;
9340 }
9341
9342 private WindowState computeFocusedWindowLocked() {
9343 WindowState result = null;
9344 WindowState win;
9345
9346 int i = mWindows.size() - 1;
9347 int nextAppIndex = mAppTokens.size()-1;
9348 WindowToken nextApp = nextAppIndex >= 0
9349 ? mAppTokens.get(nextAppIndex) : null;
9350
9351 while (i >= 0) {
9352 win = (WindowState)mWindows.get(i);
9353
9354 if (localLOGV || DEBUG_FOCUS) Log.v(
9355 TAG, "Looking for focus: " + i
9356 + " = " + win
9357 + ", flags=" + win.mAttrs.flags
9358 + ", canReceive=" + win.canReceiveKeys());
9359
9360 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009362 // If this window's application has been removed, just skip it.
9363 if (thisApp != null && thisApp.removed) {
9364 i--;
9365 continue;
9366 }
Romain Guy06882f82009-06-10 13:36:04 -07009367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009368 // If there is a focused app, don't allow focus to go to any
9369 // windows below it. If this is an application window, step
9370 // through the app tokens until we find its app.
9371 if (thisApp != null && nextApp != null && thisApp != nextApp
9372 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9373 int origAppIndex = nextAppIndex;
9374 while (nextAppIndex > 0) {
9375 if (nextApp == mFocusedApp) {
9376 // Whoops, we are below the focused app... no focus
9377 // for you!
9378 if (localLOGV || DEBUG_FOCUS) Log.v(
9379 TAG, "Reached focused app: " + mFocusedApp);
9380 return null;
9381 }
9382 nextAppIndex--;
9383 nextApp = mAppTokens.get(nextAppIndex);
9384 if (nextApp == thisApp) {
9385 break;
9386 }
9387 }
9388 if (thisApp != nextApp) {
9389 // Uh oh, the app token doesn't exist! This shouldn't
9390 // happen, but if it does we can get totally hosed...
9391 // so restart at the original app.
9392 nextAppIndex = origAppIndex;
9393 nextApp = mAppTokens.get(nextAppIndex);
9394 }
9395 }
9396
9397 // Dispatch to this window if it is wants key events.
9398 if (win.canReceiveKeys()) {
9399 if (DEBUG_FOCUS) Log.v(
9400 TAG, "Found focus @ " + i + " = " + win);
9401 result = win;
9402 break;
9403 }
9404
9405 i--;
9406 }
9407
9408 return result;
9409 }
9410
9411 private void startFreezingDisplayLocked() {
9412 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009413 // Freezing the display also suspends key event delivery, to
9414 // keep events from going astray while the display is reconfigured.
9415 // If someone has changed orientation again while the screen is
9416 // still frozen, the events will continue to be blocked while the
9417 // successive orientation change is processed. To prevent spurious
9418 // ANRs, we reset the event dispatch timeout in this case.
9419 synchronized (mKeyWaiter) {
9420 mKeyWaiter.mWasFrozen = true;
9421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009422 return;
9423 }
Romain Guy06882f82009-06-10 13:36:04 -07009424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009425 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 long now = SystemClock.uptimeMillis();
9428 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9429 if (mFreezeGcPending != 0) {
9430 if (now > (mFreezeGcPending+1000)) {
9431 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9432 mH.removeMessages(H.FORCE_GC);
9433 Runtime.getRuntime().gc();
9434 mFreezeGcPending = now;
9435 }
9436 } else {
9437 mFreezeGcPending = now;
9438 }
Romain Guy06882f82009-06-10 13:36:04 -07009439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440 mDisplayFrozen = true;
9441 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9442 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9443 mAppTransitionReady = true;
9444 }
Romain Guy06882f82009-06-10 13:36:04 -07009445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009446 if (PROFILE_ORIENTATION) {
9447 File file = new File("/data/system/frozen");
9448 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9449 }
9450 Surface.freezeDisplay(0);
9451 }
Romain Guy06882f82009-06-10 13:36:04 -07009452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009453 private void stopFreezingDisplayLocked() {
9454 if (!mDisplayFrozen) {
9455 return;
9456 }
Romain Guy06882f82009-06-10 13:36:04 -07009457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009458 mDisplayFrozen = false;
9459 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9460 if (PROFILE_ORIENTATION) {
9461 Debug.stopMethodTracing();
9462 }
9463 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009464
Chris Tate2ad63a92009-03-25 17:36:48 -07009465 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9466 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009467 synchronized (mKeyWaiter) {
9468 mKeyWaiter.mWasFrozen = true;
9469 mKeyWaiter.notifyAll();
9470 }
9471
9472 // A little kludge: a lot could have happened while the
9473 // display was frozen, so now that we are coming back we
9474 // do a gc so that any remote references the system
9475 // processes holds on others can be released if they are
9476 // no longer needed.
9477 mH.removeMessages(H.FORCE_GC);
9478 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9479 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009481 mScreenFrozenLock.release();
9482 }
Romain Guy06882f82009-06-10 13:36:04 -07009483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009484 @Override
9485 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9486 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9487 != PackageManager.PERMISSION_GRANTED) {
9488 pw.println("Permission Denial: can't dump WindowManager from from pid="
9489 + Binder.getCallingPid()
9490 + ", uid=" + Binder.getCallingUid());
9491 return;
9492 }
Romain Guy06882f82009-06-10 13:36:04 -07009493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009494 synchronized(mWindowMap) {
9495 pw.println("Current Window Manager state:");
9496 for (int i=mWindows.size()-1; i>=0; i--) {
9497 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009498 pw.print(" Window #"); pw.print(i); pw.print(' ');
9499 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009500 w.dump(pw, " ");
9501 }
9502 if (mInputMethodDialogs.size() > 0) {
9503 pw.println(" ");
9504 pw.println(" Input method dialogs:");
9505 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9506 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009507 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009508 }
9509 }
9510 if (mPendingRemove.size() > 0) {
9511 pw.println(" ");
9512 pw.println(" Remove pending for:");
9513 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9514 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009515 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9516 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009517 w.dump(pw, " ");
9518 }
9519 }
9520 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9521 pw.println(" ");
9522 pw.println(" Windows force removing:");
9523 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9524 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009525 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9526 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009527 w.dump(pw, " ");
9528 }
9529 }
9530 if (mDestroySurface.size() > 0) {
9531 pw.println(" ");
9532 pw.println(" Windows waiting to destroy their surface:");
9533 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9534 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009535 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9536 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009537 w.dump(pw, " ");
9538 }
9539 }
9540 if (mLosingFocus.size() > 0) {
9541 pw.println(" ");
9542 pw.println(" Windows losing focus:");
9543 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9544 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009545 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9546 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009547 w.dump(pw, " ");
9548 }
9549 }
9550 if (mSessions.size() > 0) {
9551 pw.println(" ");
9552 pw.println(" All active sessions:");
9553 Iterator<Session> it = mSessions.iterator();
9554 while (it.hasNext()) {
9555 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009556 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009557 s.dump(pw, " ");
9558 }
9559 }
9560 if (mTokenMap.size() > 0) {
9561 pw.println(" ");
9562 pw.println(" All tokens:");
9563 Iterator<WindowToken> it = mTokenMap.values().iterator();
9564 while (it.hasNext()) {
9565 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009566 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009567 token.dump(pw, " ");
9568 }
9569 }
9570 if (mTokenList.size() > 0) {
9571 pw.println(" ");
9572 pw.println(" Window token list:");
9573 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009574 pw.print(" #"); pw.print(i); pw.print(": ");
9575 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009576 }
9577 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009578 if (mWallpaperTokens.size() > 0) {
9579 pw.println(" ");
9580 pw.println(" Wallpaper tokens:");
9581 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
9582 WindowToken token = mWallpaperTokens.get(i);
9583 pw.print(" Wallpaper #"); pw.print(i);
9584 pw.print(' '); pw.print(token); pw.println(':');
9585 token.dump(pw, " ");
9586 }
9587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009588 if (mAppTokens.size() > 0) {
9589 pw.println(" ");
9590 pw.println(" Application tokens in Z order:");
9591 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009592 pw.print(" App #"); pw.print(i); pw.print(": ");
9593 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009594 }
9595 }
9596 if (mFinishedStarting.size() > 0) {
9597 pw.println(" ");
9598 pw.println(" Finishing start of application tokens:");
9599 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9600 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009601 pw.print(" Finished Starting #"); pw.print(i);
9602 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009603 token.dump(pw, " ");
9604 }
9605 }
9606 if (mExitingTokens.size() > 0) {
9607 pw.println(" ");
9608 pw.println(" Exiting tokens:");
9609 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9610 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009611 pw.print(" Exiting #"); pw.print(i);
9612 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009613 token.dump(pw, " ");
9614 }
9615 }
9616 if (mExitingAppTokens.size() > 0) {
9617 pw.println(" ");
9618 pw.println(" Exiting application tokens:");
9619 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9620 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009621 pw.print(" Exiting App #"); pw.print(i);
9622 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009623 token.dump(pw, " ");
9624 }
9625 }
9626 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009627 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9628 pw.print(" mLastFocus="); pw.println(mLastFocus);
9629 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9630 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9631 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -07009632 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009633 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9634 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9635 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9636 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9637 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009638 if (mDimAnimator != null) {
9639 mDimAnimator.printTo(pw);
9640 } else {
9641 pw.print( " no DimAnimator ");
9642 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009643 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009644 pw.print(mInputMethodAnimLayerAdjustment);
9645 pw.print(" mWallpaperAnimLayerAdjustment=");
9646 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009647 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9648 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9649 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9650 pw.print(" mRotation="); pw.print(mRotation);
9651 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9652 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9653 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9654 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9655 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9656 pw.print(" mNextAppTransition=0x");
9657 pw.print(Integer.toHexString(mNextAppTransition));
9658 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9659 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9660 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9661 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9662 if (mOpeningApps.size() > 0) {
9663 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9664 }
9665 if (mClosingApps.size() > 0) {
9666 pw.print(" mClosingApps="); pw.println(mClosingApps);
9667 }
9668 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9669 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009670 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009671 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9672 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9673 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9674 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9675 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9676 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009677 }
9678 }
9679
9680 public void monitor() {
9681 synchronized (mWindowMap) { }
9682 synchronized (mKeyguardDisabled) { }
9683 synchronized (mKeyWaiter) { }
9684 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009685
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07009686 public void virtualKeyFeedback(KeyEvent event) {
9687 mPolicy.keyFeedbackFromInput(event);
9688 }
9689
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009690 /**
9691 * DimAnimator class that controls the dim animation. This holds the surface and
9692 * all state used for dim animation.
9693 */
9694 private static class DimAnimator {
9695 Surface mDimSurface;
9696 boolean mDimShown = false;
9697 float mDimCurrentAlpha;
9698 float mDimTargetAlpha;
9699 float mDimDeltaPerMs;
9700 long mLastDimAnimTime;
9701
9702 DimAnimator (SurfaceSession session) {
9703 if (mDimSurface == null) {
9704 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9705 + mDimSurface + ": CREATE");
9706 try {
9707 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9708 Surface.FX_SURFACE_DIM);
9709 } catch (Exception e) {
9710 Log.e(TAG, "Exception creating Dim surface", e);
9711 }
9712 }
9713 }
9714
9715 /**
9716 * Show the dim surface.
9717 */
9718 void show(int dw, int dh) {
9719 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9720 dw + "x" + dh + ")");
9721 mDimShown = true;
9722 try {
9723 mDimSurface.setPosition(0, 0);
9724 mDimSurface.setSize(dw, dh);
9725 mDimSurface.show();
9726 } catch (RuntimeException e) {
9727 Log.w(TAG, "Failure showing dim surface", e);
9728 }
9729 }
9730
9731 /**
9732 * Set's the dim surface's layer and update dim parameters that will be used in
9733 * {@link updateSurface} after all windows are examined.
9734 */
9735 void updateParameters(WindowState w, long currentTime) {
9736 mDimSurface.setLayer(w.mAnimLayer-1);
9737
9738 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9739 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9740 if (mDimTargetAlpha != target) {
9741 // If the desired dim level has changed, then
9742 // start an animation to it.
9743 mLastDimAnimTime = currentTime;
9744 long duration = (w.mAnimating && w.mAnimation != null)
9745 ? w.mAnimation.computeDurationHint()
9746 : DEFAULT_DIM_DURATION;
9747 if (target > mDimTargetAlpha) {
9748 // This is happening behind the activity UI,
9749 // so we can make it run a little longer to
9750 // give a stronger impression without disrupting
9751 // the user.
9752 duration *= DIM_DURATION_MULTIPLIER;
9753 }
9754 if (duration < 1) {
9755 // Don't divide by zero
9756 duration = 1;
9757 }
9758 mDimTargetAlpha = target;
9759 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9760 }
9761 }
9762
9763 /**
9764 * Updating the surface's alpha. Returns true if the animation continues, or returns
9765 * false when the animation is finished and the dim surface is hidden.
9766 */
9767 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9768 if (!dimming) {
9769 if (mDimTargetAlpha != 0) {
9770 mLastDimAnimTime = currentTime;
9771 mDimTargetAlpha = 0;
9772 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9773 }
9774 }
9775
9776 boolean animating = false;
9777 if (mLastDimAnimTime != 0) {
9778 mDimCurrentAlpha += mDimDeltaPerMs
9779 * (currentTime-mLastDimAnimTime);
9780 boolean more = true;
9781 if (displayFrozen) {
9782 // If the display is frozen, there is no reason to animate.
9783 more = false;
9784 } else if (mDimDeltaPerMs > 0) {
9785 if (mDimCurrentAlpha > mDimTargetAlpha) {
9786 more = false;
9787 }
9788 } else if (mDimDeltaPerMs < 0) {
9789 if (mDimCurrentAlpha < mDimTargetAlpha) {
9790 more = false;
9791 }
9792 } else {
9793 more = false;
9794 }
9795
9796 // Do we need to continue animating?
9797 if (more) {
9798 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9799 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9800 mLastDimAnimTime = currentTime;
9801 mDimSurface.setAlpha(mDimCurrentAlpha);
9802 animating = true;
9803 } else {
9804 mDimCurrentAlpha = mDimTargetAlpha;
9805 mLastDimAnimTime = 0;
9806 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9807 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9808 mDimSurface.setAlpha(mDimCurrentAlpha);
9809 if (!dimming) {
9810 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9811 + ": HIDE");
9812 try {
9813 mDimSurface.hide();
9814 } catch (RuntimeException e) {
9815 Log.w(TAG, "Illegal argument exception hiding dim surface");
9816 }
9817 mDimShown = false;
9818 }
9819 }
9820 }
9821 return animating;
9822 }
9823
9824 public void printTo(PrintWriter pw) {
9825 pw.print(" mDimShown="); pw.print(mDimShown);
9826 pw.print(" current="); pw.print(mDimCurrentAlpha);
9827 pw.print(" target="); pw.print(mDimTargetAlpha);
9828 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9829 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9830 }
9831 }
9832
9833 /**
9834 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9835 * This is used for opening/closing transition for apps in compatible mode.
9836 */
9837 private static class FadeInOutAnimation extends Animation {
9838 int mWidth;
9839 boolean mFadeIn;
9840
9841 public FadeInOutAnimation(boolean fadeIn) {
9842 setInterpolator(new AccelerateInterpolator());
9843 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9844 mFadeIn = fadeIn;
9845 }
9846
9847 @Override
9848 protected void applyTransformation(float interpolatedTime, Transformation t) {
9849 float x = interpolatedTime;
9850 if (!mFadeIn) {
9851 x = 1.0f - x; // reverse the interpolation for fade out
9852 }
9853 if (x < 0.5) {
9854 // move the window out of the screen.
9855 t.getMatrix().setTranslate(mWidth, 0);
9856 } else {
9857 t.getMatrix().setTranslate(0, 0);// show
9858 t.setAlpha((x - 0.5f) * 2);
9859 }
9860 }
9861
9862 @Override
9863 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9864 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9865 mWidth = width;
9866 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009867
9868 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07009869 public int getZAdjustment() {
9870 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009871 }
9872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009873}