blob: b43acaf5bf6cb972af62783d3e6af642ad758d5d [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;
34import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
35import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
36import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_GPU;
37import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_HARDWARE;
38import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
39import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
40import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
41import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
42import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
43
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} */
128public class WindowManagerService extends IWindowManager.Stub implements Watchdog.Monitor {
129 static final String TAG = "WindowManager";
130 static final boolean DEBUG = false;
131 static final boolean DEBUG_FOCUS = false;
132 static final boolean DEBUG_ANIM = false;
133 static final boolean DEBUG_LAYERS = false;
134 static final boolean DEBUG_INPUT = false;
135 static final boolean DEBUG_INPUT_METHOD = false;
136 static final boolean DEBUG_VISIBILITY = false;
137 static final boolean DEBUG_ORIENTATION = false;
138 static final boolean DEBUG_APP_TRANSITIONS = false;
139 static final boolean DEBUG_STARTING_WINDOW = false;
140 static final boolean DEBUG_REORDER = false;
141 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700142 static final boolean MEASURE_LATENCY = false;
143 static private LatencyTimer lt;
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final boolean PROFILE_ORIENTATION = false;
146 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700147 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /** How long to wait for first key repeat, in milliseconds */
152 static final int KEY_REPEAT_FIRST_DELAY = 750;
Romain Guy06882f82009-06-10 13:36:04 -0700153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /** How long to wait for subsequent key repeats, in milliseconds */
155 static final int KEY_REPEAT_DELAY = 50;
156
157 /** How much to multiply the policy's type layer, to reserve room
158 * for multiple windows of the same type and Z-ordering adjustment
159 * with TYPE_LAYER_OFFSET. */
160 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
163 * or below others in the same layer. */
164 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 /** How much to increment the layer for each window, to reserve room
167 * for effect surfaces between them.
168 */
169 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 /** The maximum length we will accept for a loaded animation duration:
172 * this is 10 seconds.
173 */
174 static final int MAX_ANIMATION_DURATION = 10*1000;
175
176 /** Amount of time (in milliseconds) to animate the dim surface from one
177 * value to another, when no window animation is driving it.
178 */
179 static final int DEFAULT_DIM_DURATION = 200;
180
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700181 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
182 * compatible windows.
183 */
184 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 /** Adjustment to time to perform a dim, to make it more dramatic.
187 */
188 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700189
Dianne Hackborncfaef692009-06-15 14:24:44 -0700190 static final int INJECT_FAILED = 0;
191 static final int INJECT_SUCCEEDED = 1;
192 static final int INJECT_NO_PERMISSION = -1;
193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 static final int UPDATE_FOCUS_NORMAL = 0;
195 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
196 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
197 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700198
Michael Chane96440f2009-05-06 10:27:36 -0700199 /** The minimum time between dispatching touch events. */
200 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
201
202 // Last touch event time
203 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700204
Michael Chane96440f2009-05-06 10:27:36 -0700205 // Last touch event type
206 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700207
Michael Chane96440f2009-05-06 10:27:36 -0700208 // Time to wait before calling useractivity again. This saves CPU usage
209 // when we get a flood of touch events.
210 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
211
212 // Last time we call user activity
213 long mLastUserActivityCallTime = 0;
214
Romain Guy06882f82009-06-10 13:36:04 -0700215 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700216 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700219 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220
221 /**
222 * Condition waited on by {@link #reenableKeyguard} to know the call to
223 * the window policy has finished.
224 */
225 private boolean mWaitingUntilKeyguardReenabled = false;
226
227
228 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
229 new Handler(), "WindowManagerService.mKeyguardDisabled") {
230 public void acquired() {
231 mPolicy.enableKeyguard(false);
232 }
233 public void released() {
234 synchronized (mKeyguardDisabled) {
235 mPolicy.enableKeyguard(true);
236 mWaitingUntilKeyguardReenabled = false;
237 mKeyguardDisabled.notifyAll();
238 }
239 }
240 };
241
242 final Context mContext;
243
244 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
249
250 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 /**
255 * All currently active sessions with clients.
256 */
257 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 /**
260 * Mapping from an IWindow IBinder to the server's Window object.
261 * This is also used as the lock for all of our state.
262 */
263 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
264
265 /**
266 * Mapping from a token IBinder to a WindowToken object.
267 */
268 final HashMap<IBinder, WindowToken> mTokenMap =
269 new HashMap<IBinder, WindowToken>();
270
271 /**
272 * The same tokens as mTokenMap, stored in a list for efficient iteration
273 * over them.
274 */
275 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * Window tokens that are in the process of exiting, but still
279 * on screen for animations.
280 */
281 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
282
283 /**
284 * Z-ordered (bottom-most first) list of all application tokens, for
285 * controlling the ordering of windows in different applications. This
286 * contains WindowToken objects.
287 */
288 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
289
290 /**
291 * Application tokens that are in the process of exiting, but still
292 * on screen for animations.
293 */
294 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
295
296 /**
297 * List of window tokens that have finished starting their application,
298 * and now need to have the policy remove their windows.
299 */
300 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
301
302 /**
303 * Z-ordered (bottom-most first) list of all Window objects.
304 */
305 final ArrayList mWindows = new ArrayList();
306
307 /**
308 * Windows that are being resized. Used so we can tell the client about
309 * the resize after closing the transaction in which we resized the
310 * underlying surface.
311 */
312 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
313
314 /**
315 * Windows whose animations have ended and now must be removed.
316 */
317 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
318
319 /**
320 * Windows whose surface should be destroyed.
321 */
322 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
323
324 /**
325 * Windows that have lost input focus and are waiting for the new
326 * focus window to be displayed before they are told about this.
327 */
328 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
329
330 /**
331 * This is set when we have run out of memory, and will either be an empty
332 * list or contain windows that need to be force removed.
333 */
334 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700339 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 Surface mBlurSurface;
341 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 final float[] mTmpFloats = new float[9];
346
347 boolean mSafeMode;
348 boolean mDisplayEnabled = false;
349 boolean mSystemBooted = false;
350 int mRotation = 0;
351 int mRequestedRotation = 0;
352 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700353 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 ArrayList<IRotationWatcher> mRotationWatchers
355 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 boolean mLayoutNeeded = true;
358 boolean mAnimationPending = false;
359 boolean mDisplayFrozen = false;
360 boolean mWindowsFreezingScreen = false;
361 long mFreezeGcPending = 0;
362 int mAppsFreezingScreen = 0;
363
364 // This is held as long as we have the screen frozen, to give us time to
365 // perform a rotation animation when turning off shows the lock screen which
366 // changes the orientation.
367 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 // State management of app transitions. When we are preparing for a
370 // transition, mNextAppTransition will be the kind of transition to
371 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
372 // mOpeningApps and mClosingApps are the lists of tokens that will be
373 // made visible or hidden at the next transition.
374 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
375 boolean mAppTransitionReady = false;
376 boolean mAppTransitionTimeout = false;
377 boolean mStartingIconInTransition = false;
378 boolean mSkipAppTransitionAnimation = false;
379 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
380 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 //flag to detect fat touch events
383 boolean mFatTouch = false;
384 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 H mH = new H();
387
388 WindowState mCurrentFocus = null;
389 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 // This just indicates the window the input method is on top of, not
392 // necessarily the window its input is going to.
393 WindowState mInputMethodTarget = null;
394 WindowState mUpcomingInputMethodTarget = null;
395 boolean mInputMethodTargetWaitingAnim;
396 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 WindowState mInputMethodWindow = null;
399 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
400
401 AppWindowToken mFocusedApp = null;
402
403 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 float mWindowAnimationScale = 1.0f;
406 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 final KeyWaiter mKeyWaiter = new KeyWaiter();
409 final KeyQ mQueue;
410 final InputDispatcherThread mInputThread;
411
412 // Who is holding the screen on.
413 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 /**
416 * Whether the UI is currently running in touch mode (not showing
417 * navigational focus because the user is directly pressing the screen).
418 */
419 boolean mInTouchMode = false;
420
421 private ViewServer mViewServer;
422
423 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700424
Dianne Hackbornc485a602009-03-24 22:39:49 -0700425 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700426 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700427
428 // The frame use to limit the size of the app running in compatibility mode.
429 Rect mCompatibleScreenFrame = new Rect();
430 // The surface used to fill the outer rim of the app running in compatibility mode.
431 Surface mBackgroundFillerSurface = null;
432 boolean mBackgroundFillerShown = false;
433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 public static WindowManagerService main(Context context,
435 PowerManagerService pm, boolean haveInputMethods) {
436 WMThread thr = new WMThread(context, pm, haveInputMethods);
437 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 synchronized (thr) {
440 while (thr.mService == null) {
441 try {
442 thr.wait();
443 } catch (InterruptedException e) {
444 }
445 }
446 }
Romain Guy06882f82009-06-10 13:36:04 -0700447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 return thr.mService;
449 }
Romain Guy06882f82009-06-10 13:36:04 -0700450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 static class WMThread extends Thread {
452 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 private final Context mContext;
455 private final PowerManagerService mPM;
456 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 public WMThread(Context context, PowerManagerService pm,
459 boolean haveInputMethods) {
460 super("WindowManager");
461 mContext = context;
462 mPM = pm;
463 mHaveInputMethods = haveInputMethods;
464 }
Romain Guy06882f82009-06-10 13:36:04 -0700465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 public void run() {
467 Looper.prepare();
468 WindowManagerService s = new WindowManagerService(mContext, mPM,
469 mHaveInputMethods);
470 android.os.Process.setThreadPriority(
471 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 synchronized (this) {
474 mService = s;
475 notifyAll();
476 }
Romain Guy06882f82009-06-10 13:36:04 -0700477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 Looper.loop();
479 }
480 }
481
482 static class PolicyThread extends Thread {
483 private final WindowManagerPolicy mPolicy;
484 private final WindowManagerService mService;
485 private final Context mContext;
486 private final PowerManagerService mPM;
487 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 public PolicyThread(WindowManagerPolicy policy,
490 WindowManagerService service, Context context,
491 PowerManagerService pm) {
492 super("WindowManagerPolicy");
493 mPolicy = policy;
494 mService = service;
495 mContext = context;
496 mPM = pm;
497 }
Romain Guy06882f82009-06-10 13:36:04 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 public void run() {
500 Looper.prepare();
501 //Looper.myLooper().setMessageLogging(new LogPrinter(
502 // Log.VERBOSE, "WindowManagerPolicy"));
503 android.os.Process.setThreadPriority(
504 android.os.Process.THREAD_PRIORITY_FOREGROUND);
505 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 synchronized (this) {
508 mRunning = true;
509 notifyAll();
510 }
Romain Guy06882f82009-06-10 13:36:04 -0700511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 Looper.loop();
513 }
514 }
515
516 private WindowManagerService(Context context, PowerManagerService pm,
517 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700518 if (MEASURE_LATENCY) {
519 lt = new LatencyTimer(100, 1000);
520 }
521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 mContext = context;
523 mHaveInputMethods = haveInputMethods;
524 mLimitedAlphaCompositing = context.getResources().getBoolean(
525 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 mPowerManager = pm;
528 mPowerManager.setPolicy(mPolicy);
529 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
530 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
531 "SCREEN_FROZEN");
532 mScreenFrozenLock.setReferenceCounted(false);
533
534 mActivityManager = ActivityManagerNative.getDefault();
535 mBatteryStats = BatteryStatsService.getService();
536
537 // Get persisted window scale setting
538 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
539 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
540 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
541 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 mQueue = new KeyQ();
544
545 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
548 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 synchronized (thr) {
551 while (!thr.mRunning) {
552 try {
553 thr.wait();
554 } catch (InterruptedException e) {
555 }
556 }
557 }
Romain Guy06882f82009-06-10 13:36:04 -0700558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 // Add ourself to the Watchdog monitors.
562 Watchdog.getInstance().addMonitor(this);
563 }
564
565 @Override
566 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
567 throws RemoteException {
568 try {
569 return super.onTransact(code, data, reply, flags);
570 } catch (RuntimeException e) {
571 // The window manager only throws security exceptions, so let's
572 // log all others.
573 if (!(e instanceof SecurityException)) {
574 Log.e(TAG, "Window Manager Crash", e);
575 }
576 throw e;
577 }
578 }
579
580 private void placeWindowAfter(Object pos, WindowState window) {
581 final int i = mWindows.indexOf(pos);
582 if (localLOGV || DEBUG_FOCUS) Log.v(
583 TAG, "Adding window " + window + " at "
584 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
585 mWindows.add(i+1, window);
586 }
587
588 private void placeWindowBefore(Object pos, WindowState window) {
589 final int i = mWindows.indexOf(pos);
590 if (localLOGV || DEBUG_FOCUS) Log.v(
591 TAG, "Adding window " + window + " at "
592 + i + " of " + mWindows.size() + " (before " + pos + ")");
593 mWindows.add(i, window);
594 }
595
596 //This method finds out the index of a window that has the same app token as
597 //win. used for z ordering the windows in mWindows
598 private int findIdxBasedOnAppTokens(WindowState win) {
599 //use a local variable to cache mWindows
600 ArrayList localmWindows = mWindows;
601 int jmax = localmWindows.size();
602 if(jmax == 0) {
603 return -1;
604 }
605 for(int j = (jmax-1); j >= 0; j--) {
606 WindowState wentry = (WindowState)localmWindows.get(j);
607 if(wentry.mAppToken == win.mAppToken) {
608 return j;
609 }
610 }
611 return -1;
612 }
Romain Guy06882f82009-06-10 13:36:04 -0700613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
615 final IWindow client = win.mClient;
616 final WindowToken token = win.mToken;
617 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 final int N = localmWindows.size();
620 final WindowState attached = win.mAttachedWindow;
621 int i;
622 if (attached == null) {
623 int tokenWindowsPos = token.windows.size();
624 if (token.appWindowToken != null) {
625 int index = tokenWindowsPos-1;
626 if (index >= 0) {
627 // If this application has existing windows, we
628 // simply place the new window on top of them... but
629 // keep the starting window on top.
630 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
631 // Base windows go behind everything else.
632 placeWindowBefore(token.windows.get(0), win);
633 tokenWindowsPos = 0;
634 } else {
635 AppWindowToken atoken = win.mAppToken;
636 if (atoken != null &&
637 token.windows.get(index) == atoken.startingWindow) {
638 placeWindowBefore(token.windows.get(index), win);
639 tokenWindowsPos--;
640 } else {
641 int newIdx = findIdxBasedOnAppTokens(win);
642 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700643 //there is a window above this one associated with the same
644 //apptoken note that the window could be a floating window
645 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 //windows associated with this token.
647 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 }
650 }
651 } else {
652 if (localLOGV) Log.v(
653 TAG, "Figuring out where to add app window "
654 + client.asBinder() + " (token=" + token + ")");
655 // Figure out where the window should go, based on the
656 // order of applications.
657 final int NA = mAppTokens.size();
658 Object pos = null;
659 for (i=NA-1; i>=0; i--) {
660 AppWindowToken t = mAppTokens.get(i);
661 if (t == token) {
662 i--;
663 break;
664 }
665 if (t.windows.size() > 0) {
666 pos = t.windows.get(0);
667 }
668 }
669 // We now know the index into the apps. If we found
670 // an app window above, that gives us the position; else
671 // we need to look some more.
672 if (pos != null) {
673 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700674 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 mTokenMap.get(((WindowState)pos).mClient.asBinder());
676 if (atoken != null) {
677 final int NC = atoken.windows.size();
678 if (NC > 0) {
679 WindowState bottom = atoken.windows.get(0);
680 if (bottom.mSubLayer < 0) {
681 pos = bottom;
682 }
683 }
684 }
685 placeWindowBefore(pos, win);
686 } else {
687 while (i >= 0) {
688 AppWindowToken t = mAppTokens.get(i);
689 final int NW = t.windows.size();
690 if (NW > 0) {
691 pos = t.windows.get(NW-1);
692 break;
693 }
694 i--;
695 }
696 if (pos != null) {
697 // Move in front of any windows attached to this
698 // one.
699 WindowToken atoken =
700 mTokenMap.get(((WindowState)pos).mClient.asBinder());
701 if (atoken != null) {
702 final int NC = atoken.windows.size();
703 if (NC > 0) {
704 WindowState top = atoken.windows.get(NC-1);
705 if (top.mSubLayer >= 0) {
706 pos = top;
707 }
708 }
709 }
710 placeWindowAfter(pos, win);
711 } else {
712 // Just search for the start of this layer.
713 final int myLayer = win.mBaseLayer;
714 for (i=0; i<N; i++) {
715 WindowState w = (WindowState)localmWindows.get(i);
716 if (w.mBaseLayer > myLayer) {
717 break;
718 }
719 }
720 if (localLOGV || DEBUG_FOCUS) Log.v(
721 TAG, "Adding window " + win + " at "
722 + i + " of " + N);
723 localmWindows.add(i, win);
724 }
725 }
726 }
727 } else {
728 // Figure out where window should go, based on layer.
729 final int myLayer = win.mBaseLayer;
730 for (i=N-1; i>=0; i--) {
731 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
732 i++;
733 break;
734 }
735 }
736 if (i < 0) i = 0;
737 if (localLOGV || DEBUG_FOCUS) Log.v(
738 TAG, "Adding window " + win + " at "
739 + i + " of " + N);
740 localmWindows.add(i, win);
741 }
742 if (addToToken) {
743 token.windows.add(tokenWindowsPos, win);
744 }
745
746 } else {
747 // Figure out this window's ordering relative to the window
748 // it is attached to.
749 final int NA = token.windows.size();
750 final int sublayer = win.mSubLayer;
751 int largestSublayer = Integer.MIN_VALUE;
752 WindowState windowWithLargestSublayer = null;
753 for (i=0; i<NA; i++) {
754 WindowState w = token.windows.get(i);
755 final int wSublayer = w.mSubLayer;
756 if (wSublayer >= largestSublayer) {
757 largestSublayer = wSublayer;
758 windowWithLargestSublayer = w;
759 }
760 if (sublayer < 0) {
761 // For negative sublayers, we go below all windows
762 // in the same sublayer.
763 if (wSublayer >= sublayer) {
764 if (addToToken) {
765 token.windows.add(i, win);
766 }
767 placeWindowBefore(
768 wSublayer >= 0 ? attached : w, win);
769 break;
770 }
771 } else {
772 // For positive sublayers, we go above all windows
773 // in the same sublayer.
774 if (wSublayer > sublayer) {
775 if (addToToken) {
776 token.windows.add(i, win);
777 }
778 placeWindowBefore(w, win);
779 break;
780 }
781 }
782 }
783 if (i >= NA) {
784 if (addToToken) {
785 token.windows.add(win);
786 }
787 if (sublayer < 0) {
788 placeWindowBefore(attached, win);
789 } else {
790 placeWindowAfter(largestSublayer >= 0
791 ? windowWithLargestSublayer
792 : attached,
793 win);
794 }
795 }
796 }
Romain Guy06882f82009-06-10 13:36:04 -0700797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 if (win.mAppToken != null && addToToken) {
799 win.mAppToken.allAppWindows.add(win);
800 }
801 }
Romain Guy06882f82009-06-10 13:36:04 -0700802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 static boolean canBeImeTarget(WindowState w) {
804 final int fl = w.mAttrs.flags
805 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
806 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
807 return w.isVisibleOrAdding();
808 }
809 return false;
810 }
Romain Guy06882f82009-06-10 13:36:04 -0700811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
813 final ArrayList localmWindows = mWindows;
814 final int N = localmWindows.size();
815 WindowState w = null;
816 int i = N;
817 while (i > 0) {
818 i--;
819 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
822 // + Integer.toHexString(w.mAttrs.flags));
823 if (canBeImeTarget(w)) {
824 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 // Yet more tricksyness! If this window is a "starting"
827 // window, we do actually want to be on top of it, but
828 // it is not -really- where input will go. So if the caller
829 // is not actually looking to move the IME, look down below
830 // for a real window to target...
831 if (!willMove
832 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
833 && i > 0) {
834 WindowState wb = (WindowState)localmWindows.get(i-1);
835 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
836 i--;
837 w = wb;
838 }
839 }
840 break;
841 }
842 }
Romain Guy06882f82009-06-10 13:36:04 -0700843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
847 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 if (willMove && w != null) {
850 final WindowState curTarget = mInputMethodTarget;
851 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 // Now some fun for dealing with window animations that
854 // modify the Z order. We need to look at all windows below
855 // the current target that are in this app, finding the highest
856 // visible one in layering.
857 AppWindowToken token = curTarget.mAppToken;
858 WindowState highestTarget = null;
859 int highestPos = 0;
860 if (token.animating || token.animation != null) {
861 int pos = 0;
862 pos = localmWindows.indexOf(curTarget);
863 while (pos >= 0) {
864 WindowState win = (WindowState)localmWindows.get(pos);
865 if (win.mAppToken != token) {
866 break;
867 }
868 if (!win.mRemoved) {
869 if (highestTarget == null || win.mAnimLayer >
870 highestTarget.mAnimLayer) {
871 highestTarget = win;
872 highestPos = pos;
873 }
874 }
875 pos--;
876 }
877 }
Romain Guy06882f82009-06-10 13:36:04 -0700878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700880 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 + mNextAppTransition + " " + highestTarget
882 + " animating=" + highestTarget.isAnimating()
883 + " layer=" + highestTarget.mAnimLayer
884 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
887 // If we are currently setting up for an animation,
888 // hold everything until we can find out what will happen.
889 mInputMethodTargetWaitingAnim = true;
890 mInputMethodTarget = highestTarget;
891 return highestPos + 1;
892 } else if (highestTarget.isAnimating() &&
893 highestTarget.mAnimLayer > w.mAnimLayer) {
894 // If the window we are currently targeting is involved
895 // with an animation, and it is on top of the next target
896 // we will be over, then hold off on moving until
897 // that is done.
898 mInputMethodTarget = highestTarget;
899 return highestPos + 1;
900 }
901 }
902 }
903 }
Romain Guy06882f82009-06-10 13:36:04 -0700904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 //Log.i(TAG, "Placing input method @" + (i+1));
906 if (w != null) {
907 if (willMove) {
908 RuntimeException e = new RuntimeException();
909 e.fillInStackTrace();
910 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
911 + mInputMethodTarget + " to " + w, e);
912 mInputMethodTarget = w;
913 if (w.mAppToken != null) {
914 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
915 } else {
916 setInputMethodAnimLayerAdjustment(0);
917 }
918 }
919 return i+1;
920 }
921 if (willMove) {
922 RuntimeException e = new RuntimeException();
923 e.fillInStackTrace();
924 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
925 + mInputMethodTarget + " to null", e);
926 mInputMethodTarget = null;
927 setInputMethodAnimLayerAdjustment(0);
928 }
929 return -1;
930 }
Romain Guy06882f82009-06-10 13:36:04 -0700931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 void addInputMethodWindowToListLocked(WindowState win) {
933 int pos = findDesiredInputMethodWindowIndexLocked(true);
934 if (pos >= 0) {
935 win.mTargetAppToken = mInputMethodTarget.mAppToken;
936 mWindows.add(pos, win);
937 moveInputMethodDialogsLocked(pos+1);
938 return;
939 }
940 win.mTargetAppToken = null;
941 addWindowToListInOrderLocked(win, true);
942 moveInputMethodDialogsLocked(pos);
943 }
Romain Guy06882f82009-06-10 13:36:04 -0700944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 void setInputMethodAnimLayerAdjustment(int adj) {
946 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
947 mInputMethodAnimLayerAdjustment = adj;
948 WindowState imw = mInputMethodWindow;
949 if (imw != null) {
950 imw.mAnimLayer = imw.mLayer + adj;
951 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
952 + " anim layer: " + imw.mAnimLayer);
953 int wi = imw.mChildWindows.size();
954 while (wi > 0) {
955 wi--;
956 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
957 cw.mAnimLayer = cw.mLayer + adj;
958 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
959 + " anim layer: " + cw.mAnimLayer);
960 }
961 }
962 int di = mInputMethodDialogs.size();
963 while (di > 0) {
964 di --;
965 imw = mInputMethodDialogs.get(di);
966 imw.mAnimLayer = imw.mLayer + adj;
967 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
968 + " anim layer: " + imw.mAnimLayer);
969 }
970 }
Romain Guy06882f82009-06-10 13:36:04 -0700971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
973 int wpos = mWindows.indexOf(win);
974 if (wpos >= 0) {
975 if (wpos < interestingPos) interestingPos--;
976 mWindows.remove(wpos);
977 int NC = win.mChildWindows.size();
978 while (NC > 0) {
979 NC--;
980 WindowState cw = (WindowState)win.mChildWindows.get(NC);
981 int cpos = mWindows.indexOf(cw);
982 if (cpos >= 0) {
983 if (cpos < interestingPos) interestingPos--;
984 mWindows.remove(cpos);
985 }
986 }
987 }
988 return interestingPos;
989 }
Romain Guy06882f82009-06-10 13:36:04 -0700990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 private void reAddWindowToListInOrderLocked(WindowState win) {
992 addWindowToListInOrderLocked(win, false);
993 // This is a hack to get all of the child windows added as well
994 // at the right position. Child windows should be rare and
995 // this case should be rare, so it shouldn't be that big a deal.
996 int wpos = mWindows.indexOf(win);
997 if (wpos >= 0) {
998 mWindows.remove(wpos);
999 reAddWindowLocked(wpos, win);
1000 }
1001 }
Romain Guy06882f82009-06-10 13:36:04 -07001002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 void logWindowList(String prefix) {
1004 int N = mWindows.size();
1005 while (N > 0) {
1006 N--;
1007 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1008 }
1009 }
Romain Guy06882f82009-06-10 13:36:04 -07001010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 void moveInputMethodDialogsLocked(int pos) {
1012 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 final int N = dialogs.size();
1015 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1016 for (int i=0; i<N; i++) {
1017 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1018 }
1019 if (DEBUG_INPUT_METHOD) {
1020 Log.v(TAG, "Window list w/pos=" + pos);
1021 logWindowList(" ");
1022 }
Romain Guy06882f82009-06-10 13:36:04 -07001023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 if (pos >= 0) {
1025 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1026 if (pos < mWindows.size()) {
1027 WindowState wp = (WindowState)mWindows.get(pos);
1028 if (wp == mInputMethodWindow) {
1029 pos++;
1030 }
1031 }
1032 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1033 for (int i=0; i<N; i++) {
1034 WindowState win = dialogs.get(i);
1035 win.mTargetAppToken = targetAppToken;
1036 pos = reAddWindowLocked(pos, win);
1037 }
1038 if (DEBUG_INPUT_METHOD) {
1039 Log.v(TAG, "Final window list:");
1040 logWindowList(" ");
1041 }
1042 return;
1043 }
1044 for (int i=0; i<N; i++) {
1045 WindowState win = dialogs.get(i);
1046 win.mTargetAppToken = null;
1047 reAddWindowToListInOrderLocked(win);
1048 if (DEBUG_INPUT_METHOD) {
1049 Log.v(TAG, "No IM target, final list:");
1050 logWindowList(" ");
1051 }
1052 }
1053 }
Romain Guy06882f82009-06-10 13:36:04 -07001054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1056 final WindowState imWin = mInputMethodWindow;
1057 final int DN = mInputMethodDialogs.size();
1058 if (imWin == null && DN == 0) {
1059 return false;
1060 }
Romain Guy06882f82009-06-10 13:36:04 -07001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1063 if (imPos >= 0) {
1064 // In this case, the input method windows are to be placed
1065 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 // First check to see if the input method windows are already
1068 // located here, and contiguous.
1069 final int N = mWindows.size();
1070 WindowState firstImWin = imPos < N
1071 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 // Figure out the actual input method window that should be
1074 // at the bottom of their stack.
1075 WindowState baseImWin = imWin != null
1076 ? imWin : mInputMethodDialogs.get(0);
1077 if (baseImWin.mChildWindows.size() > 0) {
1078 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1079 if (cw.mSubLayer < 0) baseImWin = cw;
1080 }
Romain Guy06882f82009-06-10 13:36:04 -07001081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 if (firstImWin == baseImWin) {
1083 // The windows haven't moved... but are they still contiguous?
1084 // First find the top IM window.
1085 int pos = imPos+1;
1086 while (pos < N) {
1087 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1088 break;
1089 }
1090 pos++;
1091 }
1092 pos++;
1093 // Now there should be no more input method windows above.
1094 while (pos < N) {
1095 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1096 break;
1097 }
1098 pos++;
1099 }
1100 if (pos >= N) {
1101 // All is good!
1102 return false;
1103 }
1104 }
Romain Guy06882f82009-06-10 13:36:04 -07001105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 if (imWin != null) {
1107 if (DEBUG_INPUT_METHOD) {
1108 Log.v(TAG, "Moving IM from " + imPos);
1109 logWindowList(" ");
1110 }
1111 imPos = tmpRemoveWindowLocked(imPos, imWin);
1112 if (DEBUG_INPUT_METHOD) {
1113 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1114 logWindowList(" ");
1115 }
1116 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1117 reAddWindowLocked(imPos, imWin);
1118 if (DEBUG_INPUT_METHOD) {
1119 Log.v(TAG, "List after moving IM to " + imPos + ":");
1120 logWindowList(" ");
1121 }
1122 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1123 } else {
1124 moveInputMethodDialogsLocked(imPos);
1125 }
Romain Guy06882f82009-06-10 13:36:04 -07001126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 } else {
1128 // In this case, the input method windows go in a fixed layer,
1129 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 if (imWin != null) {
1132 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1133 tmpRemoveWindowLocked(0, imWin);
1134 imWin.mTargetAppToken = null;
1135 reAddWindowToListInOrderLocked(imWin);
1136 if (DEBUG_INPUT_METHOD) {
1137 Log.v(TAG, "List with no IM target:");
1138 logWindowList(" ");
1139 }
1140 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1141 } else {
1142 moveInputMethodDialogsLocked(-1);;
1143 }
Romain Guy06882f82009-06-10 13:36:04 -07001144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
Romain Guy06882f82009-06-10 13:36:04 -07001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 if (needAssignLayers) {
1148 assignLayersLocked();
1149 }
Romain Guy06882f82009-06-10 13:36:04 -07001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 return true;
1152 }
Romain Guy06882f82009-06-10 13:36:04 -07001153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 void adjustInputMethodDialogsLocked() {
1155 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1156 }
Romain Guy06882f82009-06-10 13:36:04 -07001157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 public int addWindow(Session session, IWindow client,
1159 WindowManager.LayoutParams attrs, int viewVisibility,
1160 Rect outContentInsets) {
1161 int res = mPolicy.checkAddPermission(attrs);
1162 if (res != WindowManagerImpl.ADD_OKAY) {
1163 return res;
1164 }
Romain Guy06882f82009-06-10 13:36:04 -07001165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 boolean reportNewConfig = false;
1167 WindowState attachedWindow = null;
1168 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 synchronized(mWindowMap) {
1171 // Instantiating a Display requires talking with the simulator,
1172 // so don't do it until we know the system is mostly up and
1173 // running.
1174 if (mDisplay == null) {
1175 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1176 mDisplay = wm.getDefaultDisplay();
1177 mQueue.setDisplay(mDisplay);
1178 reportNewConfig = true;
1179 }
Romain Guy06882f82009-06-10 13:36:04 -07001180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 if (mWindowMap.containsKey(client.asBinder())) {
1182 Log.w(TAG, "Window " + client + " is already added");
1183 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1184 }
1185
1186 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001187 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 if (attachedWindow == null) {
1189 Log.w(TAG, "Attempted to add window with token that is not a window: "
1190 + attrs.token + ". Aborting.");
1191 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1192 }
1193 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1194 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1195 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1196 + attrs.token + ". Aborting.");
1197 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1198 }
1199 }
1200
1201 boolean addToken = false;
1202 WindowToken token = mTokenMap.get(attrs.token);
1203 if (token == null) {
1204 if (attrs.type >= FIRST_APPLICATION_WINDOW
1205 && attrs.type <= LAST_APPLICATION_WINDOW) {
1206 Log.w(TAG, "Attempted to add application window with unknown token "
1207 + attrs.token + ". Aborting.");
1208 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1209 }
1210 if (attrs.type == TYPE_INPUT_METHOD) {
1211 Log.w(TAG, "Attempted to add input method window with unknown token "
1212 + attrs.token + ". Aborting.");
1213 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1214 }
1215 token = new WindowToken(attrs.token, -1, false);
1216 addToken = true;
1217 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1218 && attrs.type <= LAST_APPLICATION_WINDOW) {
1219 AppWindowToken atoken = token.appWindowToken;
1220 if (atoken == null) {
1221 Log.w(TAG, "Attempted to add window with non-application token "
1222 + token + ". Aborting.");
1223 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1224 } else if (atoken.removed) {
1225 Log.w(TAG, "Attempted to add window with exiting application token "
1226 + token + ". Aborting.");
1227 return WindowManagerImpl.ADD_APP_EXITING;
1228 }
1229 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1230 // No need for this guy!
1231 if (localLOGV) Log.v(
1232 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1233 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1234 }
1235 } else if (attrs.type == TYPE_INPUT_METHOD) {
1236 if (token.windowType != TYPE_INPUT_METHOD) {
1237 Log.w(TAG, "Attempted to add input method window with bad token "
1238 + attrs.token + ". Aborting.");
1239 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1240 }
1241 }
1242
1243 win = new WindowState(session, client, token,
1244 attachedWindow, attrs, viewVisibility);
1245 if (win.mDeathRecipient == null) {
1246 // Client has apparently died, so there is no reason to
1247 // continue.
1248 Log.w(TAG, "Adding window client " + client.asBinder()
1249 + " that is dead, aborting.");
1250 return WindowManagerImpl.ADD_APP_EXITING;
1251 }
1252
1253 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 res = mPolicy.prepareAddWindowLw(win, attrs);
1256 if (res != WindowManagerImpl.ADD_OKAY) {
1257 return res;
1258 }
1259
1260 // From now on, no exceptions or errors allowed!
1261
1262 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 if (addToken) {
1267 mTokenMap.put(attrs.token, token);
1268 mTokenList.add(token);
1269 }
1270 win.attach();
1271 mWindowMap.put(client.asBinder(), win);
1272
1273 if (attrs.type == TYPE_APPLICATION_STARTING &&
1274 token.appWindowToken != null) {
1275 token.appWindowToken.startingWindow = win;
1276 }
1277
1278 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 if (attrs.type == TYPE_INPUT_METHOD) {
1281 mInputMethodWindow = win;
1282 addInputMethodWindowToListLocked(win);
1283 imMayMove = false;
1284 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1285 mInputMethodDialogs.add(win);
1286 addWindowToListInOrderLocked(win, true);
1287 adjustInputMethodDialogsLocked();
1288 imMayMove = false;
1289 } else {
1290 addWindowToListInOrderLocked(win, true);
1291 }
Romain Guy06882f82009-06-10 13:36:04 -07001292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 if (mInTouchMode) {
1298 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1299 }
1300 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1301 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1302 }
Romain Guy06882f82009-06-10 13:36:04 -07001303
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001304 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001306 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1307 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 imMayMove = false;
1309 }
1310 }
Romain Guy06882f82009-06-10 13:36:04 -07001311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001313 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 }
Romain Guy06882f82009-06-10 13:36:04 -07001315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 assignLayersLocked();
1317 // Don't do layout here, the window must call
1318 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 //dump();
1321
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001322 if (focusChanged) {
1323 if (mCurrentFocus != null) {
1324 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1325 }
1326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 if (localLOGV) Log.v(
1328 TAG, "New client " + client.asBinder()
1329 + ": window=" + win);
1330 }
1331
1332 // sendNewConfiguration() checks caller permissions so we must call it with
1333 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1334 // identity anyway, so it's safe to just clear & restore around this whole
1335 // block.
1336 final long origId = Binder.clearCallingIdentity();
1337 if (reportNewConfig) {
1338 sendNewConfiguration();
1339 } else {
1340 // Update Orientation after adding a window, only if the window needs to be
1341 // displayed right away
1342 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001343 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 sendNewConfiguration();
1345 }
1346 }
1347 }
1348 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 return res;
1351 }
Romain Guy06882f82009-06-10 13:36:04 -07001352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 public void removeWindow(Session session, IWindow client) {
1354 synchronized(mWindowMap) {
1355 WindowState win = windowForClientLocked(session, client);
1356 if (win == null) {
1357 return;
1358 }
1359 removeWindowLocked(session, win);
1360 }
1361 }
Romain Guy06882f82009-06-10 13:36:04 -07001362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 public void removeWindowLocked(Session session, WindowState win) {
1364
1365 if (localLOGV || DEBUG_FOCUS) Log.v(
1366 TAG, "Remove " + win + " client="
1367 + Integer.toHexString(System.identityHashCode(
1368 win.mClient.asBinder()))
1369 + ", surface=" + win.mSurface);
1370
1371 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 if (DEBUG_APP_TRANSITIONS) Log.v(
1374 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1375 + " mExiting=" + win.mExiting
1376 + " isAnimating=" + win.isAnimating()
1377 + " app-animation="
1378 + (win.mAppToken != null ? win.mAppToken.animation : null)
1379 + " inPendingTransaction="
1380 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1381 + " mDisplayFrozen=" + mDisplayFrozen);
1382 // Visibility of the removed window. Will be used later to update orientation later on.
1383 boolean wasVisible = false;
1384 // First, see if we need to run an animation. If we do, we have
1385 // to hold off on removing the window until the animation is done.
1386 // If the display is frozen, just remove immediately, since the
1387 // animation wouldn't be seen.
1388 if (win.mSurface != null && !mDisplayFrozen) {
1389 // If we are not currently running the exit animation, we
1390 // need to see about starting one.
1391 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1394 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1395 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1396 }
1397 // Try starting an animation.
1398 if (applyAnimationLocked(win, transit, false)) {
1399 win.mExiting = true;
1400 }
1401 }
1402 if (win.mExiting || win.isAnimating()) {
1403 // The exit animation is running... wait for it!
1404 //Log.i(TAG, "*** Running exit animation...");
1405 win.mExiting = true;
1406 win.mRemoveOnExit = true;
1407 mLayoutNeeded = true;
1408 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1409 performLayoutAndPlaceSurfacesLocked();
1410 if (win.mAppToken != null) {
1411 win.mAppToken.updateReportedVisibilityLocked();
1412 }
1413 //dump();
1414 Binder.restoreCallingIdentity(origId);
1415 return;
1416 }
1417 }
1418
1419 removeWindowInnerLocked(session, win);
1420 // Removing a visible window will effect the computed orientation
1421 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001422 if (wasVisible && computeForcedAppOrientationLocked()
1423 != mForcedAppOrientation) {
1424 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
1426 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1427 Binder.restoreCallingIdentity(origId);
1428 }
Romain Guy06882f82009-06-10 13:36:04 -07001429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 private void removeWindowInnerLocked(Session session, WindowState win) {
1431 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1432 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 if (mInputMethodTarget == win) {
1437 moveInputMethodWindowsIfNeededLocked(false);
1438 }
Romain Guy06882f82009-06-10 13:36:04 -07001439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 mPolicy.removeWindowLw(win);
1441 win.removeLocked();
1442
1443 mWindowMap.remove(win.mClient.asBinder());
1444 mWindows.remove(win);
1445
1446 if (mInputMethodWindow == win) {
1447 mInputMethodWindow = null;
1448 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1449 mInputMethodDialogs.remove(win);
1450 }
Romain Guy06882f82009-06-10 13:36:04 -07001451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 final WindowToken token = win.mToken;
1453 final AppWindowToken atoken = win.mAppToken;
1454 token.windows.remove(win);
1455 if (atoken != null) {
1456 atoken.allAppWindows.remove(win);
1457 }
1458 if (localLOGV) Log.v(
1459 TAG, "**** Removing window " + win + ": count="
1460 + token.windows.size());
1461 if (token.windows.size() == 0) {
1462 if (!token.explicit) {
1463 mTokenMap.remove(token.token);
1464 mTokenList.remove(token);
1465 } else if (atoken != null) {
1466 atoken.firstWindowDrawn = false;
1467 }
1468 }
1469
1470 if (atoken != null) {
1471 if (atoken.startingWindow == win) {
1472 atoken.startingWindow = null;
1473 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1474 // If this is the last window and we had requested a starting
1475 // transition window, well there is no point now.
1476 atoken.startingData = null;
1477 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1478 // If this is the last window except for a starting transition
1479 // window, we need to get rid of the starting transition.
1480 if (DEBUG_STARTING_WINDOW) {
1481 Log.v(TAG, "Schedule remove starting " + token
1482 + ": no more real windows");
1483 }
1484 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1485 mH.sendMessage(m);
1486 }
1487 }
Romain Guy06882f82009-06-10 13:36:04 -07001488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 if (!mInLayout) {
1490 assignLayersLocked();
1491 mLayoutNeeded = true;
1492 performLayoutAndPlaceSurfacesLocked();
1493 if (win.mAppToken != null) {
1494 win.mAppToken.updateReportedVisibilityLocked();
1495 }
1496 }
1497 }
1498
1499 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1500 long origId = Binder.clearCallingIdentity();
1501 try {
1502 synchronized (mWindowMap) {
1503 WindowState w = windowForClientLocked(session, client);
1504 if ((w != null) && (w.mSurface != null)) {
1505 Surface.openTransaction();
1506 try {
1507 w.mSurface.setTransparentRegionHint(region);
1508 } finally {
1509 Surface.closeTransaction();
1510 }
1511 }
1512 }
1513 } finally {
1514 Binder.restoreCallingIdentity(origId);
1515 }
1516 }
1517
1518 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001519 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 Rect visibleInsets) {
1521 long origId = Binder.clearCallingIdentity();
1522 try {
1523 synchronized (mWindowMap) {
1524 WindowState w = windowForClientLocked(session, client);
1525 if (w != null) {
1526 w.mGivenInsetsPending = false;
1527 w.mGivenContentInsets.set(contentInsets);
1528 w.mGivenVisibleInsets.set(visibleInsets);
1529 w.mTouchableInsets = touchableInsets;
1530 mLayoutNeeded = true;
1531 performLayoutAndPlaceSurfacesLocked();
1532 }
1533 }
1534 } finally {
1535 Binder.restoreCallingIdentity(origId);
1536 }
1537 }
Romain Guy06882f82009-06-10 13:36:04 -07001538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 public void getWindowDisplayFrame(Session session, IWindow client,
1540 Rect outDisplayFrame) {
1541 synchronized(mWindowMap) {
1542 WindowState win = windowForClientLocked(session, client);
1543 if (win == null) {
1544 outDisplayFrame.setEmpty();
1545 return;
1546 }
1547 outDisplayFrame.set(win.mDisplayFrame);
1548 }
1549 }
1550
1551 public int relayoutWindow(Session session, IWindow client,
1552 WindowManager.LayoutParams attrs, int requestedWidth,
1553 int requestedHeight, int viewVisibility, boolean insetsPending,
1554 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1555 Surface outSurface) {
1556 boolean displayed = false;
1557 boolean inTouchMode;
1558 Configuration newConfig = null;
1559 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 synchronized(mWindowMap) {
1562 WindowState win = windowForClientLocked(session, client);
1563 if (win == null) {
1564 return 0;
1565 }
1566 win.mRequestedWidth = requestedWidth;
1567 win.mRequestedHeight = requestedHeight;
1568
1569 if (attrs != null) {
1570 mPolicy.adjustWindowParamsLw(attrs);
1571 }
Romain Guy06882f82009-06-10 13:36:04 -07001572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 int attrChanges = 0;
1574 int flagChanges = 0;
1575 if (attrs != null) {
1576 flagChanges = win.mAttrs.flags ^= attrs.flags;
1577 attrChanges = win.mAttrs.copyFrom(attrs);
1578 }
1579
1580 if (localLOGV) Log.v(
1581 TAG, "Relayout given client " + client.asBinder()
1582 + " (" + win.mAttrs.getTitle() + ")");
1583
1584
1585 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1586 win.mAlpha = attrs.alpha;
1587 }
1588
1589 final boolean scaledWindow =
1590 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1591
1592 if (scaledWindow) {
1593 // requested{Width|Height} Surface's physical size
1594 // attrs.{width|height} Size on screen
1595 win.mHScale = (attrs.width != requestedWidth) ?
1596 (attrs.width / (float)requestedWidth) : 1.0f;
1597 win.mVScale = (attrs.height != requestedHeight) ?
1598 (attrs.height / (float)requestedHeight) : 1.0f;
1599 }
1600
1601 boolean imMayMove = (flagChanges&(
1602 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1603 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 boolean focusMayChange = win.mViewVisibility != viewVisibility
1606 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1607 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 win.mRelayoutCalled = true;
1610 final int oldVisibility = win.mViewVisibility;
1611 win.mViewVisibility = viewVisibility;
1612 if (viewVisibility == View.VISIBLE &&
1613 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1614 displayed = !win.isVisibleLw();
1615 if (win.mExiting) {
1616 win.mExiting = false;
1617 win.mAnimation = null;
1618 }
1619 if (win.mDestroying) {
1620 win.mDestroying = false;
1621 mDestroySurface.remove(win);
1622 }
1623 if (oldVisibility == View.GONE) {
1624 win.mEnterAnimationPending = true;
1625 }
1626 if (displayed && win.mSurface != null && !win.mDrawPending
1627 && !win.mCommitDrawPending && !mDisplayFrozen) {
1628 applyEnterAnimationLocked(win);
1629 }
1630 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1631 // To change the format, we need to re-build the surface.
1632 win.destroySurfaceLocked();
1633 displayed = true;
1634 }
1635 try {
1636 Surface surface = win.createSurfaceLocked();
1637 if (surface != null) {
1638 outSurface.copyFrom(surface);
1639 } else {
1640 outSurface.clear();
1641 }
1642 } catch (Exception e) {
1643 Log.w(TAG, "Exception thrown when creating surface for client "
1644 + client + " (" + win.mAttrs.getTitle() + ")",
1645 e);
1646 Binder.restoreCallingIdentity(origId);
1647 return 0;
1648 }
1649 if (displayed) {
1650 focusMayChange = true;
1651 }
1652 if (win.mAttrs.type == TYPE_INPUT_METHOD
1653 && mInputMethodWindow == null) {
1654 mInputMethodWindow = win;
1655 imMayMove = true;
1656 }
1657 } else {
1658 win.mEnterAnimationPending = false;
1659 if (win.mSurface != null) {
1660 // If we are not currently running the exit animation, we
1661 // need to see about starting one.
1662 if (!win.mExiting) {
1663 // Try starting an animation; if there isn't one, we
1664 // can destroy the surface right away.
1665 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1666 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1667 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1668 }
1669 if (win.isWinVisibleLw() &&
1670 applyAnimationLocked(win, transit, false)) {
1671 win.mExiting = true;
1672 mKeyWaiter.finishedKey(session, client, true,
1673 KeyWaiter.RETURN_NOTHING);
1674 } else if (win.isAnimating()) {
1675 // Currently in a hide animation... turn this into
1676 // an exit.
1677 win.mExiting = true;
1678 } else {
1679 if (mInputMethodWindow == win) {
1680 mInputMethodWindow = null;
1681 }
1682 win.destroySurfaceLocked();
1683 }
1684 }
1685 }
1686 outSurface.clear();
1687 }
1688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 if (focusMayChange) {
1690 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1691 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 imMayMove = false;
1693 }
1694 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1695 }
Romain Guy06882f82009-06-10 13:36:04 -07001696
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001697 // updateFocusedWindowLocked() already assigned layers so we only need to
1698 // reassign them at this point if the IM window state gets shuffled
1699 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 if (imMayMove) {
1702 if (moveInputMethodWindowsIfNeededLocked(false)) {
1703 assignLayers = true;
1704 }
1705 }
Romain Guy06882f82009-06-10 13:36:04 -07001706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 mLayoutNeeded = true;
1708 win.mGivenInsetsPending = insetsPending;
1709 if (assignLayers) {
1710 assignLayersLocked();
1711 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001712 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 performLayoutAndPlaceSurfacesLocked();
1714 if (win.mAppToken != null) {
1715 win.mAppToken.updateReportedVisibilityLocked();
1716 }
1717 outFrame.set(win.mFrame);
1718 outContentInsets.set(win.mContentInsets);
1719 outVisibleInsets.set(win.mVisibleInsets);
1720 if (localLOGV) Log.v(
1721 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001722 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 + ", requestedHeight=" + requestedHeight
1724 + ", viewVisibility=" + viewVisibility
1725 + "\nRelayout returning frame=" + outFrame
1726 + ", surface=" + outSurface);
1727
1728 if (localLOGV || DEBUG_FOCUS) Log.v(
1729 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1730
1731 inTouchMode = mInTouchMode;
1732 }
1733
1734 if (newConfig != null) {
1735 sendNewConfiguration();
1736 }
Romain Guy06882f82009-06-10 13:36:04 -07001737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1741 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1742 }
1743
1744 public void finishDrawingWindow(Session session, IWindow client) {
1745 final long origId = Binder.clearCallingIdentity();
1746 synchronized(mWindowMap) {
1747 WindowState win = windowForClientLocked(session, client);
1748 if (win != null && win.finishDrawingLocked()) {
1749 mLayoutNeeded = true;
1750 performLayoutAndPlaceSurfacesLocked();
1751 }
1752 }
1753 Binder.restoreCallingIdentity(origId);
1754 }
1755
1756 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1757 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1758 + (lp != null ? lp.packageName : null)
1759 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1760 if (lp != null && lp.windowAnimations != 0) {
1761 // If this is a system resource, don't try to load it from the
1762 // application resources. It is nice to avoid loading application
1763 // resources if we can.
1764 String packageName = lp.packageName != null ? lp.packageName : "android";
1765 int resId = lp.windowAnimations;
1766 if ((resId&0xFF000000) == 0x01000000) {
1767 packageName = "android";
1768 }
1769 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
1770 + packageName);
1771 return AttributeCache.instance().get(packageName, resId,
1772 com.android.internal.R.styleable.WindowAnimation);
1773 }
1774 return null;
1775 }
Romain Guy06882f82009-06-10 13:36:04 -07001776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 private void applyEnterAnimationLocked(WindowState win) {
1778 int transit = WindowManagerPolicy.TRANSIT_SHOW;
1779 if (win.mEnterAnimationPending) {
1780 win.mEnterAnimationPending = false;
1781 transit = WindowManagerPolicy.TRANSIT_ENTER;
1782 }
1783
1784 applyAnimationLocked(win, transit, true);
1785 }
1786
1787 private boolean applyAnimationLocked(WindowState win,
1788 int transit, boolean isEntrance) {
1789 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
1790 // If we are trying to apply an animation, but already running
1791 // an animation of the same type, then just leave that one alone.
1792 return true;
1793 }
Romain Guy06882f82009-06-10 13:36:04 -07001794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 // Only apply an animation if the display isn't frozen. If it is
1796 // frozen, there is no reason to animate and it can cause strange
1797 // artifacts when we unfreeze the display if some different animation
1798 // is running.
1799 if (!mDisplayFrozen) {
1800 int anim = mPolicy.selectAnimationLw(win, transit);
1801 int attr = -1;
1802 Animation a = null;
1803 if (anim != 0) {
1804 a = AnimationUtils.loadAnimation(mContext, anim);
1805 } else {
1806 switch (transit) {
1807 case WindowManagerPolicy.TRANSIT_ENTER:
1808 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1809 break;
1810 case WindowManagerPolicy.TRANSIT_EXIT:
1811 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1812 break;
1813 case WindowManagerPolicy.TRANSIT_SHOW:
1814 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1815 break;
1816 case WindowManagerPolicy.TRANSIT_HIDE:
1817 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1818 break;
1819 }
1820 if (attr >= 0) {
1821 a = loadAnimation(win.mAttrs, attr);
1822 }
1823 }
1824 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
1825 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1826 + " mAnimation=" + win.mAnimation
1827 + " isEntrance=" + isEntrance);
1828 if (a != null) {
1829 if (DEBUG_ANIM) {
1830 RuntimeException e = new RuntimeException();
1831 e.fillInStackTrace();
1832 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
1833 }
1834 win.setAnimation(a);
1835 win.mAnimationIsEntrance = isEntrance;
1836 }
1837 } else {
1838 win.clearAnimation();
1839 }
1840
1841 return win.mAnimation != null;
1842 }
1843
1844 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
1845 int anim = 0;
1846 Context context = mContext;
1847 if (animAttr >= 0) {
1848 AttributeCache.Entry ent = getCachedAnimations(lp);
1849 if (ent != null) {
1850 context = ent.context;
1851 anim = ent.array.getResourceId(animAttr, 0);
1852 }
1853 }
1854 if (anim != 0) {
1855 return AnimationUtils.loadAnimation(context, anim);
1856 }
1857 return null;
1858 }
Romain Guy06882f82009-06-10 13:36:04 -07001859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 private boolean applyAnimationLocked(AppWindowToken wtoken,
1861 WindowManager.LayoutParams lp, int transit, boolean enter) {
1862 // Only apply an animation if the display isn't frozen. If it is
1863 // frozen, there is no reason to animate and it can cause strange
1864 // artifacts when we unfreeze the display if some different animation
1865 // is running.
1866 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001867 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07001868 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001869 a = new FadeInOutAnimation(enter);
1870 if (DEBUG_ANIM) Log.v(TAG,
1871 "applying FadeInOutAnimation for a window in compatibility mode");
1872 } else {
1873 int animAttr = 0;
1874 switch (transit) {
1875 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
1876 animAttr = enter
1877 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
1878 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
1879 break;
1880 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
1881 animAttr = enter
1882 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
1883 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
1884 break;
1885 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
1886 animAttr = enter
1887 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
1888 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
1889 break;
1890 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
1891 animAttr = enter
1892 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
1893 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
1894 break;
1895 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
1896 animAttr = enter
1897 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
1898 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
1899 break;
1900 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
1901 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07001902 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001903 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
1904 break;
1905 }
1906 a = loadAnimation(lp, animAttr);
1907 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
1908 + " anim=" + a
1909 + " animAttr=0x" + Integer.toHexString(animAttr)
1910 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 if (a != null) {
1913 if (DEBUG_ANIM) {
1914 RuntimeException e = new RuntimeException();
1915 e.fillInStackTrace();
1916 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
1917 }
1918 wtoken.setAnimation(a);
1919 }
1920 } else {
1921 wtoken.clearAnimation();
1922 }
1923
1924 return wtoken.animation != null;
1925 }
1926
1927 // -------------------------------------------------------------
1928 // Application Window Tokens
1929 // -------------------------------------------------------------
1930
1931 public void validateAppTokens(List tokens) {
1932 int v = tokens.size()-1;
1933 int m = mAppTokens.size()-1;
1934 while (v >= 0 && m >= 0) {
1935 AppWindowToken wtoken = mAppTokens.get(m);
1936 if (wtoken.removed) {
1937 m--;
1938 continue;
1939 }
1940 if (tokens.get(v) != wtoken.token) {
1941 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
1942 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
1943 }
1944 v--;
1945 m--;
1946 }
1947 while (v >= 0) {
1948 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
1949 v--;
1950 }
1951 while (m >= 0) {
1952 AppWindowToken wtoken = mAppTokens.get(m);
1953 if (!wtoken.removed) {
1954 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
1955 }
1956 m--;
1957 }
1958 }
1959
1960 boolean checkCallingPermission(String permission, String func) {
1961 // Quick check: if the calling permission is me, it's all okay.
1962 if (Binder.getCallingPid() == Process.myPid()) {
1963 return true;
1964 }
Romain Guy06882f82009-06-10 13:36:04 -07001965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 if (mContext.checkCallingPermission(permission)
1967 == PackageManager.PERMISSION_GRANTED) {
1968 return true;
1969 }
1970 String msg = "Permission Denial: " + func + " from pid="
1971 + Binder.getCallingPid()
1972 + ", uid=" + Binder.getCallingUid()
1973 + " requires " + permission;
1974 Log.w(TAG, msg);
1975 return false;
1976 }
Romain Guy06882f82009-06-10 13:36:04 -07001977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 AppWindowToken findAppWindowToken(IBinder token) {
1979 WindowToken wtoken = mTokenMap.get(token);
1980 if (wtoken == null) {
1981 return null;
1982 }
1983 return wtoken.appWindowToken;
1984 }
Romain Guy06882f82009-06-10 13:36:04 -07001985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 public void addWindowToken(IBinder token, int type) {
1987 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1988 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001989 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 }
Romain Guy06882f82009-06-10 13:36:04 -07001991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 synchronized(mWindowMap) {
1993 WindowToken wtoken = mTokenMap.get(token);
1994 if (wtoken != null) {
1995 Log.w(TAG, "Attempted to add existing input method token: " + token);
1996 return;
1997 }
1998 wtoken = new WindowToken(token, type, true);
1999 mTokenMap.put(token, wtoken);
2000 mTokenList.add(wtoken);
2001 }
2002 }
Romain Guy06882f82009-06-10 13:36:04 -07002003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 public void removeWindowToken(IBinder token) {
2005 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2006 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002007 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
2009
2010 final long origId = Binder.clearCallingIdentity();
2011 synchronized(mWindowMap) {
2012 WindowToken wtoken = mTokenMap.remove(token);
2013 mTokenList.remove(wtoken);
2014 if (wtoken != null) {
2015 boolean delayed = false;
2016 if (!wtoken.hidden) {
2017 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 final int N = wtoken.windows.size();
2020 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 for (int i=0; i<N; i++) {
2023 WindowState win = wtoken.windows.get(i);
2024
2025 if (win.isAnimating()) {
2026 delayed = true;
2027 }
Romain Guy06882f82009-06-10 13:36:04 -07002028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 if (win.isVisibleNow()) {
2030 applyAnimationLocked(win,
2031 WindowManagerPolicy.TRANSIT_EXIT, false);
2032 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2033 KeyWaiter.RETURN_NOTHING);
2034 changed = true;
2035 }
2036 }
2037
2038 if (changed) {
2039 mLayoutNeeded = true;
2040 performLayoutAndPlaceSurfacesLocked();
2041 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2042 }
Romain Guy06882f82009-06-10 13:36:04 -07002043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 if (delayed) {
2045 mExitingTokens.add(wtoken);
2046 }
2047 }
Romain Guy06882f82009-06-10 13:36:04 -07002048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 } else {
2050 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2051 }
2052 }
2053 Binder.restoreCallingIdentity(origId);
2054 }
2055
2056 public void addAppToken(int addPos, IApplicationToken token,
2057 int groupId, int requestedOrientation, boolean fullscreen) {
2058 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2059 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002060 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
Romain Guy06882f82009-06-10 13:36:04 -07002062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 synchronized(mWindowMap) {
2064 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2065 if (wtoken != null) {
2066 Log.w(TAG, "Attempted to add existing app token: " + token);
2067 return;
2068 }
2069 wtoken = new AppWindowToken(token);
2070 wtoken.groupId = groupId;
2071 wtoken.appFullscreen = fullscreen;
2072 wtoken.requestedOrientation = requestedOrientation;
2073 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002074 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 mTokenMap.put(token.asBinder(), wtoken);
2076 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 // Application tokens start out hidden.
2079 wtoken.hidden = true;
2080 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 //dump();
2083 }
2084 }
Romain Guy06882f82009-06-10 13:36:04 -07002085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 public void setAppGroupId(IBinder token, int groupId) {
2087 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2088 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002089 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
2091
2092 synchronized(mWindowMap) {
2093 AppWindowToken wtoken = findAppWindowToken(token);
2094 if (wtoken == null) {
2095 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2096 return;
2097 }
2098 wtoken.groupId = groupId;
2099 }
2100 }
Romain Guy06882f82009-06-10 13:36:04 -07002101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 public int getOrientationFromWindowsLocked() {
2103 int pos = mWindows.size() - 1;
2104 while (pos >= 0) {
2105 WindowState wtoken = (WindowState) mWindows.get(pos);
2106 pos--;
2107 if (wtoken.mAppToken != null) {
2108 // We hit an application window. so the orientation will be determined by the
2109 // app window. No point in continuing further.
2110 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2111 }
2112 if (!wtoken.isVisibleLw()) {
2113 continue;
2114 }
2115 int req = wtoken.mAttrs.screenOrientation;
2116 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2117 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2118 continue;
2119 } else {
2120 return req;
2121 }
2122 }
2123 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2124 }
Romain Guy06882f82009-06-10 13:36:04 -07002125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 public int getOrientationFromAppTokensLocked() {
2127 int pos = mAppTokens.size() - 1;
2128 int curGroup = 0;
2129 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002130 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002132 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 while (pos >= 0) {
2134 AppWindowToken wtoken = mAppTokens.get(pos);
2135 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002136 // if we're about to tear down this window and not seek for
2137 // the behind activity, don't use it for orientation
2138 if (!findingBehind
2139 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002140 continue;
2141 }
2142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 if (!haveGroup) {
2144 // We ignore any hidden applications on the top.
2145 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2146 continue;
2147 }
2148 haveGroup = true;
2149 curGroup = wtoken.groupId;
2150 lastOrientation = wtoken.requestedOrientation;
2151 } else if (curGroup != wtoken.groupId) {
2152 // If we have hit a new application group, and the bottom
2153 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002154 // the orientation behind it, and the last app was
2155 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002157 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2158 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 return lastOrientation;
2160 }
2161 }
2162 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002163 // If this application is fullscreen, and didn't explicitly say
2164 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002166 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002167 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002168 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 return or;
2170 }
2171 // If this application has requested an explicit orientation,
2172 // then use it.
2173 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2174 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2175 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2176 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2177 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2178 return or;
2179 }
Owen Lin3413b892009-05-01 17:12:32 -07002180 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 }
2182 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2183 }
Romain Guy06882f82009-06-10 13:36:04 -07002184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002186 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002187 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2188 "updateOrientationFromAppTokens()")) {
2189 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2190 }
2191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 Configuration config;
2193 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002194 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2195 freezeThisOneIfNeeded);
2196 Binder.restoreCallingIdentity(ident);
2197 return config;
2198 }
2199
2200 Configuration updateOrientationFromAppTokensUnchecked(
2201 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2202 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002204 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 }
2206 if (config != null) {
2207 mLayoutNeeded = true;
2208 performLayoutAndPlaceSurfacesLocked();
2209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 return config;
2211 }
Romain Guy06882f82009-06-10 13:36:04 -07002212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 /*
2214 * The orientation is computed from non-application windows first. If none of
2215 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002216 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2218 * android.os.IBinder)
2219 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002220 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002221 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 long ident = Binder.clearCallingIdentity();
2224 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002225 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 if (req != mForcedAppOrientation) {
2228 changed = true;
2229 mForcedAppOrientation = req;
2230 //send a message to Policy indicating orientation change to take
2231 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002232 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 }
Romain Guy06882f82009-06-10 13:36:04 -07002234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 if (changed) {
2236 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002237 WindowManagerPolicy.USE_LAST_ROTATION,
2238 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 if (changed) {
2240 if (freezeThisOneIfNeeded != null) {
2241 AppWindowToken wtoken = findAppWindowToken(
2242 freezeThisOneIfNeeded);
2243 if (wtoken != null) {
2244 startAppFreezingScreenLocked(wtoken,
2245 ActivityInfo.CONFIG_ORIENTATION);
2246 }
2247 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002248 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
2250 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002251
2252 // No obvious action we need to take, but if our current
2253 // state mismatches the activity maanager's, update it
2254 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002255 mTempConfiguration.setToDefaults();
2256 if (computeNewConfigurationLocked(mTempConfiguration)) {
2257 if (appConfig.diff(mTempConfiguration) != 0) {
2258 Log.i(TAG, "Config changed: " + mTempConfiguration);
2259 return new Configuration(mTempConfiguration);
2260 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002261 }
2262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 } finally {
2264 Binder.restoreCallingIdentity(ident);
2265 }
Romain Guy06882f82009-06-10 13:36:04 -07002266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 return null;
2268 }
Romain Guy06882f82009-06-10 13:36:04 -07002269
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002270 int computeForcedAppOrientationLocked() {
2271 int req = getOrientationFromWindowsLocked();
2272 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2273 req = getOrientationFromAppTokensLocked();
2274 }
2275 return req;
2276 }
Romain Guy06882f82009-06-10 13:36:04 -07002277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2279 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2280 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002281 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 }
Romain Guy06882f82009-06-10 13:36:04 -07002283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 synchronized(mWindowMap) {
2285 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2286 if (wtoken == null) {
2287 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2288 return;
2289 }
Romain Guy06882f82009-06-10 13:36:04 -07002290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 wtoken.requestedOrientation = requestedOrientation;
2292 }
2293 }
Romain Guy06882f82009-06-10 13:36:04 -07002294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 public int getAppOrientation(IApplicationToken token) {
2296 synchronized(mWindowMap) {
2297 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2298 if (wtoken == null) {
2299 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2300 }
Romain Guy06882f82009-06-10 13:36:04 -07002301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 return wtoken.requestedOrientation;
2303 }
2304 }
Romain Guy06882f82009-06-10 13:36:04 -07002305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2307 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2308 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002309 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 }
2311
2312 synchronized(mWindowMap) {
2313 boolean changed = false;
2314 if (token == null) {
2315 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2316 changed = mFocusedApp != null;
2317 mFocusedApp = null;
2318 mKeyWaiter.tickle();
2319 } else {
2320 AppWindowToken newFocus = findAppWindowToken(token);
2321 if (newFocus == null) {
2322 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2323 return;
2324 }
2325 changed = mFocusedApp != newFocus;
2326 mFocusedApp = newFocus;
2327 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2328 mKeyWaiter.tickle();
2329 }
2330
2331 if (moveFocusNow && changed) {
2332 final long origId = Binder.clearCallingIdentity();
2333 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2334 Binder.restoreCallingIdentity(origId);
2335 }
2336 }
2337 }
2338
2339 public void prepareAppTransition(int transit) {
2340 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2341 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002342 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 }
Romain Guy06882f82009-06-10 13:36:04 -07002344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 synchronized(mWindowMap) {
2346 if (DEBUG_APP_TRANSITIONS) Log.v(
2347 TAG, "Prepare app transition: transit=" + transit
2348 + " mNextAppTransition=" + mNextAppTransition);
2349 if (!mDisplayFrozen) {
2350 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2351 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002352 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2353 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2354 // Opening a new task always supersedes a close for the anim.
2355 mNextAppTransition = transit;
2356 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2357 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2358 // Opening a new activity always supersedes a close for the anim.
2359 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 }
2361 mAppTransitionReady = false;
2362 mAppTransitionTimeout = false;
2363 mStartingIconInTransition = false;
2364 mSkipAppTransitionAnimation = false;
2365 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2366 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2367 5000);
2368 }
2369 }
2370 }
2371
2372 public int getPendingAppTransition() {
2373 return mNextAppTransition;
2374 }
Romain Guy06882f82009-06-10 13:36:04 -07002375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 public void executeAppTransition() {
2377 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2378 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002379 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 }
Romain Guy06882f82009-06-10 13:36:04 -07002381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 synchronized(mWindowMap) {
2383 if (DEBUG_APP_TRANSITIONS) Log.v(
2384 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2385 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2386 mAppTransitionReady = true;
2387 final long origId = Binder.clearCallingIdentity();
2388 performLayoutAndPlaceSurfacesLocked();
2389 Binder.restoreCallingIdentity(origId);
2390 }
2391 }
2392 }
2393
2394 public void setAppStartingWindow(IBinder token, String pkg,
2395 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2396 IBinder transferFrom, boolean createIfNeeded) {
2397 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2398 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002399 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
2401
2402 synchronized(mWindowMap) {
2403 if (DEBUG_STARTING_WINDOW) Log.v(
2404 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2405 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 AppWindowToken wtoken = findAppWindowToken(token);
2408 if (wtoken == null) {
2409 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2410 return;
2411 }
2412
2413 // If the display is frozen, we won't do anything until the
2414 // actual window is displayed so there is no reason to put in
2415 // the starting window.
2416 if (mDisplayFrozen) {
2417 return;
2418 }
Romain Guy06882f82009-06-10 13:36:04 -07002419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 if (wtoken.startingData != null) {
2421 return;
2422 }
Romain Guy06882f82009-06-10 13:36:04 -07002423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 if (transferFrom != null) {
2425 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2426 if (ttoken != null) {
2427 WindowState startingWindow = ttoken.startingWindow;
2428 if (startingWindow != null) {
2429 if (mStartingIconInTransition) {
2430 // In this case, the starting icon has already
2431 // been displayed, so start letting windows get
2432 // shown immediately without any more transitions.
2433 mSkipAppTransitionAnimation = true;
2434 }
2435 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2436 "Moving existing starting from " + ttoken
2437 + " to " + wtoken);
2438 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 // Transfer the starting window over to the new
2441 // token.
2442 wtoken.startingData = ttoken.startingData;
2443 wtoken.startingView = ttoken.startingView;
2444 wtoken.startingWindow = startingWindow;
2445 ttoken.startingData = null;
2446 ttoken.startingView = null;
2447 ttoken.startingWindow = null;
2448 ttoken.startingMoved = true;
2449 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002450 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 startingWindow.mAppToken = wtoken;
2452 mWindows.remove(startingWindow);
2453 ttoken.windows.remove(startingWindow);
2454 ttoken.allAppWindows.remove(startingWindow);
2455 addWindowToListInOrderLocked(startingWindow, true);
2456 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 // Propagate other interesting state between the
2459 // tokens. If the old token is displayed, we should
2460 // immediately force the new one to be displayed. If
2461 // it is animating, we need to move that animation to
2462 // the new one.
2463 if (ttoken.allDrawn) {
2464 wtoken.allDrawn = true;
2465 }
2466 if (ttoken.firstWindowDrawn) {
2467 wtoken.firstWindowDrawn = true;
2468 }
2469 if (!ttoken.hidden) {
2470 wtoken.hidden = false;
2471 wtoken.hiddenRequested = false;
2472 wtoken.willBeHidden = false;
2473 }
2474 if (wtoken.clientHidden != ttoken.clientHidden) {
2475 wtoken.clientHidden = ttoken.clientHidden;
2476 wtoken.sendAppVisibilityToClients();
2477 }
2478 if (ttoken.animation != null) {
2479 wtoken.animation = ttoken.animation;
2480 wtoken.animating = ttoken.animating;
2481 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2482 ttoken.animation = null;
2483 ttoken.animLayerAdjustment = 0;
2484 wtoken.updateLayers();
2485 ttoken.updateLayers();
2486 }
Romain Guy06882f82009-06-10 13:36:04 -07002487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 mLayoutNeeded = true;
2490 performLayoutAndPlaceSurfacesLocked();
2491 Binder.restoreCallingIdentity(origId);
2492 return;
2493 } else if (ttoken.startingData != null) {
2494 // The previous app was getting ready to show a
2495 // starting window, but hasn't yet done so. Steal it!
2496 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2497 "Moving pending starting from " + ttoken
2498 + " to " + wtoken);
2499 wtoken.startingData = ttoken.startingData;
2500 ttoken.startingData = null;
2501 ttoken.startingMoved = true;
2502 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2503 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2504 // want to process the message ASAP, before any other queued
2505 // messages.
2506 mH.sendMessageAtFrontOfQueue(m);
2507 return;
2508 }
2509 }
2510 }
2511
2512 // There is no existing starting window, and the caller doesn't
2513 // want us to create one, so that's it!
2514 if (!createIfNeeded) {
2515 return;
2516 }
Romain Guy06882f82009-06-10 13:36:04 -07002517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 mStartingIconInTransition = true;
2519 wtoken.startingData = new StartingData(
2520 pkg, theme, nonLocalizedLabel,
2521 labelRes, icon);
2522 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2523 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2524 // want to process the message ASAP, before any other queued
2525 // messages.
2526 mH.sendMessageAtFrontOfQueue(m);
2527 }
2528 }
2529
2530 public void setAppWillBeHidden(IBinder token) {
2531 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2532 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002533 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 }
2535
2536 AppWindowToken wtoken;
2537
2538 synchronized(mWindowMap) {
2539 wtoken = findAppWindowToken(token);
2540 if (wtoken == null) {
2541 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2542 return;
2543 }
2544 wtoken.willBeHidden = true;
2545 }
2546 }
Romain Guy06882f82009-06-10 13:36:04 -07002547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2549 boolean visible, int transit, boolean performLayout) {
2550 boolean delayed = false;
2551
2552 if (wtoken.clientHidden == visible) {
2553 wtoken.clientHidden = !visible;
2554 wtoken.sendAppVisibilityToClients();
2555 }
Romain Guy06882f82009-06-10 13:36:04 -07002556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 wtoken.willBeHidden = false;
2558 if (wtoken.hidden == visible) {
2559 final int N = wtoken.allAppWindows.size();
2560 boolean changed = false;
2561 if (DEBUG_APP_TRANSITIONS) Log.v(
2562 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2563 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2568 if (wtoken.animation == sDummyAnimation) {
2569 wtoken.animation = null;
2570 }
2571 applyAnimationLocked(wtoken, lp, transit, visible);
2572 changed = true;
2573 if (wtoken.animation != null) {
2574 delayed = runningAppAnimation = true;
2575 }
2576 }
Romain Guy06882f82009-06-10 13:36:04 -07002577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 for (int i=0; i<N; i++) {
2579 WindowState win = wtoken.allAppWindows.get(i);
2580 if (win == wtoken.startingWindow) {
2581 continue;
2582 }
2583
2584 if (win.isAnimating()) {
2585 delayed = true;
2586 }
Romain Guy06882f82009-06-10 13:36:04 -07002587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2589 //win.dump(" ");
2590 if (visible) {
2591 if (!win.isVisibleNow()) {
2592 if (!runningAppAnimation) {
2593 applyAnimationLocked(win,
2594 WindowManagerPolicy.TRANSIT_ENTER, true);
2595 }
2596 changed = true;
2597 }
2598 } else if (win.isVisibleNow()) {
2599 if (!runningAppAnimation) {
2600 applyAnimationLocked(win,
2601 WindowManagerPolicy.TRANSIT_EXIT, false);
2602 }
2603 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2604 KeyWaiter.RETURN_NOTHING);
2605 changed = true;
2606 }
2607 }
2608
2609 wtoken.hidden = wtoken.hiddenRequested = !visible;
2610 if (!visible) {
2611 unsetAppFreezingScreenLocked(wtoken, true, true);
2612 } else {
2613 // If we are being set visible, and the starting window is
2614 // not yet displayed, then make sure it doesn't get displayed.
2615 WindowState swin = wtoken.startingWindow;
2616 if (swin != null && (swin.mDrawPending
2617 || swin.mCommitDrawPending)) {
2618 swin.mPolicyVisibility = false;
2619 swin.mPolicyVisibilityAfterAnim = false;
2620 }
2621 }
Romain Guy06882f82009-06-10 13:36:04 -07002622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2624 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2625 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 if (changed && performLayout) {
2628 mLayoutNeeded = true;
2629 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 performLayoutAndPlaceSurfacesLocked();
2631 }
2632 }
2633
2634 if (wtoken.animation != null) {
2635 delayed = true;
2636 }
Romain Guy06882f82009-06-10 13:36:04 -07002637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 return delayed;
2639 }
2640
2641 public void setAppVisibility(IBinder token, boolean visible) {
2642 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2643 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002644 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 }
2646
2647 AppWindowToken wtoken;
2648
2649 synchronized(mWindowMap) {
2650 wtoken = findAppWindowToken(token);
2651 if (wtoken == null) {
2652 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2653 return;
2654 }
2655
2656 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2657 RuntimeException e = new RuntimeException();
2658 e.fillInStackTrace();
2659 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2660 + "): mNextAppTransition=" + mNextAppTransition
2661 + " hidden=" + wtoken.hidden
2662 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2663 }
Romain Guy06882f82009-06-10 13:36:04 -07002664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 // If we are preparing an app transition, then delay changing
2666 // the visibility of this token until we execute that transition.
2667 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2668 // Already in requested state, don't do anything more.
2669 if (wtoken.hiddenRequested != visible) {
2670 return;
2671 }
2672 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 if (DEBUG_APP_TRANSITIONS) Log.v(
2675 TAG, "Setting dummy animation on: " + wtoken);
2676 wtoken.setDummyAnimation();
2677 mOpeningApps.remove(wtoken);
2678 mClosingApps.remove(wtoken);
2679 wtoken.inPendingTransaction = true;
2680 if (visible) {
2681 mOpeningApps.add(wtoken);
2682 wtoken.allDrawn = false;
2683 wtoken.startingDisplayed = false;
2684 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 if (wtoken.clientHidden) {
2687 // In the case where we are making an app visible
2688 // but holding off for a transition, we still need
2689 // to tell the client to make its windows visible so
2690 // they get drawn. Otherwise, we will wait on
2691 // performing the transition until all windows have
2692 // been drawn, they never will be, and we are sad.
2693 wtoken.clientHidden = false;
2694 wtoken.sendAppVisibilityToClients();
2695 }
2696 } else {
2697 mClosingApps.add(wtoken);
2698 }
2699 return;
2700 }
Romain Guy06882f82009-06-10 13:36:04 -07002701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 final long origId = Binder.clearCallingIdentity();
2703 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2704 wtoken.updateReportedVisibilityLocked();
2705 Binder.restoreCallingIdentity(origId);
2706 }
2707 }
2708
2709 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2710 boolean unfreezeSurfaceNow, boolean force) {
2711 if (wtoken.freezingScreen) {
2712 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2713 + " force=" + force);
2714 final int N = wtoken.allAppWindows.size();
2715 boolean unfrozeWindows = false;
2716 for (int i=0; i<N; i++) {
2717 WindowState w = wtoken.allAppWindows.get(i);
2718 if (w.mAppFreezing) {
2719 w.mAppFreezing = false;
2720 if (w.mSurface != null && !w.mOrientationChanging) {
2721 w.mOrientationChanging = true;
2722 }
2723 unfrozeWindows = true;
2724 }
2725 }
2726 if (force || unfrozeWindows) {
2727 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2728 wtoken.freezingScreen = false;
2729 mAppsFreezingScreen--;
2730 }
2731 if (unfreezeSurfaceNow) {
2732 if (unfrozeWindows) {
2733 mLayoutNeeded = true;
2734 performLayoutAndPlaceSurfacesLocked();
2735 }
2736 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2737 stopFreezingDisplayLocked();
2738 }
2739 }
2740 }
2741 }
Romain Guy06882f82009-06-10 13:36:04 -07002742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2744 int configChanges) {
2745 if (DEBUG_ORIENTATION) {
2746 RuntimeException e = new RuntimeException();
2747 e.fillInStackTrace();
2748 Log.i(TAG, "Set freezing of " + wtoken.appToken
2749 + ": hidden=" + wtoken.hidden + " freezing="
2750 + wtoken.freezingScreen, e);
2751 }
2752 if (!wtoken.hiddenRequested) {
2753 if (!wtoken.freezingScreen) {
2754 wtoken.freezingScreen = true;
2755 mAppsFreezingScreen++;
2756 if (mAppsFreezingScreen == 1) {
2757 startFreezingDisplayLocked();
2758 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
2759 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
2760 5000);
2761 }
2762 }
2763 final int N = wtoken.allAppWindows.size();
2764 for (int i=0; i<N; i++) {
2765 WindowState w = wtoken.allAppWindows.get(i);
2766 w.mAppFreezing = true;
2767 }
2768 }
2769 }
Romain Guy06882f82009-06-10 13:36:04 -07002770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 public void startAppFreezingScreen(IBinder token, int configChanges) {
2772 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2773 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002774 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 }
2776
2777 synchronized(mWindowMap) {
2778 if (configChanges == 0 && !mDisplayFrozen) {
2779 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
2780 return;
2781 }
Romain Guy06882f82009-06-10 13:36:04 -07002782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 AppWindowToken wtoken = findAppWindowToken(token);
2784 if (wtoken == null || wtoken.appToken == null) {
2785 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
2786 return;
2787 }
2788 final long origId = Binder.clearCallingIdentity();
2789 startAppFreezingScreenLocked(wtoken, configChanges);
2790 Binder.restoreCallingIdentity(origId);
2791 }
2792 }
Romain Guy06882f82009-06-10 13:36:04 -07002793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 public void stopAppFreezingScreen(IBinder token, boolean force) {
2795 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2796 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002797 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 }
2799
2800 synchronized(mWindowMap) {
2801 AppWindowToken wtoken = findAppWindowToken(token);
2802 if (wtoken == null || wtoken.appToken == null) {
2803 return;
2804 }
2805 final long origId = Binder.clearCallingIdentity();
2806 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
2807 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
2808 unsetAppFreezingScreenLocked(wtoken, true, force);
2809 Binder.restoreCallingIdentity(origId);
2810 }
2811 }
Romain Guy06882f82009-06-10 13:36:04 -07002812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 public void removeAppToken(IBinder token) {
2814 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2815 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002816 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 }
2818
2819 AppWindowToken wtoken = null;
2820 AppWindowToken startingToken = null;
2821 boolean delayed = false;
2822
2823 final long origId = Binder.clearCallingIdentity();
2824 synchronized(mWindowMap) {
2825 WindowToken basewtoken = mTokenMap.remove(token);
2826 mTokenList.remove(basewtoken);
2827 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
2828 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
2829 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
2830 wtoken.inPendingTransaction = false;
2831 mOpeningApps.remove(wtoken);
2832 if (mClosingApps.contains(wtoken)) {
2833 delayed = true;
2834 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2835 mClosingApps.add(wtoken);
2836 delayed = true;
2837 }
2838 if (DEBUG_APP_TRANSITIONS) Log.v(
2839 TAG, "Removing app " + wtoken + " delayed=" + delayed
2840 + " animation=" + wtoken.animation
2841 + " animating=" + wtoken.animating);
2842 if (delayed) {
2843 // set the token aside because it has an active animation to be finished
2844 mExitingAppTokens.add(wtoken);
2845 }
2846 mAppTokens.remove(wtoken);
2847 wtoken.removed = true;
2848 if (wtoken.startingData != null) {
2849 startingToken = wtoken;
2850 }
2851 unsetAppFreezingScreenLocked(wtoken, true, true);
2852 if (mFocusedApp == wtoken) {
2853 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
2854 mFocusedApp = null;
2855 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2856 mKeyWaiter.tickle();
2857 }
2858 } else {
2859 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
2860 }
Romain Guy06882f82009-06-10 13:36:04 -07002861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 if (!delayed && wtoken != null) {
2863 wtoken.updateReportedVisibilityLocked();
2864 }
2865 }
2866 Binder.restoreCallingIdentity(origId);
2867
2868 if (startingToken != null) {
2869 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
2870 + startingToken + ": app token removed");
2871 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
2872 mH.sendMessage(m);
2873 }
2874 }
2875
2876 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
2877 final int NW = token.windows.size();
2878 for (int i=0; i<NW; i++) {
2879 WindowState win = token.windows.get(i);
2880 mWindows.remove(win);
2881 int j = win.mChildWindows.size();
2882 while (j > 0) {
2883 j--;
2884 mWindows.remove(win.mChildWindows.get(j));
2885 }
2886 }
2887 return NW > 0;
2888 }
2889
2890 void dumpAppTokensLocked() {
2891 for (int i=mAppTokens.size()-1; i>=0; i--) {
2892 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
2893 }
2894 }
Romain Guy06882f82009-06-10 13:36:04 -07002895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 void dumpWindowsLocked() {
2897 for (int i=mWindows.size()-1; i>=0; i--) {
2898 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
2899 }
2900 }
Romain Guy06882f82009-06-10 13:36:04 -07002901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 private int findWindowOffsetLocked(int tokenPos) {
2903 final int NW = mWindows.size();
2904
2905 if (tokenPos >= mAppTokens.size()) {
2906 int i = NW;
2907 while (i > 0) {
2908 i--;
2909 WindowState win = (WindowState)mWindows.get(i);
2910 if (win.getAppToken() != null) {
2911 return i+1;
2912 }
2913 }
2914 }
2915
2916 while (tokenPos > 0) {
2917 // Find the first app token below the new position that has
2918 // a window displayed.
2919 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
2920 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
2921 + tokenPos + " -- " + wtoken.token);
2922 int i = wtoken.windows.size();
2923 while (i > 0) {
2924 i--;
2925 WindowState win = wtoken.windows.get(i);
2926 int j = win.mChildWindows.size();
2927 while (j > 0) {
2928 j--;
2929 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2930 if (cwin.mSubLayer >= 0 ) {
2931 for (int pos=NW-1; pos>=0; pos--) {
2932 if (mWindows.get(pos) == cwin) {
2933 if (DEBUG_REORDER) Log.v(TAG,
2934 "Found child win @" + (pos+1));
2935 return pos+1;
2936 }
2937 }
2938 }
2939 }
2940 for (int pos=NW-1; pos>=0; pos--) {
2941 if (mWindows.get(pos) == win) {
2942 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
2943 return pos+1;
2944 }
2945 }
2946 }
2947 tokenPos--;
2948 }
2949
2950 return 0;
2951 }
2952
2953 private final int reAddWindowLocked(int index, WindowState win) {
2954 final int NCW = win.mChildWindows.size();
2955 boolean added = false;
2956 for (int j=0; j<NCW; j++) {
2957 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2958 if (!added && cwin.mSubLayer >= 0) {
2959 mWindows.add(index, win);
2960 index++;
2961 added = true;
2962 }
2963 mWindows.add(index, cwin);
2964 index++;
2965 }
2966 if (!added) {
2967 mWindows.add(index, win);
2968 index++;
2969 }
2970 return index;
2971 }
Romain Guy06882f82009-06-10 13:36:04 -07002972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 private final int reAddAppWindowsLocked(int index, WindowToken token) {
2974 final int NW = token.windows.size();
2975 for (int i=0; i<NW; i++) {
2976 index = reAddWindowLocked(index, token.windows.get(i));
2977 }
2978 return index;
2979 }
2980
2981 public void moveAppToken(int index, IBinder token) {
2982 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2983 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002984 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 }
2986
2987 synchronized(mWindowMap) {
2988 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
2989 if (DEBUG_REORDER) dumpAppTokensLocked();
2990 final AppWindowToken wtoken = findAppWindowToken(token);
2991 if (wtoken == null || !mAppTokens.remove(wtoken)) {
2992 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
2993 + token + " (" + wtoken + ")");
2994 return;
2995 }
2996 mAppTokens.add(index, wtoken);
2997 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
2998 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 final long origId = Binder.clearCallingIdentity();
3001 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3002 if (DEBUG_REORDER) dumpWindowsLocked();
3003 if (tmpRemoveAppWindowsLocked(wtoken)) {
3004 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3005 if (DEBUG_REORDER) dumpWindowsLocked();
3006 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3007 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3008 if (DEBUG_REORDER) dumpWindowsLocked();
3009 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 mLayoutNeeded = true;
3011 performLayoutAndPlaceSurfacesLocked();
3012 }
3013 Binder.restoreCallingIdentity(origId);
3014 }
3015 }
3016
3017 private void removeAppTokensLocked(List<IBinder> tokens) {
3018 // XXX This should be done more efficiently!
3019 // (take advantage of the fact that both lists should be
3020 // ordered in the same way.)
3021 int N = tokens.size();
3022 for (int i=0; i<N; i++) {
3023 IBinder token = tokens.get(i);
3024 final AppWindowToken wtoken = findAppWindowToken(token);
3025 if (!mAppTokens.remove(wtoken)) {
3026 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3027 + token + " (" + wtoken + ")");
3028 i--;
3029 N--;
3030 }
3031 }
3032 }
3033
3034 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3035 // First remove all of the windows from the list.
3036 final int N = tokens.size();
3037 int i;
3038 for (i=0; i<N; i++) {
3039 WindowToken token = mTokenMap.get(tokens.get(i));
3040 if (token != null) {
3041 tmpRemoveAppWindowsLocked(token);
3042 }
3043 }
3044
3045 // Where to start adding?
3046 int pos = findWindowOffsetLocked(tokenPos);
3047
3048 // And now add them back at the correct place.
3049 for (i=0; i<N; i++) {
3050 WindowToken token = mTokenMap.get(tokens.get(i));
3051 if (token != null) {
3052 pos = reAddAppWindowsLocked(pos, token);
3053 }
3054 }
3055
3056 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 mLayoutNeeded = true;
3058 performLayoutAndPlaceSurfacesLocked();
3059
3060 //dump();
3061 }
3062
3063 public void moveAppTokensToTop(List<IBinder> tokens) {
3064 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3065 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003066 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 }
3068
3069 final long origId = Binder.clearCallingIdentity();
3070 synchronized(mWindowMap) {
3071 removeAppTokensLocked(tokens);
3072 final int N = tokens.size();
3073 for (int i=0; i<N; i++) {
3074 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3075 if (wt != null) {
3076 mAppTokens.add(wt);
3077 }
3078 }
3079 moveAppWindowsLocked(tokens, mAppTokens.size());
3080 }
3081 Binder.restoreCallingIdentity(origId);
3082 }
3083
3084 public void moveAppTokensToBottom(List<IBinder> tokens) {
3085 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3086 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003087 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 }
3089
3090 final long origId = Binder.clearCallingIdentity();
3091 synchronized(mWindowMap) {
3092 removeAppTokensLocked(tokens);
3093 final int N = tokens.size();
3094 int pos = 0;
3095 for (int i=0; i<N; i++) {
3096 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3097 if (wt != null) {
3098 mAppTokens.add(pos, wt);
3099 pos++;
3100 }
3101 }
3102 moveAppWindowsLocked(tokens, 0);
3103 }
3104 Binder.restoreCallingIdentity(origId);
3105 }
3106
3107 // -------------------------------------------------------------
3108 // Misc IWindowSession methods
3109 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 public void disableKeyguard(IBinder token, String tag) {
3112 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3113 != PackageManager.PERMISSION_GRANTED) {
3114 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3115 }
3116 mKeyguardDisabled.acquire(token, tag);
3117 }
3118
3119 public void reenableKeyguard(IBinder token) {
3120 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3121 != PackageManager.PERMISSION_GRANTED) {
3122 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3123 }
3124 synchronized (mKeyguardDisabled) {
3125 mKeyguardDisabled.release(token);
3126
3127 if (!mKeyguardDisabled.isAcquired()) {
3128 // if we are the last one to reenable the keyguard wait until
3129 // we have actaully finished reenabling until returning
3130 mWaitingUntilKeyguardReenabled = true;
3131 while (mWaitingUntilKeyguardReenabled) {
3132 try {
3133 mKeyguardDisabled.wait();
3134 } catch (InterruptedException e) {
3135 Thread.currentThread().interrupt();
3136 }
3137 }
3138 }
3139 }
3140 }
3141
3142 /**
3143 * @see android.app.KeyguardManager#exitKeyguardSecurely
3144 */
3145 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3146 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3147 != PackageManager.PERMISSION_GRANTED) {
3148 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3149 }
3150 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3151 public void onKeyguardExitResult(boolean success) {
3152 try {
3153 callback.onKeyguardExitResult(success);
3154 } catch (RemoteException e) {
3155 // Client has died, we don't care.
3156 }
3157 }
3158 });
3159 }
3160
3161 public boolean inKeyguardRestrictedInputMode() {
3162 return mPolicy.inKeyguardRestrictedKeyInputMode();
3163 }
Romain Guy06882f82009-06-10 13:36:04 -07003164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 static float fixScale(float scale) {
3166 if (scale < 0) scale = 0;
3167 else if (scale > 20) scale = 20;
3168 return Math.abs(scale);
3169 }
Romain Guy06882f82009-06-10 13:36:04 -07003170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 public void setAnimationScale(int which, float scale) {
3172 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3173 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003174 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 }
3176
3177 if (scale < 0) scale = 0;
3178 else if (scale > 20) scale = 20;
3179 scale = Math.abs(scale);
3180 switch (which) {
3181 case 0: mWindowAnimationScale = fixScale(scale); break;
3182 case 1: mTransitionAnimationScale = fixScale(scale); break;
3183 }
Romain Guy06882f82009-06-10 13:36:04 -07003184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185 // Persist setting
3186 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3187 }
Romain Guy06882f82009-06-10 13:36:04 -07003188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 public void setAnimationScales(float[] scales) {
3190 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3191 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003192 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 }
3194
3195 if (scales != null) {
3196 if (scales.length >= 1) {
3197 mWindowAnimationScale = fixScale(scales[0]);
3198 }
3199 if (scales.length >= 2) {
3200 mTransitionAnimationScale = fixScale(scales[1]);
3201 }
3202 }
Romain Guy06882f82009-06-10 13:36:04 -07003203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 // Persist setting
3205 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3206 }
Romain Guy06882f82009-06-10 13:36:04 -07003207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 public float getAnimationScale(int which) {
3209 switch (which) {
3210 case 0: return mWindowAnimationScale;
3211 case 1: return mTransitionAnimationScale;
3212 }
3213 return 0;
3214 }
Romain Guy06882f82009-06-10 13:36:04 -07003215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 public float[] getAnimationScales() {
3217 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3218 }
Romain Guy06882f82009-06-10 13:36:04 -07003219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 public int getSwitchState(int sw) {
3221 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3222 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003223 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 }
3225 return KeyInputQueue.getSwitchState(sw);
3226 }
Romain Guy06882f82009-06-10 13:36:04 -07003227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 public int getSwitchStateForDevice(int devid, int sw) {
3229 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3230 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003231 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 }
3233 return KeyInputQueue.getSwitchState(devid, sw);
3234 }
Romain Guy06882f82009-06-10 13:36:04 -07003235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 public int getScancodeState(int sw) {
3237 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3238 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003239 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 }
3241 return KeyInputQueue.getScancodeState(sw);
3242 }
Romain Guy06882f82009-06-10 13:36:04 -07003243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 public int getScancodeStateForDevice(int devid, int sw) {
3245 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3246 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003247 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 }
3249 return KeyInputQueue.getScancodeState(devid, sw);
3250 }
Romain Guy06882f82009-06-10 13:36:04 -07003251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 public int getKeycodeState(int sw) {
3253 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3254 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003255 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 }
3257 return KeyInputQueue.getKeycodeState(sw);
3258 }
Romain Guy06882f82009-06-10 13:36:04 -07003259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 public int getKeycodeStateForDevice(int devid, int sw) {
3261 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3262 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003263 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 }
3265 return KeyInputQueue.getKeycodeState(devid, sw);
3266 }
Romain Guy06882f82009-06-10 13:36:04 -07003267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3269 return KeyInputQueue.hasKeys(keycodes, keyExists);
3270 }
Romain Guy06882f82009-06-10 13:36:04 -07003271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 public void enableScreenAfterBoot() {
3273 synchronized(mWindowMap) {
3274 if (mSystemBooted) {
3275 return;
3276 }
3277 mSystemBooted = true;
3278 }
Romain Guy06882f82009-06-10 13:36:04 -07003279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 performEnableScreen();
3281 }
Romain Guy06882f82009-06-10 13:36:04 -07003282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 public void enableScreenIfNeededLocked() {
3284 if (mDisplayEnabled) {
3285 return;
3286 }
3287 if (!mSystemBooted) {
3288 return;
3289 }
3290 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3291 }
Romain Guy06882f82009-06-10 13:36:04 -07003292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 public void performEnableScreen() {
3294 synchronized(mWindowMap) {
3295 if (mDisplayEnabled) {
3296 return;
3297 }
3298 if (!mSystemBooted) {
3299 return;
3300 }
Romain Guy06882f82009-06-10 13:36:04 -07003301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 // Don't enable the screen until all existing windows
3303 // have been drawn.
3304 final int N = mWindows.size();
3305 for (int i=0; i<N; i++) {
3306 WindowState w = (WindowState)mWindows.get(i);
3307 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3308 return;
3309 }
3310 }
Romain Guy06882f82009-06-10 13:36:04 -07003311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 mDisplayEnabled = true;
3313 if (false) {
3314 Log.i(TAG, "ENABLING SCREEN!");
3315 StringWriter sw = new StringWriter();
3316 PrintWriter pw = new PrintWriter(sw);
3317 this.dump(null, pw, null);
3318 Log.i(TAG, sw.toString());
3319 }
3320 try {
3321 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3322 if (surfaceFlinger != null) {
3323 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3324 Parcel data = Parcel.obtain();
3325 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3326 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3327 data, null, 0);
3328 data.recycle();
3329 }
3330 } catch (RemoteException ex) {
3331 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3332 }
3333 }
Romain Guy06882f82009-06-10 13:36:04 -07003334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003338 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3339 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 }
Romain Guy06882f82009-06-10 13:36:04 -07003341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 public void setInTouchMode(boolean mode) {
3343 synchronized(mWindowMap) {
3344 mInTouchMode = mode;
3345 }
3346 }
3347
Romain Guy06882f82009-06-10 13:36:04 -07003348 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003349 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003351 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003352 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 }
3354
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003355 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 }
Romain Guy06882f82009-06-10 13:36:04 -07003357
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003358 public void setRotationUnchecked(int rotation,
3359 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 if(DEBUG_ORIENTATION) Log.v(TAG,
3361 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 long origId = Binder.clearCallingIdentity();
3364 boolean changed;
3365 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003366 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 }
Romain Guy06882f82009-06-10 13:36:04 -07003368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 if (changed) {
3370 sendNewConfiguration();
3371 synchronized(mWindowMap) {
3372 mLayoutNeeded = true;
3373 performLayoutAndPlaceSurfacesLocked();
3374 }
3375 } else if (alwaysSendConfiguration) {
3376 //update configuration ignoring orientation change
3377 sendNewConfiguration();
3378 }
Romain Guy06882f82009-06-10 13:36:04 -07003379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 Binder.restoreCallingIdentity(origId);
3381 }
Romain Guy06882f82009-06-10 13:36:04 -07003382
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003383 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 boolean changed;
3385 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3386 rotation = mRequestedRotation;
3387 } else {
3388 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003389 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 }
3391 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003392 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 mRotation, mDisplayEnabled);
3394 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3395 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003398 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 "Rotation changed to " + rotation
3400 + " from " + mRotation
3401 + " (forceApp=" + mForcedAppOrientation
3402 + ", req=" + mRequestedRotation + ")");
3403 mRotation = rotation;
3404 mWindowsFreezingScreen = true;
3405 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3406 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3407 2000);
3408 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003409 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 mQueue.setOrientation(rotation);
3411 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003412 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 }
3414 for (int i=mWindows.size()-1; i>=0; i--) {
3415 WindowState w = (WindowState)mWindows.get(i);
3416 if (w.mSurface != null) {
3417 w.mOrientationChanging = true;
3418 }
3419 }
3420 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3421 try {
3422 mRotationWatchers.get(i).onRotationChanged(rotation);
3423 } catch (RemoteException e) {
3424 }
3425 }
3426 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 return changed;
3429 }
Romain Guy06882f82009-06-10 13:36:04 -07003430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 public int getRotation() {
3432 return mRotation;
3433 }
3434
3435 public int watchRotation(IRotationWatcher watcher) {
3436 final IBinder watcherBinder = watcher.asBinder();
3437 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3438 public void binderDied() {
3439 synchronized (mWindowMap) {
3440 for (int i=0; i<mRotationWatchers.size(); i++) {
3441 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003442 IRotationWatcher removed = mRotationWatchers.remove(i);
3443 if (removed != null) {
3444 removed.asBinder().unlinkToDeath(this, 0);
3445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 i--;
3447 }
3448 }
3449 }
3450 }
3451 };
Romain Guy06882f82009-06-10 13:36:04 -07003452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003453 synchronized (mWindowMap) {
3454 try {
3455 watcher.asBinder().linkToDeath(dr, 0);
3456 mRotationWatchers.add(watcher);
3457 } catch (RemoteException e) {
3458 // Client died, no cleanup needed.
3459 }
Romain Guy06882f82009-06-10 13:36:04 -07003460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 return mRotation;
3462 }
3463 }
3464
3465 /**
3466 * Starts the view server on the specified port.
3467 *
3468 * @param port The port to listener to.
3469 *
3470 * @return True if the server was successfully started, false otherwise.
3471 *
3472 * @see com.android.server.ViewServer
3473 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3474 */
3475 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003476 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 return false;
3478 }
3479
3480 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3481 return false;
3482 }
3483
3484 if (port < 1024) {
3485 return false;
3486 }
3487
3488 if (mViewServer != null) {
3489 if (!mViewServer.isRunning()) {
3490 try {
3491 return mViewServer.start();
3492 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003493 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 }
3495 }
3496 return false;
3497 }
3498
3499 try {
3500 mViewServer = new ViewServer(this, port);
3501 return mViewServer.start();
3502 } catch (IOException e) {
3503 Log.w(TAG, "View server did not start");
3504 }
3505 return false;
3506 }
3507
Romain Guy06882f82009-06-10 13:36:04 -07003508 private boolean isSystemSecure() {
3509 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3510 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3511 }
3512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 /**
3514 * Stops the view server if it exists.
3515 *
3516 * @return True if the server stopped, false if it wasn't started or
3517 * couldn't be stopped.
3518 *
3519 * @see com.android.server.ViewServer
3520 */
3521 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003522 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 return false;
3524 }
3525
3526 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3527 return false;
3528 }
3529
3530 if (mViewServer != null) {
3531 return mViewServer.stop();
3532 }
3533 return false;
3534 }
3535
3536 /**
3537 * Indicates whether the view server is running.
3538 *
3539 * @return True if the server is running, false otherwise.
3540 *
3541 * @see com.android.server.ViewServer
3542 */
3543 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003544 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 return false;
3546 }
3547
3548 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3549 return false;
3550 }
3551
3552 return mViewServer != null && mViewServer.isRunning();
3553 }
3554
3555 /**
3556 * Lists all availble windows in the system. The listing is written in the
3557 * specified Socket's output stream with the following syntax:
3558 * windowHashCodeInHexadecimal windowName
3559 * Each line of the ouput represents a different window.
3560 *
3561 * @param client The remote client to send the listing to.
3562 * @return False if an error occured, true otherwise.
3563 */
3564 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003565 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 return false;
3567 }
3568
3569 boolean result = true;
3570
3571 Object[] windows;
3572 synchronized (mWindowMap) {
3573 windows = new Object[mWindows.size()];
3574 //noinspection unchecked
3575 windows = mWindows.toArray(windows);
3576 }
3577
3578 BufferedWriter out = null;
3579
3580 // Any uncaught exception will crash the system process
3581 try {
3582 OutputStream clientStream = client.getOutputStream();
3583 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3584
3585 final int count = windows.length;
3586 for (int i = 0; i < count; i++) {
3587 final WindowState w = (WindowState) windows[i];
3588 out.write(Integer.toHexString(System.identityHashCode(w)));
3589 out.write(' ');
3590 out.append(w.mAttrs.getTitle());
3591 out.write('\n');
3592 }
3593
3594 out.write("DONE.\n");
3595 out.flush();
3596 } catch (Exception e) {
3597 result = false;
3598 } finally {
3599 if (out != null) {
3600 try {
3601 out.close();
3602 } catch (IOException e) {
3603 result = false;
3604 }
3605 }
3606 }
3607
3608 return result;
3609 }
3610
3611 /**
3612 * Sends a command to a target window. The result of the command, if any, will be
3613 * written in the output stream of the specified socket.
3614 *
3615 * The parameters must follow this syntax:
3616 * windowHashcode extra
3617 *
3618 * Where XX is the length in characeters of the windowTitle.
3619 *
3620 * The first parameter is the target window. The window with the specified hashcode
3621 * will be the target. If no target can be found, nothing happens. The extra parameters
3622 * will be delivered to the target window and as parameters to the command itself.
3623 *
3624 * @param client The remote client to sent the result, if any, to.
3625 * @param command The command to execute.
3626 * @param parameters The command parameters.
3627 *
3628 * @return True if the command was successfully delivered, false otherwise. This does
3629 * not indicate whether the command itself was successful.
3630 */
3631 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003632 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 return false;
3634 }
3635
3636 boolean success = true;
3637 Parcel data = null;
3638 Parcel reply = null;
3639
3640 // Any uncaught exception will crash the system process
3641 try {
3642 // Find the hashcode of the window
3643 int index = parameters.indexOf(' ');
3644 if (index == -1) {
3645 index = parameters.length();
3646 }
3647 final String code = parameters.substring(0, index);
3648 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3649
3650 // Extract the command's parameter after the window description
3651 if (index < parameters.length()) {
3652 parameters = parameters.substring(index + 1);
3653 } else {
3654 parameters = "";
3655 }
3656
3657 final WindowManagerService.WindowState window = findWindow(hashCode);
3658 if (window == null) {
3659 return false;
3660 }
3661
3662 data = Parcel.obtain();
3663 data.writeInterfaceToken("android.view.IWindow");
3664 data.writeString(command);
3665 data.writeString(parameters);
3666 data.writeInt(1);
3667 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3668
3669 reply = Parcel.obtain();
3670
3671 final IBinder binder = window.mClient.asBinder();
3672 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3673 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3674
3675 reply.readException();
3676
3677 } catch (Exception e) {
3678 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3679 success = false;
3680 } finally {
3681 if (data != null) {
3682 data.recycle();
3683 }
3684 if (reply != null) {
3685 reply.recycle();
3686 }
3687 }
3688
3689 return success;
3690 }
3691
3692 private WindowState findWindow(int hashCode) {
3693 if (hashCode == -1) {
3694 return getFocusedWindow();
3695 }
3696
3697 synchronized (mWindowMap) {
3698 final ArrayList windows = mWindows;
3699 final int count = windows.size();
3700
3701 for (int i = 0; i < count; i++) {
3702 WindowState w = (WindowState) windows.get(i);
3703 if (System.identityHashCode(w) == hashCode) {
3704 return w;
3705 }
3706 }
3707 }
3708
3709 return null;
3710 }
3711
3712 /*
3713 * Instruct the Activity Manager to fetch the current configuration and broadcast
3714 * that to config-changed listeners if appropriate.
3715 */
3716 void sendNewConfiguration() {
3717 try {
3718 mActivityManager.updateConfiguration(null);
3719 } catch (RemoteException e) {
3720 }
3721 }
Romain Guy06882f82009-06-10 13:36:04 -07003722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 public Configuration computeNewConfiguration() {
3724 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003725 return computeNewConfigurationLocked();
3726 }
3727 }
Romain Guy06882f82009-06-10 13:36:04 -07003728
Dianne Hackbornc485a602009-03-24 22:39:49 -07003729 Configuration computeNewConfigurationLocked() {
3730 Configuration config = new Configuration();
3731 if (!computeNewConfigurationLocked(config)) {
3732 return null;
3733 }
3734 Log.i(TAG, "Config changed: " + config);
3735 long now = SystemClock.uptimeMillis();
3736 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3737 if (mFreezeGcPending != 0) {
3738 if (now > (mFreezeGcPending+1000)) {
3739 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3740 mH.removeMessages(H.FORCE_GC);
3741 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 mFreezeGcPending = now;
3743 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003744 } else {
3745 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003747 return config;
3748 }
Romain Guy06882f82009-06-10 13:36:04 -07003749
Dianne Hackbornc485a602009-03-24 22:39:49 -07003750 boolean computeNewConfigurationLocked(Configuration config) {
3751 if (mDisplay == null) {
3752 return false;
3753 }
3754 mQueue.getInputConfiguration(config);
3755 final int dw = mDisplay.getWidth();
3756 final int dh = mDisplay.getHeight();
3757 int orientation = Configuration.ORIENTATION_SQUARE;
3758 if (dw < dh) {
3759 orientation = Configuration.ORIENTATION_PORTRAIT;
3760 } else if (dw > dh) {
3761 orientation = Configuration.ORIENTATION_LANDSCAPE;
3762 }
3763 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003764
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003765 DisplayMetrics dm = new DisplayMetrics();
3766 mDisplay.getMetrics(dm);
3767 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
3768
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003769 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07003770 // Note we only do this once because at this point we don't
3771 // expect the screen to change in this way at runtime, and want
3772 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07003773 int longSize = dw;
3774 int shortSize = dh;
3775 if (longSize < shortSize) {
3776 int tmp = longSize;
3777 longSize = shortSize;
3778 shortSize = tmp;
3779 }
3780 longSize = (int)(longSize/dm.density);
3781 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003782
Dianne Hackborn723738c2009-06-25 19:48:04 -07003783 // These semi-magic numbers define our compatibility modes for
3784 // applications with different screens. Don't change unless you
3785 // make sure to test lots and lots of apps!
3786 if (longSize < 470) {
3787 // This is shorter than an HVGA normal density screen (which
3788 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003789 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
3790 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003791 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003792 // Is this a large screen?
3793 if (longSize > 640 && shortSize >= 480) {
3794 // VGA or larger screens at medium density are the point
3795 // at which we consider it to be a large screen.
3796 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
3797 } else {
3798 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
3799
3800 // If this screen is wider than normal HVGA, or taller
3801 // than FWVGA, then for old apps we want to run in size
3802 // compatibility mode.
3803 if (shortSize > 321 || longSize > 570) {
3804 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
3805 }
3806 }
3807
3808 // Is this a long screen?
3809 if (((longSize*3)/5) >= (shortSize-1)) {
3810 // Anything wider than WVGA (5:3) is considering to be long.
3811 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
3812 } else {
3813 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
3814 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07003815 }
3816 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003817 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003818
Dianne Hackbornc485a602009-03-24 22:39:49 -07003819 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3820 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3821 mPolicy.adjustConfigurationLw(config);
3822 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 }
Romain Guy06882f82009-06-10 13:36:04 -07003824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 // -------------------------------------------------------------
3826 // Input Events and Focus Management
3827 // -------------------------------------------------------------
3828
3829 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07003830 long curTime = SystemClock.uptimeMillis();
3831
Michael Chane10de972009-05-18 11:24:50 -07003832 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07003833 if (mLastTouchEventType == eventType &&
3834 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
3835 return;
3836 }
3837 mLastUserActivityCallTime = curTime;
3838 mLastTouchEventType = eventType;
3839 }
3840
3841 if (targetWin == null
3842 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
3843 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 }
3845 }
3846
3847 // tells if it's a cheek event or not -- this function is stateful
3848 private static final int EVENT_NONE = 0;
3849 private static final int EVENT_UNKNOWN = 0;
3850 private static final int EVENT_CHEEK = 0;
3851 private static final int EVENT_IGNORE_DURATION = 300; // ms
3852 private static final float CHEEK_THRESHOLD = 0.6f;
3853 private int mEventState = EVENT_NONE;
3854 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856 private int eventType(MotionEvent ev) {
3857 float size = ev.getSize();
3858 switch (ev.getAction()) {
3859 case MotionEvent.ACTION_DOWN:
3860 mEventSize = size;
3861 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
3862 case MotionEvent.ACTION_UP:
3863 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003864 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 case MotionEvent.ACTION_MOVE:
3866 final int N = ev.getHistorySize();
3867 if (size > mEventSize) mEventSize = size;
3868 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3869 for (int i=0; i<N; i++) {
3870 size = ev.getHistoricalSize(i);
3871 if (size > mEventSize) mEventSize = size;
3872 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3873 }
3874 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
3875 return TOUCH_EVENT;
3876 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003877 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 }
3879 default:
3880 // not good
3881 return OTHER_EVENT;
3882 }
3883 }
3884
3885 /**
3886 * @return Returns true if event was dispatched, false if it was dropped for any reason
3887 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07003888 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
3890 "dispatchPointer " + ev);
3891
Michael Chan53071d62009-05-13 17:29:48 -07003892 if (MEASURE_LATENCY) {
3893 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
3894 }
3895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07003897 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898
Michael Chan53071d62009-05-13 17:29:48 -07003899 if (MEASURE_LATENCY) {
3900 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
3901 }
3902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07003904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 if (action == MotionEvent.ACTION_UP) {
3906 // let go of our target
3907 mKeyWaiter.mMotionTarget = null;
3908 mPowerManager.logPointerUpEvent();
3909 } else if (action == MotionEvent.ACTION_DOWN) {
3910 mPowerManager.logPointerDownEvent();
3911 }
3912
3913 if (targetObj == null) {
3914 // In this case we are either dropping the event, or have received
3915 // a move or up without a down. It is common to receive move
3916 // events in such a way, since this means the user is moving the
3917 // pointer without actually pressing down. All other cases should
3918 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07003919 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
3921 }
3922 if (qev != null) {
3923 mQueue.recycleEvent(qev);
3924 }
3925 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003926 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 }
3928 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
3929 if (qev != null) {
3930 mQueue.recycleEvent(qev);
3931 }
3932 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003933 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 }
Romain Guy06882f82009-06-10 13:36:04 -07003935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07003937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003938 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07003939 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940
3941 //Log.i(TAG, "Sending " + ev + " to " + target);
3942
3943 if (uid != 0 && uid != target.mSession.mUid) {
3944 if (mContext.checkPermission(
3945 android.Manifest.permission.INJECT_EVENTS, pid, uid)
3946 != PackageManager.PERMISSION_GRANTED) {
3947 Log.w(TAG, "Permission denied: injecting pointer event from pid "
3948 + pid + " uid " + uid + " to window " + target
3949 + " owned by uid " + target.mSession.mUid);
3950 if (qev != null) {
3951 mQueue.recycleEvent(qev);
3952 }
3953 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003954 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 }
3956 }
3957
Michael Chan53071d62009-05-13 17:29:48 -07003958 if (MEASURE_LATENCY) {
3959 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
3960 }
3961
Romain Guy06882f82009-06-10 13:36:04 -07003962 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
3964 //target wants to ignore fat touch events
3965 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
3966 //explicit flag to return without processing event further
3967 boolean returnFlag = false;
3968 if((action == MotionEvent.ACTION_DOWN)) {
3969 mFatTouch = false;
3970 if(cheekPress) {
3971 mFatTouch = true;
3972 returnFlag = true;
3973 }
3974 } else {
3975 if(action == MotionEvent.ACTION_UP) {
3976 if(mFatTouch) {
3977 //earlier even was invalid doesnt matter if current up is cheekpress or not
3978 mFatTouch = false;
3979 returnFlag = true;
3980 } else if(cheekPress) {
3981 //cancel the earlier event
3982 ev.setAction(MotionEvent.ACTION_CANCEL);
3983 action = MotionEvent.ACTION_CANCEL;
3984 }
3985 } else if(action == MotionEvent.ACTION_MOVE) {
3986 if(mFatTouch) {
3987 //two cases here
3988 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07003989 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003990 returnFlag = true;
3991 } else if(cheekPress) {
3992 //valid down followed by invalid moves
3993 //an invalid move have to cancel earlier action
3994 ev.setAction(MotionEvent.ACTION_CANCEL);
3995 action = MotionEvent.ACTION_CANCEL;
3996 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
3997 //note that the subsequent invalid moves will not get here
3998 mFatTouch = true;
3999 }
4000 }
4001 } //else if action
4002 if(returnFlag) {
4003 //recycle que, ev
4004 if (qev != null) {
4005 mQueue.recycleEvent(qev);
4006 }
4007 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004008 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 }
4010 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004011
4012 // TODO remove once we settle on a value or make it app specific
4013 if (action == MotionEvent.ACTION_DOWN) {
4014 int max_events_per_sec = 35;
4015 try {
4016 max_events_per_sec = Integer.parseInt(SystemProperties
4017 .get("windowsmgr.max_events_per_sec"));
4018 if (max_events_per_sec < 1) {
4019 max_events_per_sec = 35;
4020 }
4021 } catch (NumberFormatException e) {
4022 }
4023 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4024 }
4025
4026 /*
4027 * Throttle events to minimize CPU usage when there's a flood of events
4028 * e.g. constant contact with the screen
4029 */
4030 if (action == MotionEvent.ACTION_MOVE) {
4031 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4032 long now = SystemClock.uptimeMillis();
4033 if (now < nextEventTime) {
4034 try {
4035 Thread.sleep(nextEventTime - now);
4036 } catch (InterruptedException e) {
4037 }
4038 mLastTouchEventTime = nextEventTime;
4039 } else {
4040 mLastTouchEventTime = now;
4041 }
4042 }
4043
Michael Chan53071d62009-05-13 17:29:48 -07004044 if (MEASURE_LATENCY) {
4045 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4046 }
4047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 synchronized(mWindowMap) {
4049 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4050 mKeyWaiter.bindTargetWindowLocked(target,
4051 KeyWaiter.RETURN_PENDING_POINTER, qev);
4052 ev = null;
4053 } else {
4054 if (action == MotionEvent.ACTION_DOWN) {
4055 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4056 if (out != null) {
4057 MotionEvent oev = MotionEvent.obtain(ev);
4058 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4059 do {
4060 final Rect frame = out.mFrame;
4061 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4062 try {
4063 out.mClient.dispatchPointer(oev, eventTime);
4064 } catch (android.os.RemoteException e) {
4065 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4066 }
4067 oev.offsetLocation((float)frame.left, (float)frame.top);
4068 out = out.mNextOutsideTouch;
4069 } while (out != null);
4070 mKeyWaiter.mOutsideTouchTargets = null;
4071 }
4072 }
4073 final Rect frame = target.mFrame;
4074 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4075 mKeyWaiter.bindTargetWindowLocked(target);
4076 }
4077 }
Romain Guy06882f82009-06-10 13:36:04 -07004078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 // finally offset the event to the target's coordinate system and
4080 // dispatch the event.
4081 try {
4082 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4083 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4084 }
Michael Chan53071d62009-05-13 17:29:48 -07004085
4086 if (MEASURE_LATENCY) {
4087 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4088 }
4089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 target.mClient.dispatchPointer(ev, eventTime);
Michael Chan53071d62009-05-13 17:29:48 -07004091
4092 if (MEASURE_LATENCY) {
4093 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4094 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004095 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 } catch (android.os.RemoteException e) {
4097 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4098 mKeyWaiter.mMotionTarget = null;
4099 try {
4100 removeWindow(target.mSession, target.mClient);
4101 } catch (java.util.NoSuchElementException ex) {
4102 // This will happen if the window has already been
4103 // removed.
4104 }
4105 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004106 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 }
Romain Guy06882f82009-06-10 13:36:04 -07004108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 /**
4110 * @return Returns true if event was dispatched, false if it was dropped for any reason
4111 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004112 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 if (DEBUG_INPUT) Log.v(
4114 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004117 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 if (focusObj == null) {
4119 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4120 if (qev != null) {
4121 mQueue.recycleEvent(qev);
4122 }
4123 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004124 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 }
4126 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4127 if (qev != null) {
4128 mQueue.recycleEvent(qev);
4129 }
4130 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004131 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 }
Romain Guy06882f82009-06-10 13:36:04 -07004133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 if (uid != 0 && uid != focus.mSession.mUid) {
4137 if (mContext.checkPermission(
4138 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4139 != PackageManager.PERMISSION_GRANTED) {
4140 Log.w(TAG, "Permission denied: injecting key event from pid "
4141 + pid + " uid " + uid + " to window " + focus
4142 + " owned by uid " + focus.mSession.mUid);
4143 if (qev != null) {
4144 mQueue.recycleEvent(qev);
4145 }
4146 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004147 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 }
4149 }
Romain Guy06882f82009-06-10 13:36:04 -07004150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 synchronized(mWindowMap) {
4154 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4155 mKeyWaiter.bindTargetWindowLocked(focus,
4156 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4157 // We don't deliver movement events to the client, we hold
4158 // them and wait for them to call back.
4159 ev = null;
4160 } else {
4161 mKeyWaiter.bindTargetWindowLocked(focus);
4162 }
4163 }
Romain Guy06882f82009-06-10 13:36:04 -07004164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 try {
4166 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004167 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 } catch (android.os.RemoteException e) {
4169 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4170 try {
4171 removeWindow(focus.mSession, focus.mClient);
4172 } catch (java.util.NoSuchElementException ex) {
4173 // This will happen if the window has already been
4174 // removed.
4175 }
4176 }
Romain Guy06882f82009-06-10 13:36:04 -07004177
Dianne Hackborncfaef692009-06-15 14:24:44 -07004178 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 }
Romain Guy06882f82009-06-10 13:36:04 -07004180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 /**
4182 * @return Returns true if event was dispatched, false if it was dropped for any reason
4183 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004184 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4186
4187 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004188 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 if (focusObj == null) {
4190 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004191 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 }
4193 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004194 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 }
Romain Guy06882f82009-06-10 13:36:04 -07004196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 if (DEBUG_INPUT) Log.v(
4200 TAG, "Dispatching to " + focus + ": " + event);
4201
4202 if (uid != 0 && uid != focus.mSession.mUid) {
4203 if (mContext.checkPermission(
4204 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4205 != PackageManager.PERMISSION_GRANTED) {
4206 Log.w(TAG, "Permission denied: injecting key event from pid "
4207 + pid + " uid " + uid + " to window " + focus
4208 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004209 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 }
4211 }
Romain Guy06882f82009-06-10 13:36:04 -07004212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 synchronized(mWindowMap) {
4214 mKeyWaiter.bindTargetWindowLocked(focus);
4215 }
4216
4217 // NOSHIP extra state logging
4218 mKeyWaiter.recordDispatchState(event, focus);
4219 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 try {
4222 if (DEBUG_INPUT || DEBUG_FOCUS) {
4223 Log.v(TAG, "Delivering key " + event.getKeyCode()
4224 + " to " + focus);
4225 }
4226 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004227 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 } catch (android.os.RemoteException e) {
4229 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4230 try {
4231 removeWindow(focus.mSession, focus.mClient);
4232 } catch (java.util.NoSuchElementException ex) {
4233 // This will happen if the window has already been
4234 // removed.
4235 }
4236 }
Romain Guy06882f82009-06-10 13:36:04 -07004237
Dianne Hackborncfaef692009-06-15 14:24:44 -07004238 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 }
Romain Guy06882f82009-06-10 13:36:04 -07004240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 public void pauseKeyDispatching(IBinder _token) {
4242 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4243 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004244 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 }
4246
4247 synchronized (mWindowMap) {
4248 WindowToken token = mTokenMap.get(_token);
4249 if (token != null) {
4250 mKeyWaiter.pauseDispatchingLocked(token);
4251 }
4252 }
4253 }
4254
4255 public void resumeKeyDispatching(IBinder _token) {
4256 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4257 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004258 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 }
4260
4261 synchronized (mWindowMap) {
4262 WindowToken token = mTokenMap.get(_token);
4263 if (token != null) {
4264 mKeyWaiter.resumeDispatchingLocked(token);
4265 }
4266 }
4267 }
4268
4269 public void setEventDispatching(boolean enabled) {
4270 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4271 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004272 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004273 }
4274
4275 synchronized (mWindowMap) {
4276 mKeyWaiter.setEventDispatchingLocked(enabled);
4277 }
4278 }
Romain Guy06882f82009-06-10 13:36:04 -07004279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 /**
4281 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004282 *
4283 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004284 * {@link SystemClock#uptimeMillis()} as the timebase.)
4285 * @param sync If true, wait for the event to be completed before returning to the caller.
4286 * @return Returns true if event was dispatched, false if it was dropped for any reason
4287 */
4288 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4289 long downTime = ev.getDownTime();
4290 long eventTime = ev.getEventTime();
4291
4292 int action = ev.getAction();
4293 int code = ev.getKeyCode();
4294 int repeatCount = ev.getRepeatCount();
4295 int metaState = ev.getMetaState();
4296 int deviceId = ev.getDeviceId();
4297 int scancode = ev.getScanCode();
4298
4299 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4300 if (downTime == 0) downTime = eventTime;
4301
4302 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004303 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004305 final int pid = Binder.getCallingPid();
4306 final int uid = Binder.getCallingUid();
4307 final long ident = Binder.clearCallingIdentity();
4308 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004310 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004312 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004313 switch (result) {
4314 case INJECT_NO_PERMISSION:
4315 throw new SecurityException(
4316 "Injecting to another application requires INJECT_EVENT permission");
4317 case INJECT_SUCCEEDED:
4318 return true;
4319 }
4320 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 }
4322
4323 /**
4324 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004325 *
4326 * @param ev A motion event describing the pointer (touch) action. (As noted in
4327 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004328 * {@link SystemClock#uptimeMillis()} as the timebase.)
4329 * @param sync If true, wait for the event to be completed before returning to the caller.
4330 * @return Returns true if event was dispatched, false if it was dropped for any reason
4331 */
4332 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004333 final int pid = Binder.getCallingPid();
4334 final int uid = Binder.getCallingUid();
4335 final long ident = Binder.clearCallingIdentity();
4336 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004338 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004340 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004341 switch (result) {
4342 case INJECT_NO_PERMISSION:
4343 throw new SecurityException(
4344 "Injecting to another application requires INJECT_EVENT permission");
4345 case INJECT_SUCCEEDED:
4346 return true;
4347 }
4348 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 }
Romain Guy06882f82009-06-10 13:36:04 -07004350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 /**
4352 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004353 *
4354 * @param ev A motion event describing the trackball action. (As noted in
4355 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 * {@link SystemClock#uptimeMillis()} as the timebase.)
4357 * @param sync If true, wait for the event to be completed before returning to the caller.
4358 * @return Returns true if event was dispatched, false if it was dropped for any reason
4359 */
4360 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004361 final int pid = Binder.getCallingPid();
4362 final int uid = Binder.getCallingUid();
4363 final long ident = Binder.clearCallingIdentity();
4364 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004366 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004368 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004369 switch (result) {
4370 case INJECT_NO_PERMISSION:
4371 throw new SecurityException(
4372 "Injecting to another application requires INJECT_EVENT permission");
4373 case INJECT_SUCCEEDED:
4374 return true;
4375 }
4376 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 }
Romain Guy06882f82009-06-10 13:36:04 -07004378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 private WindowState getFocusedWindow() {
4380 synchronized (mWindowMap) {
4381 return getFocusedWindowLocked();
4382 }
4383 }
4384
4385 private WindowState getFocusedWindowLocked() {
4386 return mCurrentFocus;
4387 }
Romain Guy06882f82009-06-10 13:36:04 -07004388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004389 /**
4390 * This class holds the state for dispatching key events. This state
4391 * is protected by the KeyWaiter instance, NOT by the window lock. You
4392 * can be holding the main window lock while acquire the KeyWaiter lock,
4393 * but not the other way around.
4394 */
4395 final class KeyWaiter {
4396 // NOSHIP debugging
4397 public class DispatchState {
4398 private KeyEvent event;
4399 private WindowState focus;
4400 private long time;
4401 private WindowState lastWin;
4402 private IBinder lastBinder;
4403 private boolean finished;
4404 private boolean gotFirstWindow;
4405 private boolean eventDispatching;
4406 private long timeToSwitch;
4407 private boolean wasFrozen;
4408 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004409 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4412 focus = theFocus;
4413 event = theEvent;
4414 time = System.currentTimeMillis();
4415 // snapshot KeyWaiter state
4416 lastWin = mLastWin;
4417 lastBinder = mLastBinder;
4418 finished = mFinished;
4419 gotFirstWindow = mGotFirstWindow;
4420 eventDispatching = mEventDispatching;
4421 timeToSwitch = mTimeToSwitch;
4422 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004423 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 // cache the paused state at ctor time as well
4425 if (theFocus == null || theFocus.mToken == null) {
4426 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4427 focusPaused = false;
4428 } else {
4429 focusPaused = theFocus.mToken.paused;
4430 }
4431 }
Romain Guy06882f82009-06-10 13:36:04 -07004432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 public String toString() {
4434 return "{{" + event + " to " + focus + " @ " + time
4435 + " lw=" + lastWin + " lb=" + lastBinder
4436 + " fin=" + finished + " gfw=" + gotFirstWindow
4437 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004438 + " wf=" + wasFrozen + " fp=" + focusPaused
4439 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 }
4441 };
4442 private DispatchState mDispatchState = null;
4443 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4444 mDispatchState = new DispatchState(theEvent, theFocus);
4445 }
4446 // END NOSHIP
4447
4448 public static final int RETURN_NOTHING = 0;
4449 public static final int RETURN_PENDING_POINTER = 1;
4450 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 final Object SKIP_TARGET_TOKEN = new Object();
4453 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 private WindowState mLastWin = null;
4456 private IBinder mLastBinder = null;
4457 private boolean mFinished = true;
4458 private boolean mGotFirstWindow = false;
4459 private boolean mEventDispatching = true;
4460 private long mTimeToSwitch = 0;
4461 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 // Target of Motion events
4464 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 // Windows above the target who would like to receive an "outside"
4467 // touch event for any down events outside of them.
4468 WindowState mOutsideTouchTargets;
4469
4470 /**
4471 * Wait for the last event dispatch to complete, then find the next
4472 * target that should receive the given event and wait for that one
4473 * to be ready to receive it.
4474 */
4475 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4476 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004477 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 long startTime = SystemClock.uptimeMillis();
4479 long keyDispatchingTimeout = 5 * 1000;
4480 long waitedFor = 0;
4481
4482 while (true) {
4483 // Figure out which window we care about. It is either the
4484 // last window we are waiting to have process the event or,
4485 // if none, then the next window we think the event should go
4486 // to. Note: we retrieve mLastWin outside of the lock, so
4487 // it may change before we lock. Thus we must check it again.
4488 WindowState targetWin = mLastWin;
4489 boolean targetIsNew = targetWin == null;
4490 if (DEBUG_INPUT) Log.v(
4491 TAG, "waitForLastKey: mFinished=" + mFinished +
4492 ", mLastWin=" + mLastWin);
4493 if (targetIsNew) {
4494 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004495 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004496 if (target == SKIP_TARGET_TOKEN) {
4497 // The user has pressed a special key, and we are
4498 // dropping all pending events before it.
4499 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4500 + " " + nextMotion);
4501 return null;
4502 }
4503 if (target == CONSUMED_EVENT_TOKEN) {
4504 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4505 + " " + nextMotion);
4506 return target;
4507 }
4508 targetWin = (WindowState)target;
4509 }
Romain Guy06882f82009-06-10 13:36:04 -07004510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004511 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 // Now: is it okay to send the next event to this window?
4514 synchronized (this) {
4515 // First: did we come here based on the last window not
4516 // being null, but it changed by the time we got here?
4517 // If so, try again.
4518 if (!targetIsNew && mLastWin == null) {
4519 continue;
4520 }
Romain Guy06882f82009-06-10 13:36:04 -07004521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 // We never dispatch events if not finished with the
4523 // last one, or the display is frozen.
4524 if (mFinished && !mDisplayFrozen) {
4525 // If event dispatching is disabled, then we
4526 // just consume the events.
4527 if (!mEventDispatching) {
4528 if (DEBUG_INPUT) Log.v(TAG,
4529 "Skipping event; dispatching disabled: "
4530 + nextKey + " " + nextMotion);
4531 return null;
4532 }
4533 if (targetWin != null) {
4534 // If this is a new target, and that target is not
4535 // paused or unresponsive, then all looks good to
4536 // handle the event.
4537 if (targetIsNew && !targetWin.mToken.paused) {
4538 return targetWin;
4539 }
Romain Guy06882f82009-06-10 13:36:04 -07004540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 // If we didn't find a target window, and there is no
4542 // focused app window, then just eat the events.
4543 } else if (mFocusedApp == null) {
4544 if (DEBUG_INPUT) Log.v(TAG,
4545 "Skipping event; no focused app: "
4546 + nextKey + " " + nextMotion);
4547 return null;
4548 }
4549 }
Romain Guy06882f82009-06-10 13:36:04 -07004550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 if (DEBUG_INPUT) Log.v(
4552 TAG, "Waiting for last key in " + mLastBinder
4553 + " target=" + targetWin
4554 + " mFinished=" + mFinished
4555 + " mDisplayFrozen=" + mDisplayFrozen
4556 + " targetIsNew=" + targetIsNew
4557 + " paused="
4558 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004559 + " mFocusedApp=" + mFocusedApp
4560 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 targetApp = targetWin != null
4563 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 long curTimeout = keyDispatchingTimeout;
4566 if (mTimeToSwitch != 0) {
4567 long now = SystemClock.uptimeMillis();
4568 if (mTimeToSwitch <= now) {
4569 // If an app switch key has been pressed, and we have
4570 // waited too long for the current app to finish
4571 // processing keys, then wait no more!
4572 doFinishedKeyLocked(true);
4573 continue;
4574 }
4575 long switchTimeout = mTimeToSwitch - now;
4576 if (curTimeout > switchTimeout) {
4577 curTimeout = switchTimeout;
4578 }
4579 }
Romain Guy06882f82009-06-10 13:36:04 -07004580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 try {
4582 // after that continue
4583 // processing keys, so we don't get stuck.
4584 if (DEBUG_INPUT) Log.v(
4585 TAG, "Waiting for key dispatch: " + curTimeout);
4586 wait(curTimeout);
4587 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4588 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004589 + startTime + " switchTime=" + mTimeToSwitch
4590 + " target=" + targetWin + " mLW=" + mLastWin
4591 + " mLB=" + mLastBinder + " fin=" + mFinished
4592 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 } catch (InterruptedException e) {
4594 }
4595 }
4596
4597 // If we were frozen during configuration change, restart the
4598 // timeout checks from now; otherwise look at whether we timed
4599 // out before awakening.
4600 if (mWasFrozen) {
4601 waitedFor = 0;
4602 mWasFrozen = false;
4603 } else {
4604 waitedFor = SystemClock.uptimeMillis() - startTime;
4605 }
4606
4607 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4608 IApplicationToken at = null;
4609 synchronized (this) {
4610 Log.w(TAG, "Key dispatching timed out sending to " +
4611 (targetWin != null ? targetWin.mAttrs.getTitle()
4612 : "<null>"));
4613 // NOSHIP debugging
4614 Log.w(TAG, "Dispatch state: " + mDispatchState);
4615 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4616 // END NOSHIP
4617 //dump();
4618 if (targetWin != null) {
4619 at = targetWin.getAppToken();
4620 } else if (targetApp != null) {
4621 at = targetApp.appToken;
4622 }
4623 }
4624
4625 boolean abort = true;
4626 if (at != null) {
4627 try {
4628 long timeout = at.getKeyDispatchingTimeout();
4629 if (timeout > waitedFor) {
4630 // we did not wait the proper amount of time for this application.
4631 // set the timeout to be the real timeout and wait again.
4632 keyDispatchingTimeout = timeout - waitedFor;
4633 continue;
4634 } else {
4635 abort = at.keyDispatchingTimedOut();
4636 }
4637 } catch (RemoteException ex) {
4638 }
4639 }
4640
4641 synchronized (this) {
4642 if (abort && (mLastWin == targetWin || targetWin == null)) {
4643 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004644 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004645 if (DEBUG_INPUT) Log.v(TAG,
4646 "Window " + mLastWin +
4647 " timed out on key input");
4648 if (mLastWin.mToken.paused) {
4649 Log.w(TAG, "Un-pausing dispatching to this window");
4650 mLastWin.mToken.paused = false;
4651 }
4652 }
4653 if (mMotionTarget == targetWin) {
4654 mMotionTarget = null;
4655 }
4656 mLastWin = null;
4657 mLastBinder = null;
4658 if (failIfTimeout || targetWin == null) {
4659 return null;
4660 }
4661 } else {
4662 Log.w(TAG, "Continuing to wait for key to be dispatched");
4663 startTime = SystemClock.uptimeMillis();
4664 }
4665 }
4666 }
4667 }
4668 }
Romain Guy06882f82009-06-10 13:36:04 -07004669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004671 MotionEvent nextMotion, boolean isPointerEvent,
4672 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004673 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 if (nextKey != null) {
4676 // Find the target window for a normal key event.
4677 final int keycode = nextKey.getKeyCode();
4678 final int repeatCount = nextKey.getRepeatCount();
4679 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4680 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004683 if (callingUid == 0 ||
4684 mContext.checkPermission(
4685 android.Manifest.permission.INJECT_EVENTS,
4686 callingPid, callingUid)
4687 == PackageManager.PERMISSION_GRANTED) {
4688 mPolicy.interceptKeyTi(null, keycode,
4689 nextKey.getMetaState(), down, repeatCount);
4690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004691 Log.w(TAG, "Event timeout during app switch: dropping "
4692 + nextKey);
4693 return SKIP_TARGET_TOKEN;
4694 }
Romain Guy06882f82009-06-10 13:36:04 -07004695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 WindowState focus = null;
4699 synchronized(mWindowMap) {
4700 focus = getFocusedWindowLocked();
4701 }
Romain Guy06882f82009-06-10 13:36:04 -07004702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004704
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004705 if (callingUid == 0 ||
4706 (focus != null && callingUid == focus.mSession.mUid) ||
4707 mContext.checkPermission(
4708 android.Manifest.permission.INJECT_EVENTS,
4709 callingPid, callingUid)
4710 == PackageManager.PERMISSION_GRANTED) {
4711 if (mPolicy.interceptKeyTi(focus,
4712 keycode, nextKey.getMetaState(), down, repeatCount)) {
4713 return CONSUMED_EVENT_TOKEN;
4714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 }
Romain Guy06882f82009-06-10 13:36:04 -07004716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004719 } else if (!isPointerEvent) {
4720 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4721 if (!dispatch) {
4722 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4723 + nextMotion);
4724 return SKIP_TARGET_TOKEN;
4725 }
Romain Guy06882f82009-06-10 13:36:04 -07004726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004727 WindowState focus = null;
4728 synchronized(mWindowMap) {
4729 focus = getFocusedWindowLocked();
4730 }
Romain Guy06882f82009-06-10 13:36:04 -07004731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004732 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4733 return focus;
4734 }
Romain Guy06882f82009-06-10 13:36:04 -07004735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004736 if (nextMotion == null) {
4737 return SKIP_TARGET_TOKEN;
4738 }
Romain Guy06882f82009-06-10 13:36:04 -07004739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004740 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4741 KeyEvent.KEYCODE_UNKNOWN);
4742 if (!dispatch) {
4743 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4744 + nextMotion);
4745 return SKIP_TARGET_TOKEN;
4746 }
Romain Guy06882f82009-06-10 13:36:04 -07004747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004748 // Find the target window for a pointer event.
4749 int action = nextMotion.getAction();
4750 final float xf = nextMotion.getX();
4751 final float yf = nextMotion.getY();
4752 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004754 final boolean screenWasOff = qev != null
4755 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07004756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07004758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759 synchronized(mWindowMap) {
4760 synchronized (this) {
4761 if (action == MotionEvent.ACTION_DOWN) {
4762 if (mMotionTarget != null) {
4763 // this is weird, we got a pen down, but we thought it was
4764 // already down!
4765 // XXX: We should probably send an ACTION_UP to the current
4766 // target.
4767 Log.w(TAG, "Pointer down received while already down in: "
4768 + mMotionTarget);
4769 mMotionTarget = null;
4770 }
Romain Guy06882f82009-06-10 13:36:04 -07004771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004772 // ACTION_DOWN is special, because we need to lock next events to
4773 // the window we'll land onto.
4774 final int x = (int)xf;
4775 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07004776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004777 final ArrayList windows = mWindows;
4778 final int N = windows.size();
4779 WindowState topErrWindow = null;
4780 final Rect tmpRect = mTempRect;
4781 for (int i=N-1; i>=0; i--) {
4782 WindowState child = (WindowState)windows.get(i);
4783 //Log.i(TAG, "Checking dispatch to: " + child);
4784 final int flags = child.mAttrs.flags;
4785 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4786 if (topErrWindow == null) {
4787 topErrWindow = child;
4788 }
4789 }
4790 if (!child.isVisibleLw()) {
4791 //Log.i(TAG, "Not visible!");
4792 continue;
4793 }
4794 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4795 //Log.i(TAG, "Not touchable!");
4796 if ((flags & WindowManager.LayoutParams
4797 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4798 child.mNextOutsideTouch = mOutsideTouchTargets;
4799 mOutsideTouchTargets = child;
4800 }
4801 continue;
4802 }
4803 tmpRect.set(child.mFrame);
4804 if (child.mTouchableInsets == ViewTreeObserver
4805 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4806 // The touch is inside of the window if it is
4807 // inside the frame, AND the content part of that
4808 // frame that was given by the application.
4809 tmpRect.left += child.mGivenContentInsets.left;
4810 tmpRect.top += child.mGivenContentInsets.top;
4811 tmpRect.right -= child.mGivenContentInsets.right;
4812 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4813 } else if (child.mTouchableInsets == ViewTreeObserver
4814 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4815 // The touch is inside of the window if it is
4816 // inside the frame, AND the visible part of that
4817 // frame that was given by the application.
4818 tmpRect.left += child.mGivenVisibleInsets.left;
4819 tmpRect.top += child.mGivenVisibleInsets.top;
4820 tmpRect.right -= child.mGivenVisibleInsets.right;
4821 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
4822 }
4823 final int touchFlags = flags &
4824 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
4825 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
4826 if (tmpRect.contains(x, y) || touchFlags == 0) {
4827 //Log.i(TAG, "Using this target!");
4828 if (!screenWasOff || (flags &
4829 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
4830 mMotionTarget = child;
4831 } else {
4832 //Log.i(TAG, "Waking, skip!");
4833 mMotionTarget = null;
4834 }
4835 break;
4836 }
Romain Guy06882f82009-06-10 13:36:04 -07004837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 if ((flags & WindowManager.LayoutParams
4839 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4840 child.mNextOutsideTouch = mOutsideTouchTargets;
4841 mOutsideTouchTargets = child;
4842 //Log.i(TAG, "Adding to outside target list: " + child);
4843 }
4844 }
4845
4846 // if there's an error window but it's not accepting
4847 // focus (typically because it is not yet visible) just
4848 // wait for it -- any other focused window may in fact
4849 // be in ANR state.
4850 if (topErrWindow != null && mMotionTarget != topErrWindow) {
4851 mMotionTarget = null;
4852 }
4853 }
Romain Guy06882f82009-06-10 13:36:04 -07004854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004855 target = mMotionTarget;
4856 }
4857 }
Romain Guy06882f82009-06-10 13:36:04 -07004858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004859 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07004860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004861 // Pointer events are a little different -- if there isn't a
4862 // target found for any event, then just drop it.
4863 return target != null ? target : SKIP_TARGET_TOKEN;
4864 }
Romain Guy06882f82009-06-10 13:36:04 -07004865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 boolean checkShouldDispatchKey(int keycode) {
4867 synchronized (this) {
4868 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
4869 mTimeToSwitch = 0;
4870 return true;
4871 }
4872 if (mTimeToSwitch != 0
4873 && mTimeToSwitch < SystemClock.uptimeMillis()) {
4874 return false;
4875 }
4876 return true;
4877 }
4878 }
Romain Guy06882f82009-06-10 13:36:04 -07004879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 void bindTargetWindowLocked(WindowState win,
4881 int pendingWhat, QueuedEvent pendingMotion) {
4882 synchronized (this) {
4883 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
4884 }
4885 }
Romain Guy06882f82009-06-10 13:36:04 -07004886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 void bindTargetWindowLocked(WindowState win) {
4888 synchronized (this) {
4889 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
4890 }
4891 }
4892
4893 void bindTargetWindowLockedLocked(WindowState win,
4894 int pendingWhat, QueuedEvent pendingMotion) {
4895 mLastWin = win;
4896 mLastBinder = win.mClient.asBinder();
4897 mFinished = false;
4898 if (pendingMotion != null) {
4899 final Session s = win.mSession;
4900 if (pendingWhat == RETURN_PENDING_POINTER) {
4901 releasePendingPointerLocked(s);
4902 s.mPendingPointerMove = pendingMotion;
4903 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07004904 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905 "bindTargetToWindow " + s.mPendingPointerMove);
4906 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
4907 releasePendingTrackballLocked(s);
4908 s.mPendingTrackballMove = pendingMotion;
4909 s.mPendingTrackballWindow = win;
4910 }
4911 }
4912 }
Romain Guy06882f82009-06-10 13:36:04 -07004913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 void releasePendingPointerLocked(Session s) {
4915 if (DEBUG_INPUT) Log.v(TAG,
4916 "releasePendingPointer " + s.mPendingPointerMove);
4917 if (s.mPendingPointerMove != null) {
4918 mQueue.recycleEvent(s.mPendingPointerMove);
4919 s.mPendingPointerMove = null;
4920 }
4921 }
Romain Guy06882f82009-06-10 13:36:04 -07004922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004923 void releasePendingTrackballLocked(Session s) {
4924 if (s.mPendingTrackballMove != null) {
4925 mQueue.recycleEvent(s.mPendingTrackballMove);
4926 s.mPendingTrackballMove = null;
4927 }
4928 }
Romain Guy06882f82009-06-10 13:36:04 -07004929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 MotionEvent finishedKey(Session session, IWindow client, boolean force,
4931 int returnWhat) {
4932 if (DEBUG_INPUT) Log.v(
4933 TAG, "finishedKey: client=" + client + ", force=" + force);
4934
4935 if (client == null) {
4936 return null;
4937 }
4938
4939 synchronized (this) {
4940 if (DEBUG_INPUT) Log.v(
4941 TAG, "finishedKey: client=" + client.asBinder()
4942 + ", force=" + force + ", last=" + mLastBinder
4943 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
4944
4945 QueuedEvent qev = null;
4946 WindowState win = null;
4947 if (returnWhat == RETURN_PENDING_POINTER) {
4948 qev = session.mPendingPointerMove;
4949 win = session.mPendingPointerWindow;
4950 session.mPendingPointerMove = null;
4951 session.mPendingPointerWindow = null;
4952 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
4953 qev = session.mPendingTrackballMove;
4954 win = session.mPendingTrackballWindow;
4955 session.mPendingTrackballMove = null;
4956 session.mPendingTrackballWindow = null;
4957 }
Romain Guy06882f82009-06-10 13:36:04 -07004958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 if (mLastBinder == client.asBinder()) {
4960 if (DEBUG_INPUT) Log.v(
4961 TAG, "finishedKey: last paused="
4962 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
4963 if (mLastWin != null && (!mLastWin.mToken.paused || force
4964 || !mEventDispatching)) {
4965 doFinishedKeyLocked(false);
4966 } else {
4967 // Make sure to wake up anyone currently waiting to
4968 // dispatch a key, so they can re-evaluate their
4969 // current situation.
4970 mFinished = true;
4971 notifyAll();
4972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973 }
Romain Guy06882f82009-06-10 13:36:04 -07004974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004975 if (qev != null) {
4976 MotionEvent res = (MotionEvent)qev.event;
4977 if (DEBUG_INPUT) Log.v(TAG,
4978 "Returning pending motion: " + res);
4979 mQueue.recycleEvent(qev);
4980 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
4981 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
4982 }
4983 return res;
4984 }
4985 return null;
4986 }
4987 }
4988
4989 void tickle() {
4990 synchronized (this) {
4991 notifyAll();
4992 }
4993 }
Romain Guy06882f82009-06-10 13:36:04 -07004994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004995 void handleNewWindowLocked(WindowState newWindow) {
4996 if (!newWindow.canReceiveKeys()) {
4997 return;
4998 }
4999 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005000 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 TAG, "New key dispatch window: win="
5002 + newWindow.mClient.asBinder()
5003 + ", last=" + mLastBinder
5004 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5005 + "), finished=" + mFinished + ", paused="
5006 + newWindow.mToken.paused);
5007
5008 // Displaying a window implicitly causes dispatching to
5009 // be unpaused. (This is to protect against bugs if someone
5010 // pauses dispatching but forgets to resume.)
5011 newWindow.mToken.paused = false;
5012
5013 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014
5015 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5016 if (DEBUG_INPUT) Log.v(TAG,
5017 "New SYSTEM_ERROR window; resetting state");
5018 mLastWin = null;
5019 mLastBinder = null;
5020 mMotionTarget = null;
5021 mFinished = true;
5022 } else if (mLastWin != null) {
5023 // If the new window is above the window we are
5024 // waiting on, then stop waiting and let key dispatching
5025 // start on the new guy.
5026 if (DEBUG_INPUT) Log.v(
5027 TAG, "Last win layer=" + mLastWin.mLayer
5028 + ", new win layer=" + newWindow.mLayer);
5029 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005030 // The new window is above the old; finish pending input to the last
5031 // window and start directing it to the new one.
5032 mLastWin.mToken.paused = false;
5033 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005035 // Either the new window is lower, so there is no need to wake key waiters,
5036 // or we just finished key input to the previous window, which implicitly
5037 // notified the key waiters. In both cases, we don't need to issue the
5038 // notification here.
5039 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 }
5041
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005042 // Now that we've put a new window state in place, make the event waiter
5043 // take notice and retarget its attentions.
5044 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 }
5046 }
5047
5048 void pauseDispatchingLocked(WindowToken token) {
5049 synchronized (this)
5050 {
5051 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5052 token.paused = true;
5053
5054 /*
5055 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5056 mPaused = true;
5057 } else {
5058 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005059 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005061 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005063 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 }
5065 }
5066 */
5067 }
5068 }
5069
5070 void resumeDispatchingLocked(WindowToken token) {
5071 synchronized (this) {
5072 if (token.paused) {
5073 if (DEBUG_INPUT) Log.v(
5074 TAG, "Resuming WindowToken " + token
5075 + ", last=" + mLastBinder
5076 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5077 + "), finished=" + mFinished + ", paused="
5078 + token.paused);
5079 token.paused = false;
5080 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5081 doFinishedKeyLocked(true);
5082 } else {
5083 notifyAll();
5084 }
5085 }
5086 }
5087 }
5088
5089 void setEventDispatchingLocked(boolean enabled) {
5090 synchronized (this) {
5091 mEventDispatching = enabled;
5092 notifyAll();
5093 }
5094 }
Romain Guy06882f82009-06-10 13:36:04 -07005095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 void appSwitchComing() {
5097 synchronized (this) {
5098 // Don't wait for more than .5 seconds for app to finish
5099 // processing the pending events.
5100 long now = SystemClock.uptimeMillis() + 500;
5101 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5102 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5103 mTimeToSwitch = now;
5104 }
5105 notifyAll();
5106 }
5107 }
Romain Guy06882f82009-06-10 13:36:04 -07005108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 private final void doFinishedKeyLocked(boolean doRecycle) {
5110 if (mLastWin != null) {
5111 releasePendingPointerLocked(mLastWin.mSession);
5112 releasePendingTrackballLocked(mLastWin.mSession);
5113 }
Romain Guy06882f82009-06-10 13:36:04 -07005114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 if (mLastWin == null || !mLastWin.mToken.paused
5116 || !mLastWin.isVisibleLw()) {
5117 // If the current window has been paused, we aren't -really-
5118 // finished... so let the waiters still wait.
5119 mLastWin = null;
5120 mLastBinder = null;
5121 }
5122 mFinished = true;
5123 notifyAll();
5124 }
5125 }
5126
5127 private class KeyQ extends KeyInputQueue
5128 implements KeyInputQueue.FilterCallback {
5129 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131 KeyQ() {
5132 super(mContext);
5133 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5134 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5135 "KEEP_SCREEN_ON_FLAG");
5136 mHoldingScreen.setReferenceCounted(false);
5137 }
5138
5139 @Override
5140 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5141 if (mPolicy.preprocessInputEventTq(event)) {
5142 return true;
5143 }
Romain Guy06882f82009-06-10 13:36:04 -07005144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 switch (event.type) {
5146 case RawInputEvent.EV_KEY: {
5147 // XXX begin hack
5148 if (DEBUG) {
5149 if (event.keycode == KeyEvent.KEYCODE_G) {
5150 if (event.value != 0) {
5151 // G down
5152 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5153 }
5154 return false;
5155 }
5156 if (event.keycode == KeyEvent.KEYCODE_D) {
5157 if (event.value != 0) {
5158 //dump();
5159 }
5160 return false;
5161 }
5162 }
5163 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005165 boolean screenIsOff = !mPowerManager.screenIsOn();
5166 boolean screenIsDim = !mPowerManager.screenIsBright();
5167 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5170 mPowerManager.goToSleep(event.when);
5171 }
5172
5173 if (screenIsOff) {
5174 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5175 }
5176 if (screenIsDim) {
5177 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5178 }
5179 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5180 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005181 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005182 }
Romain Guy06882f82009-06-10 13:36:04 -07005183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5185 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5186 filterQueue(this);
5187 mKeyWaiter.appSwitchComing();
5188 }
5189 return true;
5190 } else {
5191 return false;
5192 }
5193 }
Romain Guy06882f82009-06-10 13:36:04 -07005194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 case RawInputEvent.EV_REL: {
5196 boolean screenIsOff = !mPowerManager.screenIsOn();
5197 boolean screenIsDim = !mPowerManager.screenIsBright();
5198 if (screenIsOff) {
5199 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5200 device.classes, event)) {
5201 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5202 return false;
5203 }
5204 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5205 }
5206 if (screenIsDim) {
5207 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5208 }
5209 return true;
5210 }
Romain Guy06882f82009-06-10 13:36:04 -07005211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 case RawInputEvent.EV_ABS: {
5213 boolean screenIsOff = !mPowerManager.screenIsOn();
5214 boolean screenIsDim = !mPowerManager.screenIsBright();
5215 if (screenIsOff) {
5216 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5217 device.classes, event)) {
5218 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5219 return false;
5220 }
5221 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5222 }
5223 if (screenIsDim) {
5224 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5225 }
5226 return true;
5227 }
Romain Guy06882f82009-06-10 13:36:04 -07005228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 default:
5230 return true;
5231 }
5232 }
5233
5234 public int filterEvent(QueuedEvent ev) {
5235 switch (ev.classType) {
5236 case RawInputEvent.CLASS_KEYBOARD:
5237 KeyEvent ke = (KeyEvent)ev.event;
5238 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5239 Log.w(TAG, "Dropping movement key during app switch: "
5240 + ke.getKeyCode() + ", action=" + ke.getAction());
5241 return FILTER_REMOVE;
5242 }
5243 return FILTER_ABORT;
5244 default:
5245 return FILTER_KEEP;
5246 }
5247 }
Romain Guy06882f82009-06-10 13:36:04 -07005248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005249 /**
5250 * Must be called with the main window manager lock held.
5251 */
5252 void setHoldScreenLocked(boolean holding) {
5253 boolean state = mHoldingScreen.isHeld();
5254 if (holding != state) {
5255 if (holding) {
5256 mHoldingScreen.acquire();
5257 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005258 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 mHoldingScreen.release();
5260 }
5261 }
5262 }
Michael Chan53071d62009-05-13 17:29:48 -07005263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264
5265 public boolean detectSafeMode() {
5266 mSafeMode = mPolicy.detectSafeMode();
5267 return mSafeMode;
5268 }
Romain Guy06882f82009-06-10 13:36:04 -07005269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005270 public void systemReady() {
5271 mPolicy.systemReady();
5272 }
Romain Guy06882f82009-06-10 13:36:04 -07005273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 private final class InputDispatcherThread extends Thread {
5275 // Time to wait when there is nothing to do: 9999 seconds.
5276 static final int LONG_WAIT=9999*1000;
5277
5278 public InputDispatcherThread() {
5279 super("InputDispatcher");
5280 }
Romain Guy06882f82009-06-10 13:36:04 -07005281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005282 @Override
5283 public void run() {
5284 while (true) {
5285 try {
5286 process();
5287 } catch (Exception e) {
5288 Log.e(TAG, "Exception in input dispatcher", e);
5289 }
5290 }
5291 }
Romain Guy06882f82009-06-10 13:36:04 -07005292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005293 private void process() {
5294 android.os.Process.setThreadPriority(
5295 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 // The last key event we saw
5298 KeyEvent lastKey = null;
5299
5300 // Last keydown time for auto-repeating keys
5301 long lastKeyTime = SystemClock.uptimeMillis();
5302 long nextKeyTime = lastKeyTime+LONG_WAIT;
5303
Romain Guy06882f82009-06-10 13:36:04 -07005304 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005305 int keyRepeatCount = 0;
5306
5307 // Need to report that configuration has changed?
5308 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 while (true) {
5311 long curTime = SystemClock.uptimeMillis();
5312
5313 if (DEBUG_INPUT) Log.v(
5314 TAG, "Waiting for next key: now=" + curTime
5315 + ", repeat @ " + nextKeyTime);
5316
5317 // Retrieve next event, waiting only as long as the next
5318 // repeat timeout. If the configuration has changed, then
5319 // don't wait at all -- we'll report the change as soon as
5320 // we have processed all events.
5321 QueuedEvent ev = mQueue.getEvent(
5322 (int)((!configChanged && curTime < nextKeyTime)
5323 ? (nextKeyTime-curTime) : 0));
5324
5325 if (DEBUG_INPUT && ev != null) Log.v(
5326 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5327
Michael Chan53071d62009-05-13 17:29:48 -07005328 if (MEASURE_LATENCY) {
5329 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5330 }
5331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 try {
5333 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005334 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005335 int eventType;
5336 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5337 eventType = eventType((MotionEvent)ev.event);
5338 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5339 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5340 eventType = LocalPowerManager.BUTTON_EVENT;
5341 } else {
5342 eventType = LocalPowerManager.OTHER_EVENT;
5343 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005344 try {
Michael Chan53071d62009-05-13 17:29:48 -07005345 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005346 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005347 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005348 mBatteryStats.noteInputEvent();
5349 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005350 } catch (RemoteException e) {
5351 // Ignore
5352 }
Michael Chane10de972009-05-18 11:24:50 -07005353
5354 if (eventType != TOUCH_EVENT
5355 && eventType != LONG_TOUCH_EVENT
5356 && eventType != CHEEK_EVENT) {
5357 mPowerManager.userActivity(curTime, false,
5358 eventType, false);
5359 } else if (mLastTouchEventType != eventType
5360 || (curTime - mLastUserActivityCallTime)
5361 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5362 mLastUserActivityCallTime = curTime;
5363 mLastTouchEventType = eventType;
5364 mPowerManager.userActivity(curTime, false,
5365 eventType, false);
5366 }
5367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 switch (ev.classType) {
5369 case RawInputEvent.CLASS_KEYBOARD:
5370 KeyEvent ke = (KeyEvent)ev.event;
5371 if (ke.isDown()) {
5372 lastKey = ke;
5373 keyRepeatCount = 0;
5374 lastKeyTime = curTime;
5375 nextKeyTime = lastKeyTime
5376 + KEY_REPEAT_FIRST_DELAY;
5377 if (DEBUG_INPUT) Log.v(
5378 TAG, "Received key down: first repeat @ "
5379 + nextKeyTime);
5380 } else {
5381 lastKey = null;
5382 // Arbitrary long timeout.
5383 lastKeyTime = curTime;
5384 nextKeyTime = curTime + LONG_WAIT;
5385 if (DEBUG_INPUT) Log.v(
5386 TAG, "Received key up: ignore repeat @ "
5387 + nextKeyTime);
5388 }
5389 dispatchKey((KeyEvent)ev.event, 0, 0);
5390 mQueue.recycleEvent(ev);
5391 break;
5392 case RawInputEvent.CLASS_TOUCHSCREEN:
5393 //Log.i(TAG, "Read next event " + ev);
5394 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5395 break;
5396 case RawInputEvent.CLASS_TRACKBALL:
5397 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5398 break;
5399 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5400 configChanged = true;
5401 break;
5402 default:
5403 mQueue.recycleEvent(ev);
5404 break;
5405 }
Romain Guy06882f82009-06-10 13:36:04 -07005406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 } else if (configChanged) {
5408 configChanged = false;
5409 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005411 } else if (lastKey != null) {
5412 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 // Timeout occurred while key was down. If it is at or
5415 // past the key repeat time, dispatch the repeat.
5416 if (DEBUG_INPUT) Log.v(
5417 TAG, "Key timeout: repeat=" + nextKeyTime
5418 + ", now=" + curTime);
5419 if (curTime < nextKeyTime) {
5420 continue;
5421 }
Romain Guy06882f82009-06-10 13:36:04 -07005422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005423 lastKeyTime = nextKeyTime;
5424 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5425 keyRepeatCount++;
5426 if (DEBUG_INPUT) Log.v(
5427 TAG, "Key repeat: count=" + keyRepeatCount
5428 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005429 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 } else {
5432 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 lastKeyTime = curTime;
5435 nextKeyTime = curTime + LONG_WAIT;
5436 }
Romain Guy06882f82009-06-10 13:36:04 -07005437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005438 } catch (Exception e) {
5439 Log.e(TAG,
5440 "Input thread received uncaught exception: " + e, e);
5441 }
5442 }
5443 }
5444 }
5445
5446 // -------------------------------------------------------------
5447 // Client Session State
5448 // -------------------------------------------------------------
5449
5450 private final class Session extends IWindowSession.Stub
5451 implements IBinder.DeathRecipient {
5452 final IInputMethodClient mClient;
5453 final IInputContext mInputContext;
5454 final int mUid;
5455 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005456 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005457 SurfaceSession mSurfaceSession;
5458 int mNumWindow = 0;
5459 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005461 /**
5462 * Current pointer move event being dispatched to client window... must
5463 * hold key lock to access.
5464 */
5465 QueuedEvent mPendingPointerMove;
5466 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 /**
5469 * Current trackball move event being dispatched to client window... must
5470 * hold key lock to access.
5471 */
5472 QueuedEvent mPendingTrackballMove;
5473 WindowState mPendingTrackballWindow;
5474
5475 public Session(IInputMethodClient client, IInputContext inputContext) {
5476 mClient = client;
5477 mInputContext = inputContext;
5478 mUid = Binder.getCallingUid();
5479 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005480 StringBuilder sb = new StringBuilder();
5481 sb.append("Session{");
5482 sb.append(Integer.toHexString(System.identityHashCode(this)));
5483 sb.append(" uid ");
5484 sb.append(mUid);
5485 sb.append("}");
5486 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 synchronized (mWindowMap) {
5489 if (mInputMethodManager == null && mHaveInputMethods) {
5490 IBinder b = ServiceManager.getService(
5491 Context.INPUT_METHOD_SERVICE);
5492 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5493 }
5494 }
5495 long ident = Binder.clearCallingIdentity();
5496 try {
5497 // Note: it is safe to call in to the input method manager
5498 // here because we are not holding our lock.
5499 if (mInputMethodManager != null) {
5500 mInputMethodManager.addClient(client, inputContext,
5501 mUid, mPid);
5502 } else {
5503 client.setUsingInputMethod(false);
5504 }
5505 client.asBinder().linkToDeath(this, 0);
5506 } catch (RemoteException e) {
5507 // The caller has died, so we can just forget about this.
5508 try {
5509 if (mInputMethodManager != null) {
5510 mInputMethodManager.removeClient(client);
5511 }
5512 } catch (RemoteException ee) {
5513 }
5514 } finally {
5515 Binder.restoreCallingIdentity(ident);
5516 }
5517 }
Romain Guy06882f82009-06-10 13:36:04 -07005518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005519 @Override
5520 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5521 throws RemoteException {
5522 try {
5523 return super.onTransact(code, data, reply, flags);
5524 } catch (RuntimeException e) {
5525 // Log all 'real' exceptions thrown to the caller
5526 if (!(e instanceof SecurityException)) {
5527 Log.e(TAG, "Window Session Crash", e);
5528 }
5529 throw e;
5530 }
5531 }
5532
5533 public void binderDied() {
5534 // Note: it is safe to call in to the input method manager
5535 // here because we are not holding our lock.
5536 try {
5537 if (mInputMethodManager != null) {
5538 mInputMethodManager.removeClient(mClient);
5539 }
5540 } catch (RemoteException e) {
5541 }
5542 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005543 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 mClientDead = true;
5545 killSessionLocked();
5546 }
5547 }
5548
5549 public int add(IWindow window, WindowManager.LayoutParams attrs,
5550 int viewVisibility, Rect outContentInsets) {
5551 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5552 }
Romain Guy06882f82009-06-10 13:36:04 -07005553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005554 public void remove(IWindow window) {
5555 removeWindow(this, window);
5556 }
Romain Guy06882f82009-06-10 13:36:04 -07005557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5559 int requestedWidth, int requestedHeight, int viewFlags,
5560 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5561 Rect outVisibleInsets, Surface outSurface) {
5562 return relayoutWindow(this, window, attrs,
5563 requestedWidth, requestedHeight, viewFlags, insetsPending,
5564 outFrame, outContentInsets, outVisibleInsets, outSurface);
5565 }
Romain Guy06882f82009-06-10 13:36:04 -07005566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 public void setTransparentRegion(IWindow window, Region region) {
5568 setTransparentRegionWindow(this, window, region);
5569 }
Romain Guy06882f82009-06-10 13:36:04 -07005570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 public void setInsets(IWindow window, int touchableInsets,
5572 Rect contentInsets, Rect visibleInsets) {
5573 setInsetsWindow(this, window, touchableInsets, contentInsets,
5574 visibleInsets);
5575 }
Romain Guy06882f82009-06-10 13:36:04 -07005576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5578 getWindowDisplayFrame(this, window, outDisplayFrame);
5579 }
Romain Guy06882f82009-06-10 13:36:04 -07005580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 public void finishDrawing(IWindow window) {
5582 if (localLOGV) Log.v(
5583 TAG, "IWindow finishDrawing called for " + window);
5584 finishDrawingWindow(this, window);
5585 }
5586
5587 public void finishKey(IWindow window) {
5588 if (localLOGV) Log.v(
5589 TAG, "IWindow finishKey called for " + window);
5590 mKeyWaiter.finishedKey(this, window, false,
5591 KeyWaiter.RETURN_NOTHING);
5592 }
5593
5594 public MotionEvent getPendingPointerMove(IWindow window) {
5595 if (localLOGV) Log.v(
5596 TAG, "IWindow getPendingMotionEvent called for " + window);
5597 return mKeyWaiter.finishedKey(this, window, false,
5598 KeyWaiter.RETURN_PENDING_POINTER);
5599 }
Romain Guy06882f82009-06-10 13:36:04 -07005600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005601 public MotionEvent getPendingTrackballMove(IWindow window) {
5602 if (localLOGV) Log.v(
5603 TAG, "IWindow getPendingMotionEvent called for " + window);
5604 return mKeyWaiter.finishedKey(this, window, false,
5605 KeyWaiter.RETURN_PENDING_TRACKBALL);
5606 }
5607
5608 public void setInTouchMode(boolean mode) {
5609 synchronized(mWindowMap) {
5610 mInTouchMode = mode;
5611 }
5612 }
5613
5614 public boolean getInTouchMode() {
5615 synchronized(mWindowMap) {
5616 return mInTouchMode;
5617 }
5618 }
5619
5620 public boolean performHapticFeedback(IWindow window, int effectId,
5621 boolean always) {
5622 synchronized(mWindowMap) {
5623 long ident = Binder.clearCallingIdentity();
5624 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005625 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 windowForClientLocked(this, window), effectId, always);
5627 } finally {
5628 Binder.restoreCallingIdentity(ident);
5629 }
5630 }
5631 }
Romain Guy06882f82009-06-10 13:36:04 -07005632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 void windowAddedLocked() {
5634 if (mSurfaceSession == null) {
5635 if (localLOGV) Log.v(
5636 TAG, "First window added to " + this + ", creating SurfaceSession");
5637 mSurfaceSession = new SurfaceSession();
5638 mSessions.add(this);
5639 }
5640 mNumWindow++;
5641 }
5642
5643 void windowRemovedLocked() {
5644 mNumWindow--;
5645 killSessionLocked();
5646 }
Romain Guy06882f82009-06-10 13:36:04 -07005647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 void killSessionLocked() {
5649 if (mNumWindow <= 0 && mClientDead) {
5650 mSessions.remove(this);
5651 if (mSurfaceSession != null) {
5652 if (localLOGV) Log.v(
5653 TAG, "Last window removed from " + this
5654 + ", destroying " + mSurfaceSession);
5655 try {
5656 mSurfaceSession.kill();
5657 } catch (Exception e) {
5658 Log.w(TAG, "Exception thrown when killing surface session "
5659 + mSurfaceSession + " in session " + this
5660 + ": " + e.toString());
5661 }
5662 mSurfaceSession = null;
5663 }
5664 }
5665 }
Romain Guy06882f82009-06-10 13:36:04 -07005666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005668 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5669 pw.print(" mClientDead="); pw.print(mClientDead);
5670 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5671 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5672 pw.print(prefix);
5673 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5674 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5675 }
5676 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5677 pw.print(prefix);
5678 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5679 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 }
5682
5683 @Override
5684 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005685 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686 }
5687 }
5688
5689 // -------------------------------------------------------------
5690 // Client Window State
5691 // -------------------------------------------------------------
5692
5693 private final class WindowState implements WindowManagerPolicy.WindowState {
5694 final Session mSession;
5695 final IWindow mClient;
5696 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005697 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005698 AppWindowToken mAppToken;
5699 AppWindowToken mTargetAppToken;
5700 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5701 final DeathRecipient mDeathRecipient;
5702 final WindowState mAttachedWindow;
5703 final ArrayList mChildWindows = new ArrayList();
5704 final int mBaseLayer;
5705 final int mSubLayer;
5706 final boolean mLayoutAttached;
5707 final boolean mIsImWindow;
5708 int mViewVisibility;
5709 boolean mPolicyVisibility = true;
5710 boolean mPolicyVisibilityAfterAnim = true;
5711 boolean mAppFreezing;
5712 Surface mSurface;
5713 boolean mAttachedHidden; // is our parent window hidden?
5714 boolean mLastHidden; // was this window last hidden?
5715 int mRequestedWidth;
5716 int mRequestedHeight;
5717 int mLastRequestedWidth;
5718 int mLastRequestedHeight;
5719 int mReqXPos;
5720 int mReqYPos;
5721 int mLayer;
5722 int mAnimLayer;
5723 int mLastLayer;
5724 boolean mHaveFrame;
5725
5726 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728 // Actual frame shown on-screen (may be modified by animation)
5729 final Rect mShownFrame = new Rect();
5730 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 /**
5733 * Insets that determine the actually visible area
5734 */
5735 final Rect mVisibleInsets = new Rect();
5736 final Rect mLastVisibleInsets = new Rect();
5737 boolean mVisibleInsetsChanged;
5738
5739 /**
5740 * Insets that are covered by system windows
5741 */
5742 final Rect mContentInsets = new Rect();
5743 final Rect mLastContentInsets = new Rect();
5744 boolean mContentInsetsChanged;
5745
5746 /**
5747 * Set to true if we are waiting for this window to receive its
5748 * given internal insets before laying out other windows based on it.
5749 */
5750 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07005751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005752 /**
5753 * These are the content insets that were given during layout for
5754 * this window, to be applied to windows behind it.
5755 */
5756 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 /**
5759 * These are the visible insets that were given during layout for
5760 * this window, to be applied to windows behind it.
5761 */
5762 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 /**
5765 * Flag indicating whether the touchable region should be adjusted by
5766 * the visible insets; if false the area outside the visible insets is
5767 * NOT touchable, so we must use those to adjust the frame during hit
5768 * tests.
5769 */
5770 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07005771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005772 // Current transformation being applied.
5773 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5774 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5775 float mHScale=1, mVScale=1;
5776 float mLastHScale=1, mLastVScale=1;
5777 final Matrix mTmpMatrix = new Matrix();
5778
5779 // "Real" frame that the application sees.
5780 final Rect mFrame = new Rect();
5781 final Rect mLastFrame = new Rect();
5782
5783 final Rect mContainingFrame = new Rect();
5784 final Rect mDisplayFrame = new Rect();
5785 final Rect mContentFrame = new Rect();
5786 final Rect mVisibleFrame = new Rect();
5787
5788 float mShownAlpha = 1;
5789 float mAlpha = 1;
5790 float mLastAlpha = 1;
5791
5792 // Set to true if, when the window gets displayed, it should perform
5793 // an enter animation.
5794 boolean mEnterAnimationPending;
5795
5796 // Currently running animation.
5797 boolean mAnimating;
5798 boolean mLocalAnimating;
5799 Animation mAnimation;
5800 boolean mAnimationIsEntrance;
5801 boolean mHasTransformation;
5802 boolean mHasLocalTransformation;
5803 final Transformation mTransformation = new Transformation();
5804
5805 // This is set after IWindowSession.relayout() has been called at
5806 // least once for the window. It allows us to detect the situation
5807 // where we don't yet have a surface, but should have one soon, so
5808 // we can give the window focus before waiting for the relayout.
5809 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07005810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 // This is set after the Surface has been created but before the
5812 // window has been drawn. During this time the surface is hidden.
5813 boolean mDrawPending;
5814
5815 // This is set after the window has finished drawing for the first
5816 // time but before its surface is shown. The surface will be
5817 // displayed when the next layout is run.
5818 boolean mCommitDrawPending;
5819
5820 // This is set during the time after the window's drawing has been
5821 // committed, and before its surface is actually shown. It is used
5822 // to delay showing the surface until all windows in a token are ready
5823 // to be shown.
5824 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07005825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005826 // Set when the window has been shown in the screen the first time.
5827 boolean mHasDrawn;
5828
5829 // Currently running an exit animation?
5830 boolean mExiting;
5831
5832 // Currently on the mDestroySurface list?
5833 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07005834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835 // Completely remove from window manager after exit animation?
5836 boolean mRemoveOnExit;
5837
5838 // Set when the orientation is changing and this window has not yet
5839 // been updated for the new orientation.
5840 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07005841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 // Is this window now (or just being) removed?
5843 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07005844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005845 WindowState(Session s, IWindow c, WindowToken token,
5846 WindowState attachedWindow, WindowManager.LayoutParams a,
5847 int viewVisibility) {
5848 mSession = s;
5849 mClient = c;
5850 mToken = token;
5851 mAttrs.copyFrom(a);
5852 mViewVisibility = viewVisibility;
5853 DeathRecipient deathRecipient = new DeathRecipient();
5854 mAlpha = a.alpha;
5855 if (localLOGV) Log.v(
5856 TAG, "Window " + this + " client=" + c.asBinder()
5857 + " token=" + token + " (" + mAttrs.token + ")");
5858 try {
5859 c.asBinder().linkToDeath(deathRecipient, 0);
5860 } catch (RemoteException e) {
5861 mDeathRecipient = null;
5862 mAttachedWindow = null;
5863 mLayoutAttached = false;
5864 mIsImWindow = false;
5865 mBaseLayer = 0;
5866 mSubLayer = 0;
5867 return;
5868 }
5869 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07005870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
5872 mAttrs.type <= LAST_SUB_WINDOW)) {
5873 // The multiplier here is to reserve space for multiple
5874 // windows in the same type layer.
5875 mBaseLayer = mPolicy.windowTypeToLayerLw(
5876 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
5877 + TYPE_LAYER_OFFSET;
5878 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
5879 mAttachedWindow = attachedWindow;
5880 mAttachedWindow.mChildWindows.add(this);
5881 mLayoutAttached = mAttrs.type !=
5882 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
5883 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
5884 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5885 } else {
5886 // The multiplier here is to reserve space for multiple
5887 // windows in the same type layer.
5888 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
5889 * TYPE_LAYER_MULTIPLIER
5890 + TYPE_LAYER_OFFSET;
5891 mSubLayer = 0;
5892 mAttachedWindow = null;
5893 mLayoutAttached = false;
5894 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
5895 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5896 }
5897
5898 WindowState appWin = this;
5899 while (appWin.mAttachedWindow != null) {
5900 appWin = mAttachedWindow;
5901 }
5902 WindowToken appToken = appWin.mToken;
5903 while (appToken.appWindowToken == null) {
5904 WindowToken parent = mTokenMap.get(appToken.token);
5905 if (parent == null || appToken == parent) {
5906 break;
5907 }
5908 appToken = parent;
5909 }
The Android Open Source Project10592532009-03-18 17:39:46 -07005910 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 mAppToken = appToken.appWindowToken;
5912
5913 mSurface = null;
5914 mRequestedWidth = 0;
5915 mRequestedHeight = 0;
5916 mLastRequestedWidth = 0;
5917 mLastRequestedHeight = 0;
5918 mReqXPos = 0;
5919 mReqYPos = 0;
5920 mLayer = 0;
5921 mAnimLayer = 0;
5922 mLastLayer = 0;
5923 }
5924
5925 void attach() {
5926 if (localLOGV) Log.v(
5927 TAG, "Attaching " + this + " token=" + mToken
5928 + ", list=" + mToken.windows);
5929 mSession.windowAddedLocked();
5930 }
5931
5932 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
5933 mHaveFrame = true;
5934
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005935 final Rect container = mContainingFrame;
5936 container.set(pf);
5937
5938 final Rect display = mDisplayFrame;
5939 display.set(df);
5940
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07005941 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005942 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07005943 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
5944 display.intersect(mCompatibleScreenFrame);
5945 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005946 }
5947
5948 final int pw = container.right - container.left;
5949 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950
5951 int w,h;
5952 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
5953 w = mAttrs.width < 0 ? pw : mAttrs.width;
5954 h = mAttrs.height< 0 ? ph : mAttrs.height;
5955 } else {
5956 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
5957 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
5958 }
Romain Guy06882f82009-06-10 13:36:04 -07005959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005960 final Rect content = mContentFrame;
5961 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07005962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005963 final Rect visible = mVisibleFrame;
5964 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07005965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07005967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005968 //System.out.println("In: w=" + w + " h=" + h + " container=" +
5969 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
5970
5971 Gravity.apply(mAttrs.gravity, w, h, container,
5972 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
5973 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
5974
5975 //System.out.println("Out: " + mFrame);
5976
5977 // Now make sure the window fits in the overall display.
5978 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 // Make sure the content and visible frames are inside of the
5981 // final window frame.
5982 if (content.left < frame.left) content.left = frame.left;
5983 if (content.top < frame.top) content.top = frame.top;
5984 if (content.right > frame.right) content.right = frame.right;
5985 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
5986 if (visible.left < frame.left) visible.left = frame.left;
5987 if (visible.top < frame.top) visible.top = frame.top;
5988 if (visible.right > frame.right) visible.right = frame.right;
5989 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 final Rect contentInsets = mContentInsets;
5992 contentInsets.left = content.left-frame.left;
5993 contentInsets.top = content.top-frame.top;
5994 contentInsets.right = frame.right-content.right;
5995 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005997 final Rect visibleInsets = mVisibleInsets;
5998 visibleInsets.left = visible.left-frame.left;
5999 visibleInsets.top = visible.top-frame.top;
6000 visibleInsets.right = frame.right-visible.right;
6001 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 if (localLOGV) {
6004 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6005 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6006 Log.v(TAG, "Resolving (mRequestedWidth="
6007 + mRequestedWidth + ", mRequestedheight="
6008 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6009 + "): frame=" + mFrame.toShortString()
6010 + " ci=" + contentInsets.toShortString()
6011 + " vi=" + visibleInsets.toShortString());
6012 //}
6013 }
6014 }
Romain Guy06882f82009-06-10 13:36:04 -07006015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006016 public Rect getFrameLw() {
6017 return mFrame;
6018 }
6019
6020 public Rect getShownFrameLw() {
6021 return mShownFrame;
6022 }
6023
6024 public Rect getDisplayFrameLw() {
6025 return mDisplayFrame;
6026 }
6027
6028 public Rect getContentFrameLw() {
6029 return mContentFrame;
6030 }
6031
6032 public Rect getVisibleFrameLw() {
6033 return mVisibleFrame;
6034 }
6035
6036 public boolean getGivenInsetsPendingLw() {
6037 return mGivenInsetsPending;
6038 }
6039
6040 public Rect getGivenContentInsetsLw() {
6041 return mGivenContentInsets;
6042 }
Romain Guy06882f82009-06-10 13:36:04 -07006043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006044 public Rect getGivenVisibleInsetsLw() {
6045 return mGivenVisibleInsets;
6046 }
Romain Guy06882f82009-06-10 13:36:04 -07006047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 public WindowManager.LayoutParams getAttrs() {
6049 return mAttrs;
6050 }
6051
6052 public int getSurfaceLayer() {
6053 return mLayer;
6054 }
Romain Guy06882f82009-06-10 13:36:04 -07006055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 public IApplicationToken getAppToken() {
6057 return mAppToken != null ? mAppToken.appToken : null;
6058 }
6059
6060 public boolean hasAppShownWindows() {
6061 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6062 }
6063
6064 public boolean hasAppStartingIcon() {
6065 return mAppToken != null ? (mAppToken.startingData != null) : false;
6066 }
6067
6068 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6069 return mAppToken != null ? mAppToken.startingWindow : null;
6070 }
6071
6072 public void setAnimation(Animation anim) {
6073 if (localLOGV) Log.v(
6074 TAG, "Setting animation in " + this + ": " + anim);
6075 mAnimating = false;
6076 mLocalAnimating = false;
6077 mAnimation = anim;
6078 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6079 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6080 }
6081
6082 public void clearAnimation() {
6083 if (mAnimation != null) {
6084 mAnimating = true;
6085 mLocalAnimating = false;
6086 mAnimation = null;
6087 }
6088 }
Romain Guy06882f82009-06-10 13:36:04 -07006089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 Surface createSurfaceLocked() {
6091 if (mSurface == null) {
6092 mDrawPending = true;
6093 mCommitDrawPending = false;
6094 mReadyToShow = false;
6095 if (mAppToken != null) {
6096 mAppToken.allDrawn = false;
6097 }
6098
6099 int flags = 0;
6100 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
6101 flags |= Surface.HARDWARE;
6102 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6103 flags |= Surface.GPU;
6104 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6105 flags |= Surface.PUSH_BUFFERS;
6106 }
6107
6108 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6109 flags |= Surface.SECURE;
6110 }
6111 if (DEBUG_VISIBILITY) Log.v(
6112 TAG, "Creating surface in session "
6113 + mSession.mSurfaceSession + " window " + this
6114 + " w=" + mFrame.width()
6115 + " h=" + mFrame.height() + " format="
6116 + mAttrs.format + " flags=" + flags);
6117
6118 int w = mFrame.width();
6119 int h = mFrame.height();
6120 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6121 // for a scaled surface, we always want the requested
6122 // size.
6123 w = mRequestedWidth;
6124 h = mRequestedHeight;
6125 }
6126
6127 try {
6128 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006129 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006130 0, w, h, mAttrs.format, flags);
6131 } catch (Surface.OutOfResourcesException e) {
6132 Log.w(TAG, "OutOfResourcesException creating surface");
6133 reclaimSomeSurfaceMemoryLocked(this, "create");
6134 return null;
6135 } catch (Exception e) {
6136 Log.e(TAG, "Exception creating surface", e);
6137 return null;
6138 }
Romain Guy06882f82009-06-10 13:36:04 -07006139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006140 if (localLOGV) Log.v(
6141 TAG, "Got surface: " + mSurface
6142 + ", set left=" + mFrame.left + " top=" + mFrame.top
6143 + ", animLayer=" + mAnimLayer);
6144 if (SHOW_TRANSACTIONS) {
6145 Log.i(TAG, ">>> OPEN TRANSACTION");
6146 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6147 + mAttrs.getTitle() + ") pos=(" +
6148 mFrame.left + "," + mFrame.top + ") (" +
6149 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6150 mAnimLayer + " HIDE");
6151 }
6152 Surface.openTransaction();
6153 try {
6154 try {
6155 mSurface.setPosition(mFrame.left, mFrame.top);
6156 mSurface.setLayer(mAnimLayer);
6157 mSurface.hide();
6158 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6159 mSurface.setFlags(Surface.SURFACE_DITHER,
6160 Surface.SURFACE_DITHER);
6161 }
6162 } catch (RuntimeException e) {
6163 Log.w(TAG, "Error creating surface in " + w, e);
6164 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6165 }
6166 mLastHidden = true;
6167 } finally {
6168 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6169 Surface.closeTransaction();
6170 }
6171 if (localLOGV) Log.v(
6172 TAG, "Created surface " + this);
6173 }
6174 return mSurface;
6175 }
Romain Guy06882f82009-06-10 13:36:04 -07006176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 void destroySurfaceLocked() {
6178 // Window is no longer on-screen, so can no longer receive
6179 // key events... if we were waiting for it to finish
6180 // handling a key event, the wait is over!
6181 mKeyWaiter.finishedKey(mSession, mClient, true,
6182 KeyWaiter.RETURN_NOTHING);
6183 mKeyWaiter.releasePendingPointerLocked(mSession);
6184 mKeyWaiter.releasePendingTrackballLocked(mSession);
6185
6186 if (mAppToken != null && this == mAppToken.startingWindow) {
6187 mAppToken.startingDisplayed = false;
6188 }
Romain Guy06882f82009-06-10 13:36:04 -07006189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 if (localLOGV) Log.v(
6191 TAG, "Window " + this
6192 + " destroying surface " + mSurface + ", session " + mSession);
6193 if (mSurface != null) {
6194 try {
6195 if (SHOW_TRANSACTIONS) {
6196 RuntimeException ex = new RuntimeException();
6197 ex.fillInStackTrace();
6198 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6199 + mAttrs.getTitle() + ")", ex);
6200 }
6201 mSurface.clear();
6202 } catch (RuntimeException e) {
6203 Log.w(TAG, "Exception thrown when destroying Window " + this
6204 + " surface " + mSurface + " session " + mSession
6205 + ": " + e.toString());
6206 }
6207 mSurface = null;
6208 mDrawPending = false;
6209 mCommitDrawPending = false;
6210 mReadyToShow = false;
6211
6212 int i = mChildWindows.size();
6213 while (i > 0) {
6214 i--;
6215 WindowState c = (WindowState)mChildWindows.get(i);
6216 c.mAttachedHidden = true;
6217 }
6218 }
6219 }
6220
6221 boolean finishDrawingLocked() {
6222 if (mDrawPending) {
6223 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6224 TAG, "finishDrawingLocked: " + mSurface);
6225 mCommitDrawPending = true;
6226 mDrawPending = false;
6227 return true;
6228 }
6229 return false;
6230 }
6231
6232 // This must be called while inside a transaction.
6233 void commitFinishDrawingLocked(long currentTime) {
6234 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6235 if (!mCommitDrawPending) {
6236 return;
6237 }
6238 mCommitDrawPending = false;
6239 mReadyToShow = true;
6240 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6241 final AppWindowToken atoken = mAppToken;
6242 if (atoken == null || atoken.allDrawn || starting) {
6243 performShowLocked();
6244 }
6245 }
6246
6247 // This must be called while inside a transaction.
6248 boolean performShowLocked() {
6249 if (DEBUG_VISIBILITY) {
6250 RuntimeException e = new RuntimeException();
6251 e.fillInStackTrace();
6252 Log.v(TAG, "performShow on " + this
6253 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6254 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6255 }
6256 if (mReadyToShow && isReadyForDisplay()) {
6257 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6258 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6259 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6260 + " during animation: policyVis=" + mPolicyVisibility
6261 + " attHidden=" + mAttachedHidden
6262 + " tok.hiddenRequested="
6263 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6264 + " tok.idden="
6265 + (mAppToken != null ? mAppToken.hidden : false)
6266 + " animating=" + mAnimating
6267 + " tok animating="
6268 + (mAppToken != null ? mAppToken.animating : false));
6269 if (!showSurfaceRobustlyLocked(this)) {
6270 return false;
6271 }
6272 mLastAlpha = -1;
6273 mHasDrawn = true;
6274 mLastHidden = false;
6275 mReadyToShow = false;
6276 enableScreenIfNeededLocked();
6277
6278 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006280 int i = mChildWindows.size();
6281 while (i > 0) {
6282 i--;
6283 WindowState c = (WindowState)mChildWindows.get(i);
6284 if (c.mSurface != null && c.mAttachedHidden) {
6285 c.mAttachedHidden = false;
6286 c.performShowLocked();
6287 }
6288 }
Romain Guy06882f82009-06-10 13:36:04 -07006289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 if (mAttrs.type != TYPE_APPLICATION_STARTING
6291 && mAppToken != null) {
6292 mAppToken.firstWindowDrawn = true;
6293 if (mAnimation == null && mAppToken.startingData != null) {
6294 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6295 + mToken
6296 + ": first real window is shown, no animation");
6297 mFinishedStarting.add(mAppToken);
6298 mH.sendEmptyMessage(H.FINISHED_STARTING);
6299 }
6300 mAppToken.updateReportedVisibilityLocked();
6301 }
6302 }
6303 return true;
6304 }
Romain Guy06882f82009-06-10 13:36:04 -07006305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006306 // This must be called while inside a transaction. Returns true if
6307 // there is more animation to run.
6308 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6309 if (!mDisplayFrozen) {
6310 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006312 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6313 mHasTransformation = true;
6314 mHasLocalTransformation = true;
6315 if (!mLocalAnimating) {
6316 if (DEBUG_ANIM) Log.v(
6317 TAG, "Starting animation in " + this +
6318 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6319 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6320 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6321 mAnimation.setStartTime(currentTime);
6322 mLocalAnimating = true;
6323 mAnimating = true;
6324 }
6325 mTransformation.clear();
6326 final boolean more = mAnimation.getTransformation(
6327 currentTime, mTransformation);
6328 if (DEBUG_ANIM) Log.v(
6329 TAG, "Stepped animation in " + this +
6330 ": more=" + more + ", xform=" + mTransformation);
6331 if (more) {
6332 // we're not done!
6333 return true;
6334 }
6335 if (DEBUG_ANIM) Log.v(
6336 TAG, "Finished animation in " + this +
6337 " @ " + currentTime);
6338 mAnimation = null;
6339 //WindowManagerService.this.dump();
6340 }
6341 mHasLocalTransformation = false;
6342 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6343 && mAppToken.hasTransformation) {
6344 // When our app token is animating, we kind-of pretend like
6345 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6346 // part of this check means that we will only do this if
6347 // our window is not currently exiting, or it is not
6348 // locally animating itself. The idea being that one that
6349 // is exiting and doing a local animation should be removed
6350 // once that animation is done.
6351 mAnimating = true;
6352 mHasTransformation = true;
6353 mTransformation.clear();
6354 return false;
6355 } else if (mHasTransformation) {
6356 // Little trick to get through the path below to act like
6357 // we have finished an animation.
6358 mAnimating = true;
6359 } else if (isAnimating()) {
6360 mAnimating = true;
6361 }
6362 } else if (mAnimation != null) {
6363 // If the display is frozen, and there is a pending animation,
6364 // clear it and make sure we run the cleanup code.
6365 mAnimating = true;
6366 mLocalAnimating = true;
6367 mAnimation = null;
6368 }
Romain Guy06882f82009-06-10 13:36:04 -07006369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006370 if (!mAnimating && !mLocalAnimating) {
6371 return false;
6372 }
6373
6374 if (DEBUG_ANIM) Log.v(
6375 TAG, "Animation done in " + this + ": exiting=" + mExiting
6376 + ", reportedVisible="
6377 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006379 mAnimating = false;
6380 mLocalAnimating = false;
6381 mAnimation = null;
6382 mAnimLayer = mLayer;
6383 if (mIsImWindow) {
6384 mAnimLayer += mInputMethodAnimLayerAdjustment;
6385 }
6386 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6387 + " anim layer: " + mAnimLayer);
6388 mHasTransformation = false;
6389 mHasLocalTransformation = false;
6390 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6391 mTransformation.clear();
6392 if (mHasDrawn
6393 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6394 && mAppToken != null
6395 && mAppToken.firstWindowDrawn
6396 && mAppToken.startingData != null) {
6397 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6398 + mToken + ": first real window done animating");
6399 mFinishedStarting.add(mAppToken);
6400 mH.sendEmptyMessage(H.FINISHED_STARTING);
6401 }
Romain Guy06882f82009-06-10 13:36:04 -07006402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 finishExit();
6404
6405 if (mAppToken != null) {
6406 mAppToken.updateReportedVisibilityLocked();
6407 }
6408
6409 return false;
6410 }
6411
6412 void finishExit() {
6413 if (DEBUG_ANIM) Log.v(
6414 TAG, "finishExit in " + this
6415 + ": exiting=" + mExiting
6416 + " remove=" + mRemoveOnExit
6417 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419 final int N = mChildWindows.size();
6420 for (int i=0; i<N; i++) {
6421 ((WindowState)mChildWindows.get(i)).finishExit();
6422 }
Romain Guy06882f82009-06-10 13:36:04 -07006423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006424 if (!mExiting) {
6425 return;
6426 }
Romain Guy06882f82009-06-10 13:36:04 -07006427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006428 if (isWindowAnimating()) {
6429 return;
6430 }
6431
6432 if (localLOGV) Log.v(
6433 TAG, "Exit animation finished in " + this
6434 + ": remove=" + mRemoveOnExit);
6435 if (mSurface != null) {
6436 mDestroySurface.add(this);
6437 mDestroying = true;
6438 if (SHOW_TRANSACTIONS) Log.i(
6439 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6440 try {
6441 mSurface.hide();
6442 } catch (RuntimeException e) {
6443 Log.w(TAG, "Error hiding surface in " + this, e);
6444 }
6445 mLastHidden = true;
6446 mKeyWaiter.releasePendingPointerLocked(mSession);
6447 }
6448 mExiting = false;
6449 if (mRemoveOnExit) {
6450 mPendingRemove.add(this);
6451 mRemoveOnExit = false;
6452 }
6453 }
Romain Guy06882f82009-06-10 13:36:04 -07006454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6456 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6457 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6458 if (dtdx < -.000001f || dtdx > .000001f) return false;
6459 if (dsdy < -.000001f || dsdy > .000001f) return false;
6460 return true;
6461 }
Romain Guy06882f82009-06-10 13:36:04 -07006462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 void computeShownFrameLocked() {
6464 final boolean selfTransformation = mHasLocalTransformation;
6465 Transformation attachedTransformation =
6466 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6467 ? mAttachedWindow.mTransformation : null;
6468 Transformation appTransformation =
6469 (mAppToken != null && mAppToken.hasTransformation)
6470 ? mAppToken.transformation : null;
6471 if (selfTransformation || attachedTransformation != null
6472 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006473 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006474 final Rect frame = mFrame;
6475 final float tmpFloats[] = mTmpFloats;
6476 final Matrix tmpMatrix = mTmpMatrix;
6477
6478 // Compute the desired transformation.
6479 tmpMatrix.setTranslate(frame.left, frame.top);
6480 if (selfTransformation) {
6481 tmpMatrix.preConcat(mTransformation.getMatrix());
6482 }
6483 if (attachedTransformation != null) {
6484 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6485 }
6486 if (appTransformation != null) {
6487 tmpMatrix.preConcat(appTransformation.getMatrix());
6488 }
6489
6490 // "convert" it into SurfaceFlinger's format
6491 // (a 2x2 matrix + an offset)
6492 // Here we must not transform the position of the surface
6493 // since it is already included in the transformation.
6494 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006496 tmpMatrix.getValues(tmpFloats);
6497 mDsDx = tmpFloats[Matrix.MSCALE_X];
6498 mDtDx = tmpFloats[Matrix.MSKEW_X];
6499 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6500 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6501 int x = (int)tmpFloats[Matrix.MTRANS_X];
6502 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6503 int w = frame.width();
6504 int h = frame.height();
6505 mShownFrame.set(x, y, x+w, y+h);
6506
6507 // Now set the alpha... but because our current hardware
6508 // can't do alpha transformation on a non-opaque surface,
6509 // turn it off if we are running an animation that is also
6510 // transforming since it is more important to have that
6511 // animation be smooth.
6512 mShownAlpha = mAlpha;
6513 if (!mLimitedAlphaCompositing
6514 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6515 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6516 && x == frame.left && y == frame.top))) {
6517 //Log.i(TAG, "Applying alpha transform");
6518 if (selfTransformation) {
6519 mShownAlpha *= mTransformation.getAlpha();
6520 }
6521 if (attachedTransformation != null) {
6522 mShownAlpha *= attachedTransformation.getAlpha();
6523 }
6524 if (appTransformation != null) {
6525 mShownAlpha *= appTransformation.getAlpha();
6526 }
6527 } else {
6528 //Log.i(TAG, "Not applying alpha transform");
6529 }
Romain Guy06882f82009-06-10 13:36:04 -07006530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006531 if (localLOGV) Log.v(
6532 TAG, "Continuing animation in " + this +
6533 ": " + mShownFrame +
6534 ", alpha=" + mTransformation.getAlpha());
6535 return;
6536 }
Romain Guy06882f82009-06-10 13:36:04 -07006537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 mShownFrame.set(mFrame);
6539 mShownAlpha = mAlpha;
6540 mDsDx = 1;
6541 mDtDx = 0;
6542 mDsDy = 0;
6543 mDtDy = 1;
6544 }
Romain Guy06882f82009-06-10 13:36:04 -07006545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006546 /**
6547 * Is this window visible? It is not visible if there is no
6548 * surface, or we are in the process of running an exit animation
6549 * that will remove the surface, or its app token has been hidden.
6550 */
6551 public boolean isVisibleLw() {
6552 final AppWindowToken atoken = mAppToken;
6553 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6554 && (atoken == null || !atoken.hiddenRequested)
6555 && !mExiting && !mDestroying;
6556 }
6557
6558 /**
6559 * Is this window visible, ignoring its app token? It is not visible
6560 * if there is no surface, or we are in the process of running an exit animation
6561 * that will remove the surface.
6562 */
6563 public boolean isWinVisibleLw() {
6564 final AppWindowToken atoken = mAppToken;
6565 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6566 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6567 && !mExiting && !mDestroying;
6568 }
6569
6570 /**
6571 * The same as isVisible(), but follows the current hidden state of
6572 * the associated app token, not the pending requested hidden state.
6573 */
6574 boolean isVisibleNow() {
6575 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006576 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006577 }
6578
6579 /**
6580 * Same as isVisible(), but we also count it as visible between the
6581 * call to IWindowSession.add() and the first relayout().
6582 */
6583 boolean isVisibleOrAdding() {
6584 final AppWindowToken atoken = mAppToken;
6585 return (mSurface != null
6586 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6587 && mPolicyVisibility && !mAttachedHidden
6588 && (atoken == null || !atoken.hiddenRequested)
6589 && !mExiting && !mDestroying;
6590 }
6591
6592 /**
6593 * Is this window currently on-screen? It is on-screen either if it
6594 * is visible or it is currently running an animation before no longer
6595 * being visible.
6596 */
6597 boolean isOnScreen() {
6598 final AppWindowToken atoken = mAppToken;
6599 if (atoken != null) {
6600 return mSurface != null && mPolicyVisibility && !mDestroying
6601 && ((!mAttachedHidden && !atoken.hiddenRequested)
6602 || mAnimating || atoken.animating);
6603 } else {
6604 return mSurface != null && mPolicyVisibility && !mDestroying
6605 && (!mAttachedHidden || mAnimating);
6606 }
6607 }
Romain Guy06882f82009-06-10 13:36:04 -07006608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006609 /**
6610 * Like isOnScreen(), but we don't return true if the window is part
6611 * of a transition that has not yet been started.
6612 */
6613 boolean isReadyForDisplay() {
6614 final AppWindowToken atoken = mAppToken;
6615 final boolean animating = atoken != null ? atoken.animating : false;
6616 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006617 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006618 || mAnimating || animating);
6619 }
6620
6621 /** Is the window or its container currently animating? */
6622 boolean isAnimating() {
6623 final WindowState attached = mAttachedWindow;
6624 final AppWindowToken atoken = mAppToken;
6625 return mAnimation != null
6626 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006627 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 (atoken.animation != null
6629 || atoken.inPendingTransaction));
6630 }
6631
6632 /** Is this window currently animating? */
6633 boolean isWindowAnimating() {
6634 return mAnimation != null;
6635 }
6636
6637 /**
6638 * Like isOnScreen, but returns false if the surface hasn't yet
6639 * been drawn.
6640 */
6641 public boolean isDisplayedLw() {
6642 final AppWindowToken atoken = mAppToken;
6643 return mSurface != null && mPolicyVisibility && !mDestroying
6644 && !mDrawPending && !mCommitDrawPending
6645 && ((!mAttachedHidden &&
6646 (atoken == null || !atoken.hiddenRequested))
6647 || mAnimating);
6648 }
6649
6650 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6651 boolean shownFrame, boolean onlyOpaque) {
6652 if (mSurface == null) {
6653 return false;
6654 }
6655 if (mAppToken != null && !mAppToken.appFullscreen) {
6656 return false;
6657 }
6658 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6659 return false;
6660 }
6661 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006662
6663 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
6664 return frame.left <= mCompatibleScreenFrame.left &&
6665 frame.top <= mCompatibleScreenFrame.top &&
6666 frame.right >= mCompatibleScreenFrame.right &&
6667 frame.bottom >= mCompatibleScreenFrame.bottom;
6668 } else {
6669 return frame.left <= 0 && frame.top <= 0
6670 && frame.right >= screenWidth
6671 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 }
Romain Guy06882f82009-06-10 13:36:04 -07006674
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006675 /**
6676 * Return true if the window is opaque and fully drawn.
6677 */
6678 boolean isOpaqueDrawn() {
6679 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
6680 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
6681 }
6682
6683 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
6684 return
6685 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006686 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
6687 // only if it's visible
6688 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006689 // and only if the application fills the compatible screen
6690 mFrame.left <= mCompatibleScreenFrame.left &&
6691 mFrame.top <= mCompatibleScreenFrame.top &&
6692 mFrame.right >= mCompatibleScreenFrame.right &&
6693 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006694 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006695 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006696 }
6697
6698 boolean isFullscreen(int screenWidth, int screenHeight) {
6699 return mFrame.left <= 0 && mFrame.top <= 0 &&
6700 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006701 }
6702
6703 void removeLocked() {
6704 if (mAttachedWindow != null) {
6705 mAttachedWindow.mChildWindows.remove(this);
6706 }
6707 destroySurfaceLocked();
6708 mSession.windowRemovedLocked();
6709 try {
6710 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6711 } catch (RuntimeException e) {
6712 // Ignore if it has already been removed (usually because
6713 // we are doing this as part of processing a death note.)
6714 }
6715 }
6716
6717 private class DeathRecipient implements IBinder.DeathRecipient {
6718 public void binderDied() {
6719 try {
6720 synchronized(mWindowMap) {
6721 WindowState win = windowForClientLocked(mSession, mClient);
6722 Log.i(TAG, "WIN DEATH: " + win);
6723 if (win != null) {
6724 removeWindowLocked(mSession, win);
6725 }
6726 }
6727 } catch (IllegalArgumentException ex) {
6728 // This will happen if the window has already been
6729 // removed.
6730 }
6731 }
6732 }
6733
6734 /** Returns true if this window desires key events. */
6735 public final boolean canReceiveKeys() {
6736 return isVisibleOrAdding()
6737 && (mViewVisibility == View.VISIBLE)
6738 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6739 }
6740
6741 public boolean hasDrawnLw() {
6742 return mHasDrawn;
6743 }
6744
6745 public boolean showLw(boolean doAnimation) {
6746 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6747 mPolicyVisibility = true;
6748 mPolicyVisibilityAfterAnim = true;
6749 if (doAnimation) {
6750 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6751 }
6752 requestAnimationLocked(0);
6753 return true;
6754 }
6755 return false;
6756 }
6757
6758 public boolean hideLw(boolean doAnimation) {
6759 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6760 : mPolicyVisibility;
6761 if (current) {
6762 if (doAnimation) {
6763 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6764 if (mAnimation == null) {
6765 doAnimation = false;
6766 }
6767 }
6768 if (doAnimation) {
6769 mPolicyVisibilityAfterAnim = false;
6770 } else {
6771 mPolicyVisibilityAfterAnim = false;
6772 mPolicyVisibility = false;
6773 }
6774 requestAnimationLocked(0);
6775 return true;
6776 }
6777 return false;
6778 }
6779
6780 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006781 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07006782
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006783 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6784 pw.print(" mClient="); pw.println(mClient.asBinder());
6785 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6786 if (mAttachedWindow != null || mLayoutAttached) {
6787 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6788 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6789 }
6790 if (mIsImWindow) {
6791 pw.print(prefix); pw.print("mIsImWindow="); pw.println(mIsImWindow);
6792 }
6793 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
6794 pw.print(" mSubLayer="); pw.print(mSubLayer);
6795 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
6796 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
6797 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
6798 pw.print("="); pw.print(mAnimLayer);
6799 pw.print(" mLastLayer="); pw.println(mLastLayer);
6800 if (mSurface != null) {
6801 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
6802 }
6803 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
6804 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
6805 if (mAppToken != null) {
6806 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
6807 }
6808 if (mTargetAppToken != null) {
6809 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
6810 }
6811 pw.print(prefix); pw.print("mViewVisibility=0x");
6812 pw.print(Integer.toHexString(mViewVisibility));
6813 pw.print(" mLastHidden="); pw.print(mLastHidden);
6814 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
6815 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
6816 pw.print(prefix); pw.print("mPolicyVisibility=");
6817 pw.print(mPolicyVisibility);
6818 pw.print(" mPolicyVisibilityAfterAnim=");
6819 pw.print(mPolicyVisibilityAfterAnim);
6820 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
6821 }
6822 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
6823 pw.print(" h="); pw.print(mRequestedHeight);
6824 pw.print(" x="); pw.print(mReqXPos);
6825 pw.print(" y="); pw.println(mReqYPos);
6826 pw.print(prefix); pw.print("mGivenContentInsets=");
6827 mGivenContentInsets.printShortString(pw);
6828 pw.print(" mGivenVisibleInsets=");
6829 mGivenVisibleInsets.printShortString(pw);
6830 pw.println();
6831 if (mTouchableInsets != 0 || mGivenInsetsPending) {
6832 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
6833 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
6834 }
6835 pw.print(prefix); pw.print("mShownFrame=");
6836 mShownFrame.printShortString(pw);
6837 pw.print(" last="); mLastShownFrame.printShortString(pw);
6838 pw.println();
6839 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
6840 pw.print(" last="); mLastFrame.printShortString(pw);
6841 pw.println();
6842 pw.print(prefix); pw.print("mContainingFrame=");
6843 mContainingFrame.printShortString(pw);
6844 pw.print(" mDisplayFrame=");
6845 mDisplayFrame.printShortString(pw);
6846 pw.println();
6847 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
6848 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
6849 pw.println();
6850 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
6851 pw.print(" last="); mLastContentInsets.printShortString(pw);
6852 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
6853 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
6854 pw.println();
6855 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
6856 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
6857 pw.print(" mAlpha="); pw.print(mAlpha);
6858 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
6859 }
6860 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
6861 || mAnimation != null) {
6862 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
6863 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
6864 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
6865 pw.print(" mAnimation="); pw.println(mAnimation);
6866 }
6867 if (mHasTransformation || mHasLocalTransformation) {
6868 pw.print(prefix); pw.print("XForm: has=");
6869 pw.print(mHasTransformation);
6870 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
6871 pw.print(" "); mTransformation.printShortString(pw);
6872 pw.println();
6873 }
6874 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
6875 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
6876 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
6877 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
6878 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
6879 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
6880 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
6881 pw.print(" mDestroying="); pw.print(mDestroying);
6882 pw.print(" mRemoved="); pw.println(mRemoved);
6883 }
6884 if (mOrientationChanging || mAppFreezing) {
6885 pw.print(prefix); pw.print("mOrientationChanging=");
6886 pw.print(mOrientationChanging);
6887 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
6888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006889 }
6890
6891 @Override
6892 public String toString() {
6893 return "Window{"
6894 + Integer.toHexString(System.identityHashCode(this))
6895 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
6896 }
6897 }
Romain Guy06882f82009-06-10 13:36:04 -07006898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899 // -------------------------------------------------------------
6900 // Window Token State
6901 // -------------------------------------------------------------
6902
6903 class WindowToken {
6904 // The actual token.
6905 final IBinder token;
6906
6907 // The type of window this token is for, as per WindowManager.LayoutParams.
6908 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07006909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 // Set if this token was explicitly added by a client, so should
6911 // not be removed when all windows are removed.
6912 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07006913
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006914 // For printing.
6915 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07006916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917 // If this is an AppWindowToken, this is non-null.
6918 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07006919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006920 // All of the windows associated with this token.
6921 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
6922
6923 // Is key dispatching paused for this token?
6924 boolean paused = false;
6925
6926 // Should this token's windows be hidden?
6927 boolean hidden;
6928
6929 // Temporary for finding which tokens no longer have visible windows.
6930 boolean hasVisible;
6931
6932 WindowToken(IBinder _token, int type, boolean _explicit) {
6933 token = _token;
6934 windowType = type;
6935 explicit = _explicit;
6936 }
6937
6938 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006939 pw.print(prefix); pw.print("token="); pw.println(token);
6940 pw.print(prefix); pw.print("windows="); pw.println(windows);
6941 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
6942 pw.print(" hidden="); pw.print(hidden);
6943 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006944 }
6945
6946 @Override
6947 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006948 if (stringName == null) {
6949 StringBuilder sb = new StringBuilder();
6950 sb.append("WindowToken{");
6951 sb.append(Integer.toHexString(System.identityHashCode(this)));
6952 sb.append(" token="); sb.append(token); sb.append('}');
6953 stringName = sb.toString();
6954 }
6955 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 }
6957 };
6958
6959 class AppWindowToken extends WindowToken {
6960 // Non-null only for application tokens.
6961 final IApplicationToken appToken;
6962
6963 // All of the windows and child windows that are included in this
6964 // application token. Note this list is NOT sorted!
6965 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
6966
6967 int groupId = -1;
6968 boolean appFullscreen;
6969 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07006970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006971 // These are used for determining when all windows associated with
6972 // an activity have been drawn, so they can be made visible together
6973 // at the same time.
6974 int lastTransactionSequence = mTransactionSequence-1;
6975 int numInterestingWindows;
6976 int numDrawnWindows;
6977 boolean inPendingTransaction;
6978 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07006979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006980 // Is this token going to be hidden in a little while? If so, it
6981 // won't be taken into account for setting the screen orientation.
6982 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 // Is this window's surface needed? This is almost like hidden, except
6985 // it will sometimes be true a little earlier: when the token has
6986 // been shown, but is still waiting for its app transition to execute
6987 // before making its windows shown.
6988 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07006989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006990 // Have we told the window clients to hide themselves?
6991 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 // Last visibility state we reported to the app token.
6994 boolean reportedVisible;
6995
6996 // Set to true when the token has been removed from the window mgr.
6997 boolean removed;
6998
6999 // Have we been asked to have this token keep the screen frozen?
7000 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 boolean animating;
7003 Animation animation;
7004 boolean hasTransformation;
7005 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 // Offset to the window of all layers in the token, for use by
7008 // AppWindowToken animations.
7009 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007011 // Information about an application starting window if displayed.
7012 StartingData startingData;
7013 WindowState startingWindow;
7014 View startingView;
7015 boolean startingDisplayed;
7016 boolean startingMoved;
7017 boolean firstWindowDrawn;
7018
7019 AppWindowToken(IApplicationToken _token) {
7020 super(_token.asBinder(),
7021 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7022 appWindowToken = this;
7023 appToken = _token;
7024 }
Romain Guy06882f82009-06-10 13:36:04 -07007025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007026 public void setAnimation(Animation anim) {
7027 if (localLOGV) Log.v(
7028 TAG, "Setting animation in " + this + ": " + anim);
7029 animation = anim;
7030 animating = false;
7031 anim.restrictDuration(MAX_ANIMATION_DURATION);
7032 anim.scaleCurrentDuration(mTransitionAnimationScale);
7033 int zorder = anim.getZAdjustment();
7034 int adj = 0;
7035 if (zorder == Animation.ZORDER_TOP) {
7036 adj = TYPE_LAYER_OFFSET;
7037 } else if (zorder == Animation.ZORDER_BOTTOM) {
7038 adj = -TYPE_LAYER_OFFSET;
7039 }
Romain Guy06882f82009-06-10 13:36:04 -07007040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041 if (animLayerAdjustment != adj) {
7042 animLayerAdjustment = adj;
7043 updateLayers();
7044 }
7045 }
Romain Guy06882f82009-06-10 13:36:04 -07007046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 public void setDummyAnimation() {
7048 if (animation == null) {
7049 if (localLOGV) Log.v(
7050 TAG, "Setting dummy animation in " + this);
7051 animation = sDummyAnimation;
7052 }
7053 }
7054
7055 public void clearAnimation() {
7056 if (animation != null) {
7057 animation = null;
7058 animating = true;
7059 }
7060 }
Romain Guy06882f82009-06-10 13:36:04 -07007061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007062 void updateLayers() {
7063 final int N = allAppWindows.size();
7064 final int adj = animLayerAdjustment;
7065 for (int i=0; i<N; i++) {
7066 WindowState w = allAppWindows.get(i);
7067 w.mAnimLayer = w.mLayer + adj;
7068 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7069 + w.mAnimLayer);
7070 if (w == mInputMethodTarget) {
7071 setInputMethodAnimLayerAdjustment(adj);
7072 }
7073 }
7074 }
Romain Guy06882f82009-06-10 13:36:04 -07007075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 void sendAppVisibilityToClients() {
7077 final int N = allAppWindows.size();
7078 for (int i=0; i<N; i++) {
7079 WindowState win = allAppWindows.get(i);
7080 if (win == startingWindow && clientHidden) {
7081 // Don't hide the starting window.
7082 continue;
7083 }
7084 try {
7085 if (DEBUG_VISIBILITY) Log.v(TAG,
7086 "Setting visibility of " + win + ": " + (!clientHidden));
7087 win.mClient.dispatchAppVisibility(!clientHidden);
7088 } catch (RemoteException e) {
7089 }
7090 }
7091 }
Romain Guy06882f82009-06-10 13:36:04 -07007092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007093 void showAllWindowsLocked() {
7094 final int NW = allAppWindows.size();
7095 for (int i=0; i<NW; i++) {
7096 WindowState w = allAppWindows.get(i);
7097 if (DEBUG_VISIBILITY) Log.v(TAG,
7098 "performing show on: " + w);
7099 w.performShowLocked();
7100 }
7101 }
Romain Guy06882f82009-06-10 13:36:04 -07007102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007103 // This must be called while inside a transaction.
7104 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7105 if (!mDisplayFrozen) {
7106 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007108 if (animation == sDummyAnimation) {
7109 // This guy is going to animate, but not yet. For now count
7110 // it is not animating for purposes of scheduling transactions;
7111 // when it is really time to animate, this will be set to
7112 // a real animation and the next call will execute normally.
7113 return false;
7114 }
Romain Guy06882f82009-06-10 13:36:04 -07007115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007116 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7117 if (!animating) {
7118 if (DEBUG_ANIM) Log.v(
7119 TAG, "Starting animation in " + this +
7120 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7121 + " scale=" + mTransitionAnimationScale
7122 + " allDrawn=" + allDrawn + " animating=" + animating);
7123 animation.initialize(dw, dh, dw, dh);
7124 animation.setStartTime(currentTime);
7125 animating = true;
7126 }
7127 transformation.clear();
7128 final boolean more = animation.getTransformation(
7129 currentTime, transformation);
7130 if (DEBUG_ANIM) Log.v(
7131 TAG, "Stepped animation in " + this +
7132 ": more=" + more + ", xform=" + transformation);
7133 if (more) {
7134 // we're done!
7135 hasTransformation = true;
7136 return true;
7137 }
7138 if (DEBUG_ANIM) Log.v(
7139 TAG, "Finished animation in " + this +
7140 " @ " + currentTime);
7141 animation = null;
7142 }
7143 } else if (animation != null) {
7144 // If the display is frozen, and there is a pending animation,
7145 // clear it and make sure we run the cleanup code.
7146 animating = true;
7147 animation = null;
7148 }
7149
7150 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007152 if (!animating) {
7153 return false;
7154 }
7155
7156 clearAnimation();
7157 animating = false;
7158 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7159 moveInputMethodWindowsIfNeededLocked(true);
7160 }
Romain Guy06882f82009-06-10 13:36:04 -07007161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007162 if (DEBUG_ANIM) Log.v(
7163 TAG, "Animation done in " + this
7164 + ": reportedVisible=" + reportedVisible);
7165
7166 transformation.clear();
7167 if (animLayerAdjustment != 0) {
7168 animLayerAdjustment = 0;
7169 updateLayers();
7170 }
Romain Guy06882f82009-06-10 13:36:04 -07007171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007172 final int N = windows.size();
7173 for (int i=0; i<N; i++) {
7174 ((WindowState)windows.get(i)).finishExit();
7175 }
7176 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007178 return false;
7179 }
7180
7181 void updateReportedVisibilityLocked() {
7182 if (appToken == null) {
7183 return;
7184 }
Romain Guy06882f82009-06-10 13:36:04 -07007185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007186 int numInteresting = 0;
7187 int numVisible = 0;
7188 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007190 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7191 final int N = allAppWindows.size();
7192 for (int i=0; i<N; i++) {
7193 WindowState win = allAppWindows.get(i);
7194 if (win == startingWindow || win.mAppFreezing) {
7195 continue;
7196 }
7197 if (DEBUG_VISIBILITY) {
7198 Log.v(TAG, "Win " + win + ": isDisplayed="
7199 + win.isDisplayedLw()
7200 + ", isAnimating=" + win.isAnimating());
7201 if (!win.isDisplayedLw()) {
7202 Log.v(TAG, "Not displayed: s=" + win.mSurface
7203 + " pv=" + win.mPolicyVisibility
7204 + " dp=" + win.mDrawPending
7205 + " cdp=" + win.mCommitDrawPending
7206 + " ah=" + win.mAttachedHidden
7207 + " th="
7208 + (win.mAppToken != null
7209 ? win.mAppToken.hiddenRequested : false)
7210 + " a=" + win.mAnimating);
7211 }
7212 }
7213 numInteresting++;
7214 if (win.isDisplayedLw()) {
7215 if (!win.isAnimating()) {
7216 numVisible++;
7217 }
7218 nowGone = false;
7219 } else if (win.isAnimating()) {
7220 nowGone = false;
7221 }
7222 }
Romain Guy06882f82009-06-10 13:36:04 -07007223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7225 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7226 + numInteresting + " visible=" + numVisible);
7227 if (nowVisible != reportedVisible) {
7228 if (DEBUG_VISIBILITY) Log.v(
7229 TAG, "Visibility changed in " + this
7230 + ": vis=" + nowVisible);
7231 reportedVisible = nowVisible;
7232 Message m = mH.obtainMessage(
7233 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7234 nowVisible ? 1 : 0,
7235 nowGone ? 1 : 0,
7236 this);
7237 mH.sendMessage(m);
7238 }
7239 }
Romain Guy06882f82009-06-10 13:36:04 -07007240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 void dump(PrintWriter pw, String prefix) {
7242 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007243 if (appToken != null) {
7244 pw.print(prefix); pw.println("app=true");
7245 }
7246 if (allAppWindows.size() > 0) {
7247 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7248 }
7249 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7250 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7251 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7252 pw.print(" clientHidden="); pw.print(clientHidden);
7253 pw.print(" willBeHidden="); pw.print(willBeHidden);
7254 pw.print(" reportedVisible="); pw.println(reportedVisible);
7255 if (paused || freezingScreen) {
7256 pw.print(prefix); pw.print("paused="); pw.print(paused);
7257 pw.print(" freezingScreen="); pw.println(freezingScreen);
7258 }
7259 if (numInterestingWindows != 0 || numDrawnWindows != 0
7260 || inPendingTransaction || allDrawn) {
7261 pw.print(prefix); pw.print("numInterestingWindows=");
7262 pw.print(numInterestingWindows);
7263 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7264 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7265 pw.print(" allDrawn="); pw.println(allDrawn);
7266 }
7267 if (animating || animation != null) {
7268 pw.print(prefix); pw.print("animating="); pw.print(animating);
7269 pw.print(" animation="); pw.println(animation);
7270 }
7271 if (animLayerAdjustment != 0) {
7272 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7273 }
7274 if (hasTransformation) {
7275 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7276 pw.print(" transformation="); transformation.printShortString(pw);
7277 pw.println();
7278 }
7279 if (startingData != null || removed || firstWindowDrawn) {
7280 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7281 pw.print(" removed="); pw.print(removed);
7282 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7283 }
7284 if (startingWindow != null || startingView != null
7285 || startingDisplayed || startingMoved) {
7286 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7287 pw.print(" startingView="); pw.print(startingView);
7288 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7289 pw.print(" startingMoved"); pw.println(startingMoved);
7290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007291 }
7292
7293 @Override
7294 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007295 if (stringName == null) {
7296 StringBuilder sb = new StringBuilder();
7297 sb.append("AppWindowToken{");
7298 sb.append(Integer.toHexString(System.identityHashCode(this)));
7299 sb.append(" token="); sb.append(token); sb.append('}');
7300 stringName = sb.toString();
7301 }
7302 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007303 }
7304 }
Romain Guy06882f82009-06-10 13:36:04 -07007305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007306 public static WindowManager.LayoutParams findAnimations(
7307 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007308 ArrayList<AppWindowToken> openingTokenList1,
7309 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007310 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007311
7312 // First, check if there is a compatible window in opening/closing
7313 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007314 WindowManager.LayoutParams animParams = null;
7315 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007316 animParams = findCompatibleWindowParams(openingTokenList1);
7317 if (animParams == null) {
7318 animParams = findCompatibleWindowParams(closingTokenList2);
7319 }
7320 if (animParams != null) {
7321 return animParams;
7322 }
7323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007324 //Log.i(TAG, "Looking for animations...");
7325 for (int i=order.size()-1; i>=0; i--) {
7326 AppWindowToken wtoken = order.get(i);
7327 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007328 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 int j = wtoken.windows.size();
7330 while (j > 0) {
7331 j--;
7332 WindowState win = wtoken.windows.get(j);
7333 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7334 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7335 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7336 //Log.i(TAG, "Found base or application window, done!");
7337 if (wtoken.appFullscreen) {
7338 return win.mAttrs;
7339 }
7340 if (animSrc < 2) {
7341 animParams = win.mAttrs;
7342 animSrc = 2;
7343 }
7344 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7345 //Log.i(TAG, "Found normal window, we may use this...");
7346 animParams = win.mAttrs;
7347 animSrc = 1;
7348 }
7349 }
7350 }
7351 }
Romain Guy06882f82009-06-10 13:36:04 -07007352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007353 return animParams;
7354 }
Romain Guy06882f82009-06-10 13:36:04 -07007355
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007356 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7357 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7358 AppWindowToken wtoken = tokenList.get(appCount);
7359 // Just checking one window is sufficient as all windows have the compatible flag
7360 // if the application is in compatibility mode.
7361 if (wtoken.windows.size() > 0) {
7362 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7363 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7364 return params;
7365 }
7366 }
7367 }
7368 return null;
7369 }
7370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007371 // -------------------------------------------------------------
7372 // DummyAnimation
7373 // -------------------------------------------------------------
7374
7375 // This is an animation that does nothing: it just immediately finishes
7376 // itself every time it is called. It is used as a stub animation in cases
7377 // where we want to synchronize multiple things that may be animating.
7378 static final class DummyAnimation extends Animation {
7379 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7380 return false;
7381 }
7382 }
7383 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 // -------------------------------------------------------------
7386 // Async Handler
7387 // -------------------------------------------------------------
7388
7389 static final class StartingData {
7390 final String pkg;
7391 final int theme;
7392 final CharSequence nonLocalizedLabel;
7393 final int labelRes;
7394 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007396 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7397 int _labelRes, int _icon) {
7398 pkg = _pkg;
7399 theme = _theme;
7400 nonLocalizedLabel = _nonLocalizedLabel;
7401 labelRes = _labelRes;
7402 icon = _icon;
7403 }
7404 }
7405
7406 private final class H extends Handler {
7407 public static final int REPORT_FOCUS_CHANGE = 2;
7408 public static final int REPORT_LOSING_FOCUS = 3;
7409 public static final int ANIMATE = 4;
7410 public static final int ADD_STARTING = 5;
7411 public static final int REMOVE_STARTING = 6;
7412 public static final int FINISHED_STARTING = 7;
7413 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7415 public static final int HOLD_SCREEN_CHANGED = 12;
7416 public static final int APP_TRANSITION_TIMEOUT = 13;
7417 public static final int PERSIST_ANIMATION_SCALE = 14;
7418 public static final int FORCE_GC = 15;
7419 public static final int ENABLE_SCREEN = 16;
7420 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007421 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007423 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 public H() {
7426 }
Romain Guy06882f82009-06-10 13:36:04 -07007427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 @Override
7429 public void handleMessage(Message msg) {
7430 switch (msg.what) {
7431 case REPORT_FOCUS_CHANGE: {
7432 WindowState lastFocus;
7433 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007435 synchronized(mWindowMap) {
7436 lastFocus = mLastFocus;
7437 newFocus = mCurrentFocus;
7438 if (lastFocus == newFocus) {
7439 // Focus is not changing, so nothing to do.
7440 return;
7441 }
7442 mLastFocus = newFocus;
7443 //Log.i(TAG, "Focus moving from " + lastFocus
7444 // + " to " + newFocus);
7445 if (newFocus != null && lastFocus != null
7446 && !newFocus.isDisplayedLw()) {
7447 //Log.i(TAG, "Delaying loss of focus...");
7448 mLosingFocus.add(lastFocus);
7449 lastFocus = null;
7450 }
7451 }
7452
7453 if (lastFocus != newFocus) {
7454 //System.out.println("Changing focus from " + lastFocus
7455 // + " to " + newFocus);
7456 if (newFocus != null) {
7457 try {
7458 //Log.i(TAG, "Gaining focus: " + newFocus);
7459 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7460 } catch (RemoteException e) {
7461 // Ignore if process has died.
7462 }
7463 }
7464
7465 if (lastFocus != null) {
7466 try {
7467 //Log.i(TAG, "Losing focus: " + lastFocus);
7468 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7469 } catch (RemoteException e) {
7470 // Ignore if process has died.
7471 }
7472 }
7473 }
7474 } break;
7475
7476 case REPORT_LOSING_FOCUS: {
7477 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007479 synchronized(mWindowMap) {
7480 losers = mLosingFocus;
7481 mLosingFocus = new ArrayList<WindowState>();
7482 }
7483
7484 final int N = losers.size();
7485 for (int i=0; i<N; i++) {
7486 try {
7487 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7488 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7489 } catch (RemoteException e) {
7490 // Ignore if process has died.
7491 }
7492 }
7493 } break;
7494
7495 case ANIMATE: {
7496 synchronized(mWindowMap) {
7497 mAnimationPending = false;
7498 performLayoutAndPlaceSurfacesLocked();
7499 }
7500 } break;
7501
7502 case ADD_STARTING: {
7503 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7504 final StartingData sd = wtoken.startingData;
7505
7506 if (sd == null) {
7507 // Animation has been canceled... do nothing.
7508 return;
7509 }
Romain Guy06882f82009-06-10 13:36:04 -07007510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007511 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7512 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007514 View view = null;
7515 try {
7516 view = mPolicy.addStartingWindow(
7517 wtoken.token, sd.pkg,
7518 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7519 sd.icon);
7520 } catch (Exception e) {
7521 Log.w(TAG, "Exception when adding starting window", e);
7522 }
7523
7524 if (view != null) {
7525 boolean abort = false;
7526
7527 synchronized(mWindowMap) {
7528 if (wtoken.removed || wtoken.startingData == null) {
7529 // If the window was successfully added, then
7530 // we need to remove it.
7531 if (wtoken.startingWindow != null) {
7532 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7533 "Aborted starting " + wtoken
7534 + ": removed=" + wtoken.removed
7535 + " startingData=" + wtoken.startingData);
7536 wtoken.startingWindow = null;
7537 wtoken.startingData = null;
7538 abort = true;
7539 }
7540 } else {
7541 wtoken.startingView = view;
7542 }
7543 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7544 "Added starting " + wtoken
7545 + ": startingWindow="
7546 + wtoken.startingWindow + " startingView="
7547 + wtoken.startingView);
7548 }
7549
7550 if (abort) {
7551 try {
7552 mPolicy.removeStartingWindow(wtoken.token, view);
7553 } catch (Exception e) {
7554 Log.w(TAG, "Exception when removing starting window", e);
7555 }
7556 }
7557 }
7558 } break;
7559
7560 case REMOVE_STARTING: {
7561 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7562 IBinder token = null;
7563 View view = null;
7564 synchronized (mWindowMap) {
7565 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7566 + wtoken + ": startingWindow="
7567 + wtoken.startingWindow + " startingView="
7568 + wtoken.startingView);
7569 if (wtoken.startingWindow != null) {
7570 view = wtoken.startingView;
7571 token = wtoken.token;
7572 wtoken.startingData = null;
7573 wtoken.startingView = null;
7574 wtoken.startingWindow = null;
7575 }
7576 }
7577 if (view != null) {
7578 try {
7579 mPolicy.removeStartingWindow(token, view);
7580 } catch (Exception e) {
7581 Log.w(TAG, "Exception when removing starting window", e);
7582 }
7583 }
7584 } break;
7585
7586 case FINISHED_STARTING: {
7587 IBinder token = null;
7588 View view = null;
7589 while (true) {
7590 synchronized (mWindowMap) {
7591 final int N = mFinishedStarting.size();
7592 if (N <= 0) {
7593 break;
7594 }
7595 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7596
7597 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7598 "Finished starting " + wtoken
7599 + ": startingWindow=" + wtoken.startingWindow
7600 + " startingView=" + wtoken.startingView);
7601
7602 if (wtoken.startingWindow == null) {
7603 continue;
7604 }
7605
7606 view = wtoken.startingView;
7607 token = wtoken.token;
7608 wtoken.startingData = null;
7609 wtoken.startingView = null;
7610 wtoken.startingWindow = null;
7611 }
7612
7613 try {
7614 mPolicy.removeStartingWindow(token, view);
7615 } catch (Exception e) {
7616 Log.w(TAG, "Exception when removing starting window", e);
7617 }
7618 }
7619 } break;
7620
7621 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7622 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7623
7624 boolean nowVisible = msg.arg1 != 0;
7625 boolean nowGone = msg.arg2 != 0;
7626
7627 try {
7628 if (DEBUG_VISIBILITY) Log.v(
7629 TAG, "Reporting visible in " + wtoken
7630 + " visible=" + nowVisible
7631 + " gone=" + nowGone);
7632 if (nowVisible) {
7633 wtoken.appToken.windowsVisible();
7634 } else {
7635 wtoken.appToken.windowsGone();
7636 }
7637 } catch (RemoteException ex) {
7638 }
7639 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007641 case WINDOW_FREEZE_TIMEOUT: {
7642 synchronized (mWindowMap) {
7643 Log.w(TAG, "Window freeze timeout expired.");
7644 int i = mWindows.size();
7645 while (i > 0) {
7646 i--;
7647 WindowState w = (WindowState)mWindows.get(i);
7648 if (w.mOrientationChanging) {
7649 w.mOrientationChanging = false;
7650 Log.w(TAG, "Force clearing orientation change: " + w);
7651 }
7652 }
7653 performLayoutAndPlaceSurfacesLocked();
7654 }
7655 break;
7656 }
Romain Guy06882f82009-06-10 13:36:04 -07007657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007658 case HOLD_SCREEN_CHANGED: {
7659 Session oldHold;
7660 Session newHold;
7661 synchronized (mWindowMap) {
7662 oldHold = mLastReportedHold;
7663 newHold = (Session)msg.obj;
7664 mLastReportedHold = newHold;
7665 }
Romain Guy06882f82009-06-10 13:36:04 -07007666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 if (oldHold != newHold) {
7668 try {
7669 if (oldHold != null) {
7670 mBatteryStats.noteStopWakelock(oldHold.mUid,
7671 "window",
7672 BatteryStats.WAKE_TYPE_WINDOW);
7673 }
7674 if (newHold != null) {
7675 mBatteryStats.noteStartWakelock(newHold.mUid,
7676 "window",
7677 BatteryStats.WAKE_TYPE_WINDOW);
7678 }
7679 } catch (RemoteException e) {
7680 }
7681 }
7682 break;
7683 }
Romain Guy06882f82009-06-10 13:36:04 -07007684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007685 case APP_TRANSITION_TIMEOUT: {
7686 synchronized (mWindowMap) {
7687 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7688 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7689 "*** APP TRANSITION TIMEOUT");
7690 mAppTransitionReady = true;
7691 mAppTransitionTimeout = true;
7692 performLayoutAndPlaceSurfacesLocked();
7693 }
7694 }
7695 break;
7696 }
Romain Guy06882f82009-06-10 13:36:04 -07007697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007698 case PERSIST_ANIMATION_SCALE: {
7699 Settings.System.putFloat(mContext.getContentResolver(),
7700 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7701 Settings.System.putFloat(mContext.getContentResolver(),
7702 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7703 break;
7704 }
Romain Guy06882f82009-06-10 13:36:04 -07007705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007706 case FORCE_GC: {
7707 synchronized(mWindowMap) {
7708 if (mAnimationPending) {
7709 // If we are animating, don't do the gc now but
7710 // delay a bit so we don't interrupt the animation.
7711 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7712 2000);
7713 return;
7714 }
7715 // If we are currently rotating the display, it will
7716 // schedule a new message when done.
7717 if (mDisplayFrozen) {
7718 return;
7719 }
7720 mFreezeGcPending = 0;
7721 }
7722 Runtime.getRuntime().gc();
7723 break;
7724 }
Romain Guy06882f82009-06-10 13:36:04 -07007725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007726 case ENABLE_SCREEN: {
7727 performEnableScreen();
7728 break;
7729 }
Romain Guy06882f82009-06-10 13:36:04 -07007730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007731 case APP_FREEZE_TIMEOUT: {
7732 synchronized (mWindowMap) {
7733 Log.w(TAG, "App freeze timeout expired.");
7734 int i = mAppTokens.size();
7735 while (i > 0) {
7736 i--;
7737 AppWindowToken tok = mAppTokens.get(i);
7738 if (tok.freezingScreen) {
7739 Log.w(TAG, "Force clearing freeze: " + tok);
7740 unsetAppFreezingScreenLocked(tok, true, true);
7741 }
7742 }
7743 }
7744 break;
7745 }
Romain Guy06882f82009-06-10 13:36:04 -07007746
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007747 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07007748 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007749 sendNewConfiguration();
7750 }
7751 break;
7752 }
Romain Guy06882f82009-06-10 13:36:04 -07007753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 }
7755 }
7756 }
7757
7758 // -------------------------------------------------------------
7759 // IWindowManager API
7760 // -------------------------------------------------------------
7761
7762 public IWindowSession openSession(IInputMethodClient client,
7763 IInputContext inputContext) {
7764 if (client == null) throw new IllegalArgumentException("null client");
7765 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7766 return new Session(client, inputContext);
7767 }
7768
7769 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7770 synchronized (mWindowMap) {
7771 // The focus for the client is the window immediately below
7772 // where we would place the input method window.
7773 int idx = findDesiredInputMethodWindowIndexLocked(false);
7774 WindowState imFocus;
7775 if (idx > 0) {
7776 imFocus = (WindowState)mWindows.get(idx-1);
7777 if (imFocus != null) {
7778 if (imFocus.mSession.mClient != null &&
7779 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7780 return true;
7781 }
7782 }
7783 }
7784 }
7785 return false;
7786 }
Romain Guy06882f82009-06-10 13:36:04 -07007787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007788 // -------------------------------------------------------------
7789 // Internals
7790 // -------------------------------------------------------------
7791
7792 final WindowState windowForClientLocked(Session session, IWindow client) {
7793 return windowForClientLocked(session, client.asBinder());
7794 }
Romain Guy06882f82009-06-10 13:36:04 -07007795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007796 final WindowState windowForClientLocked(Session session, IBinder client) {
7797 WindowState win = mWindowMap.get(client);
7798 if (localLOGV) Log.v(
7799 TAG, "Looking up client " + client + ": " + win);
7800 if (win == null) {
7801 RuntimeException ex = new RuntimeException();
7802 Log.w(TAG, "Requested window " + client + " does not exist", ex);
7803 return null;
7804 }
7805 if (session != null && win.mSession != session) {
7806 RuntimeException ex = new RuntimeException();
7807 Log.w(TAG, "Requested window " + client + " is in session " +
7808 win.mSession + ", not " + session, ex);
7809 return null;
7810 }
7811
7812 return win;
7813 }
7814
7815 private final void assignLayersLocked() {
7816 int N = mWindows.size();
7817 int curBaseLayer = 0;
7818 int curLayer = 0;
7819 int i;
Romain Guy06882f82009-06-10 13:36:04 -07007820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007821 for (i=0; i<N; i++) {
7822 WindowState w = (WindowState)mWindows.get(i);
7823 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow) {
7824 curLayer += WINDOW_LAYER_MULTIPLIER;
7825 w.mLayer = curLayer;
7826 } else {
7827 curBaseLayer = curLayer = w.mBaseLayer;
7828 w.mLayer = curLayer;
7829 }
7830 if (w.mTargetAppToken != null) {
7831 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
7832 } else if (w.mAppToken != null) {
7833 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
7834 } else {
7835 w.mAnimLayer = w.mLayer;
7836 }
7837 if (w.mIsImWindow) {
7838 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
7839 }
7840 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
7841 + w.mAnimLayer);
7842 //System.out.println(
7843 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
7844 }
7845 }
7846
7847 private boolean mInLayout = false;
7848 private final void performLayoutAndPlaceSurfacesLocked() {
7849 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07007850 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007851 throw new RuntimeException("Recursive call!");
7852 }
7853 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
7854 return;
7855 }
7856
7857 boolean recoveringMemory = false;
7858 if (mForceRemoves != null) {
7859 recoveringMemory = true;
7860 // Wait a little it for things to settle down, and off we go.
7861 for (int i=0; i<mForceRemoves.size(); i++) {
7862 WindowState ws = mForceRemoves.get(i);
7863 Log.i(TAG, "Force removing: " + ws);
7864 removeWindowInnerLocked(ws.mSession, ws);
7865 }
7866 mForceRemoves = null;
7867 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
7868 Object tmp = new Object();
7869 synchronized (tmp) {
7870 try {
7871 tmp.wait(250);
7872 } catch (InterruptedException e) {
7873 }
7874 }
7875 }
Romain Guy06882f82009-06-10 13:36:04 -07007876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007877 mInLayout = true;
7878 try {
7879 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07007880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007881 int i = mPendingRemove.size()-1;
7882 if (i >= 0) {
7883 while (i >= 0) {
7884 WindowState w = mPendingRemove.get(i);
7885 removeWindowInnerLocked(w.mSession, w);
7886 i--;
7887 }
7888 mPendingRemove.clear();
7889
7890 mInLayout = false;
7891 assignLayersLocked();
7892 mLayoutNeeded = true;
7893 performLayoutAndPlaceSurfacesLocked();
7894
7895 } else {
7896 mInLayout = false;
7897 if (mLayoutNeeded) {
7898 requestAnimationLocked(0);
7899 }
7900 }
7901 } catch (RuntimeException e) {
7902 mInLayout = false;
7903 Log.e(TAG, "Unhandled exception while layout out windows", e);
7904 }
7905 }
7906
7907 private final void performLayoutLockedInner() {
7908 final int dw = mDisplay.getWidth();
7909 final int dh = mDisplay.getHeight();
7910
7911 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007912 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007913 int i;
7914
7915 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07007916
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007917 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 mPolicy.beginLayoutLw(dw, dh);
7919
7920 // First perform layout of any root windows (not attached
7921 // to another window).
7922 int topAttached = -1;
7923 for (i = N-1; i >= 0; i--) {
7924 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007925
7926 // Don't do layout of a window if it is not visible, or
7927 // soon won't be visible, to avoid wasting time and funky
7928 // changes while a window is animating away.
7929 final AppWindowToken atoken = win.mAppToken;
7930 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007932 || win.mRootToken.hidden
7933 || (atoken != null && atoken.hiddenRequested)
7934 || !win.mPolicyVisibility
7935 || win.mAttachedHidden
7936 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007937
7938 // If this view is GONE, then skip it -- keep the current
7939 // frame, and let the caller know so they can ignore it
7940 // if they want. (We do the normal layout for INVISIBLE
7941 // windows, since that means "perform layout as normal,
7942 // just don't display").
7943 if (!gone || !win.mHaveFrame) {
7944 if (!win.mLayoutAttached) {
7945 mPolicy.layoutWindowLw(win, win.mAttrs, null);
7946 } else {
7947 if (topAttached < 0) topAttached = i;
7948 }
7949 }
7950 }
Romain Guy06882f82009-06-10 13:36:04 -07007951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 // Now perform layout of attached windows, which usually
7953 // depend on the position of the window they are attached to.
7954 // XXX does not deal with windows that are attached to windows
7955 // that are themselves attached.
7956 for (i = topAttached; i >= 0; i--) {
7957 WindowState win = (WindowState) mWindows.get(i);
7958
7959 // If this view is GONE, then skip it -- keep the current
7960 // frame, and let the caller know so they can ignore it
7961 // if they want. (We do the normal layout for INVISIBLE
7962 // windows, since that means "perform layout as normal,
7963 // just don't display").
7964 if (win.mLayoutAttached) {
7965 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
7966 || !win.mHaveFrame) {
7967 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
7968 }
7969 }
7970 }
7971
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007972 if (!mPolicy.finishLayoutLw()) {
7973 mLayoutNeeded = false;
7974 } else if (repeats > 2) {
7975 Log.w(TAG, "Layout repeat aborted after too many iterations");
7976 mLayoutNeeded = false;
7977 } else {
7978 repeats++;
7979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007980 }
7981 }
Romain Guy06882f82009-06-10 13:36:04 -07007982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007983 private final void performLayoutAndPlaceSurfacesLockedInner(
7984 boolean recoveringMemory) {
7985 final long currentTime = SystemClock.uptimeMillis();
7986 final int dw = mDisplay.getWidth();
7987 final int dh = mDisplay.getHeight();
7988
7989 final int N = mWindows.size();
7990 int i;
7991
7992 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007993 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07007994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007995 if (mFxSession == null) {
7996 mFxSession = new SurfaceSession();
7997 }
Romain Guy06882f82009-06-10 13:36:04 -07007998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007999 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8000
8001 // Initialize state of exiting tokens.
8002 for (i=mExitingTokens.size()-1; i>=0; i--) {
8003 mExitingTokens.get(i).hasVisible = false;
8004 }
8005
8006 // Initialize state of exiting applications.
8007 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8008 mExitingAppTokens.get(i).hasVisible = false;
8009 }
8010
8011 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008012 boolean orientationChangeComplete = true;
8013 Session holdScreen = null;
8014 float screenBrightness = -1;
8015 boolean focusDisplayed = false;
8016 boolean animating = false;
8017
8018 Surface.openTransaction();
8019 try {
8020 boolean restart;
8021
8022 do {
8023 final int transactionSequence = ++mTransactionSequence;
8024
8025 // Update animations of all applications, including those
8026 // associated with exiting/removed apps
8027 boolean tokensAnimating = false;
8028 final int NAT = mAppTokens.size();
8029 for (i=0; i<NAT; i++) {
8030 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8031 tokensAnimating = true;
8032 }
8033 }
8034 final int NEAT = mExitingAppTokens.size();
8035 for (i=0; i<NEAT; i++) {
8036 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8037 tokensAnimating = true;
8038 }
8039 }
8040
8041 animating = tokensAnimating;
8042 restart = false;
8043
8044 boolean tokenMayBeDrawn = false;
8045
8046 mPolicy.beginAnimationLw(dw, dh);
8047
8048 for (i=N-1; i>=0; i--) {
8049 WindowState w = (WindowState)mWindows.get(i);
8050
8051 final WindowManager.LayoutParams attrs = w.mAttrs;
8052
8053 if (w.mSurface != null) {
8054 // Execute animation.
8055 w.commitFinishDrawingLocked(currentTime);
8056 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8057 animating = true;
8058 //w.dump(" ");
8059 }
8060
8061 mPolicy.animatingWindowLw(w, attrs);
8062 }
8063
8064 final AppWindowToken atoken = w.mAppToken;
8065 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8066 if (atoken.lastTransactionSequence != transactionSequence) {
8067 atoken.lastTransactionSequence = transactionSequence;
8068 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8069 atoken.startingDisplayed = false;
8070 }
8071 if ((w.isOnScreen() || w.mAttrs.type
8072 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8073 && !w.mExiting && !w.mDestroying) {
8074 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8075 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8076 + w.isDisplayedLw()
8077 + ", isAnimating=" + w.isAnimating());
8078 if (!w.isDisplayedLw()) {
8079 Log.v(TAG, "Not displayed: s=" + w.mSurface
8080 + " pv=" + w.mPolicyVisibility
8081 + " dp=" + w.mDrawPending
8082 + " cdp=" + w.mCommitDrawPending
8083 + " ah=" + w.mAttachedHidden
8084 + " th=" + atoken.hiddenRequested
8085 + " a=" + w.mAnimating);
8086 }
8087 }
8088 if (w != atoken.startingWindow) {
8089 if (!atoken.freezingScreen || !w.mAppFreezing) {
8090 atoken.numInterestingWindows++;
8091 if (w.isDisplayedLw()) {
8092 atoken.numDrawnWindows++;
8093 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8094 "tokenMayBeDrawn: " + atoken
8095 + " freezingScreen=" + atoken.freezingScreen
8096 + " mAppFreezing=" + w.mAppFreezing);
8097 tokenMayBeDrawn = true;
8098 }
8099 }
8100 } else if (w.isDisplayedLw()) {
8101 atoken.startingDisplayed = true;
8102 }
8103 }
8104 } else if (w.mReadyToShow) {
8105 w.performShowLocked();
8106 }
8107 }
8108
8109 if (mPolicy.finishAnimationLw()) {
8110 restart = true;
8111 }
8112
8113 if (tokenMayBeDrawn) {
8114 // See if any windows have been drawn, so they (and others
8115 // associated with them) can now be shown.
8116 final int NT = mTokenList.size();
8117 for (i=0; i<NT; i++) {
8118 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8119 if (wtoken == null) {
8120 continue;
8121 }
8122 if (wtoken.freezingScreen) {
8123 int numInteresting = wtoken.numInterestingWindows;
8124 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8125 if (DEBUG_VISIBILITY) Log.v(TAG,
8126 "allDrawn: " + wtoken
8127 + " interesting=" + numInteresting
8128 + " drawn=" + wtoken.numDrawnWindows);
8129 wtoken.showAllWindowsLocked();
8130 unsetAppFreezingScreenLocked(wtoken, false, true);
8131 orientationChangeComplete = true;
8132 }
8133 } else if (!wtoken.allDrawn) {
8134 int numInteresting = wtoken.numInterestingWindows;
8135 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8136 if (DEBUG_VISIBILITY) Log.v(TAG,
8137 "allDrawn: " + wtoken
8138 + " interesting=" + numInteresting
8139 + " drawn=" + wtoken.numDrawnWindows);
8140 wtoken.allDrawn = true;
8141 restart = true;
8142
8143 // We can now show all of the drawn windows!
8144 if (!mOpeningApps.contains(wtoken)) {
8145 wtoken.showAllWindowsLocked();
8146 }
8147 }
8148 }
8149 }
8150 }
8151
8152 // If we are ready to perform an app transition, check through
8153 // all of the app tokens to be shown and see if they are ready
8154 // to go.
8155 if (mAppTransitionReady) {
8156 int NN = mOpeningApps.size();
8157 boolean goodToGo = true;
8158 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8159 "Checking " + NN + " opening apps (frozen="
8160 + mDisplayFrozen + " timeout="
8161 + mAppTransitionTimeout + ")...");
8162 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8163 // If the display isn't frozen, wait to do anything until
8164 // all of the apps are ready. Otherwise just go because
8165 // we'll unfreeze the display when everyone is ready.
8166 for (i=0; i<NN && goodToGo; i++) {
8167 AppWindowToken wtoken = mOpeningApps.get(i);
8168 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8169 "Check opening app" + wtoken + ": allDrawn="
8170 + wtoken.allDrawn + " startingDisplayed="
8171 + wtoken.startingDisplayed);
8172 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8173 && !wtoken.startingMoved) {
8174 goodToGo = false;
8175 }
8176 }
8177 }
8178 if (goodToGo) {
8179 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8180 int transit = mNextAppTransition;
8181 if (mSkipAppTransitionAnimation) {
8182 transit = WindowManagerPolicy.TRANSIT_NONE;
8183 }
8184 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8185 mAppTransitionReady = false;
8186 mAppTransitionTimeout = false;
8187 mStartingIconInTransition = false;
8188 mSkipAppTransitionAnimation = false;
8189
8190 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8191
8192 // We need to figure out which animation to use...
8193 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8194 mOpeningApps, mClosingApps);
8195
8196 NN = mOpeningApps.size();
8197 for (i=0; i<NN; i++) {
8198 AppWindowToken wtoken = mOpeningApps.get(i);
8199 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8200 "Now opening app" + wtoken);
8201 wtoken.reportedVisible = false;
8202 wtoken.inPendingTransaction = false;
8203 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8204 wtoken.updateReportedVisibilityLocked();
8205 wtoken.showAllWindowsLocked();
8206 }
8207 NN = mClosingApps.size();
8208 for (i=0; i<NN; i++) {
8209 AppWindowToken wtoken = mClosingApps.get(i);
8210 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8211 "Now closing app" + wtoken);
8212 wtoken.inPendingTransaction = false;
8213 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8214 wtoken.updateReportedVisibilityLocked();
8215 // Force the allDrawn flag, because we want to start
8216 // this guy's animations regardless of whether it's
8217 // gotten drawn.
8218 wtoken.allDrawn = true;
8219 }
8220
8221 mOpeningApps.clear();
8222 mClosingApps.clear();
8223
8224 // This has changed the visibility of windows, so perform
8225 // a new layout to get them all up-to-date.
8226 mLayoutNeeded = true;
8227 moveInputMethodWindowsIfNeededLocked(true);
8228 performLayoutLockedInner();
8229 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8230
8231 restart = true;
8232 }
8233 }
8234 } while (restart);
8235
8236 // THIRD LOOP: Update the surfaces of all windows.
8237
8238 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8239
8240 boolean obscured = false;
8241 boolean blurring = false;
8242 boolean dimming = false;
8243 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008244 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008245 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008246
8247 for (i=N-1; i>=0; i--) {
8248 WindowState w = (WindowState)mWindows.get(i);
8249
8250 boolean displayed = false;
8251 final WindowManager.LayoutParams attrs = w.mAttrs;
8252 final int attrFlags = attrs.flags;
8253
8254 if (w.mSurface != null) {
8255 w.computeShownFrameLocked();
8256 if (localLOGV) Log.v(
8257 TAG, "Placing surface #" + i + " " + w.mSurface
8258 + ": new=" + w.mShownFrame + ", old="
8259 + w.mLastShownFrame);
8260
8261 boolean resize;
8262 int width, height;
8263 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8264 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8265 w.mLastRequestedHeight != w.mRequestedHeight;
8266 // for a scaled surface, we just want to use
8267 // the requested size.
8268 width = w.mRequestedWidth;
8269 height = w.mRequestedHeight;
8270 w.mLastRequestedWidth = width;
8271 w.mLastRequestedHeight = height;
8272 w.mLastShownFrame.set(w.mShownFrame);
8273 try {
8274 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8275 } catch (RuntimeException e) {
8276 Log.w(TAG, "Error positioning surface in " + w, e);
8277 if (!recoveringMemory) {
8278 reclaimSomeSurfaceMemoryLocked(w, "position");
8279 }
8280 }
8281 } else {
8282 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8283 width = w.mShownFrame.width();
8284 height = w.mShownFrame.height();
8285 w.mLastShownFrame.set(w.mShownFrame);
8286 if (resize) {
8287 if (SHOW_TRANSACTIONS) Log.i(
8288 TAG, " SURFACE " + w.mSurface + ": ("
8289 + w.mShownFrame.left + ","
8290 + w.mShownFrame.top + ") ("
8291 + w.mShownFrame.width() + "x"
8292 + w.mShownFrame.height() + ")");
8293 }
8294 }
8295
8296 if (resize) {
8297 if (width < 1) width = 1;
8298 if (height < 1) height = 1;
8299 if (w.mSurface != null) {
8300 try {
8301 w.mSurface.setSize(width, height);
8302 w.mSurface.setPosition(w.mShownFrame.left,
8303 w.mShownFrame.top);
8304 } catch (RuntimeException e) {
8305 // If something goes wrong with the surface (such
8306 // as running out of memory), don't take down the
8307 // entire system.
8308 Log.e(TAG, "Failure updating surface of " + w
8309 + "size=(" + width + "x" + height
8310 + "), pos=(" + w.mShownFrame.left
8311 + "," + w.mShownFrame.top + ")", e);
8312 if (!recoveringMemory) {
8313 reclaimSomeSurfaceMemoryLocked(w, "size");
8314 }
8315 }
8316 }
8317 }
8318 if (!w.mAppFreezing) {
8319 w.mContentInsetsChanged =
8320 !w.mLastContentInsets.equals(w.mContentInsets);
8321 w.mVisibleInsetsChanged =
8322 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008323 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008324 || w.mContentInsetsChanged
8325 || w.mVisibleInsetsChanged) {
8326 w.mLastFrame.set(w.mFrame);
8327 w.mLastContentInsets.set(w.mContentInsets);
8328 w.mLastVisibleInsets.set(w.mVisibleInsets);
8329 // If the orientation is changing, then we need to
8330 // hold off on unfreezing the display until this
8331 // window has been redrawn; to do that, we need
8332 // to go through the process of getting informed
8333 // by the application when it has finished drawing.
8334 if (w.mOrientationChanging) {
8335 if (DEBUG_ORIENTATION) Log.v(TAG,
8336 "Orientation start waiting for draw in "
8337 + w + ", surface " + w.mSurface);
8338 w.mDrawPending = true;
8339 w.mCommitDrawPending = false;
8340 w.mReadyToShow = false;
8341 if (w.mAppToken != null) {
8342 w.mAppToken.allDrawn = false;
8343 }
8344 }
Romain Guy06882f82009-06-10 13:36:04 -07008345 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008346 "Resizing window " + w + " to " + w.mFrame);
8347 mResizingWindows.add(w);
8348 } else if (w.mOrientationChanging) {
8349 if (!w.mDrawPending && !w.mCommitDrawPending) {
8350 if (DEBUG_ORIENTATION) Log.v(TAG,
8351 "Orientation not waiting for draw in "
8352 + w + ", surface " + w.mSurface);
8353 w.mOrientationChanging = false;
8354 }
8355 }
8356 }
8357
8358 if (w.mAttachedHidden) {
8359 if (!w.mLastHidden) {
8360 //dump();
8361 w.mLastHidden = true;
8362 if (SHOW_TRANSACTIONS) Log.i(
8363 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8364 if (w.mSurface != null) {
8365 try {
8366 w.mSurface.hide();
8367 } catch (RuntimeException e) {
8368 Log.w(TAG, "Exception hiding surface in " + w);
8369 }
8370 }
8371 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8372 }
8373 // If we are waiting for this window to handle an
8374 // orientation change, well, it is hidden, so
8375 // doesn't really matter. Note that this does
8376 // introduce a potential glitch if the window
8377 // becomes unhidden before it has drawn for the
8378 // new orientation.
8379 if (w.mOrientationChanging) {
8380 w.mOrientationChanging = false;
8381 if (DEBUG_ORIENTATION) Log.v(TAG,
8382 "Orientation change skips hidden " + w);
8383 }
8384 } else if (!w.isReadyForDisplay()) {
8385 if (!w.mLastHidden) {
8386 //dump();
8387 w.mLastHidden = true;
8388 if (SHOW_TRANSACTIONS) Log.i(
8389 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8390 if (w.mSurface != null) {
8391 try {
8392 w.mSurface.hide();
8393 } catch (RuntimeException e) {
8394 Log.w(TAG, "Exception exception hiding surface in " + w);
8395 }
8396 }
8397 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8398 }
8399 // If we are waiting for this window to handle an
8400 // orientation change, well, it is hidden, so
8401 // doesn't really matter. Note that this does
8402 // introduce a potential glitch if the window
8403 // becomes unhidden before it has drawn for the
8404 // new orientation.
8405 if (w.mOrientationChanging) {
8406 w.mOrientationChanging = false;
8407 if (DEBUG_ORIENTATION) Log.v(TAG,
8408 "Orientation change skips hidden " + w);
8409 }
8410 } else if (w.mLastLayer != w.mAnimLayer
8411 || w.mLastAlpha != w.mShownAlpha
8412 || w.mLastDsDx != w.mDsDx
8413 || w.mLastDtDx != w.mDtDx
8414 || w.mLastDsDy != w.mDsDy
8415 || w.mLastDtDy != w.mDtDy
8416 || w.mLastHScale != w.mHScale
8417 || w.mLastVScale != w.mVScale
8418 || w.mLastHidden) {
8419 displayed = true;
8420 w.mLastAlpha = w.mShownAlpha;
8421 w.mLastLayer = w.mAnimLayer;
8422 w.mLastDsDx = w.mDsDx;
8423 w.mLastDtDx = w.mDtDx;
8424 w.mLastDsDy = w.mDsDy;
8425 w.mLastDtDy = w.mDtDy;
8426 w.mLastHScale = w.mHScale;
8427 w.mLastVScale = w.mVScale;
8428 if (SHOW_TRANSACTIONS) Log.i(
8429 TAG, " SURFACE " + w.mSurface + ": alpha="
8430 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8431 if (w.mSurface != null) {
8432 try {
8433 w.mSurface.setAlpha(w.mShownAlpha);
8434 w.mSurface.setLayer(w.mAnimLayer);
8435 w.mSurface.setMatrix(
8436 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8437 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8438 } catch (RuntimeException e) {
8439 Log.w(TAG, "Error updating surface in " + w, e);
8440 if (!recoveringMemory) {
8441 reclaimSomeSurfaceMemoryLocked(w, "update");
8442 }
8443 }
8444 }
8445
8446 if (w.mLastHidden && !w.mDrawPending
8447 && !w.mCommitDrawPending
8448 && !w.mReadyToShow) {
8449 if (SHOW_TRANSACTIONS) Log.i(
8450 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8451 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8452 + " during relayout");
8453 if (showSurfaceRobustlyLocked(w)) {
8454 w.mHasDrawn = true;
8455 w.mLastHidden = false;
8456 } else {
8457 w.mOrientationChanging = false;
8458 }
8459 }
8460 if (w.mSurface != null) {
8461 w.mToken.hasVisible = true;
8462 }
8463 } else {
8464 displayed = true;
8465 }
8466
8467 if (displayed) {
8468 if (!covered) {
8469 if (attrs.width == LayoutParams.FILL_PARENT
8470 && attrs.height == LayoutParams.FILL_PARENT) {
8471 covered = true;
8472 }
8473 }
8474 if (w.mOrientationChanging) {
8475 if (w.mDrawPending || w.mCommitDrawPending) {
8476 orientationChangeComplete = false;
8477 if (DEBUG_ORIENTATION) Log.v(TAG,
8478 "Orientation continue waiting for draw in " + w);
8479 } else {
8480 w.mOrientationChanging = false;
8481 if (DEBUG_ORIENTATION) Log.v(TAG,
8482 "Orientation change complete in " + w);
8483 }
8484 }
8485 w.mToken.hasVisible = true;
8486 }
8487 } else if (w.mOrientationChanging) {
8488 if (DEBUG_ORIENTATION) Log.v(TAG,
8489 "Orientation change skips hidden " + w);
8490 w.mOrientationChanging = false;
8491 }
8492
8493 final boolean canBeSeen = w.isDisplayedLw();
8494
8495 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8496 focusDisplayed = true;
8497 }
8498
8499 // Update effect.
8500 if (!obscured) {
8501 if (w.mSurface != null) {
8502 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8503 holdScreen = w.mSession;
8504 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008505 if (!syswin && w.mAttrs.screenBrightness >= 0
8506 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008507 screenBrightness = w.mAttrs.screenBrightness;
8508 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008509 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8510 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8511 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8512 syswin = true;
8513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008514 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008515
8516 boolean opaqueDrawn = w.isOpaqueDrawn();
8517 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008518 // This window completely covers everything behind it,
8519 // so we want to leave all of them as unblurred (for
8520 // performance reasons).
8521 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008522 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8523 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008524 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008525 obscured = true;
8526 if (mBackgroundFillerSurface == null) {
8527 try {
8528 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8529 0, dw, dh,
8530 PixelFormat.OPAQUE,
8531 Surface.FX_SURFACE_NORMAL);
8532 } catch (Exception e) {
8533 Log.e(TAG, "Exception creating filler surface", e);
8534 }
8535 }
8536 try {
8537 mBackgroundFillerSurface.setPosition(0, 0);
8538 mBackgroundFillerSurface.setSize(dw, dh);
8539 // Using the same layer as Dim because they will never be shown at the
8540 // same time.
8541 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8542 mBackgroundFillerSurface.show();
8543 } catch (RuntimeException e) {
8544 Log.e(TAG, "Exception showing filler surface");
8545 }
8546 backgroundFillerShown = true;
8547 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 } else if (canBeSeen && !obscured &&
8549 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8550 if (localLOGV) Log.v(TAG, "Win " + w
8551 + ": blurring=" + blurring
8552 + " obscured=" + obscured
8553 + " displayed=" + displayed);
8554 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8555 if (!dimming) {
8556 //Log.i(TAG, "DIM BEHIND: " + w);
8557 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008558 if (mDimAnimator == null) {
8559 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008560 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008561 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008562 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008563 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008564 }
8565 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8566 if (!blurring) {
8567 //Log.i(TAG, "BLUR BEHIND: " + w);
8568 blurring = true;
8569 mBlurShown = true;
8570 if (mBlurSurface == null) {
8571 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8572 + mBlurSurface + ": CREATE");
8573 try {
Romain Guy06882f82009-06-10 13:36:04 -07008574 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008575 -1, 16, 16,
8576 PixelFormat.OPAQUE,
8577 Surface.FX_SURFACE_BLUR);
8578 } catch (Exception e) {
8579 Log.e(TAG, "Exception creating Blur surface", e);
8580 }
8581 }
8582 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8583 + mBlurSurface + ": SHOW pos=(0,0) (" +
8584 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8585 if (mBlurSurface != null) {
8586 mBlurSurface.setPosition(0, 0);
8587 mBlurSurface.setSize(dw, dh);
8588 try {
8589 mBlurSurface.show();
8590 } catch (RuntimeException e) {
8591 Log.w(TAG, "Failure showing blur surface", e);
8592 }
8593 }
8594 }
8595 mBlurSurface.setLayer(w.mAnimLayer-2);
8596 }
8597 }
8598 }
8599 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008600
8601 if (backgroundFillerShown == false && mBackgroundFillerShown) {
8602 mBackgroundFillerShown = false;
8603 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
8604 try {
8605 mBackgroundFillerSurface.hide();
8606 } catch (RuntimeException e) {
8607 Log.e(TAG, "Exception hiding filler surface", e);
8608 }
8609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008610
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008611 if (mDimAnimator != null && mDimAnimator.mDimShown) {
8612 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008613 }
Romain Guy06882f82009-06-10 13:36:04 -07008614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008615 if (!blurring && mBlurShown) {
8616 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8617 + ": HIDE");
8618 try {
8619 mBlurSurface.hide();
8620 } catch (IllegalArgumentException e) {
8621 Log.w(TAG, "Illegal argument exception hiding blur surface");
8622 }
8623 mBlurShown = false;
8624 }
8625
8626 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8627 } catch (RuntimeException e) {
8628 Log.e(TAG, "Unhandled exception in Window Manager", e);
8629 }
8630
8631 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008633 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8634 "With display frozen, orientationChangeComplete="
8635 + orientationChangeComplete);
8636 if (orientationChangeComplete) {
8637 if (mWindowsFreezingScreen) {
8638 mWindowsFreezingScreen = false;
8639 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8640 }
8641 if (mAppsFreezingScreen == 0) {
8642 stopFreezingDisplayLocked();
8643 }
8644 }
Romain Guy06882f82009-06-10 13:36:04 -07008645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008646 i = mResizingWindows.size();
8647 if (i > 0) {
8648 do {
8649 i--;
8650 WindowState win = mResizingWindows.get(i);
8651 try {
8652 win.mClient.resized(win.mFrame.width(),
8653 win.mFrame.height(), win.mLastContentInsets,
8654 win.mLastVisibleInsets, win.mDrawPending);
8655 win.mContentInsetsChanged = false;
8656 win.mVisibleInsetsChanged = false;
8657 } catch (RemoteException e) {
8658 win.mOrientationChanging = false;
8659 }
8660 } while (i > 0);
8661 mResizingWindows.clear();
8662 }
Romain Guy06882f82009-06-10 13:36:04 -07008663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 // Destroy the surface of any windows that are no longer visible.
8665 i = mDestroySurface.size();
8666 if (i > 0) {
8667 do {
8668 i--;
8669 WindowState win = mDestroySurface.get(i);
8670 win.mDestroying = false;
8671 if (mInputMethodWindow == win) {
8672 mInputMethodWindow = null;
8673 }
8674 win.destroySurfaceLocked();
8675 } while (i > 0);
8676 mDestroySurface.clear();
8677 }
8678
8679 // Time to remove any exiting tokens?
8680 for (i=mExitingTokens.size()-1; i>=0; i--) {
8681 WindowToken token = mExitingTokens.get(i);
8682 if (!token.hasVisible) {
8683 mExitingTokens.remove(i);
8684 }
8685 }
8686
8687 // Time to remove any exiting applications?
8688 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8689 AppWindowToken token = mExitingAppTokens.get(i);
8690 if (!token.hasVisible && !mClosingApps.contains(token)) {
8691 mAppTokens.remove(token);
8692 mExitingAppTokens.remove(i);
8693 }
8694 }
8695
8696 if (focusDisplayed) {
8697 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8698 }
8699 if (animating) {
8700 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8701 }
8702 mQueue.setHoldScreenLocked(holdScreen != null);
8703 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8704 mPowerManager.setScreenBrightnessOverride(-1);
8705 } else {
8706 mPowerManager.setScreenBrightnessOverride((int)
8707 (screenBrightness * Power.BRIGHTNESS_ON));
8708 }
8709 if (holdScreen != mHoldingScreenOn) {
8710 mHoldingScreenOn = holdScreen;
8711 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8712 mH.sendMessage(m);
8713 }
8714 }
8715
8716 void requestAnimationLocked(long delay) {
8717 if (!mAnimationPending) {
8718 mAnimationPending = true;
8719 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8720 }
8721 }
Romain Guy06882f82009-06-10 13:36:04 -07008722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008723 /**
8724 * Have the surface flinger show a surface, robustly dealing with
8725 * error conditions. In particular, if there is not enough memory
8726 * to show the surface, then we will try to get rid of other surfaces
8727 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07008728 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008729 * @return Returns true if the surface was successfully shown.
8730 */
8731 boolean showSurfaceRobustlyLocked(WindowState win) {
8732 try {
8733 if (win.mSurface != null) {
8734 win.mSurface.show();
8735 }
8736 return true;
8737 } catch (RuntimeException e) {
8738 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8739 }
Romain Guy06882f82009-06-10 13:36:04 -07008740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008741 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07008742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008743 return false;
8744 }
Romain Guy06882f82009-06-10 13:36:04 -07008745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008746 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8747 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07008748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008749 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8750 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07008751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008752 if (mForceRemoves == null) {
8753 mForceRemoves = new ArrayList<WindowState>();
8754 }
Romain Guy06882f82009-06-10 13:36:04 -07008755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008756 long callingIdentity = Binder.clearCallingIdentity();
8757 try {
8758 // There was some problem... first, do a sanity check of the
8759 // window list to make sure we haven't left any dangling surfaces
8760 // around.
8761 int N = mWindows.size();
8762 boolean leakedSurface = false;
8763 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8764 for (int i=0; i<N; i++) {
8765 WindowState ws = (WindowState)mWindows.get(i);
8766 if (ws.mSurface != null) {
8767 if (!mSessions.contains(ws.mSession)) {
8768 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8769 + ws + " surface=" + ws.mSurface
8770 + " token=" + win.mToken
8771 + " pid=" + ws.mSession.mPid
8772 + " uid=" + ws.mSession.mUid);
8773 ws.mSurface.clear();
8774 ws.mSurface = null;
8775 mForceRemoves.add(ws);
8776 i--;
8777 N--;
8778 leakedSurface = true;
8779 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
8780 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
8781 + ws + " surface=" + ws.mSurface
8782 + " token=" + win.mAppToken);
8783 ws.mSurface.clear();
8784 ws.mSurface = null;
8785 leakedSurface = true;
8786 }
8787 }
8788 }
Romain Guy06882f82009-06-10 13:36:04 -07008789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008790 boolean killedApps = false;
8791 if (!leakedSurface) {
8792 Log.w(TAG, "No leaked surfaces; killing applicatons!");
8793 SparseIntArray pidCandidates = new SparseIntArray();
8794 for (int i=0; i<N; i++) {
8795 WindowState ws = (WindowState)mWindows.get(i);
8796 if (ws.mSurface != null) {
8797 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
8798 }
8799 }
8800 if (pidCandidates.size() > 0) {
8801 int[] pids = new int[pidCandidates.size()];
8802 for (int i=0; i<pids.length; i++) {
8803 pids[i] = pidCandidates.keyAt(i);
8804 }
8805 try {
8806 if (mActivityManager.killPidsForMemory(pids)) {
8807 killedApps = true;
8808 }
8809 } catch (RemoteException e) {
8810 }
8811 }
8812 }
Romain Guy06882f82009-06-10 13:36:04 -07008813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 if (leakedSurface || killedApps) {
8815 // We managed to reclaim some memory, so get rid of the trouble
8816 // surface and ask the app to request another one.
8817 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
8818 if (surface != null) {
8819 surface.clear();
8820 win.mSurface = null;
8821 }
Romain Guy06882f82009-06-10 13:36:04 -07008822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008823 try {
8824 win.mClient.dispatchGetNewSurface();
8825 } catch (RemoteException e) {
8826 }
8827 }
8828 } finally {
8829 Binder.restoreCallingIdentity(callingIdentity);
8830 }
8831 }
Romain Guy06882f82009-06-10 13:36:04 -07008832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008833 private boolean updateFocusedWindowLocked(int mode) {
8834 WindowState newFocus = computeFocusedWindowLocked();
8835 if (mCurrentFocus != newFocus) {
8836 // This check makes sure that we don't already have the focus
8837 // change message pending.
8838 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
8839 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
8840 if (localLOGV) Log.v(
8841 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
8842 final WindowState oldFocus = mCurrentFocus;
8843 mCurrentFocus = newFocus;
8844 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07008845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008846 final WindowState imWindow = mInputMethodWindow;
8847 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008848 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008849 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008850 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
8851 mLayoutNeeded = true;
8852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
8854 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008855 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
8856 // Client will do the layout, but we need to assign layers
8857 // for handleNewWindowLocked() below.
8858 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008859 }
8860 }
Romain Guy06882f82009-06-10 13:36:04 -07008861
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008862 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
8863 mKeyWaiter.handleNewWindowLocked(newFocus);
8864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008865 return true;
8866 }
8867 return false;
8868 }
8869
8870 private WindowState computeFocusedWindowLocked() {
8871 WindowState result = null;
8872 WindowState win;
8873
8874 int i = mWindows.size() - 1;
8875 int nextAppIndex = mAppTokens.size()-1;
8876 WindowToken nextApp = nextAppIndex >= 0
8877 ? mAppTokens.get(nextAppIndex) : null;
8878
8879 while (i >= 0) {
8880 win = (WindowState)mWindows.get(i);
8881
8882 if (localLOGV || DEBUG_FOCUS) Log.v(
8883 TAG, "Looking for focus: " + i
8884 + " = " + win
8885 + ", flags=" + win.mAttrs.flags
8886 + ", canReceive=" + win.canReceiveKeys());
8887
8888 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07008889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008890 // If this window's application has been removed, just skip it.
8891 if (thisApp != null && thisApp.removed) {
8892 i--;
8893 continue;
8894 }
Romain Guy06882f82009-06-10 13:36:04 -07008895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008896 // If there is a focused app, don't allow focus to go to any
8897 // windows below it. If this is an application window, step
8898 // through the app tokens until we find its app.
8899 if (thisApp != null && nextApp != null && thisApp != nextApp
8900 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
8901 int origAppIndex = nextAppIndex;
8902 while (nextAppIndex > 0) {
8903 if (nextApp == mFocusedApp) {
8904 // Whoops, we are below the focused app... no focus
8905 // for you!
8906 if (localLOGV || DEBUG_FOCUS) Log.v(
8907 TAG, "Reached focused app: " + mFocusedApp);
8908 return null;
8909 }
8910 nextAppIndex--;
8911 nextApp = mAppTokens.get(nextAppIndex);
8912 if (nextApp == thisApp) {
8913 break;
8914 }
8915 }
8916 if (thisApp != nextApp) {
8917 // Uh oh, the app token doesn't exist! This shouldn't
8918 // happen, but if it does we can get totally hosed...
8919 // so restart at the original app.
8920 nextAppIndex = origAppIndex;
8921 nextApp = mAppTokens.get(nextAppIndex);
8922 }
8923 }
8924
8925 // Dispatch to this window if it is wants key events.
8926 if (win.canReceiveKeys()) {
8927 if (DEBUG_FOCUS) Log.v(
8928 TAG, "Found focus @ " + i + " = " + win);
8929 result = win;
8930 break;
8931 }
8932
8933 i--;
8934 }
8935
8936 return result;
8937 }
8938
8939 private void startFreezingDisplayLocked() {
8940 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07008941 // Freezing the display also suspends key event delivery, to
8942 // keep events from going astray while the display is reconfigured.
8943 // If someone has changed orientation again while the screen is
8944 // still frozen, the events will continue to be blocked while the
8945 // successive orientation change is processed. To prevent spurious
8946 // ANRs, we reset the event dispatch timeout in this case.
8947 synchronized (mKeyWaiter) {
8948 mKeyWaiter.mWasFrozen = true;
8949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008950 return;
8951 }
Romain Guy06882f82009-06-10 13:36:04 -07008952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008953 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07008954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008955 long now = SystemClock.uptimeMillis();
8956 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
8957 if (mFreezeGcPending != 0) {
8958 if (now > (mFreezeGcPending+1000)) {
8959 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
8960 mH.removeMessages(H.FORCE_GC);
8961 Runtime.getRuntime().gc();
8962 mFreezeGcPending = now;
8963 }
8964 } else {
8965 mFreezeGcPending = now;
8966 }
Romain Guy06882f82009-06-10 13:36:04 -07008967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008968 mDisplayFrozen = true;
8969 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8970 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8971 mAppTransitionReady = true;
8972 }
Romain Guy06882f82009-06-10 13:36:04 -07008973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008974 if (PROFILE_ORIENTATION) {
8975 File file = new File("/data/system/frozen");
8976 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
8977 }
8978 Surface.freezeDisplay(0);
8979 }
Romain Guy06882f82009-06-10 13:36:04 -07008980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008981 private void stopFreezingDisplayLocked() {
8982 if (!mDisplayFrozen) {
8983 return;
8984 }
Romain Guy06882f82009-06-10 13:36:04 -07008985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008986 mDisplayFrozen = false;
8987 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
8988 if (PROFILE_ORIENTATION) {
8989 Debug.stopMethodTracing();
8990 }
8991 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07008992
Chris Tate2ad63a92009-03-25 17:36:48 -07008993 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
8994 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008995 synchronized (mKeyWaiter) {
8996 mKeyWaiter.mWasFrozen = true;
8997 mKeyWaiter.notifyAll();
8998 }
8999
9000 // A little kludge: a lot could have happened while the
9001 // display was frozen, so now that we are coming back we
9002 // do a gc so that any remote references the system
9003 // processes holds on others can be released if they are
9004 // no longer needed.
9005 mH.removeMessages(H.FORCE_GC);
9006 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9007 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009009 mScreenFrozenLock.release();
9010 }
Romain Guy06882f82009-06-10 13:36:04 -07009011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009012 @Override
9013 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9014 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9015 != PackageManager.PERMISSION_GRANTED) {
9016 pw.println("Permission Denial: can't dump WindowManager from from pid="
9017 + Binder.getCallingPid()
9018 + ", uid=" + Binder.getCallingUid());
9019 return;
9020 }
Romain Guy06882f82009-06-10 13:36:04 -07009021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009022 synchronized(mWindowMap) {
9023 pw.println("Current Window Manager state:");
9024 for (int i=mWindows.size()-1; i>=0; i--) {
9025 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009026 pw.print(" Window #"); pw.print(i); pw.print(' ');
9027 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 w.dump(pw, " ");
9029 }
9030 if (mInputMethodDialogs.size() > 0) {
9031 pw.println(" ");
9032 pw.println(" Input method dialogs:");
9033 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9034 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009035 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009036 }
9037 }
9038 if (mPendingRemove.size() > 0) {
9039 pw.println(" ");
9040 pw.println(" Remove pending for:");
9041 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9042 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009043 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9044 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009045 w.dump(pw, " ");
9046 }
9047 }
9048 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9049 pw.println(" ");
9050 pw.println(" Windows force removing:");
9051 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9052 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009053 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9054 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009055 w.dump(pw, " ");
9056 }
9057 }
9058 if (mDestroySurface.size() > 0) {
9059 pw.println(" ");
9060 pw.println(" Windows waiting to destroy their surface:");
9061 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9062 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009063 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9064 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009065 w.dump(pw, " ");
9066 }
9067 }
9068 if (mLosingFocus.size() > 0) {
9069 pw.println(" ");
9070 pw.println(" Windows losing focus:");
9071 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9072 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009073 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9074 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009075 w.dump(pw, " ");
9076 }
9077 }
9078 if (mSessions.size() > 0) {
9079 pw.println(" ");
9080 pw.println(" All active sessions:");
9081 Iterator<Session> it = mSessions.iterator();
9082 while (it.hasNext()) {
9083 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009084 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009085 s.dump(pw, " ");
9086 }
9087 }
9088 if (mTokenMap.size() > 0) {
9089 pw.println(" ");
9090 pw.println(" All tokens:");
9091 Iterator<WindowToken> it = mTokenMap.values().iterator();
9092 while (it.hasNext()) {
9093 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009094 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 token.dump(pw, " ");
9096 }
9097 }
9098 if (mTokenList.size() > 0) {
9099 pw.println(" ");
9100 pw.println(" Window token list:");
9101 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009102 pw.print(" #"); pw.print(i); pw.print(": ");
9103 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009104 }
9105 }
9106 if (mAppTokens.size() > 0) {
9107 pw.println(" ");
9108 pw.println(" Application tokens in Z order:");
9109 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009110 pw.print(" App #"); pw.print(i); pw.print(": ");
9111 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009112 }
9113 }
9114 if (mFinishedStarting.size() > 0) {
9115 pw.println(" ");
9116 pw.println(" Finishing start of application tokens:");
9117 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9118 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009119 pw.print(" Finished Starting #"); pw.print(i);
9120 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009121 token.dump(pw, " ");
9122 }
9123 }
9124 if (mExitingTokens.size() > 0) {
9125 pw.println(" ");
9126 pw.println(" Exiting tokens:");
9127 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9128 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009129 pw.print(" Exiting #"); pw.print(i);
9130 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009131 token.dump(pw, " ");
9132 }
9133 }
9134 if (mExitingAppTokens.size() > 0) {
9135 pw.println(" ");
9136 pw.println(" Exiting application tokens:");
9137 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9138 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009139 pw.print(" Exiting App #"); pw.print(i);
9140 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009141 token.dump(pw, " ");
9142 }
9143 }
9144 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009145 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9146 pw.print(" mLastFocus="); pw.println(mLastFocus);
9147 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9148 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9149 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9150 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9151 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9152 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9153 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9154 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009155 if (mDimAnimator != null) {
9156 mDimAnimator.printTo(pw);
9157 } else {
9158 pw.print( " no DimAnimator ");
9159 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009160 pw.print(" mInputMethodAnimLayerAdjustment=");
9161 pw.println(mInputMethodAnimLayerAdjustment);
9162 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9163 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9164 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9165 pw.print(" mRotation="); pw.print(mRotation);
9166 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9167 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9168 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9169 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9170 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9171 pw.print(" mNextAppTransition=0x");
9172 pw.print(Integer.toHexString(mNextAppTransition));
9173 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9174 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9175 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9176 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9177 if (mOpeningApps.size() > 0) {
9178 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9179 }
9180 if (mClosingApps.size() > 0) {
9181 pw.print(" mClosingApps="); pw.println(mClosingApps);
9182 }
9183 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9184 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009185 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009186 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9187 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9188 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9189 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9190 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9191 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009192 }
9193 }
9194
9195 public void monitor() {
9196 synchronized (mWindowMap) { }
9197 synchronized (mKeyguardDisabled) { }
9198 synchronized (mKeyWaiter) { }
9199 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009200
9201 /**
9202 * DimAnimator class that controls the dim animation. This holds the surface and
9203 * all state used for dim animation.
9204 */
9205 private static class DimAnimator {
9206 Surface mDimSurface;
9207 boolean mDimShown = false;
9208 float mDimCurrentAlpha;
9209 float mDimTargetAlpha;
9210 float mDimDeltaPerMs;
9211 long mLastDimAnimTime;
9212
9213 DimAnimator (SurfaceSession session) {
9214 if (mDimSurface == null) {
9215 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9216 + mDimSurface + ": CREATE");
9217 try {
9218 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9219 Surface.FX_SURFACE_DIM);
9220 } catch (Exception e) {
9221 Log.e(TAG, "Exception creating Dim surface", e);
9222 }
9223 }
9224 }
9225
9226 /**
9227 * Show the dim surface.
9228 */
9229 void show(int dw, int dh) {
9230 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9231 dw + "x" + dh + ")");
9232 mDimShown = true;
9233 try {
9234 mDimSurface.setPosition(0, 0);
9235 mDimSurface.setSize(dw, dh);
9236 mDimSurface.show();
9237 } catch (RuntimeException e) {
9238 Log.w(TAG, "Failure showing dim surface", e);
9239 }
9240 }
9241
9242 /**
9243 * Set's the dim surface's layer and update dim parameters that will be used in
9244 * {@link updateSurface} after all windows are examined.
9245 */
9246 void updateParameters(WindowState w, long currentTime) {
9247 mDimSurface.setLayer(w.mAnimLayer-1);
9248
9249 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9250 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9251 if (mDimTargetAlpha != target) {
9252 // If the desired dim level has changed, then
9253 // start an animation to it.
9254 mLastDimAnimTime = currentTime;
9255 long duration = (w.mAnimating && w.mAnimation != null)
9256 ? w.mAnimation.computeDurationHint()
9257 : DEFAULT_DIM_DURATION;
9258 if (target > mDimTargetAlpha) {
9259 // This is happening behind the activity UI,
9260 // so we can make it run a little longer to
9261 // give a stronger impression without disrupting
9262 // the user.
9263 duration *= DIM_DURATION_MULTIPLIER;
9264 }
9265 if (duration < 1) {
9266 // Don't divide by zero
9267 duration = 1;
9268 }
9269 mDimTargetAlpha = target;
9270 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9271 }
9272 }
9273
9274 /**
9275 * Updating the surface's alpha. Returns true if the animation continues, or returns
9276 * false when the animation is finished and the dim surface is hidden.
9277 */
9278 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9279 if (!dimming) {
9280 if (mDimTargetAlpha != 0) {
9281 mLastDimAnimTime = currentTime;
9282 mDimTargetAlpha = 0;
9283 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9284 }
9285 }
9286
9287 boolean animating = false;
9288 if (mLastDimAnimTime != 0) {
9289 mDimCurrentAlpha += mDimDeltaPerMs
9290 * (currentTime-mLastDimAnimTime);
9291 boolean more = true;
9292 if (displayFrozen) {
9293 // If the display is frozen, there is no reason to animate.
9294 more = false;
9295 } else if (mDimDeltaPerMs > 0) {
9296 if (mDimCurrentAlpha > mDimTargetAlpha) {
9297 more = false;
9298 }
9299 } else if (mDimDeltaPerMs < 0) {
9300 if (mDimCurrentAlpha < mDimTargetAlpha) {
9301 more = false;
9302 }
9303 } else {
9304 more = false;
9305 }
9306
9307 // Do we need to continue animating?
9308 if (more) {
9309 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9310 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9311 mLastDimAnimTime = currentTime;
9312 mDimSurface.setAlpha(mDimCurrentAlpha);
9313 animating = true;
9314 } else {
9315 mDimCurrentAlpha = mDimTargetAlpha;
9316 mLastDimAnimTime = 0;
9317 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9318 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9319 mDimSurface.setAlpha(mDimCurrentAlpha);
9320 if (!dimming) {
9321 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9322 + ": HIDE");
9323 try {
9324 mDimSurface.hide();
9325 } catch (RuntimeException e) {
9326 Log.w(TAG, "Illegal argument exception hiding dim surface");
9327 }
9328 mDimShown = false;
9329 }
9330 }
9331 }
9332 return animating;
9333 }
9334
9335 public void printTo(PrintWriter pw) {
9336 pw.print(" mDimShown="); pw.print(mDimShown);
9337 pw.print(" current="); pw.print(mDimCurrentAlpha);
9338 pw.print(" target="); pw.print(mDimTargetAlpha);
9339 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9340 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9341 }
9342 }
9343
9344 /**
9345 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9346 * This is used for opening/closing transition for apps in compatible mode.
9347 */
9348 private static class FadeInOutAnimation extends Animation {
9349 int mWidth;
9350 boolean mFadeIn;
9351
9352 public FadeInOutAnimation(boolean fadeIn) {
9353 setInterpolator(new AccelerateInterpolator());
9354 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9355 mFadeIn = fadeIn;
9356 }
9357
9358 @Override
9359 protected void applyTransformation(float interpolatedTime, Transformation t) {
9360 float x = interpolatedTime;
9361 if (!mFadeIn) {
9362 x = 1.0f - x; // reverse the interpolation for fade out
9363 }
9364 if (x < 0.5) {
9365 // move the window out of the screen.
9366 t.getMatrix().setTranslate(mWidth, 0);
9367 } else {
9368 t.getMatrix().setTranslate(0, 0);// show
9369 t.setAlpha((x - 0.5f) * 2);
9370 }
9371 }
9372
9373 @Override
9374 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9375 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9376 mWidth = width;
9377 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009378
9379 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07009380 public int getZAdjustment() {
9381 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009382 }
9383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009384}