blob: 988c3d5ab8c3c7f519b067536a530dc3dd17b662 [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;
1208
1209 // If the wallpaper target is animating, we may need to copy
1210 // its layer adjustment.
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001211 mWallpaperAnimLayerAdjustment = w.mAppToken != null
1212 ? w.mAppToken.animLayerAdjustment : 0;
1213
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001214 // Now w is the window we are supposed to be behind... but we
1215 // need to be sure to also be behind any of its attached windows,
1216 // AND any starting window associated with it.
1217 while (i > 0) {
1218 WindowState wb = (WindowState)localmWindows.get(i-1);
1219 if (wb.mAttachedWindow != w &&
1220 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
1221 wb.mToken != w.mToken)) {
1222 // This window is not related to the previous one in any
1223 // interesting way, so stop here.
1224 break;
1225 }
1226 w = wb;
1227 i--;
1228 }
1229 }
1230
1231 // Okay i is the position immediately above the wallpaper. Look at
1232 // what is below it for later.
1233 w = i > 0 ? (WindowState)localmWindows.get(i-1) : null;
1234
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001235 final int dw = mDisplay.getWidth();
1236 final int dh = mDisplay.getHeight();
1237
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001238 // Start stepping backwards from here, ensuring that our wallpaper windows
1239 // are correctly placed.
1240 int curTokenIndex = mWallpaperTokens.size();
1241 while (curTokenIndex > 0) {
1242 curTokenIndex--;
1243 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1244 int curWallpaperIndex = token.windows.size();
1245 while (curWallpaperIndex > 0) {
1246 curWallpaperIndex--;
1247 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001248
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001249 if (visible) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001250 updateWallpaperOffsetLocked(mWallpaperTarget,
1251 wallpaper, dw, dh);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001252 }
1253
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001254 // First, make sure the client has the current visibility
1255 // state.
1256 if (wallpaper.mWallpaperVisible != visible) {
1257 wallpaper.mWallpaperVisible = visible;
1258 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001259 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001260 "Setting visibility of wallpaper " + wallpaper
1261 + ": " + visible);
1262 wallpaper.mClient.dispatchAppVisibility(visible);
1263 } catch (RemoteException e) {
1264 }
1265 }
1266
1267 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001268 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1269 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001270
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001271 // First, if this window is at the current index, then all
1272 // is well.
1273 if (wallpaper == w) {
1274 i--;
1275 w = i > 0 ? (WindowState)localmWindows.get(i-1) : null;
1276 continue;
1277 }
1278
1279 // The window didn't match... the current wallpaper window,
1280 // wherever it is, is in the wrong place, so make sure it is
1281 // not in the list.
1282 int oldIndex = localmWindows.indexOf(wallpaper);
1283 if (oldIndex >= 0) {
1284 localmWindows.remove(oldIndex);
1285 if (oldIndex < i) {
1286 i--;
1287 }
1288 }
1289
1290 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001291 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
1292 + " from " + oldIndex + " to " + i);
1293
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001294 localmWindows.add(i, wallpaper);
1295 changed = true;
1296 }
1297 }
1298
1299 return changed;
1300 }
1301
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001302 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001303 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1304 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001305 mWallpaperAnimLayerAdjustment = adj;
1306 int curTokenIndex = mWallpaperTokens.size();
1307 while (curTokenIndex > 0) {
1308 curTokenIndex--;
1309 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1310 int curWallpaperIndex = token.windows.size();
1311 while (curWallpaperIndex > 0) {
1312 curWallpaperIndex--;
1313 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1314 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001315 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1316 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001317 }
1318 }
1319 }
1320
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001321 boolean updateWallpaperOffsetLocked(WindowState target,
1322 WindowState wallpaperWin, int dw, int dh) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001323 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001324 boolean rawChanged = false;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001325 if (target.mWallpaperX >= 0) {
1326 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1327 int offset = availw > 0 ? -(int)(availw*target.mWallpaperX+.5f) : 0;
1328 changed = wallpaperWin.mXOffset != offset;
1329 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001330 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1331 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001332 wallpaperWin.mXOffset = offset;
1333 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001334 if (wallpaperWin.mWallpaperX != target.mWallpaperX) {
1335 wallpaperWin.mWallpaperX = target.mWallpaperX;
1336 rawChanged = true;
1337 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001338 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001339
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001340 if (target.mWallpaperY >= 0) {
1341 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1342 int offset = availh > 0 ? -(int)(availh*target.mWallpaperY+.5f) : 0;
1343 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001344 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1345 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001346 changed = true;
1347 wallpaperWin.mYOffset = offset;
1348 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001349 if (wallpaperWin.mWallpaperY != target.mWallpaperY) {
1350 wallpaperWin.mWallpaperY = target.mWallpaperY;
1351 rawChanged = true;
1352 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001353 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001354
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001355 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001356 try {
1357 wallpaperWin.mClient.dispatchWallpaperOffsets(
1358 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY);
1359 } catch (RemoteException e) {
1360 }
1361 }
1362
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001363 return changed;
1364 }
1365
1366 boolean updateWallpaperOffsetLocked() {
1367 final int dw = mDisplay.getWidth();
1368 final int dh = mDisplay.getHeight();
1369
1370 boolean changed = false;
1371
1372 WindowState target = mWallpaperTarget;
1373 if (target != null) {
1374 int curTokenIndex = mWallpaperTokens.size();
1375 while (curTokenIndex > 0) {
1376 curTokenIndex--;
1377 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1378 int curWallpaperIndex = token.windows.size();
1379 while (curWallpaperIndex > 0) {
1380 curWallpaperIndex--;
1381 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1382 if (updateWallpaperOffsetLocked(target, wallpaper, dw, dh)) {
1383 wallpaper.computeShownFrameLocked();
1384 changed = true;
1385 }
1386 }
1387 }
1388 }
1389
1390 return changed;
1391 }
1392
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001393 void sendPointerToWallpaperLocked(WindowState srcWin,
1394 MotionEvent pointer, long eventTime) {
1395 int curTokenIndex = mWallpaperTokens.size();
1396 while (curTokenIndex > 0) {
1397 curTokenIndex--;
1398 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1399 int curWallpaperIndex = token.windows.size();
1400 while (curWallpaperIndex > 0) {
1401 curWallpaperIndex--;
1402 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1403 if ((wallpaper.mAttrs.flags &
1404 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1405 continue;
1406 }
1407 try {
1408 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1409 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1410 srcWin.mFrame.top-wallpaper.mFrame.top);
1411 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1412 } catch (RemoteException e) {
1413 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1414 }
1415 }
1416 }
1417 }
1418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 public int addWindow(Session session, IWindow client,
1420 WindowManager.LayoutParams attrs, int viewVisibility,
1421 Rect outContentInsets) {
1422 int res = mPolicy.checkAddPermission(attrs);
1423 if (res != WindowManagerImpl.ADD_OKAY) {
1424 return res;
1425 }
Romain Guy06882f82009-06-10 13:36:04 -07001426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 boolean reportNewConfig = false;
1428 WindowState attachedWindow = null;
1429 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 synchronized(mWindowMap) {
1432 // Instantiating a Display requires talking with the simulator,
1433 // so don't do it until we know the system is mostly up and
1434 // running.
1435 if (mDisplay == null) {
1436 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1437 mDisplay = wm.getDefaultDisplay();
1438 mQueue.setDisplay(mDisplay);
1439 reportNewConfig = true;
1440 }
Romain Guy06882f82009-06-10 13:36:04 -07001441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 if (mWindowMap.containsKey(client.asBinder())) {
1443 Log.w(TAG, "Window " + client + " is already added");
1444 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1445 }
1446
1447 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001448 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 if (attachedWindow == null) {
1450 Log.w(TAG, "Attempted to add window with token that is not a window: "
1451 + attrs.token + ". Aborting.");
1452 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1453 }
1454 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1455 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1456 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1457 + attrs.token + ". Aborting.");
1458 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1459 }
1460 }
1461
1462 boolean addToken = false;
1463 WindowToken token = mTokenMap.get(attrs.token);
1464 if (token == null) {
1465 if (attrs.type >= FIRST_APPLICATION_WINDOW
1466 && attrs.type <= LAST_APPLICATION_WINDOW) {
1467 Log.w(TAG, "Attempted to add application window with unknown token "
1468 + attrs.token + ". Aborting.");
1469 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1470 }
1471 if (attrs.type == TYPE_INPUT_METHOD) {
1472 Log.w(TAG, "Attempted to add input method window with unknown token "
1473 + attrs.token + ". Aborting.");
1474 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1475 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001476 if (attrs.type == TYPE_WALLPAPER) {
1477 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1478 + attrs.token + ". Aborting.");
1479 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 token = new WindowToken(attrs.token, -1, false);
1482 addToken = true;
1483 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1484 && attrs.type <= LAST_APPLICATION_WINDOW) {
1485 AppWindowToken atoken = token.appWindowToken;
1486 if (atoken == null) {
1487 Log.w(TAG, "Attempted to add window with non-application token "
1488 + token + ". Aborting.");
1489 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1490 } else if (atoken.removed) {
1491 Log.w(TAG, "Attempted to add window with exiting application token "
1492 + token + ". Aborting.");
1493 return WindowManagerImpl.ADD_APP_EXITING;
1494 }
1495 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1496 // No need for this guy!
1497 if (localLOGV) Log.v(
1498 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1499 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1500 }
1501 } else if (attrs.type == TYPE_INPUT_METHOD) {
1502 if (token.windowType != TYPE_INPUT_METHOD) {
1503 Log.w(TAG, "Attempted to add input method window with bad token "
1504 + attrs.token + ". Aborting.");
1505 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1506 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001507 } else if (attrs.type == TYPE_WALLPAPER) {
1508 if (token.windowType != TYPE_WALLPAPER) {
1509 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1510 + attrs.token + ". Aborting.");
1511 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 }
1514
1515 win = new WindowState(session, client, token,
1516 attachedWindow, attrs, viewVisibility);
1517 if (win.mDeathRecipient == null) {
1518 // Client has apparently died, so there is no reason to
1519 // continue.
1520 Log.w(TAG, "Adding window client " + client.asBinder()
1521 + " that is dead, aborting.");
1522 return WindowManagerImpl.ADD_APP_EXITING;
1523 }
1524
1525 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 res = mPolicy.prepareAddWindowLw(win, attrs);
1528 if (res != WindowManagerImpl.ADD_OKAY) {
1529 return res;
1530 }
1531
1532 // From now on, no exceptions or errors allowed!
1533
1534 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 if (addToken) {
1539 mTokenMap.put(attrs.token, token);
1540 mTokenList.add(token);
1541 }
1542 win.attach();
1543 mWindowMap.put(client.asBinder(), win);
1544
1545 if (attrs.type == TYPE_APPLICATION_STARTING &&
1546 token.appWindowToken != null) {
1547 token.appWindowToken.startingWindow = win;
1548 }
1549
1550 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 if (attrs.type == TYPE_INPUT_METHOD) {
1553 mInputMethodWindow = win;
1554 addInputMethodWindowToListLocked(win);
1555 imMayMove = false;
1556 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1557 mInputMethodDialogs.add(win);
1558 addWindowToListInOrderLocked(win, true);
1559 adjustInputMethodDialogsLocked();
1560 imMayMove = false;
1561 } else {
1562 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001563 if (attrs.type == TYPE_WALLPAPER ||
1564 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1565 adjustWallpaperWindowsLocked();
1566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
Romain Guy06882f82009-06-10 13:36:04 -07001568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 if (mInTouchMode) {
1574 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1575 }
1576 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1577 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1578 }
Romain Guy06882f82009-06-10 13:36:04 -07001579
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001580 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001582 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1583 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 imMayMove = false;
1585 }
1586 }
Romain Guy06882f82009-06-10 13:36:04 -07001587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001589 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 }
Romain Guy06882f82009-06-10 13:36:04 -07001591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 assignLayersLocked();
1593 // Don't do layout here, the window must call
1594 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 //dump();
1597
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001598 if (focusChanged) {
1599 if (mCurrentFocus != null) {
1600 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1601 }
1602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 if (localLOGV) Log.v(
1604 TAG, "New client " + client.asBinder()
1605 + ": window=" + win);
1606 }
1607
1608 // sendNewConfiguration() checks caller permissions so we must call it with
1609 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1610 // identity anyway, so it's safe to just clear & restore around this whole
1611 // block.
1612 final long origId = Binder.clearCallingIdentity();
1613 if (reportNewConfig) {
1614 sendNewConfiguration();
1615 } else {
1616 // Update Orientation after adding a window, only if the window needs to be
1617 // displayed right away
1618 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001619 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 sendNewConfiguration();
1621 }
1622 }
1623 }
1624 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 return res;
1627 }
Romain Guy06882f82009-06-10 13:36:04 -07001628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 public void removeWindow(Session session, IWindow client) {
1630 synchronized(mWindowMap) {
1631 WindowState win = windowForClientLocked(session, client);
1632 if (win == null) {
1633 return;
1634 }
1635 removeWindowLocked(session, win);
1636 }
1637 }
Romain Guy06882f82009-06-10 13:36:04 -07001638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 public void removeWindowLocked(Session session, WindowState win) {
1640
1641 if (localLOGV || DEBUG_FOCUS) Log.v(
1642 TAG, "Remove " + win + " client="
1643 + Integer.toHexString(System.identityHashCode(
1644 win.mClient.asBinder()))
1645 + ", surface=" + win.mSurface);
1646
1647 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 if (DEBUG_APP_TRANSITIONS) Log.v(
1650 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1651 + " mExiting=" + win.mExiting
1652 + " isAnimating=" + win.isAnimating()
1653 + " app-animation="
1654 + (win.mAppToken != null ? win.mAppToken.animation : null)
1655 + " inPendingTransaction="
1656 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1657 + " mDisplayFrozen=" + mDisplayFrozen);
1658 // Visibility of the removed window. Will be used later to update orientation later on.
1659 boolean wasVisible = false;
1660 // First, see if we need to run an animation. If we do, we have
1661 // to hold off on removing the window until the animation is done.
1662 // If the display is frozen, just remove immediately, since the
1663 // animation wouldn't be seen.
1664 if (win.mSurface != null && !mDisplayFrozen) {
1665 // If we are not currently running the exit animation, we
1666 // need to see about starting one.
1667 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1670 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1671 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1672 }
1673 // Try starting an animation.
1674 if (applyAnimationLocked(win, transit, false)) {
1675 win.mExiting = true;
1676 }
1677 }
1678 if (win.mExiting || win.isAnimating()) {
1679 // The exit animation is running... wait for it!
1680 //Log.i(TAG, "*** Running exit animation...");
1681 win.mExiting = true;
1682 win.mRemoveOnExit = true;
1683 mLayoutNeeded = true;
1684 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1685 performLayoutAndPlaceSurfacesLocked();
1686 if (win.mAppToken != null) {
1687 win.mAppToken.updateReportedVisibilityLocked();
1688 }
1689 //dump();
1690 Binder.restoreCallingIdentity(origId);
1691 return;
1692 }
1693 }
1694
1695 removeWindowInnerLocked(session, win);
1696 // Removing a visible window will effect the computed orientation
1697 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001698 if (wasVisible && computeForcedAppOrientationLocked()
1699 != mForcedAppOrientation) {
1700 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
1702 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1703 Binder.restoreCallingIdentity(origId);
1704 }
Romain Guy06882f82009-06-10 13:36:04 -07001705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001707 mKeyWaiter.finishedKey(session, win.mClient, true,
1708 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1710 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 if (mInputMethodTarget == win) {
1715 moveInputMethodWindowsIfNeededLocked(false);
1716 }
Romain Guy06882f82009-06-10 13:36:04 -07001717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 mPolicy.removeWindowLw(win);
1719 win.removeLocked();
1720
1721 mWindowMap.remove(win.mClient.asBinder());
1722 mWindows.remove(win);
1723
1724 if (mInputMethodWindow == win) {
1725 mInputMethodWindow = null;
1726 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1727 mInputMethodDialogs.remove(win);
1728 }
Romain Guy06882f82009-06-10 13:36:04 -07001729
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001730 if (win.mAttrs.type == TYPE_WALLPAPER ||
1731 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1732 adjustWallpaperWindowsLocked();
1733 }
1734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 final WindowToken token = win.mToken;
1736 final AppWindowToken atoken = win.mAppToken;
1737 token.windows.remove(win);
1738 if (atoken != null) {
1739 atoken.allAppWindows.remove(win);
1740 }
1741 if (localLOGV) Log.v(
1742 TAG, "**** Removing window " + win + ": count="
1743 + token.windows.size());
1744 if (token.windows.size() == 0) {
1745 if (!token.explicit) {
1746 mTokenMap.remove(token.token);
1747 mTokenList.remove(token);
1748 } else if (atoken != null) {
1749 atoken.firstWindowDrawn = false;
1750 }
1751 }
1752
1753 if (atoken != null) {
1754 if (atoken.startingWindow == win) {
1755 atoken.startingWindow = null;
1756 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1757 // If this is the last window and we had requested a starting
1758 // transition window, well there is no point now.
1759 atoken.startingData = null;
1760 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1761 // If this is the last window except for a starting transition
1762 // window, we need to get rid of the starting transition.
1763 if (DEBUG_STARTING_WINDOW) {
1764 Log.v(TAG, "Schedule remove starting " + token
1765 + ": no more real windows");
1766 }
1767 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1768 mH.sendMessage(m);
1769 }
1770 }
Romain Guy06882f82009-06-10 13:36:04 -07001771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 if (!mInLayout) {
1773 assignLayersLocked();
1774 mLayoutNeeded = true;
1775 performLayoutAndPlaceSurfacesLocked();
1776 if (win.mAppToken != null) {
1777 win.mAppToken.updateReportedVisibilityLocked();
1778 }
1779 }
1780 }
1781
1782 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1783 long origId = Binder.clearCallingIdentity();
1784 try {
1785 synchronized (mWindowMap) {
1786 WindowState w = windowForClientLocked(session, client);
1787 if ((w != null) && (w.mSurface != null)) {
1788 Surface.openTransaction();
1789 try {
1790 w.mSurface.setTransparentRegionHint(region);
1791 } finally {
1792 Surface.closeTransaction();
1793 }
1794 }
1795 }
1796 } finally {
1797 Binder.restoreCallingIdentity(origId);
1798 }
1799 }
1800
1801 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001802 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 Rect visibleInsets) {
1804 long origId = Binder.clearCallingIdentity();
1805 try {
1806 synchronized (mWindowMap) {
1807 WindowState w = windowForClientLocked(session, client);
1808 if (w != null) {
1809 w.mGivenInsetsPending = false;
1810 w.mGivenContentInsets.set(contentInsets);
1811 w.mGivenVisibleInsets.set(visibleInsets);
1812 w.mTouchableInsets = touchableInsets;
1813 mLayoutNeeded = true;
1814 performLayoutAndPlaceSurfacesLocked();
1815 }
1816 }
1817 } finally {
1818 Binder.restoreCallingIdentity(origId);
1819 }
1820 }
Romain Guy06882f82009-06-10 13:36:04 -07001821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 public void getWindowDisplayFrame(Session session, IWindow client,
1823 Rect outDisplayFrame) {
1824 synchronized(mWindowMap) {
1825 WindowState win = windowForClientLocked(session, client);
1826 if (win == null) {
1827 outDisplayFrame.setEmpty();
1828 return;
1829 }
1830 outDisplayFrame.set(win.mDisplayFrame);
1831 }
1832 }
1833
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001834 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
1835 if (window.mWallpaperX != x || window.mWallpaperY != y) {
1836 window.mWallpaperX = x;
1837 window.mWallpaperY = y;
1838
1839 if (mWallpaperTarget == window) {
1840 if (updateWallpaperOffsetLocked()) {
1841 performLayoutAndPlaceSurfacesLocked();
1842 }
1843 }
1844 }
1845 }
1846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 public int relayoutWindow(Session session, IWindow client,
1848 WindowManager.LayoutParams attrs, int requestedWidth,
1849 int requestedHeight, int viewVisibility, boolean insetsPending,
1850 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1851 Surface outSurface) {
1852 boolean displayed = false;
1853 boolean inTouchMode;
1854 Configuration newConfig = null;
1855 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 synchronized(mWindowMap) {
1858 WindowState win = windowForClientLocked(session, client);
1859 if (win == null) {
1860 return 0;
1861 }
1862 win.mRequestedWidth = requestedWidth;
1863 win.mRequestedHeight = requestedHeight;
1864
1865 if (attrs != null) {
1866 mPolicy.adjustWindowParamsLw(attrs);
1867 }
Romain Guy06882f82009-06-10 13:36:04 -07001868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 int attrChanges = 0;
1870 int flagChanges = 0;
1871 if (attrs != null) {
1872 flagChanges = win.mAttrs.flags ^= attrs.flags;
1873 attrChanges = win.mAttrs.copyFrom(attrs);
1874 }
1875
1876 if (localLOGV) Log.v(
1877 TAG, "Relayout given client " + client.asBinder()
1878 + " (" + win.mAttrs.getTitle() + ")");
1879
1880
1881 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1882 win.mAlpha = attrs.alpha;
1883 }
1884
1885 final boolean scaledWindow =
1886 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1887
1888 if (scaledWindow) {
1889 // requested{Width|Height} Surface's physical size
1890 // attrs.{width|height} Size on screen
1891 win.mHScale = (attrs.width != requestedWidth) ?
1892 (attrs.width / (float)requestedWidth) : 1.0f;
1893 win.mVScale = (attrs.height != requestedHeight) ?
1894 (attrs.height / (float)requestedHeight) : 1.0f;
1895 }
1896
1897 boolean imMayMove = (flagChanges&(
1898 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1899 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 boolean focusMayChange = win.mViewVisibility != viewVisibility
1902 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1903 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001904
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001905 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
1906 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
1907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 win.mRelayoutCalled = true;
1909 final int oldVisibility = win.mViewVisibility;
1910 win.mViewVisibility = viewVisibility;
1911 if (viewVisibility == View.VISIBLE &&
1912 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1913 displayed = !win.isVisibleLw();
1914 if (win.mExiting) {
1915 win.mExiting = false;
1916 win.mAnimation = null;
1917 }
1918 if (win.mDestroying) {
1919 win.mDestroying = false;
1920 mDestroySurface.remove(win);
1921 }
1922 if (oldVisibility == View.GONE) {
1923 win.mEnterAnimationPending = true;
1924 }
1925 if (displayed && win.mSurface != null && !win.mDrawPending
1926 && !win.mCommitDrawPending && !mDisplayFrozen) {
1927 applyEnterAnimationLocked(win);
1928 }
1929 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1930 // To change the format, we need to re-build the surface.
1931 win.destroySurfaceLocked();
1932 displayed = true;
1933 }
1934 try {
1935 Surface surface = win.createSurfaceLocked();
1936 if (surface != null) {
1937 outSurface.copyFrom(surface);
1938 } else {
1939 outSurface.clear();
1940 }
1941 } catch (Exception e) {
1942 Log.w(TAG, "Exception thrown when creating surface for client "
1943 + client + " (" + win.mAttrs.getTitle() + ")",
1944 e);
1945 Binder.restoreCallingIdentity(origId);
1946 return 0;
1947 }
1948 if (displayed) {
1949 focusMayChange = true;
1950 }
1951 if (win.mAttrs.type == TYPE_INPUT_METHOD
1952 && mInputMethodWindow == null) {
1953 mInputMethodWindow = win;
1954 imMayMove = true;
1955 }
1956 } else {
1957 win.mEnterAnimationPending = false;
1958 if (win.mSurface != null) {
1959 // If we are not currently running the exit animation, we
1960 // need to see about starting one.
1961 if (!win.mExiting) {
1962 // Try starting an animation; if there isn't one, we
1963 // can destroy the surface right away.
1964 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1965 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1966 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1967 }
1968 if (win.isWinVisibleLw() &&
1969 applyAnimationLocked(win, transit, false)) {
1970 win.mExiting = true;
1971 mKeyWaiter.finishedKey(session, client, true,
1972 KeyWaiter.RETURN_NOTHING);
1973 } else if (win.isAnimating()) {
1974 // Currently in a hide animation... turn this into
1975 // an exit.
1976 win.mExiting = true;
1977 } else {
1978 if (mInputMethodWindow == win) {
1979 mInputMethodWindow = null;
1980 }
1981 win.destroySurfaceLocked();
1982 }
1983 }
1984 }
1985 outSurface.clear();
1986 }
1987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 if (focusMayChange) {
1989 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1990 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 imMayMove = false;
1992 }
1993 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1994 }
Romain Guy06882f82009-06-10 13:36:04 -07001995
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001996 // updateFocusedWindowLocked() already assigned layers so we only need to
1997 // reassign them at this point if the IM window state gets shuffled
1998 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 if (imMayMove) {
2001 if (moveInputMethodWindowsIfNeededLocked(false)) {
2002 assignLayers = true;
2003 }
2004 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002005 if (wallpaperMayMove) {
2006 if (adjustWallpaperWindowsLocked()) {
2007 assignLayers = true;
2008 }
2009 }
Romain Guy06882f82009-06-10 13:36:04 -07002010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 mLayoutNeeded = true;
2012 win.mGivenInsetsPending = insetsPending;
2013 if (assignLayers) {
2014 assignLayersLocked();
2015 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002016 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 performLayoutAndPlaceSurfacesLocked();
2018 if (win.mAppToken != null) {
2019 win.mAppToken.updateReportedVisibilityLocked();
2020 }
2021 outFrame.set(win.mFrame);
2022 outContentInsets.set(win.mContentInsets);
2023 outVisibleInsets.set(win.mVisibleInsets);
2024 if (localLOGV) Log.v(
2025 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002026 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 + ", requestedHeight=" + requestedHeight
2028 + ", viewVisibility=" + viewVisibility
2029 + "\nRelayout returning frame=" + outFrame
2030 + ", surface=" + outSurface);
2031
2032 if (localLOGV || DEBUG_FOCUS) Log.v(
2033 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2034
2035 inTouchMode = mInTouchMode;
2036 }
2037
2038 if (newConfig != null) {
2039 sendNewConfiguration();
2040 }
Romain Guy06882f82009-06-10 13:36:04 -07002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2045 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2046 }
2047
2048 public void finishDrawingWindow(Session session, IWindow client) {
2049 final long origId = Binder.clearCallingIdentity();
2050 synchronized(mWindowMap) {
2051 WindowState win = windowForClientLocked(session, client);
2052 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002053 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2054 adjustWallpaperWindowsLocked();
2055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 mLayoutNeeded = true;
2057 performLayoutAndPlaceSurfacesLocked();
2058 }
2059 }
2060 Binder.restoreCallingIdentity(origId);
2061 }
2062
2063 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2064 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2065 + (lp != null ? lp.packageName : null)
2066 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2067 if (lp != null && lp.windowAnimations != 0) {
2068 // If this is a system resource, don't try to load it from the
2069 // application resources. It is nice to avoid loading application
2070 // resources if we can.
2071 String packageName = lp.packageName != null ? lp.packageName : "android";
2072 int resId = lp.windowAnimations;
2073 if ((resId&0xFF000000) == 0x01000000) {
2074 packageName = "android";
2075 }
2076 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2077 + packageName);
2078 return AttributeCache.instance().get(packageName, resId,
2079 com.android.internal.R.styleable.WindowAnimation);
2080 }
2081 return null;
2082 }
Romain Guy06882f82009-06-10 13:36:04 -07002083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 private void applyEnterAnimationLocked(WindowState win) {
2085 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2086 if (win.mEnterAnimationPending) {
2087 win.mEnterAnimationPending = false;
2088 transit = WindowManagerPolicy.TRANSIT_ENTER;
2089 }
2090
2091 applyAnimationLocked(win, transit, true);
2092 }
2093
2094 private boolean applyAnimationLocked(WindowState win,
2095 int transit, boolean isEntrance) {
2096 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2097 // If we are trying to apply an animation, but already running
2098 // an animation of the same type, then just leave that one alone.
2099 return true;
2100 }
Romain Guy06882f82009-06-10 13:36:04 -07002101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 // Only apply an animation if the display isn't frozen. If it is
2103 // frozen, there is no reason to animate and it can cause strange
2104 // artifacts when we unfreeze the display if some different animation
2105 // is running.
2106 if (!mDisplayFrozen) {
2107 int anim = mPolicy.selectAnimationLw(win, transit);
2108 int attr = -1;
2109 Animation a = null;
2110 if (anim != 0) {
2111 a = AnimationUtils.loadAnimation(mContext, anim);
2112 } else {
2113 switch (transit) {
2114 case WindowManagerPolicy.TRANSIT_ENTER:
2115 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2116 break;
2117 case WindowManagerPolicy.TRANSIT_EXIT:
2118 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2119 break;
2120 case WindowManagerPolicy.TRANSIT_SHOW:
2121 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2122 break;
2123 case WindowManagerPolicy.TRANSIT_HIDE:
2124 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2125 break;
2126 }
2127 if (attr >= 0) {
2128 a = loadAnimation(win.mAttrs, attr);
2129 }
2130 }
2131 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2132 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2133 + " mAnimation=" + win.mAnimation
2134 + " isEntrance=" + isEntrance);
2135 if (a != null) {
2136 if (DEBUG_ANIM) {
2137 RuntimeException e = new RuntimeException();
2138 e.fillInStackTrace();
2139 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2140 }
2141 win.setAnimation(a);
2142 win.mAnimationIsEntrance = isEntrance;
2143 }
2144 } else {
2145 win.clearAnimation();
2146 }
2147
2148 return win.mAnimation != null;
2149 }
2150
2151 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2152 int anim = 0;
2153 Context context = mContext;
2154 if (animAttr >= 0) {
2155 AttributeCache.Entry ent = getCachedAnimations(lp);
2156 if (ent != null) {
2157 context = ent.context;
2158 anim = ent.array.getResourceId(animAttr, 0);
2159 }
2160 }
2161 if (anim != 0) {
2162 return AnimationUtils.loadAnimation(context, anim);
2163 }
2164 return null;
2165 }
Romain Guy06882f82009-06-10 13:36:04 -07002166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 private boolean applyAnimationLocked(AppWindowToken wtoken,
2168 WindowManager.LayoutParams lp, int transit, boolean enter) {
2169 // Only apply an animation if the display isn't frozen. If it is
2170 // frozen, there is no reason to animate and it can cause strange
2171 // artifacts when we unfreeze the display if some different animation
2172 // is running.
2173 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002174 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002175 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002176 a = new FadeInOutAnimation(enter);
2177 if (DEBUG_ANIM) Log.v(TAG,
2178 "applying FadeInOutAnimation for a window in compatibility mode");
2179 } else {
2180 int animAttr = 0;
2181 switch (transit) {
2182 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2183 animAttr = enter
2184 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2185 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2186 break;
2187 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2188 animAttr = enter
2189 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2190 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2191 break;
2192 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2193 animAttr = enter
2194 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2195 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2196 break;
2197 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2198 animAttr = enter
2199 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2200 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2201 break;
2202 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2203 animAttr = enter
2204 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2205 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2206 break;
2207 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2208 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002209 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002210 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2211 break;
2212 }
2213 a = loadAnimation(lp, animAttr);
2214 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2215 + " anim=" + a
2216 + " animAttr=0x" + Integer.toHexString(animAttr)
2217 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 if (a != null) {
2220 if (DEBUG_ANIM) {
2221 RuntimeException e = new RuntimeException();
2222 e.fillInStackTrace();
2223 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2224 }
2225 wtoken.setAnimation(a);
2226 }
2227 } else {
2228 wtoken.clearAnimation();
2229 }
2230
2231 return wtoken.animation != null;
2232 }
2233
2234 // -------------------------------------------------------------
2235 // Application Window Tokens
2236 // -------------------------------------------------------------
2237
2238 public void validateAppTokens(List tokens) {
2239 int v = tokens.size()-1;
2240 int m = mAppTokens.size()-1;
2241 while (v >= 0 && m >= 0) {
2242 AppWindowToken wtoken = mAppTokens.get(m);
2243 if (wtoken.removed) {
2244 m--;
2245 continue;
2246 }
2247 if (tokens.get(v) != wtoken.token) {
2248 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2249 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2250 }
2251 v--;
2252 m--;
2253 }
2254 while (v >= 0) {
2255 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2256 v--;
2257 }
2258 while (m >= 0) {
2259 AppWindowToken wtoken = mAppTokens.get(m);
2260 if (!wtoken.removed) {
2261 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2262 }
2263 m--;
2264 }
2265 }
2266
2267 boolean checkCallingPermission(String permission, String func) {
2268 // Quick check: if the calling permission is me, it's all okay.
2269 if (Binder.getCallingPid() == Process.myPid()) {
2270 return true;
2271 }
Romain Guy06882f82009-06-10 13:36:04 -07002272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 if (mContext.checkCallingPermission(permission)
2274 == PackageManager.PERMISSION_GRANTED) {
2275 return true;
2276 }
2277 String msg = "Permission Denial: " + func + " from pid="
2278 + Binder.getCallingPid()
2279 + ", uid=" + Binder.getCallingUid()
2280 + " requires " + permission;
2281 Log.w(TAG, msg);
2282 return false;
2283 }
Romain Guy06882f82009-06-10 13:36:04 -07002284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 AppWindowToken findAppWindowToken(IBinder token) {
2286 WindowToken wtoken = mTokenMap.get(token);
2287 if (wtoken == null) {
2288 return null;
2289 }
2290 return wtoken.appWindowToken;
2291 }
Romain Guy06882f82009-06-10 13:36:04 -07002292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 public void addWindowToken(IBinder token, int type) {
2294 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2295 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002296 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 }
Romain Guy06882f82009-06-10 13:36:04 -07002298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 synchronized(mWindowMap) {
2300 WindowToken wtoken = mTokenMap.get(token);
2301 if (wtoken != null) {
2302 Log.w(TAG, "Attempted to add existing input method token: " + token);
2303 return;
2304 }
2305 wtoken = new WindowToken(token, type, true);
2306 mTokenMap.put(token, wtoken);
2307 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002308 if (type == TYPE_WALLPAPER) {
2309 mWallpaperTokens.add(wtoken);
2310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 }
2312 }
Romain Guy06882f82009-06-10 13:36:04 -07002313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 public void removeWindowToken(IBinder token) {
2315 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2316 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002317 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 }
2319
2320 final long origId = Binder.clearCallingIdentity();
2321 synchronized(mWindowMap) {
2322 WindowToken wtoken = mTokenMap.remove(token);
2323 mTokenList.remove(wtoken);
2324 if (wtoken != null) {
2325 boolean delayed = false;
2326 if (!wtoken.hidden) {
2327 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 final int N = wtoken.windows.size();
2330 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 for (int i=0; i<N; i++) {
2333 WindowState win = wtoken.windows.get(i);
2334
2335 if (win.isAnimating()) {
2336 delayed = true;
2337 }
Romain Guy06882f82009-06-10 13:36:04 -07002338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 if (win.isVisibleNow()) {
2340 applyAnimationLocked(win,
2341 WindowManagerPolicy.TRANSIT_EXIT, false);
2342 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2343 KeyWaiter.RETURN_NOTHING);
2344 changed = true;
2345 }
2346 }
2347
2348 if (changed) {
2349 mLayoutNeeded = true;
2350 performLayoutAndPlaceSurfacesLocked();
2351 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2352 }
Romain Guy06882f82009-06-10 13:36:04 -07002353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 if (delayed) {
2355 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002356 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2357 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 }
2359 }
Romain Guy06882f82009-06-10 13:36:04 -07002360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 } else {
2362 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2363 }
2364 }
2365 Binder.restoreCallingIdentity(origId);
2366 }
2367
2368 public void addAppToken(int addPos, IApplicationToken token,
2369 int groupId, int requestedOrientation, boolean fullscreen) {
2370 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2371 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002372 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 }
Romain Guy06882f82009-06-10 13:36:04 -07002374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 synchronized(mWindowMap) {
2376 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2377 if (wtoken != null) {
2378 Log.w(TAG, "Attempted to add existing app token: " + token);
2379 return;
2380 }
2381 wtoken = new AppWindowToken(token);
2382 wtoken.groupId = groupId;
2383 wtoken.appFullscreen = fullscreen;
2384 wtoken.requestedOrientation = requestedOrientation;
2385 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002386 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 mTokenMap.put(token.asBinder(), wtoken);
2388 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 // Application tokens start out hidden.
2391 wtoken.hidden = true;
2392 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 //dump();
2395 }
2396 }
Romain Guy06882f82009-06-10 13:36:04 -07002397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 public void setAppGroupId(IBinder token, int groupId) {
2399 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2400 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002401 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 }
2403
2404 synchronized(mWindowMap) {
2405 AppWindowToken wtoken = findAppWindowToken(token);
2406 if (wtoken == null) {
2407 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2408 return;
2409 }
2410 wtoken.groupId = groupId;
2411 }
2412 }
Romain Guy06882f82009-06-10 13:36:04 -07002413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 public int getOrientationFromWindowsLocked() {
2415 int pos = mWindows.size() - 1;
2416 while (pos >= 0) {
2417 WindowState wtoken = (WindowState) mWindows.get(pos);
2418 pos--;
2419 if (wtoken.mAppToken != null) {
2420 // We hit an application window. so the orientation will be determined by the
2421 // app window. No point in continuing further.
2422 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2423 }
2424 if (!wtoken.isVisibleLw()) {
2425 continue;
2426 }
2427 int req = wtoken.mAttrs.screenOrientation;
2428 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2429 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2430 continue;
2431 } else {
2432 return req;
2433 }
2434 }
2435 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2436 }
Romain Guy06882f82009-06-10 13:36:04 -07002437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 public int getOrientationFromAppTokensLocked() {
2439 int pos = mAppTokens.size() - 1;
2440 int curGroup = 0;
2441 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002442 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002444 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 while (pos >= 0) {
2446 AppWindowToken wtoken = mAppTokens.get(pos);
2447 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002448 // if we're about to tear down this window and not seek for
2449 // the behind activity, don't use it for orientation
2450 if (!findingBehind
2451 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002452 continue;
2453 }
2454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 if (!haveGroup) {
2456 // We ignore any hidden applications on the top.
2457 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2458 continue;
2459 }
2460 haveGroup = true;
2461 curGroup = wtoken.groupId;
2462 lastOrientation = wtoken.requestedOrientation;
2463 } else if (curGroup != wtoken.groupId) {
2464 // If we have hit a new application group, and the bottom
2465 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002466 // the orientation behind it, and the last app was
2467 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002469 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2470 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 return lastOrientation;
2472 }
2473 }
2474 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002475 // If this application is fullscreen, and didn't explicitly say
2476 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002478 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002479 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002480 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 return or;
2482 }
2483 // If this application has requested an explicit orientation,
2484 // then use it.
2485 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2486 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2487 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2488 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2489 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2490 return or;
2491 }
Owen Lin3413b892009-05-01 17:12:32 -07002492 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 }
2494 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2495 }
Romain Guy06882f82009-06-10 13:36:04 -07002496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002498 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002499 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2500 "updateOrientationFromAppTokens()")) {
2501 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2502 }
2503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 Configuration config;
2505 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002506 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2507 freezeThisOneIfNeeded);
2508 Binder.restoreCallingIdentity(ident);
2509 return config;
2510 }
2511
2512 Configuration updateOrientationFromAppTokensUnchecked(
2513 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2514 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002516 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 }
2518 if (config != null) {
2519 mLayoutNeeded = true;
2520 performLayoutAndPlaceSurfacesLocked();
2521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 return config;
2523 }
Romain Guy06882f82009-06-10 13:36:04 -07002524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 /*
2526 * The orientation is computed from non-application windows first. If none of
2527 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002528 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2530 * android.os.IBinder)
2531 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002532 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002533 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 long ident = Binder.clearCallingIdentity();
2536 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002537 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 if (req != mForcedAppOrientation) {
2540 changed = true;
2541 mForcedAppOrientation = req;
2542 //send a message to Policy indicating orientation change to take
2543 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002544 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 }
Romain Guy06882f82009-06-10 13:36:04 -07002546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 if (changed) {
2548 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002549 WindowManagerPolicy.USE_LAST_ROTATION,
2550 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 if (changed) {
2552 if (freezeThisOneIfNeeded != null) {
2553 AppWindowToken wtoken = findAppWindowToken(
2554 freezeThisOneIfNeeded);
2555 if (wtoken != null) {
2556 startAppFreezingScreenLocked(wtoken,
2557 ActivityInfo.CONFIG_ORIENTATION);
2558 }
2559 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002560 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 }
2562 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002563
2564 // No obvious action we need to take, but if our current
2565 // state mismatches the activity maanager's, update it
2566 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002567 mTempConfiguration.setToDefaults();
2568 if (computeNewConfigurationLocked(mTempConfiguration)) {
2569 if (appConfig.diff(mTempConfiguration) != 0) {
2570 Log.i(TAG, "Config changed: " + mTempConfiguration);
2571 return new Configuration(mTempConfiguration);
2572 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002573 }
2574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 } finally {
2576 Binder.restoreCallingIdentity(ident);
2577 }
Romain Guy06882f82009-06-10 13:36:04 -07002578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 return null;
2580 }
Romain Guy06882f82009-06-10 13:36:04 -07002581
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002582 int computeForcedAppOrientationLocked() {
2583 int req = getOrientationFromWindowsLocked();
2584 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2585 req = getOrientationFromAppTokensLocked();
2586 }
2587 return req;
2588 }
Romain Guy06882f82009-06-10 13:36:04 -07002589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2591 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2592 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002593 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 }
Romain Guy06882f82009-06-10 13:36:04 -07002595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 synchronized(mWindowMap) {
2597 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2598 if (wtoken == null) {
2599 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2600 return;
2601 }
Romain Guy06882f82009-06-10 13:36:04 -07002602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 wtoken.requestedOrientation = requestedOrientation;
2604 }
2605 }
Romain Guy06882f82009-06-10 13:36:04 -07002606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 public int getAppOrientation(IApplicationToken token) {
2608 synchronized(mWindowMap) {
2609 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2610 if (wtoken == null) {
2611 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2612 }
Romain Guy06882f82009-06-10 13:36:04 -07002613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 return wtoken.requestedOrientation;
2615 }
2616 }
Romain Guy06882f82009-06-10 13:36:04 -07002617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2619 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2620 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002621 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 }
2623
2624 synchronized(mWindowMap) {
2625 boolean changed = false;
2626 if (token == null) {
2627 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2628 changed = mFocusedApp != null;
2629 mFocusedApp = null;
2630 mKeyWaiter.tickle();
2631 } else {
2632 AppWindowToken newFocus = findAppWindowToken(token);
2633 if (newFocus == null) {
2634 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2635 return;
2636 }
2637 changed = mFocusedApp != newFocus;
2638 mFocusedApp = newFocus;
2639 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2640 mKeyWaiter.tickle();
2641 }
2642
2643 if (moveFocusNow && changed) {
2644 final long origId = Binder.clearCallingIdentity();
2645 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2646 Binder.restoreCallingIdentity(origId);
2647 }
2648 }
2649 }
2650
2651 public void prepareAppTransition(int transit) {
2652 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2653 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002654 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 }
Romain Guy06882f82009-06-10 13:36:04 -07002656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 synchronized(mWindowMap) {
2658 if (DEBUG_APP_TRANSITIONS) Log.v(
2659 TAG, "Prepare app transition: transit=" + transit
2660 + " mNextAppTransition=" + mNextAppTransition);
2661 if (!mDisplayFrozen) {
2662 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2663 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002664 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2665 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2666 // Opening a new task always supersedes a close for the anim.
2667 mNextAppTransition = transit;
2668 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2669 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2670 // Opening a new activity always supersedes a close for the anim.
2671 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 }
2673 mAppTransitionReady = false;
2674 mAppTransitionTimeout = false;
2675 mStartingIconInTransition = false;
2676 mSkipAppTransitionAnimation = false;
2677 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2678 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2679 5000);
2680 }
2681 }
2682 }
2683
2684 public int getPendingAppTransition() {
2685 return mNextAppTransition;
2686 }
Romain Guy06882f82009-06-10 13:36:04 -07002687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 public void executeAppTransition() {
2689 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2690 "executeAppTransition()")) {
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, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2697 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2698 mAppTransitionReady = true;
2699 final long origId = Binder.clearCallingIdentity();
2700 performLayoutAndPlaceSurfacesLocked();
2701 Binder.restoreCallingIdentity(origId);
2702 }
2703 }
2704 }
2705
2706 public void setAppStartingWindow(IBinder token, String pkg,
2707 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2708 IBinder transferFrom, boolean createIfNeeded) {
2709 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2710 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002711 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 }
2713
2714 synchronized(mWindowMap) {
2715 if (DEBUG_STARTING_WINDOW) Log.v(
2716 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2717 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 AppWindowToken wtoken = findAppWindowToken(token);
2720 if (wtoken == null) {
2721 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2722 return;
2723 }
2724
2725 // If the display is frozen, we won't do anything until the
2726 // actual window is displayed so there is no reason to put in
2727 // the starting window.
2728 if (mDisplayFrozen) {
2729 return;
2730 }
Romain Guy06882f82009-06-10 13:36:04 -07002731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 if (wtoken.startingData != null) {
2733 return;
2734 }
Romain Guy06882f82009-06-10 13:36:04 -07002735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 if (transferFrom != null) {
2737 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2738 if (ttoken != null) {
2739 WindowState startingWindow = ttoken.startingWindow;
2740 if (startingWindow != null) {
2741 if (mStartingIconInTransition) {
2742 // In this case, the starting icon has already
2743 // been displayed, so start letting windows get
2744 // shown immediately without any more transitions.
2745 mSkipAppTransitionAnimation = true;
2746 }
2747 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2748 "Moving existing starting from " + ttoken
2749 + " to " + wtoken);
2750 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 // Transfer the starting window over to the new
2753 // token.
2754 wtoken.startingData = ttoken.startingData;
2755 wtoken.startingView = ttoken.startingView;
2756 wtoken.startingWindow = startingWindow;
2757 ttoken.startingData = null;
2758 ttoken.startingView = null;
2759 ttoken.startingWindow = null;
2760 ttoken.startingMoved = true;
2761 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002762 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 startingWindow.mAppToken = wtoken;
2764 mWindows.remove(startingWindow);
2765 ttoken.windows.remove(startingWindow);
2766 ttoken.allAppWindows.remove(startingWindow);
2767 addWindowToListInOrderLocked(startingWindow, true);
2768 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 // Propagate other interesting state between the
2771 // tokens. If the old token is displayed, we should
2772 // immediately force the new one to be displayed. If
2773 // it is animating, we need to move that animation to
2774 // the new one.
2775 if (ttoken.allDrawn) {
2776 wtoken.allDrawn = true;
2777 }
2778 if (ttoken.firstWindowDrawn) {
2779 wtoken.firstWindowDrawn = true;
2780 }
2781 if (!ttoken.hidden) {
2782 wtoken.hidden = false;
2783 wtoken.hiddenRequested = false;
2784 wtoken.willBeHidden = false;
2785 }
2786 if (wtoken.clientHidden != ttoken.clientHidden) {
2787 wtoken.clientHidden = ttoken.clientHidden;
2788 wtoken.sendAppVisibilityToClients();
2789 }
2790 if (ttoken.animation != null) {
2791 wtoken.animation = ttoken.animation;
2792 wtoken.animating = ttoken.animating;
2793 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2794 ttoken.animation = null;
2795 ttoken.animLayerAdjustment = 0;
2796 wtoken.updateLayers();
2797 ttoken.updateLayers();
2798 }
Romain Guy06882f82009-06-10 13:36:04 -07002799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 mLayoutNeeded = true;
2802 performLayoutAndPlaceSurfacesLocked();
2803 Binder.restoreCallingIdentity(origId);
2804 return;
2805 } else if (ttoken.startingData != null) {
2806 // The previous app was getting ready to show a
2807 // starting window, but hasn't yet done so. Steal it!
2808 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2809 "Moving pending starting from " + ttoken
2810 + " to " + wtoken);
2811 wtoken.startingData = ttoken.startingData;
2812 ttoken.startingData = null;
2813 ttoken.startingMoved = true;
2814 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2815 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2816 // want to process the message ASAP, before any other queued
2817 // messages.
2818 mH.sendMessageAtFrontOfQueue(m);
2819 return;
2820 }
2821 }
2822 }
2823
2824 // There is no existing starting window, and the caller doesn't
2825 // want us to create one, so that's it!
2826 if (!createIfNeeded) {
2827 return;
2828 }
Romain Guy06882f82009-06-10 13:36:04 -07002829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 mStartingIconInTransition = true;
2831 wtoken.startingData = new StartingData(
2832 pkg, theme, nonLocalizedLabel,
2833 labelRes, icon);
2834 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2835 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2836 // want to process the message ASAP, before any other queued
2837 // messages.
2838 mH.sendMessageAtFrontOfQueue(m);
2839 }
2840 }
2841
2842 public void setAppWillBeHidden(IBinder token) {
2843 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2844 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002845 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 }
2847
2848 AppWindowToken wtoken;
2849
2850 synchronized(mWindowMap) {
2851 wtoken = findAppWindowToken(token);
2852 if (wtoken == null) {
2853 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2854 return;
2855 }
2856 wtoken.willBeHidden = true;
2857 }
2858 }
Romain Guy06882f82009-06-10 13:36:04 -07002859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2861 boolean visible, int transit, boolean performLayout) {
2862 boolean delayed = false;
2863
2864 if (wtoken.clientHidden == visible) {
2865 wtoken.clientHidden = !visible;
2866 wtoken.sendAppVisibilityToClients();
2867 }
Romain Guy06882f82009-06-10 13:36:04 -07002868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 wtoken.willBeHidden = false;
2870 if (wtoken.hidden == visible) {
2871 final int N = wtoken.allAppWindows.size();
2872 boolean changed = false;
2873 if (DEBUG_APP_TRANSITIONS) Log.v(
2874 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2875 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2880 if (wtoken.animation == sDummyAnimation) {
2881 wtoken.animation = null;
2882 }
2883 applyAnimationLocked(wtoken, lp, transit, visible);
2884 changed = true;
2885 if (wtoken.animation != null) {
2886 delayed = runningAppAnimation = true;
2887 }
2888 }
Romain Guy06882f82009-06-10 13:36:04 -07002889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 for (int i=0; i<N; i++) {
2891 WindowState win = wtoken.allAppWindows.get(i);
2892 if (win == wtoken.startingWindow) {
2893 continue;
2894 }
2895
2896 if (win.isAnimating()) {
2897 delayed = true;
2898 }
Romain Guy06882f82009-06-10 13:36:04 -07002899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2901 //win.dump(" ");
2902 if (visible) {
2903 if (!win.isVisibleNow()) {
2904 if (!runningAppAnimation) {
2905 applyAnimationLocked(win,
2906 WindowManagerPolicy.TRANSIT_ENTER, true);
2907 }
2908 changed = true;
2909 }
2910 } else if (win.isVisibleNow()) {
2911 if (!runningAppAnimation) {
2912 applyAnimationLocked(win,
2913 WindowManagerPolicy.TRANSIT_EXIT, false);
2914 }
2915 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2916 KeyWaiter.RETURN_NOTHING);
2917 changed = true;
2918 }
2919 }
2920
2921 wtoken.hidden = wtoken.hiddenRequested = !visible;
2922 if (!visible) {
2923 unsetAppFreezingScreenLocked(wtoken, true, true);
2924 } else {
2925 // If we are being set visible, and the starting window is
2926 // not yet displayed, then make sure it doesn't get displayed.
2927 WindowState swin = wtoken.startingWindow;
2928 if (swin != null && (swin.mDrawPending
2929 || swin.mCommitDrawPending)) {
2930 swin.mPolicyVisibility = false;
2931 swin.mPolicyVisibilityAfterAnim = false;
2932 }
2933 }
Romain Guy06882f82009-06-10 13:36:04 -07002934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2936 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2937 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 if (changed && performLayout) {
2940 mLayoutNeeded = true;
2941 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 performLayoutAndPlaceSurfacesLocked();
2943 }
2944 }
2945
2946 if (wtoken.animation != null) {
2947 delayed = true;
2948 }
Romain Guy06882f82009-06-10 13:36:04 -07002949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 return delayed;
2951 }
2952
2953 public void setAppVisibility(IBinder token, boolean visible) {
2954 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2955 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002956 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 }
2958
2959 AppWindowToken wtoken;
2960
2961 synchronized(mWindowMap) {
2962 wtoken = findAppWindowToken(token);
2963 if (wtoken == null) {
2964 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2965 return;
2966 }
2967
2968 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2969 RuntimeException e = new RuntimeException();
2970 e.fillInStackTrace();
2971 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2972 + "): mNextAppTransition=" + mNextAppTransition
2973 + " hidden=" + wtoken.hidden
2974 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2975 }
Romain Guy06882f82009-06-10 13:36:04 -07002976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 // If we are preparing an app transition, then delay changing
2978 // the visibility of this token until we execute that transition.
2979 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2980 // Already in requested state, don't do anything more.
2981 if (wtoken.hiddenRequested != visible) {
2982 return;
2983 }
2984 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 if (DEBUG_APP_TRANSITIONS) Log.v(
2987 TAG, "Setting dummy animation on: " + wtoken);
2988 wtoken.setDummyAnimation();
2989 mOpeningApps.remove(wtoken);
2990 mClosingApps.remove(wtoken);
2991 wtoken.inPendingTransaction = true;
2992 if (visible) {
2993 mOpeningApps.add(wtoken);
2994 wtoken.allDrawn = false;
2995 wtoken.startingDisplayed = false;
2996 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 if (wtoken.clientHidden) {
2999 // In the case where we are making an app visible
3000 // but holding off for a transition, we still need
3001 // to tell the client to make its windows visible so
3002 // they get drawn. Otherwise, we will wait on
3003 // performing the transition until all windows have
3004 // been drawn, they never will be, and we are sad.
3005 wtoken.clientHidden = false;
3006 wtoken.sendAppVisibilityToClients();
3007 }
3008 } else {
3009 mClosingApps.add(wtoken);
3010 }
3011 return;
3012 }
Romain Guy06882f82009-06-10 13:36:04 -07003013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 final long origId = Binder.clearCallingIdentity();
3015 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
3016 wtoken.updateReportedVisibilityLocked();
3017 Binder.restoreCallingIdentity(origId);
3018 }
3019 }
3020
3021 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3022 boolean unfreezeSurfaceNow, boolean force) {
3023 if (wtoken.freezingScreen) {
3024 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3025 + " force=" + force);
3026 final int N = wtoken.allAppWindows.size();
3027 boolean unfrozeWindows = false;
3028 for (int i=0; i<N; i++) {
3029 WindowState w = wtoken.allAppWindows.get(i);
3030 if (w.mAppFreezing) {
3031 w.mAppFreezing = false;
3032 if (w.mSurface != null && !w.mOrientationChanging) {
3033 w.mOrientationChanging = true;
3034 }
3035 unfrozeWindows = true;
3036 }
3037 }
3038 if (force || unfrozeWindows) {
3039 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3040 wtoken.freezingScreen = false;
3041 mAppsFreezingScreen--;
3042 }
3043 if (unfreezeSurfaceNow) {
3044 if (unfrozeWindows) {
3045 mLayoutNeeded = true;
3046 performLayoutAndPlaceSurfacesLocked();
3047 }
3048 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3049 stopFreezingDisplayLocked();
3050 }
3051 }
3052 }
3053 }
Romain Guy06882f82009-06-10 13:36:04 -07003054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3056 int configChanges) {
3057 if (DEBUG_ORIENTATION) {
3058 RuntimeException e = new RuntimeException();
3059 e.fillInStackTrace();
3060 Log.i(TAG, "Set freezing of " + wtoken.appToken
3061 + ": hidden=" + wtoken.hidden + " freezing="
3062 + wtoken.freezingScreen, e);
3063 }
3064 if (!wtoken.hiddenRequested) {
3065 if (!wtoken.freezingScreen) {
3066 wtoken.freezingScreen = true;
3067 mAppsFreezingScreen++;
3068 if (mAppsFreezingScreen == 1) {
3069 startFreezingDisplayLocked();
3070 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3071 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3072 5000);
3073 }
3074 }
3075 final int N = wtoken.allAppWindows.size();
3076 for (int i=0; i<N; i++) {
3077 WindowState w = wtoken.allAppWindows.get(i);
3078 w.mAppFreezing = true;
3079 }
3080 }
3081 }
Romain Guy06882f82009-06-10 13:36:04 -07003082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 public void startAppFreezingScreen(IBinder token, int configChanges) {
3084 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3085 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003086 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
3088
3089 synchronized(mWindowMap) {
3090 if (configChanges == 0 && !mDisplayFrozen) {
3091 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3092 return;
3093 }
Romain Guy06882f82009-06-10 13:36:04 -07003094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 AppWindowToken wtoken = findAppWindowToken(token);
3096 if (wtoken == null || wtoken.appToken == null) {
3097 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3098 return;
3099 }
3100 final long origId = Binder.clearCallingIdentity();
3101 startAppFreezingScreenLocked(wtoken, configChanges);
3102 Binder.restoreCallingIdentity(origId);
3103 }
3104 }
Romain Guy06882f82009-06-10 13:36:04 -07003105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 public void stopAppFreezingScreen(IBinder token, boolean force) {
3107 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3108 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003109 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 }
3111
3112 synchronized(mWindowMap) {
3113 AppWindowToken wtoken = findAppWindowToken(token);
3114 if (wtoken == null || wtoken.appToken == null) {
3115 return;
3116 }
3117 final long origId = Binder.clearCallingIdentity();
3118 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3119 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3120 unsetAppFreezingScreenLocked(wtoken, true, force);
3121 Binder.restoreCallingIdentity(origId);
3122 }
3123 }
Romain Guy06882f82009-06-10 13:36:04 -07003124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 public void removeAppToken(IBinder token) {
3126 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3127 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003128 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 }
3130
3131 AppWindowToken wtoken = null;
3132 AppWindowToken startingToken = null;
3133 boolean delayed = false;
3134
3135 final long origId = Binder.clearCallingIdentity();
3136 synchronized(mWindowMap) {
3137 WindowToken basewtoken = mTokenMap.remove(token);
3138 mTokenList.remove(basewtoken);
3139 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3140 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3141 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3142 wtoken.inPendingTransaction = false;
3143 mOpeningApps.remove(wtoken);
3144 if (mClosingApps.contains(wtoken)) {
3145 delayed = true;
3146 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3147 mClosingApps.add(wtoken);
3148 delayed = true;
3149 }
3150 if (DEBUG_APP_TRANSITIONS) Log.v(
3151 TAG, "Removing app " + wtoken + " delayed=" + delayed
3152 + " animation=" + wtoken.animation
3153 + " animating=" + wtoken.animating);
3154 if (delayed) {
3155 // set the token aside because it has an active animation to be finished
3156 mExitingAppTokens.add(wtoken);
3157 }
3158 mAppTokens.remove(wtoken);
3159 wtoken.removed = true;
3160 if (wtoken.startingData != null) {
3161 startingToken = wtoken;
3162 }
3163 unsetAppFreezingScreenLocked(wtoken, true, true);
3164 if (mFocusedApp == wtoken) {
3165 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3166 mFocusedApp = null;
3167 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3168 mKeyWaiter.tickle();
3169 }
3170 } else {
3171 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3172 }
Romain Guy06882f82009-06-10 13:36:04 -07003173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 if (!delayed && wtoken != null) {
3175 wtoken.updateReportedVisibilityLocked();
3176 }
3177 }
3178 Binder.restoreCallingIdentity(origId);
3179
3180 if (startingToken != null) {
3181 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3182 + startingToken + ": app token removed");
3183 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3184 mH.sendMessage(m);
3185 }
3186 }
3187
3188 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3189 final int NW = token.windows.size();
3190 for (int i=0; i<NW; i++) {
3191 WindowState win = token.windows.get(i);
3192 mWindows.remove(win);
3193 int j = win.mChildWindows.size();
3194 while (j > 0) {
3195 j--;
3196 mWindows.remove(win.mChildWindows.get(j));
3197 }
3198 }
3199 return NW > 0;
3200 }
3201
3202 void dumpAppTokensLocked() {
3203 for (int i=mAppTokens.size()-1; i>=0; i--) {
3204 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3205 }
3206 }
Romain Guy06882f82009-06-10 13:36:04 -07003207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 void dumpWindowsLocked() {
3209 for (int i=mWindows.size()-1; i>=0; i--) {
3210 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3211 }
3212 }
Romain Guy06882f82009-06-10 13:36:04 -07003213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 private int findWindowOffsetLocked(int tokenPos) {
3215 final int NW = mWindows.size();
3216
3217 if (tokenPos >= mAppTokens.size()) {
3218 int i = NW;
3219 while (i > 0) {
3220 i--;
3221 WindowState win = (WindowState)mWindows.get(i);
3222 if (win.getAppToken() != null) {
3223 return i+1;
3224 }
3225 }
3226 }
3227
3228 while (tokenPos > 0) {
3229 // Find the first app token below the new position that has
3230 // a window displayed.
3231 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3232 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3233 + tokenPos + " -- " + wtoken.token);
3234 int i = wtoken.windows.size();
3235 while (i > 0) {
3236 i--;
3237 WindowState win = wtoken.windows.get(i);
3238 int j = win.mChildWindows.size();
3239 while (j > 0) {
3240 j--;
3241 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3242 if (cwin.mSubLayer >= 0 ) {
3243 for (int pos=NW-1; pos>=0; pos--) {
3244 if (mWindows.get(pos) == cwin) {
3245 if (DEBUG_REORDER) Log.v(TAG,
3246 "Found child win @" + (pos+1));
3247 return pos+1;
3248 }
3249 }
3250 }
3251 }
3252 for (int pos=NW-1; pos>=0; pos--) {
3253 if (mWindows.get(pos) == win) {
3254 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3255 return pos+1;
3256 }
3257 }
3258 }
3259 tokenPos--;
3260 }
3261
3262 return 0;
3263 }
3264
3265 private final int reAddWindowLocked(int index, WindowState win) {
3266 final int NCW = win.mChildWindows.size();
3267 boolean added = false;
3268 for (int j=0; j<NCW; j++) {
3269 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3270 if (!added && cwin.mSubLayer >= 0) {
3271 mWindows.add(index, win);
3272 index++;
3273 added = true;
3274 }
3275 mWindows.add(index, cwin);
3276 index++;
3277 }
3278 if (!added) {
3279 mWindows.add(index, win);
3280 index++;
3281 }
3282 return index;
3283 }
Romain Guy06882f82009-06-10 13:36:04 -07003284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3286 final int NW = token.windows.size();
3287 for (int i=0; i<NW; i++) {
3288 index = reAddWindowLocked(index, token.windows.get(i));
3289 }
3290 return index;
3291 }
3292
3293 public void moveAppToken(int index, IBinder token) {
3294 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3295 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003296 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 }
3298
3299 synchronized(mWindowMap) {
3300 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3301 if (DEBUG_REORDER) dumpAppTokensLocked();
3302 final AppWindowToken wtoken = findAppWindowToken(token);
3303 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3304 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3305 + token + " (" + wtoken + ")");
3306 return;
3307 }
3308 mAppTokens.add(index, wtoken);
3309 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3310 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 final long origId = Binder.clearCallingIdentity();
3313 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3314 if (DEBUG_REORDER) dumpWindowsLocked();
3315 if (tmpRemoveAppWindowsLocked(wtoken)) {
3316 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3317 if (DEBUG_REORDER) dumpWindowsLocked();
3318 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3319 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3320 if (DEBUG_REORDER) dumpWindowsLocked();
3321 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 mLayoutNeeded = true;
3323 performLayoutAndPlaceSurfacesLocked();
3324 }
3325 Binder.restoreCallingIdentity(origId);
3326 }
3327 }
3328
3329 private void removeAppTokensLocked(List<IBinder> tokens) {
3330 // XXX This should be done more efficiently!
3331 // (take advantage of the fact that both lists should be
3332 // ordered in the same way.)
3333 int N = tokens.size();
3334 for (int i=0; i<N; i++) {
3335 IBinder token = tokens.get(i);
3336 final AppWindowToken wtoken = findAppWindowToken(token);
3337 if (!mAppTokens.remove(wtoken)) {
3338 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3339 + token + " (" + wtoken + ")");
3340 i--;
3341 N--;
3342 }
3343 }
3344 }
3345
3346 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3347 // First remove all of the windows from the list.
3348 final int N = tokens.size();
3349 int i;
3350 for (i=0; i<N; i++) {
3351 WindowToken token = mTokenMap.get(tokens.get(i));
3352 if (token != null) {
3353 tmpRemoveAppWindowsLocked(token);
3354 }
3355 }
3356
3357 // Where to start adding?
3358 int pos = findWindowOffsetLocked(tokenPos);
3359
3360 // And now add them back at the correct place.
3361 for (i=0; i<N; i++) {
3362 WindowToken token = mTokenMap.get(tokens.get(i));
3363 if (token != null) {
3364 pos = reAddAppWindowsLocked(pos, token);
3365 }
3366 }
3367
3368 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 mLayoutNeeded = true;
3370 performLayoutAndPlaceSurfacesLocked();
3371
3372 //dump();
3373 }
3374
3375 public void moveAppTokensToTop(List<IBinder> tokens) {
3376 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3377 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003378 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 }
3380
3381 final long origId = Binder.clearCallingIdentity();
3382 synchronized(mWindowMap) {
3383 removeAppTokensLocked(tokens);
3384 final int N = tokens.size();
3385 for (int i=0; i<N; i++) {
3386 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3387 if (wt != null) {
3388 mAppTokens.add(wt);
3389 }
3390 }
3391 moveAppWindowsLocked(tokens, mAppTokens.size());
3392 }
3393 Binder.restoreCallingIdentity(origId);
3394 }
3395
3396 public void moveAppTokensToBottom(List<IBinder> tokens) {
3397 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3398 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003399 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 }
3401
3402 final long origId = Binder.clearCallingIdentity();
3403 synchronized(mWindowMap) {
3404 removeAppTokensLocked(tokens);
3405 final int N = tokens.size();
3406 int pos = 0;
3407 for (int i=0; i<N; i++) {
3408 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3409 if (wt != null) {
3410 mAppTokens.add(pos, wt);
3411 pos++;
3412 }
3413 }
3414 moveAppWindowsLocked(tokens, 0);
3415 }
3416 Binder.restoreCallingIdentity(origId);
3417 }
3418
3419 // -------------------------------------------------------------
3420 // Misc IWindowSession methods
3421 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 public void disableKeyguard(IBinder token, String tag) {
3424 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3425 != PackageManager.PERMISSION_GRANTED) {
3426 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3427 }
3428 mKeyguardDisabled.acquire(token, tag);
3429 }
3430
3431 public void reenableKeyguard(IBinder token) {
3432 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3433 != PackageManager.PERMISSION_GRANTED) {
3434 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3435 }
3436 synchronized (mKeyguardDisabled) {
3437 mKeyguardDisabled.release(token);
3438
3439 if (!mKeyguardDisabled.isAcquired()) {
3440 // if we are the last one to reenable the keyguard wait until
3441 // we have actaully finished reenabling until returning
3442 mWaitingUntilKeyguardReenabled = true;
3443 while (mWaitingUntilKeyguardReenabled) {
3444 try {
3445 mKeyguardDisabled.wait();
3446 } catch (InterruptedException e) {
3447 Thread.currentThread().interrupt();
3448 }
3449 }
3450 }
3451 }
3452 }
3453
3454 /**
3455 * @see android.app.KeyguardManager#exitKeyguardSecurely
3456 */
3457 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3458 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3459 != PackageManager.PERMISSION_GRANTED) {
3460 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3461 }
3462 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3463 public void onKeyguardExitResult(boolean success) {
3464 try {
3465 callback.onKeyguardExitResult(success);
3466 } catch (RemoteException e) {
3467 // Client has died, we don't care.
3468 }
3469 }
3470 });
3471 }
3472
3473 public boolean inKeyguardRestrictedInputMode() {
3474 return mPolicy.inKeyguardRestrictedKeyInputMode();
3475 }
Romain Guy06882f82009-06-10 13:36:04 -07003476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 static float fixScale(float scale) {
3478 if (scale < 0) scale = 0;
3479 else if (scale > 20) scale = 20;
3480 return Math.abs(scale);
3481 }
Romain Guy06882f82009-06-10 13:36:04 -07003482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 public void setAnimationScale(int which, float scale) {
3484 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3485 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003486 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
3488
3489 if (scale < 0) scale = 0;
3490 else if (scale > 20) scale = 20;
3491 scale = Math.abs(scale);
3492 switch (which) {
3493 case 0: mWindowAnimationScale = fixScale(scale); break;
3494 case 1: mTransitionAnimationScale = fixScale(scale); break;
3495 }
Romain Guy06882f82009-06-10 13:36:04 -07003496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 // Persist setting
3498 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3499 }
Romain Guy06882f82009-06-10 13:36:04 -07003500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 public void setAnimationScales(float[] scales) {
3502 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3503 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003504 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 }
3506
3507 if (scales != null) {
3508 if (scales.length >= 1) {
3509 mWindowAnimationScale = fixScale(scales[0]);
3510 }
3511 if (scales.length >= 2) {
3512 mTransitionAnimationScale = fixScale(scales[1]);
3513 }
3514 }
Romain Guy06882f82009-06-10 13:36:04 -07003515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 // Persist setting
3517 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3518 }
Romain Guy06882f82009-06-10 13:36:04 -07003519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 public float getAnimationScale(int which) {
3521 switch (which) {
3522 case 0: return mWindowAnimationScale;
3523 case 1: return mTransitionAnimationScale;
3524 }
3525 return 0;
3526 }
Romain Guy06882f82009-06-10 13:36:04 -07003527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 public float[] getAnimationScales() {
3529 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3530 }
Romain Guy06882f82009-06-10 13:36:04 -07003531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 public int getSwitchState(int sw) {
3533 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3534 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003535 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 }
3537 return KeyInputQueue.getSwitchState(sw);
3538 }
Romain Guy06882f82009-06-10 13:36:04 -07003539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 public int getSwitchStateForDevice(int devid, int sw) {
3541 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3542 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003543 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 }
3545 return KeyInputQueue.getSwitchState(devid, sw);
3546 }
Romain Guy06882f82009-06-10 13:36:04 -07003547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 public int getScancodeState(int sw) {
3549 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3550 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003551 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 }
3553 return KeyInputQueue.getScancodeState(sw);
3554 }
Romain Guy06882f82009-06-10 13:36:04 -07003555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 public int getScancodeStateForDevice(int devid, int sw) {
3557 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3558 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003559 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 }
3561 return KeyInputQueue.getScancodeState(devid, sw);
3562 }
Romain Guy06882f82009-06-10 13:36:04 -07003563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 public int getKeycodeState(int sw) {
3565 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3566 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003567 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 }
3569 return KeyInputQueue.getKeycodeState(sw);
3570 }
Romain Guy06882f82009-06-10 13:36:04 -07003571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 public int getKeycodeStateForDevice(int devid, int sw) {
3573 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3574 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003575 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 }
3577 return KeyInputQueue.getKeycodeState(devid, sw);
3578 }
Romain Guy06882f82009-06-10 13:36:04 -07003579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3581 return KeyInputQueue.hasKeys(keycodes, keyExists);
3582 }
Romain Guy06882f82009-06-10 13:36:04 -07003583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 public void enableScreenAfterBoot() {
3585 synchronized(mWindowMap) {
3586 if (mSystemBooted) {
3587 return;
3588 }
3589 mSystemBooted = true;
3590 }
Romain Guy06882f82009-06-10 13:36:04 -07003591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 performEnableScreen();
3593 }
Romain Guy06882f82009-06-10 13:36:04 -07003594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 public void enableScreenIfNeededLocked() {
3596 if (mDisplayEnabled) {
3597 return;
3598 }
3599 if (!mSystemBooted) {
3600 return;
3601 }
3602 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3603 }
Romain Guy06882f82009-06-10 13:36:04 -07003604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 public void performEnableScreen() {
3606 synchronized(mWindowMap) {
3607 if (mDisplayEnabled) {
3608 return;
3609 }
3610 if (!mSystemBooted) {
3611 return;
3612 }
Romain Guy06882f82009-06-10 13:36:04 -07003613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003614 // Don't enable the screen until all existing windows
3615 // have been drawn.
3616 final int N = mWindows.size();
3617 for (int i=0; i<N; i++) {
3618 WindowState w = (WindowState)mWindows.get(i);
3619 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3620 return;
3621 }
3622 }
Romain Guy06882f82009-06-10 13:36:04 -07003623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 mDisplayEnabled = true;
3625 if (false) {
3626 Log.i(TAG, "ENABLING SCREEN!");
3627 StringWriter sw = new StringWriter();
3628 PrintWriter pw = new PrintWriter(sw);
3629 this.dump(null, pw, null);
3630 Log.i(TAG, sw.toString());
3631 }
3632 try {
3633 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3634 if (surfaceFlinger != null) {
3635 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3636 Parcel data = Parcel.obtain();
3637 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3638 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3639 data, null, 0);
3640 data.recycle();
3641 }
3642 } catch (RemoteException ex) {
3643 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3644 }
3645 }
Romain Guy06882f82009-06-10 13:36:04 -07003646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003650 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3651 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 }
Romain Guy06882f82009-06-10 13:36:04 -07003653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 public void setInTouchMode(boolean mode) {
3655 synchronized(mWindowMap) {
3656 mInTouchMode = mode;
3657 }
3658 }
3659
Romain Guy06882f82009-06-10 13:36:04 -07003660 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003661 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003663 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003664 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 }
3666
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003667 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 }
Romain Guy06882f82009-06-10 13:36:04 -07003669
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003670 public void setRotationUnchecked(int rotation,
3671 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 if(DEBUG_ORIENTATION) Log.v(TAG,
3673 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 long origId = Binder.clearCallingIdentity();
3676 boolean changed;
3677 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003678 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 }
Romain Guy06882f82009-06-10 13:36:04 -07003680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681 if (changed) {
3682 sendNewConfiguration();
3683 synchronized(mWindowMap) {
3684 mLayoutNeeded = true;
3685 performLayoutAndPlaceSurfacesLocked();
3686 }
3687 } else if (alwaysSendConfiguration) {
3688 //update configuration ignoring orientation change
3689 sendNewConfiguration();
3690 }
Romain Guy06882f82009-06-10 13:36:04 -07003691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 Binder.restoreCallingIdentity(origId);
3693 }
Romain Guy06882f82009-06-10 13:36:04 -07003694
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003695 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 boolean changed;
3697 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3698 rotation = mRequestedRotation;
3699 } else {
3700 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003701 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 }
3703 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003704 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 mRotation, mDisplayEnabled);
3706 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3707 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003710 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 "Rotation changed to " + rotation
3712 + " from " + mRotation
3713 + " (forceApp=" + mForcedAppOrientation
3714 + ", req=" + mRequestedRotation + ")");
3715 mRotation = rotation;
3716 mWindowsFreezingScreen = true;
3717 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3718 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3719 2000);
3720 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003721 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 mQueue.setOrientation(rotation);
3723 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003724 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 }
3726 for (int i=mWindows.size()-1; i>=0; i--) {
3727 WindowState w = (WindowState)mWindows.get(i);
3728 if (w.mSurface != null) {
3729 w.mOrientationChanging = true;
3730 }
3731 }
3732 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3733 try {
3734 mRotationWatchers.get(i).onRotationChanged(rotation);
3735 } catch (RemoteException e) {
3736 }
3737 }
3738 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 return changed;
3741 }
Romain Guy06882f82009-06-10 13:36:04 -07003742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 public int getRotation() {
3744 return mRotation;
3745 }
3746
3747 public int watchRotation(IRotationWatcher watcher) {
3748 final IBinder watcherBinder = watcher.asBinder();
3749 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3750 public void binderDied() {
3751 synchronized (mWindowMap) {
3752 for (int i=0; i<mRotationWatchers.size(); i++) {
3753 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003754 IRotationWatcher removed = mRotationWatchers.remove(i);
3755 if (removed != null) {
3756 removed.asBinder().unlinkToDeath(this, 0);
3757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 i--;
3759 }
3760 }
3761 }
3762 }
3763 };
Romain Guy06882f82009-06-10 13:36:04 -07003764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 synchronized (mWindowMap) {
3766 try {
3767 watcher.asBinder().linkToDeath(dr, 0);
3768 mRotationWatchers.add(watcher);
3769 } catch (RemoteException e) {
3770 // Client died, no cleanup needed.
3771 }
Romain Guy06882f82009-06-10 13:36:04 -07003772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 return mRotation;
3774 }
3775 }
3776
3777 /**
3778 * Starts the view server on the specified port.
3779 *
3780 * @param port The port to listener to.
3781 *
3782 * @return True if the server was successfully started, false otherwise.
3783 *
3784 * @see com.android.server.ViewServer
3785 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3786 */
3787 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003788 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 return false;
3790 }
3791
3792 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3793 return false;
3794 }
3795
3796 if (port < 1024) {
3797 return false;
3798 }
3799
3800 if (mViewServer != null) {
3801 if (!mViewServer.isRunning()) {
3802 try {
3803 return mViewServer.start();
3804 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003805 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 }
3807 }
3808 return false;
3809 }
3810
3811 try {
3812 mViewServer = new ViewServer(this, port);
3813 return mViewServer.start();
3814 } catch (IOException e) {
3815 Log.w(TAG, "View server did not start");
3816 }
3817 return false;
3818 }
3819
Romain Guy06882f82009-06-10 13:36:04 -07003820 private boolean isSystemSecure() {
3821 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3822 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3823 }
3824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 /**
3826 * Stops the view server if it exists.
3827 *
3828 * @return True if the server stopped, false if it wasn't started or
3829 * couldn't be stopped.
3830 *
3831 * @see com.android.server.ViewServer
3832 */
3833 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003834 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 return false;
3836 }
3837
3838 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3839 return false;
3840 }
3841
3842 if (mViewServer != null) {
3843 return mViewServer.stop();
3844 }
3845 return false;
3846 }
3847
3848 /**
3849 * Indicates whether the view server is running.
3850 *
3851 * @return True if the server is running, false otherwise.
3852 *
3853 * @see com.android.server.ViewServer
3854 */
3855 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003856 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 return false;
3858 }
3859
3860 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3861 return false;
3862 }
3863
3864 return mViewServer != null && mViewServer.isRunning();
3865 }
3866
3867 /**
3868 * Lists all availble windows in the system. The listing is written in the
3869 * specified Socket's output stream with the following syntax:
3870 * windowHashCodeInHexadecimal windowName
3871 * Each line of the ouput represents a different window.
3872 *
3873 * @param client The remote client to send the listing to.
3874 * @return False if an error occured, true otherwise.
3875 */
3876 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003877 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 return false;
3879 }
3880
3881 boolean result = true;
3882
3883 Object[] windows;
3884 synchronized (mWindowMap) {
3885 windows = new Object[mWindows.size()];
3886 //noinspection unchecked
3887 windows = mWindows.toArray(windows);
3888 }
3889
3890 BufferedWriter out = null;
3891
3892 // Any uncaught exception will crash the system process
3893 try {
3894 OutputStream clientStream = client.getOutputStream();
3895 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3896
3897 final int count = windows.length;
3898 for (int i = 0; i < count; i++) {
3899 final WindowState w = (WindowState) windows[i];
3900 out.write(Integer.toHexString(System.identityHashCode(w)));
3901 out.write(' ');
3902 out.append(w.mAttrs.getTitle());
3903 out.write('\n');
3904 }
3905
3906 out.write("DONE.\n");
3907 out.flush();
3908 } catch (Exception e) {
3909 result = false;
3910 } finally {
3911 if (out != null) {
3912 try {
3913 out.close();
3914 } catch (IOException e) {
3915 result = false;
3916 }
3917 }
3918 }
3919
3920 return result;
3921 }
3922
3923 /**
3924 * Sends a command to a target window. The result of the command, if any, will be
3925 * written in the output stream of the specified socket.
3926 *
3927 * The parameters must follow this syntax:
3928 * windowHashcode extra
3929 *
3930 * Where XX is the length in characeters of the windowTitle.
3931 *
3932 * The first parameter is the target window. The window with the specified hashcode
3933 * will be the target. If no target can be found, nothing happens. The extra parameters
3934 * will be delivered to the target window and as parameters to the command itself.
3935 *
3936 * @param client The remote client to sent the result, if any, to.
3937 * @param command The command to execute.
3938 * @param parameters The command parameters.
3939 *
3940 * @return True if the command was successfully delivered, false otherwise. This does
3941 * not indicate whether the command itself was successful.
3942 */
3943 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003944 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 return false;
3946 }
3947
3948 boolean success = true;
3949 Parcel data = null;
3950 Parcel reply = null;
3951
3952 // Any uncaught exception will crash the system process
3953 try {
3954 // Find the hashcode of the window
3955 int index = parameters.indexOf(' ');
3956 if (index == -1) {
3957 index = parameters.length();
3958 }
3959 final String code = parameters.substring(0, index);
3960 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3961
3962 // Extract the command's parameter after the window description
3963 if (index < parameters.length()) {
3964 parameters = parameters.substring(index + 1);
3965 } else {
3966 parameters = "";
3967 }
3968
3969 final WindowManagerService.WindowState window = findWindow(hashCode);
3970 if (window == null) {
3971 return false;
3972 }
3973
3974 data = Parcel.obtain();
3975 data.writeInterfaceToken("android.view.IWindow");
3976 data.writeString(command);
3977 data.writeString(parameters);
3978 data.writeInt(1);
3979 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3980
3981 reply = Parcel.obtain();
3982
3983 final IBinder binder = window.mClient.asBinder();
3984 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3985 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3986
3987 reply.readException();
3988
3989 } catch (Exception e) {
3990 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3991 success = false;
3992 } finally {
3993 if (data != null) {
3994 data.recycle();
3995 }
3996 if (reply != null) {
3997 reply.recycle();
3998 }
3999 }
4000
4001 return success;
4002 }
4003
4004 private WindowState findWindow(int hashCode) {
4005 if (hashCode == -1) {
4006 return getFocusedWindow();
4007 }
4008
4009 synchronized (mWindowMap) {
4010 final ArrayList windows = mWindows;
4011 final int count = windows.size();
4012
4013 for (int i = 0; i < count; i++) {
4014 WindowState w = (WindowState) windows.get(i);
4015 if (System.identityHashCode(w) == hashCode) {
4016 return w;
4017 }
4018 }
4019 }
4020
4021 return null;
4022 }
4023
4024 /*
4025 * Instruct the Activity Manager to fetch the current configuration and broadcast
4026 * that to config-changed listeners if appropriate.
4027 */
4028 void sendNewConfiguration() {
4029 try {
4030 mActivityManager.updateConfiguration(null);
4031 } catch (RemoteException e) {
4032 }
4033 }
Romain Guy06882f82009-06-10 13:36:04 -07004034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 public Configuration computeNewConfiguration() {
4036 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004037 return computeNewConfigurationLocked();
4038 }
4039 }
Romain Guy06882f82009-06-10 13:36:04 -07004040
Dianne Hackbornc485a602009-03-24 22:39:49 -07004041 Configuration computeNewConfigurationLocked() {
4042 Configuration config = new Configuration();
4043 if (!computeNewConfigurationLocked(config)) {
4044 return null;
4045 }
4046 Log.i(TAG, "Config changed: " + config);
4047 long now = SystemClock.uptimeMillis();
4048 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
4049 if (mFreezeGcPending != 0) {
4050 if (now > (mFreezeGcPending+1000)) {
4051 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
4052 mH.removeMessages(H.FORCE_GC);
4053 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 mFreezeGcPending = now;
4055 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004056 } else {
4057 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004059 return config;
4060 }
Romain Guy06882f82009-06-10 13:36:04 -07004061
Dianne Hackbornc485a602009-03-24 22:39:49 -07004062 boolean computeNewConfigurationLocked(Configuration config) {
4063 if (mDisplay == null) {
4064 return false;
4065 }
4066 mQueue.getInputConfiguration(config);
4067 final int dw = mDisplay.getWidth();
4068 final int dh = mDisplay.getHeight();
4069 int orientation = Configuration.ORIENTATION_SQUARE;
4070 if (dw < dh) {
4071 orientation = Configuration.ORIENTATION_PORTRAIT;
4072 } else if (dw > dh) {
4073 orientation = Configuration.ORIENTATION_LANDSCAPE;
4074 }
4075 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004076
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004077 DisplayMetrics dm = new DisplayMetrics();
4078 mDisplay.getMetrics(dm);
4079 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4080
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004081 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004082 // Note we only do this once because at this point we don't
4083 // expect the screen to change in this way at runtime, and want
4084 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004085 int longSize = dw;
4086 int shortSize = dh;
4087 if (longSize < shortSize) {
4088 int tmp = longSize;
4089 longSize = shortSize;
4090 shortSize = tmp;
4091 }
4092 longSize = (int)(longSize/dm.density);
4093 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004094
Dianne Hackborn723738c2009-06-25 19:48:04 -07004095 // These semi-magic numbers define our compatibility modes for
4096 // applications with different screens. Don't change unless you
4097 // make sure to test lots and lots of apps!
4098 if (longSize < 470) {
4099 // This is shorter than an HVGA normal density screen (which
4100 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004101 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4102 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004103 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004104 // Is this a large screen?
4105 if (longSize > 640 && shortSize >= 480) {
4106 // VGA or larger screens at medium density are the point
4107 // at which we consider it to be a large screen.
4108 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4109 } else {
4110 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4111
4112 // If this screen is wider than normal HVGA, or taller
4113 // than FWVGA, then for old apps we want to run in size
4114 // compatibility mode.
4115 if (shortSize > 321 || longSize > 570) {
4116 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4117 }
4118 }
4119
4120 // Is this a long screen?
4121 if (((longSize*3)/5) >= (shortSize-1)) {
4122 // Anything wider than WVGA (5:3) is considering to be long.
4123 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4124 } else {
4125 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4126 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004127 }
4128 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004129 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004130
Dianne Hackbornc485a602009-03-24 22:39:49 -07004131 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4132 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4133 mPolicy.adjustConfigurationLw(config);
4134 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 }
Romain Guy06882f82009-06-10 13:36:04 -07004136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 // -------------------------------------------------------------
4138 // Input Events and Focus Management
4139 // -------------------------------------------------------------
4140
4141 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004142 long curTime = SystemClock.uptimeMillis();
4143
Michael Chane10de972009-05-18 11:24:50 -07004144 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004145 if (mLastTouchEventType == eventType &&
4146 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4147 return;
4148 }
4149 mLastUserActivityCallTime = curTime;
4150 mLastTouchEventType = eventType;
4151 }
4152
4153 if (targetWin == null
4154 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4155 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 }
4157 }
4158
4159 // tells if it's a cheek event or not -- this function is stateful
4160 private static final int EVENT_NONE = 0;
4161 private static final int EVENT_UNKNOWN = 0;
4162 private static final int EVENT_CHEEK = 0;
4163 private static final int EVENT_IGNORE_DURATION = 300; // ms
4164 private static final float CHEEK_THRESHOLD = 0.6f;
4165 private int mEventState = EVENT_NONE;
4166 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 private int eventType(MotionEvent ev) {
4169 float size = ev.getSize();
4170 switch (ev.getAction()) {
4171 case MotionEvent.ACTION_DOWN:
4172 mEventSize = size;
4173 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4174 case MotionEvent.ACTION_UP:
4175 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004176 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 case MotionEvent.ACTION_MOVE:
4178 final int N = ev.getHistorySize();
4179 if (size > mEventSize) mEventSize = size;
4180 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4181 for (int i=0; i<N; i++) {
4182 size = ev.getHistoricalSize(i);
4183 if (size > mEventSize) mEventSize = size;
4184 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4185 }
4186 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4187 return TOUCH_EVENT;
4188 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004189 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 }
4191 default:
4192 // not good
4193 return OTHER_EVENT;
4194 }
4195 }
4196
4197 /**
4198 * @return Returns true if event was dispatched, false if it was dropped for any reason
4199 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004200 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4202 "dispatchPointer " + ev);
4203
Michael Chan53071d62009-05-13 17:29:48 -07004204 if (MEASURE_LATENCY) {
4205 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4206 }
4207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004209 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210
Michael Chan53071d62009-05-13 17:29:48 -07004211 if (MEASURE_LATENCY) {
4212 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4213 }
4214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 if (action == MotionEvent.ACTION_UP) {
4218 // let go of our target
4219 mKeyWaiter.mMotionTarget = null;
4220 mPowerManager.logPointerUpEvent();
4221 } else if (action == MotionEvent.ACTION_DOWN) {
4222 mPowerManager.logPointerDownEvent();
4223 }
4224
4225 if (targetObj == null) {
4226 // In this case we are either dropping the event, or have received
4227 // a move or up without a down. It is common to receive move
4228 // events in such a way, since this means the user is moving the
4229 // pointer without actually pressing down. All other cases should
4230 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004231 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4233 }
4234 if (qev != null) {
4235 mQueue.recycleEvent(qev);
4236 }
4237 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004238 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 }
4240 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4241 if (qev != null) {
4242 mQueue.recycleEvent(qev);
4243 }
4244 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004245 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 }
Romain Guy06882f82009-06-10 13:36:04 -07004247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004250 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004251 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252
4253 //Log.i(TAG, "Sending " + ev + " to " + target);
4254
4255 if (uid != 0 && uid != target.mSession.mUid) {
4256 if (mContext.checkPermission(
4257 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4258 != PackageManager.PERMISSION_GRANTED) {
4259 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4260 + pid + " uid " + uid + " to window " + target
4261 + " owned by uid " + target.mSession.mUid);
4262 if (qev != null) {
4263 mQueue.recycleEvent(qev);
4264 }
4265 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004266 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 }
4268 }
4269
Michael Chan53071d62009-05-13 17:29:48 -07004270 if (MEASURE_LATENCY) {
4271 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4272 }
4273
Romain Guy06882f82009-06-10 13:36:04 -07004274 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4276 //target wants to ignore fat touch events
4277 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4278 //explicit flag to return without processing event further
4279 boolean returnFlag = false;
4280 if((action == MotionEvent.ACTION_DOWN)) {
4281 mFatTouch = false;
4282 if(cheekPress) {
4283 mFatTouch = true;
4284 returnFlag = true;
4285 }
4286 } else {
4287 if(action == MotionEvent.ACTION_UP) {
4288 if(mFatTouch) {
4289 //earlier even was invalid doesnt matter if current up is cheekpress or not
4290 mFatTouch = false;
4291 returnFlag = true;
4292 } else if(cheekPress) {
4293 //cancel the earlier event
4294 ev.setAction(MotionEvent.ACTION_CANCEL);
4295 action = MotionEvent.ACTION_CANCEL;
4296 }
4297 } else if(action == MotionEvent.ACTION_MOVE) {
4298 if(mFatTouch) {
4299 //two cases here
4300 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004301 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 returnFlag = true;
4303 } else if(cheekPress) {
4304 //valid down followed by invalid moves
4305 //an invalid move have to cancel earlier action
4306 ev.setAction(MotionEvent.ACTION_CANCEL);
4307 action = MotionEvent.ACTION_CANCEL;
4308 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4309 //note that the subsequent invalid moves will not get here
4310 mFatTouch = true;
4311 }
4312 }
4313 } //else if action
4314 if(returnFlag) {
4315 //recycle que, ev
4316 if (qev != null) {
4317 mQueue.recycleEvent(qev);
4318 }
4319 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004320 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 }
4322 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004323
Michael Chan9f028e62009-08-04 17:37:46 -07004324 // Enable this for testing the "right" value
4325 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004326 int max_events_per_sec = 35;
4327 try {
4328 max_events_per_sec = Integer.parseInt(SystemProperties
4329 .get("windowsmgr.max_events_per_sec"));
4330 if (max_events_per_sec < 1) {
4331 max_events_per_sec = 35;
4332 }
4333 } catch (NumberFormatException e) {
4334 }
4335 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4336 }
4337
4338 /*
4339 * Throttle events to minimize CPU usage when there's a flood of events
4340 * e.g. constant contact with the screen
4341 */
4342 if (action == MotionEvent.ACTION_MOVE) {
4343 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4344 long now = SystemClock.uptimeMillis();
4345 if (now < nextEventTime) {
4346 try {
4347 Thread.sleep(nextEventTime - now);
4348 } catch (InterruptedException e) {
4349 }
4350 mLastTouchEventTime = nextEventTime;
4351 } else {
4352 mLastTouchEventTime = now;
4353 }
4354 }
4355
Michael Chan53071d62009-05-13 17:29:48 -07004356 if (MEASURE_LATENCY) {
4357 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4358 }
4359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 synchronized(mWindowMap) {
4361 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4362 mKeyWaiter.bindTargetWindowLocked(target,
4363 KeyWaiter.RETURN_PENDING_POINTER, qev);
4364 ev = null;
4365 } else {
4366 if (action == MotionEvent.ACTION_DOWN) {
4367 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4368 if (out != null) {
4369 MotionEvent oev = MotionEvent.obtain(ev);
4370 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4371 do {
4372 final Rect frame = out.mFrame;
4373 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4374 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004375 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 } catch (android.os.RemoteException e) {
4377 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4378 }
4379 oev.offsetLocation((float)frame.left, (float)frame.top);
4380 out = out.mNextOutsideTouch;
4381 } while (out != null);
4382 mKeyWaiter.mOutsideTouchTargets = null;
4383 }
4384 }
4385 final Rect frame = target.mFrame;
4386 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4387 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004388
4389 // If we are on top of the wallpaper, then the wallpaper also
4390 // gets to see this movement.
4391 if (mWallpaperTarget == target) {
4392 sendPointerToWallpaperLocked(target, ev, eventTime);
4393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 }
4395 }
Romain Guy06882f82009-06-10 13:36:04 -07004396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 // finally offset the event to the target's coordinate system and
4398 // dispatch the event.
4399 try {
4400 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4401 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4402 }
Michael Chan53071d62009-05-13 17:29:48 -07004403
4404 if (MEASURE_LATENCY) {
4405 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4406 }
4407
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004408 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004409
4410 if (MEASURE_LATENCY) {
4411 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4412 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004413 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 } catch (android.os.RemoteException e) {
4415 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4416 mKeyWaiter.mMotionTarget = null;
4417 try {
4418 removeWindow(target.mSession, target.mClient);
4419 } catch (java.util.NoSuchElementException ex) {
4420 // This will happen if the window has already been
4421 // removed.
4422 }
4423 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004424 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 }
Romain Guy06882f82009-06-10 13:36:04 -07004426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 /**
4428 * @return Returns true if event was dispatched, false if it was dropped for any reason
4429 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004430 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 if (DEBUG_INPUT) Log.v(
4432 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004435 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 if (focusObj == null) {
4437 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4438 if (qev != null) {
4439 mQueue.recycleEvent(qev);
4440 }
4441 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004442 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 }
4444 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4445 if (qev != null) {
4446 mQueue.recycleEvent(qev);
4447 }
4448 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004449 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 }
Romain Guy06882f82009-06-10 13:36:04 -07004451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 if (uid != 0 && uid != focus.mSession.mUid) {
4455 if (mContext.checkPermission(
4456 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4457 != PackageManager.PERMISSION_GRANTED) {
4458 Log.w(TAG, "Permission denied: injecting key event from pid "
4459 + pid + " uid " + uid + " to window " + focus
4460 + " owned by uid " + focus.mSession.mUid);
4461 if (qev != null) {
4462 mQueue.recycleEvent(qev);
4463 }
4464 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004465 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 }
4467 }
Romain Guy06882f82009-06-10 13:36:04 -07004468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 synchronized(mWindowMap) {
4472 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4473 mKeyWaiter.bindTargetWindowLocked(focus,
4474 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4475 // We don't deliver movement events to the client, we hold
4476 // them and wait for them to call back.
4477 ev = null;
4478 } else {
4479 mKeyWaiter.bindTargetWindowLocked(focus);
4480 }
4481 }
Romain Guy06882f82009-06-10 13:36:04 -07004482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004484 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004485 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 } catch (android.os.RemoteException e) {
4487 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4488 try {
4489 removeWindow(focus.mSession, focus.mClient);
4490 } catch (java.util.NoSuchElementException ex) {
4491 // This will happen if the window has already been
4492 // removed.
4493 }
4494 }
Romain Guy06882f82009-06-10 13:36:04 -07004495
Dianne Hackborncfaef692009-06-15 14:24:44 -07004496 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004497 }
Romain Guy06882f82009-06-10 13:36:04 -07004498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 /**
4500 * @return Returns true if event was dispatched, false if it was dropped for any reason
4501 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004502 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4504
4505 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004506 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 if (focusObj == null) {
4508 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004509 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 }
4511 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004512 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 }
Romain Guy06882f82009-06-10 13:36:04 -07004514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 if (DEBUG_INPUT) Log.v(
4518 TAG, "Dispatching to " + focus + ": " + event);
4519
4520 if (uid != 0 && uid != focus.mSession.mUid) {
4521 if (mContext.checkPermission(
4522 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4523 != PackageManager.PERMISSION_GRANTED) {
4524 Log.w(TAG, "Permission denied: injecting key event from pid "
4525 + pid + " uid " + uid + " to window " + focus
4526 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004527 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528 }
4529 }
Romain Guy06882f82009-06-10 13:36:04 -07004530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531 synchronized(mWindowMap) {
4532 mKeyWaiter.bindTargetWindowLocked(focus);
4533 }
4534
4535 // NOSHIP extra state logging
4536 mKeyWaiter.recordDispatchState(event, focus);
4537 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539 try {
4540 if (DEBUG_INPUT || DEBUG_FOCUS) {
4541 Log.v(TAG, "Delivering key " + event.getKeyCode()
4542 + " to " + focus);
4543 }
4544 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004545 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 } catch (android.os.RemoteException e) {
4547 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4548 try {
4549 removeWindow(focus.mSession, focus.mClient);
4550 } catch (java.util.NoSuchElementException ex) {
4551 // This will happen if the window has already been
4552 // removed.
4553 }
4554 }
Romain Guy06882f82009-06-10 13:36:04 -07004555
Dianne Hackborncfaef692009-06-15 14:24:44 -07004556 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004557 }
Romain Guy06882f82009-06-10 13:36:04 -07004558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 public void pauseKeyDispatching(IBinder _token) {
4560 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4561 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004562 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 }
4564
4565 synchronized (mWindowMap) {
4566 WindowToken token = mTokenMap.get(_token);
4567 if (token != null) {
4568 mKeyWaiter.pauseDispatchingLocked(token);
4569 }
4570 }
4571 }
4572
4573 public void resumeKeyDispatching(IBinder _token) {
4574 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4575 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004576 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 }
4578
4579 synchronized (mWindowMap) {
4580 WindowToken token = mTokenMap.get(_token);
4581 if (token != null) {
4582 mKeyWaiter.resumeDispatchingLocked(token);
4583 }
4584 }
4585 }
4586
4587 public void setEventDispatching(boolean enabled) {
4588 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4589 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004590 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 }
4592
4593 synchronized (mWindowMap) {
4594 mKeyWaiter.setEventDispatchingLocked(enabled);
4595 }
4596 }
Romain Guy06882f82009-06-10 13:36:04 -07004597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 /**
4599 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004600 *
4601 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004602 * {@link SystemClock#uptimeMillis()} as the timebase.)
4603 * @param sync If true, wait for the event to be completed before returning to the caller.
4604 * @return Returns true if event was dispatched, false if it was dropped for any reason
4605 */
4606 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4607 long downTime = ev.getDownTime();
4608 long eventTime = ev.getEventTime();
4609
4610 int action = ev.getAction();
4611 int code = ev.getKeyCode();
4612 int repeatCount = ev.getRepeatCount();
4613 int metaState = ev.getMetaState();
4614 int deviceId = ev.getDeviceId();
4615 int scancode = ev.getScanCode();
4616
4617 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4618 if (downTime == 0) downTime = eventTime;
4619
4620 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004621 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004623 final int pid = Binder.getCallingPid();
4624 final int uid = Binder.getCallingUid();
4625 final long ident = Binder.clearCallingIdentity();
4626 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004628 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004629 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004630 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004631 switch (result) {
4632 case INJECT_NO_PERMISSION:
4633 throw new SecurityException(
4634 "Injecting to another application requires INJECT_EVENT permission");
4635 case INJECT_SUCCEEDED:
4636 return true;
4637 }
4638 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639 }
4640
4641 /**
4642 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004643 *
4644 * @param ev A motion event describing the pointer (touch) action. (As noted in
4645 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 * {@link SystemClock#uptimeMillis()} as the timebase.)
4647 * @param sync If true, wait for the event to be completed before returning to the caller.
4648 * @return Returns true if event was dispatched, false if it was dropped for any reason
4649 */
4650 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004651 final int pid = Binder.getCallingPid();
4652 final int uid = Binder.getCallingUid();
4653 final long ident = Binder.clearCallingIdentity();
4654 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004656 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004657 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004658 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004659 switch (result) {
4660 case INJECT_NO_PERMISSION:
4661 throw new SecurityException(
4662 "Injecting to another application requires INJECT_EVENT permission");
4663 case INJECT_SUCCEEDED:
4664 return true;
4665 }
4666 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004667 }
Romain Guy06882f82009-06-10 13:36:04 -07004668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 /**
4670 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004671 *
4672 * @param ev A motion event describing the trackball action. (As noted in
4673 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 * {@link SystemClock#uptimeMillis()} as the timebase.)
4675 * @param sync If true, wait for the event to be completed before returning to the caller.
4676 * @return Returns true if event was dispatched, false if it was dropped for any reason
4677 */
4678 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004679 final int pid = Binder.getCallingPid();
4680 final int uid = Binder.getCallingUid();
4681 final long ident = Binder.clearCallingIdentity();
4682 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004684 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004685 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004686 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004687 switch (result) {
4688 case INJECT_NO_PERMISSION:
4689 throw new SecurityException(
4690 "Injecting to another application requires INJECT_EVENT permission");
4691 case INJECT_SUCCEEDED:
4692 return true;
4693 }
4694 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 }
Romain Guy06882f82009-06-10 13:36:04 -07004696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 private WindowState getFocusedWindow() {
4698 synchronized (mWindowMap) {
4699 return getFocusedWindowLocked();
4700 }
4701 }
4702
4703 private WindowState getFocusedWindowLocked() {
4704 return mCurrentFocus;
4705 }
Romain Guy06882f82009-06-10 13:36:04 -07004706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 /**
4708 * This class holds the state for dispatching key events. This state
4709 * is protected by the KeyWaiter instance, NOT by the window lock. You
4710 * can be holding the main window lock while acquire the KeyWaiter lock,
4711 * but not the other way around.
4712 */
4713 final class KeyWaiter {
4714 // NOSHIP debugging
4715 public class DispatchState {
4716 private KeyEvent event;
4717 private WindowState focus;
4718 private long time;
4719 private WindowState lastWin;
4720 private IBinder lastBinder;
4721 private boolean finished;
4722 private boolean gotFirstWindow;
4723 private boolean eventDispatching;
4724 private long timeToSwitch;
4725 private boolean wasFrozen;
4726 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004727 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4730 focus = theFocus;
4731 event = theEvent;
4732 time = System.currentTimeMillis();
4733 // snapshot KeyWaiter state
4734 lastWin = mLastWin;
4735 lastBinder = mLastBinder;
4736 finished = mFinished;
4737 gotFirstWindow = mGotFirstWindow;
4738 eventDispatching = mEventDispatching;
4739 timeToSwitch = mTimeToSwitch;
4740 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004741 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004742 // cache the paused state at ctor time as well
4743 if (theFocus == null || theFocus.mToken == null) {
4744 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4745 focusPaused = false;
4746 } else {
4747 focusPaused = theFocus.mToken.paused;
4748 }
4749 }
Romain Guy06882f82009-06-10 13:36:04 -07004750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004751 public String toString() {
4752 return "{{" + event + " to " + focus + " @ " + time
4753 + " lw=" + lastWin + " lb=" + lastBinder
4754 + " fin=" + finished + " gfw=" + gotFirstWindow
4755 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004756 + " wf=" + wasFrozen + " fp=" + focusPaused
4757 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 }
4759 };
4760 private DispatchState mDispatchState = null;
4761 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4762 mDispatchState = new DispatchState(theEvent, theFocus);
4763 }
4764 // END NOSHIP
4765
4766 public static final int RETURN_NOTHING = 0;
4767 public static final int RETURN_PENDING_POINTER = 1;
4768 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 final Object SKIP_TARGET_TOKEN = new Object();
4771 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004773 private WindowState mLastWin = null;
4774 private IBinder mLastBinder = null;
4775 private boolean mFinished = true;
4776 private boolean mGotFirstWindow = false;
4777 private boolean mEventDispatching = true;
4778 private long mTimeToSwitch = 0;
4779 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 // Target of Motion events
4782 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004784 // Windows above the target who would like to receive an "outside"
4785 // touch event for any down events outside of them.
4786 WindowState mOutsideTouchTargets;
4787
4788 /**
4789 * Wait for the last event dispatch to complete, then find the next
4790 * target that should receive the given event and wait for that one
4791 * to be ready to receive it.
4792 */
4793 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4794 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004795 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 long startTime = SystemClock.uptimeMillis();
4797 long keyDispatchingTimeout = 5 * 1000;
4798 long waitedFor = 0;
4799
4800 while (true) {
4801 // Figure out which window we care about. It is either the
4802 // last window we are waiting to have process the event or,
4803 // if none, then the next window we think the event should go
4804 // to. Note: we retrieve mLastWin outside of the lock, so
4805 // it may change before we lock. Thus we must check it again.
4806 WindowState targetWin = mLastWin;
4807 boolean targetIsNew = targetWin == null;
4808 if (DEBUG_INPUT) Log.v(
4809 TAG, "waitForLastKey: mFinished=" + mFinished +
4810 ", mLastWin=" + mLastWin);
4811 if (targetIsNew) {
4812 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004813 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004814 if (target == SKIP_TARGET_TOKEN) {
4815 // The user has pressed a special key, and we are
4816 // dropping all pending events before it.
4817 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4818 + " " + nextMotion);
4819 return null;
4820 }
4821 if (target == CONSUMED_EVENT_TOKEN) {
4822 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4823 + " " + nextMotion);
4824 return target;
4825 }
4826 targetWin = (WindowState)target;
4827 }
Romain Guy06882f82009-06-10 13:36:04 -07004828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004829 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004831 // Now: is it okay to send the next event to this window?
4832 synchronized (this) {
4833 // First: did we come here based on the last window not
4834 // being null, but it changed by the time we got here?
4835 // If so, try again.
4836 if (!targetIsNew && mLastWin == null) {
4837 continue;
4838 }
Romain Guy06882f82009-06-10 13:36:04 -07004839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 // We never dispatch events if not finished with the
4841 // last one, or the display is frozen.
4842 if (mFinished && !mDisplayFrozen) {
4843 // If event dispatching is disabled, then we
4844 // just consume the events.
4845 if (!mEventDispatching) {
4846 if (DEBUG_INPUT) Log.v(TAG,
4847 "Skipping event; dispatching disabled: "
4848 + nextKey + " " + nextMotion);
4849 return null;
4850 }
4851 if (targetWin != null) {
4852 // If this is a new target, and that target is not
4853 // paused or unresponsive, then all looks good to
4854 // handle the event.
4855 if (targetIsNew && !targetWin.mToken.paused) {
4856 return targetWin;
4857 }
Romain Guy06882f82009-06-10 13:36:04 -07004858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004859 // If we didn't find a target window, and there is no
4860 // focused app window, then just eat the events.
4861 } else if (mFocusedApp == null) {
4862 if (DEBUG_INPUT) Log.v(TAG,
4863 "Skipping event; no focused app: "
4864 + nextKey + " " + nextMotion);
4865 return null;
4866 }
4867 }
Romain Guy06882f82009-06-10 13:36:04 -07004868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 if (DEBUG_INPUT) Log.v(
4870 TAG, "Waiting for last key in " + mLastBinder
4871 + " target=" + targetWin
4872 + " mFinished=" + mFinished
4873 + " mDisplayFrozen=" + mDisplayFrozen
4874 + " targetIsNew=" + targetIsNew
4875 + " paused="
4876 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004877 + " mFocusedApp=" + mFocusedApp
4878 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 targetApp = targetWin != null
4881 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 long curTimeout = keyDispatchingTimeout;
4884 if (mTimeToSwitch != 0) {
4885 long now = SystemClock.uptimeMillis();
4886 if (mTimeToSwitch <= now) {
4887 // If an app switch key has been pressed, and we have
4888 // waited too long for the current app to finish
4889 // processing keys, then wait no more!
4890 doFinishedKeyLocked(true);
4891 continue;
4892 }
4893 long switchTimeout = mTimeToSwitch - now;
4894 if (curTimeout > switchTimeout) {
4895 curTimeout = switchTimeout;
4896 }
4897 }
Romain Guy06882f82009-06-10 13:36:04 -07004898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 try {
4900 // after that continue
4901 // processing keys, so we don't get stuck.
4902 if (DEBUG_INPUT) Log.v(
4903 TAG, "Waiting for key dispatch: " + curTimeout);
4904 wait(curTimeout);
4905 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4906 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004907 + startTime + " switchTime=" + mTimeToSwitch
4908 + " target=" + targetWin + " mLW=" + mLastWin
4909 + " mLB=" + mLastBinder + " fin=" + mFinished
4910 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 } catch (InterruptedException e) {
4912 }
4913 }
4914
4915 // If we were frozen during configuration change, restart the
4916 // timeout checks from now; otherwise look at whether we timed
4917 // out before awakening.
4918 if (mWasFrozen) {
4919 waitedFor = 0;
4920 mWasFrozen = false;
4921 } else {
4922 waitedFor = SystemClock.uptimeMillis() - startTime;
4923 }
4924
4925 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4926 IApplicationToken at = null;
4927 synchronized (this) {
4928 Log.w(TAG, "Key dispatching timed out sending to " +
4929 (targetWin != null ? targetWin.mAttrs.getTitle()
4930 : "<null>"));
4931 // NOSHIP debugging
4932 Log.w(TAG, "Dispatch state: " + mDispatchState);
4933 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4934 // END NOSHIP
4935 //dump();
4936 if (targetWin != null) {
4937 at = targetWin.getAppToken();
4938 } else if (targetApp != null) {
4939 at = targetApp.appToken;
4940 }
4941 }
4942
4943 boolean abort = true;
4944 if (at != null) {
4945 try {
4946 long timeout = at.getKeyDispatchingTimeout();
4947 if (timeout > waitedFor) {
4948 // we did not wait the proper amount of time for this application.
4949 // set the timeout to be the real timeout and wait again.
4950 keyDispatchingTimeout = timeout - waitedFor;
4951 continue;
4952 } else {
4953 abort = at.keyDispatchingTimedOut();
4954 }
4955 } catch (RemoteException ex) {
4956 }
4957 }
4958
4959 synchronized (this) {
4960 if (abort && (mLastWin == targetWin || targetWin == null)) {
4961 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004962 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 if (DEBUG_INPUT) Log.v(TAG,
4964 "Window " + mLastWin +
4965 " timed out on key input");
4966 if (mLastWin.mToken.paused) {
4967 Log.w(TAG, "Un-pausing dispatching to this window");
4968 mLastWin.mToken.paused = false;
4969 }
4970 }
4971 if (mMotionTarget == targetWin) {
4972 mMotionTarget = null;
4973 }
4974 mLastWin = null;
4975 mLastBinder = null;
4976 if (failIfTimeout || targetWin == null) {
4977 return null;
4978 }
4979 } else {
4980 Log.w(TAG, "Continuing to wait for key to be dispatched");
4981 startTime = SystemClock.uptimeMillis();
4982 }
4983 }
4984 }
4985 }
4986 }
Romain Guy06882f82009-06-10 13:36:04 -07004987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004989 MotionEvent nextMotion, boolean isPointerEvent,
4990 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004991 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004993 if (nextKey != null) {
4994 // Find the target window for a normal key event.
4995 final int keycode = nextKey.getKeyCode();
4996 final int repeatCount = nextKey.getRepeatCount();
4997 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4998 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005001 if (callingUid == 0 ||
5002 mContext.checkPermission(
5003 android.Manifest.permission.INJECT_EVENTS,
5004 callingPid, callingUid)
5005 == PackageManager.PERMISSION_GRANTED) {
5006 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005007 nextKey.getMetaState(), down, repeatCount,
5008 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005010 Log.w(TAG, "Event timeout during app switch: dropping "
5011 + nextKey);
5012 return SKIP_TARGET_TOKEN;
5013 }
Romain Guy06882f82009-06-10 13:36:04 -07005014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017 WindowState focus = null;
5018 synchronized(mWindowMap) {
5019 focus = getFocusedWindowLocked();
5020 }
Romain Guy06882f82009-06-10 13:36:04 -07005021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005022 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005023
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005024 if (callingUid == 0 ||
5025 (focus != null && callingUid == focus.mSession.mUid) ||
5026 mContext.checkPermission(
5027 android.Manifest.permission.INJECT_EVENTS,
5028 callingPid, callingUid)
5029 == PackageManager.PERMISSION_GRANTED) {
5030 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005031 keycode, nextKey.getMetaState(), down, repeatCount,
5032 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005033 return CONSUMED_EVENT_TOKEN;
5034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 }
Romain Guy06882f82009-06-10 13:36:04 -07005036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005039 } else if (!isPointerEvent) {
5040 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5041 if (!dispatch) {
5042 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5043 + nextMotion);
5044 return SKIP_TARGET_TOKEN;
5045 }
Romain Guy06882f82009-06-10 13:36:04 -07005046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047 WindowState focus = null;
5048 synchronized(mWindowMap) {
5049 focus = getFocusedWindowLocked();
5050 }
Romain Guy06882f82009-06-10 13:36:04 -07005051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5053 return focus;
5054 }
Romain Guy06882f82009-06-10 13:36:04 -07005055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005056 if (nextMotion == null) {
5057 return SKIP_TARGET_TOKEN;
5058 }
Romain Guy06882f82009-06-10 13:36:04 -07005059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5061 KeyEvent.KEYCODE_UNKNOWN);
5062 if (!dispatch) {
5063 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5064 + nextMotion);
5065 return SKIP_TARGET_TOKEN;
5066 }
Romain Guy06882f82009-06-10 13:36:04 -07005067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 // Find the target window for a pointer event.
5069 int action = nextMotion.getAction();
5070 final float xf = nextMotion.getX();
5071 final float yf = nextMotion.getY();
5072 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 final boolean screenWasOff = qev != null
5075 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005077 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 synchronized(mWindowMap) {
5080 synchronized (this) {
5081 if (action == MotionEvent.ACTION_DOWN) {
5082 if (mMotionTarget != null) {
5083 // this is weird, we got a pen down, but we thought it was
5084 // already down!
5085 // XXX: We should probably send an ACTION_UP to the current
5086 // target.
5087 Log.w(TAG, "Pointer down received while already down in: "
5088 + mMotionTarget);
5089 mMotionTarget = null;
5090 }
Romain Guy06882f82009-06-10 13:36:04 -07005091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092 // ACTION_DOWN is special, because we need to lock next events to
5093 // the window we'll land onto.
5094 final int x = (int)xf;
5095 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 final ArrayList windows = mWindows;
5098 final int N = windows.size();
5099 WindowState topErrWindow = null;
5100 final Rect tmpRect = mTempRect;
5101 for (int i=N-1; i>=0; i--) {
5102 WindowState child = (WindowState)windows.get(i);
5103 //Log.i(TAG, "Checking dispatch to: " + child);
5104 final int flags = child.mAttrs.flags;
5105 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5106 if (topErrWindow == null) {
5107 topErrWindow = child;
5108 }
5109 }
5110 if (!child.isVisibleLw()) {
5111 //Log.i(TAG, "Not visible!");
5112 continue;
5113 }
5114 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5115 //Log.i(TAG, "Not touchable!");
5116 if ((flags & WindowManager.LayoutParams
5117 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5118 child.mNextOutsideTouch = mOutsideTouchTargets;
5119 mOutsideTouchTargets = child;
5120 }
5121 continue;
5122 }
5123 tmpRect.set(child.mFrame);
5124 if (child.mTouchableInsets == ViewTreeObserver
5125 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5126 // The touch is inside of the window if it is
5127 // inside the frame, AND the content part of that
5128 // frame that was given by the application.
5129 tmpRect.left += child.mGivenContentInsets.left;
5130 tmpRect.top += child.mGivenContentInsets.top;
5131 tmpRect.right -= child.mGivenContentInsets.right;
5132 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5133 } else if (child.mTouchableInsets == ViewTreeObserver
5134 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5135 // The touch is inside of the window if it is
5136 // inside the frame, AND the visible part of that
5137 // frame that was given by the application.
5138 tmpRect.left += child.mGivenVisibleInsets.left;
5139 tmpRect.top += child.mGivenVisibleInsets.top;
5140 tmpRect.right -= child.mGivenVisibleInsets.right;
5141 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5142 }
5143 final int touchFlags = flags &
5144 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5145 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5146 if (tmpRect.contains(x, y) || touchFlags == 0) {
5147 //Log.i(TAG, "Using this target!");
5148 if (!screenWasOff || (flags &
5149 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5150 mMotionTarget = child;
5151 } else {
5152 //Log.i(TAG, "Waking, skip!");
5153 mMotionTarget = null;
5154 }
5155 break;
5156 }
Romain Guy06882f82009-06-10 13:36:04 -07005157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005158 if ((flags & WindowManager.LayoutParams
5159 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5160 child.mNextOutsideTouch = mOutsideTouchTargets;
5161 mOutsideTouchTargets = child;
5162 //Log.i(TAG, "Adding to outside target list: " + child);
5163 }
5164 }
5165
5166 // if there's an error window but it's not accepting
5167 // focus (typically because it is not yet visible) just
5168 // wait for it -- any other focused window may in fact
5169 // be in ANR state.
5170 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5171 mMotionTarget = null;
5172 }
5173 }
Romain Guy06882f82009-06-10 13:36:04 -07005174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 target = mMotionTarget;
5176 }
5177 }
Romain Guy06882f82009-06-10 13:36:04 -07005178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 // Pointer events are a little different -- if there isn't a
5182 // target found for any event, then just drop it.
5183 return target != null ? target : SKIP_TARGET_TOKEN;
5184 }
Romain Guy06882f82009-06-10 13:36:04 -07005185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 boolean checkShouldDispatchKey(int keycode) {
5187 synchronized (this) {
5188 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5189 mTimeToSwitch = 0;
5190 return true;
5191 }
5192 if (mTimeToSwitch != 0
5193 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5194 return false;
5195 }
5196 return true;
5197 }
5198 }
Romain Guy06882f82009-06-10 13:36:04 -07005199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 void bindTargetWindowLocked(WindowState win,
5201 int pendingWhat, QueuedEvent pendingMotion) {
5202 synchronized (this) {
5203 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5204 }
5205 }
Romain Guy06882f82009-06-10 13:36:04 -07005206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 void bindTargetWindowLocked(WindowState win) {
5208 synchronized (this) {
5209 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5210 }
5211 }
5212
5213 void bindTargetWindowLockedLocked(WindowState win,
5214 int pendingWhat, QueuedEvent pendingMotion) {
5215 mLastWin = win;
5216 mLastBinder = win.mClient.asBinder();
5217 mFinished = false;
5218 if (pendingMotion != null) {
5219 final Session s = win.mSession;
5220 if (pendingWhat == RETURN_PENDING_POINTER) {
5221 releasePendingPointerLocked(s);
5222 s.mPendingPointerMove = pendingMotion;
5223 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005224 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 "bindTargetToWindow " + s.mPendingPointerMove);
5226 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5227 releasePendingTrackballLocked(s);
5228 s.mPendingTrackballMove = pendingMotion;
5229 s.mPendingTrackballWindow = win;
5230 }
5231 }
5232 }
Romain Guy06882f82009-06-10 13:36:04 -07005233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 void releasePendingPointerLocked(Session s) {
5235 if (DEBUG_INPUT) Log.v(TAG,
5236 "releasePendingPointer " + s.mPendingPointerMove);
5237 if (s.mPendingPointerMove != null) {
5238 mQueue.recycleEvent(s.mPendingPointerMove);
5239 s.mPendingPointerMove = null;
5240 }
5241 }
Romain Guy06882f82009-06-10 13:36:04 -07005242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 void releasePendingTrackballLocked(Session s) {
5244 if (s.mPendingTrackballMove != null) {
5245 mQueue.recycleEvent(s.mPendingTrackballMove);
5246 s.mPendingTrackballMove = null;
5247 }
5248 }
Romain Guy06882f82009-06-10 13:36:04 -07005249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005250 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5251 int returnWhat) {
5252 if (DEBUG_INPUT) Log.v(
5253 TAG, "finishedKey: client=" + client + ", force=" + force);
5254
5255 if (client == null) {
5256 return null;
5257 }
5258
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005259 MotionEvent res = null;
5260 QueuedEvent qev = null;
5261 WindowState win = null;
5262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 synchronized (this) {
5264 if (DEBUG_INPUT) Log.v(
5265 TAG, "finishedKey: client=" + client.asBinder()
5266 + ", force=" + force + ", last=" + mLastBinder
5267 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269 if (returnWhat == RETURN_PENDING_POINTER) {
5270 qev = session.mPendingPointerMove;
5271 win = session.mPendingPointerWindow;
5272 session.mPendingPointerMove = null;
5273 session.mPendingPointerWindow = null;
5274 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5275 qev = session.mPendingTrackballMove;
5276 win = session.mPendingTrackballWindow;
5277 session.mPendingTrackballMove = null;
5278 session.mPendingTrackballWindow = null;
5279 }
Romain Guy06882f82009-06-10 13:36:04 -07005280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 if (mLastBinder == client.asBinder()) {
5282 if (DEBUG_INPUT) Log.v(
5283 TAG, "finishedKey: last paused="
5284 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5285 if (mLastWin != null && (!mLastWin.mToken.paused || force
5286 || !mEventDispatching)) {
5287 doFinishedKeyLocked(false);
5288 } else {
5289 // Make sure to wake up anyone currently waiting to
5290 // dispatch a key, so they can re-evaluate their
5291 // current situation.
5292 mFinished = true;
5293 notifyAll();
5294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 }
Romain Guy06882f82009-06-10 13:36:04 -07005296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005298 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 if (DEBUG_INPUT) Log.v(TAG,
5300 "Returning pending motion: " + res);
5301 mQueue.recycleEvent(qev);
5302 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5303 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005307
5308 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5309 synchronized (mWindowMap) {
5310 if (mWallpaperTarget == win) {
5311 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5312 }
5313 }
5314 }
5315
5316 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 }
5318
5319 void tickle() {
5320 synchronized (this) {
5321 notifyAll();
5322 }
5323 }
Romain Guy06882f82009-06-10 13:36:04 -07005324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 void handleNewWindowLocked(WindowState newWindow) {
5326 if (!newWindow.canReceiveKeys()) {
5327 return;
5328 }
5329 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005330 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331 TAG, "New key dispatch window: win="
5332 + newWindow.mClient.asBinder()
5333 + ", last=" + mLastBinder
5334 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5335 + "), finished=" + mFinished + ", paused="
5336 + newWindow.mToken.paused);
5337
5338 // Displaying a window implicitly causes dispatching to
5339 // be unpaused. (This is to protect against bugs if someone
5340 // pauses dispatching but forgets to resume.)
5341 newWindow.mToken.paused = false;
5342
5343 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005344
5345 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5346 if (DEBUG_INPUT) Log.v(TAG,
5347 "New SYSTEM_ERROR window; resetting state");
5348 mLastWin = null;
5349 mLastBinder = null;
5350 mMotionTarget = null;
5351 mFinished = true;
5352 } else if (mLastWin != null) {
5353 // If the new window is above the window we are
5354 // waiting on, then stop waiting and let key dispatching
5355 // start on the new guy.
5356 if (DEBUG_INPUT) Log.v(
5357 TAG, "Last win layer=" + mLastWin.mLayer
5358 + ", new win layer=" + newWindow.mLayer);
5359 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005360 // The new window is above the old; finish pending input to the last
5361 // window and start directing it to the new one.
5362 mLastWin.mToken.paused = false;
5363 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005365 // Either the new window is lower, so there is no need to wake key waiters,
5366 // or we just finished key input to the previous window, which implicitly
5367 // notified the key waiters. In both cases, we don't need to issue the
5368 // notification here.
5369 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 }
5371
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005372 // Now that we've put a new window state in place, make the event waiter
5373 // take notice and retarget its attentions.
5374 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 }
5376 }
5377
5378 void pauseDispatchingLocked(WindowToken token) {
5379 synchronized (this)
5380 {
5381 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5382 token.paused = true;
5383
5384 /*
5385 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5386 mPaused = true;
5387 } else {
5388 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005389 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005391 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005393 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 }
5395 }
5396 */
5397 }
5398 }
5399
5400 void resumeDispatchingLocked(WindowToken token) {
5401 synchronized (this) {
5402 if (token.paused) {
5403 if (DEBUG_INPUT) Log.v(
5404 TAG, "Resuming WindowToken " + token
5405 + ", last=" + mLastBinder
5406 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5407 + "), finished=" + mFinished + ", paused="
5408 + token.paused);
5409 token.paused = false;
5410 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5411 doFinishedKeyLocked(true);
5412 } else {
5413 notifyAll();
5414 }
5415 }
5416 }
5417 }
5418
5419 void setEventDispatchingLocked(boolean enabled) {
5420 synchronized (this) {
5421 mEventDispatching = enabled;
5422 notifyAll();
5423 }
5424 }
Romain Guy06882f82009-06-10 13:36:04 -07005425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 void appSwitchComing() {
5427 synchronized (this) {
5428 // Don't wait for more than .5 seconds for app to finish
5429 // processing the pending events.
5430 long now = SystemClock.uptimeMillis() + 500;
5431 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5432 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5433 mTimeToSwitch = now;
5434 }
5435 notifyAll();
5436 }
5437 }
Romain Guy06882f82009-06-10 13:36:04 -07005438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 private final void doFinishedKeyLocked(boolean doRecycle) {
5440 if (mLastWin != null) {
5441 releasePendingPointerLocked(mLastWin.mSession);
5442 releasePendingTrackballLocked(mLastWin.mSession);
5443 }
Romain Guy06882f82009-06-10 13:36:04 -07005444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005445 if (mLastWin == null || !mLastWin.mToken.paused
5446 || !mLastWin.isVisibleLw()) {
5447 // If the current window has been paused, we aren't -really-
5448 // finished... so let the waiters still wait.
5449 mLastWin = null;
5450 mLastBinder = null;
5451 }
5452 mFinished = true;
5453 notifyAll();
5454 }
5455 }
5456
5457 private class KeyQ extends KeyInputQueue
5458 implements KeyInputQueue.FilterCallback {
5459 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005461 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005462 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5464 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5465 "KEEP_SCREEN_ON_FLAG");
5466 mHoldingScreen.setReferenceCounted(false);
5467 }
5468
5469 @Override
5470 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5471 if (mPolicy.preprocessInputEventTq(event)) {
5472 return true;
5473 }
Romain Guy06882f82009-06-10 13:36:04 -07005474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 switch (event.type) {
5476 case RawInputEvent.EV_KEY: {
5477 // XXX begin hack
5478 if (DEBUG) {
5479 if (event.keycode == KeyEvent.KEYCODE_G) {
5480 if (event.value != 0) {
5481 // G down
5482 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5483 }
5484 return false;
5485 }
5486 if (event.keycode == KeyEvent.KEYCODE_D) {
5487 if (event.value != 0) {
5488 //dump();
5489 }
5490 return false;
5491 }
5492 }
5493 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005495 boolean screenIsOff = !mPowerManager.screenIsOn();
5496 boolean screenIsDim = !mPowerManager.screenIsBright();
5497 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5500 mPowerManager.goToSleep(event.when);
5501 }
5502
5503 if (screenIsOff) {
5504 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5505 }
5506 if (screenIsDim) {
5507 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5508 }
5509 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5510 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005511 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 }
Romain Guy06882f82009-06-10 13:36:04 -07005513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5515 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5516 filterQueue(this);
5517 mKeyWaiter.appSwitchComing();
5518 }
5519 return true;
5520 } else {
5521 return false;
5522 }
5523 }
Romain Guy06882f82009-06-10 13:36:04 -07005524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005525 case RawInputEvent.EV_REL: {
5526 boolean screenIsOff = !mPowerManager.screenIsOn();
5527 boolean screenIsDim = !mPowerManager.screenIsBright();
5528 if (screenIsOff) {
5529 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5530 device.classes, event)) {
5531 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5532 return false;
5533 }
5534 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5535 }
5536 if (screenIsDim) {
5537 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5538 }
5539 return true;
5540 }
Romain Guy06882f82009-06-10 13:36:04 -07005541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 case RawInputEvent.EV_ABS: {
5543 boolean screenIsOff = !mPowerManager.screenIsOn();
5544 boolean screenIsDim = !mPowerManager.screenIsBright();
5545 if (screenIsOff) {
5546 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5547 device.classes, event)) {
5548 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5549 return false;
5550 }
5551 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5552 }
5553 if (screenIsDim) {
5554 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5555 }
5556 return true;
5557 }
Romain Guy06882f82009-06-10 13:36:04 -07005558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 default:
5560 return true;
5561 }
5562 }
5563
5564 public int filterEvent(QueuedEvent ev) {
5565 switch (ev.classType) {
5566 case RawInputEvent.CLASS_KEYBOARD:
5567 KeyEvent ke = (KeyEvent)ev.event;
5568 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5569 Log.w(TAG, "Dropping movement key during app switch: "
5570 + ke.getKeyCode() + ", action=" + ke.getAction());
5571 return FILTER_REMOVE;
5572 }
5573 return FILTER_ABORT;
5574 default:
5575 return FILTER_KEEP;
5576 }
5577 }
Romain Guy06882f82009-06-10 13:36:04 -07005578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 /**
5580 * Must be called with the main window manager lock held.
5581 */
5582 void setHoldScreenLocked(boolean holding) {
5583 boolean state = mHoldingScreen.isHeld();
5584 if (holding != state) {
5585 if (holding) {
5586 mHoldingScreen.acquire();
5587 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005588 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005589 mHoldingScreen.release();
5590 }
5591 }
5592 }
Michael Chan53071d62009-05-13 17:29:48 -07005593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594
5595 public boolean detectSafeMode() {
5596 mSafeMode = mPolicy.detectSafeMode();
5597 return mSafeMode;
5598 }
Romain Guy06882f82009-06-10 13:36:04 -07005599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 public void systemReady() {
5601 mPolicy.systemReady();
5602 }
Romain Guy06882f82009-06-10 13:36:04 -07005603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 private final class InputDispatcherThread extends Thread {
5605 // Time to wait when there is nothing to do: 9999 seconds.
5606 static final int LONG_WAIT=9999*1000;
5607
5608 public InputDispatcherThread() {
5609 super("InputDispatcher");
5610 }
Romain Guy06882f82009-06-10 13:36:04 -07005611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 @Override
5613 public void run() {
5614 while (true) {
5615 try {
5616 process();
5617 } catch (Exception e) {
5618 Log.e(TAG, "Exception in input dispatcher", e);
5619 }
5620 }
5621 }
Romain Guy06882f82009-06-10 13:36:04 -07005622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005623 private void process() {
5624 android.os.Process.setThreadPriority(
5625 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 // The last key event we saw
5628 KeyEvent lastKey = null;
5629
5630 // Last keydown time for auto-repeating keys
5631 long lastKeyTime = SystemClock.uptimeMillis();
5632 long nextKeyTime = lastKeyTime+LONG_WAIT;
5633
Romain Guy06882f82009-06-10 13:36:04 -07005634 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005635 int keyRepeatCount = 0;
5636
5637 // Need to report that configuration has changed?
5638 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 while (true) {
5641 long curTime = SystemClock.uptimeMillis();
5642
5643 if (DEBUG_INPUT) Log.v(
5644 TAG, "Waiting for next key: now=" + curTime
5645 + ", repeat @ " + nextKeyTime);
5646
5647 // Retrieve next event, waiting only as long as the next
5648 // repeat timeout. If the configuration has changed, then
5649 // don't wait at all -- we'll report the change as soon as
5650 // we have processed all events.
5651 QueuedEvent ev = mQueue.getEvent(
5652 (int)((!configChanged && curTime < nextKeyTime)
5653 ? (nextKeyTime-curTime) : 0));
5654
5655 if (DEBUG_INPUT && ev != null) Log.v(
5656 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5657
Michael Chan53071d62009-05-13 17:29:48 -07005658 if (MEASURE_LATENCY) {
5659 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5660 }
5661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005662 try {
5663 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005664 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 int eventType;
5666 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5667 eventType = eventType((MotionEvent)ev.event);
5668 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5669 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5670 eventType = LocalPowerManager.BUTTON_EVENT;
5671 } else {
5672 eventType = LocalPowerManager.OTHER_EVENT;
5673 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005674 try {
Michael Chan53071d62009-05-13 17:29:48 -07005675 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005676 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005677 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005678 mBatteryStats.noteInputEvent();
5679 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005680 } catch (RemoteException e) {
5681 // Ignore
5682 }
Michael Chane10de972009-05-18 11:24:50 -07005683
5684 if (eventType != TOUCH_EVENT
5685 && eventType != LONG_TOUCH_EVENT
5686 && eventType != CHEEK_EVENT) {
5687 mPowerManager.userActivity(curTime, false,
5688 eventType, false);
5689 } else if (mLastTouchEventType != eventType
5690 || (curTime - mLastUserActivityCallTime)
5691 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5692 mLastUserActivityCallTime = curTime;
5693 mLastTouchEventType = eventType;
5694 mPowerManager.userActivity(curTime, false,
5695 eventType, false);
5696 }
5697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005698 switch (ev.classType) {
5699 case RawInputEvent.CLASS_KEYBOARD:
5700 KeyEvent ke = (KeyEvent)ev.event;
5701 if (ke.isDown()) {
5702 lastKey = ke;
5703 keyRepeatCount = 0;
5704 lastKeyTime = curTime;
5705 nextKeyTime = lastKeyTime
5706 + KEY_REPEAT_FIRST_DELAY;
5707 if (DEBUG_INPUT) Log.v(
5708 TAG, "Received key down: first repeat @ "
5709 + nextKeyTime);
5710 } else {
5711 lastKey = null;
5712 // Arbitrary long timeout.
5713 lastKeyTime = curTime;
5714 nextKeyTime = curTime + LONG_WAIT;
5715 if (DEBUG_INPUT) Log.v(
5716 TAG, "Received key up: ignore repeat @ "
5717 + nextKeyTime);
5718 }
5719 dispatchKey((KeyEvent)ev.event, 0, 0);
5720 mQueue.recycleEvent(ev);
5721 break;
5722 case RawInputEvent.CLASS_TOUCHSCREEN:
5723 //Log.i(TAG, "Read next event " + ev);
5724 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5725 break;
5726 case RawInputEvent.CLASS_TRACKBALL:
5727 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5728 break;
5729 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5730 configChanged = true;
5731 break;
5732 default:
5733 mQueue.recycleEvent(ev);
5734 break;
5735 }
Romain Guy06882f82009-06-10 13:36:04 -07005736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 } else if (configChanged) {
5738 configChanged = false;
5739 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 } else if (lastKey != null) {
5742 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 // Timeout occurred while key was down. If it is at or
5745 // past the key repeat time, dispatch the repeat.
5746 if (DEBUG_INPUT) Log.v(
5747 TAG, "Key timeout: repeat=" + nextKeyTime
5748 + ", now=" + curTime);
5749 if (curTime < nextKeyTime) {
5750 continue;
5751 }
Romain Guy06882f82009-06-10 13:36:04 -07005752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005753 lastKeyTime = nextKeyTime;
5754 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5755 keyRepeatCount++;
5756 if (DEBUG_INPUT) Log.v(
5757 TAG, "Key repeat: count=" + keyRepeatCount
5758 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005759 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 } else {
5762 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 lastKeyTime = curTime;
5765 nextKeyTime = curTime + LONG_WAIT;
5766 }
Romain Guy06882f82009-06-10 13:36:04 -07005767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005768 } catch (Exception e) {
5769 Log.e(TAG,
5770 "Input thread received uncaught exception: " + e, e);
5771 }
5772 }
5773 }
5774 }
5775
5776 // -------------------------------------------------------------
5777 // Client Session State
5778 // -------------------------------------------------------------
5779
5780 private final class Session extends IWindowSession.Stub
5781 implements IBinder.DeathRecipient {
5782 final IInputMethodClient mClient;
5783 final IInputContext mInputContext;
5784 final int mUid;
5785 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005786 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 SurfaceSession mSurfaceSession;
5788 int mNumWindow = 0;
5789 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005791 /**
5792 * Current pointer move event being dispatched to client window... must
5793 * hold key lock to access.
5794 */
5795 QueuedEvent mPendingPointerMove;
5796 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 /**
5799 * Current trackball move event being dispatched to client window... must
5800 * hold key lock to access.
5801 */
5802 QueuedEvent mPendingTrackballMove;
5803 WindowState mPendingTrackballWindow;
5804
5805 public Session(IInputMethodClient client, IInputContext inputContext) {
5806 mClient = client;
5807 mInputContext = inputContext;
5808 mUid = Binder.getCallingUid();
5809 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005810 StringBuilder sb = new StringBuilder();
5811 sb.append("Session{");
5812 sb.append(Integer.toHexString(System.identityHashCode(this)));
5813 sb.append(" uid ");
5814 sb.append(mUid);
5815 sb.append("}");
5816 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 synchronized (mWindowMap) {
5819 if (mInputMethodManager == null && mHaveInputMethods) {
5820 IBinder b = ServiceManager.getService(
5821 Context.INPUT_METHOD_SERVICE);
5822 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5823 }
5824 }
5825 long ident = Binder.clearCallingIdentity();
5826 try {
5827 // Note: it is safe to call in to the input method manager
5828 // here because we are not holding our lock.
5829 if (mInputMethodManager != null) {
5830 mInputMethodManager.addClient(client, inputContext,
5831 mUid, mPid);
5832 } else {
5833 client.setUsingInputMethod(false);
5834 }
5835 client.asBinder().linkToDeath(this, 0);
5836 } catch (RemoteException e) {
5837 // The caller has died, so we can just forget about this.
5838 try {
5839 if (mInputMethodManager != null) {
5840 mInputMethodManager.removeClient(client);
5841 }
5842 } catch (RemoteException ee) {
5843 }
5844 } finally {
5845 Binder.restoreCallingIdentity(ident);
5846 }
5847 }
Romain Guy06882f82009-06-10 13:36:04 -07005848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 @Override
5850 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5851 throws RemoteException {
5852 try {
5853 return super.onTransact(code, data, reply, flags);
5854 } catch (RuntimeException e) {
5855 // Log all 'real' exceptions thrown to the caller
5856 if (!(e instanceof SecurityException)) {
5857 Log.e(TAG, "Window Session Crash", e);
5858 }
5859 throw e;
5860 }
5861 }
5862
5863 public void binderDied() {
5864 // Note: it is safe to call in to the input method manager
5865 // here because we are not holding our lock.
5866 try {
5867 if (mInputMethodManager != null) {
5868 mInputMethodManager.removeClient(mClient);
5869 }
5870 } catch (RemoteException e) {
5871 }
5872 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005873 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 mClientDead = true;
5875 killSessionLocked();
5876 }
5877 }
5878
5879 public int add(IWindow window, WindowManager.LayoutParams attrs,
5880 int viewVisibility, Rect outContentInsets) {
5881 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5882 }
Romain Guy06882f82009-06-10 13:36:04 -07005883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 public void remove(IWindow window) {
5885 removeWindow(this, window);
5886 }
Romain Guy06882f82009-06-10 13:36:04 -07005887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5889 int requestedWidth, int requestedHeight, int viewFlags,
5890 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5891 Rect outVisibleInsets, Surface outSurface) {
5892 return relayoutWindow(this, window, attrs,
5893 requestedWidth, requestedHeight, viewFlags, insetsPending,
5894 outFrame, outContentInsets, outVisibleInsets, outSurface);
5895 }
Romain Guy06882f82009-06-10 13:36:04 -07005896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 public void setTransparentRegion(IWindow window, Region region) {
5898 setTransparentRegionWindow(this, window, region);
5899 }
Romain Guy06882f82009-06-10 13:36:04 -07005900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005901 public void setInsets(IWindow window, int touchableInsets,
5902 Rect contentInsets, Rect visibleInsets) {
5903 setInsetsWindow(this, window, touchableInsets, contentInsets,
5904 visibleInsets);
5905 }
Romain Guy06882f82009-06-10 13:36:04 -07005906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5908 getWindowDisplayFrame(this, window, outDisplayFrame);
5909 }
Romain Guy06882f82009-06-10 13:36:04 -07005910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 public void finishDrawing(IWindow window) {
5912 if (localLOGV) Log.v(
5913 TAG, "IWindow finishDrawing called for " + window);
5914 finishDrawingWindow(this, window);
5915 }
5916
5917 public void finishKey(IWindow window) {
5918 if (localLOGV) Log.v(
5919 TAG, "IWindow finishKey called for " + window);
5920 mKeyWaiter.finishedKey(this, window, false,
5921 KeyWaiter.RETURN_NOTHING);
5922 }
5923
5924 public MotionEvent getPendingPointerMove(IWindow window) {
5925 if (localLOGV) Log.v(
5926 TAG, "IWindow getPendingMotionEvent called for " + window);
5927 return mKeyWaiter.finishedKey(this, window, false,
5928 KeyWaiter.RETURN_PENDING_POINTER);
5929 }
Romain Guy06882f82009-06-10 13:36:04 -07005930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 public MotionEvent getPendingTrackballMove(IWindow window) {
5932 if (localLOGV) Log.v(
5933 TAG, "IWindow getPendingMotionEvent called for " + window);
5934 return mKeyWaiter.finishedKey(this, window, false,
5935 KeyWaiter.RETURN_PENDING_TRACKBALL);
5936 }
5937
5938 public void setInTouchMode(boolean mode) {
5939 synchronized(mWindowMap) {
5940 mInTouchMode = mode;
5941 }
5942 }
5943
5944 public boolean getInTouchMode() {
5945 synchronized(mWindowMap) {
5946 return mInTouchMode;
5947 }
5948 }
5949
5950 public boolean performHapticFeedback(IWindow window, int effectId,
5951 boolean always) {
5952 synchronized(mWindowMap) {
5953 long ident = Binder.clearCallingIdentity();
5954 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005955 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956 windowForClientLocked(this, window), effectId, always);
5957 } finally {
5958 Binder.restoreCallingIdentity(ident);
5959 }
5960 }
5961 }
Romain Guy06882f82009-06-10 13:36:04 -07005962
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07005963 public void setWallpaperPosition(IBinder window, float x, float y) {
5964 synchronized(mWindowMap) {
5965 long ident = Binder.clearCallingIdentity();
5966 try {
5967 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
5968 x, y);
5969 } finally {
5970 Binder.restoreCallingIdentity(ident);
5971 }
5972 }
5973 }
5974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 void windowAddedLocked() {
5976 if (mSurfaceSession == null) {
5977 if (localLOGV) Log.v(
5978 TAG, "First window added to " + this + ", creating SurfaceSession");
5979 mSurfaceSession = new SurfaceSession();
5980 mSessions.add(this);
5981 }
5982 mNumWindow++;
5983 }
5984
5985 void windowRemovedLocked() {
5986 mNumWindow--;
5987 killSessionLocked();
5988 }
Romain Guy06882f82009-06-10 13:36:04 -07005989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 void killSessionLocked() {
5991 if (mNumWindow <= 0 && mClientDead) {
5992 mSessions.remove(this);
5993 if (mSurfaceSession != null) {
5994 if (localLOGV) Log.v(
5995 TAG, "Last window removed from " + this
5996 + ", destroying " + mSurfaceSession);
5997 try {
5998 mSurfaceSession.kill();
5999 } catch (Exception e) {
6000 Log.w(TAG, "Exception thrown when killing surface session "
6001 + mSurfaceSession + " in session " + this
6002 + ": " + e.toString());
6003 }
6004 mSurfaceSession = null;
6005 }
6006 }
6007 }
Romain Guy06882f82009-06-10 13:36:04 -07006008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006010 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6011 pw.print(" mClientDead="); pw.print(mClientDead);
6012 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6013 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6014 pw.print(prefix);
6015 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6016 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6017 }
6018 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6019 pw.print(prefix);
6020 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6021 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 }
6024
6025 @Override
6026 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006027 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 }
6029 }
6030
6031 // -------------------------------------------------------------
6032 // Client Window State
6033 // -------------------------------------------------------------
6034
6035 private final class WindowState implements WindowManagerPolicy.WindowState {
6036 final Session mSession;
6037 final IWindow mClient;
6038 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006039 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006040 AppWindowToken mAppToken;
6041 AppWindowToken mTargetAppToken;
6042 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6043 final DeathRecipient mDeathRecipient;
6044 final WindowState mAttachedWindow;
6045 final ArrayList mChildWindows = new ArrayList();
6046 final int mBaseLayer;
6047 final int mSubLayer;
6048 final boolean mLayoutAttached;
6049 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006050 final boolean mIsWallpaper;
6051 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052 int mViewVisibility;
6053 boolean mPolicyVisibility = true;
6054 boolean mPolicyVisibilityAfterAnim = true;
6055 boolean mAppFreezing;
6056 Surface mSurface;
6057 boolean mAttachedHidden; // is our parent window hidden?
6058 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006059 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006060 int mRequestedWidth;
6061 int mRequestedHeight;
6062 int mLastRequestedWidth;
6063 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006064 int mLayer;
6065 int mAnimLayer;
6066 int mLastLayer;
6067 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006068 boolean mObscured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006069
6070 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 // Actual frame shown on-screen (may be modified by animation)
6073 final Rect mShownFrame = new Rect();
6074 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 /**
6077 * Insets that determine the actually visible area
6078 */
6079 final Rect mVisibleInsets = new Rect();
6080 final Rect mLastVisibleInsets = new Rect();
6081 boolean mVisibleInsetsChanged;
6082
6083 /**
6084 * Insets that are covered by system windows
6085 */
6086 final Rect mContentInsets = new Rect();
6087 final Rect mLastContentInsets = new Rect();
6088 boolean mContentInsetsChanged;
6089
6090 /**
6091 * Set to true if we are waiting for this window to receive its
6092 * given internal insets before laying out other windows based on it.
6093 */
6094 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006096 /**
6097 * These are the content insets that were given during layout for
6098 * this window, to be applied to windows behind it.
6099 */
6100 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 /**
6103 * These are the visible insets that were given during layout for
6104 * this window, to be applied to windows behind it.
6105 */
6106 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 /**
6109 * Flag indicating whether the touchable region should be adjusted by
6110 * the visible insets; if false the area outside the visible insets is
6111 * NOT touchable, so we must use those to adjust the frame during hit
6112 * tests.
6113 */
6114 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 // Current transformation being applied.
6117 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6118 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6119 float mHScale=1, mVScale=1;
6120 float mLastHScale=1, mLastVScale=1;
6121 final Matrix mTmpMatrix = new Matrix();
6122
6123 // "Real" frame that the application sees.
6124 final Rect mFrame = new Rect();
6125 final Rect mLastFrame = new Rect();
6126
6127 final Rect mContainingFrame = new Rect();
6128 final Rect mDisplayFrame = new Rect();
6129 final Rect mContentFrame = new Rect();
6130 final Rect mVisibleFrame = new Rect();
6131
6132 float mShownAlpha = 1;
6133 float mAlpha = 1;
6134 float mLastAlpha = 1;
6135
6136 // Set to true if, when the window gets displayed, it should perform
6137 // an enter animation.
6138 boolean mEnterAnimationPending;
6139
6140 // Currently running animation.
6141 boolean mAnimating;
6142 boolean mLocalAnimating;
6143 Animation mAnimation;
6144 boolean mAnimationIsEntrance;
6145 boolean mHasTransformation;
6146 boolean mHasLocalTransformation;
6147 final Transformation mTransformation = new Transformation();
6148
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006149 // If a window showing a wallpaper: the requested offset for the
6150 // wallpaper; if a wallpaper window: the currently applied offset.
6151 float mWallpaperX = -1;
6152 float mWallpaperY = -1;
6153
6154 // Wallpaper windows: pixels offset based on above variables.
6155 int mXOffset;
6156 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006158 // This is set after IWindowSession.relayout() has been called at
6159 // least once for the window. It allows us to detect the situation
6160 // where we don't yet have a surface, but should have one soon, so
6161 // we can give the window focus before waiting for the relayout.
6162 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 // This is set after the Surface has been created but before the
6165 // window has been drawn. During this time the surface is hidden.
6166 boolean mDrawPending;
6167
6168 // This is set after the window has finished drawing for the first
6169 // time but before its surface is shown. The surface will be
6170 // displayed when the next layout is run.
6171 boolean mCommitDrawPending;
6172
6173 // This is set during the time after the window's drawing has been
6174 // committed, and before its surface is actually shown. It is used
6175 // to delay showing the surface until all windows in a token are ready
6176 // to be shown.
6177 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 // Set when the window has been shown in the screen the first time.
6180 boolean mHasDrawn;
6181
6182 // Currently running an exit animation?
6183 boolean mExiting;
6184
6185 // Currently on the mDestroySurface list?
6186 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 // Completely remove from window manager after exit animation?
6189 boolean mRemoveOnExit;
6190
6191 // Set when the orientation is changing and this window has not yet
6192 // been updated for the new orientation.
6193 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195 // Is this window now (or just being) removed?
6196 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006198 WindowState(Session s, IWindow c, WindowToken token,
6199 WindowState attachedWindow, WindowManager.LayoutParams a,
6200 int viewVisibility) {
6201 mSession = s;
6202 mClient = c;
6203 mToken = token;
6204 mAttrs.copyFrom(a);
6205 mViewVisibility = viewVisibility;
6206 DeathRecipient deathRecipient = new DeathRecipient();
6207 mAlpha = a.alpha;
6208 if (localLOGV) Log.v(
6209 TAG, "Window " + this + " client=" + c.asBinder()
6210 + " token=" + token + " (" + mAttrs.token + ")");
6211 try {
6212 c.asBinder().linkToDeath(deathRecipient, 0);
6213 } catch (RemoteException e) {
6214 mDeathRecipient = null;
6215 mAttachedWindow = null;
6216 mLayoutAttached = false;
6217 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006218 mIsWallpaper = false;
6219 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006220 mBaseLayer = 0;
6221 mSubLayer = 0;
6222 return;
6223 }
6224 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006226 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6227 mAttrs.type <= LAST_SUB_WINDOW)) {
6228 // The multiplier here is to reserve space for multiple
6229 // windows in the same type layer.
6230 mBaseLayer = mPolicy.windowTypeToLayerLw(
6231 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6232 + TYPE_LAYER_OFFSET;
6233 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6234 mAttachedWindow = attachedWindow;
6235 mAttachedWindow.mChildWindows.add(this);
6236 mLayoutAttached = mAttrs.type !=
6237 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6238 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6239 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006240 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6241 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006242 } else {
6243 // The multiplier here is to reserve space for multiple
6244 // windows in the same type layer.
6245 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6246 * TYPE_LAYER_MULTIPLIER
6247 + TYPE_LAYER_OFFSET;
6248 mSubLayer = 0;
6249 mAttachedWindow = null;
6250 mLayoutAttached = false;
6251 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6252 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006253 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6254 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 }
6256
6257 WindowState appWin = this;
6258 while (appWin.mAttachedWindow != null) {
6259 appWin = mAttachedWindow;
6260 }
6261 WindowToken appToken = appWin.mToken;
6262 while (appToken.appWindowToken == null) {
6263 WindowToken parent = mTokenMap.get(appToken.token);
6264 if (parent == null || appToken == parent) {
6265 break;
6266 }
6267 appToken = parent;
6268 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006269 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006270 mAppToken = appToken.appWindowToken;
6271
6272 mSurface = null;
6273 mRequestedWidth = 0;
6274 mRequestedHeight = 0;
6275 mLastRequestedWidth = 0;
6276 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006277 mXOffset = 0;
6278 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 mLayer = 0;
6280 mAnimLayer = 0;
6281 mLastLayer = 0;
6282 }
6283
6284 void attach() {
6285 if (localLOGV) Log.v(
6286 TAG, "Attaching " + this + " token=" + mToken
6287 + ", list=" + mToken.windows);
6288 mSession.windowAddedLocked();
6289 }
6290
6291 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6292 mHaveFrame = true;
6293
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006294 final Rect container = mContainingFrame;
6295 container.set(pf);
6296
6297 final Rect display = mDisplayFrame;
6298 display.set(df);
6299
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006300 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006301 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006302 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6303 display.intersect(mCompatibleScreenFrame);
6304 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006305 }
6306
6307 final int pw = container.right - container.left;
6308 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309
6310 int w,h;
6311 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6312 w = mAttrs.width < 0 ? pw : mAttrs.width;
6313 h = mAttrs.height< 0 ? ph : mAttrs.height;
6314 } else {
6315 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6316 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6317 }
Romain Guy06882f82009-06-10 13:36:04 -07006318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 final Rect content = mContentFrame;
6320 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 final Rect visible = mVisibleFrame;
6323 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006325 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07006326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6328 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6329
6330 Gravity.apply(mAttrs.gravity, w, h, container,
6331 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6332 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6333
6334 //System.out.println("Out: " + mFrame);
6335
6336 // Now make sure the window fits in the overall display.
6337 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 // Make sure the content and visible frames are inside of the
6340 // final window frame.
6341 if (content.left < frame.left) content.left = frame.left;
6342 if (content.top < frame.top) content.top = frame.top;
6343 if (content.right > frame.right) content.right = frame.right;
6344 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6345 if (visible.left < frame.left) visible.left = frame.left;
6346 if (visible.top < frame.top) visible.top = frame.top;
6347 if (visible.right > frame.right) visible.right = frame.right;
6348 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 final Rect contentInsets = mContentInsets;
6351 contentInsets.left = content.left-frame.left;
6352 contentInsets.top = content.top-frame.top;
6353 contentInsets.right = frame.right-content.right;
6354 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 final Rect visibleInsets = mVisibleInsets;
6357 visibleInsets.left = visible.left-frame.left;
6358 visibleInsets.top = visible.top-frame.top;
6359 visibleInsets.right = frame.right-visible.right;
6360 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006362 if (localLOGV) {
6363 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6364 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6365 Log.v(TAG, "Resolving (mRequestedWidth="
6366 + mRequestedWidth + ", mRequestedheight="
6367 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6368 + "): frame=" + mFrame.toShortString()
6369 + " ci=" + contentInsets.toShortString()
6370 + " vi=" + visibleInsets.toShortString());
6371 //}
6372 }
6373 }
Romain Guy06882f82009-06-10 13:36:04 -07006374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006375 public Rect getFrameLw() {
6376 return mFrame;
6377 }
6378
6379 public Rect getShownFrameLw() {
6380 return mShownFrame;
6381 }
6382
6383 public Rect getDisplayFrameLw() {
6384 return mDisplayFrame;
6385 }
6386
6387 public Rect getContentFrameLw() {
6388 return mContentFrame;
6389 }
6390
6391 public Rect getVisibleFrameLw() {
6392 return mVisibleFrame;
6393 }
6394
6395 public boolean getGivenInsetsPendingLw() {
6396 return mGivenInsetsPending;
6397 }
6398
6399 public Rect getGivenContentInsetsLw() {
6400 return mGivenContentInsets;
6401 }
Romain Guy06882f82009-06-10 13:36:04 -07006402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 public Rect getGivenVisibleInsetsLw() {
6404 return mGivenVisibleInsets;
6405 }
Romain Guy06882f82009-06-10 13:36:04 -07006406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 public WindowManager.LayoutParams getAttrs() {
6408 return mAttrs;
6409 }
6410
6411 public int getSurfaceLayer() {
6412 return mLayer;
6413 }
Romain Guy06882f82009-06-10 13:36:04 -07006414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006415 public IApplicationToken getAppToken() {
6416 return mAppToken != null ? mAppToken.appToken : null;
6417 }
6418
6419 public boolean hasAppShownWindows() {
6420 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6421 }
6422
6423 public boolean hasAppStartingIcon() {
6424 return mAppToken != null ? (mAppToken.startingData != null) : false;
6425 }
6426
6427 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6428 return mAppToken != null ? mAppToken.startingWindow : null;
6429 }
6430
6431 public void setAnimation(Animation anim) {
6432 if (localLOGV) Log.v(
6433 TAG, "Setting animation in " + this + ": " + anim);
6434 mAnimating = false;
6435 mLocalAnimating = false;
6436 mAnimation = anim;
6437 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6438 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6439 }
6440
6441 public void clearAnimation() {
6442 if (mAnimation != null) {
6443 mAnimating = true;
6444 mLocalAnimating = false;
6445 mAnimation = null;
6446 }
6447 }
Romain Guy06882f82009-06-10 13:36:04 -07006448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006449 Surface createSurfaceLocked() {
6450 if (mSurface == null) {
6451 mDrawPending = true;
6452 mCommitDrawPending = false;
6453 mReadyToShow = false;
6454 if (mAppToken != null) {
6455 mAppToken.allDrawn = false;
6456 }
6457
6458 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006459 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006460 flags |= Surface.PUSH_BUFFERS;
6461 }
6462
6463 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6464 flags |= Surface.SECURE;
6465 }
6466 if (DEBUG_VISIBILITY) Log.v(
6467 TAG, "Creating surface in session "
6468 + mSession.mSurfaceSession + " window " + this
6469 + " w=" + mFrame.width()
6470 + " h=" + mFrame.height() + " format="
6471 + mAttrs.format + " flags=" + flags);
6472
6473 int w = mFrame.width();
6474 int h = mFrame.height();
6475 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6476 // for a scaled surface, we always want the requested
6477 // size.
6478 w = mRequestedWidth;
6479 h = mRequestedHeight;
6480 }
6481
6482 try {
6483 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006484 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006485 0, w, h, mAttrs.format, flags);
6486 } catch (Surface.OutOfResourcesException e) {
6487 Log.w(TAG, "OutOfResourcesException creating surface");
6488 reclaimSomeSurfaceMemoryLocked(this, "create");
6489 return null;
6490 } catch (Exception e) {
6491 Log.e(TAG, "Exception creating surface", e);
6492 return null;
6493 }
Romain Guy06882f82009-06-10 13:36:04 -07006494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 if (localLOGV) Log.v(
6496 TAG, "Got surface: " + mSurface
6497 + ", set left=" + mFrame.left + " top=" + mFrame.top
6498 + ", animLayer=" + mAnimLayer);
6499 if (SHOW_TRANSACTIONS) {
6500 Log.i(TAG, ">>> OPEN TRANSACTION");
6501 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6502 + mAttrs.getTitle() + ") pos=(" +
6503 mFrame.left + "," + mFrame.top + ") (" +
6504 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6505 mAnimLayer + " HIDE");
6506 }
6507 Surface.openTransaction();
6508 try {
6509 try {
6510 mSurface.setPosition(mFrame.left, mFrame.top);
6511 mSurface.setLayer(mAnimLayer);
6512 mSurface.hide();
6513 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6514 mSurface.setFlags(Surface.SURFACE_DITHER,
6515 Surface.SURFACE_DITHER);
6516 }
6517 } catch (RuntimeException e) {
6518 Log.w(TAG, "Error creating surface in " + w, e);
6519 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6520 }
6521 mLastHidden = true;
6522 } finally {
6523 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6524 Surface.closeTransaction();
6525 }
6526 if (localLOGV) Log.v(
6527 TAG, "Created surface " + this);
6528 }
6529 return mSurface;
6530 }
Romain Guy06882f82009-06-10 13:36:04 -07006531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006532 void destroySurfaceLocked() {
6533 // Window is no longer on-screen, so can no longer receive
6534 // key events... if we were waiting for it to finish
6535 // handling a key event, the wait is over!
6536 mKeyWaiter.finishedKey(mSession, mClient, true,
6537 KeyWaiter.RETURN_NOTHING);
6538 mKeyWaiter.releasePendingPointerLocked(mSession);
6539 mKeyWaiter.releasePendingTrackballLocked(mSession);
6540
6541 if (mAppToken != null && this == mAppToken.startingWindow) {
6542 mAppToken.startingDisplayed = false;
6543 }
Romain Guy06882f82009-06-10 13:36:04 -07006544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006545 if (localLOGV) Log.v(
6546 TAG, "Window " + this
6547 + " destroying surface " + mSurface + ", session " + mSession);
6548 if (mSurface != null) {
6549 try {
6550 if (SHOW_TRANSACTIONS) {
6551 RuntimeException ex = new RuntimeException();
6552 ex.fillInStackTrace();
6553 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6554 + mAttrs.getTitle() + ")", ex);
6555 }
6556 mSurface.clear();
6557 } catch (RuntimeException e) {
6558 Log.w(TAG, "Exception thrown when destroying Window " + this
6559 + " surface " + mSurface + " session " + mSession
6560 + ": " + e.toString());
6561 }
6562 mSurface = null;
6563 mDrawPending = false;
6564 mCommitDrawPending = false;
6565 mReadyToShow = false;
6566
6567 int i = mChildWindows.size();
6568 while (i > 0) {
6569 i--;
6570 WindowState c = (WindowState)mChildWindows.get(i);
6571 c.mAttachedHidden = true;
6572 }
6573 }
6574 }
6575
6576 boolean finishDrawingLocked() {
6577 if (mDrawPending) {
6578 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6579 TAG, "finishDrawingLocked: " + mSurface);
6580 mCommitDrawPending = true;
6581 mDrawPending = false;
6582 return true;
6583 }
6584 return false;
6585 }
6586
6587 // This must be called while inside a transaction.
6588 void commitFinishDrawingLocked(long currentTime) {
6589 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6590 if (!mCommitDrawPending) {
6591 return;
6592 }
6593 mCommitDrawPending = false;
6594 mReadyToShow = true;
6595 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6596 final AppWindowToken atoken = mAppToken;
6597 if (atoken == null || atoken.allDrawn || starting) {
6598 performShowLocked();
6599 }
6600 }
6601
6602 // This must be called while inside a transaction.
6603 boolean performShowLocked() {
6604 if (DEBUG_VISIBILITY) {
6605 RuntimeException e = new RuntimeException();
6606 e.fillInStackTrace();
6607 Log.v(TAG, "performShow on " + this
6608 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6609 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6610 }
6611 if (mReadyToShow && isReadyForDisplay()) {
6612 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6613 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6614 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6615 + " during animation: policyVis=" + mPolicyVisibility
6616 + " attHidden=" + mAttachedHidden
6617 + " tok.hiddenRequested="
6618 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6619 + " tok.idden="
6620 + (mAppToken != null ? mAppToken.hidden : false)
6621 + " animating=" + mAnimating
6622 + " tok animating="
6623 + (mAppToken != null ? mAppToken.animating : false));
6624 if (!showSurfaceRobustlyLocked(this)) {
6625 return false;
6626 }
6627 mLastAlpha = -1;
6628 mHasDrawn = true;
6629 mLastHidden = false;
6630 mReadyToShow = false;
6631 enableScreenIfNeededLocked();
6632
6633 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006635 int i = mChildWindows.size();
6636 while (i > 0) {
6637 i--;
6638 WindowState c = (WindowState)mChildWindows.get(i);
6639 if (c.mSurface != null && c.mAttachedHidden) {
6640 c.mAttachedHidden = false;
6641 c.performShowLocked();
6642 }
6643 }
Romain Guy06882f82009-06-10 13:36:04 -07006644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 if (mAttrs.type != TYPE_APPLICATION_STARTING
6646 && mAppToken != null) {
6647 mAppToken.firstWindowDrawn = true;
6648 if (mAnimation == null && mAppToken.startingData != null) {
6649 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6650 + mToken
6651 + ": first real window is shown, no animation");
6652 mFinishedStarting.add(mAppToken);
6653 mH.sendEmptyMessage(H.FINISHED_STARTING);
6654 }
6655 mAppToken.updateReportedVisibilityLocked();
6656 }
6657 }
6658 return true;
6659 }
Romain Guy06882f82009-06-10 13:36:04 -07006660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 // This must be called while inside a transaction. Returns true if
6662 // there is more animation to run.
6663 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6664 if (!mDisplayFrozen) {
6665 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006667 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6668 mHasTransformation = true;
6669 mHasLocalTransformation = true;
6670 if (!mLocalAnimating) {
6671 if (DEBUG_ANIM) Log.v(
6672 TAG, "Starting animation in " + this +
6673 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6674 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6675 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6676 mAnimation.setStartTime(currentTime);
6677 mLocalAnimating = true;
6678 mAnimating = true;
6679 }
6680 mTransformation.clear();
6681 final boolean more = mAnimation.getTransformation(
6682 currentTime, mTransformation);
6683 if (DEBUG_ANIM) Log.v(
6684 TAG, "Stepped animation in " + this +
6685 ": more=" + more + ", xform=" + mTransformation);
6686 if (more) {
6687 // we're not done!
6688 return true;
6689 }
6690 if (DEBUG_ANIM) Log.v(
6691 TAG, "Finished animation in " + this +
6692 " @ " + currentTime);
6693 mAnimation = null;
6694 //WindowManagerService.this.dump();
6695 }
6696 mHasLocalTransformation = false;
6697 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6698 && mAppToken.hasTransformation) {
6699 // When our app token is animating, we kind-of pretend like
6700 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6701 // part of this check means that we will only do this if
6702 // our window is not currently exiting, or it is not
6703 // locally animating itself. The idea being that one that
6704 // is exiting and doing a local animation should be removed
6705 // once that animation is done.
6706 mAnimating = true;
6707 mHasTransformation = true;
6708 mTransformation.clear();
6709 return false;
6710 } else if (mHasTransformation) {
6711 // Little trick to get through the path below to act like
6712 // we have finished an animation.
6713 mAnimating = true;
6714 } else if (isAnimating()) {
6715 mAnimating = true;
6716 }
6717 } else if (mAnimation != null) {
6718 // If the display is frozen, and there is a pending animation,
6719 // clear it and make sure we run the cleanup code.
6720 mAnimating = true;
6721 mLocalAnimating = true;
6722 mAnimation = null;
6723 }
Romain Guy06882f82009-06-10 13:36:04 -07006724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006725 if (!mAnimating && !mLocalAnimating) {
6726 return false;
6727 }
6728
6729 if (DEBUG_ANIM) Log.v(
6730 TAG, "Animation done in " + this + ": exiting=" + mExiting
6731 + ", reportedVisible="
6732 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006734 mAnimating = false;
6735 mLocalAnimating = false;
6736 mAnimation = null;
6737 mAnimLayer = mLayer;
6738 if (mIsImWindow) {
6739 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006740 } else if (mIsWallpaper) {
6741 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 }
6743 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6744 + " anim layer: " + mAnimLayer);
6745 mHasTransformation = false;
6746 mHasLocalTransformation = false;
6747 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6748 mTransformation.clear();
6749 if (mHasDrawn
6750 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6751 && mAppToken != null
6752 && mAppToken.firstWindowDrawn
6753 && mAppToken.startingData != null) {
6754 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6755 + mToken + ": first real window done animating");
6756 mFinishedStarting.add(mAppToken);
6757 mH.sendEmptyMessage(H.FINISHED_STARTING);
6758 }
Romain Guy06882f82009-06-10 13:36:04 -07006759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006760 finishExit();
6761
6762 if (mAppToken != null) {
6763 mAppToken.updateReportedVisibilityLocked();
6764 }
6765
6766 return false;
6767 }
6768
6769 void finishExit() {
6770 if (DEBUG_ANIM) Log.v(
6771 TAG, "finishExit in " + this
6772 + ": exiting=" + mExiting
6773 + " remove=" + mRemoveOnExit
6774 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006776 final int N = mChildWindows.size();
6777 for (int i=0; i<N; i++) {
6778 ((WindowState)mChildWindows.get(i)).finishExit();
6779 }
Romain Guy06882f82009-06-10 13:36:04 -07006780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 if (!mExiting) {
6782 return;
6783 }
Romain Guy06882f82009-06-10 13:36:04 -07006784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006785 if (isWindowAnimating()) {
6786 return;
6787 }
6788
6789 if (localLOGV) Log.v(
6790 TAG, "Exit animation finished in " + this
6791 + ": remove=" + mRemoveOnExit);
6792 if (mSurface != null) {
6793 mDestroySurface.add(this);
6794 mDestroying = true;
6795 if (SHOW_TRANSACTIONS) Log.i(
6796 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6797 try {
6798 mSurface.hide();
6799 } catch (RuntimeException e) {
6800 Log.w(TAG, "Error hiding surface in " + this, e);
6801 }
6802 mLastHidden = true;
6803 mKeyWaiter.releasePendingPointerLocked(mSession);
6804 }
6805 mExiting = false;
6806 if (mRemoveOnExit) {
6807 mPendingRemove.add(this);
6808 mRemoveOnExit = false;
6809 }
6810 }
Romain Guy06882f82009-06-10 13:36:04 -07006811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6813 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6814 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6815 if (dtdx < -.000001f || dtdx > .000001f) return false;
6816 if (dsdy < -.000001f || dsdy > .000001f) return false;
6817 return true;
6818 }
Romain Guy06882f82009-06-10 13:36:04 -07006819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 void computeShownFrameLocked() {
6821 final boolean selfTransformation = mHasLocalTransformation;
6822 Transformation attachedTransformation =
6823 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6824 ? mAttachedWindow.mTransformation : null;
6825 Transformation appTransformation =
6826 (mAppToken != null && mAppToken.hasTransformation)
6827 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006828
6829 // Wallpapers are animated based on the "real" window they
6830 // are currently targeting.
6831 if (mAttrs.type == TYPE_WALLPAPER && mWallpaperTarget != null) {
6832 if (mWallpaperTarget.mHasLocalTransformation) {
6833 attachedTransformation = mWallpaperTarget.mTransformation;
6834 }
6835 if (mWallpaperTarget.mAppToken != null &&
6836 mWallpaperTarget.mAppToken.hasTransformation) {
6837 appTransformation = mWallpaperTarget.mAppToken.transformation;
6838 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006839 if (DEBUG_WALLPAPER && attachedTransformation != null) {
6840 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
6841 }
6842 if (DEBUG_WALLPAPER && appTransformation != null) {
6843 Log.v(TAG, "WP target app xform: " + appTransformation);
6844 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006845 }
6846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847 if (selfTransformation || attachedTransformation != null
6848 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006849 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006850 final Rect frame = mFrame;
6851 final float tmpFloats[] = mTmpFloats;
6852 final Matrix tmpMatrix = mTmpMatrix;
6853
6854 // Compute the desired transformation.
6855 tmpMatrix.setTranslate(frame.left, frame.top);
6856 if (selfTransformation) {
6857 tmpMatrix.preConcat(mTransformation.getMatrix());
6858 }
6859 if (attachedTransformation != null) {
6860 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6861 }
6862 if (appTransformation != null) {
6863 tmpMatrix.preConcat(appTransformation.getMatrix());
6864 }
6865
6866 // "convert" it into SurfaceFlinger's format
6867 // (a 2x2 matrix + an offset)
6868 // Here we must not transform the position of the surface
6869 // since it is already included in the transformation.
6870 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006872 tmpMatrix.getValues(tmpFloats);
6873 mDsDx = tmpFloats[Matrix.MSCALE_X];
6874 mDtDx = tmpFloats[Matrix.MSKEW_X];
6875 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6876 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006877 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
6878 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879 int w = frame.width();
6880 int h = frame.height();
6881 mShownFrame.set(x, y, x+w, y+h);
6882
6883 // Now set the alpha... but because our current hardware
6884 // can't do alpha transformation on a non-opaque surface,
6885 // turn it off if we are running an animation that is also
6886 // transforming since it is more important to have that
6887 // animation be smooth.
6888 mShownAlpha = mAlpha;
6889 if (!mLimitedAlphaCompositing
6890 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6891 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6892 && x == frame.left && y == frame.top))) {
6893 //Log.i(TAG, "Applying alpha transform");
6894 if (selfTransformation) {
6895 mShownAlpha *= mTransformation.getAlpha();
6896 }
6897 if (attachedTransformation != null) {
6898 mShownAlpha *= attachedTransformation.getAlpha();
6899 }
6900 if (appTransformation != null) {
6901 mShownAlpha *= appTransformation.getAlpha();
6902 }
6903 } else {
6904 //Log.i(TAG, "Not applying alpha transform");
6905 }
Romain Guy06882f82009-06-10 13:36:04 -07006906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 if (localLOGV) Log.v(
6908 TAG, "Continuing animation in " + this +
6909 ": " + mShownFrame +
6910 ", alpha=" + mTransformation.getAlpha());
6911 return;
6912 }
Romain Guy06882f82009-06-10 13:36:04 -07006913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006914 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006915 if (mXOffset != 0 || mYOffset != 0) {
6916 mShownFrame.offset(mXOffset, mYOffset);
6917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918 mShownAlpha = mAlpha;
6919 mDsDx = 1;
6920 mDtDx = 0;
6921 mDsDy = 0;
6922 mDtDy = 1;
6923 }
Romain Guy06882f82009-06-10 13:36:04 -07006924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 /**
6926 * Is this window visible? It is not visible if there is no
6927 * surface, or we are in the process of running an exit animation
6928 * that will remove the surface, or its app token has been hidden.
6929 */
6930 public boolean isVisibleLw() {
6931 final AppWindowToken atoken = mAppToken;
6932 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6933 && (atoken == null || !atoken.hiddenRequested)
6934 && !mExiting && !mDestroying;
6935 }
6936
6937 /**
6938 * Is this window visible, ignoring its app token? It is not visible
6939 * if there is no surface, or we are in the process of running an exit animation
6940 * that will remove the surface.
6941 */
6942 public boolean isWinVisibleLw() {
6943 final AppWindowToken atoken = mAppToken;
6944 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6945 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6946 && !mExiting && !mDestroying;
6947 }
6948
6949 /**
6950 * The same as isVisible(), but follows the current hidden state of
6951 * the associated app token, not the pending requested hidden state.
6952 */
6953 boolean isVisibleNow() {
6954 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006955 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 }
6957
6958 /**
6959 * Same as isVisible(), but we also count it as visible between the
6960 * call to IWindowSession.add() and the first relayout().
6961 */
6962 boolean isVisibleOrAdding() {
6963 final AppWindowToken atoken = mAppToken;
6964 return (mSurface != null
6965 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6966 && mPolicyVisibility && !mAttachedHidden
6967 && (atoken == null || !atoken.hiddenRequested)
6968 && !mExiting && !mDestroying;
6969 }
6970
6971 /**
6972 * Is this window currently on-screen? It is on-screen either if it
6973 * is visible or it is currently running an animation before no longer
6974 * being visible.
6975 */
6976 boolean isOnScreen() {
6977 final AppWindowToken atoken = mAppToken;
6978 if (atoken != null) {
6979 return mSurface != null && mPolicyVisibility && !mDestroying
6980 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07006981 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982 } else {
6983 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07006984 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006985 }
6986 }
Romain Guy06882f82009-06-10 13:36:04 -07006987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 /**
6989 * Like isOnScreen(), but we don't return true if the window is part
6990 * of a transition that has not yet been started.
6991 */
6992 boolean isReadyForDisplay() {
6993 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07006994 final boolean animating = atoken != null
6995 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006997 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
6998 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07006999 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007000 }
7001
7002 /** Is the window or its container currently animating? */
7003 boolean isAnimating() {
7004 final WindowState attached = mAttachedWindow;
7005 final AppWindowToken atoken = mAppToken;
7006 return mAnimation != null
7007 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007008 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007009 (atoken.animation != null
7010 || atoken.inPendingTransaction));
7011 }
7012
7013 /** Is this window currently animating? */
7014 boolean isWindowAnimating() {
7015 return mAnimation != null;
7016 }
7017
7018 /**
7019 * Like isOnScreen, but returns false if the surface hasn't yet
7020 * been drawn.
7021 */
7022 public boolean isDisplayedLw() {
7023 final AppWindowToken atoken = mAppToken;
7024 return mSurface != null && mPolicyVisibility && !mDestroying
7025 && !mDrawPending && !mCommitDrawPending
7026 && ((!mAttachedHidden &&
7027 (atoken == null || !atoken.hiddenRequested))
7028 || mAnimating);
7029 }
7030
7031 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7032 boolean shownFrame, boolean onlyOpaque) {
7033 if (mSurface == null) {
7034 return false;
7035 }
7036 if (mAppToken != null && !mAppToken.appFullscreen) {
7037 return false;
7038 }
7039 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7040 return false;
7041 }
7042 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007043
7044 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7045 return frame.left <= mCompatibleScreenFrame.left &&
7046 frame.top <= mCompatibleScreenFrame.top &&
7047 frame.right >= mCompatibleScreenFrame.right &&
7048 frame.bottom >= mCompatibleScreenFrame.bottom;
7049 } else {
7050 return frame.left <= 0 && frame.top <= 0
7051 && frame.right >= screenWidth
7052 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 }
Romain Guy06882f82009-06-10 13:36:04 -07007055
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007056 /**
7057 * Return true if the window is opaque and fully drawn.
7058 */
7059 boolean isOpaqueDrawn() {
7060 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
7061 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
7062 }
7063
7064 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7065 return
7066 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007067 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7068 // only if it's visible
7069 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007070 // and only if the application fills the compatible screen
7071 mFrame.left <= mCompatibleScreenFrame.left &&
7072 mFrame.top <= mCompatibleScreenFrame.top &&
7073 mFrame.right >= mCompatibleScreenFrame.right &&
7074 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007075 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007076 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007077 }
7078
7079 boolean isFullscreen(int screenWidth, int screenHeight) {
7080 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007081 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 }
7083
7084 void removeLocked() {
7085 if (mAttachedWindow != null) {
7086 mAttachedWindow.mChildWindows.remove(this);
7087 }
7088 destroySurfaceLocked();
7089 mSession.windowRemovedLocked();
7090 try {
7091 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7092 } catch (RuntimeException e) {
7093 // Ignore if it has already been removed (usually because
7094 // we are doing this as part of processing a death note.)
7095 }
7096 }
7097
7098 private class DeathRecipient implements IBinder.DeathRecipient {
7099 public void binderDied() {
7100 try {
7101 synchronized(mWindowMap) {
7102 WindowState win = windowForClientLocked(mSession, mClient);
7103 Log.i(TAG, "WIN DEATH: " + win);
7104 if (win != null) {
7105 removeWindowLocked(mSession, win);
7106 }
7107 }
7108 } catch (IllegalArgumentException ex) {
7109 // This will happen if the window has already been
7110 // removed.
7111 }
7112 }
7113 }
7114
7115 /** Returns true if this window desires key events. */
7116 public final boolean canReceiveKeys() {
7117 return isVisibleOrAdding()
7118 && (mViewVisibility == View.VISIBLE)
7119 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7120 }
7121
7122 public boolean hasDrawnLw() {
7123 return mHasDrawn;
7124 }
7125
7126 public boolean showLw(boolean doAnimation) {
7127 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7128 mPolicyVisibility = true;
7129 mPolicyVisibilityAfterAnim = true;
7130 if (doAnimation) {
7131 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7132 }
7133 requestAnimationLocked(0);
7134 return true;
7135 }
7136 return false;
7137 }
7138
7139 public boolean hideLw(boolean doAnimation) {
7140 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7141 : mPolicyVisibility;
7142 if (current) {
7143 if (doAnimation) {
7144 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7145 if (mAnimation == null) {
7146 doAnimation = false;
7147 }
7148 }
7149 if (doAnimation) {
7150 mPolicyVisibilityAfterAnim = false;
7151 } else {
7152 mPolicyVisibilityAfterAnim = false;
7153 mPolicyVisibility = false;
7154 }
7155 requestAnimationLocked(0);
7156 return true;
7157 }
7158 return false;
7159 }
7160
7161 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007162 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007163
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007164 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7165 pw.print(" mClient="); pw.println(mClient.asBinder());
7166 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7167 if (mAttachedWindow != null || mLayoutAttached) {
7168 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7169 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7170 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007171 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7172 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7173 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007174 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7175 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007176 }
7177 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7178 pw.print(" mSubLayer="); pw.print(mSubLayer);
7179 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7180 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7181 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7182 pw.print("="); pw.print(mAnimLayer);
7183 pw.print(" mLastLayer="); pw.println(mLastLayer);
7184 if (mSurface != null) {
7185 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7186 }
7187 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7188 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7189 if (mAppToken != null) {
7190 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7191 }
7192 if (mTargetAppToken != null) {
7193 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7194 }
7195 pw.print(prefix); pw.print("mViewVisibility=0x");
7196 pw.print(Integer.toHexString(mViewVisibility));
7197 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007198 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7199 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007200 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7201 pw.print(prefix); pw.print("mPolicyVisibility=");
7202 pw.print(mPolicyVisibility);
7203 pw.print(" mPolicyVisibilityAfterAnim=");
7204 pw.print(mPolicyVisibilityAfterAnim);
7205 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7206 }
7207 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007208 pw.print(" h="); pw.println(mRequestedHeight);
7209 if (mXOffset != 0 || mYOffset != 0) {
7210 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7211 pw.print(" y="); pw.println(mYOffset);
7212 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007213 pw.print(prefix); pw.print("mGivenContentInsets=");
7214 mGivenContentInsets.printShortString(pw);
7215 pw.print(" mGivenVisibleInsets=");
7216 mGivenVisibleInsets.printShortString(pw);
7217 pw.println();
7218 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7219 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7220 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7221 }
7222 pw.print(prefix); pw.print("mShownFrame=");
7223 mShownFrame.printShortString(pw);
7224 pw.print(" last="); mLastShownFrame.printShortString(pw);
7225 pw.println();
7226 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7227 pw.print(" last="); mLastFrame.printShortString(pw);
7228 pw.println();
7229 pw.print(prefix); pw.print("mContainingFrame=");
7230 mContainingFrame.printShortString(pw);
7231 pw.print(" mDisplayFrame=");
7232 mDisplayFrame.printShortString(pw);
7233 pw.println();
7234 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7235 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7236 pw.println();
7237 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7238 pw.print(" last="); mLastContentInsets.printShortString(pw);
7239 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7240 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7241 pw.println();
7242 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7243 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7244 pw.print(" mAlpha="); pw.print(mAlpha);
7245 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7246 }
7247 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7248 || mAnimation != null) {
7249 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7250 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7251 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7252 pw.print(" mAnimation="); pw.println(mAnimation);
7253 }
7254 if (mHasTransformation || mHasLocalTransformation) {
7255 pw.print(prefix); pw.print("XForm: has=");
7256 pw.print(mHasTransformation);
7257 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7258 pw.print(" "); mTransformation.printShortString(pw);
7259 pw.println();
7260 }
7261 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7262 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7263 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7264 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7265 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7266 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7267 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7268 pw.print(" mDestroying="); pw.print(mDestroying);
7269 pw.print(" mRemoved="); pw.println(mRemoved);
7270 }
7271 if (mOrientationChanging || mAppFreezing) {
7272 pw.print(prefix); pw.print("mOrientationChanging=");
7273 pw.print(mOrientationChanging);
7274 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7275 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007276 if (mHScale != 1 || mVScale != 1) {
7277 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7278 pw.print(" mVScale="); pw.println(mVScale);
7279 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007280 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007281 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7282 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007284 }
7285
7286 @Override
7287 public String toString() {
7288 return "Window{"
7289 + Integer.toHexString(System.identityHashCode(this))
7290 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7291 }
7292 }
Romain Guy06882f82009-06-10 13:36:04 -07007293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007294 // -------------------------------------------------------------
7295 // Window Token State
7296 // -------------------------------------------------------------
7297
7298 class WindowToken {
7299 // The actual token.
7300 final IBinder token;
7301
7302 // The type of window this token is for, as per WindowManager.LayoutParams.
7303 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007305 // Set if this token was explicitly added by a client, so should
7306 // not be removed when all windows are removed.
7307 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007308
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007309 // For printing.
7310 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 // If this is an AppWindowToken, this is non-null.
7313 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 // All of the windows associated with this token.
7316 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7317
7318 // Is key dispatching paused for this token?
7319 boolean paused = false;
7320
7321 // Should this token's windows be hidden?
7322 boolean hidden;
7323
7324 // Temporary for finding which tokens no longer have visible windows.
7325 boolean hasVisible;
7326
7327 WindowToken(IBinder _token, int type, boolean _explicit) {
7328 token = _token;
7329 windowType = type;
7330 explicit = _explicit;
7331 }
7332
7333 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007334 pw.print(prefix); pw.print("token="); pw.println(token);
7335 pw.print(prefix); pw.print("windows="); pw.println(windows);
7336 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7337 pw.print(" hidden="); pw.print(hidden);
7338 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007339 }
7340
7341 @Override
7342 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007343 if (stringName == null) {
7344 StringBuilder sb = new StringBuilder();
7345 sb.append("WindowToken{");
7346 sb.append(Integer.toHexString(System.identityHashCode(this)));
7347 sb.append(" token="); sb.append(token); sb.append('}');
7348 stringName = sb.toString();
7349 }
7350 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007351 }
7352 };
7353
7354 class AppWindowToken extends WindowToken {
7355 // Non-null only for application tokens.
7356 final IApplicationToken appToken;
7357
7358 // All of the windows and child windows that are included in this
7359 // application token. Note this list is NOT sorted!
7360 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7361
7362 int groupId = -1;
7363 boolean appFullscreen;
7364 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007366 // These are used for determining when all windows associated with
7367 // an activity have been drawn, so they can be made visible together
7368 // at the same time.
7369 int lastTransactionSequence = mTransactionSequence-1;
7370 int numInterestingWindows;
7371 int numDrawnWindows;
7372 boolean inPendingTransaction;
7373 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 // Is this token going to be hidden in a little while? If so, it
7376 // won't be taken into account for setting the screen orientation.
7377 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007379 // Is this window's surface needed? This is almost like hidden, except
7380 // it will sometimes be true a little earlier: when the token has
7381 // been shown, but is still waiting for its app transition to execute
7382 // before making its windows shown.
7383 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 // Have we told the window clients to hide themselves?
7386 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007388 // Last visibility state we reported to the app token.
7389 boolean reportedVisible;
7390
7391 // Set to true when the token has been removed from the window mgr.
7392 boolean removed;
7393
7394 // Have we been asked to have this token keep the screen frozen?
7395 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397 boolean animating;
7398 Animation animation;
7399 boolean hasTransformation;
7400 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 // Offset to the window of all layers in the token, for use by
7403 // AppWindowToken animations.
7404 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007406 // Information about an application starting window if displayed.
7407 StartingData startingData;
7408 WindowState startingWindow;
7409 View startingView;
7410 boolean startingDisplayed;
7411 boolean startingMoved;
7412 boolean firstWindowDrawn;
7413
7414 AppWindowToken(IApplicationToken _token) {
7415 super(_token.asBinder(),
7416 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7417 appWindowToken = this;
7418 appToken = _token;
7419 }
Romain Guy06882f82009-06-10 13:36:04 -07007420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007421 public void setAnimation(Animation anim) {
7422 if (localLOGV) Log.v(
7423 TAG, "Setting animation in " + this + ": " + anim);
7424 animation = anim;
7425 animating = false;
7426 anim.restrictDuration(MAX_ANIMATION_DURATION);
7427 anim.scaleCurrentDuration(mTransitionAnimationScale);
7428 int zorder = anim.getZAdjustment();
7429 int adj = 0;
7430 if (zorder == Animation.ZORDER_TOP) {
7431 adj = TYPE_LAYER_OFFSET;
7432 } else if (zorder == Animation.ZORDER_BOTTOM) {
7433 adj = -TYPE_LAYER_OFFSET;
7434 }
Romain Guy06882f82009-06-10 13:36:04 -07007435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007436 if (animLayerAdjustment != adj) {
7437 animLayerAdjustment = adj;
7438 updateLayers();
7439 }
7440 }
Romain Guy06882f82009-06-10 13:36:04 -07007441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007442 public void setDummyAnimation() {
7443 if (animation == null) {
7444 if (localLOGV) Log.v(
7445 TAG, "Setting dummy animation in " + this);
7446 animation = sDummyAnimation;
7447 }
7448 }
7449
7450 public void clearAnimation() {
7451 if (animation != null) {
7452 animation = null;
7453 animating = true;
7454 }
7455 }
Romain Guy06882f82009-06-10 13:36:04 -07007456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 void updateLayers() {
7458 final int N = allAppWindows.size();
7459 final int adj = animLayerAdjustment;
7460 for (int i=0; i<N; i++) {
7461 WindowState w = allAppWindows.get(i);
7462 w.mAnimLayer = w.mLayer + adj;
7463 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7464 + w.mAnimLayer);
7465 if (w == mInputMethodTarget) {
7466 setInputMethodAnimLayerAdjustment(adj);
7467 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007468 if (w == mWallpaperTarget) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007469 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007471 }
7472 }
Romain Guy06882f82009-06-10 13:36:04 -07007473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 void sendAppVisibilityToClients() {
7475 final int N = allAppWindows.size();
7476 for (int i=0; i<N; i++) {
7477 WindowState win = allAppWindows.get(i);
7478 if (win == startingWindow && clientHidden) {
7479 // Don't hide the starting window.
7480 continue;
7481 }
7482 try {
7483 if (DEBUG_VISIBILITY) Log.v(TAG,
7484 "Setting visibility of " + win + ": " + (!clientHidden));
7485 win.mClient.dispatchAppVisibility(!clientHidden);
7486 } catch (RemoteException e) {
7487 }
7488 }
7489 }
Romain Guy06882f82009-06-10 13:36:04 -07007490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007491 void showAllWindowsLocked() {
7492 final int NW = allAppWindows.size();
7493 for (int i=0; i<NW; i++) {
7494 WindowState w = allAppWindows.get(i);
7495 if (DEBUG_VISIBILITY) Log.v(TAG,
7496 "performing show on: " + w);
7497 w.performShowLocked();
7498 }
7499 }
Romain Guy06882f82009-06-10 13:36:04 -07007500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007501 // This must be called while inside a transaction.
7502 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7503 if (!mDisplayFrozen) {
7504 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007506 if (animation == sDummyAnimation) {
7507 // This guy is going to animate, but not yet. For now count
7508 // it is not animating for purposes of scheduling transactions;
7509 // when it is really time to animate, this will be set to
7510 // a real animation and the next call will execute normally.
7511 return false;
7512 }
Romain Guy06882f82009-06-10 13:36:04 -07007513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007514 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7515 if (!animating) {
7516 if (DEBUG_ANIM) Log.v(
7517 TAG, "Starting animation in " + this +
7518 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7519 + " scale=" + mTransitionAnimationScale
7520 + " allDrawn=" + allDrawn + " animating=" + animating);
7521 animation.initialize(dw, dh, dw, dh);
7522 animation.setStartTime(currentTime);
7523 animating = true;
7524 }
7525 transformation.clear();
7526 final boolean more = animation.getTransformation(
7527 currentTime, transformation);
7528 if (DEBUG_ANIM) Log.v(
7529 TAG, "Stepped animation in " + this +
7530 ": more=" + more + ", xform=" + transformation);
7531 if (more) {
7532 // we're done!
7533 hasTransformation = true;
7534 return true;
7535 }
7536 if (DEBUG_ANIM) Log.v(
7537 TAG, "Finished animation in " + this +
7538 " @ " + currentTime);
7539 animation = null;
7540 }
7541 } else if (animation != null) {
7542 // If the display is frozen, and there is a pending animation,
7543 // clear it and make sure we run the cleanup code.
7544 animating = true;
7545 animation = null;
7546 }
7547
7548 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007550 if (!animating) {
7551 return false;
7552 }
7553
7554 clearAnimation();
7555 animating = false;
7556 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7557 moveInputMethodWindowsIfNeededLocked(true);
7558 }
Romain Guy06882f82009-06-10 13:36:04 -07007559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007560 if (DEBUG_ANIM) Log.v(
7561 TAG, "Animation done in " + this
7562 + ": reportedVisible=" + reportedVisible);
7563
7564 transformation.clear();
7565 if (animLayerAdjustment != 0) {
7566 animLayerAdjustment = 0;
7567 updateLayers();
7568 }
Romain Guy06882f82009-06-10 13:36:04 -07007569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 final int N = windows.size();
7571 for (int i=0; i<N; i++) {
7572 ((WindowState)windows.get(i)).finishExit();
7573 }
7574 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007576 return false;
7577 }
7578
7579 void updateReportedVisibilityLocked() {
7580 if (appToken == null) {
7581 return;
7582 }
Romain Guy06882f82009-06-10 13:36:04 -07007583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007584 int numInteresting = 0;
7585 int numVisible = 0;
7586 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7589 final int N = allAppWindows.size();
7590 for (int i=0; i<N; i++) {
7591 WindowState win = allAppWindows.get(i);
7592 if (win == startingWindow || win.mAppFreezing) {
7593 continue;
7594 }
7595 if (DEBUG_VISIBILITY) {
7596 Log.v(TAG, "Win " + win + ": isDisplayed="
7597 + win.isDisplayedLw()
7598 + ", isAnimating=" + win.isAnimating());
7599 if (!win.isDisplayedLw()) {
7600 Log.v(TAG, "Not displayed: s=" + win.mSurface
7601 + " pv=" + win.mPolicyVisibility
7602 + " dp=" + win.mDrawPending
7603 + " cdp=" + win.mCommitDrawPending
7604 + " ah=" + win.mAttachedHidden
7605 + " th="
7606 + (win.mAppToken != null
7607 ? win.mAppToken.hiddenRequested : false)
7608 + " a=" + win.mAnimating);
7609 }
7610 }
7611 numInteresting++;
7612 if (win.isDisplayedLw()) {
7613 if (!win.isAnimating()) {
7614 numVisible++;
7615 }
7616 nowGone = false;
7617 } else if (win.isAnimating()) {
7618 nowGone = false;
7619 }
7620 }
Romain Guy06882f82009-06-10 13:36:04 -07007621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007622 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7623 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7624 + numInteresting + " visible=" + numVisible);
7625 if (nowVisible != reportedVisible) {
7626 if (DEBUG_VISIBILITY) Log.v(
7627 TAG, "Visibility changed in " + this
7628 + ": vis=" + nowVisible);
7629 reportedVisible = nowVisible;
7630 Message m = mH.obtainMessage(
7631 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7632 nowVisible ? 1 : 0,
7633 nowGone ? 1 : 0,
7634 this);
7635 mH.sendMessage(m);
7636 }
7637 }
Romain Guy06882f82009-06-10 13:36:04 -07007638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007639 void dump(PrintWriter pw, String prefix) {
7640 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007641 if (appToken != null) {
7642 pw.print(prefix); pw.println("app=true");
7643 }
7644 if (allAppWindows.size() > 0) {
7645 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7646 }
7647 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7648 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7649 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7650 pw.print(" clientHidden="); pw.print(clientHidden);
7651 pw.print(" willBeHidden="); pw.print(willBeHidden);
7652 pw.print(" reportedVisible="); pw.println(reportedVisible);
7653 if (paused || freezingScreen) {
7654 pw.print(prefix); pw.print("paused="); pw.print(paused);
7655 pw.print(" freezingScreen="); pw.println(freezingScreen);
7656 }
7657 if (numInterestingWindows != 0 || numDrawnWindows != 0
7658 || inPendingTransaction || allDrawn) {
7659 pw.print(prefix); pw.print("numInterestingWindows=");
7660 pw.print(numInterestingWindows);
7661 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7662 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7663 pw.print(" allDrawn="); pw.println(allDrawn);
7664 }
7665 if (animating || animation != null) {
7666 pw.print(prefix); pw.print("animating="); pw.print(animating);
7667 pw.print(" animation="); pw.println(animation);
7668 }
7669 if (animLayerAdjustment != 0) {
7670 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7671 }
7672 if (hasTransformation) {
7673 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7674 pw.print(" transformation="); transformation.printShortString(pw);
7675 pw.println();
7676 }
7677 if (startingData != null || removed || firstWindowDrawn) {
7678 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7679 pw.print(" removed="); pw.print(removed);
7680 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7681 }
7682 if (startingWindow != null || startingView != null
7683 || startingDisplayed || startingMoved) {
7684 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7685 pw.print(" startingView="); pw.print(startingView);
7686 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7687 pw.print(" startingMoved"); pw.println(startingMoved);
7688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007689 }
7690
7691 @Override
7692 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007693 if (stringName == null) {
7694 StringBuilder sb = new StringBuilder();
7695 sb.append("AppWindowToken{");
7696 sb.append(Integer.toHexString(System.identityHashCode(this)));
7697 sb.append(" token="); sb.append(token); sb.append('}');
7698 stringName = sb.toString();
7699 }
7700 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007701 }
7702 }
Romain Guy06882f82009-06-10 13:36:04 -07007703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007704 public static WindowManager.LayoutParams findAnimations(
7705 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007706 ArrayList<AppWindowToken> openingTokenList1,
7707 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007708 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007709
7710 // First, check if there is a compatible window in opening/closing
7711 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007712 WindowManager.LayoutParams animParams = null;
7713 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007714 animParams = findCompatibleWindowParams(openingTokenList1);
7715 if (animParams == null) {
7716 animParams = findCompatibleWindowParams(closingTokenList2);
7717 }
7718 if (animParams != null) {
7719 return animParams;
7720 }
7721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 //Log.i(TAG, "Looking for animations...");
7723 for (int i=order.size()-1; i>=0; i--) {
7724 AppWindowToken wtoken = order.get(i);
7725 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007726 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007727 int j = wtoken.windows.size();
7728 while (j > 0) {
7729 j--;
7730 WindowState win = wtoken.windows.get(j);
7731 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7732 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7733 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7734 //Log.i(TAG, "Found base or application window, done!");
7735 if (wtoken.appFullscreen) {
7736 return win.mAttrs;
7737 }
7738 if (animSrc < 2) {
7739 animParams = win.mAttrs;
7740 animSrc = 2;
7741 }
7742 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7743 //Log.i(TAG, "Found normal window, we may use this...");
7744 animParams = win.mAttrs;
7745 animSrc = 1;
7746 }
7747 }
7748 }
7749 }
Romain Guy06882f82009-06-10 13:36:04 -07007750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007751 return animParams;
7752 }
Romain Guy06882f82009-06-10 13:36:04 -07007753
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007754 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7755 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7756 AppWindowToken wtoken = tokenList.get(appCount);
7757 // Just checking one window is sufficient as all windows have the compatible flag
7758 // if the application is in compatibility mode.
7759 if (wtoken.windows.size() > 0) {
7760 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7761 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7762 return params;
7763 }
7764 }
7765 }
7766 return null;
7767 }
7768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 // -------------------------------------------------------------
7770 // DummyAnimation
7771 // -------------------------------------------------------------
7772
7773 // This is an animation that does nothing: it just immediately finishes
7774 // itself every time it is called. It is used as a stub animation in cases
7775 // where we want to synchronize multiple things that may be animating.
7776 static final class DummyAnimation extends Animation {
7777 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7778 return false;
7779 }
7780 }
7781 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 // -------------------------------------------------------------
7784 // Async Handler
7785 // -------------------------------------------------------------
7786
7787 static final class StartingData {
7788 final String pkg;
7789 final int theme;
7790 final CharSequence nonLocalizedLabel;
7791 final int labelRes;
7792 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007794 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7795 int _labelRes, int _icon) {
7796 pkg = _pkg;
7797 theme = _theme;
7798 nonLocalizedLabel = _nonLocalizedLabel;
7799 labelRes = _labelRes;
7800 icon = _icon;
7801 }
7802 }
7803
7804 private final class H extends Handler {
7805 public static final int REPORT_FOCUS_CHANGE = 2;
7806 public static final int REPORT_LOSING_FOCUS = 3;
7807 public static final int ANIMATE = 4;
7808 public static final int ADD_STARTING = 5;
7809 public static final int REMOVE_STARTING = 6;
7810 public static final int FINISHED_STARTING = 7;
7811 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007812 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7813 public static final int HOLD_SCREEN_CHANGED = 12;
7814 public static final int APP_TRANSITION_TIMEOUT = 13;
7815 public static final int PERSIST_ANIMATION_SCALE = 14;
7816 public static final int FORCE_GC = 15;
7817 public static final int ENABLE_SCREEN = 16;
7818 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007819 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007821 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 public H() {
7824 }
Romain Guy06882f82009-06-10 13:36:04 -07007825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 @Override
7827 public void handleMessage(Message msg) {
7828 switch (msg.what) {
7829 case REPORT_FOCUS_CHANGE: {
7830 WindowState lastFocus;
7831 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 synchronized(mWindowMap) {
7834 lastFocus = mLastFocus;
7835 newFocus = mCurrentFocus;
7836 if (lastFocus == newFocus) {
7837 // Focus is not changing, so nothing to do.
7838 return;
7839 }
7840 mLastFocus = newFocus;
7841 //Log.i(TAG, "Focus moving from " + lastFocus
7842 // + " to " + newFocus);
7843 if (newFocus != null && lastFocus != null
7844 && !newFocus.isDisplayedLw()) {
7845 //Log.i(TAG, "Delaying loss of focus...");
7846 mLosingFocus.add(lastFocus);
7847 lastFocus = null;
7848 }
7849 }
7850
7851 if (lastFocus != newFocus) {
7852 //System.out.println("Changing focus from " + lastFocus
7853 // + " to " + newFocus);
7854 if (newFocus != null) {
7855 try {
7856 //Log.i(TAG, "Gaining focus: " + newFocus);
7857 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7858 } catch (RemoteException e) {
7859 // Ignore if process has died.
7860 }
7861 }
7862
7863 if (lastFocus != null) {
7864 try {
7865 //Log.i(TAG, "Losing focus: " + lastFocus);
7866 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7867 } catch (RemoteException e) {
7868 // Ignore if process has died.
7869 }
7870 }
7871 }
7872 } break;
7873
7874 case REPORT_LOSING_FOCUS: {
7875 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007877 synchronized(mWindowMap) {
7878 losers = mLosingFocus;
7879 mLosingFocus = new ArrayList<WindowState>();
7880 }
7881
7882 final int N = losers.size();
7883 for (int i=0; i<N; i++) {
7884 try {
7885 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7886 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7887 } catch (RemoteException e) {
7888 // Ignore if process has died.
7889 }
7890 }
7891 } break;
7892
7893 case ANIMATE: {
7894 synchronized(mWindowMap) {
7895 mAnimationPending = false;
7896 performLayoutAndPlaceSurfacesLocked();
7897 }
7898 } break;
7899
7900 case ADD_STARTING: {
7901 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7902 final StartingData sd = wtoken.startingData;
7903
7904 if (sd == null) {
7905 // Animation has been canceled... do nothing.
7906 return;
7907 }
Romain Guy06882f82009-06-10 13:36:04 -07007908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007909 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7910 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 View view = null;
7913 try {
7914 view = mPolicy.addStartingWindow(
7915 wtoken.token, sd.pkg,
7916 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7917 sd.icon);
7918 } catch (Exception e) {
7919 Log.w(TAG, "Exception when adding starting window", e);
7920 }
7921
7922 if (view != null) {
7923 boolean abort = false;
7924
7925 synchronized(mWindowMap) {
7926 if (wtoken.removed || wtoken.startingData == null) {
7927 // If the window was successfully added, then
7928 // we need to remove it.
7929 if (wtoken.startingWindow != null) {
7930 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7931 "Aborted starting " + wtoken
7932 + ": removed=" + wtoken.removed
7933 + " startingData=" + wtoken.startingData);
7934 wtoken.startingWindow = null;
7935 wtoken.startingData = null;
7936 abort = true;
7937 }
7938 } else {
7939 wtoken.startingView = view;
7940 }
7941 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7942 "Added starting " + wtoken
7943 + ": startingWindow="
7944 + wtoken.startingWindow + " startingView="
7945 + wtoken.startingView);
7946 }
7947
7948 if (abort) {
7949 try {
7950 mPolicy.removeStartingWindow(wtoken.token, view);
7951 } catch (Exception e) {
7952 Log.w(TAG, "Exception when removing starting window", e);
7953 }
7954 }
7955 }
7956 } break;
7957
7958 case REMOVE_STARTING: {
7959 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7960 IBinder token = null;
7961 View view = null;
7962 synchronized (mWindowMap) {
7963 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7964 + wtoken + ": startingWindow="
7965 + wtoken.startingWindow + " startingView="
7966 + wtoken.startingView);
7967 if (wtoken.startingWindow != null) {
7968 view = wtoken.startingView;
7969 token = wtoken.token;
7970 wtoken.startingData = null;
7971 wtoken.startingView = null;
7972 wtoken.startingWindow = null;
7973 }
7974 }
7975 if (view != null) {
7976 try {
7977 mPolicy.removeStartingWindow(token, view);
7978 } catch (Exception e) {
7979 Log.w(TAG, "Exception when removing starting window", e);
7980 }
7981 }
7982 } break;
7983
7984 case FINISHED_STARTING: {
7985 IBinder token = null;
7986 View view = null;
7987 while (true) {
7988 synchronized (mWindowMap) {
7989 final int N = mFinishedStarting.size();
7990 if (N <= 0) {
7991 break;
7992 }
7993 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7994
7995 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7996 "Finished starting " + wtoken
7997 + ": startingWindow=" + wtoken.startingWindow
7998 + " startingView=" + wtoken.startingView);
7999
8000 if (wtoken.startingWindow == null) {
8001 continue;
8002 }
8003
8004 view = wtoken.startingView;
8005 token = wtoken.token;
8006 wtoken.startingData = null;
8007 wtoken.startingView = null;
8008 wtoken.startingWindow = null;
8009 }
8010
8011 try {
8012 mPolicy.removeStartingWindow(token, view);
8013 } catch (Exception e) {
8014 Log.w(TAG, "Exception when removing starting window", e);
8015 }
8016 }
8017 } break;
8018
8019 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8020 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8021
8022 boolean nowVisible = msg.arg1 != 0;
8023 boolean nowGone = msg.arg2 != 0;
8024
8025 try {
8026 if (DEBUG_VISIBILITY) Log.v(
8027 TAG, "Reporting visible in " + wtoken
8028 + " visible=" + nowVisible
8029 + " gone=" + nowGone);
8030 if (nowVisible) {
8031 wtoken.appToken.windowsVisible();
8032 } else {
8033 wtoken.appToken.windowsGone();
8034 }
8035 } catch (RemoteException ex) {
8036 }
8037 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008039 case WINDOW_FREEZE_TIMEOUT: {
8040 synchronized (mWindowMap) {
8041 Log.w(TAG, "Window freeze timeout expired.");
8042 int i = mWindows.size();
8043 while (i > 0) {
8044 i--;
8045 WindowState w = (WindowState)mWindows.get(i);
8046 if (w.mOrientationChanging) {
8047 w.mOrientationChanging = false;
8048 Log.w(TAG, "Force clearing orientation change: " + w);
8049 }
8050 }
8051 performLayoutAndPlaceSurfacesLocked();
8052 }
8053 break;
8054 }
Romain Guy06882f82009-06-10 13:36:04 -07008055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056 case HOLD_SCREEN_CHANGED: {
8057 Session oldHold;
8058 Session newHold;
8059 synchronized (mWindowMap) {
8060 oldHold = mLastReportedHold;
8061 newHold = (Session)msg.obj;
8062 mLastReportedHold = newHold;
8063 }
Romain Guy06882f82009-06-10 13:36:04 -07008064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008065 if (oldHold != newHold) {
8066 try {
8067 if (oldHold != null) {
8068 mBatteryStats.noteStopWakelock(oldHold.mUid,
8069 "window",
8070 BatteryStats.WAKE_TYPE_WINDOW);
8071 }
8072 if (newHold != null) {
8073 mBatteryStats.noteStartWakelock(newHold.mUid,
8074 "window",
8075 BatteryStats.WAKE_TYPE_WINDOW);
8076 }
8077 } catch (RemoteException e) {
8078 }
8079 }
8080 break;
8081 }
Romain Guy06882f82009-06-10 13:36:04 -07008082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008083 case APP_TRANSITION_TIMEOUT: {
8084 synchronized (mWindowMap) {
8085 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8086 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8087 "*** APP TRANSITION TIMEOUT");
8088 mAppTransitionReady = true;
8089 mAppTransitionTimeout = true;
8090 performLayoutAndPlaceSurfacesLocked();
8091 }
8092 }
8093 break;
8094 }
Romain Guy06882f82009-06-10 13:36:04 -07008095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008096 case PERSIST_ANIMATION_SCALE: {
8097 Settings.System.putFloat(mContext.getContentResolver(),
8098 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8099 Settings.System.putFloat(mContext.getContentResolver(),
8100 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8101 break;
8102 }
Romain Guy06882f82009-06-10 13:36:04 -07008103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008104 case FORCE_GC: {
8105 synchronized(mWindowMap) {
8106 if (mAnimationPending) {
8107 // If we are animating, don't do the gc now but
8108 // delay a bit so we don't interrupt the animation.
8109 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8110 2000);
8111 return;
8112 }
8113 // If we are currently rotating the display, it will
8114 // schedule a new message when done.
8115 if (mDisplayFrozen) {
8116 return;
8117 }
8118 mFreezeGcPending = 0;
8119 }
8120 Runtime.getRuntime().gc();
8121 break;
8122 }
Romain Guy06882f82009-06-10 13:36:04 -07008123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008124 case ENABLE_SCREEN: {
8125 performEnableScreen();
8126 break;
8127 }
Romain Guy06882f82009-06-10 13:36:04 -07008128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 case APP_FREEZE_TIMEOUT: {
8130 synchronized (mWindowMap) {
8131 Log.w(TAG, "App freeze timeout expired.");
8132 int i = mAppTokens.size();
8133 while (i > 0) {
8134 i--;
8135 AppWindowToken tok = mAppTokens.get(i);
8136 if (tok.freezingScreen) {
8137 Log.w(TAG, "Force clearing freeze: " + tok);
8138 unsetAppFreezingScreenLocked(tok, true, true);
8139 }
8140 }
8141 }
8142 break;
8143 }
Romain Guy06882f82009-06-10 13:36:04 -07008144
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008145 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008146 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008147 sendNewConfiguration();
8148 }
8149 break;
8150 }
Romain Guy06882f82009-06-10 13:36:04 -07008151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008152 }
8153 }
8154 }
8155
8156 // -------------------------------------------------------------
8157 // IWindowManager API
8158 // -------------------------------------------------------------
8159
8160 public IWindowSession openSession(IInputMethodClient client,
8161 IInputContext inputContext) {
8162 if (client == null) throw new IllegalArgumentException("null client");
8163 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8164 return new Session(client, inputContext);
8165 }
8166
8167 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8168 synchronized (mWindowMap) {
8169 // The focus for the client is the window immediately below
8170 // where we would place the input method window.
8171 int idx = findDesiredInputMethodWindowIndexLocked(false);
8172 WindowState imFocus;
8173 if (idx > 0) {
8174 imFocus = (WindowState)mWindows.get(idx-1);
8175 if (imFocus != null) {
8176 if (imFocus.mSession.mClient != null &&
8177 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8178 return true;
8179 }
8180 }
8181 }
8182 }
8183 return false;
8184 }
Romain Guy06882f82009-06-10 13:36:04 -07008185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008186 // -------------------------------------------------------------
8187 // Internals
8188 // -------------------------------------------------------------
8189
8190 final WindowState windowForClientLocked(Session session, IWindow client) {
8191 return windowForClientLocked(session, client.asBinder());
8192 }
Romain Guy06882f82009-06-10 13:36:04 -07008193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008194 final WindowState windowForClientLocked(Session session, IBinder client) {
8195 WindowState win = mWindowMap.get(client);
8196 if (localLOGV) Log.v(
8197 TAG, "Looking up client " + client + ": " + win);
8198 if (win == null) {
8199 RuntimeException ex = new RuntimeException();
8200 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8201 return null;
8202 }
8203 if (session != null && win.mSession != session) {
8204 RuntimeException ex = new RuntimeException();
8205 Log.w(TAG, "Requested window " + client + " is in session " +
8206 win.mSession + ", not " + session, ex);
8207 return null;
8208 }
8209
8210 return win;
8211 }
8212
8213 private final void assignLayersLocked() {
8214 int N = mWindows.size();
8215 int curBaseLayer = 0;
8216 int curLayer = 0;
8217 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008219 for (i=0; i<N; i++) {
8220 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008221 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8222 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223 curLayer += WINDOW_LAYER_MULTIPLIER;
8224 w.mLayer = curLayer;
8225 } else {
8226 curBaseLayer = curLayer = w.mBaseLayer;
8227 w.mLayer = curLayer;
8228 }
8229 if (w.mTargetAppToken != null) {
8230 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8231 } else if (w.mAppToken != null) {
8232 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8233 } else {
8234 w.mAnimLayer = w.mLayer;
8235 }
8236 if (w.mIsImWindow) {
8237 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008238 } else if (w.mIsWallpaper) {
8239 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008240 }
8241 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8242 + w.mAnimLayer);
8243 //System.out.println(
8244 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8245 }
8246 }
8247
8248 private boolean mInLayout = false;
8249 private final void performLayoutAndPlaceSurfacesLocked() {
8250 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008251 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008252 throw new RuntimeException("Recursive call!");
8253 }
8254 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8255 return;
8256 }
8257
8258 boolean recoveringMemory = false;
8259 if (mForceRemoves != null) {
8260 recoveringMemory = true;
8261 // Wait a little it for things to settle down, and off we go.
8262 for (int i=0; i<mForceRemoves.size(); i++) {
8263 WindowState ws = mForceRemoves.get(i);
8264 Log.i(TAG, "Force removing: " + ws);
8265 removeWindowInnerLocked(ws.mSession, ws);
8266 }
8267 mForceRemoves = null;
8268 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8269 Object tmp = new Object();
8270 synchronized (tmp) {
8271 try {
8272 tmp.wait(250);
8273 } catch (InterruptedException e) {
8274 }
8275 }
8276 }
Romain Guy06882f82009-06-10 13:36:04 -07008277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008278 mInLayout = true;
8279 try {
8280 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 int i = mPendingRemove.size()-1;
8283 if (i >= 0) {
8284 while (i >= 0) {
8285 WindowState w = mPendingRemove.get(i);
8286 removeWindowInnerLocked(w.mSession, w);
8287 i--;
8288 }
8289 mPendingRemove.clear();
8290
8291 mInLayout = false;
8292 assignLayersLocked();
8293 mLayoutNeeded = true;
8294 performLayoutAndPlaceSurfacesLocked();
8295
8296 } else {
8297 mInLayout = false;
8298 if (mLayoutNeeded) {
8299 requestAnimationLocked(0);
8300 }
8301 }
8302 } catch (RuntimeException e) {
8303 mInLayout = false;
8304 Log.e(TAG, "Unhandled exception while layout out windows", e);
8305 }
8306 }
8307
8308 private final void performLayoutLockedInner() {
8309 final int dw = mDisplay.getWidth();
8310 final int dh = mDisplay.getHeight();
8311
8312 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008313 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008314 int i;
8315
8316 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008317
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008318 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008319 mPolicy.beginLayoutLw(dw, dh);
8320
8321 // First perform layout of any root windows (not attached
8322 // to another window).
8323 int topAttached = -1;
8324 for (i = N-1; i >= 0; i--) {
8325 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008326
8327 // Don't do layout of a window if it is not visible, or
8328 // soon won't be visible, to avoid wasting time and funky
8329 // changes while a window is animating away.
8330 final AppWindowToken atoken = win.mAppToken;
8331 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008333 || win.mRootToken.hidden
8334 || (atoken != null && atoken.hiddenRequested)
8335 || !win.mPolicyVisibility
8336 || win.mAttachedHidden
8337 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008338
8339 // If this view is GONE, then skip it -- keep the current
8340 // frame, and let the caller know so they can ignore it
8341 // if they want. (We do the normal layout for INVISIBLE
8342 // windows, since that means "perform layout as normal,
8343 // just don't display").
8344 if (!gone || !win.mHaveFrame) {
8345 if (!win.mLayoutAttached) {
8346 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8347 } else {
8348 if (topAttached < 0) topAttached = i;
8349 }
8350 }
8351 }
Romain Guy06882f82009-06-10 13:36:04 -07008352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 // Now perform layout of attached windows, which usually
8354 // depend on the position of the window they are attached to.
8355 // XXX does not deal with windows that are attached to windows
8356 // that are themselves attached.
8357 for (i = topAttached; i >= 0; i--) {
8358 WindowState win = (WindowState) mWindows.get(i);
8359
8360 // If this view is GONE, then skip it -- keep the current
8361 // frame, and let the caller know so they can ignore it
8362 // if they want. (We do the normal layout for INVISIBLE
8363 // windows, since that means "perform layout as normal,
8364 // just don't display").
8365 if (win.mLayoutAttached) {
8366 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8367 || !win.mHaveFrame) {
8368 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8369 }
8370 }
8371 }
8372
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008373 if (!mPolicy.finishLayoutLw()) {
8374 mLayoutNeeded = false;
8375 } else if (repeats > 2) {
8376 Log.w(TAG, "Layout repeat aborted after too many iterations");
8377 mLayoutNeeded = false;
8378 } else {
8379 repeats++;
8380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008381 }
8382 }
Romain Guy06882f82009-06-10 13:36:04 -07008383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384 private final void performLayoutAndPlaceSurfacesLockedInner(
8385 boolean recoveringMemory) {
8386 final long currentTime = SystemClock.uptimeMillis();
8387 final int dw = mDisplay.getWidth();
8388 final int dh = mDisplay.getHeight();
8389
8390 final int N = mWindows.size();
8391 int i;
8392
8393 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008394 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008396 if (mFxSession == null) {
8397 mFxSession = new SurfaceSession();
8398 }
Romain Guy06882f82009-06-10 13:36:04 -07008399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008400 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8401
8402 // Initialize state of exiting tokens.
8403 for (i=mExitingTokens.size()-1; i>=0; i--) {
8404 mExitingTokens.get(i).hasVisible = false;
8405 }
8406
8407 // Initialize state of exiting applications.
8408 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8409 mExitingAppTokens.get(i).hasVisible = false;
8410 }
8411
8412 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008413 boolean orientationChangeComplete = true;
8414 Session holdScreen = null;
8415 float screenBrightness = -1;
8416 boolean focusDisplayed = false;
8417 boolean animating = false;
8418
8419 Surface.openTransaction();
8420 try {
8421 boolean restart;
8422
8423 do {
8424 final int transactionSequence = ++mTransactionSequence;
8425
8426 // Update animations of all applications, including those
8427 // associated with exiting/removed apps
8428 boolean tokensAnimating = false;
8429 final int NAT = mAppTokens.size();
8430 for (i=0; i<NAT; i++) {
8431 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8432 tokensAnimating = true;
8433 }
8434 }
8435 final int NEAT = mExitingAppTokens.size();
8436 for (i=0; i<NEAT; i++) {
8437 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8438 tokensAnimating = true;
8439 }
8440 }
8441
8442 animating = tokensAnimating;
8443 restart = false;
8444
8445 boolean tokenMayBeDrawn = false;
8446
8447 mPolicy.beginAnimationLw(dw, dh);
8448
8449 for (i=N-1; i>=0; i--) {
8450 WindowState w = (WindowState)mWindows.get(i);
8451
8452 final WindowManager.LayoutParams attrs = w.mAttrs;
8453
8454 if (w.mSurface != null) {
8455 // Execute animation.
8456 w.commitFinishDrawingLocked(currentTime);
8457 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8458 animating = true;
8459 //w.dump(" ");
8460 }
8461
8462 mPolicy.animatingWindowLw(w, attrs);
8463 }
8464
8465 final AppWindowToken atoken = w.mAppToken;
8466 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8467 if (atoken.lastTransactionSequence != transactionSequence) {
8468 atoken.lastTransactionSequence = transactionSequence;
8469 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8470 atoken.startingDisplayed = false;
8471 }
8472 if ((w.isOnScreen() || w.mAttrs.type
8473 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8474 && !w.mExiting && !w.mDestroying) {
8475 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8476 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8477 + w.isDisplayedLw()
8478 + ", isAnimating=" + w.isAnimating());
8479 if (!w.isDisplayedLw()) {
8480 Log.v(TAG, "Not displayed: s=" + w.mSurface
8481 + " pv=" + w.mPolicyVisibility
8482 + " dp=" + w.mDrawPending
8483 + " cdp=" + w.mCommitDrawPending
8484 + " ah=" + w.mAttachedHidden
8485 + " th=" + atoken.hiddenRequested
8486 + " a=" + w.mAnimating);
8487 }
8488 }
8489 if (w != atoken.startingWindow) {
8490 if (!atoken.freezingScreen || !w.mAppFreezing) {
8491 atoken.numInterestingWindows++;
8492 if (w.isDisplayedLw()) {
8493 atoken.numDrawnWindows++;
8494 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8495 "tokenMayBeDrawn: " + atoken
8496 + " freezingScreen=" + atoken.freezingScreen
8497 + " mAppFreezing=" + w.mAppFreezing);
8498 tokenMayBeDrawn = true;
8499 }
8500 }
8501 } else if (w.isDisplayedLw()) {
8502 atoken.startingDisplayed = true;
8503 }
8504 }
8505 } else if (w.mReadyToShow) {
8506 w.performShowLocked();
8507 }
8508 }
8509
8510 if (mPolicy.finishAnimationLw()) {
8511 restart = true;
8512 }
8513
8514 if (tokenMayBeDrawn) {
8515 // See if any windows have been drawn, so they (and others
8516 // associated with them) can now be shown.
8517 final int NT = mTokenList.size();
8518 for (i=0; i<NT; i++) {
8519 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8520 if (wtoken == null) {
8521 continue;
8522 }
8523 if (wtoken.freezingScreen) {
8524 int numInteresting = wtoken.numInterestingWindows;
8525 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8526 if (DEBUG_VISIBILITY) Log.v(TAG,
8527 "allDrawn: " + wtoken
8528 + " interesting=" + numInteresting
8529 + " drawn=" + wtoken.numDrawnWindows);
8530 wtoken.showAllWindowsLocked();
8531 unsetAppFreezingScreenLocked(wtoken, false, true);
8532 orientationChangeComplete = true;
8533 }
8534 } else if (!wtoken.allDrawn) {
8535 int numInteresting = wtoken.numInterestingWindows;
8536 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8537 if (DEBUG_VISIBILITY) Log.v(TAG,
8538 "allDrawn: " + wtoken
8539 + " interesting=" + numInteresting
8540 + " drawn=" + wtoken.numDrawnWindows);
8541 wtoken.allDrawn = true;
8542 restart = true;
8543
8544 // We can now show all of the drawn windows!
8545 if (!mOpeningApps.contains(wtoken)) {
8546 wtoken.showAllWindowsLocked();
8547 }
8548 }
8549 }
8550 }
8551 }
8552
8553 // If we are ready to perform an app transition, check through
8554 // all of the app tokens to be shown and see if they are ready
8555 // to go.
8556 if (mAppTransitionReady) {
8557 int NN = mOpeningApps.size();
8558 boolean goodToGo = true;
8559 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8560 "Checking " + NN + " opening apps (frozen="
8561 + mDisplayFrozen + " timeout="
8562 + mAppTransitionTimeout + ")...");
8563 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8564 // If the display isn't frozen, wait to do anything until
8565 // all of the apps are ready. Otherwise just go because
8566 // we'll unfreeze the display when everyone is ready.
8567 for (i=0; i<NN && goodToGo; i++) {
8568 AppWindowToken wtoken = mOpeningApps.get(i);
8569 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8570 "Check opening app" + wtoken + ": allDrawn="
8571 + wtoken.allDrawn + " startingDisplayed="
8572 + wtoken.startingDisplayed);
8573 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8574 && !wtoken.startingMoved) {
8575 goodToGo = false;
8576 }
8577 }
8578 }
8579 if (goodToGo) {
8580 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8581 int transit = mNextAppTransition;
8582 if (mSkipAppTransitionAnimation) {
8583 transit = WindowManagerPolicy.TRANSIT_NONE;
8584 }
8585 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8586 mAppTransitionReady = false;
8587 mAppTransitionTimeout = false;
8588 mStartingIconInTransition = false;
8589 mSkipAppTransitionAnimation = false;
8590
8591 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8592
8593 // We need to figure out which animation to use...
8594 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8595 mOpeningApps, mClosingApps);
8596
8597 NN = mOpeningApps.size();
8598 for (i=0; i<NN; i++) {
8599 AppWindowToken wtoken = mOpeningApps.get(i);
8600 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8601 "Now opening app" + wtoken);
8602 wtoken.reportedVisible = false;
8603 wtoken.inPendingTransaction = false;
8604 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8605 wtoken.updateReportedVisibilityLocked();
8606 wtoken.showAllWindowsLocked();
8607 }
8608 NN = mClosingApps.size();
8609 for (i=0; i<NN; i++) {
8610 AppWindowToken wtoken = mClosingApps.get(i);
8611 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8612 "Now closing app" + wtoken);
8613 wtoken.inPendingTransaction = false;
8614 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8615 wtoken.updateReportedVisibilityLocked();
8616 // Force the allDrawn flag, because we want to start
8617 // this guy's animations regardless of whether it's
8618 // gotten drawn.
8619 wtoken.allDrawn = true;
8620 }
8621
8622 mOpeningApps.clear();
8623 mClosingApps.clear();
8624
8625 // This has changed the visibility of windows, so perform
8626 // a new layout to get them all up-to-date.
8627 mLayoutNeeded = true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008628 adjustWallpaperWindowsLocked();
Dianne Hackborn20583ff2009-07-27 21:51:05 -07008629 if (!moveInputMethodWindowsIfNeededLocked(true)) {
8630 assignLayersLocked();
8631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008632 performLayoutLockedInner();
8633 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8634
8635 restart = true;
8636 }
8637 }
8638 } while (restart);
8639
8640 // THIRD LOOP: Update the surfaces of all windows.
8641
8642 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8643
8644 boolean obscured = false;
8645 boolean blurring = false;
8646 boolean dimming = false;
8647 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008648 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008649 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008650
8651 for (i=N-1; i>=0; i--) {
8652 WindowState w = (WindowState)mWindows.get(i);
8653
8654 boolean displayed = false;
8655 final WindowManager.LayoutParams attrs = w.mAttrs;
8656 final int attrFlags = attrs.flags;
8657
8658 if (w.mSurface != null) {
8659 w.computeShownFrameLocked();
8660 if (localLOGV) Log.v(
8661 TAG, "Placing surface #" + i + " " + w.mSurface
8662 + ": new=" + w.mShownFrame + ", old="
8663 + w.mLastShownFrame);
8664
8665 boolean resize;
8666 int width, height;
8667 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8668 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8669 w.mLastRequestedHeight != w.mRequestedHeight;
8670 // for a scaled surface, we just want to use
8671 // the requested size.
8672 width = w.mRequestedWidth;
8673 height = w.mRequestedHeight;
8674 w.mLastRequestedWidth = width;
8675 w.mLastRequestedHeight = height;
8676 w.mLastShownFrame.set(w.mShownFrame);
8677 try {
8678 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8679 } catch (RuntimeException e) {
8680 Log.w(TAG, "Error positioning surface in " + w, e);
8681 if (!recoveringMemory) {
8682 reclaimSomeSurfaceMemoryLocked(w, "position");
8683 }
8684 }
8685 } else {
8686 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8687 width = w.mShownFrame.width();
8688 height = w.mShownFrame.height();
8689 w.mLastShownFrame.set(w.mShownFrame);
8690 if (resize) {
8691 if (SHOW_TRANSACTIONS) Log.i(
8692 TAG, " SURFACE " + w.mSurface + ": ("
8693 + w.mShownFrame.left + ","
8694 + w.mShownFrame.top + ") ("
8695 + w.mShownFrame.width() + "x"
8696 + w.mShownFrame.height() + ")");
8697 }
8698 }
8699
8700 if (resize) {
8701 if (width < 1) width = 1;
8702 if (height < 1) height = 1;
8703 if (w.mSurface != null) {
8704 try {
8705 w.mSurface.setSize(width, height);
8706 w.mSurface.setPosition(w.mShownFrame.left,
8707 w.mShownFrame.top);
8708 } catch (RuntimeException e) {
8709 // If something goes wrong with the surface (such
8710 // as running out of memory), don't take down the
8711 // entire system.
8712 Log.e(TAG, "Failure updating surface of " + w
8713 + "size=(" + width + "x" + height
8714 + "), pos=(" + w.mShownFrame.left
8715 + "," + w.mShownFrame.top + ")", e);
8716 if (!recoveringMemory) {
8717 reclaimSomeSurfaceMemoryLocked(w, "size");
8718 }
8719 }
8720 }
8721 }
8722 if (!w.mAppFreezing) {
8723 w.mContentInsetsChanged =
8724 !w.mLastContentInsets.equals(w.mContentInsets);
8725 w.mVisibleInsetsChanged =
8726 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008727 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008728 || w.mContentInsetsChanged
8729 || w.mVisibleInsetsChanged) {
8730 w.mLastFrame.set(w.mFrame);
8731 w.mLastContentInsets.set(w.mContentInsets);
8732 w.mLastVisibleInsets.set(w.mVisibleInsets);
8733 // If the orientation is changing, then we need to
8734 // hold off on unfreezing the display until this
8735 // window has been redrawn; to do that, we need
8736 // to go through the process of getting informed
8737 // by the application when it has finished drawing.
8738 if (w.mOrientationChanging) {
8739 if (DEBUG_ORIENTATION) Log.v(TAG,
8740 "Orientation start waiting for draw in "
8741 + w + ", surface " + w.mSurface);
8742 w.mDrawPending = true;
8743 w.mCommitDrawPending = false;
8744 w.mReadyToShow = false;
8745 if (w.mAppToken != null) {
8746 w.mAppToken.allDrawn = false;
8747 }
8748 }
Romain Guy06882f82009-06-10 13:36:04 -07008749 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008750 "Resizing window " + w + " to " + w.mFrame);
8751 mResizingWindows.add(w);
8752 } else if (w.mOrientationChanging) {
8753 if (!w.mDrawPending && !w.mCommitDrawPending) {
8754 if (DEBUG_ORIENTATION) Log.v(TAG,
8755 "Orientation not waiting for draw in "
8756 + w + ", surface " + w.mSurface);
8757 w.mOrientationChanging = false;
8758 }
8759 }
8760 }
8761
8762 if (w.mAttachedHidden) {
8763 if (!w.mLastHidden) {
8764 //dump();
8765 w.mLastHidden = true;
8766 if (SHOW_TRANSACTIONS) Log.i(
8767 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8768 if (w.mSurface != null) {
8769 try {
8770 w.mSurface.hide();
8771 } catch (RuntimeException e) {
8772 Log.w(TAG, "Exception hiding surface in " + w);
8773 }
8774 }
8775 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8776 }
8777 // If we are waiting for this window to handle an
8778 // orientation change, well, it is hidden, so
8779 // doesn't really matter. Note that this does
8780 // introduce a potential glitch if the window
8781 // becomes unhidden before it has drawn for the
8782 // new orientation.
8783 if (w.mOrientationChanging) {
8784 w.mOrientationChanging = false;
8785 if (DEBUG_ORIENTATION) Log.v(TAG,
8786 "Orientation change skips hidden " + w);
8787 }
8788 } else if (!w.isReadyForDisplay()) {
8789 if (!w.mLastHidden) {
8790 //dump();
8791 w.mLastHidden = true;
8792 if (SHOW_TRANSACTIONS) Log.i(
8793 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8794 if (w.mSurface != null) {
8795 try {
8796 w.mSurface.hide();
8797 } catch (RuntimeException e) {
8798 Log.w(TAG, "Exception exception hiding surface in " + w);
8799 }
8800 }
8801 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8802 }
8803 // If we are waiting for this window to handle an
8804 // orientation change, well, it is hidden, so
8805 // doesn't really matter. Note that this does
8806 // introduce a potential glitch if the window
8807 // becomes unhidden before it has drawn for the
8808 // new orientation.
8809 if (w.mOrientationChanging) {
8810 w.mOrientationChanging = false;
8811 if (DEBUG_ORIENTATION) Log.v(TAG,
8812 "Orientation change skips hidden " + w);
8813 }
8814 } else if (w.mLastLayer != w.mAnimLayer
8815 || w.mLastAlpha != w.mShownAlpha
8816 || w.mLastDsDx != w.mDsDx
8817 || w.mLastDtDx != w.mDtDx
8818 || w.mLastDsDy != w.mDsDy
8819 || w.mLastDtDy != w.mDtDy
8820 || w.mLastHScale != w.mHScale
8821 || w.mLastVScale != w.mVScale
8822 || w.mLastHidden) {
8823 displayed = true;
8824 w.mLastAlpha = w.mShownAlpha;
8825 w.mLastLayer = w.mAnimLayer;
8826 w.mLastDsDx = w.mDsDx;
8827 w.mLastDtDx = w.mDtDx;
8828 w.mLastDsDy = w.mDsDy;
8829 w.mLastDtDy = w.mDtDy;
8830 w.mLastHScale = w.mHScale;
8831 w.mLastVScale = w.mVScale;
8832 if (SHOW_TRANSACTIONS) Log.i(
8833 TAG, " SURFACE " + w.mSurface + ": alpha="
8834 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8835 if (w.mSurface != null) {
8836 try {
8837 w.mSurface.setAlpha(w.mShownAlpha);
8838 w.mSurface.setLayer(w.mAnimLayer);
8839 w.mSurface.setMatrix(
8840 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8841 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8842 } catch (RuntimeException e) {
8843 Log.w(TAG, "Error updating surface in " + w, e);
8844 if (!recoveringMemory) {
8845 reclaimSomeSurfaceMemoryLocked(w, "update");
8846 }
8847 }
8848 }
8849
8850 if (w.mLastHidden && !w.mDrawPending
8851 && !w.mCommitDrawPending
8852 && !w.mReadyToShow) {
8853 if (SHOW_TRANSACTIONS) Log.i(
8854 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8855 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8856 + " during relayout");
8857 if (showSurfaceRobustlyLocked(w)) {
8858 w.mHasDrawn = true;
8859 w.mLastHidden = false;
8860 } else {
8861 w.mOrientationChanging = false;
8862 }
8863 }
8864 if (w.mSurface != null) {
8865 w.mToken.hasVisible = true;
8866 }
8867 } else {
8868 displayed = true;
8869 }
8870
8871 if (displayed) {
8872 if (!covered) {
8873 if (attrs.width == LayoutParams.FILL_PARENT
8874 && attrs.height == LayoutParams.FILL_PARENT) {
8875 covered = true;
8876 }
8877 }
8878 if (w.mOrientationChanging) {
8879 if (w.mDrawPending || w.mCommitDrawPending) {
8880 orientationChangeComplete = false;
8881 if (DEBUG_ORIENTATION) Log.v(TAG,
8882 "Orientation continue waiting for draw in " + w);
8883 } else {
8884 w.mOrientationChanging = false;
8885 if (DEBUG_ORIENTATION) Log.v(TAG,
8886 "Orientation change complete in " + w);
8887 }
8888 }
8889 w.mToken.hasVisible = true;
8890 }
8891 } else if (w.mOrientationChanging) {
8892 if (DEBUG_ORIENTATION) Log.v(TAG,
8893 "Orientation change skips hidden " + w);
8894 w.mOrientationChanging = false;
8895 }
8896
8897 final boolean canBeSeen = w.isDisplayedLw();
8898
8899 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8900 focusDisplayed = true;
8901 }
8902
8903 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008904 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008905 if (w.mSurface != null) {
8906 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8907 holdScreen = w.mSession;
8908 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008909 if (!syswin && w.mAttrs.screenBrightness >= 0
8910 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008911 screenBrightness = w.mAttrs.screenBrightness;
8912 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008913 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8914 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8915 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8916 syswin = true;
8917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008918 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008919
8920 boolean opaqueDrawn = w.isOpaqueDrawn();
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008921 if ((opaqueDrawn && w.isFullscreen(dw, dh))
8922 || attrs.type == TYPE_WALLPAPER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 // This window completely covers everything behind it,
8924 // so we want to leave all of them as unblurred (for
8925 // performance reasons).
8926 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008927 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8928 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008929 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008930 obscured = true;
8931 if (mBackgroundFillerSurface == null) {
8932 try {
8933 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8934 0, dw, dh,
8935 PixelFormat.OPAQUE,
8936 Surface.FX_SURFACE_NORMAL);
8937 } catch (Exception e) {
8938 Log.e(TAG, "Exception creating filler surface", e);
8939 }
8940 }
8941 try {
8942 mBackgroundFillerSurface.setPosition(0, 0);
8943 mBackgroundFillerSurface.setSize(dw, dh);
8944 // Using the same layer as Dim because they will never be shown at the
8945 // same time.
8946 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8947 mBackgroundFillerSurface.show();
8948 } catch (RuntimeException e) {
8949 Log.e(TAG, "Exception showing filler surface");
8950 }
8951 backgroundFillerShown = true;
8952 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008953 } else if (canBeSeen && !obscured &&
8954 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8955 if (localLOGV) Log.v(TAG, "Win " + w
8956 + ": blurring=" + blurring
8957 + " obscured=" + obscured
8958 + " displayed=" + displayed);
8959 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8960 if (!dimming) {
8961 //Log.i(TAG, "DIM BEHIND: " + w);
8962 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008963 if (mDimAnimator == null) {
8964 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008965 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008966 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008967 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008968 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008969 }
8970 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8971 if (!blurring) {
8972 //Log.i(TAG, "BLUR BEHIND: " + w);
8973 blurring = true;
8974 mBlurShown = true;
8975 if (mBlurSurface == null) {
8976 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8977 + mBlurSurface + ": CREATE");
8978 try {
Romain Guy06882f82009-06-10 13:36:04 -07008979 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008980 -1, 16, 16,
8981 PixelFormat.OPAQUE,
8982 Surface.FX_SURFACE_BLUR);
8983 } catch (Exception e) {
8984 Log.e(TAG, "Exception creating Blur surface", e);
8985 }
8986 }
8987 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8988 + mBlurSurface + ": SHOW pos=(0,0) (" +
8989 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8990 if (mBlurSurface != null) {
8991 mBlurSurface.setPosition(0, 0);
8992 mBlurSurface.setSize(dw, dh);
8993 try {
8994 mBlurSurface.show();
8995 } catch (RuntimeException e) {
8996 Log.w(TAG, "Failure showing blur surface", e);
8997 }
8998 }
8999 }
9000 mBlurSurface.setLayer(w.mAnimLayer-2);
9001 }
9002 }
9003 }
9004 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009005
9006 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9007 mBackgroundFillerShown = false;
9008 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9009 try {
9010 mBackgroundFillerSurface.hide();
9011 } catch (RuntimeException e) {
9012 Log.e(TAG, "Exception hiding filler surface", e);
9013 }
9014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009015
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009016 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9017 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009018 }
Romain Guy06882f82009-06-10 13:36:04 -07009019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009020 if (!blurring && mBlurShown) {
9021 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9022 + ": HIDE");
9023 try {
9024 mBlurSurface.hide();
9025 } catch (IllegalArgumentException e) {
9026 Log.w(TAG, "Illegal argument exception hiding blur surface");
9027 }
9028 mBlurShown = false;
9029 }
9030
9031 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9032 } catch (RuntimeException e) {
9033 Log.e(TAG, "Unhandled exception in Window Manager", e);
9034 }
9035
9036 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009038 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9039 "With display frozen, orientationChangeComplete="
9040 + orientationChangeComplete);
9041 if (orientationChangeComplete) {
9042 if (mWindowsFreezingScreen) {
9043 mWindowsFreezingScreen = false;
9044 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9045 }
9046 if (mAppsFreezingScreen == 0) {
9047 stopFreezingDisplayLocked();
9048 }
9049 }
Romain Guy06882f82009-06-10 13:36:04 -07009050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009051 i = mResizingWindows.size();
9052 if (i > 0) {
9053 do {
9054 i--;
9055 WindowState win = mResizingWindows.get(i);
9056 try {
9057 win.mClient.resized(win.mFrame.width(),
9058 win.mFrame.height(), win.mLastContentInsets,
9059 win.mLastVisibleInsets, win.mDrawPending);
9060 win.mContentInsetsChanged = false;
9061 win.mVisibleInsetsChanged = false;
9062 } catch (RemoteException e) {
9063 win.mOrientationChanging = false;
9064 }
9065 } while (i > 0);
9066 mResizingWindows.clear();
9067 }
Romain Guy06882f82009-06-10 13:36:04 -07009068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009069 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009070 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009071 i = mDestroySurface.size();
9072 if (i > 0) {
9073 do {
9074 i--;
9075 WindowState win = mDestroySurface.get(i);
9076 win.mDestroying = false;
9077 if (mInputMethodWindow == win) {
9078 mInputMethodWindow = null;
9079 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009080 if (win == mWallpaperTarget) {
9081 wallpaperDestroyed = true;
9082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009083 win.destroySurfaceLocked();
9084 } while (i > 0);
9085 mDestroySurface.clear();
9086 }
9087
9088 // Time to remove any exiting tokens?
9089 for (i=mExitingTokens.size()-1; i>=0; i--) {
9090 WindowToken token = mExitingTokens.get(i);
9091 if (!token.hasVisible) {
9092 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009093 if (token.windowType == TYPE_WALLPAPER) {
9094 mWallpaperTokens.remove(token);
9095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009096 }
9097 }
9098
9099 // Time to remove any exiting applications?
9100 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9101 AppWindowToken token = mExitingAppTokens.get(i);
9102 if (!token.hasVisible && !mClosingApps.contains(token)) {
9103 mAppTokens.remove(token);
9104 mExitingAppTokens.remove(i);
9105 }
9106 }
9107
9108 if (focusDisplayed) {
9109 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9110 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009111 if (wallpaperDestroyed) {
9112 wallpaperDestroyed = adjustWallpaperWindowsLocked();
9113 }
9114 if (wallpaperDestroyed) {
9115 requestAnimationLocked(0);
9116 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009117 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9118 }
9119 mQueue.setHoldScreenLocked(holdScreen != null);
9120 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9121 mPowerManager.setScreenBrightnessOverride(-1);
9122 } else {
9123 mPowerManager.setScreenBrightnessOverride((int)
9124 (screenBrightness * Power.BRIGHTNESS_ON));
9125 }
9126 if (holdScreen != mHoldingScreenOn) {
9127 mHoldingScreenOn = holdScreen;
9128 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9129 mH.sendMessage(m);
9130 }
9131 }
9132
9133 void requestAnimationLocked(long delay) {
9134 if (!mAnimationPending) {
9135 mAnimationPending = true;
9136 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9137 }
9138 }
Romain Guy06882f82009-06-10 13:36:04 -07009139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009140 /**
9141 * Have the surface flinger show a surface, robustly dealing with
9142 * error conditions. In particular, if there is not enough memory
9143 * to show the surface, then we will try to get rid of other surfaces
9144 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009145 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009146 * @return Returns true if the surface was successfully shown.
9147 */
9148 boolean showSurfaceRobustlyLocked(WindowState win) {
9149 try {
9150 if (win.mSurface != null) {
9151 win.mSurface.show();
9152 }
9153 return true;
9154 } catch (RuntimeException e) {
9155 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9156 }
Romain Guy06882f82009-06-10 13:36:04 -07009157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009158 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009160 return false;
9161 }
Romain Guy06882f82009-06-10 13:36:04 -07009162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009163 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9164 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009166 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9167 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009169 if (mForceRemoves == null) {
9170 mForceRemoves = new ArrayList<WindowState>();
9171 }
Romain Guy06882f82009-06-10 13:36:04 -07009172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 long callingIdentity = Binder.clearCallingIdentity();
9174 try {
9175 // There was some problem... first, do a sanity check of the
9176 // window list to make sure we haven't left any dangling surfaces
9177 // around.
9178 int N = mWindows.size();
9179 boolean leakedSurface = false;
9180 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9181 for (int i=0; i<N; i++) {
9182 WindowState ws = (WindowState)mWindows.get(i);
9183 if (ws.mSurface != null) {
9184 if (!mSessions.contains(ws.mSession)) {
9185 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9186 + ws + " surface=" + ws.mSurface
9187 + " token=" + win.mToken
9188 + " pid=" + ws.mSession.mPid
9189 + " uid=" + ws.mSession.mUid);
9190 ws.mSurface.clear();
9191 ws.mSurface = null;
9192 mForceRemoves.add(ws);
9193 i--;
9194 N--;
9195 leakedSurface = true;
9196 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9197 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9198 + ws + " surface=" + ws.mSurface
9199 + " token=" + win.mAppToken);
9200 ws.mSurface.clear();
9201 ws.mSurface = null;
9202 leakedSurface = true;
9203 }
9204 }
9205 }
Romain Guy06882f82009-06-10 13:36:04 -07009206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009207 boolean killedApps = false;
9208 if (!leakedSurface) {
9209 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9210 SparseIntArray pidCandidates = new SparseIntArray();
9211 for (int i=0; i<N; i++) {
9212 WindowState ws = (WindowState)mWindows.get(i);
9213 if (ws.mSurface != null) {
9214 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9215 }
9216 }
9217 if (pidCandidates.size() > 0) {
9218 int[] pids = new int[pidCandidates.size()];
9219 for (int i=0; i<pids.length; i++) {
9220 pids[i] = pidCandidates.keyAt(i);
9221 }
9222 try {
9223 if (mActivityManager.killPidsForMemory(pids)) {
9224 killedApps = true;
9225 }
9226 } catch (RemoteException e) {
9227 }
9228 }
9229 }
Romain Guy06882f82009-06-10 13:36:04 -07009230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009231 if (leakedSurface || killedApps) {
9232 // We managed to reclaim some memory, so get rid of the trouble
9233 // surface and ask the app to request another one.
9234 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9235 if (surface != null) {
9236 surface.clear();
9237 win.mSurface = null;
9238 }
Romain Guy06882f82009-06-10 13:36:04 -07009239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009240 try {
9241 win.mClient.dispatchGetNewSurface();
9242 } catch (RemoteException e) {
9243 }
9244 }
9245 } finally {
9246 Binder.restoreCallingIdentity(callingIdentity);
9247 }
9248 }
Romain Guy06882f82009-06-10 13:36:04 -07009249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009250 private boolean updateFocusedWindowLocked(int mode) {
9251 WindowState newFocus = computeFocusedWindowLocked();
9252 if (mCurrentFocus != newFocus) {
9253 // This check makes sure that we don't already have the focus
9254 // change message pending.
9255 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9256 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9257 if (localLOGV) Log.v(
9258 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9259 final WindowState oldFocus = mCurrentFocus;
9260 mCurrentFocus = newFocus;
9261 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009263 final WindowState imWindow = mInputMethodWindow;
9264 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009265 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009267 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9268 mLayoutNeeded = true;
9269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009270 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9271 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009272 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9273 // Client will do the layout, but we need to assign layers
9274 // for handleNewWindowLocked() below.
9275 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009276 }
9277 }
Romain Guy06882f82009-06-10 13:36:04 -07009278
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009279 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9280 mKeyWaiter.handleNewWindowLocked(newFocus);
9281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009282 return true;
9283 }
9284 return false;
9285 }
9286
9287 private WindowState computeFocusedWindowLocked() {
9288 WindowState result = null;
9289 WindowState win;
9290
9291 int i = mWindows.size() - 1;
9292 int nextAppIndex = mAppTokens.size()-1;
9293 WindowToken nextApp = nextAppIndex >= 0
9294 ? mAppTokens.get(nextAppIndex) : null;
9295
9296 while (i >= 0) {
9297 win = (WindowState)mWindows.get(i);
9298
9299 if (localLOGV || DEBUG_FOCUS) Log.v(
9300 TAG, "Looking for focus: " + i
9301 + " = " + win
9302 + ", flags=" + win.mAttrs.flags
9303 + ", canReceive=" + win.canReceiveKeys());
9304
9305 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009307 // If this window's application has been removed, just skip it.
9308 if (thisApp != null && thisApp.removed) {
9309 i--;
9310 continue;
9311 }
Romain Guy06882f82009-06-10 13:36:04 -07009312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009313 // If there is a focused app, don't allow focus to go to any
9314 // windows below it. If this is an application window, step
9315 // through the app tokens until we find its app.
9316 if (thisApp != null && nextApp != null && thisApp != nextApp
9317 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9318 int origAppIndex = nextAppIndex;
9319 while (nextAppIndex > 0) {
9320 if (nextApp == mFocusedApp) {
9321 // Whoops, we are below the focused app... no focus
9322 // for you!
9323 if (localLOGV || DEBUG_FOCUS) Log.v(
9324 TAG, "Reached focused app: " + mFocusedApp);
9325 return null;
9326 }
9327 nextAppIndex--;
9328 nextApp = mAppTokens.get(nextAppIndex);
9329 if (nextApp == thisApp) {
9330 break;
9331 }
9332 }
9333 if (thisApp != nextApp) {
9334 // Uh oh, the app token doesn't exist! This shouldn't
9335 // happen, but if it does we can get totally hosed...
9336 // so restart at the original app.
9337 nextAppIndex = origAppIndex;
9338 nextApp = mAppTokens.get(nextAppIndex);
9339 }
9340 }
9341
9342 // Dispatch to this window if it is wants key events.
9343 if (win.canReceiveKeys()) {
9344 if (DEBUG_FOCUS) Log.v(
9345 TAG, "Found focus @ " + i + " = " + win);
9346 result = win;
9347 break;
9348 }
9349
9350 i--;
9351 }
9352
9353 return result;
9354 }
9355
9356 private void startFreezingDisplayLocked() {
9357 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009358 // Freezing the display also suspends key event delivery, to
9359 // keep events from going astray while the display is reconfigured.
9360 // If someone has changed orientation again while the screen is
9361 // still frozen, the events will continue to be blocked while the
9362 // successive orientation change is processed. To prevent spurious
9363 // ANRs, we reset the event dispatch timeout in this case.
9364 synchronized (mKeyWaiter) {
9365 mKeyWaiter.mWasFrozen = true;
9366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009367 return;
9368 }
Romain Guy06882f82009-06-10 13:36:04 -07009369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009370 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009372 long now = SystemClock.uptimeMillis();
9373 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9374 if (mFreezeGcPending != 0) {
9375 if (now > (mFreezeGcPending+1000)) {
9376 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9377 mH.removeMessages(H.FORCE_GC);
9378 Runtime.getRuntime().gc();
9379 mFreezeGcPending = now;
9380 }
9381 } else {
9382 mFreezeGcPending = now;
9383 }
Romain Guy06882f82009-06-10 13:36:04 -07009384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009385 mDisplayFrozen = true;
9386 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9387 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9388 mAppTransitionReady = true;
9389 }
Romain Guy06882f82009-06-10 13:36:04 -07009390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009391 if (PROFILE_ORIENTATION) {
9392 File file = new File("/data/system/frozen");
9393 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9394 }
9395 Surface.freezeDisplay(0);
9396 }
Romain Guy06882f82009-06-10 13:36:04 -07009397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009398 private void stopFreezingDisplayLocked() {
9399 if (!mDisplayFrozen) {
9400 return;
9401 }
Romain Guy06882f82009-06-10 13:36:04 -07009402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009403 mDisplayFrozen = false;
9404 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9405 if (PROFILE_ORIENTATION) {
9406 Debug.stopMethodTracing();
9407 }
9408 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009409
Chris Tate2ad63a92009-03-25 17:36:48 -07009410 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9411 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009412 synchronized (mKeyWaiter) {
9413 mKeyWaiter.mWasFrozen = true;
9414 mKeyWaiter.notifyAll();
9415 }
9416
9417 // A little kludge: a lot could have happened while the
9418 // display was frozen, so now that we are coming back we
9419 // do a gc so that any remote references the system
9420 // processes holds on others can be released if they are
9421 // no longer needed.
9422 mH.removeMessages(H.FORCE_GC);
9423 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9424 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009426 mScreenFrozenLock.release();
9427 }
Romain Guy06882f82009-06-10 13:36:04 -07009428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009429 @Override
9430 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9431 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9432 != PackageManager.PERMISSION_GRANTED) {
9433 pw.println("Permission Denial: can't dump WindowManager from from pid="
9434 + Binder.getCallingPid()
9435 + ", uid=" + Binder.getCallingUid());
9436 return;
9437 }
Romain Guy06882f82009-06-10 13:36:04 -07009438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009439 synchronized(mWindowMap) {
9440 pw.println("Current Window Manager state:");
9441 for (int i=mWindows.size()-1; i>=0; i--) {
9442 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009443 pw.print(" Window #"); pw.print(i); pw.print(' ');
9444 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009445 w.dump(pw, " ");
9446 }
9447 if (mInputMethodDialogs.size() > 0) {
9448 pw.println(" ");
9449 pw.println(" Input method dialogs:");
9450 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9451 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009452 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009453 }
9454 }
9455 if (mPendingRemove.size() > 0) {
9456 pw.println(" ");
9457 pw.println(" Remove pending for:");
9458 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9459 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009460 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9461 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009462 w.dump(pw, " ");
9463 }
9464 }
9465 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9466 pw.println(" ");
9467 pw.println(" Windows force removing:");
9468 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9469 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009470 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9471 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009472 w.dump(pw, " ");
9473 }
9474 }
9475 if (mDestroySurface.size() > 0) {
9476 pw.println(" ");
9477 pw.println(" Windows waiting to destroy their surface:");
9478 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9479 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009480 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9481 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009482 w.dump(pw, " ");
9483 }
9484 }
9485 if (mLosingFocus.size() > 0) {
9486 pw.println(" ");
9487 pw.println(" Windows losing focus:");
9488 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9489 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009490 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9491 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009492 w.dump(pw, " ");
9493 }
9494 }
9495 if (mSessions.size() > 0) {
9496 pw.println(" ");
9497 pw.println(" All active sessions:");
9498 Iterator<Session> it = mSessions.iterator();
9499 while (it.hasNext()) {
9500 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009501 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009502 s.dump(pw, " ");
9503 }
9504 }
9505 if (mTokenMap.size() > 0) {
9506 pw.println(" ");
9507 pw.println(" All tokens:");
9508 Iterator<WindowToken> it = mTokenMap.values().iterator();
9509 while (it.hasNext()) {
9510 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009511 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009512 token.dump(pw, " ");
9513 }
9514 }
9515 if (mTokenList.size() > 0) {
9516 pw.println(" ");
9517 pw.println(" Window token list:");
9518 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009519 pw.print(" #"); pw.print(i); pw.print(": ");
9520 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009521 }
9522 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009523 if (mWallpaperTokens.size() > 0) {
9524 pw.println(" ");
9525 pw.println(" Wallpaper tokens:");
9526 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
9527 WindowToken token = mWallpaperTokens.get(i);
9528 pw.print(" Wallpaper #"); pw.print(i);
9529 pw.print(' '); pw.print(token); pw.println(':');
9530 token.dump(pw, " ");
9531 }
9532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009533 if (mAppTokens.size() > 0) {
9534 pw.println(" ");
9535 pw.println(" Application tokens in Z order:");
9536 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009537 pw.print(" App #"); pw.print(i); pw.print(": ");
9538 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 }
9540 }
9541 if (mFinishedStarting.size() > 0) {
9542 pw.println(" ");
9543 pw.println(" Finishing start of application tokens:");
9544 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9545 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009546 pw.print(" Finished Starting #"); pw.print(i);
9547 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009548 token.dump(pw, " ");
9549 }
9550 }
9551 if (mExitingTokens.size() > 0) {
9552 pw.println(" ");
9553 pw.println(" Exiting tokens:");
9554 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9555 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009556 pw.print(" Exiting #"); pw.print(i);
9557 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009558 token.dump(pw, " ");
9559 }
9560 }
9561 if (mExitingAppTokens.size() > 0) {
9562 pw.println(" ");
9563 pw.println(" Exiting application tokens:");
9564 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9565 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009566 pw.print(" Exiting App #"); pw.print(i);
9567 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009568 token.dump(pw, " ");
9569 }
9570 }
9571 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009572 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9573 pw.print(" mLastFocus="); pw.println(mLastFocus);
9574 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9575 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9576 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -07009577 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009578 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9579 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9580 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9581 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9582 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009583 if (mDimAnimator != null) {
9584 mDimAnimator.printTo(pw);
9585 } else {
9586 pw.print( " no DimAnimator ");
9587 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009588 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009589 pw.print(mInputMethodAnimLayerAdjustment);
9590 pw.print(" mWallpaperAnimLayerAdjustment=");
9591 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009592 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9593 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9594 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9595 pw.print(" mRotation="); pw.print(mRotation);
9596 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9597 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9598 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9599 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9600 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9601 pw.print(" mNextAppTransition=0x");
9602 pw.print(Integer.toHexString(mNextAppTransition));
9603 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9604 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9605 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9606 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9607 if (mOpeningApps.size() > 0) {
9608 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9609 }
9610 if (mClosingApps.size() > 0) {
9611 pw.print(" mClosingApps="); pw.println(mClosingApps);
9612 }
9613 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9614 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009615 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009616 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9617 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9618 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9619 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9620 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9621 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009622 }
9623 }
9624
9625 public void monitor() {
9626 synchronized (mWindowMap) { }
9627 synchronized (mKeyguardDisabled) { }
9628 synchronized (mKeyWaiter) { }
9629 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009630
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07009631 public void virtualKeyFeedback(KeyEvent event) {
9632 mPolicy.keyFeedbackFromInput(event);
9633 }
9634
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009635 /**
9636 * DimAnimator class that controls the dim animation. This holds the surface and
9637 * all state used for dim animation.
9638 */
9639 private static class DimAnimator {
9640 Surface mDimSurface;
9641 boolean mDimShown = false;
9642 float mDimCurrentAlpha;
9643 float mDimTargetAlpha;
9644 float mDimDeltaPerMs;
9645 long mLastDimAnimTime;
9646
9647 DimAnimator (SurfaceSession session) {
9648 if (mDimSurface == null) {
9649 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9650 + mDimSurface + ": CREATE");
9651 try {
9652 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9653 Surface.FX_SURFACE_DIM);
9654 } catch (Exception e) {
9655 Log.e(TAG, "Exception creating Dim surface", e);
9656 }
9657 }
9658 }
9659
9660 /**
9661 * Show the dim surface.
9662 */
9663 void show(int dw, int dh) {
9664 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9665 dw + "x" + dh + ")");
9666 mDimShown = true;
9667 try {
9668 mDimSurface.setPosition(0, 0);
9669 mDimSurface.setSize(dw, dh);
9670 mDimSurface.show();
9671 } catch (RuntimeException e) {
9672 Log.w(TAG, "Failure showing dim surface", e);
9673 }
9674 }
9675
9676 /**
9677 * Set's the dim surface's layer and update dim parameters that will be used in
9678 * {@link updateSurface} after all windows are examined.
9679 */
9680 void updateParameters(WindowState w, long currentTime) {
9681 mDimSurface.setLayer(w.mAnimLayer-1);
9682
9683 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9684 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9685 if (mDimTargetAlpha != target) {
9686 // If the desired dim level has changed, then
9687 // start an animation to it.
9688 mLastDimAnimTime = currentTime;
9689 long duration = (w.mAnimating && w.mAnimation != null)
9690 ? w.mAnimation.computeDurationHint()
9691 : DEFAULT_DIM_DURATION;
9692 if (target > mDimTargetAlpha) {
9693 // This is happening behind the activity UI,
9694 // so we can make it run a little longer to
9695 // give a stronger impression without disrupting
9696 // the user.
9697 duration *= DIM_DURATION_MULTIPLIER;
9698 }
9699 if (duration < 1) {
9700 // Don't divide by zero
9701 duration = 1;
9702 }
9703 mDimTargetAlpha = target;
9704 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9705 }
9706 }
9707
9708 /**
9709 * Updating the surface's alpha. Returns true if the animation continues, or returns
9710 * false when the animation is finished and the dim surface is hidden.
9711 */
9712 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9713 if (!dimming) {
9714 if (mDimTargetAlpha != 0) {
9715 mLastDimAnimTime = currentTime;
9716 mDimTargetAlpha = 0;
9717 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9718 }
9719 }
9720
9721 boolean animating = false;
9722 if (mLastDimAnimTime != 0) {
9723 mDimCurrentAlpha += mDimDeltaPerMs
9724 * (currentTime-mLastDimAnimTime);
9725 boolean more = true;
9726 if (displayFrozen) {
9727 // If the display is frozen, there is no reason to animate.
9728 more = false;
9729 } else if (mDimDeltaPerMs > 0) {
9730 if (mDimCurrentAlpha > mDimTargetAlpha) {
9731 more = false;
9732 }
9733 } else if (mDimDeltaPerMs < 0) {
9734 if (mDimCurrentAlpha < mDimTargetAlpha) {
9735 more = false;
9736 }
9737 } else {
9738 more = false;
9739 }
9740
9741 // Do we need to continue animating?
9742 if (more) {
9743 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9744 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9745 mLastDimAnimTime = currentTime;
9746 mDimSurface.setAlpha(mDimCurrentAlpha);
9747 animating = true;
9748 } else {
9749 mDimCurrentAlpha = mDimTargetAlpha;
9750 mLastDimAnimTime = 0;
9751 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9752 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9753 mDimSurface.setAlpha(mDimCurrentAlpha);
9754 if (!dimming) {
9755 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9756 + ": HIDE");
9757 try {
9758 mDimSurface.hide();
9759 } catch (RuntimeException e) {
9760 Log.w(TAG, "Illegal argument exception hiding dim surface");
9761 }
9762 mDimShown = false;
9763 }
9764 }
9765 }
9766 return animating;
9767 }
9768
9769 public void printTo(PrintWriter pw) {
9770 pw.print(" mDimShown="); pw.print(mDimShown);
9771 pw.print(" current="); pw.print(mDimCurrentAlpha);
9772 pw.print(" target="); pw.print(mDimTargetAlpha);
9773 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9774 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9775 }
9776 }
9777
9778 /**
9779 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9780 * This is used for opening/closing transition for apps in compatible mode.
9781 */
9782 private static class FadeInOutAnimation extends Animation {
9783 int mWidth;
9784 boolean mFadeIn;
9785
9786 public FadeInOutAnimation(boolean fadeIn) {
9787 setInterpolator(new AccelerateInterpolator());
9788 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9789 mFadeIn = fadeIn;
9790 }
9791
9792 @Override
9793 protected void applyTransformation(float interpolatedTime, Transformation t) {
9794 float x = interpolatedTime;
9795 if (!mFadeIn) {
9796 x = 1.0f - x; // reverse the interpolation for fade out
9797 }
9798 if (x < 0.5) {
9799 // move the window out of the screen.
9800 t.getMatrix().setTranslate(mWidth, 0);
9801 } else {
9802 t.getMatrix().setTranslate(0, 0);// show
9803 t.setAlpha((x - 0.5f) * 2);
9804 }
9805 }
9806
9807 @Override
9808 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9809 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9810 mWidth = width;
9811 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009812
9813 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07009814 public int getZAdjustment() {
9815 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009816 }
9817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818}