blob: 78ca8316b6534217803ba74243ad71aa4c87bf5f [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} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700128public class WindowManagerService extends IWindowManager.Stub
129 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 static final String TAG = "WindowManager";
131 static final boolean DEBUG = false;
132 static final boolean DEBUG_FOCUS = false;
133 static final boolean DEBUG_ANIM = false;
134 static final boolean DEBUG_LAYERS = false;
135 static final boolean DEBUG_INPUT = false;
136 static final boolean DEBUG_INPUT_METHOD = false;
137 static final boolean DEBUG_VISIBILITY = false;
138 static final boolean DEBUG_ORIENTATION = false;
139 static final boolean DEBUG_APP_TRANSITIONS = false;
140 static final boolean DEBUG_STARTING_WINDOW = false;
141 static final boolean DEBUG_REORDER = false;
142 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700143 static final boolean MEASURE_LATENCY = false;
144 static private LatencyTimer lt;
145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final boolean PROFILE_ORIENTATION = false;
147 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700148 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 /** How long to wait for first key repeat, in milliseconds */
153 static final int KEY_REPEAT_FIRST_DELAY = 750;
Romain Guy06882f82009-06-10 13:36:04 -0700154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 /** How long to wait for subsequent key repeats, in milliseconds */
156 static final int KEY_REPEAT_DELAY = 50;
157
158 /** How much to multiply the policy's type layer, to reserve room
159 * for multiple windows of the same type and Z-ordering adjustment
160 * with TYPE_LAYER_OFFSET. */
161 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
164 * or below others in the same layer. */
165 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 /** How much to increment the layer for each window, to reserve room
168 * for effect surfaces between them.
169 */
170 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 /** The maximum length we will accept for a loaded animation duration:
173 * this is 10 seconds.
174 */
175 static final int MAX_ANIMATION_DURATION = 10*1000;
176
177 /** Amount of time (in milliseconds) to animate the dim surface from one
178 * value to another, when no window animation is driving it.
179 */
180 static final int DEFAULT_DIM_DURATION = 200;
181
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700182 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
183 * compatible windows.
184 */
185 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 /** Adjustment to time to perform a dim, to make it more dramatic.
188 */
189 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700190
Dianne Hackborncfaef692009-06-15 14:24:44 -0700191 static final int INJECT_FAILED = 0;
192 static final int INJECT_SUCCEEDED = 1;
193 static final int INJECT_NO_PERMISSION = -1;
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 static final int UPDATE_FOCUS_NORMAL = 0;
196 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
197 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
198 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700199
Michael Chane96440f2009-05-06 10:27:36 -0700200 /** The minimum time between dispatching touch events. */
201 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
202
203 // Last touch event time
204 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700205
Michael Chane96440f2009-05-06 10:27:36 -0700206 // Last touch event type
207 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700208
Michael Chane96440f2009-05-06 10:27:36 -0700209 // Time to wait before calling useractivity again. This saves CPU usage
210 // when we get a flood of touch events.
211 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
212
213 // Last time we call user activity
214 long mLastUserActivityCallTime = 0;
215
Romain Guy06882f82009-06-10 13:36:04 -0700216 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700217 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700220 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
222 /**
223 * Condition waited on by {@link #reenableKeyguard} to know the call to
224 * the window policy has finished.
225 */
226 private boolean mWaitingUntilKeyguardReenabled = false;
227
228
229 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
230 new Handler(), "WindowManagerService.mKeyguardDisabled") {
231 public void acquired() {
232 mPolicy.enableKeyguard(false);
233 }
234 public void released() {
235 synchronized (mKeyguardDisabled) {
236 mPolicy.enableKeyguard(true);
237 mWaitingUntilKeyguardReenabled = false;
238 mKeyguardDisabled.notifyAll();
239 }
240 }
241 };
242
243 final Context mContext;
244
245 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
250
251 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * All currently active sessions with clients.
257 */
258 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * Mapping from an IWindow IBinder to the server's Window object.
262 * This is also used as the lock for all of our state.
263 */
264 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
265
266 /**
267 * Mapping from a token IBinder to a WindowToken object.
268 */
269 final HashMap<IBinder, WindowToken> mTokenMap =
270 new HashMap<IBinder, WindowToken>();
271
272 /**
273 * The same tokens as mTokenMap, stored in a list for efficient iteration
274 * over them.
275 */
276 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * Window tokens that are in the process of exiting, but still
280 * on screen for animations.
281 */
282 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
283
284 /**
285 * Z-ordered (bottom-most first) list of all application tokens, for
286 * controlling the ordering of windows in different applications. This
287 * contains WindowToken objects.
288 */
289 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
290
291 /**
292 * Application tokens that are in the process of exiting, but still
293 * on screen for animations.
294 */
295 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
296
297 /**
298 * List of window tokens that have finished starting their application,
299 * and now need to have the policy remove their windows.
300 */
301 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
302
303 /**
304 * Z-ordered (bottom-most first) list of all Window objects.
305 */
306 final ArrayList mWindows = new ArrayList();
307
308 /**
309 * Windows that are being resized. Used so we can tell the client about
310 * the resize after closing the transaction in which we resized the
311 * underlying surface.
312 */
313 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
314
315 /**
316 * Windows whose animations have ended and now must be removed.
317 */
318 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
319
320 /**
321 * Windows whose surface should be destroyed.
322 */
323 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
324
325 /**
326 * Windows that have lost input focus and are waiting for the new
327 * focus window to be displayed before they are told about this.
328 */
329 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
330
331 /**
332 * This is set when we have run out of memory, and will either be an empty
333 * list or contain windows that need to be force removed.
334 */
335 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700340 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 Surface mBlurSurface;
342 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 final float[] mTmpFloats = new float[9];
347
348 boolean mSafeMode;
349 boolean mDisplayEnabled = false;
350 boolean mSystemBooted = false;
351 int mRotation = 0;
352 int mRequestedRotation = 0;
353 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700354 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 ArrayList<IRotationWatcher> mRotationWatchers
356 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 boolean mLayoutNeeded = true;
359 boolean mAnimationPending = false;
360 boolean mDisplayFrozen = false;
361 boolean mWindowsFreezingScreen = false;
362 long mFreezeGcPending = 0;
363 int mAppsFreezingScreen = 0;
364
365 // This is held as long as we have the screen frozen, to give us time to
366 // perform a rotation animation when turning off shows the lock screen which
367 // changes the orientation.
368 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 // State management of app transitions. When we are preparing for a
371 // transition, mNextAppTransition will be the kind of transition to
372 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
373 // mOpeningApps and mClosingApps are the lists of tokens that will be
374 // made visible or hidden at the next transition.
375 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
376 boolean mAppTransitionReady = false;
377 boolean mAppTransitionTimeout = false;
378 boolean mStartingIconInTransition = false;
379 boolean mSkipAppTransitionAnimation = false;
380 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
381 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 //flag to detect fat touch events
384 boolean mFatTouch = false;
385 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 H mH = new H();
388
389 WindowState mCurrentFocus = null;
390 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 // This just indicates the window the input method is on top of, not
393 // necessarily the window its input is going to.
394 WindowState mInputMethodTarget = null;
395 WindowState mUpcomingInputMethodTarget = null;
396 boolean mInputMethodTargetWaitingAnim;
397 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 WindowState mInputMethodWindow = null;
400 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
401
402 AppWindowToken mFocusedApp = null;
403
404 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 float mWindowAnimationScale = 1.0f;
407 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 final KeyWaiter mKeyWaiter = new KeyWaiter();
410 final KeyQ mQueue;
411 final InputDispatcherThread mInputThread;
412
413 // Who is holding the screen on.
414 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 /**
417 * Whether the UI is currently running in touch mode (not showing
418 * navigational focus because the user is directly pressing the screen).
419 */
420 boolean mInTouchMode = false;
421
422 private ViewServer mViewServer;
423
424 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700425
Dianne Hackbornc485a602009-03-24 22:39:49 -0700426 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700427 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700428
429 // The frame use to limit the size of the app running in compatibility mode.
430 Rect mCompatibleScreenFrame = new Rect();
431 // The surface used to fill the outer rim of the app running in compatibility mode.
432 Surface mBackgroundFillerSurface = null;
433 boolean mBackgroundFillerShown = false;
434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 public static WindowManagerService main(Context context,
436 PowerManagerService pm, boolean haveInputMethods) {
437 WMThread thr = new WMThread(context, pm, haveInputMethods);
438 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 synchronized (thr) {
441 while (thr.mService == null) {
442 try {
443 thr.wait();
444 } catch (InterruptedException e) {
445 }
446 }
447 }
Romain Guy06882f82009-06-10 13:36:04 -0700448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 return thr.mService;
450 }
Romain Guy06882f82009-06-10 13:36:04 -0700451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 static class WMThread extends Thread {
453 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 private final Context mContext;
456 private final PowerManagerService mPM;
457 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 public WMThread(Context context, PowerManagerService pm,
460 boolean haveInputMethods) {
461 super("WindowManager");
462 mContext = context;
463 mPM = pm;
464 mHaveInputMethods = haveInputMethods;
465 }
Romain Guy06882f82009-06-10 13:36:04 -0700466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 public void run() {
468 Looper.prepare();
469 WindowManagerService s = new WindowManagerService(mContext, mPM,
470 mHaveInputMethods);
471 android.os.Process.setThreadPriority(
472 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 synchronized (this) {
475 mService = s;
476 notifyAll();
477 }
Romain Guy06882f82009-06-10 13:36:04 -0700478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 Looper.loop();
480 }
481 }
482
483 static class PolicyThread extends Thread {
484 private final WindowManagerPolicy mPolicy;
485 private final WindowManagerService mService;
486 private final Context mContext;
487 private final PowerManagerService mPM;
488 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public PolicyThread(WindowManagerPolicy policy,
491 WindowManagerService service, Context context,
492 PowerManagerService pm) {
493 super("WindowManagerPolicy");
494 mPolicy = policy;
495 mService = service;
496 mContext = context;
497 mPM = pm;
498 }
Romain Guy06882f82009-06-10 13:36:04 -0700499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 public void run() {
501 Looper.prepare();
502 //Looper.myLooper().setMessageLogging(new LogPrinter(
503 // Log.VERBOSE, "WindowManagerPolicy"));
504 android.os.Process.setThreadPriority(
505 android.os.Process.THREAD_PRIORITY_FOREGROUND);
506 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 synchronized (this) {
509 mRunning = true;
510 notifyAll();
511 }
Romain Guy06882f82009-06-10 13:36:04 -0700512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 Looper.loop();
514 }
515 }
516
517 private WindowManagerService(Context context, PowerManagerService pm,
518 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700519 if (MEASURE_LATENCY) {
520 lt = new LatencyTimer(100, 1000);
521 }
522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 mContext = context;
524 mHaveInputMethods = haveInputMethods;
525 mLimitedAlphaCompositing = context.getResources().getBoolean(
526 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 mPowerManager = pm;
529 mPowerManager.setPolicy(mPolicy);
530 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
531 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
532 "SCREEN_FROZEN");
533 mScreenFrozenLock.setReferenceCounted(false);
534
535 mActivityManager = ActivityManagerNative.getDefault();
536 mBatteryStats = BatteryStatsService.getService();
537
538 // Get persisted window scale setting
539 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
540 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
541 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
542 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 mQueue = new KeyQ();
545
546 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
549 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 synchronized (thr) {
552 while (!thr.mRunning) {
553 try {
554 thr.wait();
555 } catch (InterruptedException e) {
556 }
557 }
558 }
Romain Guy06882f82009-06-10 13:36:04 -0700559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 // Add ourself to the Watchdog monitors.
563 Watchdog.getInstance().addMonitor(this);
564 }
565
566 @Override
567 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
568 throws RemoteException {
569 try {
570 return super.onTransact(code, data, reply, flags);
571 } catch (RuntimeException e) {
572 // The window manager only throws security exceptions, so let's
573 // log all others.
574 if (!(e instanceof SecurityException)) {
575 Log.e(TAG, "Window Manager Crash", e);
576 }
577 throw e;
578 }
579 }
580
581 private void placeWindowAfter(Object pos, WindowState window) {
582 final int i = mWindows.indexOf(pos);
583 if (localLOGV || DEBUG_FOCUS) Log.v(
584 TAG, "Adding window " + window + " at "
585 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
586 mWindows.add(i+1, window);
587 }
588
589 private void placeWindowBefore(Object pos, WindowState window) {
590 final int i = mWindows.indexOf(pos);
591 if (localLOGV || DEBUG_FOCUS) Log.v(
592 TAG, "Adding window " + window + " at "
593 + i + " of " + mWindows.size() + " (before " + pos + ")");
594 mWindows.add(i, window);
595 }
596
597 //This method finds out the index of a window that has the same app token as
598 //win. used for z ordering the windows in mWindows
599 private int findIdxBasedOnAppTokens(WindowState win) {
600 //use a local variable to cache mWindows
601 ArrayList localmWindows = mWindows;
602 int jmax = localmWindows.size();
603 if(jmax == 0) {
604 return -1;
605 }
606 for(int j = (jmax-1); j >= 0; j--) {
607 WindowState wentry = (WindowState)localmWindows.get(j);
608 if(wentry.mAppToken == win.mAppToken) {
609 return j;
610 }
611 }
612 return -1;
613 }
Romain Guy06882f82009-06-10 13:36:04 -0700614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
616 final IWindow client = win.mClient;
617 final WindowToken token = win.mToken;
618 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 final int N = localmWindows.size();
621 final WindowState attached = win.mAttachedWindow;
622 int i;
623 if (attached == null) {
624 int tokenWindowsPos = token.windows.size();
625 if (token.appWindowToken != null) {
626 int index = tokenWindowsPos-1;
627 if (index >= 0) {
628 // If this application has existing windows, we
629 // simply place the new window on top of them... but
630 // keep the starting window on top.
631 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
632 // Base windows go behind everything else.
633 placeWindowBefore(token.windows.get(0), win);
634 tokenWindowsPos = 0;
635 } else {
636 AppWindowToken atoken = win.mAppToken;
637 if (atoken != null &&
638 token.windows.get(index) == atoken.startingWindow) {
639 placeWindowBefore(token.windows.get(index), win);
640 tokenWindowsPos--;
641 } else {
642 int newIdx = findIdxBasedOnAppTokens(win);
643 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700644 //there is a window above this one associated with the same
645 //apptoken note that the window could be a floating window
646 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 //windows associated with this token.
648 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
651 }
652 } else {
653 if (localLOGV) Log.v(
654 TAG, "Figuring out where to add app window "
655 + client.asBinder() + " (token=" + token + ")");
656 // Figure out where the window should go, based on the
657 // order of applications.
658 final int NA = mAppTokens.size();
659 Object pos = null;
660 for (i=NA-1; i>=0; i--) {
661 AppWindowToken t = mAppTokens.get(i);
662 if (t == token) {
663 i--;
664 break;
665 }
666 if (t.windows.size() > 0) {
667 pos = t.windows.get(0);
668 }
669 }
670 // We now know the index into the apps. If we found
671 // an app window above, that gives us the position; else
672 // we need to look some more.
673 if (pos != null) {
674 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700675 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 mTokenMap.get(((WindowState)pos).mClient.asBinder());
677 if (atoken != null) {
678 final int NC = atoken.windows.size();
679 if (NC > 0) {
680 WindowState bottom = atoken.windows.get(0);
681 if (bottom.mSubLayer < 0) {
682 pos = bottom;
683 }
684 }
685 }
686 placeWindowBefore(pos, win);
687 } else {
688 while (i >= 0) {
689 AppWindowToken t = mAppTokens.get(i);
690 final int NW = t.windows.size();
691 if (NW > 0) {
692 pos = t.windows.get(NW-1);
693 break;
694 }
695 i--;
696 }
697 if (pos != null) {
698 // Move in front of any windows attached to this
699 // one.
700 WindowToken atoken =
701 mTokenMap.get(((WindowState)pos).mClient.asBinder());
702 if (atoken != null) {
703 final int NC = atoken.windows.size();
704 if (NC > 0) {
705 WindowState top = atoken.windows.get(NC-1);
706 if (top.mSubLayer >= 0) {
707 pos = top;
708 }
709 }
710 }
711 placeWindowAfter(pos, win);
712 } else {
713 // Just search for the start of this layer.
714 final int myLayer = win.mBaseLayer;
715 for (i=0; i<N; i++) {
716 WindowState w = (WindowState)localmWindows.get(i);
717 if (w.mBaseLayer > myLayer) {
718 break;
719 }
720 }
721 if (localLOGV || DEBUG_FOCUS) Log.v(
722 TAG, "Adding window " + win + " at "
723 + i + " of " + N);
724 localmWindows.add(i, win);
725 }
726 }
727 }
728 } else {
729 // Figure out where window should go, based on layer.
730 final int myLayer = win.mBaseLayer;
731 for (i=N-1; i>=0; i--) {
732 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
733 i++;
734 break;
735 }
736 }
737 if (i < 0) i = 0;
738 if (localLOGV || DEBUG_FOCUS) Log.v(
739 TAG, "Adding window " + win + " at "
740 + i + " of " + N);
741 localmWindows.add(i, win);
742 }
743 if (addToToken) {
744 token.windows.add(tokenWindowsPos, win);
745 }
746
747 } else {
748 // Figure out this window's ordering relative to the window
749 // it is attached to.
750 final int NA = token.windows.size();
751 final int sublayer = win.mSubLayer;
752 int largestSublayer = Integer.MIN_VALUE;
753 WindowState windowWithLargestSublayer = null;
754 for (i=0; i<NA; i++) {
755 WindowState w = token.windows.get(i);
756 final int wSublayer = w.mSubLayer;
757 if (wSublayer >= largestSublayer) {
758 largestSublayer = wSublayer;
759 windowWithLargestSublayer = w;
760 }
761 if (sublayer < 0) {
762 // For negative sublayers, we go below all windows
763 // in the same sublayer.
764 if (wSublayer >= sublayer) {
765 if (addToToken) {
766 token.windows.add(i, win);
767 }
768 placeWindowBefore(
769 wSublayer >= 0 ? attached : w, win);
770 break;
771 }
772 } else {
773 // For positive sublayers, we go above all windows
774 // in the same sublayer.
775 if (wSublayer > sublayer) {
776 if (addToToken) {
777 token.windows.add(i, win);
778 }
779 placeWindowBefore(w, win);
780 break;
781 }
782 }
783 }
784 if (i >= NA) {
785 if (addToToken) {
786 token.windows.add(win);
787 }
788 if (sublayer < 0) {
789 placeWindowBefore(attached, win);
790 } else {
791 placeWindowAfter(largestSublayer >= 0
792 ? windowWithLargestSublayer
793 : attached,
794 win);
795 }
796 }
797 }
Romain Guy06882f82009-06-10 13:36:04 -0700798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 if (win.mAppToken != null && addToToken) {
800 win.mAppToken.allAppWindows.add(win);
801 }
802 }
Romain Guy06882f82009-06-10 13:36:04 -0700803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 static boolean canBeImeTarget(WindowState w) {
805 final int fl = w.mAttrs.flags
806 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
807 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
808 return w.isVisibleOrAdding();
809 }
810 return false;
811 }
Romain Guy06882f82009-06-10 13:36:04 -0700812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
814 final ArrayList localmWindows = mWindows;
815 final int N = localmWindows.size();
816 WindowState w = null;
817 int i = N;
818 while (i > 0) {
819 i--;
820 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
823 // + Integer.toHexString(w.mAttrs.flags));
824 if (canBeImeTarget(w)) {
825 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 // Yet more tricksyness! If this window is a "starting"
828 // window, we do actually want to be on top of it, but
829 // it is not -really- where input will go. So if the caller
830 // is not actually looking to move the IME, look down below
831 // for a real window to target...
832 if (!willMove
833 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
834 && i > 0) {
835 WindowState wb = (WindowState)localmWindows.get(i-1);
836 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
837 i--;
838 w = wb;
839 }
840 }
841 break;
842 }
843 }
Romain Guy06882f82009-06-10 13:36:04 -0700844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
848 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 if (willMove && w != null) {
851 final WindowState curTarget = mInputMethodTarget;
852 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 // Now some fun for dealing with window animations that
855 // modify the Z order. We need to look at all windows below
856 // the current target that are in this app, finding the highest
857 // visible one in layering.
858 AppWindowToken token = curTarget.mAppToken;
859 WindowState highestTarget = null;
860 int highestPos = 0;
861 if (token.animating || token.animation != null) {
862 int pos = 0;
863 pos = localmWindows.indexOf(curTarget);
864 while (pos >= 0) {
865 WindowState win = (WindowState)localmWindows.get(pos);
866 if (win.mAppToken != token) {
867 break;
868 }
869 if (!win.mRemoved) {
870 if (highestTarget == null || win.mAnimLayer >
871 highestTarget.mAnimLayer) {
872 highestTarget = win;
873 highestPos = pos;
874 }
875 }
876 pos--;
877 }
878 }
Romain Guy06882f82009-06-10 13:36:04 -0700879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700881 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 + mNextAppTransition + " " + highestTarget
883 + " animating=" + highestTarget.isAnimating()
884 + " layer=" + highestTarget.mAnimLayer
885 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
888 // If we are currently setting up for an animation,
889 // hold everything until we can find out what will happen.
890 mInputMethodTargetWaitingAnim = true;
891 mInputMethodTarget = highestTarget;
892 return highestPos + 1;
893 } else if (highestTarget.isAnimating() &&
894 highestTarget.mAnimLayer > w.mAnimLayer) {
895 // If the window we are currently targeting is involved
896 // with an animation, and it is on top of the next target
897 // we will be over, then hold off on moving until
898 // that is done.
899 mInputMethodTarget = highestTarget;
900 return highestPos + 1;
901 }
902 }
903 }
904 }
Romain Guy06882f82009-06-10 13:36:04 -0700905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 //Log.i(TAG, "Placing input method @" + (i+1));
907 if (w != null) {
908 if (willMove) {
909 RuntimeException e = new RuntimeException();
910 e.fillInStackTrace();
911 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
912 + mInputMethodTarget + " to " + w, e);
913 mInputMethodTarget = w;
914 if (w.mAppToken != null) {
915 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
916 } else {
917 setInputMethodAnimLayerAdjustment(0);
918 }
919 }
920 return i+1;
921 }
922 if (willMove) {
923 RuntimeException e = new RuntimeException();
924 e.fillInStackTrace();
925 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
926 + mInputMethodTarget + " to null", e);
927 mInputMethodTarget = null;
928 setInputMethodAnimLayerAdjustment(0);
929 }
930 return -1;
931 }
Romain Guy06882f82009-06-10 13:36:04 -0700932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 void addInputMethodWindowToListLocked(WindowState win) {
934 int pos = findDesiredInputMethodWindowIndexLocked(true);
935 if (pos >= 0) {
936 win.mTargetAppToken = mInputMethodTarget.mAppToken;
937 mWindows.add(pos, win);
938 moveInputMethodDialogsLocked(pos+1);
939 return;
940 }
941 win.mTargetAppToken = null;
942 addWindowToListInOrderLocked(win, true);
943 moveInputMethodDialogsLocked(pos);
944 }
Romain Guy06882f82009-06-10 13:36:04 -0700945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 void setInputMethodAnimLayerAdjustment(int adj) {
947 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
948 mInputMethodAnimLayerAdjustment = adj;
949 WindowState imw = mInputMethodWindow;
950 if (imw != null) {
951 imw.mAnimLayer = imw.mLayer + adj;
952 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
953 + " anim layer: " + imw.mAnimLayer);
954 int wi = imw.mChildWindows.size();
955 while (wi > 0) {
956 wi--;
957 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
958 cw.mAnimLayer = cw.mLayer + adj;
959 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
960 + " anim layer: " + cw.mAnimLayer);
961 }
962 }
963 int di = mInputMethodDialogs.size();
964 while (di > 0) {
965 di --;
966 imw = mInputMethodDialogs.get(di);
967 imw.mAnimLayer = imw.mLayer + adj;
968 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
969 + " anim layer: " + imw.mAnimLayer);
970 }
971 }
Romain Guy06882f82009-06-10 13:36:04 -0700972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
974 int wpos = mWindows.indexOf(win);
975 if (wpos >= 0) {
976 if (wpos < interestingPos) interestingPos--;
977 mWindows.remove(wpos);
978 int NC = win.mChildWindows.size();
979 while (NC > 0) {
980 NC--;
981 WindowState cw = (WindowState)win.mChildWindows.get(NC);
982 int cpos = mWindows.indexOf(cw);
983 if (cpos >= 0) {
984 if (cpos < interestingPos) interestingPos--;
985 mWindows.remove(cpos);
986 }
987 }
988 }
989 return interestingPos;
990 }
Romain Guy06882f82009-06-10 13:36:04 -0700991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 private void reAddWindowToListInOrderLocked(WindowState win) {
993 addWindowToListInOrderLocked(win, false);
994 // This is a hack to get all of the child windows added as well
995 // at the right position. Child windows should be rare and
996 // this case should be rare, so it shouldn't be that big a deal.
997 int wpos = mWindows.indexOf(win);
998 if (wpos >= 0) {
999 mWindows.remove(wpos);
1000 reAddWindowLocked(wpos, win);
1001 }
1002 }
Romain Guy06882f82009-06-10 13:36:04 -07001003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 void logWindowList(String prefix) {
1005 int N = mWindows.size();
1006 while (N > 0) {
1007 N--;
1008 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1009 }
1010 }
Romain Guy06882f82009-06-10 13:36:04 -07001011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 void moveInputMethodDialogsLocked(int pos) {
1013 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 final int N = dialogs.size();
1016 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1017 for (int i=0; i<N; i++) {
1018 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1019 }
1020 if (DEBUG_INPUT_METHOD) {
1021 Log.v(TAG, "Window list w/pos=" + pos);
1022 logWindowList(" ");
1023 }
Romain Guy06882f82009-06-10 13:36:04 -07001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 if (pos >= 0) {
1026 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1027 if (pos < mWindows.size()) {
1028 WindowState wp = (WindowState)mWindows.get(pos);
1029 if (wp == mInputMethodWindow) {
1030 pos++;
1031 }
1032 }
1033 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1034 for (int i=0; i<N; i++) {
1035 WindowState win = dialogs.get(i);
1036 win.mTargetAppToken = targetAppToken;
1037 pos = reAddWindowLocked(pos, win);
1038 }
1039 if (DEBUG_INPUT_METHOD) {
1040 Log.v(TAG, "Final window list:");
1041 logWindowList(" ");
1042 }
1043 return;
1044 }
1045 for (int i=0; i<N; i++) {
1046 WindowState win = dialogs.get(i);
1047 win.mTargetAppToken = null;
1048 reAddWindowToListInOrderLocked(win);
1049 if (DEBUG_INPUT_METHOD) {
1050 Log.v(TAG, "No IM target, final list:");
1051 logWindowList(" ");
1052 }
1053 }
1054 }
Romain Guy06882f82009-06-10 13:36:04 -07001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1057 final WindowState imWin = mInputMethodWindow;
1058 final int DN = mInputMethodDialogs.size();
1059 if (imWin == null && DN == 0) {
1060 return false;
1061 }
Romain Guy06882f82009-06-10 13:36:04 -07001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1064 if (imPos >= 0) {
1065 // In this case, the input method windows are to be placed
1066 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 // First check to see if the input method windows are already
1069 // located here, and contiguous.
1070 final int N = mWindows.size();
1071 WindowState firstImWin = imPos < N
1072 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 // Figure out the actual input method window that should be
1075 // at the bottom of their stack.
1076 WindowState baseImWin = imWin != null
1077 ? imWin : mInputMethodDialogs.get(0);
1078 if (baseImWin.mChildWindows.size() > 0) {
1079 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1080 if (cw.mSubLayer < 0) baseImWin = cw;
1081 }
Romain Guy06882f82009-06-10 13:36:04 -07001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 if (firstImWin == baseImWin) {
1084 // The windows haven't moved... but are they still contiguous?
1085 // First find the top IM window.
1086 int pos = imPos+1;
1087 while (pos < N) {
1088 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1089 break;
1090 }
1091 pos++;
1092 }
1093 pos++;
1094 // Now there should be no more input method windows above.
1095 while (pos < N) {
1096 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1097 break;
1098 }
1099 pos++;
1100 }
1101 if (pos >= N) {
1102 // All is good!
1103 return false;
1104 }
1105 }
Romain Guy06882f82009-06-10 13:36:04 -07001106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 if (imWin != null) {
1108 if (DEBUG_INPUT_METHOD) {
1109 Log.v(TAG, "Moving IM from " + imPos);
1110 logWindowList(" ");
1111 }
1112 imPos = tmpRemoveWindowLocked(imPos, imWin);
1113 if (DEBUG_INPUT_METHOD) {
1114 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1115 logWindowList(" ");
1116 }
1117 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1118 reAddWindowLocked(imPos, imWin);
1119 if (DEBUG_INPUT_METHOD) {
1120 Log.v(TAG, "List after moving IM to " + imPos + ":");
1121 logWindowList(" ");
1122 }
1123 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1124 } else {
1125 moveInputMethodDialogsLocked(imPos);
1126 }
Romain Guy06882f82009-06-10 13:36:04 -07001127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 } else {
1129 // In this case, the input method windows go in a fixed layer,
1130 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 if (imWin != null) {
1133 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1134 tmpRemoveWindowLocked(0, imWin);
1135 imWin.mTargetAppToken = null;
1136 reAddWindowToListInOrderLocked(imWin);
1137 if (DEBUG_INPUT_METHOD) {
1138 Log.v(TAG, "List with no IM target:");
1139 logWindowList(" ");
1140 }
1141 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1142 } else {
1143 moveInputMethodDialogsLocked(-1);;
1144 }
Romain Guy06882f82009-06-10 13:36:04 -07001145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
Romain Guy06882f82009-06-10 13:36:04 -07001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 if (needAssignLayers) {
1149 assignLayersLocked();
1150 }
Romain Guy06882f82009-06-10 13:36:04 -07001151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 return true;
1153 }
Romain Guy06882f82009-06-10 13:36:04 -07001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 void adjustInputMethodDialogsLocked() {
1156 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1157 }
Romain Guy06882f82009-06-10 13:36:04 -07001158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 public int addWindow(Session session, IWindow client,
1160 WindowManager.LayoutParams attrs, int viewVisibility,
1161 Rect outContentInsets) {
1162 int res = mPolicy.checkAddPermission(attrs);
1163 if (res != WindowManagerImpl.ADD_OKAY) {
1164 return res;
1165 }
Romain Guy06882f82009-06-10 13:36:04 -07001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 boolean reportNewConfig = false;
1168 WindowState attachedWindow = null;
1169 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 synchronized(mWindowMap) {
1172 // Instantiating a Display requires talking with the simulator,
1173 // so don't do it until we know the system is mostly up and
1174 // running.
1175 if (mDisplay == null) {
1176 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1177 mDisplay = wm.getDefaultDisplay();
1178 mQueue.setDisplay(mDisplay);
1179 reportNewConfig = true;
1180 }
Romain Guy06882f82009-06-10 13:36:04 -07001181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 if (mWindowMap.containsKey(client.asBinder())) {
1183 Log.w(TAG, "Window " + client + " is already added");
1184 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1185 }
1186
1187 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001188 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 if (attachedWindow == null) {
1190 Log.w(TAG, "Attempted to add window with token that is not a window: "
1191 + attrs.token + ". Aborting.");
1192 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1193 }
1194 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1195 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1196 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1197 + attrs.token + ". Aborting.");
1198 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1199 }
1200 }
1201
1202 boolean addToken = false;
1203 WindowToken token = mTokenMap.get(attrs.token);
1204 if (token == null) {
1205 if (attrs.type >= FIRST_APPLICATION_WINDOW
1206 && attrs.type <= LAST_APPLICATION_WINDOW) {
1207 Log.w(TAG, "Attempted to add application window with unknown token "
1208 + attrs.token + ". Aborting.");
1209 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1210 }
1211 if (attrs.type == TYPE_INPUT_METHOD) {
1212 Log.w(TAG, "Attempted to add input method window with unknown token "
1213 + attrs.token + ". Aborting.");
1214 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1215 }
1216 token = new WindowToken(attrs.token, -1, false);
1217 addToken = true;
1218 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1219 && attrs.type <= LAST_APPLICATION_WINDOW) {
1220 AppWindowToken atoken = token.appWindowToken;
1221 if (atoken == null) {
1222 Log.w(TAG, "Attempted to add window with non-application token "
1223 + token + ". Aborting.");
1224 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1225 } else if (atoken.removed) {
1226 Log.w(TAG, "Attempted to add window with exiting application token "
1227 + token + ". Aborting.");
1228 return WindowManagerImpl.ADD_APP_EXITING;
1229 }
1230 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1231 // No need for this guy!
1232 if (localLOGV) Log.v(
1233 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1234 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1235 }
1236 } else if (attrs.type == TYPE_INPUT_METHOD) {
1237 if (token.windowType != TYPE_INPUT_METHOD) {
1238 Log.w(TAG, "Attempted to add input method window with bad token "
1239 + attrs.token + ". Aborting.");
1240 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1241 }
1242 }
1243
1244 win = new WindowState(session, client, token,
1245 attachedWindow, attrs, viewVisibility);
1246 if (win.mDeathRecipient == null) {
1247 // Client has apparently died, so there is no reason to
1248 // continue.
1249 Log.w(TAG, "Adding window client " + client.asBinder()
1250 + " that is dead, aborting.");
1251 return WindowManagerImpl.ADD_APP_EXITING;
1252 }
1253
1254 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 res = mPolicy.prepareAddWindowLw(win, attrs);
1257 if (res != WindowManagerImpl.ADD_OKAY) {
1258 return res;
1259 }
1260
1261 // From now on, no exceptions or errors allowed!
1262
1263 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 if (addToken) {
1268 mTokenMap.put(attrs.token, token);
1269 mTokenList.add(token);
1270 }
1271 win.attach();
1272 mWindowMap.put(client.asBinder(), win);
1273
1274 if (attrs.type == TYPE_APPLICATION_STARTING &&
1275 token.appWindowToken != null) {
1276 token.appWindowToken.startingWindow = win;
1277 }
1278
1279 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 if (attrs.type == TYPE_INPUT_METHOD) {
1282 mInputMethodWindow = win;
1283 addInputMethodWindowToListLocked(win);
1284 imMayMove = false;
1285 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1286 mInputMethodDialogs.add(win);
1287 addWindowToListInOrderLocked(win, true);
1288 adjustInputMethodDialogsLocked();
1289 imMayMove = false;
1290 } else {
1291 addWindowToListInOrderLocked(win, true);
1292 }
Romain Guy06882f82009-06-10 13:36:04 -07001293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 if (mInTouchMode) {
1299 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1300 }
1301 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1302 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1303 }
Romain Guy06882f82009-06-10 13:36:04 -07001304
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001305 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001307 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1308 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 imMayMove = false;
1310 }
1311 }
Romain Guy06882f82009-06-10 13:36:04 -07001312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001314 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
Romain Guy06882f82009-06-10 13:36:04 -07001316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 assignLayersLocked();
1318 // Don't do layout here, the window must call
1319 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 //dump();
1322
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001323 if (focusChanged) {
1324 if (mCurrentFocus != null) {
1325 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1326 }
1327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 if (localLOGV) Log.v(
1329 TAG, "New client " + client.asBinder()
1330 + ": window=" + win);
1331 }
1332
1333 // sendNewConfiguration() checks caller permissions so we must call it with
1334 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1335 // identity anyway, so it's safe to just clear & restore around this whole
1336 // block.
1337 final long origId = Binder.clearCallingIdentity();
1338 if (reportNewConfig) {
1339 sendNewConfiguration();
1340 } else {
1341 // Update Orientation after adding a window, only if the window needs to be
1342 // displayed right away
1343 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001344 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 sendNewConfiguration();
1346 }
1347 }
1348 }
1349 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 return res;
1352 }
Romain Guy06882f82009-06-10 13:36:04 -07001353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 public void removeWindow(Session session, IWindow client) {
1355 synchronized(mWindowMap) {
1356 WindowState win = windowForClientLocked(session, client);
1357 if (win == null) {
1358 return;
1359 }
1360 removeWindowLocked(session, win);
1361 }
1362 }
Romain Guy06882f82009-06-10 13:36:04 -07001363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 public void removeWindowLocked(Session session, WindowState win) {
1365
1366 if (localLOGV || DEBUG_FOCUS) Log.v(
1367 TAG, "Remove " + win + " client="
1368 + Integer.toHexString(System.identityHashCode(
1369 win.mClient.asBinder()))
1370 + ", surface=" + win.mSurface);
1371
1372 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 if (DEBUG_APP_TRANSITIONS) Log.v(
1375 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1376 + " mExiting=" + win.mExiting
1377 + " isAnimating=" + win.isAnimating()
1378 + " app-animation="
1379 + (win.mAppToken != null ? win.mAppToken.animation : null)
1380 + " inPendingTransaction="
1381 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1382 + " mDisplayFrozen=" + mDisplayFrozen);
1383 // Visibility of the removed window. Will be used later to update orientation later on.
1384 boolean wasVisible = false;
1385 // First, see if we need to run an animation. If we do, we have
1386 // to hold off on removing the window until the animation is done.
1387 // If the display is frozen, just remove immediately, since the
1388 // animation wouldn't be seen.
1389 if (win.mSurface != null && !mDisplayFrozen) {
1390 // If we are not currently running the exit animation, we
1391 // need to see about starting one.
1392 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1395 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1396 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1397 }
1398 // Try starting an animation.
1399 if (applyAnimationLocked(win, transit, false)) {
1400 win.mExiting = true;
1401 }
1402 }
1403 if (win.mExiting || win.isAnimating()) {
1404 // The exit animation is running... wait for it!
1405 //Log.i(TAG, "*** Running exit animation...");
1406 win.mExiting = true;
1407 win.mRemoveOnExit = true;
1408 mLayoutNeeded = true;
1409 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1410 performLayoutAndPlaceSurfacesLocked();
1411 if (win.mAppToken != null) {
1412 win.mAppToken.updateReportedVisibilityLocked();
1413 }
1414 //dump();
1415 Binder.restoreCallingIdentity(origId);
1416 return;
1417 }
1418 }
1419
1420 removeWindowInnerLocked(session, win);
1421 // Removing a visible window will effect the computed orientation
1422 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001423 if (wasVisible && computeForcedAppOrientationLocked()
1424 != mForcedAppOrientation) {
1425 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 }
1427 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1428 Binder.restoreCallingIdentity(origId);
1429 }
Romain Guy06882f82009-06-10 13:36:04 -07001430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 private void removeWindowInnerLocked(Session session, WindowState win) {
1432 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1433 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 if (mInputMethodTarget == win) {
1438 moveInputMethodWindowsIfNeededLocked(false);
1439 }
Romain Guy06882f82009-06-10 13:36:04 -07001440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 mPolicy.removeWindowLw(win);
1442 win.removeLocked();
1443
1444 mWindowMap.remove(win.mClient.asBinder());
1445 mWindows.remove(win);
1446
1447 if (mInputMethodWindow == win) {
1448 mInputMethodWindow = null;
1449 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1450 mInputMethodDialogs.remove(win);
1451 }
Romain Guy06882f82009-06-10 13:36:04 -07001452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 final WindowToken token = win.mToken;
1454 final AppWindowToken atoken = win.mAppToken;
1455 token.windows.remove(win);
1456 if (atoken != null) {
1457 atoken.allAppWindows.remove(win);
1458 }
1459 if (localLOGV) Log.v(
1460 TAG, "**** Removing window " + win + ": count="
1461 + token.windows.size());
1462 if (token.windows.size() == 0) {
1463 if (!token.explicit) {
1464 mTokenMap.remove(token.token);
1465 mTokenList.remove(token);
1466 } else if (atoken != null) {
1467 atoken.firstWindowDrawn = false;
1468 }
1469 }
1470
1471 if (atoken != null) {
1472 if (atoken.startingWindow == win) {
1473 atoken.startingWindow = null;
1474 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1475 // If this is the last window and we had requested a starting
1476 // transition window, well there is no point now.
1477 atoken.startingData = null;
1478 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1479 // If this is the last window except for a starting transition
1480 // window, we need to get rid of the starting transition.
1481 if (DEBUG_STARTING_WINDOW) {
1482 Log.v(TAG, "Schedule remove starting " + token
1483 + ": no more real windows");
1484 }
1485 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1486 mH.sendMessage(m);
1487 }
1488 }
Romain Guy06882f82009-06-10 13:36:04 -07001489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 if (!mInLayout) {
1491 assignLayersLocked();
1492 mLayoutNeeded = true;
1493 performLayoutAndPlaceSurfacesLocked();
1494 if (win.mAppToken != null) {
1495 win.mAppToken.updateReportedVisibilityLocked();
1496 }
1497 }
1498 }
1499
1500 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1501 long origId = Binder.clearCallingIdentity();
1502 try {
1503 synchronized (mWindowMap) {
1504 WindowState w = windowForClientLocked(session, client);
1505 if ((w != null) && (w.mSurface != null)) {
1506 Surface.openTransaction();
1507 try {
1508 w.mSurface.setTransparentRegionHint(region);
1509 } finally {
1510 Surface.closeTransaction();
1511 }
1512 }
1513 }
1514 } finally {
1515 Binder.restoreCallingIdentity(origId);
1516 }
1517 }
1518
1519 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001520 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 Rect visibleInsets) {
1522 long origId = Binder.clearCallingIdentity();
1523 try {
1524 synchronized (mWindowMap) {
1525 WindowState w = windowForClientLocked(session, client);
1526 if (w != null) {
1527 w.mGivenInsetsPending = false;
1528 w.mGivenContentInsets.set(contentInsets);
1529 w.mGivenVisibleInsets.set(visibleInsets);
1530 w.mTouchableInsets = touchableInsets;
1531 mLayoutNeeded = true;
1532 performLayoutAndPlaceSurfacesLocked();
1533 }
1534 }
1535 } finally {
1536 Binder.restoreCallingIdentity(origId);
1537 }
1538 }
Romain Guy06882f82009-06-10 13:36:04 -07001539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 public void getWindowDisplayFrame(Session session, IWindow client,
1541 Rect outDisplayFrame) {
1542 synchronized(mWindowMap) {
1543 WindowState win = windowForClientLocked(session, client);
1544 if (win == null) {
1545 outDisplayFrame.setEmpty();
1546 return;
1547 }
1548 outDisplayFrame.set(win.mDisplayFrame);
1549 }
1550 }
1551
1552 public int relayoutWindow(Session session, IWindow client,
1553 WindowManager.LayoutParams attrs, int requestedWidth,
1554 int requestedHeight, int viewVisibility, boolean insetsPending,
1555 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1556 Surface outSurface) {
1557 boolean displayed = false;
1558 boolean inTouchMode;
1559 Configuration newConfig = null;
1560 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 synchronized(mWindowMap) {
1563 WindowState win = windowForClientLocked(session, client);
1564 if (win == null) {
1565 return 0;
1566 }
1567 win.mRequestedWidth = requestedWidth;
1568 win.mRequestedHeight = requestedHeight;
1569
1570 if (attrs != null) {
1571 mPolicy.adjustWindowParamsLw(attrs);
1572 }
Romain Guy06882f82009-06-10 13:36:04 -07001573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 int attrChanges = 0;
1575 int flagChanges = 0;
1576 if (attrs != null) {
1577 flagChanges = win.mAttrs.flags ^= attrs.flags;
1578 attrChanges = win.mAttrs.copyFrom(attrs);
1579 }
1580
1581 if (localLOGV) Log.v(
1582 TAG, "Relayout given client " + client.asBinder()
1583 + " (" + win.mAttrs.getTitle() + ")");
1584
1585
1586 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1587 win.mAlpha = attrs.alpha;
1588 }
1589
1590 final boolean scaledWindow =
1591 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1592
1593 if (scaledWindow) {
1594 // requested{Width|Height} Surface's physical size
1595 // attrs.{width|height} Size on screen
1596 win.mHScale = (attrs.width != requestedWidth) ?
1597 (attrs.width / (float)requestedWidth) : 1.0f;
1598 win.mVScale = (attrs.height != requestedHeight) ?
1599 (attrs.height / (float)requestedHeight) : 1.0f;
1600 }
1601
1602 boolean imMayMove = (flagChanges&(
1603 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1604 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 boolean focusMayChange = win.mViewVisibility != viewVisibility
1607 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1608 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 win.mRelayoutCalled = true;
1611 final int oldVisibility = win.mViewVisibility;
1612 win.mViewVisibility = viewVisibility;
1613 if (viewVisibility == View.VISIBLE &&
1614 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1615 displayed = !win.isVisibleLw();
1616 if (win.mExiting) {
1617 win.mExiting = false;
1618 win.mAnimation = null;
1619 }
1620 if (win.mDestroying) {
1621 win.mDestroying = false;
1622 mDestroySurface.remove(win);
1623 }
1624 if (oldVisibility == View.GONE) {
1625 win.mEnterAnimationPending = true;
1626 }
1627 if (displayed && win.mSurface != null && !win.mDrawPending
1628 && !win.mCommitDrawPending && !mDisplayFrozen) {
1629 applyEnterAnimationLocked(win);
1630 }
1631 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1632 // To change the format, we need to re-build the surface.
1633 win.destroySurfaceLocked();
1634 displayed = true;
1635 }
1636 try {
1637 Surface surface = win.createSurfaceLocked();
1638 if (surface != null) {
1639 outSurface.copyFrom(surface);
1640 } else {
1641 outSurface.clear();
1642 }
1643 } catch (Exception e) {
1644 Log.w(TAG, "Exception thrown when creating surface for client "
1645 + client + " (" + win.mAttrs.getTitle() + ")",
1646 e);
1647 Binder.restoreCallingIdentity(origId);
1648 return 0;
1649 }
1650 if (displayed) {
1651 focusMayChange = true;
1652 }
1653 if (win.mAttrs.type == TYPE_INPUT_METHOD
1654 && mInputMethodWindow == null) {
1655 mInputMethodWindow = win;
1656 imMayMove = true;
1657 }
1658 } else {
1659 win.mEnterAnimationPending = false;
1660 if (win.mSurface != null) {
1661 // If we are not currently running the exit animation, we
1662 // need to see about starting one.
1663 if (!win.mExiting) {
1664 // Try starting an animation; if there isn't one, we
1665 // can destroy the surface right away.
1666 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1667 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1668 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1669 }
1670 if (win.isWinVisibleLw() &&
1671 applyAnimationLocked(win, transit, false)) {
1672 win.mExiting = true;
1673 mKeyWaiter.finishedKey(session, client, true,
1674 KeyWaiter.RETURN_NOTHING);
1675 } else if (win.isAnimating()) {
1676 // Currently in a hide animation... turn this into
1677 // an exit.
1678 win.mExiting = true;
1679 } else {
1680 if (mInputMethodWindow == win) {
1681 mInputMethodWindow = null;
1682 }
1683 win.destroySurfaceLocked();
1684 }
1685 }
1686 }
1687 outSurface.clear();
1688 }
1689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 if (focusMayChange) {
1691 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1692 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 imMayMove = false;
1694 }
1695 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1696 }
Romain Guy06882f82009-06-10 13:36:04 -07001697
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001698 // updateFocusedWindowLocked() already assigned layers so we only need to
1699 // reassign them at this point if the IM window state gets shuffled
1700 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 if (imMayMove) {
1703 if (moveInputMethodWindowsIfNeededLocked(false)) {
1704 assignLayers = true;
1705 }
1706 }
Romain Guy06882f82009-06-10 13:36:04 -07001707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 mLayoutNeeded = true;
1709 win.mGivenInsetsPending = insetsPending;
1710 if (assignLayers) {
1711 assignLayersLocked();
1712 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001713 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 performLayoutAndPlaceSurfacesLocked();
1715 if (win.mAppToken != null) {
1716 win.mAppToken.updateReportedVisibilityLocked();
1717 }
1718 outFrame.set(win.mFrame);
1719 outContentInsets.set(win.mContentInsets);
1720 outVisibleInsets.set(win.mVisibleInsets);
1721 if (localLOGV) Log.v(
1722 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001723 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 + ", requestedHeight=" + requestedHeight
1725 + ", viewVisibility=" + viewVisibility
1726 + "\nRelayout returning frame=" + outFrame
1727 + ", surface=" + outSurface);
1728
1729 if (localLOGV || DEBUG_FOCUS) Log.v(
1730 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1731
1732 inTouchMode = mInTouchMode;
1733 }
1734
1735 if (newConfig != null) {
1736 sendNewConfiguration();
1737 }
Romain Guy06882f82009-06-10 13:36:04 -07001738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1742 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1743 }
1744
1745 public void finishDrawingWindow(Session session, IWindow client) {
1746 final long origId = Binder.clearCallingIdentity();
1747 synchronized(mWindowMap) {
1748 WindowState win = windowForClientLocked(session, client);
1749 if (win != null && win.finishDrawingLocked()) {
1750 mLayoutNeeded = true;
1751 performLayoutAndPlaceSurfacesLocked();
1752 }
1753 }
1754 Binder.restoreCallingIdentity(origId);
1755 }
1756
1757 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1758 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1759 + (lp != null ? lp.packageName : null)
1760 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1761 if (lp != null && lp.windowAnimations != 0) {
1762 // If this is a system resource, don't try to load it from the
1763 // application resources. It is nice to avoid loading application
1764 // resources if we can.
1765 String packageName = lp.packageName != null ? lp.packageName : "android";
1766 int resId = lp.windowAnimations;
1767 if ((resId&0xFF000000) == 0x01000000) {
1768 packageName = "android";
1769 }
1770 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
1771 + packageName);
1772 return AttributeCache.instance().get(packageName, resId,
1773 com.android.internal.R.styleable.WindowAnimation);
1774 }
1775 return null;
1776 }
Romain Guy06882f82009-06-10 13:36:04 -07001777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 private void applyEnterAnimationLocked(WindowState win) {
1779 int transit = WindowManagerPolicy.TRANSIT_SHOW;
1780 if (win.mEnterAnimationPending) {
1781 win.mEnterAnimationPending = false;
1782 transit = WindowManagerPolicy.TRANSIT_ENTER;
1783 }
1784
1785 applyAnimationLocked(win, transit, true);
1786 }
1787
1788 private boolean applyAnimationLocked(WindowState win,
1789 int transit, boolean isEntrance) {
1790 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
1791 // If we are trying to apply an animation, but already running
1792 // an animation of the same type, then just leave that one alone.
1793 return true;
1794 }
Romain Guy06882f82009-06-10 13:36:04 -07001795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 // Only apply an animation if the display isn't frozen. If it is
1797 // frozen, there is no reason to animate and it can cause strange
1798 // artifacts when we unfreeze the display if some different animation
1799 // is running.
1800 if (!mDisplayFrozen) {
1801 int anim = mPolicy.selectAnimationLw(win, transit);
1802 int attr = -1;
1803 Animation a = null;
1804 if (anim != 0) {
1805 a = AnimationUtils.loadAnimation(mContext, anim);
1806 } else {
1807 switch (transit) {
1808 case WindowManagerPolicy.TRANSIT_ENTER:
1809 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1810 break;
1811 case WindowManagerPolicy.TRANSIT_EXIT:
1812 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1813 break;
1814 case WindowManagerPolicy.TRANSIT_SHOW:
1815 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1816 break;
1817 case WindowManagerPolicy.TRANSIT_HIDE:
1818 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1819 break;
1820 }
1821 if (attr >= 0) {
1822 a = loadAnimation(win.mAttrs, attr);
1823 }
1824 }
1825 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
1826 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1827 + " mAnimation=" + win.mAnimation
1828 + " isEntrance=" + isEntrance);
1829 if (a != null) {
1830 if (DEBUG_ANIM) {
1831 RuntimeException e = new RuntimeException();
1832 e.fillInStackTrace();
1833 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
1834 }
1835 win.setAnimation(a);
1836 win.mAnimationIsEntrance = isEntrance;
1837 }
1838 } else {
1839 win.clearAnimation();
1840 }
1841
1842 return win.mAnimation != null;
1843 }
1844
1845 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
1846 int anim = 0;
1847 Context context = mContext;
1848 if (animAttr >= 0) {
1849 AttributeCache.Entry ent = getCachedAnimations(lp);
1850 if (ent != null) {
1851 context = ent.context;
1852 anim = ent.array.getResourceId(animAttr, 0);
1853 }
1854 }
1855 if (anim != 0) {
1856 return AnimationUtils.loadAnimation(context, anim);
1857 }
1858 return null;
1859 }
Romain Guy06882f82009-06-10 13:36:04 -07001860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 private boolean applyAnimationLocked(AppWindowToken wtoken,
1862 WindowManager.LayoutParams lp, int transit, boolean enter) {
1863 // Only apply an animation if the display isn't frozen. If it is
1864 // frozen, there is no reason to animate and it can cause strange
1865 // artifacts when we unfreeze the display if some different animation
1866 // is running.
1867 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001868 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07001869 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001870 a = new FadeInOutAnimation(enter);
1871 if (DEBUG_ANIM) Log.v(TAG,
1872 "applying FadeInOutAnimation for a window in compatibility mode");
1873 } else {
1874 int animAttr = 0;
1875 switch (transit) {
1876 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
1877 animAttr = enter
1878 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
1879 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
1880 break;
1881 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
1882 animAttr = enter
1883 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
1884 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
1885 break;
1886 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
1887 animAttr = enter
1888 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
1889 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
1890 break;
1891 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
1892 animAttr = enter
1893 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
1894 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
1895 break;
1896 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
1897 animAttr = enter
1898 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
1899 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
1900 break;
1901 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
1902 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07001903 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001904 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
1905 break;
1906 }
1907 a = loadAnimation(lp, animAttr);
1908 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
1909 + " anim=" + a
1910 + " animAttr=0x" + Integer.toHexString(animAttr)
1911 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 if (a != null) {
1914 if (DEBUG_ANIM) {
1915 RuntimeException e = new RuntimeException();
1916 e.fillInStackTrace();
1917 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
1918 }
1919 wtoken.setAnimation(a);
1920 }
1921 } else {
1922 wtoken.clearAnimation();
1923 }
1924
1925 return wtoken.animation != null;
1926 }
1927
1928 // -------------------------------------------------------------
1929 // Application Window Tokens
1930 // -------------------------------------------------------------
1931
1932 public void validateAppTokens(List tokens) {
1933 int v = tokens.size()-1;
1934 int m = mAppTokens.size()-1;
1935 while (v >= 0 && m >= 0) {
1936 AppWindowToken wtoken = mAppTokens.get(m);
1937 if (wtoken.removed) {
1938 m--;
1939 continue;
1940 }
1941 if (tokens.get(v) != wtoken.token) {
1942 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
1943 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
1944 }
1945 v--;
1946 m--;
1947 }
1948 while (v >= 0) {
1949 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
1950 v--;
1951 }
1952 while (m >= 0) {
1953 AppWindowToken wtoken = mAppTokens.get(m);
1954 if (!wtoken.removed) {
1955 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
1956 }
1957 m--;
1958 }
1959 }
1960
1961 boolean checkCallingPermission(String permission, String func) {
1962 // Quick check: if the calling permission is me, it's all okay.
1963 if (Binder.getCallingPid() == Process.myPid()) {
1964 return true;
1965 }
Romain Guy06882f82009-06-10 13:36:04 -07001966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 if (mContext.checkCallingPermission(permission)
1968 == PackageManager.PERMISSION_GRANTED) {
1969 return true;
1970 }
1971 String msg = "Permission Denial: " + func + " from pid="
1972 + Binder.getCallingPid()
1973 + ", uid=" + Binder.getCallingUid()
1974 + " requires " + permission;
1975 Log.w(TAG, msg);
1976 return false;
1977 }
Romain Guy06882f82009-06-10 13:36:04 -07001978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 AppWindowToken findAppWindowToken(IBinder token) {
1980 WindowToken wtoken = mTokenMap.get(token);
1981 if (wtoken == null) {
1982 return null;
1983 }
1984 return wtoken.appWindowToken;
1985 }
Romain Guy06882f82009-06-10 13:36:04 -07001986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 public void addWindowToken(IBinder token, int type) {
1988 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1989 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001990 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 }
Romain Guy06882f82009-06-10 13:36:04 -07001992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 synchronized(mWindowMap) {
1994 WindowToken wtoken = mTokenMap.get(token);
1995 if (wtoken != null) {
1996 Log.w(TAG, "Attempted to add existing input method token: " + token);
1997 return;
1998 }
1999 wtoken = new WindowToken(token, type, true);
2000 mTokenMap.put(token, wtoken);
2001 mTokenList.add(wtoken);
2002 }
2003 }
Romain Guy06882f82009-06-10 13:36:04 -07002004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 public void removeWindowToken(IBinder token) {
2006 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2007 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002008 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 }
2010
2011 final long origId = Binder.clearCallingIdentity();
2012 synchronized(mWindowMap) {
2013 WindowToken wtoken = mTokenMap.remove(token);
2014 mTokenList.remove(wtoken);
2015 if (wtoken != null) {
2016 boolean delayed = false;
2017 if (!wtoken.hidden) {
2018 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 final int N = wtoken.windows.size();
2021 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 for (int i=0; i<N; i++) {
2024 WindowState win = wtoken.windows.get(i);
2025
2026 if (win.isAnimating()) {
2027 delayed = true;
2028 }
Romain Guy06882f82009-06-10 13:36:04 -07002029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 if (win.isVisibleNow()) {
2031 applyAnimationLocked(win,
2032 WindowManagerPolicy.TRANSIT_EXIT, false);
2033 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2034 KeyWaiter.RETURN_NOTHING);
2035 changed = true;
2036 }
2037 }
2038
2039 if (changed) {
2040 mLayoutNeeded = true;
2041 performLayoutAndPlaceSurfacesLocked();
2042 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2043 }
Romain Guy06882f82009-06-10 13:36:04 -07002044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 if (delayed) {
2046 mExitingTokens.add(wtoken);
2047 }
2048 }
Romain Guy06882f82009-06-10 13:36:04 -07002049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 } else {
2051 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2052 }
2053 }
2054 Binder.restoreCallingIdentity(origId);
2055 }
2056
2057 public void addAppToken(int addPos, IApplicationToken token,
2058 int groupId, int requestedOrientation, boolean fullscreen) {
2059 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2060 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002061 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
Romain Guy06882f82009-06-10 13:36:04 -07002063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 synchronized(mWindowMap) {
2065 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2066 if (wtoken != null) {
2067 Log.w(TAG, "Attempted to add existing app token: " + token);
2068 return;
2069 }
2070 wtoken = new AppWindowToken(token);
2071 wtoken.groupId = groupId;
2072 wtoken.appFullscreen = fullscreen;
2073 wtoken.requestedOrientation = requestedOrientation;
2074 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002075 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 mTokenMap.put(token.asBinder(), wtoken);
2077 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 // Application tokens start out hidden.
2080 wtoken.hidden = true;
2081 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 //dump();
2084 }
2085 }
Romain Guy06882f82009-06-10 13:36:04 -07002086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 public void setAppGroupId(IBinder token, int groupId) {
2088 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2089 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002090 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
2092
2093 synchronized(mWindowMap) {
2094 AppWindowToken wtoken = findAppWindowToken(token);
2095 if (wtoken == null) {
2096 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2097 return;
2098 }
2099 wtoken.groupId = groupId;
2100 }
2101 }
Romain Guy06882f82009-06-10 13:36:04 -07002102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 public int getOrientationFromWindowsLocked() {
2104 int pos = mWindows.size() - 1;
2105 while (pos >= 0) {
2106 WindowState wtoken = (WindowState) mWindows.get(pos);
2107 pos--;
2108 if (wtoken.mAppToken != null) {
2109 // We hit an application window. so the orientation will be determined by the
2110 // app window. No point in continuing further.
2111 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2112 }
2113 if (!wtoken.isVisibleLw()) {
2114 continue;
2115 }
2116 int req = wtoken.mAttrs.screenOrientation;
2117 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2118 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2119 continue;
2120 } else {
2121 return req;
2122 }
2123 }
2124 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2125 }
Romain Guy06882f82009-06-10 13:36:04 -07002126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 public int getOrientationFromAppTokensLocked() {
2128 int pos = mAppTokens.size() - 1;
2129 int curGroup = 0;
2130 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002131 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002133 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 while (pos >= 0) {
2135 AppWindowToken wtoken = mAppTokens.get(pos);
2136 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002137 // if we're about to tear down this window and not seek for
2138 // the behind activity, don't use it for orientation
2139 if (!findingBehind
2140 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002141 continue;
2142 }
2143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 if (!haveGroup) {
2145 // We ignore any hidden applications on the top.
2146 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2147 continue;
2148 }
2149 haveGroup = true;
2150 curGroup = wtoken.groupId;
2151 lastOrientation = wtoken.requestedOrientation;
2152 } else if (curGroup != wtoken.groupId) {
2153 // If we have hit a new application group, and the bottom
2154 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002155 // the orientation behind it, and the last app was
2156 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002158 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2159 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 return lastOrientation;
2161 }
2162 }
2163 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002164 // If this application is fullscreen, and didn't explicitly say
2165 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002167 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002168 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002169 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 return or;
2171 }
2172 // If this application has requested an explicit orientation,
2173 // then use it.
2174 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2175 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2176 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2177 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2178 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2179 return or;
2180 }
Owen Lin3413b892009-05-01 17:12:32 -07002181 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 }
2183 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2184 }
Romain Guy06882f82009-06-10 13:36:04 -07002185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002187 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002188 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2189 "updateOrientationFromAppTokens()")) {
2190 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2191 }
2192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 Configuration config;
2194 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002195 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2196 freezeThisOneIfNeeded);
2197 Binder.restoreCallingIdentity(ident);
2198 return config;
2199 }
2200
2201 Configuration updateOrientationFromAppTokensUnchecked(
2202 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2203 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002205 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 }
2207 if (config != null) {
2208 mLayoutNeeded = true;
2209 performLayoutAndPlaceSurfacesLocked();
2210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 return config;
2212 }
Romain Guy06882f82009-06-10 13:36:04 -07002213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 /*
2215 * The orientation is computed from non-application windows first. If none of
2216 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002217 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2219 * android.os.IBinder)
2220 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002221 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002222 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 long ident = Binder.clearCallingIdentity();
2225 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002226 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 if (req != mForcedAppOrientation) {
2229 changed = true;
2230 mForcedAppOrientation = req;
2231 //send a message to Policy indicating orientation change to take
2232 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002233 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 }
Romain Guy06882f82009-06-10 13:36:04 -07002235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 if (changed) {
2237 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002238 WindowManagerPolicy.USE_LAST_ROTATION,
2239 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 if (changed) {
2241 if (freezeThisOneIfNeeded != null) {
2242 AppWindowToken wtoken = findAppWindowToken(
2243 freezeThisOneIfNeeded);
2244 if (wtoken != null) {
2245 startAppFreezingScreenLocked(wtoken,
2246 ActivityInfo.CONFIG_ORIENTATION);
2247 }
2248 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002249 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 }
2251 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002252
2253 // No obvious action we need to take, but if our current
2254 // state mismatches the activity maanager's, update it
2255 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002256 mTempConfiguration.setToDefaults();
2257 if (computeNewConfigurationLocked(mTempConfiguration)) {
2258 if (appConfig.diff(mTempConfiguration) != 0) {
2259 Log.i(TAG, "Config changed: " + mTempConfiguration);
2260 return new Configuration(mTempConfiguration);
2261 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002262 }
2263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 } finally {
2265 Binder.restoreCallingIdentity(ident);
2266 }
Romain Guy06882f82009-06-10 13:36:04 -07002267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 return null;
2269 }
Romain Guy06882f82009-06-10 13:36:04 -07002270
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002271 int computeForcedAppOrientationLocked() {
2272 int req = getOrientationFromWindowsLocked();
2273 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2274 req = getOrientationFromAppTokensLocked();
2275 }
2276 return req;
2277 }
Romain Guy06882f82009-06-10 13:36:04 -07002278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2280 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2281 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002282 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 }
Romain Guy06882f82009-06-10 13:36:04 -07002284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 synchronized(mWindowMap) {
2286 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2287 if (wtoken == null) {
2288 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2289 return;
2290 }
Romain Guy06882f82009-06-10 13:36:04 -07002291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 wtoken.requestedOrientation = requestedOrientation;
2293 }
2294 }
Romain Guy06882f82009-06-10 13:36:04 -07002295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 public int getAppOrientation(IApplicationToken token) {
2297 synchronized(mWindowMap) {
2298 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2299 if (wtoken == null) {
2300 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2301 }
Romain Guy06882f82009-06-10 13:36:04 -07002302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 return wtoken.requestedOrientation;
2304 }
2305 }
Romain Guy06882f82009-06-10 13:36:04 -07002306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2308 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2309 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002310 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 }
2312
2313 synchronized(mWindowMap) {
2314 boolean changed = false;
2315 if (token == null) {
2316 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2317 changed = mFocusedApp != null;
2318 mFocusedApp = null;
2319 mKeyWaiter.tickle();
2320 } else {
2321 AppWindowToken newFocus = findAppWindowToken(token);
2322 if (newFocus == null) {
2323 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2324 return;
2325 }
2326 changed = mFocusedApp != newFocus;
2327 mFocusedApp = newFocus;
2328 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2329 mKeyWaiter.tickle();
2330 }
2331
2332 if (moveFocusNow && changed) {
2333 final long origId = Binder.clearCallingIdentity();
2334 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2335 Binder.restoreCallingIdentity(origId);
2336 }
2337 }
2338 }
2339
2340 public void prepareAppTransition(int transit) {
2341 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2342 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002343 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 }
Romain Guy06882f82009-06-10 13:36:04 -07002345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 synchronized(mWindowMap) {
2347 if (DEBUG_APP_TRANSITIONS) Log.v(
2348 TAG, "Prepare app transition: transit=" + transit
2349 + " mNextAppTransition=" + mNextAppTransition);
2350 if (!mDisplayFrozen) {
2351 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2352 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002353 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2354 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2355 // Opening a new task always supersedes a close for the anim.
2356 mNextAppTransition = transit;
2357 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2358 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2359 // Opening a new activity always supersedes a close for the anim.
2360 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 }
2362 mAppTransitionReady = false;
2363 mAppTransitionTimeout = false;
2364 mStartingIconInTransition = false;
2365 mSkipAppTransitionAnimation = false;
2366 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2367 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2368 5000);
2369 }
2370 }
2371 }
2372
2373 public int getPendingAppTransition() {
2374 return mNextAppTransition;
2375 }
Romain Guy06882f82009-06-10 13:36:04 -07002376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 public void executeAppTransition() {
2378 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2379 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002380 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 }
Romain Guy06882f82009-06-10 13:36:04 -07002382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 synchronized(mWindowMap) {
2384 if (DEBUG_APP_TRANSITIONS) Log.v(
2385 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2386 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2387 mAppTransitionReady = true;
2388 final long origId = Binder.clearCallingIdentity();
2389 performLayoutAndPlaceSurfacesLocked();
2390 Binder.restoreCallingIdentity(origId);
2391 }
2392 }
2393 }
2394
2395 public void setAppStartingWindow(IBinder token, String pkg,
2396 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2397 IBinder transferFrom, boolean createIfNeeded) {
2398 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2399 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002400 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 }
2402
2403 synchronized(mWindowMap) {
2404 if (DEBUG_STARTING_WINDOW) Log.v(
2405 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2406 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 AppWindowToken wtoken = findAppWindowToken(token);
2409 if (wtoken == null) {
2410 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2411 return;
2412 }
2413
2414 // If the display is frozen, we won't do anything until the
2415 // actual window is displayed so there is no reason to put in
2416 // the starting window.
2417 if (mDisplayFrozen) {
2418 return;
2419 }
Romain Guy06882f82009-06-10 13:36:04 -07002420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 if (wtoken.startingData != null) {
2422 return;
2423 }
Romain Guy06882f82009-06-10 13:36:04 -07002424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 if (transferFrom != null) {
2426 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2427 if (ttoken != null) {
2428 WindowState startingWindow = ttoken.startingWindow;
2429 if (startingWindow != null) {
2430 if (mStartingIconInTransition) {
2431 // In this case, the starting icon has already
2432 // been displayed, so start letting windows get
2433 // shown immediately without any more transitions.
2434 mSkipAppTransitionAnimation = true;
2435 }
2436 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2437 "Moving existing starting from " + ttoken
2438 + " to " + wtoken);
2439 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 // Transfer the starting window over to the new
2442 // token.
2443 wtoken.startingData = ttoken.startingData;
2444 wtoken.startingView = ttoken.startingView;
2445 wtoken.startingWindow = startingWindow;
2446 ttoken.startingData = null;
2447 ttoken.startingView = null;
2448 ttoken.startingWindow = null;
2449 ttoken.startingMoved = true;
2450 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002451 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 startingWindow.mAppToken = wtoken;
2453 mWindows.remove(startingWindow);
2454 ttoken.windows.remove(startingWindow);
2455 ttoken.allAppWindows.remove(startingWindow);
2456 addWindowToListInOrderLocked(startingWindow, true);
2457 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 // Propagate other interesting state between the
2460 // tokens. If the old token is displayed, we should
2461 // immediately force the new one to be displayed. If
2462 // it is animating, we need to move that animation to
2463 // the new one.
2464 if (ttoken.allDrawn) {
2465 wtoken.allDrawn = true;
2466 }
2467 if (ttoken.firstWindowDrawn) {
2468 wtoken.firstWindowDrawn = true;
2469 }
2470 if (!ttoken.hidden) {
2471 wtoken.hidden = false;
2472 wtoken.hiddenRequested = false;
2473 wtoken.willBeHidden = false;
2474 }
2475 if (wtoken.clientHidden != ttoken.clientHidden) {
2476 wtoken.clientHidden = ttoken.clientHidden;
2477 wtoken.sendAppVisibilityToClients();
2478 }
2479 if (ttoken.animation != null) {
2480 wtoken.animation = ttoken.animation;
2481 wtoken.animating = ttoken.animating;
2482 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2483 ttoken.animation = null;
2484 ttoken.animLayerAdjustment = 0;
2485 wtoken.updateLayers();
2486 ttoken.updateLayers();
2487 }
Romain Guy06882f82009-06-10 13:36:04 -07002488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 mLayoutNeeded = true;
2491 performLayoutAndPlaceSurfacesLocked();
2492 Binder.restoreCallingIdentity(origId);
2493 return;
2494 } else if (ttoken.startingData != null) {
2495 // The previous app was getting ready to show a
2496 // starting window, but hasn't yet done so. Steal it!
2497 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2498 "Moving pending starting from " + ttoken
2499 + " to " + wtoken);
2500 wtoken.startingData = ttoken.startingData;
2501 ttoken.startingData = null;
2502 ttoken.startingMoved = true;
2503 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2504 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2505 // want to process the message ASAP, before any other queued
2506 // messages.
2507 mH.sendMessageAtFrontOfQueue(m);
2508 return;
2509 }
2510 }
2511 }
2512
2513 // There is no existing starting window, and the caller doesn't
2514 // want us to create one, so that's it!
2515 if (!createIfNeeded) {
2516 return;
2517 }
Romain Guy06882f82009-06-10 13:36:04 -07002518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 mStartingIconInTransition = true;
2520 wtoken.startingData = new StartingData(
2521 pkg, theme, nonLocalizedLabel,
2522 labelRes, icon);
2523 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2524 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2525 // want to process the message ASAP, before any other queued
2526 // messages.
2527 mH.sendMessageAtFrontOfQueue(m);
2528 }
2529 }
2530
2531 public void setAppWillBeHidden(IBinder token) {
2532 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2533 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002534 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 }
2536
2537 AppWindowToken wtoken;
2538
2539 synchronized(mWindowMap) {
2540 wtoken = findAppWindowToken(token);
2541 if (wtoken == null) {
2542 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2543 return;
2544 }
2545 wtoken.willBeHidden = true;
2546 }
2547 }
Romain Guy06882f82009-06-10 13:36:04 -07002548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2550 boolean visible, int transit, boolean performLayout) {
2551 boolean delayed = false;
2552
2553 if (wtoken.clientHidden == visible) {
2554 wtoken.clientHidden = !visible;
2555 wtoken.sendAppVisibilityToClients();
2556 }
Romain Guy06882f82009-06-10 13:36:04 -07002557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 wtoken.willBeHidden = false;
2559 if (wtoken.hidden == visible) {
2560 final int N = wtoken.allAppWindows.size();
2561 boolean changed = false;
2562 if (DEBUG_APP_TRANSITIONS) Log.v(
2563 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2564 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2569 if (wtoken.animation == sDummyAnimation) {
2570 wtoken.animation = null;
2571 }
2572 applyAnimationLocked(wtoken, lp, transit, visible);
2573 changed = true;
2574 if (wtoken.animation != null) {
2575 delayed = runningAppAnimation = true;
2576 }
2577 }
Romain Guy06882f82009-06-10 13:36:04 -07002578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 for (int i=0; i<N; i++) {
2580 WindowState win = wtoken.allAppWindows.get(i);
2581 if (win == wtoken.startingWindow) {
2582 continue;
2583 }
2584
2585 if (win.isAnimating()) {
2586 delayed = true;
2587 }
Romain Guy06882f82009-06-10 13:36:04 -07002588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2590 //win.dump(" ");
2591 if (visible) {
2592 if (!win.isVisibleNow()) {
2593 if (!runningAppAnimation) {
2594 applyAnimationLocked(win,
2595 WindowManagerPolicy.TRANSIT_ENTER, true);
2596 }
2597 changed = true;
2598 }
2599 } else if (win.isVisibleNow()) {
2600 if (!runningAppAnimation) {
2601 applyAnimationLocked(win,
2602 WindowManagerPolicy.TRANSIT_EXIT, false);
2603 }
2604 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2605 KeyWaiter.RETURN_NOTHING);
2606 changed = true;
2607 }
2608 }
2609
2610 wtoken.hidden = wtoken.hiddenRequested = !visible;
2611 if (!visible) {
2612 unsetAppFreezingScreenLocked(wtoken, true, true);
2613 } else {
2614 // If we are being set visible, and the starting window is
2615 // not yet displayed, then make sure it doesn't get displayed.
2616 WindowState swin = wtoken.startingWindow;
2617 if (swin != null && (swin.mDrawPending
2618 || swin.mCommitDrawPending)) {
2619 swin.mPolicyVisibility = false;
2620 swin.mPolicyVisibilityAfterAnim = false;
2621 }
2622 }
Romain Guy06882f82009-06-10 13:36:04 -07002623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2625 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2626 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 if (changed && performLayout) {
2629 mLayoutNeeded = true;
2630 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 performLayoutAndPlaceSurfacesLocked();
2632 }
2633 }
2634
2635 if (wtoken.animation != null) {
2636 delayed = true;
2637 }
Romain Guy06882f82009-06-10 13:36:04 -07002638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 return delayed;
2640 }
2641
2642 public void setAppVisibility(IBinder token, boolean visible) {
2643 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2644 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002645 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 }
2647
2648 AppWindowToken wtoken;
2649
2650 synchronized(mWindowMap) {
2651 wtoken = findAppWindowToken(token);
2652 if (wtoken == null) {
2653 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2654 return;
2655 }
2656
2657 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2658 RuntimeException e = new RuntimeException();
2659 e.fillInStackTrace();
2660 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2661 + "): mNextAppTransition=" + mNextAppTransition
2662 + " hidden=" + wtoken.hidden
2663 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2664 }
Romain Guy06882f82009-06-10 13:36:04 -07002665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 // If we are preparing an app transition, then delay changing
2667 // the visibility of this token until we execute that transition.
2668 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2669 // Already in requested state, don't do anything more.
2670 if (wtoken.hiddenRequested != visible) {
2671 return;
2672 }
2673 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 if (DEBUG_APP_TRANSITIONS) Log.v(
2676 TAG, "Setting dummy animation on: " + wtoken);
2677 wtoken.setDummyAnimation();
2678 mOpeningApps.remove(wtoken);
2679 mClosingApps.remove(wtoken);
2680 wtoken.inPendingTransaction = true;
2681 if (visible) {
2682 mOpeningApps.add(wtoken);
2683 wtoken.allDrawn = false;
2684 wtoken.startingDisplayed = false;
2685 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 if (wtoken.clientHidden) {
2688 // In the case where we are making an app visible
2689 // but holding off for a transition, we still need
2690 // to tell the client to make its windows visible so
2691 // they get drawn. Otherwise, we will wait on
2692 // performing the transition until all windows have
2693 // been drawn, they never will be, and we are sad.
2694 wtoken.clientHidden = false;
2695 wtoken.sendAppVisibilityToClients();
2696 }
2697 } else {
2698 mClosingApps.add(wtoken);
2699 }
2700 return;
2701 }
Romain Guy06882f82009-06-10 13:36:04 -07002702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 final long origId = Binder.clearCallingIdentity();
2704 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2705 wtoken.updateReportedVisibilityLocked();
2706 Binder.restoreCallingIdentity(origId);
2707 }
2708 }
2709
2710 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2711 boolean unfreezeSurfaceNow, boolean force) {
2712 if (wtoken.freezingScreen) {
2713 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2714 + " force=" + force);
2715 final int N = wtoken.allAppWindows.size();
2716 boolean unfrozeWindows = false;
2717 for (int i=0; i<N; i++) {
2718 WindowState w = wtoken.allAppWindows.get(i);
2719 if (w.mAppFreezing) {
2720 w.mAppFreezing = false;
2721 if (w.mSurface != null && !w.mOrientationChanging) {
2722 w.mOrientationChanging = true;
2723 }
2724 unfrozeWindows = true;
2725 }
2726 }
2727 if (force || unfrozeWindows) {
2728 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2729 wtoken.freezingScreen = false;
2730 mAppsFreezingScreen--;
2731 }
2732 if (unfreezeSurfaceNow) {
2733 if (unfrozeWindows) {
2734 mLayoutNeeded = true;
2735 performLayoutAndPlaceSurfacesLocked();
2736 }
2737 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2738 stopFreezingDisplayLocked();
2739 }
2740 }
2741 }
2742 }
Romain Guy06882f82009-06-10 13:36:04 -07002743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2745 int configChanges) {
2746 if (DEBUG_ORIENTATION) {
2747 RuntimeException e = new RuntimeException();
2748 e.fillInStackTrace();
2749 Log.i(TAG, "Set freezing of " + wtoken.appToken
2750 + ": hidden=" + wtoken.hidden + " freezing="
2751 + wtoken.freezingScreen, e);
2752 }
2753 if (!wtoken.hiddenRequested) {
2754 if (!wtoken.freezingScreen) {
2755 wtoken.freezingScreen = true;
2756 mAppsFreezingScreen++;
2757 if (mAppsFreezingScreen == 1) {
2758 startFreezingDisplayLocked();
2759 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
2760 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
2761 5000);
2762 }
2763 }
2764 final int N = wtoken.allAppWindows.size();
2765 for (int i=0; i<N; i++) {
2766 WindowState w = wtoken.allAppWindows.get(i);
2767 w.mAppFreezing = true;
2768 }
2769 }
2770 }
Romain Guy06882f82009-06-10 13:36:04 -07002771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 public void startAppFreezingScreen(IBinder token, int configChanges) {
2773 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2774 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002775 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 }
2777
2778 synchronized(mWindowMap) {
2779 if (configChanges == 0 && !mDisplayFrozen) {
2780 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
2781 return;
2782 }
Romain Guy06882f82009-06-10 13:36:04 -07002783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 AppWindowToken wtoken = findAppWindowToken(token);
2785 if (wtoken == null || wtoken.appToken == null) {
2786 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
2787 return;
2788 }
2789 final long origId = Binder.clearCallingIdentity();
2790 startAppFreezingScreenLocked(wtoken, configChanges);
2791 Binder.restoreCallingIdentity(origId);
2792 }
2793 }
Romain Guy06882f82009-06-10 13:36:04 -07002794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 public void stopAppFreezingScreen(IBinder token, boolean force) {
2796 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2797 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002798 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 }
2800
2801 synchronized(mWindowMap) {
2802 AppWindowToken wtoken = findAppWindowToken(token);
2803 if (wtoken == null || wtoken.appToken == null) {
2804 return;
2805 }
2806 final long origId = Binder.clearCallingIdentity();
2807 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
2808 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
2809 unsetAppFreezingScreenLocked(wtoken, true, force);
2810 Binder.restoreCallingIdentity(origId);
2811 }
2812 }
Romain Guy06882f82009-06-10 13:36:04 -07002813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 public void removeAppToken(IBinder token) {
2815 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2816 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002817 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 }
2819
2820 AppWindowToken wtoken = null;
2821 AppWindowToken startingToken = null;
2822 boolean delayed = false;
2823
2824 final long origId = Binder.clearCallingIdentity();
2825 synchronized(mWindowMap) {
2826 WindowToken basewtoken = mTokenMap.remove(token);
2827 mTokenList.remove(basewtoken);
2828 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
2829 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
2830 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
2831 wtoken.inPendingTransaction = false;
2832 mOpeningApps.remove(wtoken);
2833 if (mClosingApps.contains(wtoken)) {
2834 delayed = true;
2835 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2836 mClosingApps.add(wtoken);
2837 delayed = true;
2838 }
2839 if (DEBUG_APP_TRANSITIONS) Log.v(
2840 TAG, "Removing app " + wtoken + " delayed=" + delayed
2841 + " animation=" + wtoken.animation
2842 + " animating=" + wtoken.animating);
2843 if (delayed) {
2844 // set the token aside because it has an active animation to be finished
2845 mExitingAppTokens.add(wtoken);
2846 }
2847 mAppTokens.remove(wtoken);
2848 wtoken.removed = true;
2849 if (wtoken.startingData != null) {
2850 startingToken = wtoken;
2851 }
2852 unsetAppFreezingScreenLocked(wtoken, true, true);
2853 if (mFocusedApp == wtoken) {
2854 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
2855 mFocusedApp = null;
2856 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2857 mKeyWaiter.tickle();
2858 }
2859 } else {
2860 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
2861 }
Romain Guy06882f82009-06-10 13:36:04 -07002862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 if (!delayed && wtoken != null) {
2864 wtoken.updateReportedVisibilityLocked();
2865 }
2866 }
2867 Binder.restoreCallingIdentity(origId);
2868
2869 if (startingToken != null) {
2870 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
2871 + startingToken + ": app token removed");
2872 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
2873 mH.sendMessage(m);
2874 }
2875 }
2876
2877 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
2878 final int NW = token.windows.size();
2879 for (int i=0; i<NW; i++) {
2880 WindowState win = token.windows.get(i);
2881 mWindows.remove(win);
2882 int j = win.mChildWindows.size();
2883 while (j > 0) {
2884 j--;
2885 mWindows.remove(win.mChildWindows.get(j));
2886 }
2887 }
2888 return NW > 0;
2889 }
2890
2891 void dumpAppTokensLocked() {
2892 for (int i=mAppTokens.size()-1; i>=0; i--) {
2893 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
2894 }
2895 }
Romain Guy06882f82009-06-10 13:36:04 -07002896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 void dumpWindowsLocked() {
2898 for (int i=mWindows.size()-1; i>=0; i--) {
2899 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
2900 }
2901 }
Romain Guy06882f82009-06-10 13:36:04 -07002902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 private int findWindowOffsetLocked(int tokenPos) {
2904 final int NW = mWindows.size();
2905
2906 if (tokenPos >= mAppTokens.size()) {
2907 int i = NW;
2908 while (i > 0) {
2909 i--;
2910 WindowState win = (WindowState)mWindows.get(i);
2911 if (win.getAppToken() != null) {
2912 return i+1;
2913 }
2914 }
2915 }
2916
2917 while (tokenPos > 0) {
2918 // Find the first app token below the new position that has
2919 // a window displayed.
2920 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
2921 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
2922 + tokenPos + " -- " + wtoken.token);
2923 int i = wtoken.windows.size();
2924 while (i > 0) {
2925 i--;
2926 WindowState win = wtoken.windows.get(i);
2927 int j = win.mChildWindows.size();
2928 while (j > 0) {
2929 j--;
2930 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2931 if (cwin.mSubLayer >= 0 ) {
2932 for (int pos=NW-1; pos>=0; pos--) {
2933 if (mWindows.get(pos) == cwin) {
2934 if (DEBUG_REORDER) Log.v(TAG,
2935 "Found child win @" + (pos+1));
2936 return pos+1;
2937 }
2938 }
2939 }
2940 }
2941 for (int pos=NW-1; pos>=0; pos--) {
2942 if (mWindows.get(pos) == win) {
2943 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
2944 return pos+1;
2945 }
2946 }
2947 }
2948 tokenPos--;
2949 }
2950
2951 return 0;
2952 }
2953
2954 private final int reAddWindowLocked(int index, WindowState win) {
2955 final int NCW = win.mChildWindows.size();
2956 boolean added = false;
2957 for (int j=0; j<NCW; j++) {
2958 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2959 if (!added && cwin.mSubLayer >= 0) {
2960 mWindows.add(index, win);
2961 index++;
2962 added = true;
2963 }
2964 mWindows.add(index, cwin);
2965 index++;
2966 }
2967 if (!added) {
2968 mWindows.add(index, win);
2969 index++;
2970 }
2971 return index;
2972 }
Romain Guy06882f82009-06-10 13:36:04 -07002973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 private final int reAddAppWindowsLocked(int index, WindowToken token) {
2975 final int NW = token.windows.size();
2976 for (int i=0; i<NW; i++) {
2977 index = reAddWindowLocked(index, token.windows.get(i));
2978 }
2979 return index;
2980 }
2981
2982 public void moveAppToken(int index, IBinder token) {
2983 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2984 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002985 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 }
2987
2988 synchronized(mWindowMap) {
2989 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
2990 if (DEBUG_REORDER) dumpAppTokensLocked();
2991 final AppWindowToken wtoken = findAppWindowToken(token);
2992 if (wtoken == null || !mAppTokens.remove(wtoken)) {
2993 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
2994 + token + " (" + wtoken + ")");
2995 return;
2996 }
2997 mAppTokens.add(index, wtoken);
2998 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
2999 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 final long origId = Binder.clearCallingIdentity();
3002 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3003 if (DEBUG_REORDER) dumpWindowsLocked();
3004 if (tmpRemoveAppWindowsLocked(wtoken)) {
3005 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3006 if (DEBUG_REORDER) dumpWindowsLocked();
3007 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3008 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3009 if (DEBUG_REORDER) dumpWindowsLocked();
3010 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 mLayoutNeeded = true;
3012 performLayoutAndPlaceSurfacesLocked();
3013 }
3014 Binder.restoreCallingIdentity(origId);
3015 }
3016 }
3017
3018 private void removeAppTokensLocked(List<IBinder> tokens) {
3019 // XXX This should be done more efficiently!
3020 // (take advantage of the fact that both lists should be
3021 // ordered in the same way.)
3022 int N = tokens.size();
3023 for (int i=0; i<N; i++) {
3024 IBinder token = tokens.get(i);
3025 final AppWindowToken wtoken = findAppWindowToken(token);
3026 if (!mAppTokens.remove(wtoken)) {
3027 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3028 + token + " (" + wtoken + ")");
3029 i--;
3030 N--;
3031 }
3032 }
3033 }
3034
3035 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3036 // First remove all of the windows from the list.
3037 final int N = tokens.size();
3038 int i;
3039 for (i=0; i<N; i++) {
3040 WindowToken token = mTokenMap.get(tokens.get(i));
3041 if (token != null) {
3042 tmpRemoveAppWindowsLocked(token);
3043 }
3044 }
3045
3046 // Where to start adding?
3047 int pos = findWindowOffsetLocked(tokenPos);
3048
3049 // And now add them back at the correct place.
3050 for (i=0; i<N; i++) {
3051 WindowToken token = mTokenMap.get(tokens.get(i));
3052 if (token != null) {
3053 pos = reAddAppWindowsLocked(pos, token);
3054 }
3055 }
3056
3057 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 mLayoutNeeded = true;
3059 performLayoutAndPlaceSurfacesLocked();
3060
3061 //dump();
3062 }
3063
3064 public void moveAppTokensToTop(List<IBinder> tokens) {
3065 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3066 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003067 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 }
3069
3070 final long origId = Binder.clearCallingIdentity();
3071 synchronized(mWindowMap) {
3072 removeAppTokensLocked(tokens);
3073 final int N = tokens.size();
3074 for (int i=0; i<N; i++) {
3075 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3076 if (wt != null) {
3077 mAppTokens.add(wt);
3078 }
3079 }
3080 moveAppWindowsLocked(tokens, mAppTokens.size());
3081 }
3082 Binder.restoreCallingIdentity(origId);
3083 }
3084
3085 public void moveAppTokensToBottom(List<IBinder> tokens) {
3086 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3087 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003088 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 }
3090
3091 final long origId = Binder.clearCallingIdentity();
3092 synchronized(mWindowMap) {
3093 removeAppTokensLocked(tokens);
3094 final int N = tokens.size();
3095 int pos = 0;
3096 for (int i=0; i<N; i++) {
3097 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3098 if (wt != null) {
3099 mAppTokens.add(pos, wt);
3100 pos++;
3101 }
3102 }
3103 moveAppWindowsLocked(tokens, 0);
3104 }
3105 Binder.restoreCallingIdentity(origId);
3106 }
3107
3108 // -------------------------------------------------------------
3109 // Misc IWindowSession methods
3110 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 public void disableKeyguard(IBinder token, String tag) {
3113 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3114 != PackageManager.PERMISSION_GRANTED) {
3115 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3116 }
3117 mKeyguardDisabled.acquire(token, tag);
3118 }
3119
3120 public void reenableKeyguard(IBinder token) {
3121 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3122 != PackageManager.PERMISSION_GRANTED) {
3123 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3124 }
3125 synchronized (mKeyguardDisabled) {
3126 mKeyguardDisabled.release(token);
3127
3128 if (!mKeyguardDisabled.isAcquired()) {
3129 // if we are the last one to reenable the keyguard wait until
3130 // we have actaully finished reenabling until returning
3131 mWaitingUntilKeyguardReenabled = true;
3132 while (mWaitingUntilKeyguardReenabled) {
3133 try {
3134 mKeyguardDisabled.wait();
3135 } catch (InterruptedException e) {
3136 Thread.currentThread().interrupt();
3137 }
3138 }
3139 }
3140 }
3141 }
3142
3143 /**
3144 * @see android.app.KeyguardManager#exitKeyguardSecurely
3145 */
3146 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3147 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3148 != PackageManager.PERMISSION_GRANTED) {
3149 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3150 }
3151 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3152 public void onKeyguardExitResult(boolean success) {
3153 try {
3154 callback.onKeyguardExitResult(success);
3155 } catch (RemoteException e) {
3156 // Client has died, we don't care.
3157 }
3158 }
3159 });
3160 }
3161
3162 public boolean inKeyguardRestrictedInputMode() {
3163 return mPolicy.inKeyguardRestrictedKeyInputMode();
3164 }
Romain Guy06882f82009-06-10 13:36:04 -07003165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 static float fixScale(float scale) {
3167 if (scale < 0) scale = 0;
3168 else if (scale > 20) scale = 20;
3169 return Math.abs(scale);
3170 }
Romain Guy06882f82009-06-10 13:36:04 -07003171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 public void setAnimationScale(int which, float scale) {
3173 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3174 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003175 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 }
3177
3178 if (scale < 0) scale = 0;
3179 else if (scale > 20) scale = 20;
3180 scale = Math.abs(scale);
3181 switch (which) {
3182 case 0: mWindowAnimationScale = fixScale(scale); break;
3183 case 1: mTransitionAnimationScale = fixScale(scale); break;
3184 }
Romain Guy06882f82009-06-10 13:36:04 -07003185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 // Persist setting
3187 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3188 }
Romain Guy06882f82009-06-10 13:36:04 -07003189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 public void setAnimationScales(float[] scales) {
3191 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3192 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003193 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 }
3195
3196 if (scales != null) {
3197 if (scales.length >= 1) {
3198 mWindowAnimationScale = fixScale(scales[0]);
3199 }
3200 if (scales.length >= 2) {
3201 mTransitionAnimationScale = fixScale(scales[1]);
3202 }
3203 }
Romain Guy06882f82009-06-10 13:36:04 -07003204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 // Persist setting
3206 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3207 }
Romain Guy06882f82009-06-10 13:36:04 -07003208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 public float getAnimationScale(int which) {
3210 switch (which) {
3211 case 0: return mWindowAnimationScale;
3212 case 1: return mTransitionAnimationScale;
3213 }
3214 return 0;
3215 }
Romain Guy06882f82009-06-10 13:36:04 -07003216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 public float[] getAnimationScales() {
3218 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3219 }
Romain Guy06882f82009-06-10 13:36:04 -07003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 public int getSwitchState(int sw) {
3222 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3223 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003224 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 }
3226 return KeyInputQueue.getSwitchState(sw);
3227 }
Romain Guy06882f82009-06-10 13:36:04 -07003228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 public int getSwitchStateForDevice(int devid, int sw) {
3230 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3231 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003232 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 }
3234 return KeyInputQueue.getSwitchState(devid, sw);
3235 }
Romain Guy06882f82009-06-10 13:36:04 -07003236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 public int getScancodeState(int sw) {
3238 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3239 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003240 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 }
3242 return KeyInputQueue.getScancodeState(sw);
3243 }
Romain Guy06882f82009-06-10 13:36:04 -07003244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 public int getScancodeStateForDevice(int devid, int sw) {
3246 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3247 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003248 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 }
3250 return KeyInputQueue.getScancodeState(devid, sw);
3251 }
Romain Guy06882f82009-06-10 13:36:04 -07003252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 public int getKeycodeState(int sw) {
3254 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3255 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003256 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 }
3258 return KeyInputQueue.getKeycodeState(sw);
3259 }
Romain Guy06882f82009-06-10 13:36:04 -07003260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 public int getKeycodeStateForDevice(int devid, int sw) {
3262 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3263 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003264 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 }
3266 return KeyInputQueue.getKeycodeState(devid, sw);
3267 }
Romain Guy06882f82009-06-10 13:36:04 -07003268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3270 return KeyInputQueue.hasKeys(keycodes, keyExists);
3271 }
Romain Guy06882f82009-06-10 13:36:04 -07003272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 public void enableScreenAfterBoot() {
3274 synchronized(mWindowMap) {
3275 if (mSystemBooted) {
3276 return;
3277 }
3278 mSystemBooted = true;
3279 }
Romain Guy06882f82009-06-10 13:36:04 -07003280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 performEnableScreen();
3282 }
Romain Guy06882f82009-06-10 13:36:04 -07003283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 public void enableScreenIfNeededLocked() {
3285 if (mDisplayEnabled) {
3286 return;
3287 }
3288 if (!mSystemBooted) {
3289 return;
3290 }
3291 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3292 }
Romain Guy06882f82009-06-10 13:36:04 -07003293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 public void performEnableScreen() {
3295 synchronized(mWindowMap) {
3296 if (mDisplayEnabled) {
3297 return;
3298 }
3299 if (!mSystemBooted) {
3300 return;
3301 }
Romain Guy06882f82009-06-10 13:36:04 -07003302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 // Don't enable the screen until all existing windows
3304 // have been drawn.
3305 final int N = mWindows.size();
3306 for (int i=0; i<N; i++) {
3307 WindowState w = (WindowState)mWindows.get(i);
3308 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3309 return;
3310 }
3311 }
Romain Guy06882f82009-06-10 13:36:04 -07003312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 mDisplayEnabled = true;
3314 if (false) {
3315 Log.i(TAG, "ENABLING SCREEN!");
3316 StringWriter sw = new StringWriter();
3317 PrintWriter pw = new PrintWriter(sw);
3318 this.dump(null, pw, null);
3319 Log.i(TAG, sw.toString());
3320 }
3321 try {
3322 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3323 if (surfaceFlinger != null) {
3324 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3325 Parcel data = Parcel.obtain();
3326 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3327 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3328 data, null, 0);
3329 data.recycle();
3330 }
3331 } catch (RemoteException ex) {
3332 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3333 }
3334 }
Romain Guy06882f82009-06-10 13:36:04 -07003335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003339 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3340 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 }
Romain Guy06882f82009-06-10 13:36:04 -07003342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 public void setInTouchMode(boolean mode) {
3344 synchronized(mWindowMap) {
3345 mInTouchMode = mode;
3346 }
3347 }
3348
Romain Guy06882f82009-06-10 13:36:04 -07003349 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003350 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003352 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003353 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 }
3355
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003356 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 }
Romain Guy06882f82009-06-10 13:36:04 -07003358
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003359 public void setRotationUnchecked(int rotation,
3360 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 if(DEBUG_ORIENTATION) Log.v(TAG,
3362 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 long origId = Binder.clearCallingIdentity();
3365 boolean changed;
3366 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003367 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 }
Romain Guy06882f82009-06-10 13:36:04 -07003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 if (changed) {
3371 sendNewConfiguration();
3372 synchronized(mWindowMap) {
3373 mLayoutNeeded = true;
3374 performLayoutAndPlaceSurfacesLocked();
3375 }
3376 } else if (alwaysSendConfiguration) {
3377 //update configuration ignoring orientation change
3378 sendNewConfiguration();
3379 }
Romain Guy06882f82009-06-10 13:36:04 -07003380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 Binder.restoreCallingIdentity(origId);
3382 }
Romain Guy06882f82009-06-10 13:36:04 -07003383
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003384 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 boolean changed;
3386 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3387 rotation = mRequestedRotation;
3388 } else {
3389 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003390 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 }
3392 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003393 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 mRotation, mDisplayEnabled);
3395 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3396 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003399 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 "Rotation changed to " + rotation
3401 + " from " + mRotation
3402 + " (forceApp=" + mForcedAppOrientation
3403 + ", req=" + mRequestedRotation + ")");
3404 mRotation = rotation;
3405 mWindowsFreezingScreen = true;
3406 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3407 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3408 2000);
3409 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003410 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 mQueue.setOrientation(rotation);
3412 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003413 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 }
3415 for (int i=mWindows.size()-1; i>=0; i--) {
3416 WindowState w = (WindowState)mWindows.get(i);
3417 if (w.mSurface != null) {
3418 w.mOrientationChanging = true;
3419 }
3420 }
3421 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3422 try {
3423 mRotationWatchers.get(i).onRotationChanged(rotation);
3424 } catch (RemoteException e) {
3425 }
3426 }
3427 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 return changed;
3430 }
Romain Guy06882f82009-06-10 13:36:04 -07003431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 public int getRotation() {
3433 return mRotation;
3434 }
3435
3436 public int watchRotation(IRotationWatcher watcher) {
3437 final IBinder watcherBinder = watcher.asBinder();
3438 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3439 public void binderDied() {
3440 synchronized (mWindowMap) {
3441 for (int i=0; i<mRotationWatchers.size(); i++) {
3442 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003443 IRotationWatcher removed = mRotationWatchers.remove(i);
3444 if (removed != null) {
3445 removed.asBinder().unlinkToDeath(this, 0);
3446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 i--;
3448 }
3449 }
3450 }
3451 }
3452 };
Romain Guy06882f82009-06-10 13:36:04 -07003453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 synchronized (mWindowMap) {
3455 try {
3456 watcher.asBinder().linkToDeath(dr, 0);
3457 mRotationWatchers.add(watcher);
3458 } catch (RemoteException e) {
3459 // Client died, no cleanup needed.
3460 }
Romain Guy06882f82009-06-10 13:36:04 -07003461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 return mRotation;
3463 }
3464 }
3465
3466 /**
3467 * Starts the view server on the specified port.
3468 *
3469 * @param port The port to listener to.
3470 *
3471 * @return True if the server was successfully started, false otherwise.
3472 *
3473 * @see com.android.server.ViewServer
3474 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3475 */
3476 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003477 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 return false;
3479 }
3480
3481 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3482 return false;
3483 }
3484
3485 if (port < 1024) {
3486 return false;
3487 }
3488
3489 if (mViewServer != null) {
3490 if (!mViewServer.isRunning()) {
3491 try {
3492 return mViewServer.start();
3493 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003494 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 }
3496 }
3497 return false;
3498 }
3499
3500 try {
3501 mViewServer = new ViewServer(this, port);
3502 return mViewServer.start();
3503 } catch (IOException e) {
3504 Log.w(TAG, "View server did not start");
3505 }
3506 return false;
3507 }
3508
Romain Guy06882f82009-06-10 13:36:04 -07003509 private boolean isSystemSecure() {
3510 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3511 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3512 }
3513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 /**
3515 * Stops the view server if it exists.
3516 *
3517 * @return True if the server stopped, false if it wasn't started or
3518 * couldn't be stopped.
3519 *
3520 * @see com.android.server.ViewServer
3521 */
3522 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003523 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 return false;
3525 }
3526
3527 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3528 return false;
3529 }
3530
3531 if (mViewServer != null) {
3532 return mViewServer.stop();
3533 }
3534 return false;
3535 }
3536
3537 /**
3538 * Indicates whether the view server is running.
3539 *
3540 * @return True if the server is running, false otherwise.
3541 *
3542 * @see com.android.server.ViewServer
3543 */
3544 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003545 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 return false;
3547 }
3548
3549 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3550 return false;
3551 }
3552
3553 return mViewServer != null && mViewServer.isRunning();
3554 }
3555
3556 /**
3557 * Lists all availble windows in the system. The listing is written in the
3558 * specified Socket's output stream with the following syntax:
3559 * windowHashCodeInHexadecimal windowName
3560 * Each line of the ouput represents a different window.
3561 *
3562 * @param client The remote client to send the listing to.
3563 * @return False if an error occured, true otherwise.
3564 */
3565 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003566 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 return false;
3568 }
3569
3570 boolean result = true;
3571
3572 Object[] windows;
3573 synchronized (mWindowMap) {
3574 windows = new Object[mWindows.size()];
3575 //noinspection unchecked
3576 windows = mWindows.toArray(windows);
3577 }
3578
3579 BufferedWriter out = null;
3580
3581 // Any uncaught exception will crash the system process
3582 try {
3583 OutputStream clientStream = client.getOutputStream();
3584 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3585
3586 final int count = windows.length;
3587 for (int i = 0; i < count; i++) {
3588 final WindowState w = (WindowState) windows[i];
3589 out.write(Integer.toHexString(System.identityHashCode(w)));
3590 out.write(' ');
3591 out.append(w.mAttrs.getTitle());
3592 out.write('\n');
3593 }
3594
3595 out.write("DONE.\n");
3596 out.flush();
3597 } catch (Exception e) {
3598 result = false;
3599 } finally {
3600 if (out != null) {
3601 try {
3602 out.close();
3603 } catch (IOException e) {
3604 result = false;
3605 }
3606 }
3607 }
3608
3609 return result;
3610 }
3611
3612 /**
3613 * Sends a command to a target window. The result of the command, if any, will be
3614 * written in the output stream of the specified socket.
3615 *
3616 * The parameters must follow this syntax:
3617 * windowHashcode extra
3618 *
3619 * Where XX is the length in characeters of the windowTitle.
3620 *
3621 * The first parameter is the target window. The window with the specified hashcode
3622 * will be the target. If no target can be found, nothing happens. The extra parameters
3623 * will be delivered to the target window and as parameters to the command itself.
3624 *
3625 * @param client The remote client to sent the result, if any, to.
3626 * @param command The command to execute.
3627 * @param parameters The command parameters.
3628 *
3629 * @return True if the command was successfully delivered, false otherwise. This does
3630 * not indicate whether the command itself was successful.
3631 */
3632 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003633 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 return false;
3635 }
3636
3637 boolean success = true;
3638 Parcel data = null;
3639 Parcel reply = null;
3640
3641 // Any uncaught exception will crash the system process
3642 try {
3643 // Find the hashcode of the window
3644 int index = parameters.indexOf(' ');
3645 if (index == -1) {
3646 index = parameters.length();
3647 }
3648 final String code = parameters.substring(0, index);
3649 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3650
3651 // Extract the command's parameter after the window description
3652 if (index < parameters.length()) {
3653 parameters = parameters.substring(index + 1);
3654 } else {
3655 parameters = "";
3656 }
3657
3658 final WindowManagerService.WindowState window = findWindow(hashCode);
3659 if (window == null) {
3660 return false;
3661 }
3662
3663 data = Parcel.obtain();
3664 data.writeInterfaceToken("android.view.IWindow");
3665 data.writeString(command);
3666 data.writeString(parameters);
3667 data.writeInt(1);
3668 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3669
3670 reply = Parcel.obtain();
3671
3672 final IBinder binder = window.mClient.asBinder();
3673 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3674 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3675
3676 reply.readException();
3677
3678 } catch (Exception e) {
3679 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3680 success = false;
3681 } finally {
3682 if (data != null) {
3683 data.recycle();
3684 }
3685 if (reply != null) {
3686 reply.recycle();
3687 }
3688 }
3689
3690 return success;
3691 }
3692
3693 private WindowState findWindow(int hashCode) {
3694 if (hashCode == -1) {
3695 return getFocusedWindow();
3696 }
3697
3698 synchronized (mWindowMap) {
3699 final ArrayList windows = mWindows;
3700 final int count = windows.size();
3701
3702 for (int i = 0; i < count; i++) {
3703 WindowState w = (WindowState) windows.get(i);
3704 if (System.identityHashCode(w) == hashCode) {
3705 return w;
3706 }
3707 }
3708 }
3709
3710 return null;
3711 }
3712
3713 /*
3714 * Instruct the Activity Manager to fetch the current configuration and broadcast
3715 * that to config-changed listeners if appropriate.
3716 */
3717 void sendNewConfiguration() {
3718 try {
3719 mActivityManager.updateConfiguration(null);
3720 } catch (RemoteException e) {
3721 }
3722 }
Romain Guy06882f82009-06-10 13:36:04 -07003723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 public Configuration computeNewConfiguration() {
3725 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003726 return computeNewConfigurationLocked();
3727 }
3728 }
Romain Guy06882f82009-06-10 13:36:04 -07003729
Dianne Hackbornc485a602009-03-24 22:39:49 -07003730 Configuration computeNewConfigurationLocked() {
3731 Configuration config = new Configuration();
3732 if (!computeNewConfigurationLocked(config)) {
3733 return null;
3734 }
3735 Log.i(TAG, "Config changed: " + config);
3736 long now = SystemClock.uptimeMillis();
3737 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3738 if (mFreezeGcPending != 0) {
3739 if (now > (mFreezeGcPending+1000)) {
3740 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3741 mH.removeMessages(H.FORCE_GC);
3742 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 mFreezeGcPending = now;
3744 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003745 } else {
3746 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003748 return config;
3749 }
Romain Guy06882f82009-06-10 13:36:04 -07003750
Dianne Hackbornc485a602009-03-24 22:39:49 -07003751 boolean computeNewConfigurationLocked(Configuration config) {
3752 if (mDisplay == null) {
3753 return false;
3754 }
3755 mQueue.getInputConfiguration(config);
3756 final int dw = mDisplay.getWidth();
3757 final int dh = mDisplay.getHeight();
3758 int orientation = Configuration.ORIENTATION_SQUARE;
3759 if (dw < dh) {
3760 orientation = Configuration.ORIENTATION_PORTRAIT;
3761 } else if (dw > dh) {
3762 orientation = Configuration.ORIENTATION_LANDSCAPE;
3763 }
3764 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003765
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003766 DisplayMetrics dm = new DisplayMetrics();
3767 mDisplay.getMetrics(dm);
3768 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
3769
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003770 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07003771 // Note we only do this once because at this point we don't
3772 // expect the screen to change in this way at runtime, and want
3773 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07003774 int longSize = dw;
3775 int shortSize = dh;
3776 if (longSize < shortSize) {
3777 int tmp = longSize;
3778 longSize = shortSize;
3779 shortSize = tmp;
3780 }
3781 longSize = (int)(longSize/dm.density);
3782 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003783
Dianne Hackborn723738c2009-06-25 19:48:04 -07003784 // These semi-magic numbers define our compatibility modes for
3785 // applications with different screens. Don't change unless you
3786 // make sure to test lots and lots of apps!
3787 if (longSize < 470) {
3788 // This is shorter than an HVGA normal density screen (which
3789 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003790 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
3791 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003792 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003793 // Is this a large screen?
3794 if (longSize > 640 && shortSize >= 480) {
3795 // VGA or larger screens at medium density are the point
3796 // at which we consider it to be a large screen.
3797 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
3798 } else {
3799 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
3800
3801 // If this screen is wider than normal HVGA, or taller
3802 // than FWVGA, then for old apps we want to run in size
3803 // compatibility mode.
3804 if (shortSize > 321 || longSize > 570) {
3805 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
3806 }
3807 }
3808
3809 // Is this a long screen?
3810 if (((longSize*3)/5) >= (shortSize-1)) {
3811 // Anything wider than WVGA (5:3) is considering to be long.
3812 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
3813 } else {
3814 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
3815 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07003816 }
3817 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003818 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003819
Dianne Hackbornc485a602009-03-24 22:39:49 -07003820 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3821 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3822 mPolicy.adjustConfigurationLw(config);
3823 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 }
Romain Guy06882f82009-06-10 13:36:04 -07003825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 // -------------------------------------------------------------
3827 // Input Events and Focus Management
3828 // -------------------------------------------------------------
3829
3830 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07003831 long curTime = SystemClock.uptimeMillis();
3832
Michael Chane10de972009-05-18 11:24:50 -07003833 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07003834 if (mLastTouchEventType == eventType &&
3835 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
3836 return;
3837 }
3838 mLastUserActivityCallTime = curTime;
3839 mLastTouchEventType = eventType;
3840 }
3841
3842 if (targetWin == null
3843 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
3844 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003845 }
3846 }
3847
3848 // tells if it's a cheek event or not -- this function is stateful
3849 private static final int EVENT_NONE = 0;
3850 private static final int EVENT_UNKNOWN = 0;
3851 private static final int EVENT_CHEEK = 0;
3852 private static final int EVENT_IGNORE_DURATION = 300; // ms
3853 private static final float CHEEK_THRESHOLD = 0.6f;
3854 private int mEventState = EVENT_NONE;
3855 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 private int eventType(MotionEvent ev) {
3858 float size = ev.getSize();
3859 switch (ev.getAction()) {
3860 case MotionEvent.ACTION_DOWN:
3861 mEventSize = size;
3862 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
3863 case MotionEvent.ACTION_UP:
3864 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003865 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 case MotionEvent.ACTION_MOVE:
3867 final int N = ev.getHistorySize();
3868 if (size > mEventSize) mEventSize = size;
3869 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3870 for (int i=0; i<N; i++) {
3871 size = ev.getHistoricalSize(i);
3872 if (size > mEventSize) mEventSize = size;
3873 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3874 }
3875 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
3876 return TOUCH_EVENT;
3877 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003878 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003879 }
3880 default:
3881 // not good
3882 return OTHER_EVENT;
3883 }
3884 }
3885
3886 /**
3887 * @return Returns true if event was dispatched, false if it was dropped for any reason
3888 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07003889 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
3891 "dispatchPointer " + ev);
3892
Michael Chan53071d62009-05-13 17:29:48 -07003893 if (MEASURE_LATENCY) {
3894 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
3895 }
3896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07003898 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899
Michael Chan53071d62009-05-13 17:29:48 -07003900 if (MEASURE_LATENCY) {
3901 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
3902 }
3903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07003905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 if (action == MotionEvent.ACTION_UP) {
3907 // let go of our target
3908 mKeyWaiter.mMotionTarget = null;
3909 mPowerManager.logPointerUpEvent();
3910 } else if (action == MotionEvent.ACTION_DOWN) {
3911 mPowerManager.logPointerDownEvent();
3912 }
3913
3914 if (targetObj == null) {
3915 // In this case we are either dropping the event, or have received
3916 // a move or up without a down. It is common to receive move
3917 // events in such a way, since this means the user is moving the
3918 // pointer without actually pressing down. All other cases should
3919 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07003920 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
3922 }
3923 if (qev != null) {
3924 mQueue.recycleEvent(qev);
3925 }
3926 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003927 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 }
3929 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
3930 if (qev != null) {
3931 mQueue.recycleEvent(qev);
3932 }
3933 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003934 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 }
Romain Guy06882f82009-06-10 13:36:04 -07003936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07003938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07003940 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941
3942 //Log.i(TAG, "Sending " + ev + " to " + target);
3943
3944 if (uid != 0 && uid != target.mSession.mUid) {
3945 if (mContext.checkPermission(
3946 android.Manifest.permission.INJECT_EVENTS, pid, uid)
3947 != PackageManager.PERMISSION_GRANTED) {
3948 Log.w(TAG, "Permission denied: injecting pointer event from pid "
3949 + pid + " uid " + uid + " to window " + target
3950 + " owned by uid " + target.mSession.mUid);
3951 if (qev != null) {
3952 mQueue.recycleEvent(qev);
3953 }
3954 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003955 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 }
3957 }
3958
Michael Chan53071d62009-05-13 17:29:48 -07003959 if (MEASURE_LATENCY) {
3960 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
3961 }
3962
Romain Guy06882f82009-06-10 13:36:04 -07003963 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
3965 //target wants to ignore fat touch events
3966 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
3967 //explicit flag to return without processing event further
3968 boolean returnFlag = false;
3969 if((action == MotionEvent.ACTION_DOWN)) {
3970 mFatTouch = false;
3971 if(cheekPress) {
3972 mFatTouch = true;
3973 returnFlag = true;
3974 }
3975 } else {
3976 if(action == MotionEvent.ACTION_UP) {
3977 if(mFatTouch) {
3978 //earlier even was invalid doesnt matter if current up is cheekpress or not
3979 mFatTouch = false;
3980 returnFlag = true;
3981 } else if(cheekPress) {
3982 //cancel the earlier event
3983 ev.setAction(MotionEvent.ACTION_CANCEL);
3984 action = MotionEvent.ACTION_CANCEL;
3985 }
3986 } else if(action == MotionEvent.ACTION_MOVE) {
3987 if(mFatTouch) {
3988 //two cases here
3989 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07003990 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 returnFlag = true;
3992 } else if(cheekPress) {
3993 //valid down followed by invalid moves
3994 //an invalid move have to cancel earlier action
3995 ev.setAction(MotionEvent.ACTION_CANCEL);
3996 action = MotionEvent.ACTION_CANCEL;
3997 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
3998 //note that the subsequent invalid moves will not get here
3999 mFatTouch = true;
4000 }
4001 }
4002 } //else if action
4003 if(returnFlag) {
4004 //recycle que, ev
4005 if (qev != null) {
4006 mQueue.recycleEvent(qev);
4007 }
4008 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004009 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 }
4011 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004012
4013 // TODO remove once we settle on a value or make it app specific
4014 if (action == MotionEvent.ACTION_DOWN) {
4015 int max_events_per_sec = 35;
4016 try {
4017 max_events_per_sec = Integer.parseInt(SystemProperties
4018 .get("windowsmgr.max_events_per_sec"));
4019 if (max_events_per_sec < 1) {
4020 max_events_per_sec = 35;
4021 }
4022 } catch (NumberFormatException e) {
4023 }
4024 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4025 }
4026
4027 /*
4028 * Throttle events to minimize CPU usage when there's a flood of events
4029 * e.g. constant contact with the screen
4030 */
4031 if (action == MotionEvent.ACTION_MOVE) {
4032 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4033 long now = SystemClock.uptimeMillis();
4034 if (now < nextEventTime) {
4035 try {
4036 Thread.sleep(nextEventTime - now);
4037 } catch (InterruptedException e) {
4038 }
4039 mLastTouchEventTime = nextEventTime;
4040 } else {
4041 mLastTouchEventTime = now;
4042 }
4043 }
4044
Michael Chan53071d62009-05-13 17:29:48 -07004045 if (MEASURE_LATENCY) {
4046 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4047 }
4048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 synchronized(mWindowMap) {
4050 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4051 mKeyWaiter.bindTargetWindowLocked(target,
4052 KeyWaiter.RETURN_PENDING_POINTER, qev);
4053 ev = null;
4054 } else {
4055 if (action == MotionEvent.ACTION_DOWN) {
4056 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4057 if (out != null) {
4058 MotionEvent oev = MotionEvent.obtain(ev);
4059 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4060 do {
4061 final Rect frame = out.mFrame;
4062 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4063 try {
4064 out.mClient.dispatchPointer(oev, eventTime);
4065 } catch (android.os.RemoteException e) {
4066 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4067 }
4068 oev.offsetLocation((float)frame.left, (float)frame.top);
4069 out = out.mNextOutsideTouch;
4070 } while (out != null);
4071 mKeyWaiter.mOutsideTouchTargets = null;
4072 }
4073 }
4074 final Rect frame = target.mFrame;
4075 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4076 mKeyWaiter.bindTargetWindowLocked(target);
4077 }
4078 }
Romain Guy06882f82009-06-10 13:36:04 -07004079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 // finally offset the event to the target's coordinate system and
4081 // dispatch the event.
4082 try {
4083 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4084 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4085 }
Michael Chan53071d62009-05-13 17:29:48 -07004086
4087 if (MEASURE_LATENCY) {
4088 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4089 }
4090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 target.mClient.dispatchPointer(ev, eventTime);
Michael Chan53071d62009-05-13 17:29:48 -07004092
4093 if (MEASURE_LATENCY) {
4094 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4095 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004096 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 } catch (android.os.RemoteException e) {
4098 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4099 mKeyWaiter.mMotionTarget = null;
4100 try {
4101 removeWindow(target.mSession, target.mClient);
4102 } catch (java.util.NoSuchElementException ex) {
4103 // This will happen if the window has already been
4104 // removed.
4105 }
4106 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004107 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 }
Romain Guy06882f82009-06-10 13:36:04 -07004109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 /**
4111 * @return Returns true if event was dispatched, false if it was dropped for any reason
4112 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004113 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 if (DEBUG_INPUT) Log.v(
4115 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004118 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 if (focusObj == null) {
4120 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4121 if (qev != null) {
4122 mQueue.recycleEvent(qev);
4123 }
4124 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004125 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 }
4127 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4128 if (qev != null) {
4129 mQueue.recycleEvent(qev);
4130 }
4131 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004132 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 }
Romain Guy06882f82009-06-10 13:36:04 -07004134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 if (uid != 0 && uid != focus.mSession.mUid) {
4138 if (mContext.checkPermission(
4139 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4140 != PackageManager.PERMISSION_GRANTED) {
4141 Log.w(TAG, "Permission denied: injecting key event from pid "
4142 + pid + " uid " + uid + " to window " + focus
4143 + " owned by uid " + focus.mSession.mUid);
4144 if (qev != null) {
4145 mQueue.recycleEvent(qev);
4146 }
4147 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004148 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 }
4150 }
Romain Guy06882f82009-06-10 13:36:04 -07004151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 synchronized(mWindowMap) {
4155 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4156 mKeyWaiter.bindTargetWindowLocked(focus,
4157 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4158 // We don't deliver movement events to the client, we hold
4159 // them and wait for them to call back.
4160 ev = null;
4161 } else {
4162 mKeyWaiter.bindTargetWindowLocked(focus);
4163 }
4164 }
Romain Guy06882f82009-06-10 13:36:04 -07004165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 try {
4167 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004168 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 } catch (android.os.RemoteException e) {
4170 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4171 try {
4172 removeWindow(focus.mSession, focus.mClient);
4173 } catch (java.util.NoSuchElementException ex) {
4174 // This will happen if the window has already been
4175 // removed.
4176 }
4177 }
Romain Guy06882f82009-06-10 13:36:04 -07004178
Dianne Hackborncfaef692009-06-15 14:24:44 -07004179 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 }
Romain Guy06882f82009-06-10 13:36:04 -07004181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 /**
4183 * @return Returns true if event was dispatched, false if it was dropped for any reason
4184 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004185 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4187
4188 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004189 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 if (focusObj == null) {
4191 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004192 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 }
4194 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004195 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 }
Romain Guy06882f82009-06-10 13:36:04 -07004197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 if (DEBUG_INPUT) Log.v(
4201 TAG, "Dispatching to " + focus + ": " + event);
4202
4203 if (uid != 0 && uid != focus.mSession.mUid) {
4204 if (mContext.checkPermission(
4205 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4206 != PackageManager.PERMISSION_GRANTED) {
4207 Log.w(TAG, "Permission denied: injecting key event from pid "
4208 + pid + " uid " + uid + " to window " + focus
4209 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004210 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 }
4212 }
Romain Guy06882f82009-06-10 13:36:04 -07004213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 synchronized(mWindowMap) {
4215 mKeyWaiter.bindTargetWindowLocked(focus);
4216 }
4217
4218 // NOSHIP extra state logging
4219 mKeyWaiter.recordDispatchState(event, focus);
4220 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 try {
4223 if (DEBUG_INPUT || DEBUG_FOCUS) {
4224 Log.v(TAG, "Delivering key " + event.getKeyCode()
4225 + " to " + focus);
4226 }
4227 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004228 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 } catch (android.os.RemoteException e) {
4230 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4231 try {
4232 removeWindow(focus.mSession, focus.mClient);
4233 } catch (java.util.NoSuchElementException ex) {
4234 // This will happen if the window has already been
4235 // removed.
4236 }
4237 }
Romain Guy06882f82009-06-10 13:36:04 -07004238
Dianne Hackborncfaef692009-06-15 14:24:44 -07004239 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 }
Romain Guy06882f82009-06-10 13:36:04 -07004241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 public void pauseKeyDispatching(IBinder _token) {
4243 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4244 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004245 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 }
4247
4248 synchronized (mWindowMap) {
4249 WindowToken token = mTokenMap.get(_token);
4250 if (token != null) {
4251 mKeyWaiter.pauseDispatchingLocked(token);
4252 }
4253 }
4254 }
4255
4256 public void resumeKeyDispatching(IBinder _token) {
4257 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4258 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004259 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 }
4261
4262 synchronized (mWindowMap) {
4263 WindowToken token = mTokenMap.get(_token);
4264 if (token != null) {
4265 mKeyWaiter.resumeDispatchingLocked(token);
4266 }
4267 }
4268 }
4269
4270 public void setEventDispatching(boolean enabled) {
4271 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4272 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004273 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 }
4275
4276 synchronized (mWindowMap) {
4277 mKeyWaiter.setEventDispatchingLocked(enabled);
4278 }
4279 }
Romain Guy06882f82009-06-10 13:36:04 -07004280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 /**
4282 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004283 *
4284 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 * {@link SystemClock#uptimeMillis()} as the timebase.)
4286 * @param sync If true, wait for the event to be completed before returning to the caller.
4287 * @return Returns true if event was dispatched, false if it was dropped for any reason
4288 */
4289 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4290 long downTime = ev.getDownTime();
4291 long eventTime = ev.getEventTime();
4292
4293 int action = ev.getAction();
4294 int code = ev.getKeyCode();
4295 int repeatCount = ev.getRepeatCount();
4296 int metaState = ev.getMetaState();
4297 int deviceId = ev.getDeviceId();
4298 int scancode = ev.getScanCode();
4299
4300 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4301 if (downTime == 0) downTime = eventTime;
4302
4303 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004304 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004306 final int pid = Binder.getCallingPid();
4307 final int uid = Binder.getCallingUid();
4308 final long ident = Binder.clearCallingIdentity();
4309 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004311 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004313 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004314 switch (result) {
4315 case INJECT_NO_PERMISSION:
4316 throw new SecurityException(
4317 "Injecting to another application requires INJECT_EVENT permission");
4318 case INJECT_SUCCEEDED:
4319 return true;
4320 }
4321 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 }
4323
4324 /**
4325 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004326 *
4327 * @param ev A motion event describing the pointer (touch) action. (As noted in
4328 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 * {@link SystemClock#uptimeMillis()} as the timebase.)
4330 * @param sync If true, wait for the event to be completed before returning to the caller.
4331 * @return Returns true if event was dispatched, false if it was dropped for any reason
4332 */
4333 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004334 final int pid = Binder.getCallingPid();
4335 final int uid = Binder.getCallingUid();
4336 final long ident = Binder.clearCallingIdentity();
4337 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004339 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004341 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004342 switch (result) {
4343 case INJECT_NO_PERMISSION:
4344 throw new SecurityException(
4345 "Injecting to another application requires INJECT_EVENT permission");
4346 case INJECT_SUCCEEDED:
4347 return true;
4348 }
4349 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 }
Romain Guy06882f82009-06-10 13:36:04 -07004351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352 /**
4353 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004354 *
4355 * @param ev A motion event describing the trackball action. (As noted in
4356 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 * {@link SystemClock#uptimeMillis()} as the timebase.)
4358 * @param sync If true, wait for the event to be completed before returning to the caller.
4359 * @return Returns true if event was dispatched, false if it was dropped for any reason
4360 */
4361 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004362 final int pid = Binder.getCallingPid();
4363 final int uid = Binder.getCallingUid();
4364 final long ident = Binder.clearCallingIdentity();
4365 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004367 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004369 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004370 switch (result) {
4371 case INJECT_NO_PERMISSION:
4372 throw new SecurityException(
4373 "Injecting to another application requires INJECT_EVENT permission");
4374 case INJECT_SUCCEEDED:
4375 return true;
4376 }
4377 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 }
Romain Guy06882f82009-06-10 13:36:04 -07004379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 private WindowState getFocusedWindow() {
4381 synchronized (mWindowMap) {
4382 return getFocusedWindowLocked();
4383 }
4384 }
4385
4386 private WindowState getFocusedWindowLocked() {
4387 return mCurrentFocus;
4388 }
Romain Guy06882f82009-06-10 13:36:04 -07004389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 /**
4391 * This class holds the state for dispatching key events. This state
4392 * is protected by the KeyWaiter instance, NOT by the window lock. You
4393 * can be holding the main window lock while acquire the KeyWaiter lock,
4394 * but not the other way around.
4395 */
4396 final class KeyWaiter {
4397 // NOSHIP debugging
4398 public class DispatchState {
4399 private KeyEvent event;
4400 private WindowState focus;
4401 private long time;
4402 private WindowState lastWin;
4403 private IBinder lastBinder;
4404 private boolean finished;
4405 private boolean gotFirstWindow;
4406 private boolean eventDispatching;
4407 private long timeToSwitch;
4408 private boolean wasFrozen;
4409 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004410 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4413 focus = theFocus;
4414 event = theEvent;
4415 time = System.currentTimeMillis();
4416 // snapshot KeyWaiter state
4417 lastWin = mLastWin;
4418 lastBinder = mLastBinder;
4419 finished = mFinished;
4420 gotFirstWindow = mGotFirstWindow;
4421 eventDispatching = mEventDispatching;
4422 timeToSwitch = mTimeToSwitch;
4423 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004424 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 // cache the paused state at ctor time as well
4426 if (theFocus == null || theFocus.mToken == null) {
4427 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4428 focusPaused = false;
4429 } else {
4430 focusPaused = theFocus.mToken.paused;
4431 }
4432 }
Romain Guy06882f82009-06-10 13:36:04 -07004433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 public String toString() {
4435 return "{{" + event + " to " + focus + " @ " + time
4436 + " lw=" + lastWin + " lb=" + lastBinder
4437 + " fin=" + finished + " gfw=" + gotFirstWindow
4438 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004439 + " wf=" + wasFrozen + " fp=" + focusPaused
4440 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 }
4442 };
4443 private DispatchState mDispatchState = null;
4444 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4445 mDispatchState = new DispatchState(theEvent, theFocus);
4446 }
4447 // END NOSHIP
4448
4449 public static final int RETURN_NOTHING = 0;
4450 public static final int RETURN_PENDING_POINTER = 1;
4451 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453 final Object SKIP_TARGET_TOKEN = new Object();
4454 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 private WindowState mLastWin = null;
4457 private IBinder mLastBinder = null;
4458 private boolean mFinished = true;
4459 private boolean mGotFirstWindow = false;
4460 private boolean mEventDispatching = true;
4461 private long mTimeToSwitch = 0;
4462 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 // Target of Motion events
4465 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467 // Windows above the target who would like to receive an "outside"
4468 // touch event for any down events outside of them.
4469 WindowState mOutsideTouchTargets;
4470
4471 /**
4472 * Wait for the last event dispatch to complete, then find the next
4473 * target that should receive the given event and wait for that one
4474 * to be ready to receive it.
4475 */
4476 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4477 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004478 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 long startTime = SystemClock.uptimeMillis();
4480 long keyDispatchingTimeout = 5 * 1000;
4481 long waitedFor = 0;
4482
4483 while (true) {
4484 // Figure out which window we care about. It is either the
4485 // last window we are waiting to have process the event or,
4486 // if none, then the next window we think the event should go
4487 // to. Note: we retrieve mLastWin outside of the lock, so
4488 // it may change before we lock. Thus we must check it again.
4489 WindowState targetWin = mLastWin;
4490 boolean targetIsNew = targetWin == null;
4491 if (DEBUG_INPUT) Log.v(
4492 TAG, "waitForLastKey: mFinished=" + mFinished +
4493 ", mLastWin=" + mLastWin);
4494 if (targetIsNew) {
4495 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004496 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004497 if (target == SKIP_TARGET_TOKEN) {
4498 // The user has pressed a special key, and we are
4499 // dropping all pending events before it.
4500 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4501 + " " + nextMotion);
4502 return null;
4503 }
4504 if (target == CONSUMED_EVENT_TOKEN) {
4505 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4506 + " " + nextMotion);
4507 return target;
4508 }
4509 targetWin = (WindowState)target;
4510 }
Romain Guy06882f82009-06-10 13:36:04 -07004511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 // Now: is it okay to send the next event to this window?
4515 synchronized (this) {
4516 // First: did we come here based on the last window not
4517 // being null, but it changed by the time we got here?
4518 // If so, try again.
4519 if (!targetIsNew && mLastWin == null) {
4520 continue;
4521 }
Romain Guy06882f82009-06-10 13:36:04 -07004522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 // We never dispatch events if not finished with the
4524 // last one, or the display is frozen.
4525 if (mFinished && !mDisplayFrozen) {
4526 // If event dispatching is disabled, then we
4527 // just consume the events.
4528 if (!mEventDispatching) {
4529 if (DEBUG_INPUT) Log.v(TAG,
4530 "Skipping event; dispatching disabled: "
4531 + nextKey + " " + nextMotion);
4532 return null;
4533 }
4534 if (targetWin != null) {
4535 // If this is a new target, and that target is not
4536 // paused or unresponsive, then all looks good to
4537 // handle the event.
4538 if (targetIsNew && !targetWin.mToken.paused) {
4539 return targetWin;
4540 }
Romain Guy06882f82009-06-10 13:36:04 -07004541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 // If we didn't find a target window, and there is no
4543 // focused app window, then just eat the events.
4544 } else if (mFocusedApp == null) {
4545 if (DEBUG_INPUT) Log.v(TAG,
4546 "Skipping event; no focused app: "
4547 + nextKey + " " + nextMotion);
4548 return null;
4549 }
4550 }
Romain Guy06882f82009-06-10 13:36:04 -07004551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552 if (DEBUG_INPUT) Log.v(
4553 TAG, "Waiting for last key in " + mLastBinder
4554 + " target=" + targetWin
4555 + " mFinished=" + mFinished
4556 + " mDisplayFrozen=" + mDisplayFrozen
4557 + " targetIsNew=" + targetIsNew
4558 + " paused="
4559 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004560 + " mFocusedApp=" + mFocusedApp
4561 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 targetApp = targetWin != null
4564 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004566 long curTimeout = keyDispatchingTimeout;
4567 if (mTimeToSwitch != 0) {
4568 long now = SystemClock.uptimeMillis();
4569 if (mTimeToSwitch <= now) {
4570 // If an app switch key has been pressed, and we have
4571 // waited too long for the current app to finish
4572 // processing keys, then wait no more!
4573 doFinishedKeyLocked(true);
4574 continue;
4575 }
4576 long switchTimeout = mTimeToSwitch - now;
4577 if (curTimeout > switchTimeout) {
4578 curTimeout = switchTimeout;
4579 }
4580 }
Romain Guy06882f82009-06-10 13:36:04 -07004581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 try {
4583 // after that continue
4584 // processing keys, so we don't get stuck.
4585 if (DEBUG_INPUT) Log.v(
4586 TAG, "Waiting for key dispatch: " + curTimeout);
4587 wait(curTimeout);
4588 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4589 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004590 + startTime + " switchTime=" + mTimeToSwitch
4591 + " target=" + targetWin + " mLW=" + mLastWin
4592 + " mLB=" + mLastBinder + " fin=" + mFinished
4593 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 } catch (InterruptedException e) {
4595 }
4596 }
4597
4598 // If we were frozen during configuration change, restart the
4599 // timeout checks from now; otherwise look at whether we timed
4600 // out before awakening.
4601 if (mWasFrozen) {
4602 waitedFor = 0;
4603 mWasFrozen = false;
4604 } else {
4605 waitedFor = SystemClock.uptimeMillis() - startTime;
4606 }
4607
4608 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4609 IApplicationToken at = null;
4610 synchronized (this) {
4611 Log.w(TAG, "Key dispatching timed out sending to " +
4612 (targetWin != null ? targetWin.mAttrs.getTitle()
4613 : "<null>"));
4614 // NOSHIP debugging
4615 Log.w(TAG, "Dispatch state: " + mDispatchState);
4616 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4617 // END NOSHIP
4618 //dump();
4619 if (targetWin != null) {
4620 at = targetWin.getAppToken();
4621 } else if (targetApp != null) {
4622 at = targetApp.appToken;
4623 }
4624 }
4625
4626 boolean abort = true;
4627 if (at != null) {
4628 try {
4629 long timeout = at.getKeyDispatchingTimeout();
4630 if (timeout > waitedFor) {
4631 // we did not wait the proper amount of time for this application.
4632 // set the timeout to be the real timeout and wait again.
4633 keyDispatchingTimeout = timeout - waitedFor;
4634 continue;
4635 } else {
4636 abort = at.keyDispatchingTimedOut();
4637 }
4638 } catch (RemoteException ex) {
4639 }
4640 }
4641
4642 synchronized (this) {
4643 if (abort && (mLastWin == targetWin || targetWin == null)) {
4644 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004645 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 if (DEBUG_INPUT) Log.v(TAG,
4647 "Window " + mLastWin +
4648 " timed out on key input");
4649 if (mLastWin.mToken.paused) {
4650 Log.w(TAG, "Un-pausing dispatching to this window");
4651 mLastWin.mToken.paused = false;
4652 }
4653 }
4654 if (mMotionTarget == targetWin) {
4655 mMotionTarget = null;
4656 }
4657 mLastWin = null;
4658 mLastBinder = null;
4659 if (failIfTimeout || targetWin == null) {
4660 return null;
4661 }
4662 } else {
4663 Log.w(TAG, "Continuing to wait for key to be dispatched");
4664 startTime = SystemClock.uptimeMillis();
4665 }
4666 }
4667 }
4668 }
4669 }
Romain Guy06882f82009-06-10 13:36:04 -07004670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004671 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004672 MotionEvent nextMotion, boolean isPointerEvent,
4673 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004676 if (nextKey != null) {
4677 // Find the target window for a normal key event.
4678 final int keycode = nextKey.getKeyCode();
4679 final int repeatCount = nextKey.getRepeatCount();
4680 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4681 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004684 if (callingUid == 0 ||
4685 mContext.checkPermission(
4686 android.Manifest.permission.INJECT_EVENTS,
4687 callingPid, callingUid)
4688 == PackageManager.PERMISSION_GRANTED) {
4689 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07004690 nextKey.getMetaState(), down, repeatCount,
4691 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004693 Log.w(TAG, "Event timeout during app switch: dropping "
4694 + nextKey);
4695 return SKIP_TARGET_TOKEN;
4696 }
Romain Guy06882f82009-06-10 13:36:04 -07004697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004700 WindowState focus = null;
4701 synchronized(mWindowMap) {
4702 focus = getFocusedWindowLocked();
4703 }
Romain Guy06882f82009-06-10 13:36:04 -07004704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004706
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004707 if (callingUid == 0 ||
4708 (focus != null && callingUid == focus.mSession.mUid) ||
4709 mContext.checkPermission(
4710 android.Manifest.permission.INJECT_EVENTS,
4711 callingPid, callingUid)
4712 == PackageManager.PERMISSION_GRANTED) {
4713 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07004714 keycode, nextKey.getMetaState(), down, repeatCount,
4715 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004716 return CONSUMED_EVENT_TOKEN;
4717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 }
Romain Guy06882f82009-06-10 13:36:04 -07004719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 } else if (!isPointerEvent) {
4723 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4724 if (!dispatch) {
4725 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4726 + nextMotion);
4727 return SKIP_TARGET_TOKEN;
4728 }
Romain Guy06882f82009-06-10 13:36:04 -07004729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004730 WindowState focus = null;
4731 synchronized(mWindowMap) {
4732 focus = getFocusedWindowLocked();
4733 }
Romain Guy06882f82009-06-10 13:36:04 -07004734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004735 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4736 return focus;
4737 }
Romain Guy06882f82009-06-10 13:36:04 -07004738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 if (nextMotion == null) {
4740 return SKIP_TARGET_TOKEN;
4741 }
Romain Guy06882f82009-06-10 13:36:04 -07004742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004743 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4744 KeyEvent.KEYCODE_UNKNOWN);
4745 if (!dispatch) {
4746 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4747 + nextMotion);
4748 return SKIP_TARGET_TOKEN;
4749 }
Romain Guy06882f82009-06-10 13:36:04 -07004750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004751 // Find the target window for a pointer event.
4752 int action = nextMotion.getAction();
4753 final float xf = nextMotion.getX();
4754 final float yf = nextMotion.getY();
4755 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 final boolean screenWasOff = qev != null
4758 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07004759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07004761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004762 synchronized(mWindowMap) {
4763 synchronized (this) {
4764 if (action == MotionEvent.ACTION_DOWN) {
4765 if (mMotionTarget != null) {
4766 // this is weird, we got a pen down, but we thought it was
4767 // already down!
4768 // XXX: We should probably send an ACTION_UP to the current
4769 // target.
4770 Log.w(TAG, "Pointer down received while already down in: "
4771 + mMotionTarget);
4772 mMotionTarget = null;
4773 }
Romain Guy06882f82009-06-10 13:36:04 -07004774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775 // ACTION_DOWN is special, because we need to lock next events to
4776 // the window we'll land onto.
4777 final int x = (int)xf;
4778 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07004779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 final ArrayList windows = mWindows;
4781 final int N = windows.size();
4782 WindowState topErrWindow = null;
4783 final Rect tmpRect = mTempRect;
4784 for (int i=N-1; i>=0; i--) {
4785 WindowState child = (WindowState)windows.get(i);
4786 //Log.i(TAG, "Checking dispatch to: " + child);
4787 final int flags = child.mAttrs.flags;
4788 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4789 if (topErrWindow == null) {
4790 topErrWindow = child;
4791 }
4792 }
4793 if (!child.isVisibleLw()) {
4794 //Log.i(TAG, "Not visible!");
4795 continue;
4796 }
4797 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4798 //Log.i(TAG, "Not touchable!");
4799 if ((flags & WindowManager.LayoutParams
4800 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4801 child.mNextOutsideTouch = mOutsideTouchTargets;
4802 mOutsideTouchTargets = child;
4803 }
4804 continue;
4805 }
4806 tmpRect.set(child.mFrame);
4807 if (child.mTouchableInsets == ViewTreeObserver
4808 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4809 // The touch is inside of the window if it is
4810 // inside the frame, AND the content part of that
4811 // frame that was given by the application.
4812 tmpRect.left += child.mGivenContentInsets.left;
4813 tmpRect.top += child.mGivenContentInsets.top;
4814 tmpRect.right -= child.mGivenContentInsets.right;
4815 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4816 } else if (child.mTouchableInsets == ViewTreeObserver
4817 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4818 // The touch is inside of the window if it is
4819 // inside the frame, AND the visible part of that
4820 // frame that was given by the application.
4821 tmpRect.left += child.mGivenVisibleInsets.left;
4822 tmpRect.top += child.mGivenVisibleInsets.top;
4823 tmpRect.right -= child.mGivenVisibleInsets.right;
4824 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
4825 }
4826 final int touchFlags = flags &
4827 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
4828 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
4829 if (tmpRect.contains(x, y) || touchFlags == 0) {
4830 //Log.i(TAG, "Using this target!");
4831 if (!screenWasOff || (flags &
4832 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
4833 mMotionTarget = child;
4834 } else {
4835 //Log.i(TAG, "Waking, skip!");
4836 mMotionTarget = null;
4837 }
4838 break;
4839 }
Romain Guy06882f82009-06-10 13:36:04 -07004840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 if ((flags & WindowManager.LayoutParams
4842 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4843 child.mNextOutsideTouch = mOutsideTouchTargets;
4844 mOutsideTouchTargets = child;
4845 //Log.i(TAG, "Adding to outside target list: " + child);
4846 }
4847 }
4848
4849 // if there's an error window but it's not accepting
4850 // focus (typically because it is not yet visible) just
4851 // wait for it -- any other focused window may in fact
4852 // be in ANR state.
4853 if (topErrWindow != null && mMotionTarget != topErrWindow) {
4854 mMotionTarget = null;
4855 }
4856 }
Romain Guy06882f82009-06-10 13:36:04 -07004857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004858 target = mMotionTarget;
4859 }
4860 }
Romain Guy06882f82009-06-10 13:36:04 -07004861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004862 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07004863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004864 // Pointer events are a little different -- if there isn't a
4865 // target found for any event, then just drop it.
4866 return target != null ? target : SKIP_TARGET_TOKEN;
4867 }
Romain Guy06882f82009-06-10 13:36:04 -07004868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 boolean checkShouldDispatchKey(int keycode) {
4870 synchronized (this) {
4871 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
4872 mTimeToSwitch = 0;
4873 return true;
4874 }
4875 if (mTimeToSwitch != 0
4876 && mTimeToSwitch < SystemClock.uptimeMillis()) {
4877 return false;
4878 }
4879 return true;
4880 }
4881 }
Romain Guy06882f82009-06-10 13:36:04 -07004882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 void bindTargetWindowLocked(WindowState win,
4884 int pendingWhat, QueuedEvent pendingMotion) {
4885 synchronized (this) {
4886 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
4887 }
4888 }
Romain Guy06882f82009-06-10 13:36:04 -07004889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 void bindTargetWindowLocked(WindowState win) {
4891 synchronized (this) {
4892 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
4893 }
4894 }
4895
4896 void bindTargetWindowLockedLocked(WindowState win,
4897 int pendingWhat, QueuedEvent pendingMotion) {
4898 mLastWin = win;
4899 mLastBinder = win.mClient.asBinder();
4900 mFinished = false;
4901 if (pendingMotion != null) {
4902 final Session s = win.mSession;
4903 if (pendingWhat == RETURN_PENDING_POINTER) {
4904 releasePendingPointerLocked(s);
4905 s.mPendingPointerMove = pendingMotion;
4906 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07004907 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004908 "bindTargetToWindow " + s.mPendingPointerMove);
4909 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
4910 releasePendingTrackballLocked(s);
4911 s.mPendingTrackballMove = pendingMotion;
4912 s.mPendingTrackballWindow = win;
4913 }
4914 }
4915 }
Romain Guy06882f82009-06-10 13:36:04 -07004916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004917 void releasePendingPointerLocked(Session s) {
4918 if (DEBUG_INPUT) Log.v(TAG,
4919 "releasePendingPointer " + s.mPendingPointerMove);
4920 if (s.mPendingPointerMove != null) {
4921 mQueue.recycleEvent(s.mPendingPointerMove);
4922 s.mPendingPointerMove = null;
4923 }
4924 }
Romain Guy06882f82009-06-10 13:36:04 -07004925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 void releasePendingTrackballLocked(Session s) {
4927 if (s.mPendingTrackballMove != null) {
4928 mQueue.recycleEvent(s.mPendingTrackballMove);
4929 s.mPendingTrackballMove = null;
4930 }
4931 }
Romain Guy06882f82009-06-10 13:36:04 -07004932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933 MotionEvent finishedKey(Session session, IWindow client, boolean force,
4934 int returnWhat) {
4935 if (DEBUG_INPUT) Log.v(
4936 TAG, "finishedKey: client=" + client + ", force=" + force);
4937
4938 if (client == null) {
4939 return null;
4940 }
4941
4942 synchronized (this) {
4943 if (DEBUG_INPUT) Log.v(
4944 TAG, "finishedKey: client=" + client.asBinder()
4945 + ", force=" + force + ", last=" + mLastBinder
4946 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
4947
4948 QueuedEvent qev = null;
4949 WindowState win = null;
4950 if (returnWhat == RETURN_PENDING_POINTER) {
4951 qev = session.mPendingPointerMove;
4952 win = session.mPendingPointerWindow;
4953 session.mPendingPointerMove = null;
4954 session.mPendingPointerWindow = null;
4955 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
4956 qev = session.mPendingTrackballMove;
4957 win = session.mPendingTrackballWindow;
4958 session.mPendingTrackballMove = null;
4959 session.mPendingTrackballWindow = null;
4960 }
Romain Guy06882f82009-06-10 13:36:04 -07004961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962 if (mLastBinder == client.asBinder()) {
4963 if (DEBUG_INPUT) Log.v(
4964 TAG, "finishedKey: last paused="
4965 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
4966 if (mLastWin != null && (!mLastWin.mToken.paused || force
4967 || !mEventDispatching)) {
4968 doFinishedKeyLocked(false);
4969 } else {
4970 // Make sure to wake up anyone currently waiting to
4971 // dispatch a key, so they can re-evaluate their
4972 // current situation.
4973 mFinished = true;
4974 notifyAll();
4975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 }
Romain Guy06882f82009-06-10 13:36:04 -07004977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978 if (qev != null) {
4979 MotionEvent res = (MotionEvent)qev.event;
4980 if (DEBUG_INPUT) Log.v(TAG,
4981 "Returning pending motion: " + res);
4982 mQueue.recycleEvent(qev);
4983 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
4984 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
4985 }
4986 return res;
4987 }
4988 return null;
4989 }
4990 }
4991
4992 void tickle() {
4993 synchronized (this) {
4994 notifyAll();
4995 }
4996 }
Romain Guy06882f82009-06-10 13:36:04 -07004997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004998 void handleNewWindowLocked(WindowState newWindow) {
4999 if (!newWindow.canReceiveKeys()) {
5000 return;
5001 }
5002 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005003 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 TAG, "New key dispatch window: win="
5005 + newWindow.mClient.asBinder()
5006 + ", last=" + mLastBinder
5007 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5008 + "), finished=" + mFinished + ", paused="
5009 + newWindow.mToken.paused);
5010
5011 // Displaying a window implicitly causes dispatching to
5012 // be unpaused. (This is to protect against bugs if someone
5013 // pauses dispatching but forgets to resume.)
5014 newWindow.mToken.paused = false;
5015
5016 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017
5018 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5019 if (DEBUG_INPUT) Log.v(TAG,
5020 "New SYSTEM_ERROR window; resetting state");
5021 mLastWin = null;
5022 mLastBinder = null;
5023 mMotionTarget = null;
5024 mFinished = true;
5025 } else if (mLastWin != null) {
5026 // If the new window is above the window we are
5027 // waiting on, then stop waiting and let key dispatching
5028 // start on the new guy.
5029 if (DEBUG_INPUT) Log.v(
5030 TAG, "Last win layer=" + mLastWin.mLayer
5031 + ", new win layer=" + newWindow.mLayer);
5032 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005033 // The new window is above the old; finish pending input to the last
5034 // window and start directing it to the new one.
5035 mLastWin.mToken.paused = false;
5036 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005038 // Either the new window is lower, so there is no need to wake key waiters,
5039 // or we just finished key input to the previous window, which implicitly
5040 // notified the key waiters. In both cases, we don't need to issue the
5041 // notification here.
5042 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 }
5044
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005045 // Now that we've put a new window state in place, make the event waiter
5046 // take notice and retarget its attentions.
5047 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048 }
5049 }
5050
5051 void pauseDispatchingLocked(WindowToken token) {
5052 synchronized (this)
5053 {
5054 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5055 token.paused = true;
5056
5057 /*
5058 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5059 mPaused = true;
5060 } else {
5061 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005062 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005064 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005066 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 }
5068 }
5069 */
5070 }
5071 }
5072
5073 void resumeDispatchingLocked(WindowToken token) {
5074 synchronized (this) {
5075 if (token.paused) {
5076 if (DEBUG_INPUT) Log.v(
5077 TAG, "Resuming WindowToken " + token
5078 + ", last=" + mLastBinder
5079 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5080 + "), finished=" + mFinished + ", paused="
5081 + token.paused);
5082 token.paused = false;
5083 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5084 doFinishedKeyLocked(true);
5085 } else {
5086 notifyAll();
5087 }
5088 }
5089 }
5090 }
5091
5092 void setEventDispatchingLocked(boolean enabled) {
5093 synchronized (this) {
5094 mEventDispatching = enabled;
5095 notifyAll();
5096 }
5097 }
Romain Guy06882f82009-06-10 13:36:04 -07005098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099 void appSwitchComing() {
5100 synchronized (this) {
5101 // Don't wait for more than .5 seconds for app to finish
5102 // processing the pending events.
5103 long now = SystemClock.uptimeMillis() + 500;
5104 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5105 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5106 mTimeToSwitch = now;
5107 }
5108 notifyAll();
5109 }
5110 }
Romain Guy06882f82009-06-10 13:36:04 -07005111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 private final void doFinishedKeyLocked(boolean doRecycle) {
5113 if (mLastWin != null) {
5114 releasePendingPointerLocked(mLastWin.mSession);
5115 releasePendingTrackballLocked(mLastWin.mSession);
5116 }
Romain Guy06882f82009-06-10 13:36:04 -07005117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 if (mLastWin == null || !mLastWin.mToken.paused
5119 || !mLastWin.isVisibleLw()) {
5120 // If the current window has been paused, we aren't -really-
5121 // finished... so let the waiters still wait.
5122 mLastWin = null;
5123 mLastBinder = null;
5124 }
5125 mFinished = true;
5126 notifyAll();
5127 }
5128 }
5129
5130 private class KeyQ extends KeyInputQueue
5131 implements KeyInputQueue.FilterCallback {
5132 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005134 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005135 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005136 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5137 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5138 "KEEP_SCREEN_ON_FLAG");
5139 mHoldingScreen.setReferenceCounted(false);
5140 }
5141
5142 @Override
5143 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5144 if (mPolicy.preprocessInputEventTq(event)) {
5145 return true;
5146 }
Romain Guy06882f82009-06-10 13:36:04 -07005147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 switch (event.type) {
5149 case RawInputEvent.EV_KEY: {
5150 // XXX begin hack
5151 if (DEBUG) {
5152 if (event.keycode == KeyEvent.KEYCODE_G) {
5153 if (event.value != 0) {
5154 // G down
5155 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5156 }
5157 return false;
5158 }
5159 if (event.keycode == KeyEvent.KEYCODE_D) {
5160 if (event.value != 0) {
5161 //dump();
5162 }
5163 return false;
5164 }
5165 }
5166 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 boolean screenIsOff = !mPowerManager.screenIsOn();
5169 boolean screenIsDim = !mPowerManager.screenIsBright();
5170 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5173 mPowerManager.goToSleep(event.when);
5174 }
5175
5176 if (screenIsOff) {
5177 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5178 }
5179 if (screenIsDim) {
5180 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5181 }
5182 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5183 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005184 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 }
Romain Guy06882f82009-06-10 13:36:04 -07005186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005187 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5188 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5189 filterQueue(this);
5190 mKeyWaiter.appSwitchComing();
5191 }
5192 return true;
5193 } else {
5194 return false;
5195 }
5196 }
Romain Guy06882f82009-06-10 13:36:04 -07005197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005198 case RawInputEvent.EV_REL: {
5199 boolean screenIsOff = !mPowerManager.screenIsOn();
5200 boolean screenIsDim = !mPowerManager.screenIsBright();
5201 if (screenIsOff) {
5202 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5203 device.classes, event)) {
5204 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5205 return false;
5206 }
5207 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5208 }
5209 if (screenIsDim) {
5210 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5211 }
5212 return true;
5213 }
Romain Guy06882f82009-06-10 13:36:04 -07005214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215 case RawInputEvent.EV_ABS: {
5216 boolean screenIsOff = !mPowerManager.screenIsOn();
5217 boolean screenIsDim = !mPowerManager.screenIsBright();
5218 if (screenIsOff) {
5219 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5220 device.classes, event)) {
5221 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5222 return false;
5223 }
5224 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5225 }
5226 if (screenIsDim) {
5227 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5228 }
5229 return true;
5230 }
Romain Guy06882f82009-06-10 13:36:04 -07005231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005232 default:
5233 return true;
5234 }
5235 }
5236
5237 public int filterEvent(QueuedEvent ev) {
5238 switch (ev.classType) {
5239 case RawInputEvent.CLASS_KEYBOARD:
5240 KeyEvent ke = (KeyEvent)ev.event;
5241 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5242 Log.w(TAG, "Dropping movement key during app switch: "
5243 + ke.getKeyCode() + ", action=" + ke.getAction());
5244 return FILTER_REMOVE;
5245 }
5246 return FILTER_ABORT;
5247 default:
5248 return FILTER_KEEP;
5249 }
5250 }
Romain Guy06882f82009-06-10 13:36:04 -07005251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 /**
5253 * Must be called with the main window manager lock held.
5254 */
5255 void setHoldScreenLocked(boolean holding) {
5256 boolean state = mHoldingScreen.isHeld();
5257 if (holding != state) {
5258 if (holding) {
5259 mHoldingScreen.acquire();
5260 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005261 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 mHoldingScreen.release();
5263 }
5264 }
5265 }
Michael Chan53071d62009-05-13 17:29:48 -07005266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267
5268 public boolean detectSafeMode() {
5269 mSafeMode = mPolicy.detectSafeMode();
5270 return mSafeMode;
5271 }
Romain Guy06882f82009-06-10 13:36:04 -07005272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005273 public void systemReady() {
5274 mPolicy.systemReady();
5275 }
Romain Guy06882f82009-06-10 13:36:04 -07005276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 private final class InputDispatcherThread extends Thread {
5278 // Time to wait when there is nothing to do: 9999 seconds.
5279 static final int LONG_WAIT=9999*1000;
5280
5281 public InputDispatcherThread() {
5282 super("InputDispatcher");
5283 }
Romain Guy06882f82009-06-10 13:36:04 -07005284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 @Override
5286 public void run() {
5287 while (true) {
5288 try {
5289 process();
5290 } catch (Exception e) {
5291 Log.e(TAG, "Exception in input dispatcher", e);
5292 }
5293 }
5294 }
Romain Guy06882f82009-06-10 13:36:04 -07005295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 private void process() {
5297 android.os.Process.setThreadPriority(
5298 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 // The last key event we saw
5301 KeyEvent lastKey = null;
5302
5303 // Last keydown time for auto-repeating keys
5304 long lastKeyTime = SystemClock.uptimeMillis();
5305 long nextKeyTime = lastKeyTime+LONG_WAIT;
5306
Romain Guy06882f82009-06-10 13:36:04 -07005307 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 int keyRepeatCount = 0;
5309
5310 // Need to report that configuration has changed?
5311 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 while (true) {
5314 long curTime = SystemClock.uptimeMillis();
5315
5316 if (DEBUG_INPUT) Log.v(
5317 TAG, "Waiting for next key: now=" + curTime
5318 + ", repeat @ " + nextKeyTime);
5319
5320 // Retrieve next event, waiting only as long as the next
5321 // repeat timeout. If the configuration has changed, then
5322 // don't wait at all -- we'll report the change as soon as
5323 // we have processed all events.
5324 QueuedEvent ev = mQueue.getEvent(
5325 (int)((!configChanged && curTime < nextKeyTime)
5326 ? (nextKeyTime-curTime) : 0));
5327
5328 if (DEBUG_INPUT && ev != null) Log.v(
5329 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5330
Michael Chan53071d62009-05-13 17:29:48 -07005331 if (MEASURE_LATENCY) {
5332 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5333 }
5334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005335 try {
5336 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005337 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 int eventType;
5339 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5340 eventType = eventType((MotionEvent)ev.event);
5341 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5342 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5343 eventType = LocalPowerManager.BUTTON_EVENT;
5344 } else {
5345 eventType = LocalPowerManager.OTHER_EVENT;
5346 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005347 try {
Michael Chan53071d62009-05-13 17:29:48 -07005348 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005349 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005350 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005351 mBatteryStats.noteInputEvent();
5352 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005353 } catch (RemoteException e) {
5354 // Ignore
5355 }
Michael Chane10de972009-05-18 11:24:50 -07005356
5357 if (eventType != TOUCH_EVENT
5358 && eventType != LONG_TOUCH_EVENT
5359 && eventType != CHEEK_EVENT) {
5360 mPowerManager.userActivity(curTime, false,
5361 eventType, false);
5362 } else if (mLastTouchEventType != eventType
5363 || (curTime - mLastUserActivityCallTime)
5364 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5365 mLastUserActivityCallTime = curTime;
5366 mLastTouchEventType = eventType;
5367 mPowerManager.userActivity(curTime, false,
5368 eventType, false);
5369 }
5370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005371 switch (ev.classType) {
5372 case RawInputEvent.CLASS_KEYBOARD:
5373 KeyEvent ke = (KeyEvent)ev.event;
5374 if (ke.isDown()) {
5375 lastKey = ke;
5376 keyRepeatCount = 0;
5377 lastKeyTime = curTime;
5378 nextKeyTime = lastKeyTime
5379 + KEY_REPEAT_FIRST_DELAY;
5380 if (DEBUG_INPUT) Log.v(
5381 TAG, "Received key down: first repeat @ "
5382 + nextKeyTime);
5383 } else {
5384 lastKey = null;
5385 // Arbitrary long timeout.
5386 lastKeyTime = curTime;
5387 nextKeyTime = curTime + LONG_WAIT;
5388 if (DEBUG_INPUT) Log.v(
5389 TAG, "Received key up: ignore repeat @ "
5390 + nextKeyTime);
5391 }
5392 dispatchKey((KeyEvent)ev.event, 0, 0);
5393 mQueue.recycleEvent(ev);
5394 break;
5395 case RawInputEvent.CLASS_TOUCHSCREEN:
5396 //Log.i(TAG, "Read next event " + ev);
5397 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5398 break;
5399 case RawInputEvent.CLASS_TRACKBALL:
5400 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5401 break;
5402 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5403 configChanged = true;
5404 break;
5405 default:
5406 mQueue.recycleEvent(ev);
5407 break;
5408 }
Romain Guy06882f82009-06-10 13:36:04 -07005409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 } else if (configChanged) {
5411 configChanged = false;
5412 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 } else if (lastKey != null) {
5415 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 // Timeout occurred while key was down. If it is at or
5418 // past the key repeat time, dispatch the repeat.
5419 if (DEBUG_INPUT) Log.v(
5420 TAG, "Key timeout: repeat=" + nextKeyTime
5421 + ", now=" + curTime);
5422 if (curTime < nextKeyTime) {
5423 continue;
5424 }
Romain Guy06882f82009-06-10 13:36:04 -07005425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 lastKeyTime = nextKeyTime;
5427 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5428 keyRepeatCount++;
5429 if (DEBUG_INPUT) Log.v(
5430 TAG, "Key repeat: count=" + keyRepeatCount
5431 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005432 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 } else {
5435 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 lastKeyTime = curTime;
5438 nextKeyTime = curTime + LONG_WAIT;
5439 }
Romain Guy06882f82009-06-10 13:36:04 -07005440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005441 } catch (Exception e) {
5442 Log.e(TAG,
5443 "Input thread received uncaught exception: " + e, e);
5444 }
5445 }
5446 }
5447 }
5448
5449 // -------------------------------------------------------------
5450 // Client Session State
5451 // -------------------------------------------------------------
5452
5453 private final class Session extends IWindowSession.Stub
5454 implements IBinder.DeathRecipient {
5455 final IInputMethodClient mClient;
5456 final IInputContext mInputContext;
5457 final int mUid;
5458 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005459 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 SurfaceSession mSurfaceSession;
5461 int mNumWindow = 0;
5462 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 /**
5465 * Current pointer move event being dispatched to client window... must
5466 * hold key lock to access.
5467 */
5468 QueuedEvent mPendingPointerMove;
5469 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471 /**
5472 * Current trackball move event being dispatched to client window... must
5473 * hold key lock to access.
5474 */
5475 QueuedEvent mPendingTrackballMove;
5476 WindowState mPendingTrackballWindow;
5477
5478 public Session(IInputMethodClient client, IInputContext inputContext) {
5479 mClient = client;
5480 mInputContext = inputContext;
5481 mUid = Binder.getCallingUid();
5482 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005483 StringBuilder sb = new StringBuilder();
5484 sb.append("Session{");
5485 sb.append(Integer.toHexString(System.identityHashCode(this)));
5486 sb.append(" uid ");
5487 sb.append(mUid);
5488 sb.append("}");
5489 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 synchronized (mWindowMap) {
5492 if (mInputMethodManager == null && mHaveInputMethods) {
5493 IBinder b = ServiceManager.getService(
5494 Context.INPUT_METHOD_SERVICE);
5495 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5496 }
5497 }
5498 long ident = Binder.clearCallingIdentity();
5499 try {
5500 // Note: it is safe to call in to the input method manager
5501 // here because we are not holding our lock.
5502 if (mInputMethodManager != null) {
5503 mInputMethodManager.addClient(client, inputContext,
5504 mUid, mPid);
5505 } else {
5506 client.setUsingInputMethod(false);
5507 }
5508 client.asBinder().linkToDeath(this, 0);
5509 } catch (RemoteException e) {
5510 // The caller has died, so we can just forget about this.
5511 try {
5512 if (mInputMethodManager != null) {
5513 mInputMethodManager.removeClient(client);
5514 }
5515 } catch (RemoteException ee) {
5516 }
5517 } finally {
5518 Binder.restoreCallingIdentity(ident);
5519 }
5520 }
Romain Guy06882f82009-06-10 13:36:04 -07005521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 @Override
5523 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5524 throws RemoteException {
5525 try {
5526 return super.onTransact(code, data, reply, flags);
5527 } catch (RuntimeException e) {
5528 // Log all 'real' exceptions thrown to the caller
5529 if (!(e instanceof SecurityException)) {
5530 Log.e(TAG, "Window Session Crash", e);
5531 }
5532 throw e;
5533 }
5534 }
5535
5536 public void binderDied() {
5537 // Note: it is safe to call in to the input method manager
5538 // here because we are not holding our lock.
5539 try {
5540 if (mInputMethodManager != null) {
5541 mInputMethodManager.removeClient(mClient);
5542 }
5543 } catch (RemoteException e) {
5544 }
5545 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005546 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 mClientDead = true;
5548 killSessionLocked();
5549 }
5550 }
5551
5552 public int add(IWindow window, WindowManager.LayoutParams attrs,
5553 int viewVisibility, Rect outContentInsets) {
5554 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5555 }
Romain Guy06882f82009-06-10 13:36:04 -07005556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005557 public void remove(IWindow window) {
5558 removeWindow(this, window);
5559 }
Romain Guy06882f82009-06-10 13:36:04 -07005560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005561 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5562 int requestedWidth, int requestedHeight, int viewFlags,
5563 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5564 Rect outVisibleInsets, Surface outSurface) {
5565 return relayoutWindow(this, window, attrs,
5566 requestedWidth, requestedHeight, viewFlags, insetsPending,
5567 outFrame, outContentInsets, outVisibleInsets, outSurface);
5568 }
Romain Guy06882f82009-06-10 13:36:04 -07005569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 public void setTransparentRegion(IWindow window, Region region) {
5571 setTransparentRegionWindow(this, window, region);
5572 }
Romain Guy06882f82009-06-10 13:36:04 -07005573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 public void setInsets(IWindow window, int touchableInsets,
5575 Rect contentInsets, Rect visibleInsets) {
5576 setInsetsWindow(this, window, touchableInsets, contentInsets,
5577 visibleInsets);
5578 }
Romain Guy06882f82009-06-10 13:36:04 -07005579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5581 getWindowDisplayFrame(this, window, outDisplayFrame);
5582 }
Romain Guy06882f82009-06-10 13:36:04 -07005583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005584 public void finishDrawing(IWindow window) {
5585 if (localLOGV) Log.v(
5586 TAG, "IWindow finishDrawing called for " + window);
5587 finishDrawingWindow(this, window);
5588 }
5589
5590 public void finishKey(IWindow window) {
5591 if (localLOGV) Log.v(
5592 TAG, "IWindow finishKey called for " + window);
5593 mKeyWaiter.finishedKey(this, window, false,
5594 KeyWaiter.RETURN_NOTHING);
5595 }
5596
5597 public MotionEvent getPendingPointerMove(IWindow window) {
5598 if (localLOGV) Log.v(
5599 TAG, "IWindow getPendingMotionEvent called for " + window);
5600 return mKeyWaiter.finishedKey(this, window, false,
5601 KeyWaiter.RETURN_PENDING_POINTER);
5602 }
Romain Guy06882f82009-06-10 13:36:04 -07005603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 public MotionEvent getPendingTrackballMove(IWindow window) {
5605 if (localLOGV) Log.v(
5606 TAG, "IWindow getPendingMotionEvent called for " + window);
5607 return mKeyWaiter.finishedKey(this, window, false,
5608 KeyWaiter.RETURN_PENDING_TRACKBALL);
5609 }
5610
5611 public void setInTouchMode(boolean mode) {
5612 synchronized(mWindowMap) {
5613 mInTouchMode = mode;
5614 }
5615 }
5616
5617 public boolean getInTouchMode() {
5618 synchronized(mWindowMap) {
5619 return mInTouchMode;
5620 }
5621 }
5622
5623 public boolean performHapticFeedback(IWindow window, int effectId,
5624 boolean always) {
5625 synchronized(mWindowMap) {
5626 long ident = Binder.clearCallingIdentity();
5627 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005628 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 windowForClientLocked(this, window), effectId, always);
5630 } finally {
5631 Binder.restoreCallingIdentity(ident);
5632 }
5633 }
5634 }
Romain Guy06882f82009-06-10 13:36:04 -07005635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 void windowAddedLocked() {
5637 if (mSurfaceSession == null) {
5638 if (localLOGV) Log.v(
5639 TAG, "First window added to " + this + ", creating SurfaceSession");
5640 mSurfaceSession = new SurfaceSession();
5641 mSessions.add(this);
5642 }
5643 mNumWindow++;
5644 }
5645
5646 void windowRemovedLocked() {
5647 mNumWindow--;
5648 killSessionLocked();
5649 }
Romain Guy06882f82009-06-10 13:36:04 -07005650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 void killSessionLocked() {
5652 if (mNumWindow <= 0 && mClientDead) {
5653 mSessions.remove(this);
5654 if (mSurfaceSession != null) {
5655 if (localLOGV) Log.v(
5656 TAG, "Last window removed from " + this
5657 + ", destroying " + mSurfaceSession);
5658 try {
5659 mSurfaceSession.kill();
5660 } catch (Exception e) {
5661 Log.w(TAG, "Exception thrown when killing surface session "
5662 + mSurfaceSession + " in session " + this
5663 + ": " + e.toString());
5664 }
5665 mSurfaceSession = null;
5666 }
5667 }
5668 }
Romain Guy06882f82009-06-10 13:36:04 -07005669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005671 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5672 pw.print(" mClientDead="); pw.print(mClientDead);
5673 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5674 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5675 pw.print(prefix);
5676 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5677 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5678 }
5679 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5680 pw.print(prefix);
5681 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5682 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 }
5685
5686 @Override
5687 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005688 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 }
5690 }
5691
5692 // -------------------------------------------------------------
5693 // Client Window State
5694 // -------------------------------------------------------------
5695
5696 private final class WindowState implements WindowManagerPolicy.WindowState {
5697 final Session mSession;
5698 final IWindow mClient;
5699 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005700 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 AppWindowToken mAppToken;
5702 AppWindowToken mTargetAppToken;
5703 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5704 final DeathRecipient mDeathRecipient;
5705 final WindowState mAttachedWindow;
5706 final ArrayList mChildWindows = new ArrayList();
5707 final int mBaseLayer;
5708 final int mSubLayer;
5709 final boolean mLayoutAttached;
5710 final boolean mIsImWindow;
5711 int mViewVisibility;
5712 boolean mPolicyVisibility = true;
5713 boolean mPolicyVisibilityAfterAnim = true;
5714 boolean mAppFreezing;
5715 Surface mSurface;
5716 boolean mAttachedHidden; // is our parent window hidden?
5717 boolean mLastHidden; // was this window last hidden?
5718 int mRequestedWidth;
5719 int mRequestedHeight;
5720 int mLastRequestedWidth;
5721 int mLastRequestedHeight;
5722 int mReqXPos;
5723 int mReqYPos;
5724 int mLayer;
5725 int mAnimLayer;
5726 int mLastLayer;
5727 boolean mHaveFrame;
5728
5729 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005731 // Actual frame shown on-screen (may be modified by animation)
5732 final Rect mShownFrame = new Rect();
5733 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 /**
5736 * Insets that determine the actually visible area
5737 */
5738 final Rect mVisibleInsets = new Rect();
5739 final Rect mLastVisibleInsets = new Rect();
5740 boolean mVisibleInsetsChanged;
5741
5742 /**
5743 * Insets that are covered by system windows
5744 */
5745 final Rect mContentInsets = new Rect();
5746 final Rect mLastContentInsets = new Rect();
5747 boolean mContentInsetsChanged;
5748
5749 /**
5750 * Set to true if we are waiting for this window to receive its
5751 * given internal insets before laying out other windows based on it.
5752 */
5753 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07005754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 /**
5756 * These are the content insets that were given during layout for
5757 * this window, to be applied to windows behind it.
5758 */
5759 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 /**
5762 * These are the visible insets that were given during layout for
5763 * this window, to be applied to windows behind it.
5764 */
5765 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 /**
5768 * Flag indicating whether the touchable region should be adjusted by
5769 * the visible insets; if false the area outside the visible insets is
5770 * NOT touchable, so we must use those to adjust the frame during hit
5771 * tests.
5772 */
5773 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07005774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 // Current transformation being applied.
5776 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5777 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5778 float mHScale=1, mVScale=1;
5779 float mLastHScale=1, mLastVScale=1;
5780 final Matrix mTmpMatrix = new Matrix();
5781
5782 // "Real" frame that the application sees.
5783 final Rect mFrame = new Rect();
5784 final Rect mLastFrame = new Rect();
5785
5786 final Rect mContainingFrame = new Rect();
5787 final Rect mDisplayFrame = new Rect();
5788 final Rect mContentFrame = new Rect();
5789 final Rect mVisibleFrame = new Rect();
5790
5791 float mShownAlpha = 1;
5792 float mAlpha = 1;
5793 float mLastAlpha = 1;
5794
5795 // Set to true if, when the window gets displayed, it should perform
5796 // an enter animation.
5797 boolean mEnterAnimationPending;
5798
5799 // Currently running animation.
5800 boolean mAnimating;
5801 boolean mLocalAnimating;
5802 Animation mAnimation;
5803 boolean mAnimationIsEntrance;
5804 boolean mHasTransformation;
5805 boolean mHasLocalTransformation;
5806 final Transformation mTransformation = new Transformation();
5807
5808 // This is set after IWindowSession.relayout() has been called at
5809 // least once for the window. It allows us to detect the situation
5810 // where we don't yet have a surface, but should have one soon, so
5811 // we can give the window focus before waiting for the relayout.
5812 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07005813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 // This is set after the Surface has been created but before the
5815 // window has been drawn. During this time the surface is hidden.
5816 boolean mDrawPending;
5817
5818 // This is set after the window has finished drawing for the first
5819 // time but before its surface is shown. The surface will be
5820 // displayed when the next layout is run.
5821 boolean mCommitDrawPending;
5822
5823 // This is set during the time after the window's drawing has been
5824 // committed, and before its surface is actually shown. It is used
5825 // to delay showing the surface until all windows in a token are ready
5826 // to be shown.
5827 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07005828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005829 // Set when the window has been shown in the screen the first time.
5830 boolean mHasDrawn;
5831
5832 // Currently running an exit animation?
5833 boolean mExiting;
5834
5835 // Currently on the mDestroySurface list?
5836 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07005837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 // Completely remove from window manager after exit animation?
5839 boolean mRemoveOnExit;
5840
5841 // Set when the orientation is changing and this window has not yet
5842 // been updated for the new orientation.
5843 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07005844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005845 // Is this window now (or just being) removed?
5846 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07005847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005848 WindowState(Session s, IWindow c, WindowToken token,
5849 WindowState attachedWindow, WindowManager.LayoutParams a,
5850 int viewVisibility) {
5851 mSession = s;
5852 mClient = c;
5853 mToken = token;
5854 mAttrs.copyFrom(a);
5855 mViewVisibility = viewVisibility;
5856 DeathRecipient deathRecipient = new DeathRecipient();
5857 mAlpha = a.alpha;
5858 if (localLOGV) Log.v(
5859 TAG, "Window " + this + " client=" + c.asBinder()
5860 + " token=" + token + " (" + mAttrs.token + ")");
5861 try {
5862 c.asBinder().linkToDeath(deathRecipient, 0);
5863 } catch (RemoteException e) {
5864 mDeathRecipient = null;
5865 mAttachedWindow = null;
5866 mLayoutAttached = false;
5867 mIsImWindow = false;
5868 mBaseLayer = 0;
5869 mSubLayer = 0;
5870 return;
5871 }
5872 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07005873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
5875 mAttrs.type <= LAST_SUB_WINDOW)) {
5876 // The multiplier here is to reserve space for multiple
5877 // windows in the same type layer.
5878 mBaseLayer = mPolicy.windowTypeToLayerLw(
5879 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
5880 + TYPE_LAYER_OFFSET;
5881 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
5882 mAttachedWindow = attachedWindow;
5883 mAttachedWindow.mChildWindows.add(this);
5884 mLayoutAttached = mAttrs.type !=
5885 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
5886 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
5887 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5888 } else {
5889 // The multiplier here is to reserve space for multiple
5890 // windows in the same type layer.
5891 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
5892 * TYPE_LAYER_MULTIPLIER
5893 + TYPE_LAYER_OFFSET;
5894 mSubLayer = 0;
5895 mAttachedWindow = null;
5896 mLayoutAttached = false;
5897 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
5898 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5899 }
5900
5901 WindowState appWin = this;
5902 while (appWin.mAttachedWindow != null) {
5903 appWin = mAttachedWindow;
5904 }
5905 WindowToken appToken = appWin.mToken;
5906 while (appToken.appWindowToken == null) {
5907 WindowToken parent = mTokenMap.get(appToken.token);
5908 if (parent == null || appToken == parent) {
5909 break;
5910 }
5911 appToken = parent;
5912 }
The Android Open Source Project10592532009-03-18 17:39:46 -07005913 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005914 mAppToken = appToken.appWindowToken;
5915
5916 mSurface = null;
5917 mRequestedWidth = 0;
5918 mRequestedHeight = 0;
5919 mLastRequestedWidth = 0;
5920 mLastRequestedHeight = 0;
5921 mReqXPos = 0;
5922 mReqYPos = 0;
5923 mLayer = 0;
5924 mAnimLayer = 0;
5925 mLastLayer = 0;
5926 }
5927
5928 void attach() {
5929 if (localLOGV) Log.v(
5930 TAG, "Attaching " + this + " token=" + mToken
5931 + ", list=" + mToken.windows);
5932 mSession.windowAddedLocked();
5933 }
5934
5935 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
5936 mHaveFrame = true;
5937
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005938 final Rect container = mContainingFrame;
5939 container.set(pf);
5940
5941 final Rect display = mDisplayFrame;
5942 display.set(df);
5943
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07005944 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005945 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07005946 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
5947 display.intersect(mCompatibleScreenFrame);
5948 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005949 }
5950
5951 final int pw = container.right - container.left;
5952 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953
5954 int w,h;
5955 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
5956 w = mAttrs.width < 0 ? pw : mAttrs.width;
5957 h = mAttrs.height< 0 ? ph : mAttrs.height;
5958 } else {
5959 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
5960 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
5961 }
Romain Guy06882f82009-06-10 13:36:04 -07005962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005963 final Rect content = mContentFrame;
5964 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07005965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 final Rect visible = mVisibleFrame;
5967 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07005968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005969 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07005970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 //System.out.println("In: w=" + w + " h=" + h + " container=" +
5972 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
5973
5974 Gravity.apply(mAttrs.gravity, w, h, container,
5975 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
5976 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
5977
5978 //System.out.println("Out: " + mFrame);
5979
5980 // Now make sure the window fits in the overall display.
5981 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 // Make sure the content and visible frames are inside of the
5984 // final window frame.
5985 if (content.left < frame.left) content.left = frame.left;
5986 if (content.top < frame.top) content.top = frame.top;
5987 if (content.right > frame.right) content.right = frame.right;
5988 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
5989 if (visible.left < frame.left) visible.left = frame.left;
5990 if (visible.top < frame.top) visible.top = frame.top;
5991 if (visible.right > frame.right) visible.right = frame.right;
5992 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 final Rect contentInsets = mContentInsets;
5995 contentInsets.left = content.left-frame.left;
5996 contentInsets.top = content.top-frame.top;
5997 contentInsets.right = frame.right-content.right;
5998 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006000 final Rect visibleInsets = mVisibleInsets;
6001 visibleInsets.left = visible.left-frame.left;
6002 visibleInsets.top = visible.top-frame.top;
6003 visibleInsets.right = frame.right-visible.right;
6004 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 if (localLOGV) {
6007 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6008 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6009 Log.v(TAG, "Resolving (mRequestedWidth="
6010 + mRequestedWidth + ", mRequestedheight="
6011 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6012 + "): frame=" + mFrame.toShortString()
6013 + " ci=" + contentInsets.toShortString()
6014 + " vi=" + visibleInsets.toShortString());
6015 //}
6016 }
6017 }
Romain Guy06882f82009-06-10 13:36:04 -07006018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 public Rect getFrameLw() {
6020 return mFrame;
6021 }
6022
6023 public Rect getShownFrameLw() {
6024 return mShownFrame;
6025 }
6026
6027 public Rect getDisplayFrameLw() {
6028 return mDisplayFrame;
6029 }
6030
6031 public Rect getContentFrameLw() {
6032 return mContentFrame;
6033 }
6034
6035 public Rect getVisibleFrameLw() {
6036 return mVisibleFrame;
6037 }
6038
6039 public boolean getGivenInsetsPendingLw() {
6040 return mGivenInsetsPending;
6041 }
6042
6043 public Rect getGivenContentInsetsLw() {
6044 return mGivenContentInsets;
6045 }
Romain Guy06882f82009-06-10 13:36:04 -07006046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047 public Rect getGivenVisibleInsetsLw() {
6048 return mGivenVisibleInsets;
6049 }
Romain Guy06882f82009-06-10 13:36:04 -07006050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 public WindowManager.LayoutParams getAttrs() {
6052 return mAttrs;
6053 }
6054
6055 public int getSurfaceLayer() {
6056 return mLayer;
6057 }
Romain Guy06882f82009-06-10 13:36:04 -07006058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006059 public IApplicationToken getAppToken() {
6060 return mAppToken != null ? mAppToken.appToken : null;
6061 }
6062
6063 public boolean hasAppShownWindows() {
6064 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6065 }
6066
6067 public boolean hasAppStartingIcon() {
6068 return mAppToken != null ? (mAppToken.startingData != null) : false;
6069 }
6070
6071 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6072 return mAppToken != null ? mAppToken.startingWindow : null;
6073 }
6074
6075 public void setAnimation(Animation anim) {
6076 if (localLOGV) Log.v(
6077 TAG, "Setting animation in " + this + ": " + anim);
6078 mAnimating = false;
6079 mLocalAnimating = false;
6080 mAnimation = anim;
6081 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6082 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6083 }
6084
6085 public void clearAnimation() {
6086 if (mAnimation != null) {
6087 mAnimating = true;
6088 mLocalAnimating = false;
6089 mAnimation = null;
6090 }
6091 }
Romain Guy06882f82009-06-10 13:36:04 -07006092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 Surface createSurfaceLocked() {
6094 if (mSurface == null) {
6095 mDrawPending = true;
6096 mCommitDrawPending = false;
6097 mReadyToShow = false;
6098 if (mAppToken != null) {
6099 mAppToken.allDrawn = false;
6100 }
6101
6102 int flags = 0;
6103 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
6104 flags |= Surface.HARDWARE;
6105 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6106 flags |= Surface.GPU;
6107 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6108 flags |= Surface.PUSH_BUFFERS;
6109 }
6110
6111 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6112 flags |= Surface.SECURE;
6113 }
6114 if (DEBUG_VISIBILITY) Log.v(
6115 TAG, "Creating surface in session "
6116 + mSession.mSurfaceSession + " window " + this
6117 + " w=" + mFrame.width()
6118 + " h=" + mFrame.height() + " format="
6119 + mAttrs.format + " flags=" + flags);
6120
6121 int w = mFrame.width();
6122 int h = mFrame.height();
6123 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6124 // for a scaled surface, we always want the requested
6125 // size.
6126 w = mRequestedWidth;
6127 h = mRequestedHeight;
6128 }
6129
6130 try {
6131 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006132 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 0, w, h, mAttrs.format, flags);
6134 } catch (Surface.OutOfResourcesException e) {
6135 Log.w(TAG, "OutOfResourcesException creating surface");
6136 reclaimSomeSurfaceMemoryLocked(this, "create");
6137 return null;
6138 } catch (Exception e) {
6139 Log.e(TAG, "Exception creating surface", e);
6140 return null;
6141 }
Romain Guy06882f82009-06-10 13:36:04 -07006142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006143 if (localLOGV) Log.v(
6144 TAG, "Got surface: " + mSurface
6145 + ", set left=" + mFrame.left + " top=" + mFrame.top
6146 + ", animLayer=" + mAnimLayer);
6147 if (SHOW_TRANSACTIONS) {
6148 Log.i(TAG, ">>> OPEN TRANSACTION");
6149 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6150 + mAttrs.getTitle() + ") pos=(" +
6151 mFrame.left + "," + mFrame.top + ") (" +
6152 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6153 mAnimLayer + " HIDE");
6154 }
6155 Surface.openTransaction();
6156 try {
6157 try {
6158 mSurface.setPosition(mFrame.left, mFrame.top);
6159 mSurface.setLayer(mAnimLayer);
6160 mSurface.hide();
6161 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6162 mSurface.setFlags(Surface.SURFACE_DITHER,
6163 Surface.SURFACE_DITHER);
6164 }
6165 } catch (RuntimeException e) {
6166 Log.w(TAG, "Error creating surface in " + w, e);
6167 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6168 }
6169 mLastHidden = true;
6170 } finally {
6171 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6172 Surface.closeTransaction();
6173 }
6174 if (localLOGV) Log.v(
6175 TAG, "Created surface " + this);
6176 }
6177 return mSurface;
6178 }
Romain Guy06882f82009-06-10 13:36:04 -07006179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 void destroySurfaceLocked() {
6181 // Window is no longer on-screen, so can no longer receive
6182 // key events... if we were waiting for it to finish
6183 // handling a key event, the wait is over!
6184 mKeyWaiter.finishedKey(mSession, mClient, true,
6185 KeyWaiter.RETURN_NOTHING);
6186 mKeyWaiter.releasePendingPointerLocked(mSession);
6187 mKeyWaiter.releasePendingTrackballLocked(mSession);
6188
6189 if (mAppToken != null && this == mAppToken.startingWindow) {
6190 mAppToken.startingDisplayed = false;
6191 }
Romain Guy06882f82009-06-10 13:36:04 -07006192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006193 if (localLOGV) Log.v(
6194 TAG, "Window " + this
6195 + " destroying surface " + mSurface + ", session " + mSession);
6196 if (mSurface != null) {
6197 try {
6198 if (SHOW_TRANSACTIONS) {
6199 RuntimeException ex = new RuntimeException();
6200 ex.fillInStackTrace();
6201 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6202 + mAttrs.getTitle() + ")", ex);
6203 }
6204 mSurface.clear();
6205 } catch (RuntimeException e) {
6206 Log.w(TAG, "Exception thrown when destroying Window " + this
6207 + " surface " + mSurface + " session " + mSession
6208 + ": " + e.toString());
6209 }
6210 mSurface = null;
6211 mDrawPending = false;
6212 mCommitDrawPending = false;
6213 mReadyToShow = false;
6214
6215 int i = mChildWindows.size();
6216 while (i > 0) {
6217 i--;
6218 WindowState c = (WindowState)mChildWindows.get(i);
6219 c.mAttachedHidden = true;
6220 }
6221 }
6222 }
6223
6224 boolean finishDrawingLocked() {
6225 if (mDrawPending) {
6226 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6227 TAG, "finishDrawingLocked: " + mSurface);
6228 mCommitDrawPending = true;
6229 mDrawPending = false;
6230 return true;
6231 }
6232 return false;
6233 }
6234
6235 // This must be called while inside a transaction.
6236 void commitFinishDrawingLocked(long currentTime) {
6237 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6238 if (!mCommitDrawPending) {
6239 return;
6240 }
6241 mCommitDrawPending = false;
6242 mReadyToShow = true;
6243 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6244 final AppWindowToken atoken = mAppToken;
6245 if (atoken == null || atoken.allDrawn || starting) {
6246 performShowLocked();
6247 }
6248 }
6249
6250 // This must be called while inside a transaction.
6251 boolean performShowLocked() {
6252 if (DEBUG_VISIBILITY) {
6253 RuntimeException e = new RuntimeException();
6254 e.fillInStackTrace();
6255 Log.v(TAG, "performShow on " + this
6256 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6257 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6258 }
6259 if (mReadyToShow && isReadyForDisplay()) {
6260 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6261 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6262 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6263 + " during animation: policyVis=" + mPolicyVisibility
6264 + " attHidden=" + mAttachedHidden
6265 + " tok.hiddenRequested="
6266 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6267 + " tok.idden="
6268 + (mAppToken != null ? mAppToken.hidden : false)
6269 + " animating=" + mAnimating
6270 + " tok animating="
6271 + (mAppToken != null ? mAppToken.animating : false));
6272 if (!showSurfaceRobustlyLocked(this)) {
6273 return false;
6274 }
6275 mLastAlpha = -1;
6276 mHasDrawn = true;
6277 mLastHidden = false;
6278 mReadyToShow = false;
6279 enableScreenIfNeededLocked();
6280
6281 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006283 int i = mChildWindows.size();
6284 while (i > 0) {
6285 i--;
6286 WindowState c = (WindowState)mChildWindows.get(i);
6287 if (c.mSurface != null && c.mAttachedHidden) {
6288 c.mAttachedHidden = false;
6289 c.performShowLocked();
6290 }
6291 }
Romain Guy06882f82009-06-10 13:36:04 -07006292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006293 if (mAttrs.type != TYPE_APPLICATION_STARTING
6294 && mAppToken != null) {
6295 mAppToken.firstWindowDrawn = true;
6296 if (mAnimation == null && mAppToken.startingData != null) {
6297 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6298 + mToken
6299 + ": first real window is shown, no animation");
6300 mFinishedStarting.add(mAppToken);
6301 mH.sendEmptyMessage(H.FINISHED_STARTING);
6302 }
6303 mAppToken.updateReportedVisibilityLocked();
6304 }
6305 }
6306 return true;
6307 }
Romain Guy06882f82009-06-10 13:36:04 -07006308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309 // This must be called while inside a transaction. Returns true if
6310 // there is more animation to run.
6311 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6312 if (!mDisplayFrozen) {
6313 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6316 mHasTransformation = true;
6317 mHasLocalTransformation = true;
6318 if (!mLocalAnimating) {
6319 if (DEBUG_ANIM) Log.v(
6320 TAG, "Starting animation in " + this +
6321 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6322 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6323 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6324 mAnimation.setStartTime(currentTime);
6325 mLocalAnimating = true;
6326 mAnimating = true;
6327 }
6328 mTransformation.clear();
6329 final boolean more = mAnimation.getTransformation(
6330 currentTime, mTransformation);
6331 if (DEBUG_ANIM) Log.v(
6332 TAG, "Stepped animation in " + this +
6333 ": more=" + more + ", xform=" + mTransformation);
6334 if (more) {
6335 // we're not done!
6336 return true;
6337 }
6338 if (DEBUG_ANIM) Log.v(
6339 TAG, "Finished animation in " + this +
6340 " @ " + currentTime);
6341 mAnimation = null;
6342 //WindowManagerService.this.dump();
6343 }
6344 mHasLocalTransformation = false;
6345 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6346 && mAppToken.hasTransformation) {
6347 // When our app token is animating, we kind-of pretend like
6348 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6349 // part of this check means that we will only do this if
6350 // our window is not currently exiting, or it is not
6351 // locally animating itself. The idea being that one that
6352 // is exiting and doing a local animation should be removed
6353 // once that animation is done.
6354 mAnimating = true;
6355 mHasTransformation = true;
6356 mTransformation.clear();
6357 return false;
6358 } else if (mHasTransformation) {
6359 // Little trick to get through the path below to act like
6360 // we have finished an animation.
6361 mAnimating = true;
6362 } else if (isAnimating()) {
6363 mAnimating = true;
6364 }
6365 } else if (mAnimation != null) {
6366 // If the display is frozen, and there is a pending animation,
6367 // clear it and make sure we run the cleanup code.
6368 mAnimating = true;
6369 mLocalAnimating = true;
6370 mAnimation = null;
6371 }
Romain Guy06882f82009-06-10 13:36:04 -07006372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006373 if (!mAnimating && !mLocalAnimating) {
6374 return false;
6375 }
6376
6377 if (DEBUG_ANIM) Log.v(
6378 TAG, "Animation done in " + this + ": exiting=" + mExiting
6379 + ", reportedVisible="
6380 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006382 mAnimating = false;
6383 mLocalAnimating = false;
6384 mAnimation = null;
6385 mAnimLayer = mLayer;
6386 if (mIsImWindow) {
6387 mAnimLayer += mInputMethodAnimLayerAdjustment;
6388 }
6389 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6390 + " anim layer: " + mAnimLayer);
6391 mHasTransformation = false;
6392 mHasLocalTransformation = false;
6393 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6394 mTransformation.clear();
6395 if (mHasDrawn
6396 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6397 && mAppToken != null
6398 && mAppToken.firstWindowDrawn
6399 && mAppToken.startingData != null) {
6400 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6401 + mToken + ": first real window done animating");
6402 mFinishedStarting.add(mAppToken);
6403 mH.sendEmptyMessage(H.FINISHED_STARTING);
6404 }
Romain Guy06882f82009-06-10 13:36:04 -07006405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 finishExit();
6407
6408 if (mAppToken != null) {
6409 mAppToken.updateReportedVisibilityLocked();
6410 }
6411
6412 return false;
6413 }
6414
6415 void finishExit() {
6416 if (DEBUG_ANIM) Log.v(
6417 TAG, "finishExit in " + this
6418 + ": exiting=" + mExiting
6419 + " remove=" + mRemoveOnExit
6420 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006422 final int N = mChildWindows.size();
6423 for (int i=0; i<N; i++) {
6424 ((WindowState)mChildWindows.get(i)).finishExit();
6425 }
Romain Guy06882f82009-06-10 13:36:04 -07006426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006427 if (!mExiting) {
6428 return;
6429 }
Romain Guy06882f82009-06-10 13:36:04 -07006430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006431 if (isWindowAnimating()) {
6432 return;
6433 }
6434
6435 if (localLOGV) Log.v(
6436 TAG, "Exit animation finished in " + this
6437 + ": remove=" + mRemoveOnExit);
6438 if (mSurface != null) {
6439 mDestroySurface.add(this);
6440 mDestroying = true;
6441 if (SHOW_TRANSACTIONS) Log.i(
6442 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6443 try {
6444 mSurface.hide();
6445 } catch (RuntimeException e) {
6446 Log.w(TAG, "Error hiding surface in " + this, e);
6447 }
6448 mLastHidden = true;
6449 mKeyWaiter.releasePendingPointerLocked(mSession);
6450 }
6451 mExiting = false;
6452 if (mRemoveOnExit) {
6453 mPendingRemove.add(this);
6454 mRemoveOnExit = false;
6455 }
6456 }
Romain Guy06882f82009-06-10 13:36:04 -07006457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6459 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6460 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6461 if (dtdx < -.000001f || dtdx > .000001f) return false;
6462 if (dsdy < -.000001f || dsdy > .000001f) return false;
6463 return true;
6464 }
Romain Guy06882f82009-06-10 13:36:04 -07006465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006466 void computeShownFrameLocked() {
6467 final boolean selfTransformation = mHasLocalTransformation;
6468 Transformation attachedTransformation =
6469 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6470 ? mAttachedWindow.mTransformation : null;
6471 Transformation appTransformation =
6472 (mAppToken != null && mAppToken.hasTransformation)
6473 ? mAppToken.transformation : null;
6474 if (selfTransformation || attachedTransformation != null
6475 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006476 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006477 final Rect frame = mFrame;
6478 final float tmpFloats[] = mTmpFloats;
6479 final Matrix tmpMatrix = mTmpMatrix;
6480
6481 // Compute the desired transformation.
6482 tmpMatrix.setTranslate(frame.left, frame.top);
6483 if (selfTransformation) {
6484 tmpMatrix.preConcat(mTransformation.getMatrix());
6485 }
6486 if (attachedTransformation != null) {
6487 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6488 }
6489 if (appTransformation != null) {
6490 tmpMatrix.preConcat(appTransformation.getMatrix());
6491 }
6492
6493 // "convert" it into SurfaceFlinger's format
6494 // (a 2x2 matrix + an offset)
6495 // Here we must not transform the position of the surface
6496 // since it is already included in the transformation.
6497 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 tmpMatrix.getValues(tmpFloats);
6500 mDsDx = tmpFloats[Matrix.MSCALE_X];
6501 mDtDx = tmpFloats[Matrix.MSKEW_X];
6502 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6503 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6504 int x = (int)tmpFloats[Matrix.MTRANS_X];
6505 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6506 int w = frame.width();
6507 int h = frame.height();
6508 mShownFrame.set(x, y, x+w, y+h);
6509
6510 // Now set the alpha... but because our current hardware
6511 // can't do alpha transformation on a non-opaque surface,
6512 // turn it off if we are running an animation that is also
6513 // transforming since it is more important to have that
6514 // animation be smooth.
6515 mShownAlpha = mAlpha;
6516 if (!mLimitedAlphaCompositing
6517 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6518 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6519 && x == frame.left && y == frame.top))) {
6520 //Log.i(TAG, "Applying alpha transform");
6521 if (selfTransformation) {
6522 mShownAlpha *= mTransformation.getAlpha();
6523 }
6524 if (attachedTransformation != null) {
6525 mShownAlpha *= attachedTransformation.getAlpha();
6526 }
6527 if (appTransformation != null) {
6528 mShownAlpha *= appTransformation.getAlpha();
6529 }
6530 } else {
6531 //Log.i(TAG, "Not applying alpha transform");
6532 }
Romain Guy06882f82009-06-10 13:36:04 -07006533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006534 if (localLOGV) Log.v(
6535 TAG, "Continuing animation in " + this +
6536 ": " + mShownFrame +
6537 ", alpha=" + mTransformation.getAlpha());
6538 return;
6539 }
Romain Guy06882f82009-06-10 13:36:04 -07006540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006541 mShownFrame.set(mFrame);
6542 mShownAlpha = mAlpha;
6543 mDsDx = 1;
6544 mDtDx = 0;
6545 mDsDy = 0;
6546 mDtDy = 1;
6547 }
Romain Guy06882f82009-06-10 13:36:04 -07006548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 /**
6550 * Is this window visible? It is not visible if there is no
6551 * surface, or we are in the process of running an exit animation
6552 * that will remove the surface, or its app token has been hidden.
6553 */
6554 public boolean isVisibleLw() {
6555 final AppWindowToken atoken = mAppToken;
6556 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6557 && (atoken == null || !atoken.hiddenRequested)
6558 && !mExiting && !mDestroying;
6559 }
6560
6561 /**
6562 * Is this window visible, ignoring its app token? It is not visible
6563 * if there is no surface, or we are in the process of running an exit animation
6564 * that will remove the surface.
6565 */
6566 public boolean isWinVisibleLw() {
6567 final AppWindowToken atoken = mAppToken;
6568 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6569 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6570 && !mExiting && !mDestroying;
6571 }
6572
6573 /**
6574 * The same as isVisible(), but follows the current hidden state of
6575 * the associated app token, not the pending requested hidden state.
6576 */
6577 boolean isVisibleNow() {
6578 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006579 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 }
6581
6582 /**
6583 * Same as isVisible(), but we also count it as visible between the
6584 * call to IWindowSession.add() and the first relayout().
6585 */
6586 boolean isVisibleOrAdding() {
6587 final AppWindowToken atoken = mAppToken;
6588 return (mSurface != null
6589 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6590 && mPolicyVisibility && !mAttachedHidden
6591 && (atoken == null || !atoken.hiddenRequested)
6592 && !mExiting && !mDestroying;
6593 }
6594
6595 /**
6596 * Is this window currently on-screen? It is on-screen either if it
6597 * is visible or it is currently running an animation before no longer
6598 * being visible.
6599 */
6600 boolean isOnScreen() {
6601 final AppWindowToken atoken = mAppToken;
6602 if (atoken != null) {
6603 return mSurface != null && mPolicyVisibility && !mDestroying
6604 && ((!mAttachedHidden && !atoken.hiddenRequested)
6605 || mAnimating || atoken.animating);
6606 } else {
6607 return mSurface != null && mPolicyVisibility && !mDestroying
6608 && (!mAttachedHidden || mAnimating);
6609 }
6610 }
Romain Guy06882f82009-06-10 13:36:04 -07006611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006612 /**
6613 * Like isOnScreen(), but we don't return true if the window is part
6614 * of a transition that has not yet been started.
6615 */
6616 boolean isReadyForDisplay() {
6617 final AppWindowToken atoken = mAppToken;
6618 final boolean animating = atoken != null ? atoken.animating : false;
6619 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006620 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621 || mAnimating || animating);
6622 }
6623
6624 /** Is the window or its container currently animating? */
6625 boolean isAnimating() {
6626 final WindowState attached = mAttachedWindow;
6627 final AppWindowToken atoken = mAppToken;
6628 return mAnimation != null
6629 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006630 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006631 (atoken.animation != null
6632 || atoken.inPendingTransaction));
6633 }
6634
6635 /** Is this window currently animating? */
6636 boolean isWindowAnimating() {
6637 return mAnimation != null;
6638 }
6639
6640 /**
6641 * Like isOnScreen, but returns false if the surface hasn't yet
6642 * been drawn.
6643 */
6644 public boolean isDisplayedLw() {
6645 final AppWindowToken atoken = mAppToken;
6646 return mSurface != null && mPolicyVisibility && !mDestroying
6647 && !mDrawPending && !mCommitDrawPending
6648 && ((!mAttachedHidden &&
6649 (atoken == null || !atoken.hiddenRequested))
6650 || mAnimating);
6651 }
6652
6653 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6654 boolean shownFrame, boolean onlyOpaque) {
6655 if (mSurface == null) {
6656 return false;
6657 }
6658 if (mAppToken != null && !mAppToken.appFullscreen) {
6659 return false;
6660 }
6661 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6662 return false;
6663 }
6664 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006665
6666 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
6667 return frame.left <= mCompatibleScreenFrame.left &&
6668 frame.top <= mCompatibleScreenFrame.top &&
6669 frame.right >= mCompatibleScreenFrame.right &&
6670 frame.bottom >= mCompatibleScreenFrame.bottom;
6671 } else {
6672 return frame.left <= 0 && frame.top <= 0
6673 && frame.right >= screenWidth
6674 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 }
Romain Guy06882f82009-06-10 13:36:04 -07006677
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006678 /**
6679 * Return true if the window is opaque and fully drawn.
6680 */
6681 boolean isOpaqueDrawn() {
6682 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
6683 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
6684 }
6685
6686 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
6687 return
6688 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006689 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
6690 // only if it's visible
6691 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006692 // and only if the application fills the compatible screen
6693 mFrame.left <= mCompatibleScreenFrame.left &&
6694 mFrame.top <= mCompatibleScreenFrame.top &&
6695 mFrame.right >= mCompatibleScreenFrame.right &&
6696 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006697 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006698 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006699 }
6700
6701 boolean isFullscreen(int screenWidth, int screenHeight) {
6702 return mFrame.left <= 0 && mFrame.top <= 0 &&
6703 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006704 }
6705
6706 void removeLocked() {
6707 if (mAttachedWindow != null) {
6708 mAttachedWindow.mChildWindows.remove(this);
6709 }
6710 destroySurfaceLocked();
6711 mSession.windowRemovedLocked();
6712 try {
6713 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6714 } catch (RuntimeException e) {
6715 // Ignore if it has already been removed (usually because
6716 // we are doing this as part of processing a death note.)
6717 }
6718 }
6719
6720 private class DeathRecipient implements IBinder.DeathRecipient {
6721 public void binderDied() {
6722 try {
6723 synchronized(mWindowMap) {
6724 WindowState win = windowForClientLocked(mSession, mClient);
6725 Log.i(TAG, "WIN DEATH: " + win);
6726 if (win != null) {
6727 removeWindowLocked(mSession, win);
6728 }
6729 }
6730 } catch (IllegalArgumentException ex) {
6731 // This will happen if the window has already been
6732 // removed.
6733 }
6734 }
6735 }
6736
6737 /** Returns true if this window desires key events. */
6738 public final boolean canReceiveKeys() {
6739 return isVisibleOrAdding()
6740 && (mViewVisibility == View.VISIBLE)
6741 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6742 }
6743
6744 public boolean hasDrawnLw() {
6745 return mHasDrawn;
6746 }
6747
6748 public boolean showLw(boolean doAnimation) {
6749 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6750 mPolicyVisibility = true;
6751 mPolicyVisibilityAfterAnim = true;
6752 if (doAnimation) {
6753 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6754 }
6755 requestAnimationLocked(0);
6756 return true;
6757 }
6758 return false;
6759 }
6760
6761 public boolean hideLw(boolean doAnimation) {
6762 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6763 : mPolicyVisibility;
6764 if (current) {
6765 if (doAnimation) {
6766 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6767 if (mAnimation == null) {
6768 doAnimation = false;
6769 }
6770 }
6771 if (doAnimation) {
6772 mPolicyVisibilityAfterAnim = false;
6773 } else {
6774 mPolicyVisibilityAfterAnim = false;
6775 mPolicyVisibility = false;
6776 }
6777 requestAnimationLocked(0);
6778 return true;
6779 }
6780 return false;
6781 }
6782
6783 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006784 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07006785
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006786 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6787 pw.print(" mClient="); pw.println(mClient.asBinder());
6788 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6789 if (mAttachedWindow != null || mLayoutAttached) {
6790 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6791 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6792 }
6793 if (mIsImWindow) {
6794 pw.print(prefix); pw.print("mIsImWindow="); pw.println(mIsImWindow);
6795 }
6796 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
6797 pw.print(" mSubLayer="); pw.print(mSubLayer);
6798 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
6799 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
6800 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
6801 pw.print("="); pw.print(mAnimLayer);
6802 pw.print(" mLastLayer="); pw.println(mLastLayer);
6803 if (mSurface != null) {
6804 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
6805 }
6806 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
6807 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
6808 if (mAppToken != null) {
6809 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
6810 }
6811 if (mTargetAppToken != null) {
6812 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
6813 }
6814 pw.print(prefix); pw.print("mViewVisibility=0x");
6815 pw.print(Integer.toHexString(mViewVisibility));
6816 pw.print(" mLastHidden="); pw.print(mLastHidden);
6817 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
6818 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
6819 pw.print(prefix); pw.print("mPolicyVisibility=");
6820 pw.print(mPolicyVisibility);
6821 pw.print(" mPolicyVisibilityAfterAnim=");
6822 pw.print(mPolicyVisibilityAfterAnim);
6823 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
6824 }
6825 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
6826 pw.print(" h="); pw.print(mRequestedHeight);
6827 pw.print(" x="); pw.print(mReqXPos);
6828 pw.print(" y="); pw.println(mReqYPos);
6829 pw.print(prefix); pw.print("mGivenContentInsets=");
6830 mGivenContentInsets.printShortString(pw);
6831 pw.print(" mGivenVisibleInsets=");
6832 mGivenVisibleInsets.printShortString(pw);
6833 pw.println();
6834 if (mTouchableInsets != 0 || mGivenInsetsPending) {
6835 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
6836 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
6837 }
6838 pw.print(prefix); pw.print("mShownFrame=");
6839 mShownFrame.printShortString(pw);
6840 pw.print(" last="); mLastShownFrame.printShortString(pw);
6841 pw.println();
6842 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
6843 pw.print(" last="); mLastFrame.printShortString(pw);
6844 pw.println();
6845 pw.print(prefix); pw.print("mContainingFrame=");
6846 mContainingFrame.printShortString(pw);
6847 pw.print(" mDisplayFrame=");
6848 mDisplayFrame.printShortString(pw);
6849 pw.println();
6850 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
6851 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
6852 pw.println();
6853 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
6854 pw.print(" last="); mLastContentInsets.printShortString(pw);
6855 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
6856 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
6857 pw.println();
6858 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
6859 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
6860 pw.print(" mAlpha="); pw.print(mAlpha);
6861 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
6862 }
6863 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
6864 || mAnimation != null) {
6865 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
6866 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
6867 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
6868 pw.print(" mAnimation="); pw.println(mAnimation);
6869 }
6870 if (mHasTransformation || mHasLocalTransformation) {
6871 pw.print(prefix); pw.print("XForm: has=");
6872 pw.print(mHasTransformation);
6873 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
6874 pw.print(" "); mTransformation.printShortString(pw);
6875 pw.println();
6876 }
6877 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
6878 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
6879 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
6880 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
6881 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
6882 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
6883 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
6884 pw.print(" mDestroying="); pw.print(mDestroying);
6885 pw.print(" mRemoved="); pw.println(mRemoved);
6886 }
6887 if (mOrientationChanging || mAppFreezing) {
6888 pw.print(prefix); pw.print("mOrientationChanging=");
6889 pw.print(mOrientationChanging);
6890 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
6891 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07006892 if (mHScale != 1 || mVScale != 1) {
6893 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
6894 pw.print(" mVScale="); pw.println(mVScale);
6895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896 }
6897
6898 @Override
6899 public String toString() {
6900 return "Window{"
6901 + Integer.toHexString(System.identityHashCode(this))
6902 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
6903 }
6904 }
Romain Guy06882f82009-06-10 13:36:04 -07006905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 // -------------------------------------------------------------
6907 // Window Token State
6908 // -------------------------------------------------------------
6909
6910 class WindowToken {
6911 // The actual token.
6912 final IBinder token;
6913
6914 // The type of window this token is for, as per WindowManager.LayoutParams.
6915 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07006916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917 // Set if this token was explicitly added by a client, so should
6918 // not be removed when all windows are removed.
6919 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07006920
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006921 // For printing.
6922 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07006923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924 // If this is an AppWindowToken, this is non-null.
6925 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07006926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006927 // All of the windows associated with this token.
6928 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
6929
6930 // Is key dispatching paused for this token?
6931 boolean paused = false;
6932
6933 // Should this token's windows be hidden?
6934 boolean hidden;
6935
6936 // Temporary for finding which tokens no longer have visible windows.
6937 boolean hasVisible;
6938
6939 WindowToken(IBinder _token, int type, boolean _explicit) {
6940 token = _token;
6941 windowType = type;
6942 explicit = _explicit;
6943 }
6944
6945 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006946 pw.print(prefix); pw.print("token="); pw.println(token);
6947 pw.print(prefix); pw.print("windows="); pw.println(windows);
6948 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
6949 pw.print(" hidden="); pw.print(hidden);
6950 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006951 }
6952
6953 @Override
6954 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006955 if (stringName == null) {
6956 StringBuilder sb = new StringBuilder();
6957 sb.append("WindowToken{");
6958 sb.append(Integer.toHexString(System.identityHashCode(this)));
6959 sb.append(" token="); sb.append(token); sb.append('}');
6960 stringName = sb.toString();
6961 }
6962 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006963 }
6964 };
6965
6966 class AppWindowToken extends WindowToken {
6967 // Non-null only for application tokens.
6968 final IApplicationToken appToken;
6969
6970 // All of the windows and child windows that are included in this
6971 // application token. Note this list is NOT sorted!
6972 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
6973
6974 int groupId = -1;
6975 boolean appFullscreen;
6976 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07006977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006978 // These are used for determining when all windows associated with
6979 // an activity have been drawn, so they can be made visible together
6980 // at the same time.
6981 int lastTransactionSequence = mTransactionSequence-1;
6982 int numInterestingWindows;
6983 int numDrawnWindows;
6984 boolean inPendingTransaction;
6985 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07006986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006987 // Is this token going to be hidden in a little while? If so, it
6988 // won't be taken into account for setting the screen orientation.
6989 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006991 // Is this window's surface needed? This is almost like hidden, except
6992 // it will sometimes be true a little earlier: when the token has
6993 // been shown, but is still waiting for its app transition to execute
6994 // before making its windows shown.
6995 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07006996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006997 // Have we told the window clients to hide themselves?
6998 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007000 // Last visibility state we reported to the app token.
7001 boolean reportedVisible;
7002
7003 // Set to true when the token has been removed from the window mgr.
7004 boolean removed;
7005
7006 // Have we been asked to have this token keep the screen frozen?
7007 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007009 boolean animating;
7010 Animation animation;
7011 boolean hasTransformation;
7012 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007014 // Offset to the window of all layers in the token, for use by
7015 // AppWindowToken animations.
7016 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007018 // Information about an application starting window if displayed.
7019 StartingData startingData;
7020 WindowState startingWindow;
7021 View startingView;
7022 boolean startingDisplayed;
7023 boolean startingMoved;
7024 boolean firstWindowDrawn;
7025
7026 AppWindowToken(IApplicationToken _token) {
7027 super(_token.asBinder(),
7028 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7029 appWindowToken = this;
7030 appToken = _token;
7031 }
Romain Guy06882f82009-06-10 13:36:04 -07007032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007033 public void setAnimation(Animation anim) {
7034 if (localLOGV) Log.v(
7035 TAG, "Setting animation in " + this + ": " + anim);
7036 animation = anim;
7037 animating = false;
7038 anim.restrictDuration(MAX_ANIMATION_DURATION);
7039 anim.scaleCurrentDuration(mTransitionAnimationScale);
7040 int zorder = anim.getZAdjustment();
7041 int adj = 0;
7042 if (zorder == Animation.ZORDER_TOP) {
7043 adj = TYPE_LAYER_OFFSET;
7044 } else if (zorder == Animation.ZORDER_BOTTOM) {
7045 adj = -TYPE_LAYER_OFFSET;
7046 }
Romain Guy06882f82009-06-10 13:36:04 -07007047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007048 if (animLayerAdjustment != adj) {
7049 animLayerAdjustment = adj;
7050 updateLayers();
7051 }
7052 }
Romain Guy06882f82009-06-10 13:36:04 -07007053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 public void setDummyAnimation() {
7055 if (animation == null) {
7056 if (localLOGV) Log.v(
7057 TAG, "Setting dummy animation in " + this);
7058 animation = sDummyAnimation;
7059 }
7060 }
7061
7062 public void clearAnimation() {
7063 if (animation != null) {
7064 animation = null;
7065 animating = true;
7066 }
7067 }
Romain Guy06882f82009-06-10 13:36:04 -07007068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 void updateLayers() {
7070 final int N = allAppWindows.size();
7071 final int adj = animLayerAdjustment;
7072 for (int i=0; i<N; i++) {
7073 WindowState w = allAppWindows.get(i);
7074 w.mAnimLayer = w.mLayer + adj;
7075 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7076 + w.mAnimLayer);
7077 if (w == mInputMethodTarget) {
7078 setInputMethodAnimLayerAdjustment(adj);
7079 }
7080 }
7081 }
Romain Guy06882f82009-06-10 13:36:04 -07007082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 void sendAppVisibilityToClients() {
7084 final int N = allAppWindows.size();
7085 for (int i=0; i<N; i++) {
7086 WindowState win = allAppWindows.get(i);
7087 if (win == startingWindow && clientHidden) {
7088 // Don't hide the starting window.
7089 continue;
7090 }
7091 try {
7092 if (DEBUG_VISIBILITY) Log.v(TAG,
7093 "Setting visibility of " + win + ": " + (!clientHidden));
7094 win.mClient.dispatchAppVisibility(!clientHidden);
7095 } catch (RemoteException e) {
7096 }
7097 }
7098 }
Romain Guy06882f82009-06-10 13:36:04 -07007099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 void showAllWindowsLocked() {
7101 final int NW = allAppWindows.size();
7102 for (int i=0; i<NW; i++) {
7103 WindowState w = allAppWindows.get(i);
7104 if (DEBUG_VISIBILITY) Log.v(TAG,
7105 "performing show on: " + w);
7106 w.performShowLocked();
7107 }
7108 }
Romain Guy06882f82009-06-10 13:36:04 -07007109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 // This must be called while inside a transaction.
7111 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7112 if (!mDisplayFrozen) {
7113 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007115 if (animation == sDummyAnimation) {
7116 // This guy is going to animate, but not yet. For now count
7117 // it is not animating for purposes of scheduling transactions;
7118 // when it is really time to animate, this will be set to
7119 // a real animation and the next call will execute normally.
7120 return false;
7121 }
Romain Guy06882f82009-06-10 13:36:04 -07007122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7124 if (!animating) {
7125 if (DEBUG_ANIM) Log.v(
7126 TAG, "Starting animation in " + this +
7127 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7128 + " scale=" + mTransitionAnimationScale
7129 + " allDrawn=" + allDrawn + " animating=" + animating);
7130 animation.initialize(dw, dh, dw, dh);
7131 animation.setStartTime(currentTime);
7132 animating = true;
7133 }
7134 transformation.clear();
7135 final boolean more = animation.getTransformation(
7136 currentTime, transformation);
7137 if (DEBUG_ANIM) Log.v(
7138 TAG, "Stepped animation in " + this +
7139 ": more=" + more + ", xform=" + transformation);
7140 if (more) {
7141 // we're done!
7142 hasTransformation = true;
7143 return true;
7144 }
7145 if (DEBUG_ANIM) Log.v(
7146 TAG, "Finished animation in " + this +
7147 " @ " + currentTime);
7148 animation = null;
7149 }
7150 } else if (animation != null) {
7151 // If the display is frozen, and there is a pending animation,
7152 // clear it and make sure we run the cleanup code.
7153 animating = true;
7154 animation = null;
7155 }
7156
7157 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007159 if (!animating) {
7160 return false;
7161 }
7162
7163 clearAnimation();
7164 animating = false;
7165 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7166 moveInputMethodWindowsIfNeededLocked(true);
7167 }
Romain Guy06882f82009-06-10 13:36:04 -07007168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007169 if (DEBUG_ANIM) Log.v(
7170 TAG, "Animation done in " + this
7171 + ": reportedVisible=" + reportedVisible);
7172
7173 transformation.clear();
7174 if (animLayerAdjustment != 0) {
7175 animLayerAdjustment = 0;
7176 updateLayers();
7177 }
Romain Guy06882f82009-06-10 13:36:04 -07007178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 final int N = windows.size();
7180 for (int i=0; i<N; i++) {
7181 ((WindowState)windows.get(i)).finishExit();
7182 }
7183 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007185 return false;
7186 }
7187
7188 void updateReportedVisibilityLocked() {
7189 if (appToken == null) {
7190 return;
7191 }
Romain Guy06882f82009-06-10 13:36:04 -07007192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007193 int numInteresting = 0;
7194 int numVisible = 0;
7195 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007197 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7198 final int N = allAppWindows.size();
7199 for (int i=0; i<N; i++) {
7200 WindowState win = allAppWindows.get(i);
7201 if (win == startingWindow || win.mAppFreezing) {
7202 continue;
7203 }
7204 if (DEBUG_VISIBILITY) {
7205 Log.v(TAG, "Win " + win + ": isDisplayed="
7206 + win.isDisplayedLw()
7207 + ", isAnimating=" + win.isAnimating());
7208 if (!win.isDisplayedLw()) {
7209 Log.v(TAG, "Not displayed: s=" + win.mSurface
7210 + " pv=" + win.mPolicyVisibility
7211 + " dp=" + win.mDrawPending
7212 + " cdp=" + win.mCommitDrawPending
7213 + " ah=" + win.mAttachedHidden
7214 + " th="
7215 + (win.mAppToken != null
7216 ? win.mAppToken.hiddenRequested : false)
7217 + " a=" + win.mAnimating);
7218 }
7219 }
7220 numInteresting++;
7221 if (win.isDisplayedLw()) {
7222 if (!win.isAnimating()) {
7223 numVisible++;
7224 }
7225 nowGone = false;
7226 } else if (win.isAnimating()) {
7227 nowGone = false;
7228 }
7229 }
Romain Guy06882f82009-06-10 13:36:04 -07007230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7232 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7233 + numInteresting + " visible=" + numVisible);
7234 if (nowVisible != reportedVisible) {
7235 if (DEBUG_VISIBILITY) Log.v(
7236 TAG, "Visibility changed in " + this
7237 + ": vis=" + nowVisible);
7238 reportedVisible = nowVisible;
7239 Message m = mH.obtainMessage(
7240 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7241 nowVisible ? 1 : 0,
7242 nowGone ? 1 : 0,
7243 this);
7244 mH.sendMessage(m);
7245 }
7246 }
Romain Guy06882f82009-06-10 13:36:04 -07007247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 void dump(PrintWriter pw, String prefix) {
7249 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007250 if (appToken != null) {
7251 pw.print(prefix); pw.println("app=true");
7252 }
7253 if (allAppWindows.size() > 0) {
7254 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7255 }
7256 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7257 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7258 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7259 pw.print(" clientHidden="); pw.print(clientHidden);
7260 pw.print(" willBeHidden="); pw.print(willBeHidden);
7261 pw.print(" reportedVisible="); pw.println(reportedVisible);
7262 if (paused || freezingScreen) {
7263 pw.print(prefix); pw.print("paused="); pw.print(paused);
7264 pw.print(" freezingScreen="); pw.println(freezingScreen);
7265 }
7266 if (numInterestingWindows != 0 || numDrawnWindows != 0
7267 || inPendingTransaction || allDrawn) {
7268 pw.print(prefix); pw.print("numInterestingWindows=");
7269 pw.print(numInterestingWindows);
7270 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7271 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7272 pw.print(" allDrawn="); pw.println(allDrawn);
7273 }
7274 if (animating || animation != null) {
7275 pw.print(prefix); pw.print("animating="); pw.print(animating);
7276 pw.print(" animation="); pw.println(animation);
7277 }
7278 if (animLayerAdjustment != 0) {
7279 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7280 }
7281 if (hasTransformation) {
7282 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7283 pw.print(" transformation="); transformation.printShortString(pw);
7284 pw.println();
7285 }
7286 if (startingData != null || removed || firstWindowDrawn) {
7287 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7288 pw.print(" removed="); pw.print(removed);
7289 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7290 }
7291 if (startingWindow != null || startingView != null
7292 || startingDisplayed || startingMoved) {
7293 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7294 pw.print(" startingView="); pw.print(startingView);
7295 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7296 pw.print(" startingMoved"); pw.println(startingMoved);
7297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007298 }
7299
7300 @Override
7301 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007302 if (stringName == null) {
7303 StringBuilder sb = new StringBuilder();
7304 sb.append("AppWindowToken{");
7305 sb.append(Integer.toHexString(System.identityHashCode(this)));
7306 sb.append(" token="); sb.append(token); sb.append('}');
7307 stringName = sb.toString();
7308 }
7309 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007310 }
7311 }
Romain Guy06882f82009-06-10 13:36:04 -07007312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007313 public static WindowManager.LayoutParams findAnimations(
7314 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007315 ArrayList<AppWindowToken> openingTokenList1,
7316 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007318
7319 // First, check if there is a compatible window in opening/closing
7320 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 WindowManager.LayoutParams animParams = null;
7322 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007323 animParams = findCompatibleWindowParams(openingTokenList1);
7324 if (animParams == null) {
7325 animParams = findCompatibleWindowParams(closingTokenList2);
7326 }
7327 if (animParams != null) {
7328 return animParams;
7329 }
7330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007331 //Log.i(TAG, "Looking for animations...");
7332 for (int i=order.size()-1; i>=0; i--) {
7333 AppWindowToken wtoken = order.get(i);
7334 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007335 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007336 int j = wtoken.windows.size();
7337 while (j > 0) {
7338 j--;
7339 WindowState win = wtoken.windows.get(j);
7340 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7341 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7342 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7343 //Log.i(TAG, "Found base or application window, done!");
7344 if (wtoken.appFullscreen) {
7345 return win.mAttrs;
7346 }
7347 if (animSrc < 2) {
7348 animParams = win.mAttrs;
7349 animSrc = 2;
7350 }
7351 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7352 //Log.i(TAG, "Found normal window, we may use this...");
7353 animParams = win.mAttrs;
7354 animSrc = 1;
7355 }
7356 }
7357 }
7358 }
Romain Guy06882f82009-06-10 13:36:04 -07007359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007360 return animParams;
7361 }
Romain Guy06882f82009-06-10 13:36:04 -07007362
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007363 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7364 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7365 AppWindowToken wtoken = tokenList.get(appCount);
7366 // Just checking one window is sufficient as all windows have the compatible flag
7367 // if the application is in compatibility mode.
7368 if (wtoken.windows.size() > 0) {
7369 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7370 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7371 return params;
7372 }
7373 }
7374 }
7375 return null;
7376 }
7377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007378 // -------------------------------------------------------------
7379 // DummyAnimation
7380 // -------------------------------------------------------------
7381
7382 // This is an animation that does nothing: it just immediately finishes
7383 // itself every time it is called. It is used as a stub animation in cases
7384 // where we want to synchronize multiple things that may be animating.
7385 static final class DummyAnimation extends Animation {
7386 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7387 return false;
7388 }
7389 }
7390 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 // -------------------------------------------------------------
7393 // Async Handler
7394 // -------------------------------------------------------------
7395
7396 static final class StartingData {
7397 final String pkg;
7398 final int theme;
7399 final CharSequence nonLocalizedLabel;
7400 final int labelRes;
7401 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7404 int _labelRes, int _icon) {
7405 pkg = _pkg;
7406 theme = _theme;
7407 nonLocalizedLabel = _nonLocalizedLabel;
7408 labelRes = _labelRes;
7409 icon = _icon;
7410 }
7411 }
7412
7413 private final class H extends Handler {
7414 public static final int REPORT_FOCUS_CHANGE = 2;
7415 public static final int REPORT_LOSING_FOCUS = 3;
7416 public static final int ANIMATE = 4;
7417 public static final int ADD_STARTING = 5;
7418 public static final int REMOVE_STARTING = 6;
7419 public static final int FINISHED_STARTING = 7;
7420 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007421 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7422 public static final int HOLD_SCREEN_CHANGED = 12;
7423 public static final int APP_TRANSITION_TIMEOUT = 13;
7424 public static final int PERSIST_ANIMATION_SCALE = 14;
7425 public static final int FORCE_GC = 15;
7426 public static final int ENABLE_SCREEN = 16;
7427 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007428 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007430 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007432 public H() {
7433 }
Romain Guy06882f82009-06-10 13:36:04 -07007434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007435 @Override
7436 public void handleMessage(Message msg) {
7437 switch (msg.what) {
7438 case REPORT_FOCUS_CHANGE: {
7439 WindowState lastFocus;
7440 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007442 synchronized(mWindowMap) {
7443 lastFocus = mLastFocus;
7444 newFocus = mCurrentFocus;
7445 if (lastFocus == newFocus) {
7446 // Focus is not changing, so nothing to do.
7447 return;
7448 }
7449 mLastFocus = newFocus;
7450 //Log.i(TAG, "Focus moving from " + lastFocus
7451 // + " to " + newFocus);
7452 if (newFocus != null && lastFocus != null
7453 && !newFocus.isDisplayedLw()) {
7454 //Log.i(TAG, "Delaying loss of focus...");
7455 mLosingFocus.add(lastFocus);
7456 lastFocus = null;
7457 }
7458 }
7459
7460 if (lastFocus != newFocus) {
7461 //System.out.println("Changing focus from " + lastFocus
7462 // + " to " + newFocus);
7463 if (newFocus != null) {
7464 try {
7465 //Log.i(TAG, "Gaining focus: " + newFocus);
7466 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7467 } catch (RemoteException e) {
7468 // Ignore if process has died.
7469 }
7470 }
7471
7472 if (lastFocus != null) {
7473 try {
7474 //Log.i(TAG, "Losing focus: " + lastFocus);
7475 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7476 } catch (RemoteException e) {
7477 // Ignore if process has died.
7478 }
7479 }
7480 }
7481 } break;
7482
7483 case REPORT_LOSING_FOCUS: {
7484 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007486 synchronized(mWindowMap) {
7487 losers = mLosingFocus;
7488 mLosingFocus = new ArrayList<WindowState>();
7489 }
7490
7491 final int N = losers.size();
7492 for (int i=0; i<N; i++) {
7493 try {
7494 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7495 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7496 } catch (RemoteException e) {
7497 // Ignore if process has died.
7498 }
7499 }
7500 } break;
7501
7502 case ANIMATE: {
7503 synchronized(mWindowMap) {
7504 mAnimationPending = false;
7505 performLayoutAndPlaceSurfacesLocked();
7506 }
7507 } break;
7508
7509 case ADD_STARTING: {
7510 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7511 final StartingData sd = wtoken.startingData;
7512
7513 if (sd == null) {
7514 // Animation has been canceled... do nothing.
7515 return;
7516 }
Romain Guy06882f82009-06-10 13:36:04 -07007517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7519 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007521 View view = null;
7522 try {
7523 view = mPolicy.addStartingWindow(
7524 wtoken.token, sd.pkg,
7525 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7526 sd.icon);
7527 } catch (Exception e) {
7528 Log.w(TAG, "Exception when adding starting window", e);
7529 }
7530
7531 if (view != null) {
7532 boolean abort = false;
7533
7534 synchronized(mWindowMap) {
7535 if (wtoken.removed || wtoken.startingData == null) {
7536 // If the window was successfully added, then
7537 // we need to remove it.
7538 if (wtoken.startingWindow != null) {
7539 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7540 "Aborted starting " + wtoken
7541 + ": removed=" + wtoken.removed
7542 + " startingData=" + wtoken.startingData);
7543 wtoken.startingWindow = null;
7544 wtoken.startingData = null;
7545 abort = true;
7546 }
7547 } else {
7548 wtoken.startingView = view;
7549 }
7550 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7551 "Added starting " + wtoken
7552 + ": startingWindow="
7553 + wtoken.startingWindow + " startingView="
7554 + wtoken.startingView);
7555 }
7556
7557 if (abort) {
7558 try {
7559 mPolicy.removeStartingWindow(wtoken.token, view);
7560 } catch (Exception e) {
7561 Log.w(TAG, "Exception when removing starting window", e);
7562 }
7563 }
7564 }
7565 } break;
7566
7567 case REMOVE_STARTING: {
7568 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7569 IBinder token = null;
7570 View view = null;
7571 synchronized (mWindowMap) {
7572 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7573 + wtoken + ": startingWindow="
7574 + wtoken.startingWindow + " startingView="
7575 + wtoken.startingView);
7576 if (wtoken.startingWindow != null) {
7577 view = wtoken.startingView;
7578 token = wtoken.token;
7579 wtoken.startingData = null;
7580 wtoken.startingView = null;
7581 wtoken.startingWindow = null;
7582 }
7583 }
7584 if (view != null) {
7585 try {
7586 mPolicy.removeStartingWindow(token, view);
7587 } catch (Exception e) {
7588 Log.w(TAG, "Exception when removing starting window", e);
7589 }
7590 }
7591 } break;
7592
7593 case FINISHED_STARTING: {
7594 IBinder token = null;
7595 View view = null;
7596 while (true) {
7597 synchronized (mWindowMap) {
7598 final int N = mFinishedStarting.size();
7599 if (N <= 0) {
7600 break;
7601 }
7602 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7603
7604 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7605 "Finished starting " + wtoken
7606 + ": startingWindow=" + wtoken.startingWindow
7607 + " startingView=" + wtoken.startingView);
7608
7609 if (wtoken.startingWindow == null) {
7610 continue;
7611 }
7612
7613 view = wtoken.startingView;
7614 token = wtoken.token;
7615 wtoken.startingData = null;
7616 wtoken.startingView = null;
7617 wtoken.startingWindow = null;
7618 }
7619
7620 try {
7621 mPolicy.removeStartingWindow(token, view);
7622 } catch (Exception e) {
7623 Log.w(TAG, "Exception when removing starting window", e);
7624 }
7625 }
7626 } break;
7627
7628 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7629 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7630
7631 boolean nowVisible = msg.arg1 != 0;
7632 boolean nowGone = msg.arg2 != 0;
7633
7634 try {
7635 if (DEBUG_VISIBILITY) Log.v(
7636 TAG, "Reporting visible in " + wtoken
7637 + " visible=" + nowVisible
7638 + " gone=" + nowGone);
7639 if (nowVisible) {
7640 wtoken.appToken.windowsVisible();
7641 } else {
7642 wtoken.appToken.windowsGone();
7643 }
7644 } catch (RemoteException ex) {
7645 }
7646 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 case WINDOW_FREEZE_TIMEOUT: {
7649 synchronized (mWindowMap) {
7650 Log.w(TAG, "Window freeze timeout expired.");
7651 int i = mWindows.size();
7652 while (i > 0) {
7653 i--;
7654 WindowState w = (WindowState)mWindows.get(i);
7655 if (w.mOrientationChanging) {
7656 w.mOrientationChanging = false;
7657 Log.w(TAG, "Force clearing orientation change: " + w);
7658 }
7659 }
7660 performLayoutAndPlaceSurfacesLocked();
7661 }
7662 break;
7663 }
Romain Guy06882f82009-06-10 13:36:04 -07007664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665 case HOLD_SCREEN_CHANGED: {
7666 Session oldHold;
7667 Session newHold;
7668 synchronized (mWindowMap) {
7669 oldHold = mLastReportedHold;
7670 newHold = (Session)msg.obj;
7671 mLastReportedHold = newHold;
7672 }
Romain Guy06882f82009-06-10 13:36:04 -07007673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007674 if (oldHold != newHold) {
7675 try {
7676 if (oldHold != null) {
7677 mBatteryStats.noteStopWakelock(oldHold.mUid,
7678 "window",
7679 BatteryStats.WAKE_TYPE_WINDOW);
7680 }
7681 if (newHold != null) {
7682 mBatteryStats.noteStartWakelock(newHold.mUid,
7683 "window",
7684 BatteryStats.WAKE_TYPE_WINDOW);
7685 }
7686 } catch (RemoteException e) {
7687 }
7688 }
7689 break;
7690 }
Romain Guy06882f82009-06-10 13:36:04 -07007691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007692 case APP_TRANSITION_TIMEOUT: {
7693 synchronized (mWindowMap) {
7694 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7695 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7696 "*** APP TRANSITION TIMEOUT");
7697 mAppTransitionReady = true;
7698 mAppTransitionTimeout = true;
7699 performLayoutAndPlaceSurfacesLocked();
7700 }
7701 }
7702 break;
7703 }
Romain Guy06882f82009-06-10 13:36:04 -07007704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007705 case PERSIST_ANIMATION_SCALE: {
7706 Settings.System.putFloat(mContext.getContentResolver(),
7707 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7708 Settings.System.putFloat(mContext.getContentResolver(),
7709 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7710 break;
7711 }
Romain Guy06882f82009-06-10 13:36:04 -07007712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 case FORCE_GC: {
7714 synchronized(mWindowMap) {
7715 if (mAnimationPending) {
7716 // If we are animating, don't do the gc now but
7717 // delay a bit so we don't interrupt the animation.
7718 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7719 2000);
7720 return;
7721 }
7722 // If we are currently rotating the display, it will
7723 // schedule a new message when done.
7724 if (mDisplayFrozen) {
7725 return;
7726 }
7727 mFreezeGcPending = 0;
7728 }
7729 Runtime.getRuntime().gc();
7730 break;
7731 }
Romain Guy06882f82009-06-10 13:36:04 -07007732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007733 case ENABLE_SCREEN: {
7734 performEnableScreen();
7735 break;
7736 }
Romain Guy06882f82009-06-10 13:36:04 -07007737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007738 case APP_FREEZE_TIMEOUT: {
7739 synchronized (mWindowMap) {
7740 Log.w(TAG, "App freeze timeout expired.");
7741 int i = mAppTokens.size();
7742 while (i > 0) {
7743 i--;
7744 AppWindowToken tok = mAppTokens.get(i);
7745 if (tok.freezingScreen) {
7746 Log.w(TAG, "Force clearing freeze: " + tok);
7747 unsetAppFreezingScreenLocked(tok, true, true);
7748 }
7749 }
7750 }
7751 break;
7752 }
Romain Guy06882f82009-06-10 13:36:04 -07007753
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007754 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07007755 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007756 sendNewConfiguration();
7757 }
7758 break;
7759 }
Romain Guy06882f82009-06-10 13:36:04 -07007760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007761 }
7762 }
7763 }
7764
7765 // -------------------------------------------------------------
7766 // IWindowManager API
7767 // -------------------------------------------------------------
7768
7769 public IWindowSession openSession(IInputMethodClient client,
7770 IInputContext inputContext) {
7771 if (client == null) throw new IllegalArgumentException("null client");
7772 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7773 return new Session(client, inputContext);
7774 }
7775
7776 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7777 synchronized (mWindowMap) {
7778 // The focus for the client is the window immediately below
7779 // where we would place the input method window.
7780 int idx = findDesiredInputMethodWindowIndexLocked(false);
7781 WindowState imFocus;
7782 if (idx > 0) {
7783 imFocus = (WindowState)mWindows.get(idx-1);
7784 if (imFocus != null) {
7785 if (imFocus.mSession.mClient != null &&
7786 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7787 return true;
7788 }
7789 }
7790 }
7791 }
7792 return false;
7793 }
Romain Guy06882f82009-06-10 13:36:04 -07007794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 // -------------------------------------------------------------
7796 // Internals
7797 // -------------------------------------------------------------
7798
7799 final WindowState windowForClientLocked(Session session, IWindow client) {
7800 return windowForClientLocked(session, client.asBinder());
7801 }
Romain Guy06882f82009-06-10 13:36:04 -07007802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 final WindowState windowForClientLocked(Session session, IBinder client) {
7804 WindowState win = mWindowMap.get(client);
7805 if (localLOGV) Log.v(
7806 TAG, "Looking up client " + client + ": " + win);
7807 if (win == null) {
7808 RuntimeException ex = new RuntimeException();
7809 Log.w(TAG, "Requested window " + client + " does not exist", ex);
7810 return null;
7811 }
7812 if (session != null && win.mSession != session) {
7813 RuntimeException ex = new RuntimeException();
7814 Log.w(TAG, "Requested window " + client + " is in session " +
7815 win.mSession + ", not " + session, ex);
7816 return null;
7817 }
7818
7819 return win;
7820 }
7821
7822 private final void assignLayersLocked() {
7823 int N = mWindows.size();
7824 int curBaseLayer = 0;
7825 int curLayer = 0;
7826 int i;
Romain Guy06882f82009-06-10 13:36:04 -07007827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 for (i=0; i<N; i++) {
7829 WindowState w = (WindowState)mWindows.get(i);
7830 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow) {
7831 curLayer += WINDOW_LAYER_MULTIPLIER;
7832 w.mLayer = curLayer;
7833 } else {
7834 curBaseLayer = curLayer = w.mBaseLayer;
7835 w.mLayer = curLayer;
7836 }
7837 if (w.mTargetAppToken != null) {
7838 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
7839 } else if (w.mAppToken != null) {
7840 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
7841 } else {
7842 w.mAnimLayer = w.mLayer;
7843 }
7844 if (w.mIsImWindow) {
7845 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
7846 }
7847 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
7848 + w.mAnimLayer);
7849 //System.out.println(
7850 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
7851 }
7852 }
7853
7854 private boolean mInLayout = false;
7855 private final void performLayoutAndPlaceSurfacesLocked() {
7856 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07007857 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007858 throw new RuntimeException("Recursive call!");
7859 }
7860 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
7861 return;
7862 }
7863
7864 boolean recoveringMemory = false;
7865 if (mForceRemoves != null) {
7866 recoveringMemory = true;
7867 // Wait a little it for things to settle down, and off we go.
7868 for (int i=0; i<mForceRemoves.size(); i++) {
7869 WindowState ws = mForceRemoves.get(i);
7870 Log.i(TAG, "Force removing: " + ws);
7871 removeWindowInnerLocked(ws.mSession, ws);
7872 }
7873 mForceRemoves = null;
7874 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
7875 Object tmp = new Object();
7876 synchronized (tmp) {
7877 try {
7878 tmp.wait(250);
7879 } catch (InterruptedException e) {
7880 }
7881 }
7882 }
Romain Guy06882f82009-06-10 13:36:04 -07007883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 mInLayout = true;
7885 try {
7886 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07007887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007888 int i = mPendingRemove.size()-1;
7889 if (i >= 0) {
7890 while (i >= 0) {
7891 WindowState w = mPendingRemove.get(i);
7892 removeWindowInnerLocked(w.mSession, w);
7893 i--;
7894 }
7895 mPendingRemove.clear();
7896
7897 mInLayout = false;
7898 assignLayersLocked();
7899 mLayoutNeeded = true;
7900 performLayoutAndPlaceSurfacesLocked();
7901
7902 } else {
7903 mInLayout = false;
7904 if (mLayoutNeeded) {
7905 requestAnimationLocked(0);
7906 }
7907 }
7908 } catch (RuntimeException e) {
7909 mInLayout = false;
7910 Log.e(TAG, "Unhandled exception while layout out windows", e);
7911 }
7912 }
7913
7914 private final void performLayoutLockedInner() {
7915 final int dw = mDisplay.getWidth();
7916 final int dh = mDisplay.getHeight();
7917
7918 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007919 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 int i;
7921
7922 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07007923
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007924 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 mPolicy.beginLayoutLw(dw, dh);
7926
7927 // First perform layout of any root windows (not attached
7928 // to another window).
7929 int topAttached = -1;
7930 for (i = N-1; i >= 0; i--) {
7931 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007932
7933 // Don't do layout of a window if it is not visible, or
7934 // soon won't be visible, to avoid wasting time and funky
7935 // changes while a window is animating away.
7936 final AppWindowToken atoken = win.mAppToken;
7937 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007938 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007939 || win.mRootToken.hidden
7940 || (atoken != null && atoken.hiddenRequested)
7941 || !win.mPolicyVisibility
7942 || win.mAttachedHidden
7943 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007944
7945 // If this view is GONE, then skip it -- keep the current
7946 // frame, and let the caller know so they can ignore it
7947 // if they want. (We do the normal layout for INVISIBLE
7948 // windows, since that means "perform layout as normal,
7949 // just don't display").
7950 if (!gone || !win.mHaveFrame) {
7951 if (!win.mLayoutAttached) {
7952 mPolicy.layoutWindowLw(win, win.mAttrs, null);
7953 } else {
7954 if (topAttached < 0) topAttached = i;
7955 }
7956 }
7957 }
Romain Guy06882f82009-06-10 13:36:04 -07007958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959 // Now perform layout of attached windows, which usually
7960 // depend on the position of the window they are attached to.
7961 // XXX does not deal with windows that are attached to windows
7962 // that are themselves attached.
7963 for (i = topAttached; i >= 0; i--) {
7964 WindowState win = (WindowState) mWindows.get(i);
7965
7966 // If this view is GONE, then skip it -- keep the current
7967 // frame, and let the caller know so they can ignore it
7968 // if they want. (We do the normal layout for INVISIBLE
7969 // windows, since that means "perform layout as normal,
7970 // just don't display").
7971 if (win.mLayoutAttached) {
7972 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
7973 || !win.mHaveFrame) {
7974 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
7975 }
7976 }
7977 }
7978
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007979 if (!mPolicy.finishLayoutLw()) {
7980 mLayoutNeeded = false;
7981 } else if (repeats > 2) {
7982 Log.w(TAG, "Layout repeat aborted after too many iterations");
7983 mLayoutNeeded = false;
7984 } else {
7985 repeats++;
7986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007987 }
7988 }
Romain Guy06882f82009-06-10 13:36:04 -07007989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007990 private final void performLayoutAndPlaceSurfacesLockedInner(
7991 boolean recoveringMemory) {
7992 final long currentTime = SystemClock.uptimeMillis();
7993 final int dw = mDisplay.getWidth();
7994 final int dh = mDisplay.getHeight();
7995
7996 final int N = mWindows.size();
7997 int i;
7998
7999 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008002 if (mFxSession == null) {
8003 mFxSession = new SurfaceSession();
8004 }
Romain Guy06882f82009-06-10 13:36:04 -07008005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008006 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8007
8008 // Initialize state of exiting tokens.
8009 for (i=mExitingTokens.size()-1; i>=0; i--) {
8010 mExitingTokens.get(i).hasVisible = false;
8011 }
8012
8013 // Initialize state of exiting applications.
8014 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8015 mExitingAppTokens.get(i).hasVisible = false;
8016 }
8017
8018 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 boolean orientationChangeComplete = true;
8020 Session holdScreen = null;
8021 float screenBrightness = -1;
8022 boolean focusDisplayed = false;
8023 boolean animating = false;
8024
8025 Surface.openTransaction();
8026 try {
8027 boolean restart;
8028
8029 do {
8030 final int transactionSequence = ++mTransactionSequence;
8031
8032 // Update animations of all applications, including those
8033 // associated with exiting/removed apps
8034 boolean tokensAnimating = false;
8035 final int NAT = mAppTokens.size();
8036 for (i=0; i<NAT; i++) {
8037 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8038 tokensAnimating = true;
8039 }
8040 }
8041 final int NEAT = mExitingAppTokens.size();
8042 for (i=0; i<NEAT; i++) {
8043 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8044 tokensAnimating = true;
8045 }
8046 }
8047
8048 animating = tokensAnimating;
8049 restart = false;
8050
8051 boolean tokenMayBeDrawn = false;
8052
8053 mPolicy.beginAnimationLw(dw, dh);
8054
8055 for (i=N-1; i>=0; i--) {
8056 WindowState w = (WindowState)mWindows.get(i);
8057
8058 final WindowManager.LayoutParams attrs = w.mAttrs;
8059
8060 if (w.mSurface != null) {
8061 // Execute animation.
8062 w.commitFinishDrawingLocked(currentTime);
8063 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8064 animating = true;
8065 //w.dump(" ");
8066 }
8067
8068 mPolicy.animatingWindowLw(w, attrs);
8069 }
8070
8071 final AppWindowToken atoken = w.mAppToken;
8072 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8073 if (atoken.lastTransactionSequence != transactionSequence) {
8074 atoken.lastTransactionSequence = transactionSequence;
8075 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8076 atoken.startingDisplayed = false;
8077 }
8078 if ((w.isOnScreen() || w.mAttrs.type
8079 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8080 && !w.mExiting && !w.mDestroying) {
8081 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8082 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8083 + w.isDisplayedLw()
8084 + ", isAnimating=" + w.isAnimating());
8085 if (!w.isDisplayedLw()) {
8086 Log.v(TAG, "Not displayed: s=" + w.mSurface
8087 + " pv=" + w.mPolicyVisibility
8088 + " dp=" + w.mDrawPending
8089 + " cdp=" + w.mCommitDrawPending
8090 + " ah=" + w.mAttachedHidden
8091 + " th=" + atoken.hiddenRequested
8092 + " a=" + w.mAnimating);
8093 }
8094 }
8095 if (w != atoken.startingWindow) {
8096 if (!atoken.freezingScreen || !w.mAppFreezing) {
8097 atoken.numInterestingWindows++;
8098 if (w.isDisplayedLw()) {
8099 atoken.numDrawnWindows++;
8100 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8101 "tokenMayBeDrawn: " + atoken
8102 + " freezingScreen=" + atoken.freezingScreen
8103 + " mAppFreezing=" + w.mAppFreezing);
8104 tokenMayBeDrawn = true;
8105 }
8106 }
8107 } else if (w.isDisplayedLw()) {
8108 atoken.startingDisplayed = true;
8109 }
8110 }
8111 } else if (w.mReadyToShow) {
8112 w.performShowLocked();
8113 }
8114 }
8115
8116 if (mPolicy.finishAnimationLw()) {
8117 restart = true;
8118 }
8119
8120 if (tokenMayBeDrawn) {
8121 // See if any windows have been drawn, so they (and others
8122 // associated with them) can now be shown.
8123 final int NT = mTokenList.size();
8124 for (i=0; i<NT; i++) {
8125 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8126 if (wtoken == null) {
8127 continue;
8128 }
8129 if (wtoken.freezingScreen) {
8130 int numInteresting = wtoken.numInterestingWindows;
8131 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8132 if (DEBUG_VISIBILITY) Log.v(TAG,
8133 "allDrawn: " + wtoken
8134 + " interesting=" + numInteresting
8135 + " drawn=" + wtoken.numDrawnWindows);
8136 wtoken.showAllWindowsLocked();
8137 unsetAppFreezingScreenLocked(wtoken, false, true);
8138 orientationChangeComplete = true;
8139 }
8140 } else if (!wtoken.allDrawn) {
8141 int numInteresting = wtoken.numInterestingWindows;
8142 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8143 if (DEBUG_VISIBILITY) Log.v(TAG,
8144 "allDrawn: " + wtoken
8145 + " interesting=" + numInteresting
8146 + " drawn=" + wtoken.numDrawnWindows);
8147 wtoken.allDrawn = true;
8148 restart = true;
8149
8150 // We can now show all of the drawn windows!
8151 if (!mOpeningApps.contains(wtoken)) {
8152 wtoken.showAllWindowsLocked();
8153 }
8154 }
8155 }
8156 }
8157 }
8158
8159 // If we are ready to perform an app transition, check through
8160 // all of the app tokens to be shown and see if they are ready
8161 // to go.
8162 if (mAppTransitionReady) {
8163 int NN = mOpeningApps.size();
8164 boolean goodToGo = true;
8165 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8166 "Checking " + NN + " opening apps (frozen="
8167 + mDisplayFrozen + " timeout="
8168 + mAppTransitionTimeout + ")...");
8169 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8170 // If the display isn't frozen, wait to do anything until
8171 // all of the apps are ready. Otherwise just go because
8172 // we'll unfreeze the display when everyone is ready.
8173 for (i=0; i<NN && goodToGo; i++) {
8174 AppWindowToken wtoken = mOpeningApps.get(i);
8175 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8176 "Check opening app" + wtoken + ": allDrawn="
8177 + wtoken.allDrawn + " startingDisplayed="
8178 + wtoken.startingDisplayed);
8179 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8180 && !wtoken.startingMoved) {
8181 goodToGo = false;
8182 }
8183 }
8184 }
8185 if (goodToGo) {
8186 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8187 int transit = mNextAppTransition;
8188 if (mSkipAppTransitionAnimation) {
8189 transit = WindowManagerPolicy.TRANSIT_NONE;
8190 }
8191 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8192 mAppTransitionReady = false;
8193 mAppTransitionTimeout = false;
8194 mStartingIconInTransition = false;
8195 mSkipAppTransitionAnimation = false;
8196
8197 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8198
8199 // We need to figure out which animation to use...
8200 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8201 mOpeningApps, mClosingApps);
8202
8203 NN = mOpeningApps.size();
8204 for (i=0; i<NN; i++) {
8205 AppWindowToken wtoken = mOpeningApps.get(i);
8206 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8207 "Now opening app" + wtoken);
8208 wtoken.reportedVisible = false;
8209 wtoken.inPendingTransaction = false;
8210 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8211 wtoken.updateReportedVisibilityLocked();
8212 wtoken.showAllWindowsLocked();
8213 }
8214 NN = mClosingApps.size();
8215 for (i=0; i<NN; i++) {
8216 AppWindowToken wtoken = mClosingApps.get(i);
8217 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8218 "Now closing app" + wtoken);
8219 wtoken.inPendingTransaction = false;
8220 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8221 wtoken.updateReportedVisibilityLocked();
8222 // Force the allDrawn flag, because we want to start
8223 // this guy's animations regardless of whether it's
8224 // gotten drawn.
8225 wtoken.allDrawn = true;
8226 }
8227
8228 mOpeningApps.clear();
8229 mClosingApps.clear();
8230
8231 // This has changed the visibility of windows, so perform
8232 // a new layout to get them all up-to-date.
8233 mLayoutNeeded = true;
8234 moveInputMethodWindowsIfNeededLocked(true);
8235 performLayoutLockedInner();
8236 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8237
8238 restart = true;
8239 }
8240 }
8241 } while (restart);
8242
8243 // THIRD LOOP: Update the surfaces of all windows.
8244
8245 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8246
8247 boolean obscured = false;
8248 boolean blurring = false;
8249 boolean dimming = false;
8250 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008251 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008252 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008253
8254 for (i=N-1; i>=0; i--) {
8255 WindowState w = (WindowState)mWindows.get(i);
8256
8257 boolean displayed = false;
8258 final WindowManager.LayoutParams attrs = w.mAttrs;
8259 final int attrFlags = attrs.flags;
8260
8261 if (w.mSurface != null) {
8262 w.computeShownFrameLocked();
8263 if (localLOGV) Log.v(
8264 TAG, "Placing surface #" + i + " " + w.mSurface
8265 + ": new=" + w.mShownFrame + ", old="
8266 + w.mLastShownFrame);
8267
8268 boolean resize;
8269 int width, height;
8270 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8271 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8272 w.mLastRequestedHeight != w.mRequestedHeight;
8273 // for a scaled surface, we just want to use
8274 // the requested size.
8275 width = w.mRequestedWidth;
8276 height = w.mRequestedHeight;
8277 w.mLastRequestedWidth = width;
8278 w.mLastRequestedHeight = height;
8279 w.mLastShownFrame.set(w.mShownFrame);
8280 try {
8281 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8282 } catch (RuntimeException e) {
8283 Log.w(TAG, "Error positioning surface in " + w, e);
8284 if (!recoveringMemory) {
8285 reclaimSomeSurfaceMemoryLocked(w, "position");
8286 }
8287 }
8288 } else {
8289 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8290 width = w.mShownFrame.width();
8291 height = w.mShownFrame.height();
8292 w.mLastShownFrame.set(w.mShownFrame);
8293 if (resize) {
8294 if (SHOW_TRANSACTIONS) Log.i(
8295 TAG, " SURFACE " + w.mSurface + ": ("
8296 + w.mShownFrame.left + ","
8297 + w.mShownFrame.top + ") ("
8298 + w.mShownFrame.width() + "x"
8299 + w.mShownFrame.height() + ")");
8300 }
8301 }
8302
8303 if (resize) {
8304 if (width < 1) width = 1;
8305 if (height < 1) height = 1;
8306 if (w.mSurface != null) {
8307 try {
8308 w.mSurface.setSize(width, height);
8309 w.mSurface.setPosition(w.mShownFrame.left,
8310 w.mShownFrame.top);
8311 } catch (RuntimeException e) {
8312 // If something goes wrong with the surface (such
8313 // as running out of memory), don't take down the
8314 // entire system.
8315 Log.e(TAG, "Failure updating surface of " + w
8316 + "size=(" + width + "x" + height
8317 + "), pos=(" + w.mShownFrame.left
8318 + "," + w.mShownFrame.top + ")", e);
8319 if (!recoveringMemory) {
8320 reclaimSomeSurfaceMemoryLocked(w, "size");
8321 }
8322 }
8323 }
8324 }
8325 if (!w.mAppFreezing) {
8326 w.mContentInsetsChanged =
8327 !w.mLastContentInsets.equals(w.mContentInsets);
8328 w.mVisibleInsetsChanged =
8329 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008330 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008331 || w.mContentInsetsChanged
8332 || w.mVisibleInsetsChanged) {
8333 w.mLastFrame.set(w.mFrame);
8334 w.mLastContentInsets.set(w.mContentInsets);
8335 w.mLastVisibleInsets.set(w.mVisibleInsets);
8336 // If the orientation is changing, then we need to
8337 // hold off on unfreezing the display until this
8338 // window has been redrawn; to do that, we need
8339 // to go through the process of getting informed
8340 // by the application when it has finished drawing.
8341 if (w.mOrientationChanging) {
8342 if (DEBUG_ORIENTATION) Log.v(TAG,
8343 "Orientation start waiting for draw in "
8344 + w + ", surface " + w.mSurface);
8345 w.mDrawPending = true;
8346 w.mCommitDrawPending = false;
8347 w.mReadyToShow = false;
8348 if (w.mAppToken != null) {
8349 w.mAppToken.allDrawn = false;
8350 }
8351 }
Romain Guy06882f82009-06-10 13:36:04 -07008352 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 "Resizing window " + w + " to " + w.mFrame);
8354 mResizingWindows.add(w);
8355 } else if (w.mOrientationChanging) {
8356 if (!w.mDrawPending && !w.mCommitDrawPending) {
8357 if (DEBUG_ORIENTATION) Log.v(TAG,
8358 "Orientation not waiting for draw in "
8359 + w + ", surface " + w.mSurface);
8360 w.mOrientationChanging = false;
8361 }
8362 }
8363 }
8364
8365 if (w.mAttachedHidden) {
8366 if (!w.mLastHidden) {
8367 //dump();
8368 w.mLastHidden = true;
8369 if (SHOW_TRANSACTIONS) Log.i(
8370 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8371 if (w.mSurface != null) {
8372 try {
8373 w.mSurface.hide();
8374 } catch (RuntimeException e) {
8375 Log.w(TAG, "Exception hiding surface in " + w);
8376 }
8377 }
8378 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8379 }
8380 // If we are waiting for this window to handle an
8381 // orientation change, well, it is hidden, so
8382 // doesn't really matter. Note that this does
8383 // introduce a potential glitch if the window
8384 // becomes unhidden before it has drawn for the
8385 // new orientation.
8386 if (w.mOrientationChanging) {
8387 w.mOrientationChanging = false;
8388 if (DEBUG_ORIENTATION) Log.v(TAG,
8389 "Orientation change skips hidden " + w);
8390 }
8391 } else if (!w.isReadyForDisplay()) {
8392 if (!w.mLastHidden) {
8393 //dump();
8394 w.mLastHidden = true;
8395 if (SHOW_TRANSACTIONS) Log.i(
8396 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8397 if (w.mSurface != null) {
8398 try {
8399 w.mSurface.hide();
8400 } catch (RuntimeException e) {
8401 Log.w(TAG, "Exception exception hiding surface in " + w);
8402 }
8403 }
8404 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8405 }
8406 // If we are waiting for this window to handle an
8407 // orientation change, well, it is hidden, so
8408 // doesn't really matter. Note that this does
8409 // introduce a potential glitch if the window
8410 // becomes unhidden before it has drawn for the
8411 // new orientation.
8412 if (w.mOrientationChanging) {
8413 w.mOrientationChanging = false;
8414 if (DEBUG_ORIENTATION) Log.v(TAG,
8415 "Orientation change skips hidden " + w);
8416 }
8417 } else if (w.mLastLayer != w.mAnimLayer
8418 || w.mLastAlpha != w.mShownAlpha
8419 || w.mLastDsDx != w.mDsDx
8420 || w.mLastDtDx != w.mDtDx
8421 || w.mLastDsDy != w.mDsDy
8422 || w.mLastDtDy != w.mDtDy
8423 || w.mLastHScale != w.mHScale
8424 || w.mLastVScale != w.mVScale
8425 || w.mLastHidden) {
8426 displayed = true;
8427 w.mLastAlpha = w.mShownAlpha;
8428 w.mLastLayer = w.mAnimLayer;
8429 w.mLastDsDx = w.mDsDx;
8430 w.mLastDtDx = w.mDtDx;
8431 w.mLastDsDy = w.mDsDy;
8432 w.mLastDtDy = w.mDtDy;
8433 w.mLastHScale = w.mHScale;
8434 w.mLastVScale = w.mVScale;
8435 if (SHOW_TRANSACTIONS) Log.i(
8436 TAG, " SURFACE " + w.mSurface + ": alpha="
8437 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8438 if (w.mSurface != null) {
8439 try {
8440 w.mSurface.setAlpha(w.mShownAlpha);
8441 w.mSurface.setLayer(w.mAnimLayer);
8442 w.mSurface.setMatrix(
8443 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8444 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8445 } catch (RuntimeException e) {
8446 Log.w(TAG, "Error updating surface in " + w, e);
8447 if (!recoveringMemory) {
8448 reclaimSomeSurfaceMemoryLocked(w, "update");
8449 }
8450 }
8451 }
8452
8453 if (w.mLastHidden && !w.mDrawPending
8454 && !w.mCommitDrawPending
8455 && !w.mReadyToShow) {
8456 if (SHOW_TRANSACTIONS) Log.i(
8457 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8458 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8459 + " during relayout");
8460 if (showSurfaceRobustlyLocked(w)) {
8461 w.mHasDrawn = true;
8462 w.mLastHidden = false;
8463 } else {
8464 w.mOrientationChanging = false;
8465 }
8466 }
8467 if (w.mSurface != null) {
8468 w.mToken.hasVisible = true;
8469 }
8470 } else {
8471 displayed = true;
8472 }
8473
8474 if (displayed) {
8475 if (!covered) {
8476 if (attrs.width == LayoutParams.FILL_PARENT
8477 && attrs.height == LayoutParams.FILL_PARENT) {
8478 covered = true;
8479 }
8480 }
8481 if (w.mOrientationChanging) {
8482 if (w.mDrawPending || w.mCommitDrawPending) {
8483 orientationChangeComplete = false;
8484 if (DEBUG_ORIENTATION) Log.v(TAG,
8485 "Orientation continue waiting for draw in " + w);
8486 } else {
8487 w.mOrientationChanging = false;
8488 if (DEBUG_ORIENTATION) Log.v(TAG,
8489 "Orientation change complete in " + w);
8490 }
8491 }
8492 w.mToken.hasVisible = true;
8493 }
8494 } else if (w.mOrientationChanging) {
8495 if (DEBUG_ORIENTATION) Log.v(TAG,
8496 "Orientation change skips hidden " + w);
8497 w.mOrientationChanging = false;
8498 }
8499
8500 final boolean canBeSeen = w.isDisplayedLw();
8501
8502 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8503 focusDisplayed = true;
8504 }
8505
8506 // Update effect.
8507 if (!obscured) {
8508 if (w.mSurface != null) {
8509 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8510 holdScreen = w.mSession;
8511 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008512 if (!syswin && w.mAttrs.screenBrightness >= 0
8513 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008514 screenBrightness = w.mAttrs.screenBrightness;
8515 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008516 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8517 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8518 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8519 syswin = true;
8520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008522
8523 boolean opaqueDrawn = w.isOpaqueDrawn();
8524 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008525 // This window completely covers everything behind it,
8526 // so we want to leave all of them as unblurred (for
8527 // performance reasons).
8528 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008529 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8530 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008531 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008532 obscured = true;
8533 if (mBackgroundFillerSurface == null) {
8534 try {
8535 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8536 0, dw, dh,
8537 PixelFormat.OPAQUE,
8538 Surface.FX_SURFACE_NORMAL);
8539 } catch (Exception e) {
8540 Log.e(TAG, "Exception creating filler surface", e);
8541 }
8542 }
8543 try {
8544 mBackgroundFillerSurface.setPosition(0, 0);
8545 mBackgroundFillerSurface.setSize(dw, dh);
8546 // Using the same layer as Dim because they will never be shown at the
8547 // same time.
8548 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8549 mBackgroundFillerSurface.show();
8550 } catch (RuntimeException e) {
8551 Log.e(TAG, "Exception showing filler surface");
8552 }
8553 backgroundFillerShown = true;
8554 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 } else if (canBeSeen && !obscured &&
8556 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8557 if (localLOGV) Log.v(TAG, "Win " + w
8558 + ": blurring=" + blurring
8559 + " obscured=" + obscured
8560 + " displayed=" + displayed);
8561 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8562 if (!dimming) {
8563 //Log.i(TAG, "DIM BEHIND: " + w);
8564 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008565 if (mDimAnimator == null) {
8566 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008567 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008568 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008569 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008570 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008571 }
8572 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8573 if (!blurring) {
8574 //Log.i(TAG, "BLUR BEHIND: " + w);
8575 blurring = true;
8576 mBlurShown = true;
8577 if (mBlurSurface == null) {
8578 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8579 + mBlurSurface + ": CREATE");
8580 try {
Romain Guy06882f82009-06-10 13:36:04 -07008581 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 -1, 16, 16,
8583 PixelFormat.OPAQUE,
8584 Surface.FX_SURFACE_BLUR);
8585 } catch (Exception e) {
8586 Log.e(TAG, "Exception creating Blur surface", e);
8587 }
8588 }
8589 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8590 + mBlurSurface + ": SHOW pos=(0,0) (" +
8591 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8592 if (mBlurSurface != null) {
8593 mBlurSurface.setPosition(0, 0);
8594 mBlurSurface.setSize(dw, dh);
8595 try {
8596 mBlurSurface.show();
8597 } catch (RuntimeException e) {
8598 Log.w(TAG, "Failure showing blur surface", e);
8599 }
8600 }
8601 }
8602 mBlurSurface.setLayer(w.mAnimLayer-2);
8603 }
8604 }
8605 }
8606 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008607
8608 if (backgroundFillerShown == false && mBackgroundFillerShown) {
8609 mBackgroundFillerShown = false;
8610 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
8611 try {
8612 mBackgroundFillerSurface.hide();
8613 } catch (RuntimeException e) {
8614 Log.e(TAG, "Exception hiding filler surface", e);
8615 }
8616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008617
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008618 if (mDimAnimator != null && mDimAnimator.mDimShown) {
8619 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008620 }
Romain Guy06882f82009-06-10 13:36:04 -07008621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008622 if (!blurring && mBlurShown) {
8623 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8624 + ": HIDE");
8625 try {
8626 mBlurSurface.hide();
8627 } catch (IllegalArgumentException e) {
8628 Log.w(TAG, "Illegal argument exception hiding blur surface");
8629 }
8630 mBlurShown = false;
8631 }
8632
8633 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8634 } catch (RuntimeException e) {
8635 Log.e(TAG, "Unhandled exception in Window Manager", e);
8636 }
8637
8638 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008640 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8641 "With display frozen, orientationChangeComplete="
8642 + orientationChangeComplete);
8643 if (orientationChangeComplete) {
8644 if (mWindowsFreezingScreen) {
8645 mWindowsFreezingScreen = false;
8646 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8647 }
8648 if (mAppsFreezingScreen == 0) {
8649 stopFreezingDisplayLocked();
8650 }
8651 }
Romain Guy06882f82009-06-10 13:36:04 -07008652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008653 i = mResizingWindows.size();
8654 if (i > 0) {
8655 do {
8656 i--;
8657 WindowState win = mResizingWindows.get(i);
8658 try {
8659 win.mClient.resized(win.mFrame.width(),
8660 win.mFrame.height(), win.mLastContentInsets,
8661 win.mLastVisibleInsets, win.mDrawPending);
8662 win.mContentInsetsChanged = false;
8663 win.mVisibleInsetsChanged = false;
8664 } catch (RemoteException e) {
8665 win.mOrientationChanging = false;
8666 }
8667 } while (i > 0);
8668 mResizingWindows.clear();
8669 }
Romain Guy06882f82009-06-10 13:36:04 -07008670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008671 // Destroy the surface of any windows that are no longer visible.
8672 i = mDestroySurface.size();
8673 if (i > 0) {
8674 do {
8675 i--;
8676 WindowState win = mDestroySurface.get(i);
8677 win.mDestroying = false;
8678 if (mInputMethodWindow == win) {
8679 mInputMethodWindow = null;
8680 }
8681 win.destroySurfaceLocked();
8682 } while (i > 0);
8683 mDestroySurface.clear();
8684 }
8685
8686 // Time to remove any exiting tokens?
8687 for (i=mExitingTokens.size()-1; i>=0; i--) {
8688 WindowToken token = mExitingTokens.get(i);
8689 if (!token.hasVisible) {
8690 mExitingTokens.remove(i);
8691 }
8692 }
8693
8694 // Time to remove any exiting applications?
8695 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8696 AppWindowToken token = mExitingAppTokens.get(i);
8697 if (!token.hasVisible && !mClosingApps.contains(token)) {
8698 mAppTokens.remove(token);
8699 mExitingAppTokens.remove(i);
8700 }
8701 }
8702
8703 if (focusDisplayed) {
8704 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8705 }
8706 if (animating) {
8707 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8708 }
8709 mQueue.setHoldScreenLocked(holdScreen != null);
8710 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8711 mPowerManager.setScreenBrightnessOverride(-1);
8712 } else {
8713 mPowerManager.setScreenBrightnessOverride((int)
8714 (screenBrightness * Power.BRIGHTNESS_ON));
8715 }
8716 if (holdScreen != mHoldingScreenOn) {
8717 mHoldingScreenOn = holdScreen;
8718 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8719 mH.sendMessage(m);
8720 }
8721 }
8722
8723 void requestAnimationLocked(long delay) {
8724 if (!mAnimationPending) {
8725 mAnimationPending = true;
8726 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8727 }
8728 }
Romain Guy06882f82009-06-10 13:36:04 -07008729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 /**
8731 * Have the surface flinger show a surface, robustly dealing with
8732 * error conditions. In particular, if there is not enough memory
8733 * to show the surface, then we will try to get rid of other surfaces
8734 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07008735 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008736 * @return Returns true if the surface was successfully shown.
8737 */
8738 boolean showSurfaceRobustlyLocked(WindowState win) {
8739 try {
8740 if (win.mSurface != null) {
8741 win.mSurface.show();
8742 }
8743 return true;
8744 } catch (RuntimeException e) {
8745 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8746 }
Romain Guy06882f82009-06-10 13:36:04 -07008747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07008749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008750 return false;
8751 }
Romain Guy06882f82009-06-10 13:36:04 -07008752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8754 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07008755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008756 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8757 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07008758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008759 if (mForceRemoves == null) {
8760 mForceRemoves = new ArrayList<WindowState>();
8761 }
Romain Guy06882f82009-06-10 13:36:04 -07008762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008763 long callingIdentity = Binder.clearCallingIdentity();
8764 try {
8765 // There was some problem... first, do a sanity check of the
8766 // window list to make sure we haven't left any dangling surfaces
8767 // around.
8768 int N = mWindows.size();
8769 boolean leakedSurface = false;
8770 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8771 for (int i=0; i<N; i++) {
8772 WindowState ws = (WindowState)mWindows.get(i);
8773 if (ws.mSurface != null) {
8774 if (!mSessions.contains(ws.mSession)) {
8775 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8776 + ws + " surface=" + ws.mSurface
8777 + " token=" + win.mToken
8778 + " pid=" + ws.mSession.mPid
8779 + " uid=" + ws.mSession.mUid);
8780 ws.mSurface.clear();
8781 ws.mSurface = null;
8782 mForceRemoves.add(ws);
8783 i--;
8784 N--;
8785 leakedSurface = true;
8786 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
8787 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
8788 + ws + " surface=" + ws.mSurface
8789 + " token=" + win.mAppToken);
8790 ws.mSurface.clear();
8791 ws.mSurface = null;
8792 leakedSurface = true;
8793 }
8794 }
8795 }
Romain Guy06882f82009-06-10 13:36:04 -07008796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 boolean killedApps = false;
8798 if (!leakedSurface) {
8799 Log.w(TAG, "No leaked surfaces; killing applicatons!");
8800 SparseIntArray pidCandidates = new SparseIntArray();
8801 for (int i=0; i<N; i++) {
8802 WindowState ws = (WindowState)mWindows.get(i);
8803 if (ws.mSurface != null) {
8804 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
8805 }
8806 }
8807 if (pidCandidates.size() > 0) {
8808 int[] pids = new int[pidCandidates.size()];
8809 for (int i=0; i<pids.length; i++) {
8810 pids[i] = pidCandidates.keyAt(i);
8811 }
8812 try {
8813 if (mActivityManager.killPidsForMemory(pids)) {
8814 killedApps = true;
8815 }
8816 } catch (RemoteException e) {
8817 }
8818 }
8819 }
Romain Guy06882f82009-06-10 13:36:04 -07008820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008821 if (leakedSurface || killedApps) {
8822 // We managed to reclaim some memory, so get rid of the trouble
8823 // surface and ask the app to request another one.
8824 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
8825 if (surface != null) {
8826 surface.clear();
8827 win.mSurface = null;
8828 }
Romain Guy06882f82009-06-10 13:36:04 -07008829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008830 try {
8831 win.mClient.dispatchGetNewSurface();
8832 } catch (RemoteException e) {
8833 }
8834 }
8835 } finally {
8836 Binder.restoreCallingIdentity(callingIdentity);
8837 }
8838 }
Romain Guy06882f82009-06-10 13:36:04 -07008839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008840 private boolean updateFocusedWindowLocked(int mode) {
8841 WindowState newFocus = computeFocusedWindowLocked();
8842 if (mCurrentFocus != newFocus) {
8843 // This check makes sure that we don't already have the focus
8844 // change message pending.
8845 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
8846 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
8847 if (localLOGV) Log.v(
8848 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
8849 final WindowState oldFocus = mCurrentFocus;
8850 mCurrentFocus = newFocus;
8851 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07008852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 final WindowState imWindow = mInputMethodWindow;
8854 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008855 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008856 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008857 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
8858 mLayoutNeeded = true;
8859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008860 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
8861 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008862 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
8863 // Client will do the layout, but we need to assign layers
8864 // for handleNewWindowLocked() below.
8865 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008866 }
8867 }
Romain Guy06882f82009-06-10 13:36:04 -07008868
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008869 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
8870 mKeyWaiter.handleNewWindowLocked(newFocus);
8871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 return true;
8873 }
8874 return false;
8875 }
8876
8877 private WindowState computeFocusedWindowLocked() {
8878 WindowState result = null;
8879 WindowState win;
8880
8881 int i = mWindows.size() - 1;
8882 int nextAppIndex = mAppTokens.size()-1;
8883 WindowToken nextApp = nextAppIndex >= 0
8884 ? mAppTokens.get(nextAppIndex) : null;
8885
8886 while (i >= 0) {
8887 win = (WindowState)mWindows.get(i);
8888
8889 if (localLOGV || DEBUG_FOCUS) Log.v(
8890 TAG, "Looking for focus: " + i
8891 + " = " + win
8892 + ", flags=" + win.mAttrs.flags
8893 + ", canReceive=" + win.canReceiveKeys());
8894
8895 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07008896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008897 // If this window's application has been removed, just skip it.
8898 if (thisApp != null && thisApp.removed) {
8899 i--;
8900 continue;
8901 }
Romain Guy06882f82009-06-10 13:36:04 -07008902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008903 // If there is a focused app, don't allow focus to go to any
8904 // windows below it. If this is an application window, step
8905 // through the app tokens until we find its app.
8906 if (thisApp != null && nextApp != null && thisApp != nextApp
8907 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
8908 int origAppIndex = nextAppIndex;
8909 while (nextAppIndex > 0) {
8910 if (nextApp == mFocusedApp) {
8911 // Whoops, we are below the focused app... no focus
8912 // for you!
8913 if (localLOGV || DEBUG_FOCUS) Log.v(
8914 TAG, "Reached focused app: " + mFocusedApp);
8915 return null;
8916 }
8917 nextAppIndex--;
8918 nextApp = mAppTokens.get(nextAppIndex);
8919 if (nextApp == thisApp) {
8920 break;
8921 }
8922 }
8923 if (thisApp != nextApp) {
8924 // Uh oh, the app token doesn't exist! This shouldn't
8925 // happen, but if it does we can get totally hosed...
8926 // so restart at the original app.
8927 nextAppIndex = origAppIndex;
8928 nextApp = mAppTokens.get(nextAppIndex);
8929 }
8930 }
8931
8932 // Dispatch to this window if it is wants key events.
8933 if (win.canReceiveKeys()) {
8934 if (DEBUG_FOCUS) Log.v(
8935 TAG, "Found focus @ " + i + " = " + win);
8936 result = win;
8937 break;
8938 }
8939
8940 i--;
8941 }
8942
8943 return result;
8944 }
8945
8946 private void startFreezingDisplayLocked() {
8947 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07008948 // Freezing the display also suspends key event delivery, to
8949 // keep events from going astray while the display is reconfigured.
8950 // If someone has changed orientation again while the screen is
8951 // still frozen, the events will continue to be blocked while the
8952 // successive orientation change is processed. To prevent spurious
8953 // ANRs, we reset the event dispatch timeout in this case.
8954 synchronized (mKeyWaiter) {
8955 mKeyWaiter.mWasFrozen = true;
8956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008957 return;
8958 }
Romain Guy06882f82009-06-10 13:36:04 -07008959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008960 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07008961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008962 long now = SystemClock.uptimeMillis();
8963 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
8964 if (mFreezeGcPending != 0) {
8965 if (now > (mFreezeGcPending+1000)) {
8966 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
8967 mH.removeMessages(H.FORCE_GC);
8968 Runtime.getRuntime().gc();
8969 mFreezeGcPending = now;
8970 }
8971 } else {
8972 mFreezeGcPending = now;
8973 }
Romain Guy06882f82009-06-10 13:36:04 -07008974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008975 mDisplayFrozen = true;
8976 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8977 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8978 mAppTransitionReady = true;
8979 }
Romain Guy06882f82009-06-10 13:36:04 -07008980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008981 if (PROFILE_ORIENTATION) {
8982 File file = new File("/data/system/frozen");
8983 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
8984 }
8985 Surface.freezeDisplay(0);
8986 }
Romain Guy06882f82009-06-10 13:36:04 -07008987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008988 private void stopFreezingDisplayLocked() {
8989 if (!mDisplayFrozen) {
8990 return;
8991 }
Romain Guy06882f82009-06-10 13:36:04 -07008992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008993 mDisplayFrozen = false;
8994 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
8995 if (PROFILE_ORIENTATION) {
8996 Debug.stopMethodTracing();
8997 }
8998 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07008999
Chris Tate2ad63a92009-03-25 17:36:48 -07009000 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9001 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009002 synchronized (mKeyWaiter) {
9003 mKeyWaiter.mWasFrozen = true;
9004 mKeyWaiter.notifyAll();
9005 }
9006
9007 // A little kludge: a lot could have happened while the
9008 // display was frozen, so now that we are coming back we
9009 // do a gc so that any remote references the system
9010 // processes holds on others can be released if they are
9011 // no longer needed.
9012 mH.removeMessages(H.FORCE_GC);
9013 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9014 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009016 mScreenFrozenLock.release();
9017 }
Romain Guy06882f82009-06-10 13:36:04 -07009018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009019 @Override
9020 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9021 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9022 != PackageManager.PERMISSION_GRANTED) {
9023 pw.println("Permission Denial: can't dump WindowManager from from pid="
9024 + Binder.getCallingPid()
9025 + ", uid=" + Binder.getCallingUid());
9026 return;
9027 }
Romain Guy06882f82009-06-10 13:36:04 -07009028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009029 synchronized(mWindowMap) {
9030 pw.println("Current Window Manager state:");
9031 for (int i=mWindows.size()-1; i>=0; i--) {
9032 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009033 pw.print(" Window #"); pw.print(i); pw.print(' ');
9034 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009035 w.dump(pw, " ");
9036 }
9037 if (mInputMethodDialogs.size() > 0) {
9038 pw.println(" ");
9039 pw.println(" Input method dialogs:");
9040 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9041 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009042 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009043 }
9044 }
9045 if (mPendingRemove.size() > 0) {
9046 pw.println(" ");
9047 pw.println(" Remove pending for:");
9048 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9049 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009050 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9051 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009052 w.dump(pw, " ");
9053 }
9054 }
9055 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9056 pw.println(" ");
9057 pw.println(" Windows force removing:");
9058 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9059 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009060 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9061 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009062 w.dump(pw, " ");
9063 }
9064 }
9065 if (mDestroySurface.size() > 0) {
9066 pw.println(" ");
9067 pw.println(" Windows waiting to destroy their surface:");
9068 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9069 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009070 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9071 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009072 w.dump(pw, " ");
9073 }
9074 }
9075 if (mLosingFocus.size() > 0) {
9076 pw.println(" ");
9077 pw.println(" Windows losing focus:");
9078 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9079 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009080 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9081 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082 w.dump(pw, " ");
9083 }
9084 }
9085 if (mSessions.size() > 0) {
9086 pw.println(" ");
9087 pw.println(" All active sessions:");
9088 Iterator<Session> it = mSessions.iterator();
9089 while (it.hasNext()) {
9090 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009091 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009092 s.dump(pw, " ");
9093 }
9094 }
9095 if (mTokenMap.size() > 0) {
9096 pw.println(" ");
9097 pw.println(" All tokens:");
9098 Iterator<WindowToken> it = mTokenMap.values().iterator();
9099 while (it.hasNext()) {
9100 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009101 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 token.dump(pw, " ");
9103 }
9104 }
9105 if (mTokenList.size() > 0) {
9106 pw.println(" ");
9107 pw.println(" Window token list:");
9108 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009109 pw.print(" #"); pw.print(i); pw.print(": ");
9110 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009111 }
9112 }
9113 if (mAppTokens.size() > 0) {
9114 pw.println(" ");
9115 pw.println(" Application tokens in Z order:");
9116 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009117 pw.print(" App #"); pw.print(i); pw.print(": ");
9118 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009119 }
9120 }
9121 if (mFinishedStarting.size() > 0) {
9122 pw.println(" ");
9123 pw.println(" Finishing start of application tokens:");
9124 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9125 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009126 pw.print(" Finished Starting #"); pw.print(i);
9127 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009128 token.dump(pw, " ");
9129 }
9130 }
9131 if (mExitingTokens.size() > 0) {
9132 pw.println(" ");
9133 pw.println(" Exiting tokens:");
9134 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9135 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009136 pw.print(" Exiting #"); pw.print(i);
9137 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009138 token.dump(pw, " ");
9139 }
9140 }
9141 if (mExitingAppTokens.size() > 0) {
9142 pw.println(" ");
9143 pw.println(" Exiting application tokens:");
9144 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9145 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009146 pw.print(" Exiting App #"); pw.print(i);
9147 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009148 token.dump(pw, " ");
9149 }
9150 }
9151 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009152 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9153 pw.print(" mLastFocus="); pw.println(mLastFocus);
9154 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9155 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9156 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9157 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9158 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9159 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9160 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9161 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009162 if (mDimAnimator != null) {
9163 mDimAnimator.printTo(pw);
9164 } else {
9165 pw.print( " no DimAnimator ");
9166 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009167 pw.print(" mInputMethodAnimLayerAdjustment=");
9168 pw.println(mInputMethodAnimLayerAdjustment);
9169 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9170 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9171 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9172 pw.print(" mRotation="); pw.print(mRotation);
9173 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9174 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9175 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9176 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9177 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9178 pw.print(" mNextAppTransition=0x");
9179 pw.print(Integer.toHexString(mNextAppTransition));
9180 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9181 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9182 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9183 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9184 if (mOpeningApps.size() > 0) {
9185 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9186 }
9187 if (mClosingApps.size() > 0) {
9188 pw.print(" mClosingApps="); pw.println(mClosingApps);
9189 }
9190 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9191 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009192 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009193 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9194 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9195 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9196 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9197 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9198 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009199 }
9200 }
9201
9202 public void monitor() {
9203 synchronized (mWindowMap) { }
9204 synchronized (mKeyguardDisabled) { }
9205 synchronized (mKeyWaiter) { }
9206 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009207
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07009208 public void virtualKeyFeedback(KeyEvent event) {
9209 mPolicy.keyFeedbackFromInput(event);
9210 }
9211
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009212 /**
9213 * DimAnimator class that controls the dim animation. This holds the surface and
9214 * all state used for dim animation.
9215 */
9216 private static class DimAnimator {
9217 Surface mDimSurface;
9218 boolean mDimShown = false;
9219 float mDimCurrentAlpha;
9220 float mDimTargetAlpha;
9221 float mDimDeltaPerMs;
9222 long mLastDimAnimTime;
9223
9224 DimAnimator (SurfaceSession session) {
9225 if (mDimSurface == null) {
9226 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9227 + mDimSurface + ": CREATE");
9228 try {
9229 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9230 Surface.FX_SURFACE_DIM);
9231 } catch (Exception e) {
9232 Log.e(TAG, "Exception creating Dim surface", e);
9233 }
9234 }
9235 }
9236
9237 /**
9238 * Show the dim surface.
9239 */
9240 void show(int dw, int dh) {
9241 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9242 dw + "x" + dh + ")");
9243 mDimShown = true;
9244 try {
9245 mDimSurface.setPosition(0, 0);
9246 mDimSurface.setSize(dw, dh);
9247 mDimSurface.show();
9248 } catch (RuntimeException e) {
9249 Log.w(TAG, "Failure showing dim surface", e);
9250 }
9251 }
9252
9253 /**
9254 * Set's the dim surface's layer and update dim parameters that will be used in
9255 * {@link updateSurface} after all windows are examined.
9256 */
9257 void updateParameters(WindowState w, long currentTime) {
9258 mDimSurface.setLayer(w.mAnimLayer-1);
9259
9260 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9261 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9262 if (mDimTargetAlpha != target) {
9263 // If the desired dim level has changed, then
9264 // start an animation to it.
9265 mLastDimAnimTime = currentTime;
9266 long duration = (w.mAnimating && w.mAnimation != null)
9267 ? w.mAnimation.computeDurationHint()
9268 : DEFAULT_DIM_DURATION;
9269 if (target > mDimTargetAlpha) {
9270 // This is happening behind the activity UI,
9271 // so we can make it run a little longer to
9272 // give a stronger impression without disrupting
9273 // the user.
9274 duration *= DIM_DURATION_MULTIPLIER;
9275 }
9276 if (duration < 1) {
9277 // Don't divide by zero
9278 duration = 1;
9279 }
9280 mDimTargetAlpha = target;
9281 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9282 }
9283 }
9284
9285 /**
9286 * Updating the surface's alpha. Returns true if the animation continues, or returns
9287 * false when the animation is finished and the dim surface is hidden.
9288 */
9289 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9290 if (!dimming) {
9291 if (mDimTargetAlpha != 0) {
9292 mLastDimAnimTime = currentTime;
9293 mDimTargetAlpha = 0;
9294 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9295 }
9296 }
9297
9298 boolean animating = false;
9299 if (mLastDimAnimTime != 0) {
9300 mDimCurrentAlpha += mDimDeltaPerMs
9301 * (currentTime-mLastDimAnimTime);
9302 boolean more = true;
9303 if (displayFrozen) {
9304 // If the display is frozen, there is no reason to animate.
9305 more = false;
9306 } else if (mDimDeltaPerMs > 0) {
9307 if (mDimCurrentAlpha > mDimTargetAlpha) {
9308 more = false;
9309 }
9310 } else if (mDimDeltaPerMs < 0) {
9311 if (mDimCurrentAlpha < mDimTargetAlpha) {
9312 more = false;
9313 }
9314 } else {
9315 more = false;
9316 }
9317
9318 // Do we need to continue animating?
9319 if (more) {
9320 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9321 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9322 mLastDimAnimTime = currentTime;
9323 mDimSurface.setAlpha(mDimCurrentAlpha);
9324 animating = true;
9325 } else {
9326 mDimCurrentAlpha = mDimTargetAlpha;
9327 mLastDimAnimTime = 0;
9328 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9329 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9330 mDimSurface.setAlpha(mDimCurrentAlpha);
9331 if (!dimming) {
9332 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9333 + ": HIDE");
9334 try {
9335 mDimSurface.hide();
9336 } catch (RuntimeException e) {
9337 Log.w(TAG, "Illegal argument exception hiding dim surface");
9338 }
9339 mDimShown = false;
9340 }
9341 }
9342 }
9343 return animating;
9344 }
9345
9346 public void printTo(PrintWriter pw) {
9347 pw.print(" mDimShown="); pw.print(mDimShown);
9348 pw.print(" current="); pw.print(mDimCurrentAlpha);
9349 pw.print(" target="); pw.print(mDimTargetAlpha);
9350 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9351 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9352 }
9353 }
9354
9355 /**
9356 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9357 * This is used for opening/closing transition for apps in compatible mode.
9358 */
9359 private static class FadeInOutAnimation extends Animation {
9360 int mWidth;
9361 boolean mFadeIn;
9362
9363 public FadeInOutAnimation(boolean fadeIn) {
9364 setInterpolator(new AccelerateInterpolator());
9365 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9366 mFadeIn = fadeIn;
9367 }
9368
9369 @Override
9370 protected void applyTransformation(float interpolatedTime, Transformation t) {
9371 float x = interpolatedTime;
9372 if (!mFadeIn) {
9373 x = 1.0f - x; // reverse the interpolation for fade out
9374 }
9375 if (x < 0.5) {
9376 // move the window out of the screen.
9377 t.getMatrix().setTranslate(mWidth, 0);
9378 } else {
9379 t.getMatrix().setTranslate(0, 0);// show
9380 t.setAlpha((x - 0.5f) * 2);
9381 }
9382 }
9383
9384 @Override
9385 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9386 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9387 mWidth = width;
9388 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009389
9390 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07009391 public int getZAdjustment() {
9392 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009393 }
9394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009395}