blob: 69c48d39457bc4dc2145b917d0b85b13ef119547 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import static android.os.LocalPowerManager.CHEEK_EVENT;
20import static android.os.LocalPowerManager.OTHER_EVENT;
21import static android.os.LocalPowerManager.TOUCH_EVENT;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -070022import static android.os.LocalPowerManager.LONG_TOUCH_EVENT;
23import static android.os.LocalPowerManager.TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
25import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
26import static android.view.WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070027import static android.view.WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
29import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -070030import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
32import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
33import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070034import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
36import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
37import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_GPU;
38import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_HARDWARE;
39import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
40import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
41import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
42import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
43import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070044import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
46import com.android.internal.app.IBatteryStats;
47import com.android.internal.policy.PolicyManager;
48import com.android.internal.view.IInputContext;
49import com.android.internal.view.IInputMethodClient;
50import com.android.internal.view.IInputMethodManager;
51import com.android.server.KeyInputQueue.QueuedEvent;
52import com.android.server.am.BatteryStatsService;
53
54import android.Manifest;
55import android.app.ActivityManagerNative;
56import android.app.IActivityManager;
57import android.content.Context;
58import android.content.pm.ActivityInfo;
59import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070060import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.res.Configuration;
62import android.graphics.Matrix;
63import android.graphics.PixelFormat;
64import android.graphics.Rect;
65import android.graphics.Region;
66import android.os.BatteryStats;
67import android.os.Binder;
68import android.os.Debug;
69import android.os.Handler;
70import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070071import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.LocalPowerManager;
73import android.os.Looper;
74import android.os.Message;
75import android.os.Parcel;
76import android.os.ParcelFileDescriptor;
77import android.os.Power;
78import android.os.PowerManager;
79import android.os.Process;
80import android.os.RemoteException;
81import android.os.ServiceManager;
82import android.os.SystemClock;
83import android.os.SystemProperties;
84import android.os.TokenWatcher;
85import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070086import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.EventLog;
88import android.util.Log;
89import android.util.SparseIntArray;
90import android.view.Display;
91import android.view.Gravity;
92import android.view.IApplicationToken;
93import android.view.IOnKeyguardExitResult;
94import android.view.IRotationWatcher;
95import android.view.IWindow;
96import android.view.IWindowManager;
97import android.view.IWindowSession;
98import android.view.KeyEvent;
99import android.view.MotionEvent;
100import android.view.RawInputEvent;
101import android.view.Surface;
102import android.view.SurfaceSession;
103import android.view.View;
104import android.view.ViewTreeObserver;
105import android.view.WindowManager;
106import android.view.WindowManagerImpl;
107import android.view.WindowManagerPolicy;
108import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700109import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.animation.Animation;
111import android.view.animation.AnimationUtils;
112import android.view.animation.Transformation;
113
114import java.io.BufferedWriter;
115import java.io.File;
116import java.io.FileDescriptor;
117import java.io.IOException;
118import java.io.OutputStream;
119import java.io.OutputStreamWriter;
120import java.io.PrintWriter;
121import java.io.StringWriter;
122import java.net.Socket;
123import java.util.ArrayList;
124import java.util.HashMap;
125import java.util.HashSet;
126import java.util.Iterator;
127import java.util.List;
128
129/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700130public class WindowManagerService extends IWindowManager.Stub
131 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 static final String TAG = "WindowManager";
133 static final boolean DEBUG = false;
134 static final boolean DEBUG_FOCUS = false;
135 static final boolean DEBUG_ANIM = false;
136 static final boolean DEBUG_LAYERS = false;
137 static final boolean DEBUG_INPUT = false;
138 static final boolean DEBUG_INPUT_METHOD = false;
139 static final boolean DEBUG_VISIBILITY = false;
140 static final boolean DEBUG_ORIENTATION = false;
141 static final boolean DEBUG_APP_TRANSITIONS = false;
142 static final boolean DEBUG_STARTING_WINDOW = false;
143 static final boolean DEBUG_REORDER = false;
144 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700145 static final boolean MEASURE_LATENCY = false;
146 static private LatencyTimer lt;
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final boolean PROFILE_ORIENTATION = false;
149 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700150 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /** How long to wait for first key repeat, in milliseconds */
155 static final int KEY_REPEAT_FIRST_DELAY = 750;
Romain Guy06882f82009-06-10 13:36:04 -0700156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 /** How long to wait for subsequent key repeats, in milliseconds */
158 static final int KEY_REPEAT_DELAY = 50;
159
160 /** How much to multiply the policy's type layer, to reserve room
161 * for multiple windows of the same type and Z-ordering adjustment
162 * with TYPE_LAYER_OFFSET. */
163 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
166 * or below others in the same layer. */
167 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /** How much to increment the layer for each window, to reserve room
170 * for effect surfaces between them.
171 */
172 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 /** The maximum length we will accept for a loaded animation duration:
175 * this is 10 seconds.
176 */
177 static final int MAX_ANIMATION_DURATION = 10*1000;
178
179 /** Amount of time (in milliseconds) to animate the dim surface from one
180 * value to another, when no window animation is driving it.
181 */
182 static final int DEFAULT_DIM_DURATION = 200;
183
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700184 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
185 * compatible windows.
186 */
187 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 /** Adjustment to time to perform a dim, to make it more dramatic.
190 */
191 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700192
Dianne Hackborncfaef692009-06-15 14:24:44 -0700193 static final int INJECT_FAILED = 0;
194 static final int INJECT_SUCCEEDED = 1;
195 static final int INJECT_NO_PERMISSION = -1;
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 static final int UPDATE_FOCUS_NORMAL = 0;
198 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
199 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
200 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700201
Michael Chane96440f2009-05-06 10:27:36 -0700202 /** The minimum time between dispatching touch events. */
203 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
204
205 // Last touch event time
206 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700207
Michael Chane96440f2009-05-06 10:27:36 -0700208 // Last touch event type
209 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700210
Michael Chane96440f2009-05-06 10:27:36 -0700211 // Time to wait before calling useractivity again. This saves CPU usage
212 // when we get a flood of touch events.
213 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
214
215 // Last time we call user activity
216 long mLastUserActivityCallTime = 0;
217
Romain Guy06882f82009-06-10 13:36:04 -0700218 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700219 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700222 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
224 /**
225 * Condition waited on by {@link #reenableKeyguard} to know the call to
226 * the window policy has finished.
227 */
228 private boolean mWaitingUntilKeyguardReenabled = false;
229
230
231 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
232 new Handler(), "WindowManagerService.mKeyguardDisabled") {
233 public void acquired() {
234 mPolicy.enableKeyguard(false);
235 }
236 public void released() {
237 synchronized (mKeyguardDisabled) {
238 mPolicy.enableKeyguard(true);
239 mWaitingUntilKeyguardReenabled = false;
240 mKeyguardDisabled.notifyAll();
241 }
242 }
243 };
244
245 final Context mContext;
246
247 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
252
253 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * All currently active sessions with clients.
259 */
260 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * Mapping from an IWindow IBinder to the server's Window object.
264 * This is also used as the lock for all of our state.
265 */
266 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
267
268 /**
269 * Mapping from a token IBinder to a WindowToken object.
270 */
271 final HashMap<IBinder, WindowToken> mTokenMap =
272 new HashMap<IBinder, WindowToken>();
273
274 /**
275 * The same tokens as mTokenMap, stored in a list for efficient iteration
276 * over them.
277 */
278 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
281 * Window tokens that are in the process of exiting, but still
282 * on screen for animations.
283 */
284 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
285
286 /**
287 * Z-ordered (bottom-most first) list of all application tokens, for
288 * controlling the ordering of windows in different applications. This
289 * contains WindowToken objects.
290 */
291 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
292
293 /**
294 * Application tokens that are in the process of exiting, but still
295 * on screen for animations.
296 */
297 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
298
299 /**
300 * List of window tokens that have finished starting their application,
301 * and now need to have the policy remove their windows.
302 */
303 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
304
305 /**
306 * Z-ordered (bottom-most first) list of all Window objects.
307 */
308 final ArrayList mWindows = new ArrayList();
309
310 /**
311 * Windows that are being resized. Used so we can tell the client about
312 * the resize after closing the transaction in which we resized the
313 * underlying surface.
314 */
315 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
316
317 /**
318 * Windows whose animations have ended and now must be removed.
319 */
320 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
321
322 /**
323 * Windows whose surface should be destroyed.
324 */
325 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
326
327 /**
328 * Windows that have lost input focus and are waiting for the new
329 * focus window to be displayed before they are told about this.
330 */
331 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
332
333 /**
334 * This is set when we have run out of memory, and will either be an empty
335 * list or contain windows that need to be force removed.
336 */
337 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700342 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 Surface mBlurSurface;
344 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 final float[] mTmpFloats = new float[9];
349
350 boolean mSafeMode;
351 boolean mDisplayEnabled = false;
352 boolean mSystemBooted = false;
353 int mRotation = 0;
354 int mRequestedRotation = 0;
355 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700356 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 ArrayList<IRotationWatcher> mRotationWatchers
358 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 boolean mLayoutNeeded = true;
361 boolean mAnimationPending = false;
362 boolean mDisplayFrozen = false;
363 boolean mWindowsFreezingScreen = false;
364 long mFreezeGcPending = 0;
365 int mAppsFreezingScreen = 0;
366
367 // This is held as long as we have the screen frozen, to give us time to
368 // perform a rotation animation when turning off shows the lock screen which
369 // changes the orientation.
370 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 // State management of app transitions. When we are preparing for a
373 // transition, mNextAppTransition will be the kind of transition to
374 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
375 // mOpeningApps and mClosingApps are the lists of tokens that will be
376 // made visible or hidden at the next transition.
377 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
378 boolean mAppTransitionReady = false;
379 boolean mAppTransitionTimeout = false;
380 boolean mStartingIconInTransition = false;
381 boolean mSkipAppTransitionAnimation = false;
382 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
383 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 //flag to detect fat touch events
386 boolean mFatTouch = false;
387 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 H mH = new H();
390
391 WindowState mCurrentFocus = null;
392 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 // This just indicates the window the input method is on top of, not
395 // necessarily the window its input is going to.
396 WindowState mInputMethodTarget = null;
397 WindowState mUpcomingInputMethodTarget = null;
398 boolean mInputMethodTargetWaitingAnim;
399 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 WindowState mInputMethodWindow = null;
402 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
403
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700404 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
405
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700406 // If non-null, this is the currently visible window that is associated
407 // with the wallpaper.
408 WindowState mWallpaperTarget = null;
409 int mWallpaperAnimLayerAdjustment;
410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 AppWindowToken mFocusedApp = null;
412
413 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 float mWindowAnimationScale = 1.0f;
416 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 final KeyWaiter mKeyWaiter = new KeyWaiter();
419 final KeyQ mQueue;
420 final InputDispatcherThread mInputThread;
421
422 // Who is holding the screen on.
423 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
426 * Whether the UI is currently running in touch mode (not showing
427 * navigational focus because the user is directly pressing the screen).
428 */
429 boolean mInTouchMode = false;
430
431 private ViewServer mViewServer;
432
433 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700434
Dianne Hackbornc485a602009-03-24 22:39:49 -0700435 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700436 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700437
438 // The frame use to limit the size of the app running in compatibility mode.
439 Rect mCompatibleScreenFrame = new Rect();
440 // The surface used to fill the outer rim of the app running in compatibility mode.
441 Surface mBackgroundFillerSurface = null;
442 boolean mBackgroundFillerShown = false;
443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 public static WindowManagerService main(Context context,
445 PowerManagerService pm, boolean haveInputMethods) {
446 WMThread thr = new WMThread(context, pm, haveInputMethods);
447 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 synchronized (thr) {
450 while (thr.mService == null) {
451 try {
452 thr.wait();
453 } catch (InterruptedException e) {
454 }
455 }
456 }
Romain Guy06882f82009-06-10 13:36:04 -0700457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 return thr.mService;
459 }
Romain Guy06882f82009-06-10 13:36:04 -0700460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 static class WMThread extends Thread {
462 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 private final Context mContext;
465 private final PowerManagerService mPM;
466 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 public WMThread(Context context, PowerManagerService pm,
469 boolean haveInputMethods) {
470 super("WindowManager");
471 mContext = context;
472 mPM = pm;
473 mHaveInputMethods = haveInputMethods;
474 }
Romain Guy06882f82009-06-10 13:36:04 -0700475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 public void run() {
477 Looper.prepare();
478 WindowManagerService s = new WindowManagerService(mContext, mPM,
479 mHaveInputMethods);
480 android.os.Process.setThreadPriority(
481 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 synchronized (this) {
484 mService = s;
485 notifyAll();
486 }
Romain Guy06882f82009-06-10 13:36:04 -0700487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 Looper.loop();
489 }
490 }
491
492 static class PolicyThread extends Thread {
493 private final WindowManagerPolicy mPolicy;
494 private final WindowManagerService mService;
495 private final Context mContext;
496 private final PowerManagerService mPM;
497 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 public PolicyThread(WindowManagerPolicy policy,
500 WindowManagerService service, Context context,
501 PowerManagerService pm) {
502 super("WindowManagerPolicy");
503 mPolicy = policy;
504 mService = service;
505 mContext = context;
506 mPM = pm;
507 }
Romain Guy06882f82009-06-10 13:36:04 -0700508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public void run() {
510 Looper.prepare();
511 //Looper.myLooper().setMessageLogging(new LogPrinter(
512 // Log.VERBOSE, "WindowManagerPolicy"));
513 android.os.Process.setThreadPriority(
514 android.os.Process.THREAD_PRIORITY_FOREGROUND);
515 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 synchronized (this) {
518 mRunning = true;
519 notifyAll();
520 }
Romain Guy06882f82009-06-10 13:36:04 -0700521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 Looper.loop();
523 }
524 }
525
526 private WindowManagerService(Context context, PowerManagerService pm,
527 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700528 if (MEASURE_LATENCY) {
529 lt = new LatencyTimer(100, 1000);
530 }
531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 mContext = context;
533 mHaveInputMethods = haveInputMethods;
534 mLimitedAlphaCompositing = context.getResources().getBoolean(
535 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 mPowerManager = pm;
538 mPowerManager.setPolicy(mPolicy);
539 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
540 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
541 "SCREEN_FROZEN");
542 mScreenFrozenLock.setReferenceCounted(false);
543
544 mActivityManager = ActivityManagerNative.getDefault();
545 mBatteryStats = BatteryStatsService.getService();
546
547 // Get persisted window scale setting
548 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
549 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
550 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
551 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700552
Michael Chan9f028e62009-08-04 17:37:46 -0700553 int max_events_per_sec = 35;
554 try {
555 max_events_per_sec = Integer.parseInt(SystemProperties
556 .get("windowsmgr.max_events_per_sec"));
557 if (max_events_per_sec < 1) {
558 max_events_per_sec = 35;
559 }
560 } catch (NumberFormatException e) {
561 }
562 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 mQueue = new KeyQ();
565
566 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
569 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 synchronized (thr) {
572 while (!thr.mRunning) {
573 try {
574 thr.wait();
575 } catch (InterruptedException e) {
576 }
577 }
578 }
Romain Guy06882f82009-06-10 13:36:04 -0700579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 // Add ourself to the Watchdog monitors.
583 Watchdog.getInstance().addMonitor(this);
584 }
585
586 @Override
587 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
588 throws RemoteException {
589 try {
590 return super.onTransact(code, data, reply, flags);
591 } catch (RuntimeException e) {
592 // The window manager only throws security exceptions, so let's
593 // log all others.
594 if (!(e instanceof SecurityException)) {
595 Log.e(TAG, "Window Manager Crash", e);
596 }
597 throw e;
598 }
599 }
600
601 private void placeWindowAfter(Object pos, WindowState window) {
602 final int i = mWindows.indexOf(pos);
603 if (localLOGV || DEBUG_FOCUS) Log.v(
604 TAG, "Adding window " + window + " at "
605 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
606 mWindows.add(i+1, window);
607 }
608
609 private void placeWindowBefore(Object pos, WindowState window) {
610 final int i = mWindows.indexOf(pos);
611 if (localLOGV || DEBUG_FOCUS) Log.v(
612 TAG, "Adding window " + window + " at "
613 + i + " of " + mWindows.size() + " (before " + pos + ")");
614 mWindows.add(i, window);
615 }
616
617 //This method finds out the index of a window that has the same app token as
618 //win. used for z ordering the windows in mWindows
619 private int findIdxBasedOnAppTokens(WindowState win) {
620 //use a local variable to cache mWindows
621 ArrayList localmWindows = mWindows;
622 int jmax = localmWindows.size();
623 if(jmax == 0) {
624 return -1;
625 }
626 for(int j = (jmax-1); j >= 0; j--) {
627 WindowState wentry = (WindowState)localmWindows.get(j);
628 if(wentry.mAppToken == win.mAppToken) {
629 return j;
630 }
631 }
632 return -1;
633 }
Romain Guy06882f82009-06-10 13:36:04 -0700634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
636 final IWindow client = win.mClient;
637 final WindowToken token = win.mToken;
638 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 final int N = localmWindows.size();
641 final WindowState attached = win.mAttachedWindow;
642 int i;
643 if (attached == null) {
644 int tokenWindowsPos = token.windows.size();
645 if (token.appWindowToken != null) {
646 int index = tokenWindowsPos-1;
647 if (index >= 0) {
648 // If this application has existing windows, we
649 // simply place the new window on top of them... but
650 // keep the starting window on top.
651 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
652 // Base windows go behind everything else.
653 placeWindowBefore(token.windows.get(0), win);
654 tokenWindowsPos = 0;
655 } else {
656 AppWindowToken atoken = win.mAppToken;
657 if (atoken != null &&
658 token.windows.get(index) == atoken.startingWindow) {
659 placeWindowBefore(token.windows.get(index), win);
660 tokenWindowsPos--;
661 } else {
662 int newIdx = findIdxBasedOnAppTokens(win);
663 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700664 //there is a window above this one associated with the same
665 //apptoken note that the window could be a floating window
666 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 //windows associated with this token.
668 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
671 }
672 } else {
673 if (localLOGV) Log.v(
674 TAG, "Figuring out where to add app window "
675 + client.asBinder() + " (token=" + token + ")");
676 // Figure out where the window should go, based on the
677 // order of applications.
678 final int NA = mAppTokens.size();
679 Object pos = null;
680 for (i=NA-1; i>=0; i--) {
681 AppWindowToken t = mAppTokens.get(i);
682 if (t == token) {
683 i--;
684 break;
685 }
686 if (t.windows.size() > 0) {
687 pos = t.windows.get(0);
688 }
689 }
690 // We now know the index into the apps. If we found
691 // an app window above, that gives us the position; else
692 // we need to look some more.
693 if (pos != null) {
694 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700695 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 mTokenMap.get(((WindowState)pos).mClient.asBinder());
697 if (atoken != null) {
698 final int NC = atoken.windows.size();
699 if (NC > 0) {
700 WindowState bottom = atoken.windows.get(0);
701 if (bottom.mSubLayer < 0) {
702 pos = bottom;
703 }
704 }
705 }
706 placeWindowBefore(pos, win);
707 } else {
708 while (i >= 0) {
709 AppWindowToken t = mAppTokens.get(i);
710 final int NW = t.windows.size();
711 if (NW > 0) {
712 pos = t.windows.get(NW-1);
713 break;
714 }
715 i--;
716 }
717 if (pos != null) {
718 // Move in front of any windows attached to this
719 // one.
720 WindowToken atoken =
721 mTokenMap.get(((WindowState)pos).mClient.asBinder());
722 if (atoken != null) {
723 final int NC = atoken.windows.size();
724 if (NC > 0) {
725 WindowState top = atoken.windows.get(NC-1);
726 if (top.mSubLayer >= 0) {
727 pos = top;
728 }
729 }
730 }
731 placeWindowAfter(pos, win);
732 } else {
733 // Just search for the start of this layer.
734 final int myLayer = win.mBaseLayer;
735 for (i=0; i<N; i++) {
736 WindowState w = (WindowState)localmWindows.get(i);
737 if (w.mBaseLayer > myLayer) {
738 break;
739 }
740 }
741 if (localLOGV || DEBUG_FOCUS) Log.v(
742 TAG, "Adding window " + win + " at "
743 + i + " of " + N);
744 localmWindows.add(i, win);
745 }
746 }
747 }
748 } else {
749 // Figure out where window should go, based on layer.
750 final int myLayer = win.mBaseLayer;
751 for (i=N-1; i>=0; i--) {
752 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
753 i++;
754 break;
755 }
756 }
757 if (i < 0) i = 0;
758 if (localLOGV || DEBUG_FOCUS) Log.v(
759 TAG, "Adding window " + win + " at "
760 + i + " of " + N);
761 localmWindows.add(i, win);
762 }
763 if (addToToken) {
764 token.windows.add(tokenWindowsPos, win);
765 }
766
767 } else {
768 // Figure out this window's ordering relative to the window
769 // it is attached to.
770 final int NA = token.windows.size();
771 final int sublayer = win.mSubLayer;
772 int largestSublayer = Integer.MIN_VALUE;
773 WindowState windowWithLargestSublayer = null;
774 for (i=0; i<NA; i++) {
775 WindowState w = token.windows.get(i);
776 final int wSublayer = w.mSubLayer;
777 if (wSublayer >= largestSublayer) {
778 largestSublayer = wSublayer;
779 windowWithLargestSublayer = w;
780 }
781 if (sublayer < 0) {
782 // For negative sublayers, we go below all windows
783 // in the same sublayer.
784 if (wSublayer >= sublayer) {
785 if (addToToken) {
786 token.windows.add(i, win);
787 }
788 placeWindowBefore(
789 wSublayer >= 0 ? attached : w, win);
790 break;
791 }
792 } else {
793 // For positive sublayers, we go above all windows
794 // in the same sublayer.
795 if (wSublayer > sublayer) {
796 if (addToToken) {
797 token.windows.add(i, win);
798 }
799 placeWindowBefore(w, win);
800 break;
801 }
802 }
803 }
804 if (i >= NA) {
805 if (addToToken) {
806 token.windows.add(win);
807 }
808 if (sublayer < 0) {
809 placeWindowBefore(attached, win);
810 } else {
811 placeWindowAfter(largestSublayer >= 0
812 ? windowWithLargestSublayer
813 : attached,
814 win);
815 }
816 }
817 }
Romain Guy06882f82009-06-10 13:36:04 -0700818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 if (win.mAppToken != null && addToToken) {
820 win.mAppToken.allAppWindows.add(win);
821 }
822 }
Romain Guy06882f82009-06-10 13:36:04 -0700823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 static boolean canBeImeTarget(WindowState w) {
825 final int fl = w.mAttrs.flags
826 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
827 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
828 return w.isVisibleOrAdding();
829 }
830 return false;
831 }
Romain Guy06882f82009-06-10 13:36:04 -0700832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
834 final ArrayList localmWindows = mWindows;
835 final int N = localmWindows.size();
836 WindowState w = null;
837 int i = N;
838 while (i > 0) {
839 i--;
840 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
843 // + Integer.toHexString(w.mAttrs.flags));
844 if (canBeImeTarget(w)) {
845 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 // Yet more tricksyness! If this window is a "starting"
848 // window, we do actually want to be on top of it, but
849 // it is not -really- where input will go. So if the caller
850 // is not actually looking to move the IME, look down below
851 // for a real window to target...
852 if (!willMove
853 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
854 && i > 0) {
855 WindowState wb = (WindowState)localmWindows.get(i-1);
856 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
857 i--;
858 w = wb;
859 }
860 }
861 break;
862 }
863 }
Romain Guy06882f82009-06-10 13:36:04 -0700864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
868 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 if (willMove && w != null) {
871 final WindowState curTarget = mInputMethodTarget;
872 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 // Now some fun for dealing with window animations that
875 // modify the Z order. We need to look at all windows below
876 // the current target that are in this app, finding the highest
877 // visible one in layering.
878 AppWindowToken token = curTarget.mAppToken;
879 WindowState highestTarget = null;
880 int highestPos = 0;
881 if (token.animating || token.animation != null) {
882 int pos = 0;
883 pos = localmWindows.indexOf(curTarget);
884 while (pos >= 0) {
885 WindowState win = (WindowState)localmWindows.get(pos);
886 if (win.mAppToken != token) {
887 break;
888 }
889 if (!win.mRemoved) {
890 if (highestTarget == null || win.mAnimLayer >
891 highestTarget.mAnimLayer) {
892 highestTarget = win;
893 highestPos = pos;
894 }
895 }
896 pos--;
897 }
898 }
Romain Guy06882f82009-06-10 13:36:04 -0700899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700901 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 + mNextAppTransition + " " + highestTarget
903 + " animating=" + highestTarget.isAnimating()
904 + " layer=" + highestTarget.mAnimLayer
905 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
908 // If we are currently setting up for an animation,
909 // hold everything until we can find out what will happen.
910 mInputMethodTargetWaitingAnim = true;
911 mInputMethodTarget = highestTarget;
912 return highestPos + 1;
913 } else if (highestTarget.isAnimating() &&
914 highestTarget.mAnimLayer > w.mAnimLayer) {
915 // If the window we are currently targeting is involved
916 // with an animation, and it is on top of the next target
917 // we will be over, then hold off on moving until
918 // that is done.
919 mInputMethodTarget = highestTarget;
920 return highestPos + 1;
921 }
922 }
923 }
924 }
Romain Guy06882f82009-06-10 13:36:04 -0700925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 //Log.i(TAG, "Placing input method @" + (i+1));
927 if (w != null) {
928 if (willMove) {
929 RuntimeException e = new RuntimeException();
930 e.fillInStackTrace();
931 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
932 + mInputMethodTarget + " to " + w, e);
933 mInputMethodTarget = w;
934 if (w.mAppToken != null) {
935 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
936 } else {
937 setInputMethodAnimLayerAdjustment(0);
938 }
939 }
940 return i+1;
941 }
942 if (willMove) {
943 RuntimeException e = new RuntimeException();
944 e.fillInStackTrace();
945 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
946 + mInputMethodTarget + " to null", e);
947 mInputMethodTarget = null;
948 setInputMethodAnimLayerAdjustment(0);
949 }
950 return -1;
951 }
Romain Guy06882f82009-06-10 13:36:04 -0700952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 void addInputMethodWindowToListLocked(WindowState win) {
954 int pos = findDesiredInputMethodWindowIndexLocked(true);
955 if (pos >= 0) {
956 win.mTargetAppToken = mInputMethodTarget.mAppToken;
957 mWindows.add(pos, win);
958 moveInputMethodDialogsLocked(pos+1);
959 return;
960 }
961 win.mTargetAppToken = null;
962 addWindowToListInOrderLocked(win, true);
963 moveInputMethodDialogsLocked(pos);
964 }
Romain Guy06882f82009-06-10 13:36:04 -0700965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 void setInputMethodAnimLayerAdjustment(int adj) {
967 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
968 mInputMethodAnimLayerAdjustment = adj;
969 WindowState imw = mInputMethodWindow;
970 if (imw != null) {
971 imw.mAnimLayer = imw.mLayer + adj;
972 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
973 + " anim layer: " + imw.mAnimLayer);
974 int wi = imw.mChildWindows.size();
975 while (wi > 0) {
976 wi--;
977 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
978 cw.mAnimLayer = cw.mLayer + adj;
979 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
980 + " anim layer: " + cw.mAnimLayer);
981 }
982 }
983 int di = mInputMethodDialogs.size();
984 while (di > 0) {
985 di --;
986 imw = mInputMethodDialogs.get(di);
987 imw.mAnimLayer = imw.mLayer + adj;
988 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
989 + " anim layer: " + imw.mAnimLayer);
990 }
991 }
Romain Guy06882f82009-06-10 13:36:04 -0700992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
994 int wpos = mWindows.indexOf(win);
995 if (wpos >= 0) {
996 if (wpos < interestingPos) interestingPos--;
997 mWindows.remove(wpos);
998 int NC = win.mChildWindows.size();
999 while (NC > 0) {
1000 NC--;
1001 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1002 int cpos = mWindows.indexOf(cw);
1003 if (cpos >= 0) {
1004 if (cpos < interestingPos) interestingPos--;
1005 mWindows.remove(cpos);
1006 }
1007 }
1008 }
1009 return interestingPos;
1010 }
Romain Guy06882f82009-06-10 13:36:04 -07001011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 private void reAddWindowToListInOrderLocked(WindowState win) {
1013 addWindowToListInOrderLocked(win, false);
1014 // This is a hack to get all of the child windows added as well
1015 // at the right position. Child windows should be rare and
1016 // this case should be rare, so it shouldn't be that big a deal.
1017 int wpos = mWindows.indexOf(win);
1018 if (wpos >= 0) {
1019 mWindows.remove(wpos);
1020 reAddWindowLocked(wpos, win);
1021 }
1022 }
Romain Guy06882f82009-06-10 13:36:04 -07001023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 void logWindowList(String prefix) {
1025 int N = mWindows.size();
1026 while (N > 0) {
1027 N--;
1028 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1029 }
1030 }
Romain Guy06882f82009-06-10 13:36:04 -07001031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 void moveInputMethodDialogsLocked(int pos) {
1033 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 final int N = dialogs.size();
1036 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1037 for (int i=0; i<N; i++) {
1038 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1039 }
1040 if (DEBUG_INPUT_METHOD) {
1041 Log.v(TAG, "Window list w/pos=" + pos);
1042 logWindowList(" ");
1043 }
Romain Guy06882f82009-06-10 13:36:04 -07001044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (pos >= 0) {
1046 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1047 if (pos < mWindows.size()) {
1048 WindowState wp = (WindowState)mWindows.get(pos);
1049 if (wp == mInputMethodWindow) {
1050 pos++;
1051 }
1052 }
1053 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1054 for (int i=0; i<N; i++) {
1055 WindowState win = dialogs.get(i);
1056 win.mTargetAppToken = targetAppToken;
1057 pos = reAddWindowLocked(pos, win);
1058 }
1059 if (DEBUG_INPUT_METHOD) {
1060 Log.v(TAG, "Final window list:");
1061 logWindowList(" ");
1062 }
1063 return;
1064 }
1065 for (int i=0; i<N; i++) {
1066 WindowState win = dialogs.get(i);
1067 win.mTargetAppToken = null;
1068 reAddWindowToListInOrderLocked(win);
1069 if (DEBUG_INPUT_METHOD) {
1070 Log.v(TAG, "No IM target, final list:");
1071 logWindowList(" ");
1072 }
1073 }
1074 }
Romain Guy06882f82009-06-10 13:36:04 -07001075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1077 final WindowState imWin = mInputMethodWindow;
1078 final int DN = mInputMethodDialogs.size();
1079 if (imWin == null && DN == 0) {
1080 return false;
1081 }
Romain Guy06882f82009-06-10 13:36:04 -07001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1084 if (imPos >= 0) {
1085 // In this case, the input method windows are to be placed
1086 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 // First check to see if the input method windows are already
1089 // located here, and contiguous.
1090 final int N = mWindows.size();
1091 WindowState firstImWin = imPos < N
1092 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 // Figure out the actual input method window that should be
1095 // at the bottom of their stack.
1096 WindowState baseImWin = imWin != null
1097 ? imWin : mInputMethodDialogs.get(0);
1098 if (baseImWin.mChildWindows.size() > 0) {
1099 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1100 if (cw.mSubLayer < 0) baseImWin = cw;
1101 }
Romain Guy06882f82009-06-10 13:36:04 -07001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 if (firstImWin == baseImWin) {
1104 // The windows haven't moved... but are they still contiguous?
1105 // First find the top IM window.
1106 int pos = imPos+1;
1107 while (pos < N) {
1108 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1109 break;
1110 }
1111 pos++;
1112 }
1113 pos++;
1114 // Now there should be no more input method windows above.
1115 while (pos < N) {
1116 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1117 break;
1118 }
1119 pos++;
1120 }
1121 if (pos >= N) {
1122 // All is good!
1123 return false;
1124 }
1125 }
Romain Guy06882f82009-06-10 13:36:04 -07001126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 if (imWin != null) {
1128 if (DEBUG_INPUT_METHOD) {
1129 Log.v(TAG, "Moving IM from " + imPos);
1130 logWindowList(" ");
1131 }
1132 imPos = tmpRemoveWindowLocked(imPos, imWin);
1133 if (DEBUG_INPUT_METHOD) {
1134 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1135 logWindowList(" ");
1136 }
1137 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1138 reAddWindowLocked(imPos, imWin);
1139 if (DEBUG_INPUT_METHOD) {
1140 Log.v(TAG, "List after moving IM to " + imPos + ":");
1141 logWindowList(" ");
1142 }
1143 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1144 } else {
1145 moveInputMethodDialogsLocked(imPos);
1146 }
Romain Guy06882f82009-06-10 13:36:04 -07001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 } else {
1149 // In this case, the input method windows go in a fixed layer,
1150 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 if (imWin != null) {
1153 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1154 tmpRemoveWindowLocked(0, imWin);
1155 imWin.mTargetAppToken = null;
1156 reAddWindowToListInOrderLocked(imWin);
1157 if (DEBUG_INPUT_METHOD) {
1158 Log.v(TAG, "List with no IM target:");
1159 logWindowList(" ");
1160 }
1161 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1162 } else {
1163 moveInputMethodDialogsLocked(-1);;
1164 }
Romain Guy06882f82009-06-10 13:36:04 -07001165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
Romain Guy06882f82009-06-10 13:36:04 -07001167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 if (needAssignLayers) {
1169 assignLayersLocked();
1170 }
Romain Guy06882f82009-06-10 13:36:04 -07001171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 return true;
1173 }
Romain Guy06882f82009-06-10 13:36:04 -07001174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 void adjustInputMethodDialogsLocked() {
1176 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1177 }
Romain Guy06882f82009-06-10 13:36:04 -07001178
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001179 boolean adjustWallpaperWindowsLocked() {
1180 boolean changed = false;
1181
1182 // First find top-most window that has asked to be on top of the
1183 // wallpaper; all wallpapers go behind it.
1184 final ArrayList localmWindows = mWindows;
1185 int N = localmWindows.size();
1186 WindowState w = null;
1187 int i = N;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001188 boolean visible = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001189 while (i > 0) {
1190 i--;
1191 w = (WindowState)localmWindows.get(i);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001192 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()) {
1193 visible = true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001194 break;
1195 }
1196 }
1197
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001198 if (!visible) w = null;
1199 mWallpaperTarget = w;
1200
1201 if (visible) {
1202 mWallpaperAnimLayerAdjustment = w.mAppToken != null
1203 ? w.mAppToken.animLayerAdjustment : 0;
1204
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001205 // Now w is the window we are supposed to be behind... but we
1206 // need to be sure to also be behind any of its attached windows,
1207 // AND any starting window associated with it.
1208 while (i > 0) {
1209 WindowState wb = (WindowState)localmWindows.get(i-1);
1210 if (wb.mAttachedWindow != w &&
1211 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
1212 wb.mToken != w.mToken)) {
1213 // This window is not related to the previous one in any
1214 // interesting way, so stop here.
1215 break;
1216 }
1217 w = wb;
1218 i--;
1219 }
1220 }
1221
1222 // Okay i is the position immediately above the wallpaper. Look at
1223 // what is below it for later.
1224 w = i > 0 ? (WindowState)localmWindows.get(i-1) : null;
1225
1226 // Start stepping backwards from here, ensuring that our wallpaper windows
1227 // are correctly placed.
1228 int curTokenIndex = mWallpaperTokens.size();
1229 while (curTokenIndex > 0) {
1230 curTokenIndex--;
1231 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1232 int curWallpaperIndex = token.windows.size();
1233 while (curWallpaperIndex > 0) {
1234 curWallpaperIndex--;
1235 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001236
1237 // First, make sure the client has the current visibility
1238 // state.
1239 if (wallpaper.mWallpaperVisible != visible) {
1240 wallpaper.mWallpaperVisible = visible;
1241 try {
1242 if (DEBUG_VISIBILITY) Log.v(TAG,
1243 "Setting visibility of wallpaper " + wallpaper
1244 + ": " + visible);
1245 wallpaper.mClient.dispatchAppVisibility(visible);
1246 } catch (RemoteException e) {
1247 }
1248 }
1249
1250 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
1251 if (DEBUG_LAYERS) Log.v(TAG, "Wallpaper win " + wallpaper
1252 + " anim layer: " + wallpaper.mAnimLayer);
1253
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001254 // First, if this window is at the current index, then all
1255 // is well.
1256 if (wallpaper == w) {
1257 i--;
1258 w = i > 0 ? (WindowState)localmWindows.get(i-1) : null;
1259 continue;
1260 }
1261
1262 // The window didn't match... the current wallpaper window,
1263 // wherever it is, is in the wrong place, so make sure it is
1264 // not in the list.
1265 int oldIndex = localmWindows.indexOf(wallpaper);
1266 if (oldIndex >= 0) {
1267 localmWindows.remove(oldIndex);
1268 if (oldIndex < i) {
1269 i--;
1270 }
1271 }
1272
1273 // Now stick it in.
1274 localmWindows.add(i, wallpaper);
1275 changed = true;
1276 }
1277 }
1278
1279 return changed;
1280 }
1281
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001282 void setWallpaperAnimLayerAdjustment(int adj) {
1283 if (DEBUG_LAYERS) Log.v(TAG, "Setting wallpaper layer adj to " + adj);
1284 mWallpaperAnimLayerAdjustment = adj;
1285 int curTokenIndex = mWallpaperTokens.size();
1286 while (curTokenIndex > 0) {
1287 curTokenIndex--;
1288 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1289 int curWallpaperIndex = token.windows.size();
1290 while (curWallpaperIndex > 0) {
1291 curWallpaperIndex--;
1292 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1293 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
1294 if (DEBUG_LAYERS) Log.v(TAG, "Wallpaper win " + wallpaper
1295 + " anim layer: " + wallpaper.mAnimLayer);
1296 }
1297 }
1298 }
1299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 public int addWindow(Session session, IWindow client,
1301 WindowManager.LayoutParams attrs, int viewVisibility,
1302 Rect outContentInsets) {
1303 int res = mPolicy.checkAddPermission(attrs);
1304 if (res != WindowManagerImpl.ADD_OKAY) {
1305 return res;
1306 }
Romain Guy06882f82009-06-10 13:36:04 -07001307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 boolean reportNewConfig = false;
1309 WindowState attachedWindow = null;
1310 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 synchronized(mWindowMap) {
1313 // Instantiating a Display requires talking with the simulator,
1314 // so don't do it until we know the system is mostly up and
1315 // running.
1316 if (mDisplay == null) {
1317 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1318 mDisplay = wm.getDefaultDisplay();
1319 mQueue.setDisplay(mDisplay);
1320 reportNewConfig = true;
1321 }
Romain Guy06882f82009-06-10 13:36:04 -07001322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 if (mWindowMap.containsKey(client.asBinder())) {
1324 Log.w(TAG, "Window " + client + " is already added");
1325 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1326 }
1327
1328 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001329 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 if (attachedWindow == null) {
1331 Log.w(TAG, "Attempted to add window with token that is not a window: "
1332 + attrs.token + ". Aborting.");
1333 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1334 }
1335 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1336 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1337 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1338 + attrs.token + ". Aborting.");
1339 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1340 }
1341 }
1342
1343 boolean addToken = false;
1344 WindowToken token = mTokenMap.get(attrs.token);
1345 if (token == null) {
1346 if (attrs.type >= FIRST_APPLICATION_WINDOW
1347 && attrs.type <= LAST_APPLICATION_WINDOW) {
1348 Log.w(TAG, "Attempted to add application window with unknown token "
1349 + attrs.token + ". Aborting.");
1350 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1351 }
1352 if (attrs.type == TYPE_INPUT_METHOD) {
1353 Log.w(TAG, "Attempted to add input method window with unknown token "
1354 + attrs.token + ". Aborting.");
1355 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1356 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001357 if (attrs.type == TYPE_WALLPAPER) {
1358 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1359 + attrs.token + ". Aborting.");
1360 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 token = new WindowToken(attrs.token, -1, false);
1363 addToken = true;
1364 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1365 && attrs.type <= LAST_APPLICATION_WINDOW) {
1366 AppWindowToken atoken = token.appWindowToken;
1367 if (atoken == null) {
1368 Log.w(TAG, "Attempted to add window with non-application token "
1369 + token + ". Aborting.");
1370 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1371 } else if (atoken.removed) {
1372 Log.w(TAG, "Attempted to add window with exiting application token "
1373 + token + ". Aborting.");
1374 return WindowManagerImpl.ADD_APP_EXITING;
1375 }
1376 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1377 // No need for this guy!
1378 if (localLOGV) Log.v(
1379 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1380 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1381 }
1382 } else if (attrs.type == TYPE_INPUT_METHOD) {
1383 if (token.windowType != TYPE_INPUT_METHOD) {
1384 Log.w(TAG, "Attempted to add input method window with bad token "
1385 + attrs.token + ". Aborting.");
1386 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1387 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001388 } else if (attrs.type == TYPE_WALLPAPER) {
1389 if (token.windowType != TYPE_WALLPAPER) {
1390 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1391 + attrs.token + ". Aborting.");
1392 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 }
1395
1396 win = new WindowState(session, client, token,
1397 attachedWindow, attrs, viewVisibility);
1398 if (win.mDeathRecipient == null) {
1399 // Client has apparently died, so there is no reason to
1400 // continue.
1401 Log.w(TAG, "Adding window client " + client.asBinder()
1402 + " that is dead, aborting.");
1403 return WindowManagerImpl.ADD_APP_EXITING;
1404 }
1405
1406 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 res = mPolicy.prepareAddWindowLw(win, attrs);
1409 if (res != WindowManagerImpl.ADD_OKAY) {
1410 return res;
1411 }
1412
1413 // From now on, no exceptions or errors allowed!
1414
1415 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 if (addToken) {
1420 mTokenMap.put(attrs.token, token);
1421 mTokenList.add(token);
1422 }
1423 win.attach();
1424 mWindowMap.put(client.asBinder(), win);
1425
1426 if (attrs.type == TYPE_APPLICATION_STARTING &&
1427 token.appWindowToken != null) {
1428 token.appWindowToken.startingWindow = win;
1429 }
1430
1431 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 if (attrs.type == TYPE_INPUT_METHOD) {
1434 mInputMethodWindow = win;
1435 addInputMethodWindowToListLocked(win);
1436 imMayMove = false;
1437 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1438 mInputMethodDialogs.add(win);
1439 addWindowToListInOrderLocked(win, true);
1440 adjustInputMethodDialogsLocked();
1441 imMayMove = false;
1442 } else {
1443 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001444 if (attrs.type == TYPE_WALLPAPER ||
1445 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1446 adjustWallpaperWindowsLocked();
1447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 }
Romain Guy06882f82009-06-10 13:36:04 -07001449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 if (mInTouchMode) {
1455 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1456 }
1457 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1458 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1459 }
Romain Guy06882f82009-06-10 13:36:04 -07001460
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001461 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001463 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1464 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 imMayMove = false;
1466 }
1467 }
Romain Guy06882f82009-06-10 13:36:04 -07001468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001470 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 }
Romain Guy06882f82009-06-10 13:36:04 -07001472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 assignLayersLocked();
1474 // Don't do layout here, the window must call
1475 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 //dump();
1478
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001479 if (focusChanged) {
1480 if (mCurrentFocus != null) {
1481 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1482 }
1483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 if (localLOGV) Log.v(
1485 TAG, "New client " + client.asBinder()
1486 + ": window=" + win);
1487 }
1488
1489 // sendNewConfiguration() checks caller permissions so we must call it with
1490 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1491 // identity anyway, so it's safe to just clear & restore around this whole
1492 // block.
1493 final long origId = Binder.clearCallingIdentity();
1494 if (reportNewConfig) {
1495 sendNewConfiguration();
1496 } else {
1497 // Update Orientation after adding a window, only if the window needs to be
1498 // displayed right away
1499 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001500 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 sendNewConfiguration();
1502 }
1503 }
1504 }
1505 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 return res;
1508 }
Romain Guy06882f82009-06-10 13:36:04 -07001509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 public void removeWindow(Session session, IWindow client) {
1511 synchronized(mWindowMap) {
1512 WindowState win = windowForClientLocked(session, client);
1513 if (win == null) {
1514 return;
1515 }
1516 removeWindowLocked(session, win);
1517 }
1518 }
Romain Guy06882f82009-06-10 13:36:04 -07001519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 public void removeWindowLocked(Session session, WindowState win) {
1521
1522 if (localLOGV || DEBUG_FOCUS) Log.v(
1523 TAG, "Remove " + win + " client="
1524 + Integer.toHexString(System.identityHashCode(
1525 win.mClient.asBinder()))
1526 + ", surface=" + win.mSurface);
1527
1528 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 if (DEBUG_APP_TRANSITIONS) Log.v(
1531 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1532 + " mExiting=" + win.mExiting
1533 + " isAnimating=" + win.isAnimating()
1534 + " app-animation="
1535 + (win.mAppToken != null ? win.mAppToken.animation : null)
1536 + " inPendingTransaction="
1537 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1538 + " mDisplayFrozen=" + mDisplayFrozen);
1539 // Visibility of the removed window. Will be used later to update orientation later on.
1540 boolean wasVisible = false;
1541 // First, see if we need to run an animation. If we do, we have
1542 // to hold off on removing the window until the animation is done.
1543 // If the display is frozen, just remove immediately, since the
1544 // animation wouldn't be seen.
1545 if (win.mSurface != null && !mDisplayFrozen) {
1546 // If we are not currently running the exit animation, we
1547 // need to see about starting one.
1548 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1551 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1552 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1553 }
1554 // Try starting an animation.
1555 if (applyAnimationLocked(win, transit, false)) {
1556 win.mExiting = true;
1557 }
1558 }
1559 if (win.mExiting || win.isAnimating()) {
1560 // The exit animation is running... wait for it!
1561 //Log.i(TAG, "*** Running exit animation...");
1562 win.mExiting = true;
1563 win.mRemoveOnExit = true;
1564 mLayoutNeeded = true;
1565 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1566 performLayoutAndPlaceSurfacesLocked();
1567 if (win.mAppToken != null) {
1568 win.mAppToken.updateReportedVisibilityLocked();
1569 }
1570 //dump();
1571 Binder.restoreCallingIdentity(origId);
1572 return;
1573 }
1574 }
1575
1576 removeWindowInnerLocked(session, win);
1577 // Removing a visible window will effect the computed orientation
1578 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001579 if (wasVisible && computeForcedAppOrientationLocked()
1580 != mForcedAppOrientation) {
1581 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
1583 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1584 Binder.restoreCallingIdentity(origId);
1585 }
Romain Guy06882f82009-06-10 13:36:04 -07001586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 private void removeWindowInnerLocked(Session session, WindowState win) {
1588 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1589 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 if (mInputMethodTarget == win) {
1594 moveInputMethodWindowsIfNeededLocked(false);
1595 }
Romain Guy06882f82009-06-10 13:36:04 -07001596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 mPolicy.removeWindowLw(win);
1598 win.removeLocked();
1599
1600 mWindowMap.remove(win.mClient.asBinder());
1601 mWindows.remove(win);
1602
1603 if (mInputMethodWindow == win) {
1604 mInputMethodWindow = null;
1605 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1606 mInputMethodDialogs.remove(win);
1607 }
Romain Guy06882f82009-06-10 13:36:04 -07001608
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001609 if (win.mAttrs.type == TYPE_WALLPAPER ||
1610 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1611 adjustWallpaperWindowsLocked();
1612 }
1613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 final WindowToken token = win.mToken;
1615 final AppWindowToken atoken = win.mAppToken;
1616 token.windows.remove(win);
1617 if (atoken != null) {
1618 atoken.allAppWindows.remove(win);
1619 }
1620 if (localLOGV) Log.v(
1621 TAG, "**** Removing window " + win + ": count="
1622 + token.windows.size());
1623 if (token.windows.size() == 0) {
1624 if (!token.explicit) {
1625 mTokenMap.remove(token.token);
1626 mTokenList.remove(token);
1627 } else if (atoken != null) {
1628 atoken.firstWindowDrawn = false;
1629 }
1630 }
1631
1632 if (atoken != null) {
1633 if (atoken.startingWindow == win) {
1634 atoken.startingWindow = null;
1635 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1636 // If this is the last window and we had requested a starting
1637 // transition window, well there is no point now.
1638 atoken.startingData = null;
1639 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1640 // If this is the last window except for a starting transition
1641 // window, we need to get rid of the starting transition.
1642 if (DEBUG_STARTING_WINDOW) {
1643 Log.v(TAG, "Schedule remove starting " + token
1644 + ": no more real windows");
1645 }
1646 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1647 mH.sendMessage(m);
1648 }
1649 }
Romain Guy06882f82009-06-10 13:36:04 -07001650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 if (!mInLayout) {
1652 assignLayersLocked();
1653 mLayoutNeeded = true;
1654 performLayoutAndPlaceSurfacesLocked();
1655 if (win.mAppToken != null) {
1656 win.mAppToken.updateReportedVisibilityLocked();
1657 }
1658 }
1659 }
1660
1661 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1662 long origId = Binder.clearCallingIdentity();
1663 try {
1664 synchronized (mWindowMap) {
1665 WindowState w = windowForClientLocked(session, client);
1666 if ((w != null) && (w.mSurface != null)) {
1667 Surface.openTransaction();
1668 try {
1669 w.mSurface.setTransparentRegionHint(region);
1670 } finally {
1671 Surface.closeTransaction();
1672 }
1673 }
1674 }
1675 } finally {
1676 Binder.restoreCallingIdentity(origId);
1677 }
1678 }
1679
1680 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001681 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 Rect visibleInsets) {
1683 long origId = Binder.clearCallingIdentity();
1684 try {
1685 synchronized (mWindowMap) {
1686 WindowState w = windowForClientLocked(session, client);
1687 if (w != null) {
1688 w.mGivenInsetsPending = false;
1689 w.mGivenContentInsets.set(contentInsets);
1690 w.mGivenVisibleInsets.set(visibleInsets);
1691 w.mTouchableInsets = touchableInsets;
1692 mLayoutNeeded = true;
1693 performLayoutAndPlaceSurfacesLocked();
1694 }
1695 }
1696 } finally {
1697 Binder.restoreCallingIdentity(origId);
1698 }
1699 }
Romain Guy06882f82009-06-10 13:36:04 -07001700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 public void getWindowDisplayFrame(Session session, IWindow client,
1702 Rect outDisplayFrame) {
1703 synchronized(mWindowMap) {
1704 WindowState win = windowForClientLocked(session, client);
1705 if (win == null) {
1706 outDisplayFrame.setEmpty();
1707 return;
1708 }
1709 outDisplayFrame.set(win.mDisplayFrame);
1710 }
1711 }
1712
1713 public int relayoutWindow(Session session, IWindow client,
1714 WindowManager.LayoutParams attrs, int requestedWidth,
1715 int requestedHeight, int viewVisibility, boolean insetsPending,
1716 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1717 Surface outSurface) {
1718 boolean displayed = false;
1719 boolean inTouchMode;
1720 Configuration newConfig = null;
1721 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 synchronized(mWindowMap) {
1724 WindowState win = windowForClientLocked(session, client);
1725 if (win == null) {
1726 return 0;
1727 }
1728 win.mRequestedWidth = requestedWidth;
1729 win.mRequestedHeight = requestedHeight;
1730
1731 if (attrs != null) {
1732 mPolicy.adjustWindowParamsLw(attrs);
1733 }
Romain Guy06882f82009-06-10 13:36:04 -07001734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 int attrChanges = 0;
1736 int flagChanges = 0;
1737 if (attrs != null) {
1738 flagChanges = win.mAttrs.flags ^= attrs.flags;
1739 attrChanges = win.mAttrs.copyFrom(attrs);
1740 }
1741
1742 if (localLOGV) Log.v(
1743 TAG, "Relayout given client " + client.asBinder()
1744 + " (" + win.mAttrs.getTitle() + ")");
1745
1746
1747 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1748 win.mAlpha = attrs.alpha;
1749 }
1750
1751 final boolean scaledWindow =
1752 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1753
1754 if (scaledWindow) {
1755 // requested{Width|Height} Surface's physical size
1756 // attrs.{width|height} Size on screen
1757 win.mHScale = (attrs.width != requestedWidth) ?
1758 (attrs.width / (float)requestedWidth) : 1.0f;
1759 win.mVScale = (attrs.height != requestedHeight) ?
1760 (attrs.height / (float)requestedHeight) : 1.0f;
1761 }
1762
1763 boolean imMayMove = (flagChanges&(
1764 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1765 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 boolean focusMayChange = win.mViewVisibility != viewVisibility
1768 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1769 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001770
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001771 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
1772 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
1773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 win.mRelayoutCalled = true;
1775 final int oldVisibility = win.mViewVisibility;
1776 win.mViewVisibility = viewVisibility;
1777 if (viewVisibility == View.VISIBLE &&
1778 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1779 displayed = !win.isVisibleLw();
1780 if (win.mExiting) {
1781 win.mExiting = false;
1782 win.mAnimation = null;
1783 }
1784 if (win.mDestroying) {
1785 win.mDestroying = false;
1786 mDestroySurface.remove(win);
1787 }
1788 if (oldVisibility == View.GONE) {
1789 win.mEnterAnimationPending = true;
1790 }
1791 if (displayed && win.mSurface != null && !win.mDrawPending
1792 && !win.mCommitDrawPending && !mDisplayFrozen) {
1793 applyEnterAnimationLocked(win);
1794 }
1795 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1796 // To change the format, we need to re-build the surface.
1797 win.destroySurfaceLocked();
1798 displayed = true;
1799 }
1800 try {
1801 Surface surface = win.createSurfaceLocked();
1802 if (surface != null) {
1803 outSurface.copyFrom(surface);
1804 } else {
1805 outSurface.clear();
1806 }
1807 } catch (Exception e) {
1808 Log.w(TAG, "Exception thrown when creating surface for client "
1809 + client + " (" + win.mAttrs.getTitle() + ")",
1810 e);
1811 Binder.restoreCallingIdentity(origId);
1812 return 0;
1813 }
1814 if (displayed) {
1815 focusMayChange = true;
1816 }
1817 if (win.mAttrs.type == TYPE_INPUT_METHOD
1818 && mInputMethodWindow == null) {
1819 mInputMethodWindow = win;
1820 imMayMove = true;
1821 }
1822 } else {
1823 win.mEnterAnimationPending = false;
1824 if (win.mSurface != null) {
1825 // If we are not currently running the exit animation, we
1826 // need to see about starting one.
1827 if (!win.mExiting) {
1828 // Try starting an animation; if there isn't one, we
1829 // can destroy the surface right away.
1830 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1831 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1832 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1833 }
1834 if (win.isWinVisibleLw() &&
1835 applyAnimationLocked(win, transit, false)) {
1836 win.mExiting = true;
1837 mKeyWaiter.finishedKey(session, client, true,
1838 KeyWaiter.RETURN_NOTHING);
1839 } else if (win.isAnimating()) {
1840 // Currently in a hide animation... turn this into
1841 // an exit.
1842 win.mExiting = true;
1843 } else {
1844 if (mInputMethodWindow == win) {
1845 mInputMethodWindow = null;
1846 }
1847 win.destroySurfaceLocked();
1848 }
1849 }
1850 }
1851 outSurface.clear();
1852 }
1853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 if (focusMayChange) {
1855 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1856 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 imMayMove = false;
1858 }
1859 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1860 }
Romain Guy06882f82009-06-10 13:36:04 -07001861
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001862 // updateFocusedWindowLocked() already assigned layers so we only need to
1863 // reassign them at this point if the IM window state gets shuffled
1864 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 if (imMayMove) {
1867 if (moveInputMethodWindowsIfNeededLocked(false)) {
1868 assignLayers = true;
1869 }
1870 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001871 if (wallpaperMayMove) {
1872 if (adjustWallpaperWindowsLocked()) {
1873 assignLayers = true;
1874 }
1875 }
Romain Guy06882f82009-06-10 13:36:04 -07001876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 mLayoutNeeded = true;
1878 win.mGivenInsetsPending = insetsPending;
1879 if (assignLayers) {
1880 assignLayersLocked();
1881 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001882 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 performLayoutAndPlaceSurfacesLocked();
1884 if (win.mAppToken != null) {
1885 win.mAppToken.updateReportedVisibilityLocked();
1886 }
1887 outFrame.set(win.mFrame);
1888 outContentInsets.set(win.mContentInsets);
1889 outVisibleInsets.set(win.mVisibleInsets);
1890 if (localLOGV) Log.v(
1891 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001892 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 + ", requestedHeight=" + requestedHeight
1894 + ", viewVisibility=" + viewVisibility
1895 + "\nRelayout returning frame=" + outFrame
1896 + ", surface=" + outSurface);
1897
1898 if (localLOGV || DEBUG_FOCUS) Log.v(
1899 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1900
1901 inTouchMode = mInTouchMode;
1902 }
1903
1904 if (newConfig != null) {
1905 sendNewConfiguration();
1906 }
Romain Guy06882f82009-06-10 13:36:04 -07001907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1911 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1912 }
1913
1914 public void finishDrawingWindow(Session session, IWindow client) {
1915 final long origId = Binder.clearCallingIdentity();
1916 synchronized(mWindowMap) {
1917 WindowState win = windowForClientLocked(session, client);
1918 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001919 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1920 adjustWallpaperWindowsLocked();
1921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 mLayoutNeeded = true;
1923 performLayoutAndPlaceSurfacesLocked();
1924 }
1925 }
1926 Binder.restoreCallingIdentity(origId);
1927 }
1928
1929 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1930 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1931 + (lp != null ? lp.packageName : null)
1932 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1933 if (lp != null && lp.windowAnimations != 0) {
1934 // If this is a system resource, don't try to load it from the
1935 // application resources. It is nice to avoid loading application
1936 // resources if we can.
1937 String packageName = lp.packageName != null ? lp.packageName : "android";
1938 int resId = lp.windowAnimations;
1939 if ((resId&0xFF000000) == 0x01000000) {
1940 packageName = "android";
1941 }
1942 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
1943 + packageName);
1944 return AttributeCache.instance().get(packageName, resId,
1945 com.android.internal.R.styleable.WindowAnimation);
1946 }
1947 return null;
1948 }
Romain Guy06882f82009-06-10 13:36:04 -07001949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 private void applyEnterAnimationLocked(WindowState win) {
1951 int transit = WindowManagerPolicy.TRANSIT_SHOW;
1952 if (win.mEnterAnimationPending) {
1953 win.mEnterAnimationPending = false;
1954 transit = WindowManagerPolicy.TRANSIT_ENTER;
1955 }
1956
1957 applyAnimationLocked(win, transit, true);
1958 }
1959
1960 private boolean applyAnimationLocked(WindowState win,
1961 int transit, boolean isEntrance) {
1962 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
1963 // If we are trying to apply an animation, but already running
1964 // an animation of the same type, then just leave that one alone.
1965 return true;
1966 }
Romain Guy06882f82009-06-10 13:36:04 -07001967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 // Only apply an animation if the display isn't frozen. If it is
1969 // frozen, there is no reason to animate and it can cause strange
1970 // artifacts when we unfreeze the display if some different animation
1971 // is running.
1972 if (!mDisplayFrozen) {
1973 int anim = mPolicy.selectAnimationLw(win, transit);
1974 int attr = -1;
1975 Animation a = null;
1976 if (anim != 0) {
1977 a = AnimationUtils.loadAnimation(mContext, anim);
1978 } else {
1979 switch (transit) {
1980 case WindowManagerPolicy.TRANSIT_ENTER:
1981 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1982 break;
1983 case WindowManagerPolicy.TRANSIT_EXIT:
1984 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1985 break;
1986 case WindowManagerPolicy.TRANSIT_SHOW:
1987 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1988 break;
1989 case WindowManagerPolicy.TRANSIT_HIDE:
1990 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1991 break;
1992 }
1993 if (attr >= 0) {
1994 a = loadAnimation(win.mAttrs, attr);
1995 }
1996 }
1997 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
1998 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1999 + " mAnimation=" + win.mAnimation
2000 + " isEntrance=" + isEntrance);
2001 if (a != null) {
2002 if (DEBUG_ANIM) {
2003 RuntimeException e = new RuntimeException();
2004 e.fillInStackTrace();
2005 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2006 }
2007 win.setAnimation(a);
2008 win.mAnimationIsEntrance = isEntrance;
2009 }
2010 } else {
2011 win.clearAnimation();
2012 }
2013
2014 return win.mAnimation != null;
2015 }
2016
2017 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2018 int anim = 0;
2019 Context context = mContext;
2020 if (animAttr >= 0) {
2021 AttributeCache.Entry ent = getCachedAnimations(lp);
2022 if (ent != null) {
2023 context = ent.context;
2024 anim = ent.array.getResourceId(animAttr, 0);
2025 }
2026 }
2027 if (anim != 0) {
2028 return AnimationUtils.loadAnimation(context, anim);
2029 }
2030 return null;
2031 }
Romain Guy06882f82009-06-10 13:36:04 -07002032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 private boolean applyAnimationLocked(AppWindowToken wtoken,
2034 WindowManager.LayoutParams lp, int transit, boolean enter) {
2035 // Only apply an animation if the display isn't frozen. If it is
2036 // frozen, there is no reason to animate and it can cause strange
2037 // artifacts when we unfreeze the display if some different animation
2038 // is running.
2039 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002040 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002041 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002042 a = new FadeInOutAnimation(enter);
2043 if (DEBUG_ANIM) Log.v(TAG,
2044 "applying FadeInOutAnimation for a window in compatibility mode");
2045 } else {
2046 int animAttr = 0;
2047 switch (transit) {
2048 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2049 animAttr = enter
2050 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2051 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2052 break;
2053 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2054 animAttr = enter
2055 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2056 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2057 break;
2058 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2059 animAttr = enter
2060 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2061 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2062 break;
2063 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2064 animAttr = enter
2065 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2066 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2067 break;
2068 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2069 animAttr = enter
2070 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2071 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2072 break;
2073 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2074 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002075 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002076 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2077 break;
2078 }
2079 a = loadAnimation(lp, animAttr);
2080 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2081 + " anim=" + a
2082 + " animAttr=0x" + Integer.toHexString(animAttr)
2083 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 if (a != null) {
2086 if (DEBUG_ANIM) {
2087 RuntimeException e = new RuntimeException();
2088 e.fillInStackTrace();
2089 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2090 }
2091 wtoken.setAnimation(a);
2092 }
2093 } else {
2094 wtoken.clearAnimation();
2095 }
2096
2097 return wtoken.animation != null;
2098 }
2099
2100 // -------------------------------------------------------------
2101 // Application Window Tokens
2102 // -------------------------------------------------------------
2103
2104 public void validateAppTokens(List tokens) {
2105 int v = tokens.size()-1;
2106 int m = mAppTokens.size()-1;
2107 while (v >= 0 && m >= 0) {
2108 AppWindowToken wtoken = mAppTokens.get(m);
2109 if (wtoken.removed) {
2110 m--;
2111 continue;
2112 }
2113 if (tokens.get(v) != wtoken.token) {
2114 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2115 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2116 }
2117 v--;
2118 m--;
2119 }
2120 while (v >= 0) {
2121 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2122 v--;
2123 }
2124 while (m >= 0) {
2125 AppWindowToken wtoken = mAppTokens.get(m);
2126 if (!wtoken.removed) {
2127 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2128 }
2129 m--;
2130 }
2131 }
2132
2133 boolean checkCallingPermission(String permission, String func) {
2134 // Quick check: if the calling permission is me, it's all okay.
2135 if (Binder.getCallingPid() == Process.myPid()) {
2136 return true;
2137 }
Romain Guy06882f82009-06-10 13:36:04 -07002138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 if (mContext.checkCallingPermission(permission)
2140 == PackageManager.PERMISSION_GRANTED) {
2141 return true;
2142 }
2143 String msg = "Permission Denial: " + func + " from pid="
2144 + Binder.getCallingPid()
2145 + ", uid=" + Binder.getCallingUid()
2146 + " requires " + permission;
2147 Log.w(TAG, msg);
2148 return false;
2149 }
Romain Guy06882f82009-06-10 13:36:04 -07002150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 AppWindowToken findAppWindowToken(IBinder token) {
2152 WindowToken wtoken = mTokenMap.get(token);
2153 if (wtoken == null) {
2154 return null;
2155 }
2156 return wtoken.appWindowToken;
2157 }
Romain Guy06882f82009-06-10 13:36:04 -07002158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 public void addWindowToken(IBinder token, int type) {
2160 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2161 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002162 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 }
Romain Guy06882f82009-06-10 13:36:04 -07002164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 synchronized(mWindowMap) {
2166 WindowToken wtoken = mTokenMap.get(token);
2167 if (wtoken != null) {
2168 Log.w(TAG, "Attempted to add existing input method token: " + token);
2169 return;
2170 }
2171 wtoken = new WindowToken(token, type, true);
2172 mTokenMap.put(token, wtoken);
2173 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002174 if (type == TYPE_WALLPAPER) {
2175 mWallpaperTokens.add(wtoken);
2176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
2178 }
Romain Guy06882f82009-06-10 13:36:04 -07002179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 public void removeWindowToken(IBinder token) {
2181 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2182 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002183 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 }
2185
2186 final long origId = Binder.clearCallingIdentity();
2187 synchronized(mWindowMap) {
2188 WindowToken wtoken = mTokenMap.remove(token);
2189 mTokenList.remove(wtoken);
2190 if (wtoken != null) {
2191 boolean delayed = false;
2192 if (!wtoken.hidden) {
2193 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 final int N = wtoken.windows.size();
2196 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 for (int i=0; i<N; i++) {
2199 WindowState win = wtoken.windows.get(i);
2200
2201 if (win.isAnimating()) {
2202 delayed = true;
2203 }
Romain Guy06882f82009-06-10 13:36:04 -07002204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 if (win.isVisibleNow()) {
2206 applyAnimationLocked(win,
2207 WindowManagerPolicy.TRANSIT_EXIT, false);
2208 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2209 KeyWaiter.RETURN_NOTHING);
2210 changed = true;
2211 }
2212 }
2213
2214 if (changed) {
2215 mLayoutNeeded = true;
2216 performLayoutAndPlaceSurfacesLocked();
2217 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2218 }
Romain Guy06882f82009-06-10 13:36:04 -07002219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 if (delayed) {
2221 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002222 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2223 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 }
2225 }
Romain Guy06882f82009-06-10 13:36:04 -07002226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 } else {
2228 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2229 }
2230 }
2231 Binder.restoreCallingIdentity(origId);
2232 }
2233
2234 public void addAppToken(int addPos, IApplicationToken token,
2235 int groupId, int requestedOrientation, boolean fullscreen) {
2236 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2237 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002238 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 }
Romain Guy06882f82009-06-10 13:36:04 -07002240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 synchronized(mWindowMap) {
2242 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2243 if (wtoken != null) {
2244 Log.w(TAG, "Attempted to add existing app token: " + token);
2245 return;
2246 }
2247 wtoken = new AppWindowToken(token);
2248 wtoken.groupId = groupId;
2249 wtoken.appFullscreen = fullscreen;
2250 wtoken.requestedOrientation = requestedOrientation;
2251 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002252 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 mTokenMap.put(token.asBinder(), wtoken);
2254 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 // Application tokens start out hidden.
2257 wtoken.hidden = true;
2258 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 //dump();
2261 }
2262 }
Romain Guy06882f82009-06-10 13:36:04 -07002263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 public void setAppGroupId(IBinder token, int groupId) {
2265 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2266 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002267 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 }
2269
2270 synchronized(mWindowMap) {
2271 AppWindowToken wtoken = findAppWindowToken(token);
2272 if (wtoken == null) {
2273 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2274 return;
2275 }
2276 wtoken.groupId = groupId;
2277 }
2278 }
Romain Guy06882f82009-06-10 13:36:04 -07002279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 public int getOrientationFromWindowsLocked() {
2281 int pos = mWindows.size() - 1;
2282 while (pos >= 0) {
2283 WindowState wtoken = (WindowState) mWindows.get(pos);
2284 pos--;
2285 if (wtoken.mAppToken != null) {
2286 // We hit an application window. so the orientation will be determined by the
2287 // app window. No point in continuing further.
2288 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2289 }
2290 if (!wtoken.isVisibleLw()) {
2291 continue;
2292 }
2293 int req = wtoken.mAttrs.screenOrientation;
2294 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2295 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2296 continue;
2297 } else {
2298 return req;
2299 }
2300 }
2301 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2302 }
Romain Guy06882f82009-06-10 13:36:04 -07002303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 public int getOrientationFromAppTokensLocked() {
2305 int pos = mAppTokens.size() - 1;
2306 int curGroup = 0;
2307 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002308 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002310 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 while (pos >= 0) {
2312 AppWindowToken wtoken = mAppTokens.get(pos);
2313 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002314 // if we're about to tear down this window and not seek for
2315 // the behind activity, don't use it for orientation
2316 if (!findingBehind
2317 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002318 continue;
2319 }
2320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 if (!haveGroup) {
2322 // We ignore any hidden applications on the top.
2323 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2324 continue;
2325 }
2326 haveGroup = true;
2327 curGroup = wtoken.groupId;
2328 lastOrientation = wtoken.requestedOrientation;
2329 } else if (curGroup != wtoken.groupId) {
2330 // If we have hit a new application group, and the bottom
2331 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002332 // the orientation behind it, and the last app was
2333 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002335 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2336 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 return lastOrientation;
2338 }
2339 }
2340 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002341 // If this application is fullscreen, and didn't explicitly say
2342 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002344 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002345 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002346 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 return or;
2348 }
2349 // If this application has requested an explicit orientation,
2350 // then use it.
2351 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2352 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2353 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2354 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2355 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2356 return or;
2357 }
Owen Lin3413b892009-05-01 17:12:32 -07002358 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
2360 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2361 }
Romain Guy06882f82009-06-10 13:36:04 -07002362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002364 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002365 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2366 "updateOrientationFromAppTokens()")) {
2367 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2368 }
2369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 Configuration config;
2371 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002372 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2373 freezeThisOneIfNeeded);
2374 Binder.restoreCallingIdentity(ident);
2375 return config;
2376 }
2377
2378 Configuration updateOrientationFromAppTokensUnchecked(
2379 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2380 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002382 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 }
2384 if (config != null) {
2385 mLayoutNeeded = true;
2386 performLayoutAndPlaceSurfacesLocked();
2387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 return config;
2389 }
Romain Guy06882f82009-06-10 13:36:04 -07002390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 /*
2392 * The orientation is computed from non-application windows first. If none of
2393 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002394 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2396 * android.os.IBinder)
2397 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002398 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002399 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 long ident = Binder.clearCallingIdentity();
2402 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002403 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 if (req != mForcedAppOrientation) {
2406 changed = true;
2407 mForcedAppOrientation = req;
2408 //send a message to Policy indicating orientation change to take
2409 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002410 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 }
Romain Guy06882f82009-06-10 13:36:04 -07002412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 if (changed) {
2414 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002415 WindowManagerPolicy.USE_LAST_ROTATION,
2416 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 if (changed) {
2418 if (freezeThisOneIfNeeded != null) {
2419 AppWindowToken wtoken = findAppWindowToken(
2420 freezeThisOneIfNeeded);
2421 if (wtoken != null) {
2422 startAppFreezingScreenLocked(wtoken,
2423 ActivityInfo.CONFIG_ORIENTATION);
2424 }
2425 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002426 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 }
2428 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002429
2430 // No obvious action we need to take, but if our current
2431 // state mismatches the activity maanager's, update it
2432 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002433 mTempConfiguration.setToDefaults();
2434 if (computeNewConfigurationLocked(mTempConfiguration)) {
2435 if (appConfig.diff(mTempConfiguration) != 0) {
2436 Log.i(TAG, "Config changed: " + mTempConfiguration);
2437 return new Configuration(mTempConfiguration);
2438 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002439 }
2440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 } finally {
2442 Binder.restoreCallingIdentity(ident);
2443 }
Romain Guy06882f82009-06-10 13:36:04 -07002444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 return null;
2446 }
Romain Guy06882f82009-06-10 13:36:04 -07002447
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002448 int computeForcedAppOrientationLocked() {
2449 int req = getOrientationFromWindowsLocked();
2450 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2451 req = getOrientationFromAppTokensLocked();
2452 }
2453 return req;
2454 }
Romain Guy06882f82009-06-10 13:36:04 -07002455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2457 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2458 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002459 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 }
Romain Guy06882f82009-06-10 13:36:04 -07002461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 synchronized(mWindowMap) {
2463 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2464 if (wtoken == null) {
2465 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2466 return;
2467 }
Romain Guy06882f82009-06-10 13:36:04 -07002468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 wtoken.requestedOrientation = requestedOrientation;
2470 }
2471 }
Romain Guy06882f82009-06-10 13:36:04 -07002472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 public int getAppOrientation(IApplicationToken token) {
2474 synchronized(mWindowMap) {
2475 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2476 if (wtoken == null) {
2477 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2478 }
Romain Guy06882f82009-06-10 13:36:04 -07002479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 return wtoken.requestedOrientation;
2481 }
2482 }
Romain Guy06882f82009-06-10 13:36:04 -07002483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2485 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2486 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002487 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 }
2489
2490 synchronized(mWindowMap) {
2491 boolean changed = false;
2492 if (token == null) {
2493 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2494 changed = mFocusedApp != null;
2495 mFocusedApp = null;
2496 mKeyWaiter.tickle();
2497 } else {
2498 AppWindowToken newFocus = findAppWindowToken(token);
2499 if (newFocus == null) {
2500 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2501 return;
2502 }
2503 changed = mFocusedApp != newFocus;
2504 mFocusedApp = newFocus;
2505 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2506 mKeyWaiter.tickle();
2507 }
2508
2509 if (moveFocusNow && changed) {
2510 final long origId = Binder.clearCallingIdentity();
2511 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2512 Binder.restoreCallingIdentity(origId);
2513 }
2514 }
2515 }
2516
2517 public void prepareAppTransition(int transit) {
2518 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2519 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002520 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 }
Romain Guy06882f82009-06-10 13:36:04 -07002522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 synchronized(mWindowMap) {
2524 if (DEBUG_APP_TRANSITIONS) Log.v(
2525 TAG, "Prepare app transition: transit=" + transit
2526 + " mNextAppTransition=" + mNextAppTransition);
2527 if (!mDisplayFrozen) {
2528 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2529 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002530 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2531 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2532 // Opening a new task always supersedes a close for the anim.
2533 mNextAppTransition = transit;
2534 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2535 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2536 // Opening a new activity always supersedes a close for the anim.
2537 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 }
2539 mAppTransitionReady = false;
2540 mAppTransitionTimeout = false;
2541 mStartingIconInTransition = false;
2542 mSkipAppTransitionAnimation = false;
2543 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2544 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2545 5000);
2546 }
2547 }
2548 }
2549
2550 public int getPendingAppTransition() {
2551 return mNextAppTransition;
2552 }
Romain Guy06882f82009-06-10 13:36:04 -07002553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 public void executeAppTransition() {
2555 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2556 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002557 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 }
Romain Guy06882f82009-06-10 13:36:04 -07002559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 synchronized(mWindowMap) {
2561 if (DEBUG_APP_TRANSITIONS) Log.v(
2562 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2563 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2564 mAppTransitionReady = true;
2565 final long origId = Binder.clearCallingIdentity();
2566 performLayoutAndPlaceSurfacesLocked();
2567 Binder.restoreCallingIdentity(origId);
2568 }
2569 }
2570 }
2571
2572 public void setAppStartingWindow(IBinder token, String pkg,
2573 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2574 IBinder transferFrom, boolean createIfNeeded) {
2575 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2576 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002577 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 }
2579
2580 synchronized(mWindowMap) {
2581 if (DEBUG_STARTING_WINDOW) Log.v(
2582 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2583 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 AppWindowToken wtoken = findAppWindowToken(token);
2586 if (wtoken == null) {
2587 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2588 return;
2589 }
2590
2591 // If the display is frozen, we won't do anything until the
2592 // actual window is displayed so there is no reason to put in
2593 // the starting window.
2594 if (mDisplayFrozen) {
2595 return;
2596 }
Romain Guy06882f82009-06-10 13:36:04 -07002597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 if (wtoken.startingData != null) {
2599 return;
2600 }
Romain Guy06882f82009-06-10 13:36:04 -07002601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 if (transferFrom != null) {
2603 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2604 if (ttoken != null) {
2605 WindowState startingWindow = ttoken.startingWindow;
2606 if (startingWindow != null) {
2607 if (mStartingIconInTransition) {
2608 // In this case, the starting icon has already
2609 // been displayed, so start letting windows get
2610 // shown immediately without any more transitions.
2611 mSkipAppTransitionAnimation = true;
2612 }
2613 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2614 "Moving existing starting from " + ttoken
2615 + " to " + wtoken);
2616 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 // Transfer the starting window over to the new
2619 // token.
2620 wtoken.startingData = ttoken.startingData;
2621 wtoken.startingView = ttoken.startingView;
2622 wtoken.startingWindow = startingWindow;
2623 ttoken.startingData = null;
2624 ttoken.startingView = null;
2625 ttoken.startingWindow = null;
2626 ttoken.startingMoved = true;
2627 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002628 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 startingWindow.mAppToken = wtoken;
2630 mWindows.remove(startingWindow);
2631 ttoken.windows.remove(startingWindow);
2632 ttoken.allAppWindows.remove(startingWindow);
2633 addWindowToListInOrderLocked(startingWindow, true);
2634 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 // Propagate other interesting state between the
2637 // tokens. If the old token is displayed, we should
2638 // immediately force the new one to be displayed. If
2639 // it is animating, we need to move that animation to
2640 // the new one.
2641 if (ttoken.allDrawn) {
2642 wtoken.allDrawn = true;
2643 }
2644 if (ttoken.firstWindowDrawn) {
2645 wtoken.firstWindowDrawn = true;
2646 }
2647 if (!ttoken.hidden) {
2648 wtoken.hidden = false;
2649 wtoken.hiddenRequested = false;
2650 wtoken.willBeHidden = false;
2651 }
2652 if (wtoken.clientHidden != ttoken.clientHidden) {
2653 wtoken.clientHidden = ttoken.clientHidden;
2654 wtoken.sendAppVisibilityToClients();
2655 }
2656 if (ttoken.animation != null) {
2657 wtoken.animation = ttoken.animation;
2658 wtoken.animating = ttoken.animating;
2659 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2660 ttoken.animation = null;
2661 ttoken.animLayerAdjustment = 0;
2662 wtoken.updateLayers();
2663 ttoken.updateLayers();
2664 }
Romain Guy06882f82009-06-10 13:36:04 -07002665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 mLayoutNeeded = true;
2668 performLayoutAndPlaceSurfacesLocked();
2669 Binder.restoreCallingIdentity(origId);
2670 return;
2671 } else if (ttoken.startingData != null) {
2672 // The previous app was getting ready to show a
2673 // starting window, but hasn't yet done so. Steal it!
2674 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2675 "Moving pending starting from " + ttoken
2676 + " to " + wtoken);
2677 wtoken.startingData = ttoken.startingData;
2678 ttoken.startingData = null;
2679 ttoken.startingMoved = true;
2680 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2681 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2682 // want to process the message ASAP, before any other queued
2683 // messages.
2684 mH.sendMessageAtFrontOfQueue(m);
2685 return;
2686 }
2687 }
2688 }
2689
2690 // There is no existing starting window, and the caller doesn't
2691 // want us to create one, so that's it!
2692 if (!createIfNeeded) {
2693 return;
2694 }
Romain Guy06882f82009-06-10 13:36:04 -07002695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 mStartingIconInTransition = true;
2697 wtoken.startingData = new StartingData(
2698 pkg, theme, nonLocalizedLabel,
2699 labelRes, icon);
2700 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2701 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2702 // want to process the message ASAP, before any other queued
2703 // messages.
2704 mH.sendMessageAtFrontOfQueue(m);
2705 }
2706 }
2707
2708 public void setAppWillBeHidden(IBinder token) {
2709 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2710 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002711 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 }
2713
2714 AppWindowToken wtoken;
2715
2716 synchronized(mWindowMap) {
2717 wtoken = findAppWindowToken(token);
2718 if (wtoken == null) {
2719 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2720 return;
2721 }
2722 wtoken.willBeHidden = true;
2723 }
2724 }
Romain Guy06882f82009-06-10 13:36:04 -07002725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2727 boolean visible, int transit, boolean performLayout) {
2728 boolean delayed = false;
2729
2730 if (wtoken.clientHidden == visible) {
2731 wtoken.clientHidden = !visible;
2732 wtoken.sendAppVisibilityToClients();
2733 }
Romain Guy06882f82009-06-10 13:36:04 -07002734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 wtoken.willBeHidden = false;
2736 if (wtoken.hidden == visible) {
2737 final int N = wtoken.allAppWindows.size();
2738 boolean changed = false;
2739 if (DEBUG_APP_TRANSITIONS) Log.v(
2740 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2741 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2746 if (wtoken.animation == sDummyAnimation) {
2747 wtoken.animation = null;
2748 }
2749 applyAnimationLocked(wtoken, lp, transit, visible);
2750 changed = true;
2751 if (wtoken.animation != null) {
2752 delayed = runningAppAnimation = true;
2753 }
2754 }
Romain Guy06882f82009-06-10 13:36:04 -07002755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 for (int i=0; i<N; i++) {
2757 WindowState win = wtoken.allAppWindows.get(i);
2758 if (win == wtoken.startingWindow) {
2759 continue;
2760 }
2761
2762 if (win.isAnimating()) {
2763 delayed = true;
2764 }
Romain Guy06882f82009-06-10 13:36:04 -07002765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2767 //win.dump(" ");
2768 if (visible) {
2769 if (!win.isVisibleNow()) {
2770 if (!runningAppAnimation) {
2771 applyAnimationLocked(win,
2772 WindowManagerPolicy.TRANSIT_ENTER, true);
2773 }
2774 changed = true;
2775 }
2776 } else if (win.isVisibleNow()) {
2777 if (!runningAppAnimation) {
2778 applyAnimationLocked(win,
2779 WindowManagerPolicy.TRANSIT_EXIT, false);
2780 }
2781 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2782 KeyWaiter.RETURN_NOTHING);
2783 changed = true;
2784 }
2785 }
2786
2787 wtoken.hidden = wtoken.hiddenRequested = !visible;
2788 if (!visible) {
2789 unsetAppFreezingScreenLocked(wtoken, true, true);
2790 } else {
2791 // If we are being set visible, and the starting window is
2792 // not yet displayed, then make sure it doesn't get displayed.
2793 WindowState swin = wtoken.startingWindow;
2794 if (swin != null && (swin.mDrawPending
2795 || swin.mCommitDrawPending)) {
2796 swin.mPolicyVisibility = false;
2797 swin.mPolicyVisibilityAfterAnim = false;
2798 }
2799 }
Romain Guy06882f82009-06-10 13:36:04 -07002800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2802 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2803 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 if (changed && performLayout) {
2806 mLayoutNeeded = true;
2807 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 performLayoutAndPlaceSurfacesLocked();
2809 }
2810 }
2811
2812 if (wtoken.animation != null) {
2813 delayed = true;
2814 }
Romain Guy06882f82009-06-10 13:36:04 -07002815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 return delayed;
2817 }
2818
2819 public void setAppVisibility(IBinder token, boolean visible) {
2820 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2821 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002822 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
2824
2825 AppWindowToken wtoken;
2826
2827 synchronized(mWindowMap) {
2828 wtoken = findAppWindowToken(token);
2829 if (wtoken == null) {
2830 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2831 return;
2832 }
2833
2834 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2835 RuntimeException e = new RuntimeException();
2836 e.fillInStackTrace();
2837 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2838 + "): mNextAppTransition=" + mNextAppTransition
2839 + " hidden=" + wtoken.hidden
2840 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2841 }
Romain Guy06882f82009-06-10 13:36:04 -07002842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 // If we are preparing an app transition, then delay changing
2844 // the visibility of this token until we execute that transition.
2845 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2846 // Already in requested state, don't do anything more.
2847 if (wtoken.hiddenRequested != visible) {
2848 return;
2849 }
2850 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 if (DEBUG_APP_TRANSITIONS) Log.v(
2853 TAG, "Setting dummy animation on: " + wtoken);
2854 wtoken.setDummyAnimation();
2855 mOpeningApps.remove(wtoken);
2856 mClosingApps.remove(wtoken);
2857 wtoken.inPendingTransaction = true;
2858 if (visible) {
2859 mOpeningApps.add(wtoken);
2860 wtoken.allDrawn = false;
2861 wtoken.startingDisplayed = false;
2862 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 if (wtoken.clientHidden) {
2865 // In the case where we are making an app visible
2866 // but holding off for a transition, we still need
2867 // to tell the client to make its windows visible so
2868 // they get drawn. Otherwise, we will wait on
2869 // performing the transition until all windows have
2870 // been drawn, they never will be, and we are sad.
2871 wtoken.clientHidden = false;
2872 wtoken.sendAppVisibilityToClients();
2873 }
2874 } else {
2875 mClosingApps.add(wtoken);
2876 }
2877 return;
2878 }
Romain Guy06882f82009-06-10 13:36:04 -07002879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 final long origId = Binder.clearCallingIdentity();
2881 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2882 wtoken.updateReportedVisibilityLocked();
2883 Binder.restoreCallingIdentity(origId);
2884 }
2885 }
2886
2887 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2888 boolean unfreezeSurfaceNow, boolean force) {
2889 if (wtoken.freezingScreen) {
2890 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2891 + " force=" + force);
2892 final int N = wtoken.allAppWindows.size();
2893 boolean unfrozeWindows = false;
2894 for (int i=0; i<N; i++) {
2895 WindowState w = wtoken.allAppWindows.get(i);
2896 if (w.mAppFreezing) {
2897 w.mAppFreezing = false;
2898 if (w.mSurface != null && !w.mOrientationChanging) {
2899 w.mOrientationChanging = true;
2900 }
2901 unfrozeWindows = true;
2902 }
2903 }
2904 if (force || unfrozeWindows) {
2905 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2906 wtoken.freezingScreen = false;
2907 mAppsFreezingScreen--;
2908 }
2909 if (unfreezeSurfaceNow) {
2910 if (unfrozeWindows) {
2911 mLayoutNeeded = true;
2912 performLayoutAndPlaceSurfacesLocked();
2913 }
2914 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2915 stopFreezingDisplayLocked();
2916 }
2917 }
2918 }
2919 }
Romain Guy06882f82009-06-10 13:36:04 -07002920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2922 int configChanges) {
2923 if (DEBUG_ORIENTATION) {
2924 RuntimeException e = new RuntimeException();
2925 e.fillInStackTrace();
2926 Log.i(TAG, "Set freezing of " + wtoken.appToken
2927 + ": hidden=" + wtoken.hidden + " freezing="
2928 + wtoken.freezingScreen, e);
2929 }
2930 if (!wtoken.hiddenRequested) {
2931 if (!wtoken.freezingScreen) {
2932 wtoken.freezingScreen = true;
2933 mAppsFreezingScreen++;
2934 if (mAppsFreezingScreen == 1) {
2935 startFreezingDisplayLocked();
2936 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
2937 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
2938 5000);
2939 }
2940 }
2941 final int N = wtoken.allAppWindows.size();
2942 for (int i=0; i<N; i++) {
2943 WindowState w = wtoken.allAppWindows.get(i);
2944 w.mAppFreezing = true;
2945 }
2946 }
2947 }
Romain Guy06882f82009-06-10 13:36:04 -07002948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 public void startAppFreezingScreen(IBinder token, int configChanges) {
2950 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2951 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002952 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
2954
2955 synchronized(mWindowMap) {
2956 if (configChanges == 0 && !mDisplayFrozen) {
2957 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
2958 return;
2959 }
Romain Guy06882f82009-06-10 13:36:04 -07002960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 AppWindowToken wtoken = findAppWindowToken(token);
2962 if (wtoken == null || wtoken.appToken == null) {
2963 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
2964 return;
2965 }
2966 final long origId = Binder.clearCallingIdentity();
2967 startAppFreezingScreenLocked(wtoken, configChanges);
2968 Binder.restoreCallingIdentity(origId);
2969 }
2970 }
Romain Guy06882f82009-06-10 13:36:04 -07002971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 public void stopAppFreezingScreen(IBinder token, boolean force) {
2973 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2974 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002975 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 }
2977
2978 synchronized(mWindowMap) {
2979 AppWindowToken wtoken = findAppWindowToken(token);
2980 if (wtoken == null || wtoken.appToken == null) {
2981 return;
2982 }
2983 final long origId = Binder.clearCallingIdentity();
2984 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
2985 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
2986 unsetAppFreezingScreenLocked(wtoken, true, force);
2987 Binder.restoreCallingIdentity(origId);
2988 }
2989 }
Romain Guy06882f82009-06-10 13:36:04 -07002990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 public void removeAppToken(IBinder token) {
2992 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2993 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002994 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 }
2996
2997 AppWindowToken wtoken = null;
2998 AppWindowToken startingToken = null;
2999 boolean delayed = false;
3000
3001 final long origId = Binder.clearCallingIdentity();
3002 synchronized(mWindowMap) {
3003 WindowToken basewtoken = mTokenMap.remove(token);
3004 mTokenList.remove(basewtoken);
3005 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3006 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3007 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3008 wtoken.inPendingTransaction = false;
3009 mOpeningApps.remove(wtoken);
3010 if (mClosingApps.contains(wtoken)) {
3011 delayed = true;
3012 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3013 mClosingApps.add(wtoken);
3014 delayed = true;
3015 }
3016 if (DEBUG_APP_TRANSITIONS) Log.v(
3017 TAG, "Removing app " + wtoken + " delayed=" + delayed
3018 + " animation=" + wtoken.animation
3019 + " animating=" + wtoken.animating);
3020 if (delayed) {
3021 // set the token aside because it has an active animation to be finished
3022 mExitingAppTokens.add(wtoken);
3023 }
3024 mAppTokens.remove(wtoken);
3025 wtoken.removed = true;
3026 if (wtoken.startingData != null) {
3027 startingToken = wtoken;
3028 }
3029 unsetAppFreezingScreenLocked(wtoken, true, true);
3030 if (mFocusedApp == wtoken) {
3031 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3032 mFocusedApp = null;
3033 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3034 mKeyWaiter.tickle();
3035 }
3036 } else {
3037 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3038 }
Romain Guy06882f82009-06-10 13:36:04 -07003039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 if (!delayed && wtoken != null) {
3041 wtoken.updateReportedVisibilityLocked();
3042 }
3043 }
3044 Binder.restoreCallingIdentity(origId);
3045
3046 if (startingToken != null) {
3047 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3048 + startingToken + ": app token removed");
3049 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3050 mH.sendMessage(m);
3051 }
3052 }
3053
3054 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3055 final int NW = token.windows.size();
3056 for (int i=0; i<NW; i++) {
3057 WindowState win = token.windows.get(i);
3058 mWindows.remove(win);
3059 int j = win.mChildWindows.size();
3060 while (j > 0) {
3061 j--;
3062 mWindows.remove(win.mChildWindows.get(j));
3063 }
3064 }
3065 return NW > 0;
3066 }
3067
3068 void dumpAppTokensLocked() {
3069 for (int i=mAppTokens.size()-1; i>=0; i--) {
3070 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3071 }
3072 }
Romain Guy06882f82009-06-10 13:36:04 -07003073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 void dumpWindowsLocked() {
3075 for (int i=mWindows.size()-1; i>=0; i--) {
3076 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3077 }
3078 }
Romain Guy06882f82009-06-10 13:36:04 -07003079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 private int findWindowOffsetLocked(int tokenPos) {
3081 final int NW = mWindows.size();
3082
3083 if (tokenPos >= mAppTokens.size()) {
3084 int i = NW;
3085 while (i > 0) {
3086 i--;
3087 WindowState win = (WindowState)mWindows.get(i);
3088 if (win.getAppToken() != null) {
3089 return i+1;
3090 }
3091 }
3092 }
3093
3094 while (tokenPos > 0) {
3095 // Find the first app token below the new position that has
3096 // a window displayed.
3097 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3098 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3099 + tokenPos + " -- " + wtoken.token);
3100 int i = wtoken.windows.size();
3101 while (i > 0) {
3102 i--;
3103 WindowState win = wtoken.windows.get(i);
3104 int j = win.mChildWindows.size();
3105 while (j > 0) {
3106 j--;
3107 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3108 if (cwin.mSubLayer >= 0 ) {
3109 for (int pos=NW-1; pos>=0; pos--) {
3110 if (mWindows.get(pos) == cwin) {
3111 if (DEBUG_REORDER) Log.v(TAG,
3112 "Found child win @" + (pos+1));
3113 return pos+1;
3114 }
3115 }
3116 }
3117 }
3118 for (int pos=NW-1; pos>=0; pos--) {
3119 if (mWindows.get(pos) == win) {
3120 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3121 return pos+1;
3122 }
3123 }
3124 }
3125 tokenPos--;
3126 }
3127
3128 return 0;
3129 }
3130
3131 private final int reAddWindowLocked(int index, WindowState win) {
3132 final int NCW = win.mChildWindows.size();
3133 boolean added = false;
3134 for (int j=0; j<NCW; j++) {
3135 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3136 if (!added && cwin.mSubLayer >= 0) {
3137 mWindows.add(index, win);
3138 index++;
3139 added = true;
3140 }
3141 mWindows.add(index, cwin);
3142 index++;
3143 }
3144 if (!added) {
3145 mWindows.add(index, win);
3146 index++;
3147 }
3148 return index;
3149 }
Romain Guy06882f82009-06-10 13:36:04 -07003150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3152 final int NW = token.windows.size();
3153 for (int i=0; i<NW; i++) {
3154 index = reAddWindowLocked(index, token.windows.get(i));
3155 }
3156 return index;
3157 }
3158
3159 public void moveAppToken(int index, IBinder token) {
3160 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3161 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003162 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 }
3164
3165 synchronized(mWindowMap) {
3166 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3167 if (DEBUG_REORDER) dumpAppTokensLocked();
3168 final AppWindowToken wtoken = findAppWindowToken(token);
3169 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3170 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3171 + token + " (" + wtoken + ")");
3172 return;
3173 }
3174 mAppTokens.add(index, wtoken);
3175 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3176 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 final long origId = Binder.clearCallingIdentity();
3179 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3180 if (DEBUG_REORDER) dumpWindowsLocked();
3181 if (tmpRemoveAppWindowsLocked(wtoken)) {
3182 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3183 if (DEBUG_REORDER) dumpWindowsLocked();
3184 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3185 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3186 if (DEBUG_REORDER) dumpWindowsLocked();
3187 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 mLayoutNeeded = true;
3189 performLayoutAndPlaceSurfacesLocked();
3190 }
3191 Binder.restoreCallingIdentity(origId);
3192 }
3193 }
3194
3195 private void removeAppTokensLocked(List<IBinder> tokens) {
3196 // XXX This should be done more efficiently!
3197 // (take advantage of the fact that both lists should be
3198 // ordered in the same way.)
3199 int N = tokens.size();
3200 for (int i=0; i<N; i++) {
3201 IBinder token = tokens.get(i);
3202 final AppWindowToken wtoken = findAppWindowToken(token);
3203 if (!mAppTokens.remove(wtoken)) {
3204 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3205 + token + " (" + wtoken + ")");
3206 i--;
3207 N--;
3208 }
3209 }
3210 }
3211
3212 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3213 // First remove all of the windows from the list.
3214 final int N = tokens.size();
3215 int i;
3216 for (i=0; i<N; i++) {
3217 WindowToken token = mTokenMap.get(tokens.get(i));
3218 if (token != null) {
3219 tmpRemoveAppWindowsLocked(token);
3220 }
3221 }
3222
3223 // Where to start adding?
3224 int pos = findWindowOffsetLocked(tokenPos);
3225
3226 // And now add them back at the correct place.
3227 for (i=0; i<N; i++) {
3228 WindowToken token = mTokenMap.get(tokens.get(i));
3229 if (token != null) {
3230 pos = reAddAppWindowsLocked(pos, token);
3231 }
3232 }
3233
3234 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 mLayoutNeeded = true;
3236 performLayoutAndPlaceSurfacesLocked();
3237
3238 //dump();
3239 }
3240
3241 public void moveAppTokensToTop(List<IBinder> tokens) {
3242 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3243 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003244 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 }
3246
3247 final long origId = Binder.clearCallingIdentity();
3248 synchronized(mWindowMap) {
3249 removeAppTokensLocked(tokens);
3250 final int N = tokens.size();
3251 for (int i=0; i<N; i++) {
3252 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3253 if (wt != null) {
3254 mAppTokens.add(wt);
3255 }
3256 }
3257 moveAppWindowsLocked(tokens, mAppTokens.size());
3258 }
3259 Binder.restoreCallingIdentity(origId);
3260 }
3261
3262 public void moveAppTokensToBottom(List<IBinder> tokens) {
3263 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3264 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003265 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 }
3267
3268 final long origId = Binder.clearCallingIdentity();
3269 synchronized(mWindowMap) {
3270 removeAppTokensLocked(tokens);
3271 final int N = tokens.size();
3272 int pos = 0;
3273 for (int i=0; i<N; i++) {
3274 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3275 if (wt != null) {
3276 mAppTokens.add(pos, wt);
3277 pos++;
3278 }
3279 }
3280 moveAppWindowsLocked(tokens, 0);
3281 }
3282 Binder.restoreCallingIdentity(origId);
3283 }
3284
3285 // -------------------------------------------------------------
3286 // Misc IWindowSession methods
3287 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 public void disableKeyguard(IBinder token, String tag) {
3290 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3291 != PackageManager.PERMISSION_GRANTED) {
3292 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3293 }
3294 mKeyguardDisabled.acquire(token, tag);
3295 }
3296
3297 public void reenableKeyguard(IBinder token) {
3298 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3299 != PackageManager.PERMISSION_GRANTED) {
3300 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3301 }
3302 synchronized (mKeyguardDisabled) {
3303 mKeyguardDisabled.release(token);
3304
3305 if (!mKeyguardDisabled.isAcquired()) {
3306 // if we are the last one to reenable the keyguard wait until
3307 // we have actaully finished reenabling until returning
3308 mWaitingUntilKeyguardReenabled = true;
3309 while (mWaitingUntilKeyguardReenabled) {
3310 try {
3311 mKeyguardDisabled.wait();
3312 } catch (InterruptedException e) {
3313 Thread.currentThread().interrupt();
3314 }
3315 }
3316 }
3317 }
3318 }
3319
3320 /**
3321 * @see android.app.KeyguardManager#exitKeyguardSecurely
3322 */
3323 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3324 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3325 != PackageManager.PERMISSION_GRANTED) {
3326 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3327 }
3328 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3329 public void onKeyguardExitResult(boolean success) {
3330 try {
3331 callback.onKeyguardExitResult(success);
3332 } catch (RemoteException e) {
3333 // Client has died, we don't care.
3334 }
3335 }
3336 });
3337 }
3338
3339 public boolean inKeyguardRestrictedInputMode() {
3340 return mPolicy.inKeyguardRestrictedKeyInputMode();
3341 }
Romain Guy06882f82009-06-10 13:36:04 -07003342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 static float fixScale(float scale) {
3344 if (scale < 0) scale = 0;
3345 else if (scale > 20) scale = 20;
3346 return Math.abs(scale);
3347 }
Romain Guy06882f82009-06-10 13:36:04 -07003348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 public void setAnimationScale(int which, float scale) {
3350 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3351 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003352 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 }
3354
3355 if (scale < 0) scale = 0;
3356 else if (scale > 20) scale = 20;
3357 scale = Math.abs(scale);
3358 switch (which) {
3359 case 0: mWindowAnimationScale = fixScale(scale); break;
3360 case 1: mTransitionAnimationScale = fixScale(scale); break;
3361 }
Romain Guy06882f82009-06-10 13:36:04 -07003362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 // Persist setting
3364 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3365 }
Romain Guy06882f82009-06-10 13:36:04 -07003366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 public void setAnimationScales(float[] scales) {
3368 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3369 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003370 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 }
3372
3373 if (scales != null) {
3374 if (scales.length >= 1) {
3375 mWindowAnimationScale = fixScale(scales[0]);
3376 }
3377 if (scales.length >= 2) {
3378 mTransitionAnimationScale = fixScale(scales[1]);
3379 }
3380 }
Romain Guy06882f82009-06-10 13:36:04 -07003381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 // Persist setting
3383 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3384 }
Romain Guy06882f82009-06-10 13:36:04 -07003385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 public float getAnimationScale(int which) {
3387 switch (which) {
3388 case 0: return mWindowAnimationScale;
3389 case 1: return mTransitionAnimationScale;
3390 }
3391 return 0;
3392 }
Romain Guy06882f82009-06-10 13:36:04 -07003393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 public float[] getAnimationScales() {
3395 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3396 }
Romain Guy06882f82009-06-10 13:36:04 -07003397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 public int getSwitchState(int sw) {
3399 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3400 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003401 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 }
3403 return KeyInputQueue.getSwitchState(sw);
3404 }
Romain Guy06882f82009-06-10 13:36:04 -07003405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 public int getSwitchStateForDevice(int devid, int sw) {
3407 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3408 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003409 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 }
3411 return KeyInputQueue.getSwitchState(devid, sw);
3412 }
Romain Guy06882f82009-06-10 13:36:04 -07003413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 public int getScancodeState(int sw) {
3415 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3416 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003417 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 }
3419 return KeyInputQueue.getScancodeState(sw);
3420 }
Romain Guy06882f82009-06-10 13:36:04 -07003421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 public int getScancodeStateForDevice(int devid, int sw) {
3423 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3424 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003425 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 }
3427 return KeyInputQueue.getScancodeState(devid, sw);
3428 }
Romain Guy06882f82009-06-10 13:36:04 -07003429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 public int getKeycodeState(int sw) {
3431 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3432 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003433 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 }
3435 return KeyInputQueue.getKeycodeState(sw);
3436 }
Romain Guy06882f82009-06-10 13:36:04 -07003437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 public int getKeycodeStateForDevice(int devid, int sw) {
3439 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3440 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003441 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 }
3443 return KeyInputQueue.getKeycodeState(devid, sw);
3444 }
Romain Guy06882f82009-06-10 13:36:04 -07003445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3447 return KeyInputQueue.hasKeys(keycodes, keyExists);
3448 }
Romain Guy06882f82009-06-10 13:36:04 -07003449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 public void enableScreenAfterBoot() {
3451 synchronized(mWindowMap) {
3452 if (mSystemBooted) {
3453 return;
3454 }
3455 mSystemBooted = true;
3456 }
Romain Guy06882f82009-06-10 13:36:04 -07003457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 performEnableScreen();
3459 }
Romain Guy06882f82009-06-10 13:36:04 -07003460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 public void enableScreenIfNeededLocked() {
3462 if (mDisplayEnabled) {
3463 return;
3464 }
3465 if (!mSystemBooted) {
3466 return;
3467 }
3468 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3469 }
Romain Guy06882f82009-06-10 13:36:04 -07003470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 public void performEnableScreen() {
3472 synchronized(mWindowMap) {
3473 if (mDisplayEnabled) {
3474 return;
3475 }
3476 if (!mSystemBooted) {
3477 return;
3478 }
Romain Guy06882f82009-06-10 13:36:04 -07003479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 // Don't enable the screen until all existing windows
3481 // have been drawn.
3482 final int N = mWindows.size();
3483 for (int i=0; i<N; i++) {
3484 WindowState w = (WindowState)mWindows.get(i);
3485 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3486 return;
3487 }
3488 }
Romain Guy06882f82009-06-10 13:36:04 -07003489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 mDisplayEnabled = true;
3491 if (false) {
3492 Log.i(TAG, "ENABLING SCREEN!");
3493 StringWriter sw = new StringWriter();
3494 PrintWriter pw = new PrintWriter(sw);
3495 this.dump(null, pw, null);
3496 Log.i(TAG, sw.toString());
3497 }
3498 try {
3499 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3500 if (surfaceFlinger != null) {
3501 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3502 Parcel data = Parcel.obtain();
3503 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3504 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3505 data, null, 0);
3506 data.recycle();
3507 }
3508 } catch (RemoteException ex) {
3509 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3510 }
3511 }
Romain Guy06882f82009-06-10 13:36:04 -07003512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003516 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3517 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 }
Romain Guy06882f82009-06-10 13:36:04 -07003519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 public void setInTouchMode(boolean mode) {
3521 synchronized(mWindowMap) {
3522 mInTouchMode = mode;
3523 }
3524 }
3525
Romain Guy06882f82009-06-10 13:36:04 -07003526 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003527 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003529 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003530 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 }
3532
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003533 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 }
Romain Guy06882f82009-06-10 13:36:04 -07003535
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003536 public void setRotationUnchecked(int rotation,
3537 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 if(DEBUG_ORIENTATION) Log.v(TAG,
3539 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 long origId = Binder.clearCallingIdentity();
3542 boolean changed;
3543 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003544 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 }
Romain Guy06882f82009-06-10 13:36:04 -07003546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 if (changed) {
3548 sendNewConfiguration();
3549 synchronized(mWindowMap) {
3550 mLayoutNeeded = true;
3551 performLayoutAndPlaceSurfacesLocked();
3552 }
3553 } else if (alwaysSendConfiguration) {
3554 //update configuration ignoring orientation change
3555 sendNewConfiguration();
3556 }
Romain Guy06882f82009-06-10 13:36:04 -07003557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 Binder.restoreCallingIdentity(origId);
3559 }
Romain Guy06882f82009-06-10 13:36:04 -07003560
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003561 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 boolean changed;
3563 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3564 rotation = mRequestedRotation;
3565 } else {
3566 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003567 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 }
3569 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003570 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 mRotation, mDisplayEnabled);
3572 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3573 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003576 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 "Rotation changed to " + rotation
3578 + " from " + mRotation
3579 + " (forceApp=" + mForcedAppOrientation
3580 + ", req=" + mRequestedRotation + ")");
3581 mRotation = rotation;
3582 mWindowsFreezingScreen = true;
3583 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3584 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3585 2000);
3586 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003587 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 mQueue.setOrientation(rotation);
3589 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003590 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 }
3592 for (int i=mWindows.size()-1; i>=0; i--) {
3593 WindowState w = (WindowState)mWindows.get(i);
3594 if (w.mSurface != null) {
3595 w.mOrientationChanging = true;
3596 }
3597 }
3598 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3599 try {
3600 mRotationWatchers.get(i).onRotationChanged(rotation);
3601 } catch (RemoteException e) {
3602 }
3603 }
3604 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003606 return changed;
3607 }
Romain Guy06882f82009-06-10 13:36:04 -07003608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 public int getRotation() {
3610 return mRotation;
3611 }
3612
3613 public int watchRotation(IRotationWatcher watcher) {
3614 final IBinder watcherBinder = watcher.asBinder();
3615 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3616 public void binderDied() {
3617 synchronized (mWindowMap) {
3618 for (int i=0; i<mRotationWatchers.size(); i++) {
3619 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003620 IRotationWatcher removed = mRotationWatchers.remove(i);
3621 if (removed != null) {
3622 removed.asBinder().unlinkToDeath(this, 0);
3623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 i--;
3625 }
3626 }
3627 }
3628 }
3629 };
Romain Guy06882f82009-06-10 13:36:04 -07003630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 synchronized (mWindowMap) {
3632 try {
3633 watcher.asBinder().linkToDeath(dr, 0);
3634 mRotationWatchers.add(watcher);
3635 } catch (RemoteException e) {
3636 // Client died, no cleanup needed.
3637 }
Romain Guy06882f82009-06-10 13:36:04 -07003638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 return mRotation;
3640 }
3641 }
3642
3643 /**
3644 * Starts the view server on the specified port.
3645 *
3646 * @param port The port to listener to.
3647 *
3648 * @return True if the server was successfully started, false otherwise.
3649 *
3650 * @see com.android.server.ViewServer
3651 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3652 */
3653 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003654 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 return false;
3656 }
3657
3658 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3659 return false;
3660 }
3661
3662 if (port < 1024) {
3663 return false;
3664 }
3665
3666 if (mViewServer != null) {
3667 if (!mViewServer.isRunning()) {
3668 try {
3669 return mViewServer.start();
3670 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003671 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 }
3673 }
3674 return false;
3675 }
3676
3677 try {
3678 mViewServer = new ViewServer(this, port);
3679 return mViewServer.start();
3680 } catch (IOException e) {
3681 Log.w(TAG, "View server did not start");
3682 }
3683 return false;
3684 }
3685
Romain Guy06882f82009-06-10 13:36:04 -07003686 private boolean isSystemSecure() {
3687 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3688 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3689 }
3690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 /**
3692 * Stops the view server if it exists.
3693 *
3694 * @return True if the server stopped, false if it wasn't started or
3695 * couldn't be stopped.
3696 *
3697 * @see com.android.server.ViewServer
3698 */
3699 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003700 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 return false;
3702 }
3703
3704 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3705 return false;
3706 }
3707
3708 if (mViewServer != null) {
3709 return mViewServer.stop();
3710 }
3711 return false;
3712 }
3713
3714 /**
3715 * Indicates whether the view server is running.
3716 *
3717 * @return True if the server is running, false otherwise.
3718 *
3719 * @see com.android.server.ViewServer
3720 */
3721 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003722 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 return false;
3724 }
3725
3726 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3727 return false;
3728 }
3729
3730 return mViewServer != null && mViewServer.isRunning();
3731 }
3732
3733 /**
3734 * Lists all availble windows in the system. The listing is written in the
3735 * specified Socket's output stream with the following syntax:
3736 * windowHashCodeInHexadecimal windowName
3737 * Each line of the ouput represents a different window.
3738 *
3739 * @param client The remote client to send the listing to.
3740 * @return False if an error occured, true otherwise.
3741 */
3742 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003743 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 return false;
3745 }
3746
3747 boolean result = true;
3748
3749 Object[] windows;
3750 synchronized (mWindowMap) {
3751 windows = new Object[mWindows.size()];
3752 //noinspection unchecked
3753 windows = mWindows.toArray(windows);
3754 }
3755
3756 BufferedWriter out = null;
3757
3758 // Any uncaught exception will crash the system process
3759 try {
3760 OutputStream clientStream = client.getOutputStream();
3761 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3762
3763 final int count = windows.length;
3764 for (int i = 0; i < count; i++) {
3765 final WindowState w = (WindowState) windows[i];
3766 out.write(Integer.toHexString(System.identityHashCode(w)));
3767 out.write(' ');
3768 out.append(w.mAttrs.getTitle());
3769 out.write('\n');
3770 }
3771
3772 out.write("DONE.\n");
3773 out.flush();
3774 } catch (Exception e) {
3775 result = false;
3776 } finally {
3777 if (out != null) {
3778 try {
3779 out.close();
3780 } catch (IOException e) {
3781 result = false;
3782 }
3783 }
3784 }
3785
3786 return result;
3787 }
3788
3789 /**
3790 * Sends a command to a target window. The result of the command, if any, will be
3791 * written in the output stream of the specified socket.
3792 *
3793 * The parameters must follow this syntax:
3794 * windowHashcode extra
3795 *
3796 * Where XX is the length in characeters of the windowTitle.
3797 *
3798 * The first parameter is the target window. The window with the specified hashcode
3799 * will be the target. If no target can be found, nothing happens. The extra parameters
3800 * will be delivered to the target window and as parameters to the command itself.
3801 *
3802 * @param client The remote client to sent the result, if any, to.
3803 * @param command The command to execute.
3804 * @param parameters The command parameters.
3805 *
3806 * @return True if the command was successfully delivered, false otherwise. This does
3807 * not indicate whether the command itself was successful.
3808 */
3809 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003810 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811 return false;
3812 }
3813
3814 boolean success = true;
3815 Parcel data = null;
3816 Parcel reply = null;
3817
3818 // Any uncaught exception will crash the system process
3819 try {
3820 // Find the hashcode of the window
3821 int index = parameters.indexOf(' ');
3822 if (index == -1) {
3823 index = parameters.length();
3824 }
3825 final String code = parameters.substring(0, index);
3826 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3827
3828 // Extract the command's parameter after the window description
3829 if (index < parameters.length()) {
3830 parameters = parameters.substring(index + 1);
3831 } else {
3832 parameters = "";
3833 }
3834
3835 final WindowManagerService.WindowState window = findWindow(hashCode);
3836 if (window == null) {
3837 return false;
3838 }
3839
3840 data = Parcel.obtain();
3841 data.writeInterfaceToken("android.view.IWindow");
3842 data.writeString(command);
3843 data.writeString(parameters);
3844 data.writeInt(1);
3845 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3846
3847 reply = Parcel.obtain();
3848
3849 final IBinder binder = window.mClient.asBinder();
3850 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3851 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3852
3853 reply.readException();
3854
3855 } catch (Exception e) {
3856 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3857 success = false;
3858 } finally {
3859 if (data != null) {
3860 data.recycle();
3861 }
3862 if (reply != null) {
3863 reply.recycle();
3864 }
3865 }
3866
3867 return success;
3868 }
3869
3870 private WindowState findWindow(int hashCode) {
3871 if (hashCode == -1) {
3872 return getFocusedWindow();
3873 }
3874
3875 synchronized (mWindowMap) {
3876 final ArrayList windows = mWindows;
3877 final int count = windows.size();
3878
3879 for (int i = 0; i < count; i++) {
3880 WindowState w = (WindowState) windows.get(i);
3881 if (System.identityHashCode(w) == hashCode) {
3882 return w;
3883 }
3884 }
3885 }
3886
3887 return null;
3888 }
3889
3890 /*
3891 * Instruct the Activity Manager to fetch the current configuration and broadcast
3892 * that to config-changed listeners if appropriate.
3893 */
3894 void sendNewConfiguration() {
3895 try {
3896 mActivityManager.updateConfiguration(null);
3897 } catch (RemoteException e) {
3898 }
3899 }
Romain Guy06882f82009-06-10 13:36:04 -07003900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003901 public Configuration computeNewConfiguration() {
3902 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003903 return computeNewConfigurationLocked();
3904 }
3905 }
Romain Guy06882f82009-06-10 13:36:04 -07003906
Dianne Hackbornc485a602009-03-24 22:39:49 -07003907 Configuration computeNewConfigurationLocked() {
3908 Configuration config = new Configuration();
3909 if (!computeNewConfigurationLocked(config)) {
3910 return null;
3911 }
3912 Log.i(TAG, "Config changed: " + config);
3913 long now = SystemClock.uptimeMillis();
3914 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3915 if (mFreezeGcPending != 0) {
3916 if (now > (mFreezeGcPending+1000)) {
3917 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3918 mH.removeMessages(H.FORCE_GC);
3919 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 mFreezeGcPending = now;
3921 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003922 } else {
3923 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003925 return config;
3926 }
Romain Guy06882f82009-06-10 13:36:04 -07003927
Dianne Hackbornc485a602009-03-24 22:39:49 -07003928 boolean computeNewConfigurationLocked(Configuration config) {
3929 if (mDisplay == null) {
3930 return false;
3931 }
3932 mQueue.getInputConfiguration(config);
3933 final int dw = mDisplay.getWidth();
3934 final int dh = mDisplay.getHeight();
3935 int orientation = Configuration.ORIENTATION_SQUARE;
3936 if (dw < dh) {
3937 orientation = Configuration.ORIENTATION_PORTRAIT;
3938 } else if (dw > dh) {
3939 orientation = Configuration.ORIENTATION_LANDSCAPE;
3940 }
3941 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003942
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003943 DisplayMetrics dm = new DisplayMetrics();
3944 mDisplay.getMetrics(dm);
3945 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
3946
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003947 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07003948 // Note we only do this once because at this point we don't
3949 // expect the screen to change in this way at runtime, and want
3950 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07003951 int longSize = dw;
3952 int shortSize = dh;
3953 if (longSize < shortSize) {
3954 int tmp = longSize;
3955 longSize = shortSize;
3956 shortSize = tmp;
3957 }
3958 longSize = (int)(longSize/dm.density);
3959 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003960
Dianne Hackborn723738c2009-06-25 19:48:04 -07003961 // These semi-magic numbers define our compatibility modes for
3962 // applications with different screens. Don't change unless you
3963 // make sure to test lots and lots of apps!
3964 if (longSize < 470) {
3965 // This is shorter than an HVGA normal density screen (which
3966 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003967 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
3968 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003969 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003970 // Is this a large screen?
3971 if (longSize > 640 && shortSize >= 480) {
3972 // VGA or larger screens at medium density are the point
3973 // at which we consider it to be a large screen.
3974 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
3975 } else {
3976 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
3977
3978 // If this screen is wider than normal HVGA, or taller
3979 // than FWVGA, then for old apps we want to run in size
3980 // compatibility mode.
3981 if (shortSize > 321 || longSize > 570) {
3982 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
3983 }
3984 }
3985
3986 // Is this a long screen?
3987 if (((longSize*3)/5) >= (shortSize-1)) {
3988 // Anything wider than WVGA (5:3) is considering to be long.
3989 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
3990 } else {
3991 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
3992 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07003993 }
3994 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003995 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003996
Dianne Hackbornc485a602009-03-24 22:39:49 -07003997 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3998 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3999 mPolicy.adjustConfigurationLw(config);
4000 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 }
Romain Guy06882f82009-06-10 13:36:04 -07004002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 // -------------------------------------------------------------
4004 // Input Events and Focus Management
4005 // -------------------------------------------------------------
4006
4007 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004008 long curTime = SystemClock.uptimeMillis();
4009
Michael Chane10de972009-05-18 11:24:50 -07004010 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004011 if (mLastTouchEventType == eventType &&
4012 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4013 return;
4014 }
4015 mLastUserActivityCallTime = curTime;
4016 mLastTouchEventType = eventType;
4017 }
4018
4019 if (targetWin == null
4020 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4021 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 }
4023 }
4024
4025 // tells if it's a cheek event or not -- this function is stateful
4026 private static final int EVENT_NONE = 0;
4027 private static final int EVENT_UNKNOWN = 0;
4028 private static final int EVENT_CHEEK = 0;
4029 private static final int EVENT_IGNORE_DURATION = 300; // ms
4030 private static final float CHEEK_THRESHOLD = 0.6f;
4031 private int mEventState = EVENT_NONE;
4032 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 private int eventType(MotionEvent ev) {
4035 float size = ev.getSize();
4036 switch (ev.getAction()) {
4037 case MotionEvent.ACTION_DOWN:
4038 mEventSize = size;
4039 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4040 case MotionEvent.ACTION_UP:
4041 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004042 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 case MotionEvent.ACTION_MOVE:
4044 final int N = ev.getHistorySize();
4045 if (size > mEventSize) mEventSize = size;
4046 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4047 for (int i=0; i<N; i++) {
4048 size = ev.getHistoricalSize(i);
4049 if (size > mEventSize) mEventSize = size;
4050 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4051 }
4052 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4053 return TOUCH_EVENT;
4054 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004055 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 }
4057 default:
4058 // not good
4059 return OTHER_EVENT;
4060 }
4061 }
4062
4063 /**
4064 * @return Returns true if event was dispatched, false if it was dropped for any reason
4065 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004066 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4068 "dispatchPointer " + ev);
4069
Michael Chan53071d62009-05-13 17:29:48 -07004070 if (MEASURE_LATENCY) {
4071 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4072 }
4073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004075 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076
Michael Chan53071d62009-05-13 17:29:48 -07004077 if (MEASURE_LATENCY) {
4078 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4079 }
4080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 if (action == MotionEvent.ACTION_UP) {
4084 // let go of our target
4085 mKeyWaiter.mMotionTarget = null;
4086 mPowerManager.logPointerUpEvent();
4087 } else if (action == MotionEvent.ACTION_DOWN) {
4088 mPowerManager.logPointerDownEvent();
4089 }
4090
4091 if (targetObj == null) {
4092 // In this case we are either dropping the event, or have received
4093 // a move or up without a down. It is common to receive move
4094 // events in such a way, since this means the user is moving the
4095 // pointer without actually pressing down. All other cases should
4096 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004097 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4099 }
4100 if (qev != null) {
4101 mQueue.recycleEvent(qev);
4102 }
4103 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004104 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 }
4106 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4107 if (qev != null) {
4108 mQueue.recycleEvent(qev);
4109 }
4110 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004111 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 }
Romain Guy06882f82009-06-10 13:36:04 -07004113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004117 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118
4119 //Log.i(TAG, "Sending " + ev + " to " + target);
4120
4121 if (uid != 0 && uid != target.mSession.mUid) {
4122 if (mContext.checkPermission(
4123 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4124 != PackageManager.PERMISSION_GRANTED) {
4125 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4126 + pid + " uid " + uid + " to window " + target
4127 + " owned by uid " + target.mSession.mUid);
4128 if (qev != null) {
4129 mQueue.recycleEvent(qev);
4130 }
4131 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004132 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 }
4134 }
4135
Michael Chan53071d62009-05-13 17:29:48 -07004136 if (MEASURE_LATENCY) {
4137 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4138 }
4139
Romain Guy06882f82009-06-10 13:36:04 -07004140 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4142 //target wants to ignore fat touch events
4143 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4144 //explicit flag to return without processing event further
4145 boolean returnFlag = false;
4146 if((action == MotionEvent.ACTION_DOWN)) {
4147 mFatTouch = false;
4148 if(cheekPress) {
4149 mFatTouch = true;
4150 returnFlag = true;
4151 }
4152 } else {
4153 if(action == MotionEvent.ACTION_UP) {
4154 if(mFatTouch) {
4155 //earlier even was invalid doesnt matter if current up is cheekpress or not
4156 mFatTouch = false;
4157 returnFlag = true;
4158 } else if(cheekPress) {
4159 //cancel the earlier event
4160 ev.setAction(MotionEvent.ACTION_CANCEL);
4161 action = MotionEvent.ACTION_CANCEL;
4162 }
4163 } else if(action == MotionEvent.ACTION_MOVE) {
4164 if(mFatTouch) {
4165 //two cases here
4166 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004167 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 returnFlag = true;
4169 } else if(cheekPress) {
4170 //valid down followed by invalid moves
4171 //an invalid move have to cancel earlier action
4172 ev.setAction(MotionEvent.ACTION_CANCEL);
4173 action = MotionEvent.ACTION_CANCEL;
4174 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4175 //note that the subsequent invalid moves will not get here
4176 mFatTouch = true;
4177 }
4178 }
4179 } //else if action
4180 if(returnFlag) {
4181 //recycle que, ev
4182 if (qev != null) {
4183 mQueue.recycleEvent(qev);
4184 }
4185 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004186 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 }
4188 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004189
Michael Chan9f028e62009-08-04 17:37:46 -07004190 // Enable this for testing the "right" value
4191 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004192 int max_events_per_sec = 35;
4193 try {
4194 max_events_per_sec = Integer.parseInt(SystemProperties
4195 .get("windowsmgr.max_events_per_sec"));
4196 if (max_events_per_sec < 1) {
4197 max_events_per_sec = 35;
4198 }
4199 } catch (NumberFormatException e) {
4200 }
4201 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4202 }
4203
4204 /*
4205 * Throttle events to minimize CPU usage when there's a flood of events
4206 * e.g. constant contact with the screen
4207 */
4208 if (action == MotionEvent.ACTION_MOVE) {
4209 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4210 long now = SystemClock.uptimeMillis();
4211 if (now < nextEventTime) {
4212 try {
4213 Thread.sleep(nextEventTime - now);
4214 } catch (InterruptedException e) {
4215 }
4216 mLastTouchEventTime = nextEventTime;
4217 } else {
4218 mLastTouchEventTime = now;
4219 }
4220 }
4221
Michael Chan53071d62009-05-13 17:29:48 -07004222 if (MEASURE_LATENCY) {
4223 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4224 }
4225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 synchronized(mWindowMap) {
4227 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4228 mKeyWaiter.bindTargetWindowLocked(target,
4229 KeyWaiter.RETURN_PENDING_POINTER, qev);
4230 ev = null;
4231 } else {
4232 if (action == MotionEvent.ACTION_DOWN) {
4233 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4234 if (out != null) {
4235 MotionEvent oev = MotionEvent.obtain(ev);
4236 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4237 do {
4238 final Rect frame = out.mFrame;
4239 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4240 try {
4241 out.mClient.dispatchPointer(oev, eventTime);
4242 } catch (android.os.RemoteException e) {
4243 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4244 }
4245 oev.offsetLocation((float)frame.left, (float)frame.top);
4246 out = out.mNextOutsideTouch;
4247 } while (out != null);
4248 mKeyWaiter.mOutsideTouchTargets = null;
4249 }
4250 }
4251 final Rect frame = target.mFrame;
4252 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4253 mKeyWaiter.bindTargetWindowLocked(target);
4254 }
4255 }
Romain Guy06882f82009-06-10 13:36:04 -07004256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 // finally offset the event to the target's coordinate system and
4258 // dispatch the event.
4259 try {
4260 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4261 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4262 }
Michael Chan53071d62009-05-13 17:29:48 -07004263
4264 if (MEASURE_LATENCY) {
4265 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4266 }
4267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004268 target.mClient.dispatchPointer(ev, eventTime);
Michael Chan53071d62009-05-13 17:29:48 -07004269
4270 if (MEASURE_LATENCY) {
4271 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4272 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004273 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 } catch (android.os.RemoteException e) {
4275 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4276 mKeyWaiter.mMotionTarget = null;
4277 try {
4278 removeWindow(target.mSession, target.mClient);
4279 } catch (java.util.NoSuchElementException ex) {
4280 // This will happen if the window has already been
4281 // removed.
4282 }
4283 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004284 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 }
Romain Guy06882f82009-06-10 13:36:04 -07004286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 /**
4288 * @return Returns true if event was dispatched, false if it was dropped for any reason
4289 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004290 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 if (DEBUG_INPUT) Log.v(
4292 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004295 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296 if (focusObj == null) {
4297 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4298 if (qev != null) {
4299 mQueue.recycleEvent(qev);
4300 }
4301 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004302 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 }
4304 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4305 if (qev != null) {
4306 mQueue.recycleEvent(qev);
4307 }
4308 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004309 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 }
Romain Guy06882f82009-06-10 13:36:04 -07004311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 if (uid != 0 && uid != focus.mSession.mUid) {
4315 if (mContext.checkPermission(
4316 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4317 != PackageManager.PERMISSION_GRANTED) {
4318 Log.w(TAG, "Permission denied: injecting key event from pid "
4319 + pid + " uid " + uid + " to window " + focus
4320 + " owned by uid " + focus.mSession.mUid);
4321 if (qev != null) {
4322 mQueue.recycleEvent(qev);
4323 }
4324 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004325 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326 }
4327 }
Romain Guy06882f82009-06-10 13:36:04 -07004328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 synchronized(mWindowMap) {
4332 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4333 mKeyWaiter.bindTargetWindowLocked(focus,
4334 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4335 // We don't deliver movement events to the client, we hold
4336 // them and wait for them to call back.
4337 ev = null;
4338 } else {
4339 mKeyWaiter.bindTargetWindowLocked(focus);
4340 }
4341 }
Romain Guy06882f82009-06-10 13:36:04 -07004342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 try {
4344 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004345 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 } catch (android.os.RemoteException e) {
4347 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4348 try {
4349 removeWindow(focus.mSession, focus.mClient);
4350 } catch (java.util.NoSuchElementException ex) {
4351 // This will happen if the window has already been
4352 // removed.
4353 }
4354 }
Romain Guy06882f82009-06-10 13:36:04 -07004355
Dianne Hackborncfaef692009-06-15 14:24:44 -07004356 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 }
Romain Guy06882f82009-06-10 13:36:04 -07004358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 /**
4360 * @return Returns true if event was dispatched, false if it was dropped for any reason
4361 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004362 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4364
4365 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004366 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 if (focusObj == null) {
4368 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004369 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 }
4371 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004372 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 }
Romain Guy06882f82009-06-10 13:36:04 -07004374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 if (DEBUG_INPUT) Log.v(
4378 TAG, "Dispatching to " + focus + ": " + event);
4379
4380 if (uid != 0 && uid != focus.mSession.mUid) {
4381 if (mContext.checkPermission(
4382 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4383 != PackageManager.PERMISSION_GRANTED) {
4384 Log.w(TAG, "Permission denied: injecting key event from pid "
4385 + pid + " uid " + uid + " to window " + focus
4386 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004387 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 }
4389 }
Romain Guy06882f82009-06-10 13:36:04 -07004390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 synchronized(mWindowMap) {
4392 mKeyWaiter.bindTargetWindowLocked(focus);
4393 }
4394
4395 // NOSHIP extra state logging
4396 mKeyWaiter.recordDispatchState(event, focus);
4397 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 try {
4400 if (DEBUG_INPUT || DEBUG_FOCUS) {
4401 Log.v(TAG, "Delivering key " + event.getKeyCode()
4402 + " to " + focus);
4403 }
4404 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004405 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 } catch (android.os.RemoteException e) {
4407 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4408 try {
4409 removeWindow(focus.mSession, focus.mClient);
4410 } catch (java.util.NoSuchElementException ex) {
4411 // This will happen if the window has already been
4412 // removed.
4413 }
4414 }
Romain Guy06882f82009-06-10 13:36:04 -07004415
Dianne Hackborncfaef692009-06-15 14:24:44 -07004416 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 }
Romain Guy06882f82009-06-10 13:36:04 -07004418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 public void pauseKeyDispatching(IBinder _token) {
4420 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4421 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004422 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 }
4424
4425 synchronized (mWindowMap) {
4426 WindowToken token = mTokenMap.get(_token);
4427 if (token != null) {
4428 mKeyWaiter.pauseDispatchingLocked(token);
4429 }
4430 }
4431 }
4432
4433 public void resumeKeyDispatching(IBinder _token) {
4434 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4435 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004436 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437 }
4438
4439 synchronized (mWindowMap) {
4440 WindowToken token = mTokenMap.get(_token);
4441 if (token != null) {
4442 mKeyWaiter.resumeDispatchingLocked(token);
4443 }
4444 }
4445 }
4446
4447 public void setEventDispatching(boolean enabled) {
4448 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4449 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004450 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 }
4452
4453 synchronized (mWindowMap) {
4454 mKeyWaiter.setEventDispatchingLocked(enabled);
4455 }
4456 }
Romain Guy06882f82009-06-10 13:36:04 -07004457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 /**
4459 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004460 *
4461 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 * {@link SystemClock#uptimeMillis()} as the timebase.)
4463 * @param sync If true, wait for the event to be completed before returning to the caller.
4464 * @return Returns true if event was dispatched, false if it was dropped for any reason
4465 */
4466 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4467 long downTime = ev.getDownTime();
4468 long eventTime = ev.getEventTime();
4469
4470 int action = ev.getAction();
4471 int code = ev.getKeyCode();
4472 int repeatCount = ev.getRepeatCount();
4473 int metaState = ev.getMetaState();
4474 int deviceId = ev.getDeviceId();
4475 int scancode = ev.getScanCode();
4476
4477 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4478 if (downTime == 0) downTime = eventTime;
4479
4480 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004481 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004483 final int pid = Binder.getCallingPid();
4484 final int uid = Binder.getCallingUid();
4485 final long ident = Binder.clearCallingIdentity();
4486 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004488 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004490 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004491 switch (result) {
4492 case INJECT_NO_PERMISSION:
4493 throw new SecurityException(
4494 "Injecting to another application requires INJECT_EVENT permission");
4495 case INJECT_SUCCEEDED:
4496 return true;
4497 }
4498 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 }
4500
4501 /**
4502 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004503 *
4504 * @param ev A motion event describing the pointer (touch) action. (As noted in
4505 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 * {@link SystemClock#uptimeMillis()} as the timebase.)
4507 * @param sync If true, wait for the event to be completed before returning to the caller.
4508 * @return Returns true if event was dispatched, false if it was dropped for any reason
4509 */
4510 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004511 final int pid = Binder.getCallingPid();
4512 final int uid = Binder.getCallingUid();
4513 final long ident = Binder.clearCallingIdentity();
4514 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004516 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004518 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004519 switch (result) {
4520 case INJECT_NO_PERMISSION:
4521 throw new SecurityException(
4522 "Injecting to another application requires INJECT_EVENT permission");
4523 case INJECT_SUCCEEDED:
4524 return true;
4525 }
4526 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004527 }
Romain Guy06882f82009-06-10 13:36:04 -07004528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 /**
4530 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004531 *
4532 * @param ev A motion event describing the trackball action. (As noted in
4533 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 * {@link SystemClock#uptimeMillis()} as the timebase.)
4535 * @param sync If true, wait for the event to be completed before returning to the caller.
4536 * @return Returns true if event was dispatched, false if it was dropped for any reason
4537 */
4538 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004539 final int pid = Binder.getCallingPid();
4540 final int uid = Binder.getCallingUid();
4541 final long ident = Binder.clearCallingIdentity();
4542 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004544 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004545 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004546 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004547 switch (result) {
4548 case INJECT_NO_PERMISSION:
4549 throw new SecurityException(
4550 "Injecting to another application requires INJECT_EVENT permission");
4551 case INJECT_SUCCEEDED:
4552 return true;
4553 }
4554 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004555 }
Romain Guy06882f82009-06-10 13:36:04 -07004556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004557 private WindowState getFocusedWindow() {
4558 synchronized (mWindowMap) {
4559 return getFocusedWindowLocked();
4560 }
4561 }
4562
4563 private WindowState getFocusedWindowLocked() {
4564 return mCurrentFocus;
4565 }
Romain Guy06882f82009-06-10 13:36:04 -07004566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 /**
4568 * This class holds the state for dispatching key events. This state
4569 * is protected by the KeyWaiter instance, NOT by the window lock. You
4570 * can be holding the main window lock while acquire the KeyWaiter lock,
4571 * but not the other way around.
4572 */
4573 final class KeyWaiter {
4574 // NOSHIP debugging
4575 public class DispatchState {
4576 private KeyEvent event;
4577 private WindowState focus;
4578 private long time;
4579 private WindowState lastWin;
4580 private IBinder lastBinder;
4581 private boolean finished;
4582 private boolean gotFirstWindow;
4583 private boolean eventDispatching;
4584 private long timeToSwitch;
4585 private boolean wasFrozen;
4586 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004587 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004589 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4590 focus = theFocus;
4591 event = theEvent;
4592 time = System.currentTimeMillis();
4593 // snapshot KeyWaiter state
4594 lastWin = mLastWin;
4595 lastBinder = mLastBinder;
4596 finished = mFinished;
4597 gotFirstWindow = mGotFirstWindow;
4598 eventDispatching = mEventDispatching;
4599 timeToSwitch = mTimeToSwitch;
4600 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004601 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004602 // cache the paused state at ctor time as well
4603 if (theFocus == null || theFocus.mToken == null) {
4604 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4605 focusPaused = false;
4606 } else {
4607 focusPaused = theFocus.mToken.paused;
4608 }
4609 }
Romain Guy06882f82009-06-10 13:36:04 -07004610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 public String toString() {
4612 return "{{" + event + " to " + focus + " @ " + time
4613 + " lw=" + lastWin + " lb=" + lastBinder
4614 + " fin=" + finished + " gfw=" + gotFirstWindow
4615 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004616 + " wf=" + wasFrozen + " fp=" + focusPaused
4617 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004618 }
4619 };
4620 private DispatchState mDispatchState = null;
4621 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4622 mDispatchState = new DispatchState(theEvent, theFocus);
4623 }
4624 // END NOSHIP
4625
4626 public static final int RETURN_NOTHING = 0;
4627 public static final int RETURN_PENDING_POINTER = 1;
4628 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 final Object SKIP_TARGET_TOKEN = new Object();
4631 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004633 private WindowState mLastWin = null;
4634 private IBinder mLastBinder = null;
4635 private boolean mFinished = true;
4636 private boolean mGotFirstWindow = false;
4637 private boolean mEventDispatching = true;
4638 private long mTimeToSwitch = 0;
4639 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 // Target of Motion events
4642 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 // Windows above the target who would like to receive an "outside"
4645 // touch event for any down events outside of them.
4646 WindowState mOutsideTouchTargets;
4647
4648 /**
4649 * Wait for the last event dispatch to complete, then find the next
4650 * target that should receive the given event and wait for that one
4651 * to be ready to receive it.
4652 */
4653 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4654 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004655 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004656 long startTime = SystemClock.uptimeMillis();
4657 long keyDispatchingTimeout = 5 * 1000;
4658 long waitedFor = 0;
4659
4660 while (true) {
4661 // Figure out which window we care about. It is either the
4662 // last window we are waiting to have process the event or,
4663 // if none, then the next window we think the event should go
4664 // to. Note: we retrieve mLastWin outside of the lock, so
4665 // it may change before we lock. Thus we must check it again.
4666 WindowState targetWin = mLastWin;
4667 boolean targetIsNew = targetWin == null;
4668 if (DEBUG_INPUT) Log.v(
4669 TAG, "waitForLastKey: mFinished=" + mFinished +
4670 ", mLastWin=" + mLastWin);
4671 if (targetIsNew) {
4672 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004673 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 if (target == SKIP_TARGET_TOKEN) {
4675 // The user has pressed a special key, and we are
4676 // dropping all pending events before it.
4677 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4678 + " " + nextMotion);
4679 return null;
4680 }
4681 if (target == CONSUMED_EVENT_TOKEN) {
4682 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4683 + " " + nextMotion);
4684 return target;
4685 }
4686 targetWin = (WindowState)target;
4687 }
Romain Guy06882f82009-06-10 13:36:04 -07004688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004689 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004691 // Now: is it okay to send the next event to this window?
4692 synchronized (this) {
4693 // First: did we come here based on the last window not
4694 // being null, but it changed by the time we got here?
4695 // If so, try again.
4696 if (!targetIsNew && mLastWin == null) {
4697 continue;
4698 }
Romain Guy06882f82009-06-10 13:36:04 -07004699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004700 // We never dispatch events if not finished with the
4701 // last one, or the display is frozen.
4702 if (mFinished && !mDisplayFrozen) {
4703 // If event dispatching is disabled, then we
4704 // just consume the events.
4705 if (!mEventDispatching) {
4706 if (DEBUG_INPUT) Log.v(TAG,
4707 "Skipping event; dispatching disabled: "
4708 + nextKey + " " + nextMotion);
4709 return null;
4710 }
4711 if (targetWin != null) {
4712 // If this is a new target, and that target is not
4713 // paused or unresponsive, then all looks good to
4714 // handle the event.
4715 if (targetIsNew && !targetWin.mToken.paused) {
4716 return targetWin;
4717 }
Romain Guy06882f82009-06-10 13:36:04 -07004718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004719 // If we didn't find a target window, and there is no
4720 // focused app window, then just eat the events.
4721 } else if (mFocusedApp == null) {
4722 if (DEBUG_INPUT) Log.v(TAG,
4723 "Skipping event; no focused app: "
4724 + nextKey + " " + nextMotion);
4725 return null;
4726 }
4727 }
Romain Guy06882f82009-06-10 13:36:04 -07004728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 if (DEBUG_INPUT) Log.v(
4730 TAG, "Waiting for last key in " + mLastBinder
4731 + " target=" + targetWin
4732 + " mFinished=" + mFinished
4733 + " mDisplayFrozen=" + mDisplayFrozen
4734 + " targetIsNew=" + targetIsNew
4735 + " paused="
4736 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004737 + " mFocusedApp=" + mFocusedApp
4738 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004740 targetApp = targetWin != null
4741 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004743 long curTimeout = keyDispatchingTimeout;
4744 if (mTimeToSwitch != 0) {
4745 long now = SystemClock.uptimeMillis();
4746 if (mTimeToSwitch <= now) {
4747 // If an app switch key has been pressed, and we have
4748 // waited too long for the current app to finish
4749 // processing keys, then wait no more!
4750 doFinishedKeyLocked(true);
4751 continue;
4752 }
4753 long switchTimeout = mTimeToSwitch - now;
4754 if (curTimeout > switchTimeout) {
4755 curTimeout = switchTimeout;
4756 }
4757 }
Romain Guy06882f82009-06-10 13:36:04 -07004758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759 try {
4760 // after that continue
4761 // processing keys, so we don't get stuck.
4762 if (DEBUG_INPUT) Log.v(
4763 TAG, "Waiting for key dispatch: " + curTimeout);
4764 wait(curTimeout);
4765 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4766 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004767 + startTime + " switchTime=" + mTimeToSwitch
4768 + " target=" + targetWin + " mLW=" + mLastWin
4769 + " mLB=" + mLastBinder + " fin=" + mFinished
4770 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004771 } catch (InterruptedException e) {
4772 }
4773 }
4774
4775 // If we were frozen during configuration change, restart the
4776 // timeout checks from now; otherwise look at whether we timed
4777 // out before awakening.
4778 if (mWasFrozen) {
4779 waitedFor = 0;
4780 mWasFrozen = false;
4781 } else {
4782 waitedFor = SystemClock.uptimeMillis() - startTime;
4783 }
4784
4785 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4786 IApplicationToken at = null;
4787 synchronized (this) {
4788 Log.w(TAG, "Key dispatching timed out sending to " +
4789 (targetWin != null ? targetWin.mAttrs.getTitle()
4790 : "<null>"));
4791 // NOSHIP debugging
4792 Log.w(TAG, "Dispatch state: " + mDispatchState);
4793 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4794 // END NOSHIP
4795 //dump();
4796 if (targetWin != null) {
4797 at = targetWin.getAppToken();
4798 } else if (targetApp != null) {
4799 at = targetApp.appToken;
4800 }
4801 }
4802
4803 boolean abort = true;
4804 if (at != null) {
4805 try {
4806 long timeout = at.getKeyDispatchingTimeout();
4807 if (timeout > waitedFor) {
4808 // we did not wait the proper amount of time for this application.
4809 // set the timeout to be the real timeout and wait again.
4810 keyDispatchingTimeout = timeout - waitedFor;
4811 continue;
4812 } else {
4813 abort = at.keyDispatchingTimedOut();
4814 }
4815 } catch (RemoteException ex) {
4816 }
4817 }
4818
4819 synchronized (this) {
4820 if (abort && (mLastWin == targetWin || targetWin == null)) {
4821 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004822 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 if (DEBUG_INPUT) Log.v(TAG,
4824 "Window " + mLastWin +
4825 " timed out on key input");
4826 if (mLastWin.mToken.paused) {
4827 Log.w(TAG, "Un-pausing dispatching to this window");
4828 mLastWin.mToken.paused = false;
4829 }
4830 }
4831 if (mMotionTarget == targetWin) {
4832 mMotionTarget = null;
4833 }
4834 mLastWin = null;
4835 mLastBinder = null;
4836 if (failIfTimeout || targetWin == null) {
4837 return null;
4838 }
4839 } else {
4840 Log.w(TAG, "Continuing to wait for key to be dispatched");
4841 startTime = SystemClock.uptimeMillis();
4842 }
4843 }
4844 }
4845 }
4846 }
Romain Guy06882f82009-06-10 13:36:04 -07004847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004849 MotionEvent nextMotion, boolean isPointerEvent,
4850 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004851 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 if (nextKey != null) {
4854 // Find the target window for a normal key event.
4855 final int keycode = nextKey.getKeyCode();
4856 final int repeatCount = nextKey.getRepeatCount();
4857 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4858 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004861 if (callingUid == 0 ||
4862 mContext.checkPermission(
4863 android.Manifest.permission.INJECT_EVENTS,
4864 callingPid, callingUid)
4865 == PackageManager.PERMISSION_GRANTED) {
4866 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07004867 nextKey.getMetaState(), down, repeatCount,
4868 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004870 Log.w(TAG, "Event timeout during app switch: dropping "
4871 + nextKey);
4872 return SKIP_TARGET_TOKEN;
4873 }
Romain Guy06882f82009-06-10 13:36:04 -07004874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004875 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 WindowState focus = null;
4878 synchronized(mWindowMap) {
4879 focus = getFocusedWindowLocked();
4880 }
Romain Guy06882f82009-06-10 13:36:04 -07004881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004883
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004884 if (callingUid == 0 ||
4885 (focus != null && callingUid == focus.mSession.mUid) ||
4886 mContext.checkPermission(
4887 android.Manifest.permission.INJECT_EVENTS,
4888 callingPid, callingUid)
4889 == PackageManager.PERMISSION_GRANTED) {
4890 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07004891 keycode, nextKey.getMetaState(), down, repeatCount,
4892 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004893 return CONSUMED_EVENT_TOKEN;
4894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 }
Romain Guy06882f82009-06-10 13:36:04 -07004896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004897 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 } else if (!isPointerEvent) {
4900 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4901 if (!dispatch) {
4902 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4903 + nextMotion);
4904 return SKIP_TARGET_TOKEN;
4905 }
Romain Guy06882f82009-06-10 13:36:04 -07004906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004907 WindowState focus = null;
4908 synchronized(mWindowMap) {
4909 focus = getFocusedWindowLocked();
4910 }
Romain Guy06882f82009-06-10 13:36:04 -07004911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4913 return focus;
4914 }
Romain Guy06882f82009-06-10 13:36:04 -07004915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 if (nextMotion == null) {
4917 return SKIP_TARGET_TOKEN;
4918 }
Romain Guy06882f82009-06-10 13:36:04 -07004919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004920 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4921 KeyEvent.KEYCODE_UNKNOWN);
4922 if (!dispatch) {
4923 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4924 + nextMotion);
4925 return SKIP_TARGET_TOKEN;
4926 }
Romain Guy06882f82009-06-10 13:36:04 -07004927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004928 // Find the target window for a pointer event.
4929 int action = nextMotion.getAction();
4930 final float xf = nextMotion.getX();
4931 final float yf = nextMotion.getY();
4932 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 final boolean screenWasOff = qev != null
4935 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07004936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004937 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07004938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 synchronized(mWindowMap) {
4940 synchronized (this) {
4941 if (action == MotionEvent.ACTION_DOWN) {
4942 if (mMotionTarget != null) {
4943 // this is weird, we got a pen down, but we thought it was
4944 // already down!
4945 // XXX: We should probably send an ACTION_UP to the current
4946 // target.
4947 Log.w(TAG, "Pointer down received while already down in: "
4948 + mMotionTarget);
4949 mMotionTarget = null;
4950 }
Romain Guy06882f82009-06-10 13:36:04 -07004951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952 // ACTION_DOWN is special, because we need to lock next events to
4953 // the window we'll land onto.
4954 final int x = (int)xf;
4955 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07004956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004957 final ArrayList windows = mWindows;
4958 final int N = windows.size();
4959 WindowState topErrWindow = null;
4960 final Rect tmpRect = mTempRect;
4961 for (int i=N-1; i>=0; i--) {
4962 WindowState child = (WindowState)windows.get(i);
4963 //Log.i(TAG, "Checking dispatch to: " + child);
4964 final int flags = child.mAttrs.flags;
4965 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4966 if (topErrWindow == null) {
4967 topErrWindow = child;
4968 }
4969 }
4970 if (!child.isVisibleLw()) {
4971 //Log.i(TAG, "Not visible!");
4972 continue;
4973 }
4974 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4975 //Log.i(TAG, "Not touchable!");
4976 if ((flags & WindowManager.LayoutParams
4977 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4978 child.mNextOutsideTouch = mOutsideTouchTargets;
4979 mOutsideTouchTargets = child;
4980 }
4981 continue;
4982 }
4983 tmpRect.set(child.mFrame);
4984 if (child.mTouchableInsets == ViewTreeObserver
4985 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4986 // The touch is inside of the window if it is
4987 // inside the frame, AND the content part of that
4988 // frame that was given by the application.
4989 tmpRect.left += child.mGivenContentInsets.left;
4990 tmpRect.top += child.mGivenContentInsets.top;
4991 tmpRect.right -= child.mGivenContentInsets.right;
4992 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4993 } else if (child.mTouchableInsets == ViewTreeObserver
4994 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4995 // The touch is inside of the window if it is
4996 // inside the frame, AND the visible part of that
4997 // frame that was given by the application.
4998 tmpRect.left += child.mGivenVisibleInsets.left;
4999 tmpRect.top += child.mGivenVisibleInsets.top;
5000 tmpRect.right -= child.mGivenVisibleInsets.right;
5001 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5002 }
5003 final int touchFlags = flags &
5004 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5005 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5006 if (tmpRect.contains(x, y) || touchFlags == 0) {
5007 //Log.i(TAG, "Using this target!");
5008 if (!screenWasOff || (flags &
5009 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5010 mMotionTarget = child;
5011 } else {
5012 //Log.i(TAG, "Waking, skip!");
5013 mMotionTarget = null;
5014 }
5015 break;
5016 }
Romain Guy06882f82009-06-10 13:36:04 -07005017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 if ((flags & WindowManager.LayoutParams
5019 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5020 child.mNextOutsideTouch = mOutsideTouchTargets;
5021 mOutsideTouchTargets = child;
5022 //Log.i(TAG, "Adding to outside target list: " + child);
5023 }
5024 }
5025
5026 // if there's an error window but it's not accepting
5027 // focus (typically because it is not yet visible) just
5028 // wait for it -- any other focused window may in fact
5029 // be in ANR state.
5030 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5031 mMotionTarget = null;
5032 }
5033 }
Romain Guy06882f82009-06-10 13:36:04 -07005034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 target = mMotionTarget;
5036 }
5037 }
Romain Guy06882f82009-06-10 13:36:04 -07005038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005039 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005041 // Pointer events are a little different -- if there isn't a
5042 // target found for any event, then just drop it.
5043 return target != null ? target : SKIP_TARGET_TOKEN;
5044 }
Romain Guy06882f82009-06-10 13:36:04 -07005045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005046 boolean checkShouldDispatchKey(int keycode) {
5047 synchronized (this) {
5048 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5049 mTimeToSwitch = 0;
5050 return true;
5051 }
5052 if (mTimeToSwitch != 0
5053 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5054 return false;
5055 }
5056 return true;
5057 }
5058 }
Romain Guy06882f82009-06-10 13:36:04 -07005059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 void bindTargetWindowLocked(WindowState win,
5061 int pendingWhat, QueuedEvent pendingMotion) {
5062 synchronized (this) {
5063 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5064 }
5065 }
Romain Guy06882f82009-06-10 13:36:04 -07005066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 void bindTargetWindowLocked(WindowState win) {
5068 synchronized (this) {
5069 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5070 }
5071 }
5072
5073 void bindTargetWindowLockedLocked(WindowState win,
5074 int pendingWhat, QueuedEvent pendingMotion) {
5075 mLastWin = win;
5076 mLastBinder = win.mClient.asBinder();
5077 mFinished = false;
5078 if (pendingMotion != null) {
5079 final Session s = win.mSession;
5080 if (pendingWhat == RETURN_PENDING_POINTER) {
5081 releasePendingPointerLocked(s);
5082 s.mPendingPointerMove = pendingMotion;
5083 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005084 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 "bindTargetToWindow " + s.mPendingPointerMove);
5086 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5087 releasePendingTrackballLocked(s);
5088 s.mPendingTrackballMove = pendingMotion;
5089 s.mPendingTrackballWindow = win;
5090 }
5091 }
5092 }
Romain Guy06882f82009-06-10 13:36:04 -07005093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 void releasePendingPointerLocked(Session s) {
5095 if (DEBUG_INPUT) Log.v(TAG,
5096 "releasePendingPointer " + s.mPendingPointerMove);
5097 if (s.mPendingPointerMove != null) {
5098 mQueue.recycleEvent(s.mPendingPointerMove);
5099 s.mPendingPointerMove = null;
5100 }
5101 }
Romain Guy06882f82009-06-10 13:36:04 -07005102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 void releasePendingTrackballLocked(Session s) {
5104 if (s.mPendingTrackballMove != null) {
5105 mQueue.recycleEvent(s.mPendingTrackballMove);
5106 s.mPendingTrackballMove = null;
5107 }
5108 }
Romain Guy06882f82009-06-10 13:36:04 -07005109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5111 int returnWhat) {
5112 if (DEBUG_INPUT) Log.v(
5113 TAG, "finishedKey: client=" + client + ", force=" + force);
5114
5115 if (client == null) {
5116 return null;
5117 }
5118
5119 synchronized (this) {
5120 if (DEBUG_INPUT) Log.v(
5121 TAG, "finishedKey: client=" + client.asBinder()
5122 + ", force=" + force + ", last=" + mLastBinder
5123 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5124
5125 QueuedEvent qev = null;
5126 WindowState win = null;
5127 if (returnWhat == RETURN_PENDING_POINTER) {
5128 qev = session.mPendingPointerMove;
5129 win = session.mPendingPointerWindow;
5130 session.mPendingPointerMove = null;
5131 session.mPendingPointerWindow = null;
5132 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5133 qev = session.mPendingTrackballMove;
5134 win = session.mPendingTrackballWindow;
5135 session.mPendingTrackballMove = null;
5136 session.mPendingTrackballWindow = null;
5137 }
Romain Guy06882f82009-06-10 13:36:04 -07005138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 if (mLastBinder == client.asBinder()) {
5140 if (DEBUG_INPUT) Log.v(
5141 TAG, "finishedKey: last paused="
5142 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5143 if (mLastWin != null && (!mLastWin.mToken.paused || force
5144 || !mEventDispatching)) {
5145 doFinishedKeyLocked(false);
5146 } else {
5147 // Make sure to wake up anyone currently waiting to
5148 // dispatch a key, so they can re-evaluate their
5149 // current situation.
5150 mFinished = true;
5151 notifyAll();
5152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153 }
Romain Guy06882f82009-06-10 13:36:04 -07005154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 if (qev != null) {
5156 MotionEvent res = (MotionEvent)qev.event;
5157 if (DEBUG_INPUT) Log.v(TAG,
5158 "Returning pending motion: " + res);
5159 mQueue.recycleEvent(qev);
5160 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5161 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5162 }
5163 return res;
5164 }
5165 return null;
5166 }
5167 }
5168
5169 void tickle() {
5170 synchronized (this) {
5171 notifyAll();
5172 }
5173 }
Romain Guy06882f82009-06-10 13:36:04 -07005174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 void handleNewWindowLocked(WindowState newWindow) {
5176 if (!newWindow.canReceiveKeys()) {
5177 return;
5178 }
5179 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005180 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 TAG, "New key dispatch window: win="
5182 + newWindow.mClient.asBinder()
5183 + ", last=" + mLastBinder
5184 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5185 + "), finished=" + mFinished + ", paused="
5186 + newWindow.mToken.paused);
5187
5188 // Displaying a window implicitly causes dispatching to
5189 // be unpaused. (This is to protect against bugs if someone
5190 // pauses dispatching but forgets to resume.)
5191 newWindow.mToken.paused = false;
5192
5193 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194
5195 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5196 if (DEBUG_INPUT) Log.v(TAG,
5197 "New SYSTEM_ERROR window; resetting state");
5198 mLastWin = null;
5199 mLastBinder = null;
5200 mMotionTarget = null;
5201 mFinished = true;
5202 } else if (mLastWin != null) {
5203 // If the new window is above the window we are
5204 // waiting on, then stop waiting and let key dispatching
5205 // start on the new guy.
5206 if (DEBUG_INPUT) Log.v(
5207 TAG, "Last win layer=" + mLastWin.mLayer
5208 + ", new win layer=" + newWindow.mLayer);
5209 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005210 // The new window is above the old; finish pending input to the last
5211 // window and start directing it to the new one.
5212 mLastWin.mToken.paused = false;
5213 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005215 // Either the new window is lower, so there is no need to wake key waiters,
5216 // or we just finished key input to the previous window, which implicitly
5217 // notified the key waiters. In both cases, we don't need to issue the
5218 // notification here.
5219 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 }
5221
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005222 // Now that we've put a new window state in place, make the event waiter
5223 // take notice and retarget its attentions.
5224 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 }
5226 }
5227
5228 void pauseDispatchingLocked(WindowToken token) {
5229 synchronized (this)
5230 {
5231 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5232 token.paused = true;
5233
5234 /*
5235 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5236 mPaused = true;
5237 } else {
5238 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005239 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005241 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005243 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 }
5245 }
5246 */
5247 }
5248 }
5249
5250 void resumeDispatchingLocked(WindowToken token) {
5251 synchronized (this) {
5252 if (token.paused) {
5253 if (DEBUG_INPUT) Log.v(
5254 TAG, "Resuming WindowToken " + token
5255 + ", last=" + mLastBinder
5256 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5257 + "), finished=" + mFinished + ", paused="
5258 + token.paused);
5259 token.paused = false;
5260 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5261 doFinishedKeyLocked(true);
5262 } else {
5263 notifyAll();
5264 }
5265 }
5266 }
5267 }
5268
5269 void setEventDispatchingLocked(boolean enabled) {
5270 synchronized (this) {
5271 mEventDispatching = enabled;
5272 notifyAll();
5273 }
5274 }
Romain Guy06882f82009-06-10 13:36:04 -07005275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 void appSwitchComing() {
5277 synchronized (this) {
5278 // Don't wait for more than .5 seconds for app to finish
5279 // processing the pending events.
5280 long now = SystemClock.uptimeMillis() + 500;
5281 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5282 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5283 mTimeToSwitch = now;
5284 }
5285 notifyAll();
5286 }
5287 }
Romain Guy06882f82009-06-10 13:36:04 -07005288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 private final void doFinishedKeyLocked(boolean doRecycle) {
5290 if (mLastWin != null) {
5291 releasePendingPointerLocked(mLastWin.mSession);
5292 releasePendingTrackballLocked(mLastWin.mSession);
5293 }
Romain Guy06882f82009-06-10 13:36:04 -07005294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 if (mLastWin == null || !mLastWin.mToken.paused
5296 || !mLastWin.isVisibleLw()) {
5297 // If the current window has been paused, we aren't -really-
5298 // finished... so let the waiters still wait.
5299 mLastWin = null;
5300 mLastBinder = null;
5301 }
5302 mFinished = true;
5303 notifyAll();
5304 }
5305 }
5306
5307 private class KeyQ extends KeyInputQueue
5308 implements KeyInputQueue.FilterCallback {
5309 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005312 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5314 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5315 "KEEP_SCREEN_ON_FLAG");
5316 mHoldingScreen.setReferenceCounted(false);
5317 }
5318
5319 @Override
5320 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5321 if (mPolicy.preprocessInputEventTq(event)) {
5322 return true;
5323 }
Romain Guy06882f82009-06-10 13:36:04 -07005324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 switch (event.type) {
5326 case RawInputEvent.EV_KEY: {
5327 // XXX begin hack
5328 if (DEBUG) {
5329 if (event.keycode == KeyEvent.KEYCODE_G) {
5330 if (event.value != 0) {
5331 // G down
5332 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5333 }
5334 return false;
5335 }
5336 if (event.keycode == KeyEvent.KEYCODE_D) {
5337 if (event.value != 0) {
5338 //dump();
5339 }
5340 return false;
5341 }
5342 }
5343 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005345 boolean screenIsOff = !mPowerManager.screenIsOn();
5346 boolean screenIsDim = !mPowerManager.screenIsBright();
5347 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5350 mPowerManager.goToSleep(event.when);
5351 }
5352
5353 if (screenIsOff) {
5354 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5355 }
5356 if (screenIsDim) {
5357 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5358 }
5359 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5360 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005361 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 }
Romain Guy06882f82009-06-10 13:36:04 -07005363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5365 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5366 filterQueue(this);
5367 mKeyWaiter.appSwitchComing();
5368 }
5369 return true;
5370 } else {
5371 return false;
5372 }
5373 }
Romain Guy06882f82009-06-10 13:36:04 -07005374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 case RawInputEvent.EV_REL: {
5376 boolean screenIsOff = !mPowerManager.screenIsOn();
5377 boolean screenIsDim = !mPowerManager.screenIsBright();
5378 if (screenIsOff) {
5379 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5380 device.classes, event)) {
5381 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5382 return false;
5383 }
5384 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5385 }
5386 if (screenIsDim) {
5387 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5388 }
5389 return true;
5390 }
Romain Guy06882f82009-06-10 13:36:04 -07005391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392 case RawInputEvent.EV_ABS: {
5393 boolean screenIsOff = !mPowerManager.screenIsOn();
5394 boolean screenIsDim = !mPowerManager.screenIsBright();
5395 if (screenIsOff) {
5396 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5397 device.classes, event)) {
5398 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5399 return false;
5400 }
5401 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5402 }
5403 if (screenIsDim) {
5404 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5405 }
5406 return true;
5407 }
Romain Guy06882f82009-06-10 13:36:04 -07005408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 default:
5410 return true;
5411 }
5412 }
5413
5414 public int filterEvent(QueuedEvent ev) {
5415 switch (ev.classType) {
5416 case RawInputEvent.CLASS_KEYBOARD:
5417 KeyEvent ke = (KeyEvent)ev.event;
5418 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5419 Log.w(TAG, "Dropping movement key during app switch: "
5420 + ke.getKeyCode() + ", action=" + ke.getAction());
5421 return FILTER_REMOVE;
5422 }
5423 return FILTER_ABORT;
5424 default:
5425 return FILTER_KEEP;
5426 }
5427 }
Romain Guy06882f82009-06-10 13:36:04 -07005428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005429 /**
5430 * Must be called with the main window manager lock held.
5431 */
5432 void setHoldScreenLocked(boolean holding) {
5433 boolean state = mHoldingScreen.isHeld();
5434 if (holding != state) {
5435 if (holding) {
5436 mHoldingScreen.acquire();
5437 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005438 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 mHoldingScreen.release();
5440 }
5441 }
5442 }
Michael Chan53071d62009-05-13 17:29:48 -07005443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005444
5445 public boolean detectSafeMode() {
5446 mSafeMode = mPolicy.detectSafeMode();
5447 return mSafeMode;
5448 }
Romain Guy06882f82009-06-10 13:36:04 -07005449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 public void systemReady() {
5451 mPolicy.systemReady();
5452 }
Romain Guy06882f82009-06-10 13:36:04 -07005453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 private final class InputDispatcherThread extends Thread {
5455 // Time to wait when there is nothing to do: 9999 seconds.
5456 static final int LONG_WAIT=9999*1000;
5457
5458 public InputDispatcherThread() {
5459 super("InputDispatcher");
5460 }
Romain Guy06882f82009-06-10 13:36:04 -07005461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005462 @Override
5463 public void run() {
5464 while (true) {
5465 try {
5466 process();
5467 } catch (Exception e) {
5468 Log.e(TAG, "Exception in input dispatcher", e);
5469 }
5470 }
5471 }
Romain Guy06882f82009-06-10 13:36:04 -07005472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 private void process() {
5474 android.os.Process.setThreadPriority(
5475 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 // The last key event we saw
5478 KeyEvent lastKey = null;
5479
5480 // Last keydown time for auto-repeating keys
5481 long lastKeyTime = SystemClock.uptimeMillis();
5482 long nextKeyTime = lastKeyTime+LONG_WAIT;
5483
Romain Guy06882f82009-06-10 13:36:04 -07005484 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 int keyRepeatCount = 0;
5486
5487 // Need to report that configuration has changed?
5488 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 while (true) {
5491 long curTime = SystemClock.uptimeMillis();
5492
5493 if (DEBUG_INPUT) Log.v(
5494 TAG, "Waiting for next key: now=" + curTime
5495 + ", repeat @ " + nextKeyTime);
5496
5497 // Retrieve next event, waiting only as long as the next
5498 // repeat timeout. If the configuration has changed, then
5499 // don't wait at all -- we'll report the change as soon as
5500 // we have processed all events.
5501 QueuedEvent ev = mQueue.getEvent(
5502 (int)((!configChanged && curTime < nextKeyTime)
5503 ? (nextKeyTime-curTime) : 0));
5504
5505 if (DEBUG_INPUT && ev != null) Log.v(
5506 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5507
Michael Chan53071d62009-05-13 17:29:48 -07005508 if (MEASURE_LATENCY) {
5509 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5510 }
5511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 try {
5513 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005514 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005515 int eventType;
5516 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5517 eventType = eventType((MotionEvent)ev.event);
5518 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5519 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5520 eventType = LocalPowerManager.BUTTON_EVENT;
5521 } else {
5522 eventType = LocalPowerManager.OTHER_EVENT;
5523 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005524 try {
Michael Chan53071d62009-05-13 17:29:48 -07005525 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005526 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005527 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005528 mBatteryStats.noteInputEvent();
5529 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005530 } catch (RemoteException e) {
5531 // Ignore
5532 }
Michael Chane10de972009-05-18 11:24:50 -07005533
5534 if (eventType != TOUCH_EVENT
5535 && eventType != LONG_TOUCH_EVENT
5536 && eventType != CHEEK_EVENT) {
5537 mPowerManager.userActivity(curTime, false,
5538 eventType, false);
5539 } else if (mLastTouchEventType != eventType
5540 || (curTime - mLastUserActivityCallTime)
5541 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5542 mLastUserActivityCallTime = curTime;
5543 mLastTouchEventType = eventType;
5544 mPowerManager.userActivity(curTime, false,
5545 eventType, false);
5546 }
5547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 switch (ev.classType) {
5549 case RawInputEvent.CLASS_KEYBOARD:
5550 KeyEvent ke = (KeyEvent)ev.event;
5551 if (ke.isDown()) {
5552 lastKey = ke;
5553 keyRepeatCount = 0;
5554 lastKeyTime = curTime;
5555 nextKeyTime = lastKeyTime
5556 + KEY_REPEAT_FIRST_DELAY;
5557 if (DEBUG_INPUT) Log.v(
5558 TAG, "Received key down: first repeat @ "
5559 + nextKeyTime);
5560 } else {
5561 lastKey = null;
5562 // Arbitrary long timeout.
5563 lastKeyTime = curTime;
5564 nextKeyTime = curTime + LONG_WAIT;
5565 if (DEBUG_INPUT) Log.v(
5566 TAG, "Received key up: ignore repeat @ "
5567 + nextKeyTime);
5568 }
5569 dispatchKey((KeyEvent)ev.event, 0, 0);
5570 mQueue.recycleEvent(ev);
5571 break;
5572 case RawInputEvent.CLASS_TOUCHSCREEN:
5573 //Log.i(TAG, "Read next event " + ev);
5574 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5575 break;
5576 case RawInputEvent.CLASS_TRACKBALL:
5577 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5578 break;
5579 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5580 configChanged = true;
5581 break;
5582 default:
5583 mQueue.recycleEvent(ev);
5584 break;
5585 }
Romain Guy06882f82009-06-10 13:36:04 -07005586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005587 } else if (configChanged) {
5588 configChanged = false;
5589 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 } else if (lastKey != null) {
5592 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 // Timeout occurred while key was down. If it is at or
5595 // past the key repeat time, dispatch the repeat.
5596 if (DEBUG_INPUT) Log.v(
5597 TAG, "Key timeout: repeat=" + nextKeyTime
5598 + ", now=" + curTime);
5599 if (curTime < nextKeyTime) {
5600 continue;
5601 }
Romain Guy06882f82009-06-10 13:36:04 -07005602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 lastKeyTime = nextKeyTime;
5604 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5605 keyRepeatCount++;
5606 if (DEBUG_INPUT) Log.v(
5607 TAG, "Key repeat: count=" + keyRepeatCount
5608 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005609 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005611 } else {
5612 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005614 lastKeyTime = curTime;
5615 nextKeyTime = curTime + LONG_WAIT;
5616 }
Romain Guy06882f82009-06-10 13:36:04 -07005617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 } catch (Exception e) {
5619 Log.e(TAG,
5620 "Input thread received uncaught exception: " + e, e);
5621 }
5622 }
5623 }
5624 }
5625
5626 // -------------------------------------------------------------
5627 // Client Session State
5628 // -------------------------------------------------------------
5629
5630 private final class Session extends IWindowSession.Stub
5631 implements IBinder.DeathRecipient {
5632 final IInputMethodClient mClient;
5633 final IInputContext mInputContext;
5634 final int mUid;
5635 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005636 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005637 SurfaceSession mSurfaceSession;
5638 int mNumWindow = 0;
5639 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005641 /**
5642 * Current pointer move event being dispatched to client window... must
5643 * hold key lock to access.
5644 */
5645 QueuedEvent mPendingPointerMove;
5646 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 /**
5649 * Current trackball move event being dispatched to client window... must
5650 * hold key lock to access.
5651 */
5652 QueuedEvent mPendingTrackballMove;
5653 WindowState mPendingTrackballWindow;
5654
5655 public Session(IInputMethodClient client, IInputContext inputContext) {
5656 mClient = client;
5657 mInputContext = inputContext;
5658 mUid = Binder.getCallingUid();
5659 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005660 StringBuilder sb = new StringBuilder();
5661 sb.append("Session{");
5662 sb.append(Integer.toHexString(System.identityHashCode(this)));
5663 sb.append(" uid ");
5664 sb.append(mUid);
5665 sb.append("}");
5666 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668 synchronized (mWindowMap) {
5669 if (mInputMethodManager == null && mHaveInputMethods) {
5670 IBinder b = ServiceManager.getService(
5671 Context.INPUT_METHOD_SERVICE);
5672 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5673 }
5674 }
5675 long ident = Binder.clearCallingIdentity();
5676 try {
5677 // Note: it is safe to call in to the input method manager
5678 // here because we are not holding our lock.
5679 if (mInputMethodManager != null) {
5680 mInputMethodManager.addClient(client, inputContext,
5681 mUid, mPid);
5682 } else {
5683 client.setUsingInputMethod(false);
5684 }
5685 client.asBinder().linkToDeath(this, 0);
5686 } catch (RemoteException e) {
5687 // The caller has died, so we can just forget about this.
5688 try {
5689 if (mInputMethodManager != null) {
5690 mInputMethodManager.removeClient(client);
5691 }
5692 } catch (RemoteException ee) {
5693 }
5694 } finally {
5695 Binder.restoreCallingIdentity(ident);
5696 }
5697 }
Romain Guy06882f82009-06-10 13:36:04 -07005698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 @Override
5700 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5701 throws RemoteException {
5702 try {
5703 return super.onTransact(code, data, reply, flags);
5704 } catch (RuntimeException e) {
5705 // Log all 'real' exceptions thrown to the caller
5706 if (!(e instanceof SecurityException)) {
5707 Log.e(TAG, "Window Session Crash", e);
5708 }
5709 throw e;
5710 }
5711 }
5712
5713 public void binderDied() {
5714 // Note: it is safe to call in to the input method manager
5715 // here because we are not holding our lock.
5716 try {
5717 if (mInputMethodManager != null) {
5718 mInputMethodManager.removeClient(mClient);
5719 }
5720 } catch (RemoteException e) {
5721 }
5722 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005723 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 mClientDead = true;
5725 killSessionLocked();
5726 }
5727 }
5728
5729 public int add(IWindow window, WindowManager.LayoutParams attrs,
5730 int viewVisibility, Rect outContentInsets) {
5731 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5732 }
Romain Guy06882f82009-06-10 13:36:04 -07005733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 public void remove(IWindow window) {
5735 removeWindow(this, window);
5736 }
Romain Guy06882f82009-06-10 13:36:04 -07005737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5739 int requestedWidth, int requestedHeight, int viewFlags,
5740 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5741 Rect outVisibleInsets, Surface outSurface) {
5742 return relayoutWindow(this, window, attrs,
5743 requestedWidth, requestedHeight, viewFlags, insetsPending,
5744 outFrame, outContentInsets, outVisibleInsets, outSurface);
5745 }
Romain Guy06882f82009-06-10 13:36:04 -07005746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 public void setTransparentRegion(IWindow window, Region region) {
5748 setTransparentRegionWindow(this, window, region);
5749 }
Romain Guy06882f82009-06-10 13:36:04 -07005750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 public void setInsets(IWindow window, int touchableInsets,
5752 Rect contentInsets, Rect visibleInsets) {
5753 setInsetsWindow(this, window, touchableInsets, contentInsets,
5754 visibleInsets);
5755 }
Romain Guy06882f82009-06-10 13:36:04 -07005756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5758 getWindowDisplayFrame(this, window, outDisplayFrame);
5759 }
Romain Guy06882f82009-06-10 13:36:04 -07005760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 public void finishDrawing(IWindow window) {
5762 if (localLOGV) Log.v(
5763 TAG, "IWindow finishDrawing called for " + window);
5764 finishDrawingWindow(this, window);
5765 }
5766
5767 public void finishKey(IWindow window) {
5768 if (localLOGV) Log.v(
5769 TAG, "IWindow finishKey called for " + window);
5770 mKeyWaiter.finishedKey(this, window, false,
5771 KeyWaiter.RETURN_NOTHING);
5772 }
5773
5774 public MotionEvent getPendingPointerMove(IWindow window) {
5775 if (localLOGV) Log.v(
5776 TAG, "IWindow getPendingMotionEvent called for " + window);
5777 return mKeyWaiter.finishedKey(this, window, false,
5778 KeyWaiter.RETURN_PENDING_POINTER);
5779 }
Romain Guy06882f82009-06-10 13:36:04 -07005780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 public MotionEvent getPendingTrackballMove(IWindow window) {
5782 if (localLOGV) Log.v(
5783 TAG, "IWindow getPendingMotionEvent called for " + window);
5784 return mKeyWaiter.finishedKey(this, window, false,
5785 KeyWaiter.RETURN_PENDING_TRACKBALL);
5786 }
5787
5788 public void setInTouchMode(boolean mode) {
5789 synchronized(mWindowMap) {
5790 mInTouchMode = mode;
5791 }
5792 }
5793
5794 public boolean getInTouchMode() {
5795 synchronized(mWindowMap) {
5796 return mInTouchMode;
5797 }
5798 }
5799
5800 public boolean performHapticFeedback(IWindow window, int effectId,
5801 boolean always) {
5802 synchronized(mWindowMap) {
5803 long ident = Binder.clearCallingIdentity();
5804 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005805 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 windowForClientLocked(this, window), effectId, always);
5807 } finally {
5808 Binder.restoreCallingIdentity(ident);
5809 }
5810 }
5811 }
Romain Guy06882f82009-06-10 13:36:04 -07005812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005813 void windowAddedLocked() {
5814 if (mSurfaceSession == null) {
5815 if (localLOGV) Log.v(
5816 TAG, "First window added to " + this + ", creating SurfaceSession");
5817 mSurfaceSession = new SurfaceSession();
5818 mSessions.add(this);
5819 }
5820 mNumWindow++;
5821 }
5822
5823 void windowRemovedLocked() {
5824 mNumWindow--;
5825 killSessionLocked();
5826 }
Romain Guy06882f82009-06-10 13:36:04 -07005827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 void killSessionLocked() {
5829 if (mNumWindow <= 0 && mClientDead) {
5830 mSessions.remove(this);
5831 if (mSurfaceSession != null) {
5832 if (localLOGV) Log.v(
5833 TAG, "Last window removed from " + this
5834 + ", destroying " + mSurfaceSession);
5835 try {
5836 mSurfaceSession.kill();
5837 } catch (Exception e) {
5838 Log.w(TAG, "Exception thrown when killing surface session "
5839 + mSurfaceSession + " in session " + this
5840 + ": " + e.toString());
5841 }
5842 mSurfaceSession = null;
5843 }
5844 }
5845 }
Romain Guy06882f82009-06-10 13:36:04 -07005846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005848 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5849 pw.print(" mClientDead="); pw.print(mClientDead);
5850 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5851 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5852 pw.print(prefix);
5853 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5854 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5855 }
5856 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5857 pw.print(prefix);
5858 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5859 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 }
5862
5863 @Override
5864 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005865 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 }
5867 }
5868
5869 // -------------------------------------------------------------
5870 // Client Window State
5871 // -------------------------------------------------------------
5872
5873 private final class WindowState implements WindowManagerPolicy.WindowState {
5874 final Session mSession;
5875 final IWindow mClient;
5876 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005877 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005878 AppWindowToken mAppToken;
5879 AppWindowToken mTargetAppToken;
5880 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5881 final DeathRecipient mDeathRecipient;
5882 final WindowState mAttachedWindow;
5883 final ArrayList mChildWindows = new ArrayList();
5884 final int mBaseLayer;
5885 final int mSubLayer;
5886 final boolean mLayoutAttached;
5887 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07005888 final boolean mIsWallpaper;
5889 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 int mViewVisibility;
5891 boolean mPolicyVisibility = true;
5892 boolean mPolicyVisibilityAfterAnim = true;
5893 boolean mAppFreezing;
5894 Surface mSurface;
5895 boolean mAttachedHidden; // is our parent window hidden?
5896 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07005897 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 int mRequestedWidth;
5899 int mRequestedHeight;
5900 int mLastRequestedWidth;
5901 int mLastRequestedHeight;
5902 int mReqXPos;
5903 int mReqYPos;
5904 int mLayer;
5905 int mAnimLayer;
5906 int mLastLayer;
5907 boolean mHaveFrame;
5908
5909 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 // Actual frame shown on-screen (may be modified by animation)
5912 final Rect mShownFrame = new Rect();
5913 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 /**
5916 * Insets that determine the actually visible area
5917 */
5918 final Rect mVisibleInsets = new Rect();
5919 final Rect mLastVisibleInsets = new Rect();
5920 boolean mVisibleInsetsChanged;
5921
5922 /**
5923 * Insets that are covered by system windows
5924 */
5925 final Rect mContentInsets = new Rect();
5926 final Rect mLastContentInsets = new Rect();
5927 boolean mContentInsetsChanged;
5928
5929 /**
5930 * Set to true if we are waiting for this window to receive its
5931 * given internal insets before laying out other windows based on it.
5932 */
5933 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07005934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 /**
5936 * These are the content insets that were given during layout for
5937 * this window, to be applied to windows behind it.
5938 */
5939 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941 /**
5942 * These are the visible insets that were given during layout for
5943 * this window, to be applied to windows behind it.
5944 */
5945 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005947 /**
5948 * Flag indicating whether the touchable region should be adjusted by
5949 * the visible insets; if false the area outside the visible insets is
5950 * NOT touchable, so we must use those to adjust the frame during hit
5951 * tests.
5952 */
5953 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07005954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 // Current transformation being applied.
5956 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5957 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5958 float mHScale=1, mVScale=1;
5959 float mLastHScale=1, mLastVScale=1;
5960 final Matrix mTmpMatrix = new Matrix();
5961
5962 // "Real" frame that the application sees.
5963 final Rect mFrame = new Rect();
5964 final Rect mLastFrame = new Rect();
5965
5966 final Rect mContainingFrame = new Rect();
5967 final Rect mDisplayFrame = new Rect();
5968 final Rect mContentFrame = new Rect();
5969 final Rect mVisibleFrame = new Rect();
5970
5971 float mShownAlpha = 1;
5972 float mAlpha = 1;
5973 float mLastAlpha = 1;
5974
5975 // Set to true if, when the window gets displayed, it should perform
5976 // an enter animation.
5977 boolean mEnterAnimationPending;
5978
5979 // Currently running animation.
5980 boolean mAnimating;
5981 boolean mLocalAnimating;
5982 Animation mAnimation;
5983 boolean mAnimationIsEntrance;
5984 boolean mHasTransformation;
5985 boolean mHasLocalTransformation;
5986 final Transformation mTransformation = new Transformation();
5987
5988 // This is set after IWindowSession.relayout() has been called at
5989 // least once for the window. It allows us to detect the situation
5990 // where we don't yet have a surface, but should have one soon, so
5991 // we can give the window focus before waiting for the relayout.
5992 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07005993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 // This is set after the Surface has been created but before the
5995 // window has been drawn. During this time the surface is hidden.
5996 boolean mDrawPending;
5997
5998 // This is set after the window has finished drawing for the first
5999 // time but before its surface is shown. The surface will be
6000 // displayed when the next layout is run.
6001 boolean mCommitDrawPending;
6002
6003 // This is set during the time after the window's drawing has been
6004 // committed, and before its surface is actually shown. It is used
6005 // to delay showing the surface until all windows in a token are ready
6006 // to be shown.
6007 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 // Set when the window has been shown in the screen the first time.
6010 boolean mHasDrawn;
6011
6012 // Currently running an exit animation?
6013 boolean mExiting;
6014
6015 // Currently on the mDestroySurface list?
6016 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006018 // Completely remove from window manager after exit animation?
6019 boolean mRemoveOnExit;
6020
6021 // Set when the orientation is changing and this window has not yet
6022 // been updated for the new orientation.
6023 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006025 // Is this window now (or just being) removed?
6026 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 WindowState(Session s, IWindow c, WindowToken token,
6029 WindowState attachedWindow, WindowManager.LayoutParams a,
6030 int viewVisibility) {
6031 mSession = s;
6032 mClient = c;
6033 mToken = token;
6034 mAttrs.copyFrom(a);
6035 mViewVisibility = viewVisibility;
6036 DeathRecipient deathRecipient = new DeathRecipient();
6037 mAlpha = a.alpha;
6038 if (localLOGV) Log.v(
6039 TAG, "Window " + this + " client=" + c.asBinder()
6040 + " token=" + token + " (" + mAttrs.token + ")");
6041 try {
6042 c.asBinder().linkToDeath(deathRecipient, 0);
6043 } catch (RemoteException e) {
6044 mDeathRecipient = null;
6045 mAttachedWindow = null;
6046 mLayoutAttached = false;
6047 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006048 mIsWallpaper = false;
6049 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 mBaseLayer = 0;
6051 mSubLayer = 0;
6052 return;
6053 }
6054 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6057 mAttrs.type <= LAST_SUB_WINDOW)) {
6058 // The multiplier here is to reserve space for multiple
6059 // windows in the same type layer.
6060 mBaseLayer = mPolicy.windowTypeToLayerLw(
6061 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6062 + TYPE_LAYER_OFFSET;
6063 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6064 mAttachedWindow = attachedWindow;
6065 mAttachedWindow.mChildWindows.add(this);
6066 mLayoutAttached = mAttrs.type !=
6067 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6068 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6069 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006070 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6071 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 } else {
6073 // The multiplier here is to reserve space for multiple
6074 // windows in the same type layer.
6075 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6076 * TYPE_LAYER_MULTIPLIER
6077 + TYPE_LAYER_OFFSET;
6078 mSubLayer = 0;
6079 mAttachedWindow = null;
6080 mLayoutAttached = false;
6081 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6082 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006083 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6084 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006085 }
6086
6087 WindowState appWin = this;
6088 while (appWin.mAttachedWindow != null) {
6089 appWin = mAttachedWindow;
6090 }
6091 WindowToken appToken = appWin.mToken;
6092 while (appToken.appWindowToken == null) {
6093 WindowToken parent = mTokenMap.get(appToken.token);
6094 if (parent == null || appToken == parent) {
6095 break;
6096 }
6097 appToken = parent;
6098 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006099 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 mAppToken = appToken.appWindowToken;
6101
6102 mSurface = null;
6103 mRequestedWidth = 0;
6104 mRequestedHeight = 0;
6105 mLastRequestedWidth = 0;
6106 mLastRequestedHeight = 0;
6107 mReqXPos = 0;
6108 mReqYPos = 0;
6109 mLayer = 0;
6110 mAnimLayer = 0;
6111 mLastLayer = 0;
6112 }
6113
6114 void attach() {
6115 if (localLOGV) Log.v(
6116 TAG, "Attaching " + this + " token=" + mToken
6117 + ", list=" + mToken.windows);
6118 mSession.windowAddedLocked();
6119 }
6120
6121 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6122 mHaveFrame = true;
6123
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006124 final Rect container = mContainingFrame;
6125 container.set(pf);
6126
6127 final Rect display = mDisplayFrame;
6128 display.set(df);
6129
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006130 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006131 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006132 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6133 display.intersect(mCompatibleScreenFrame);
6134 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006135 }
6136
6137 final int pw = container.right - container.left;
6138 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139
6140 int w,h;
6141 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6142 w = mAttrs.width < 0 ? pw : mAttrs.width;
6143 h = mAttrs.height< 0 ? ph : mAttrs.height;
6144 } else {
6145 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6146 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6147 }
Romain Guy06882f82009-06-10 13:36:04 -07006148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 final Rect content = mContentFrame;
6150 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 final Rect visible = mVisibleFrame;
6153 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07006156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6158 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6159
6160 Gravity.apply(mAttrs.gravity, w, h, container,
6161 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6162 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6163
6164 //System.out.println("Out: " + mFrame);
6165
6166 // Now make sure the window fits in the overall display.
6167 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 // Make sure the content and visible frames are inside of the
6170 // final window frame.
6171 if (content.left < frame.left) content.left = frame.left;
6172 if (content.top < frame.top) content.top = frame.top;
6173 if (content.right > frame.right) content.right = frame.right;
6174 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6175 if (visible.left < frame.left) visible.left = frame.left;
6176 if (visible.top < frame.top) visible.top = frame.top;
6177 if (visible.right > frame.right) visible.right = frame.right;
6178 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 final Rect contentInsets = mContentInsets;
6181 contentInsets.left = content.left-frame.left;
6182 contentInsets.top = content.top-frame.top;
6183 contentInsets.right = frame.right-content.right;
6184 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006186 final Rect visibleInsets = mVisibleInsets;
6187 visibleInsets.left = visible.left-frame.left;
6188 visibleInsets.top = visible.top-frame.top;
6189 visibleInsets.right = frame.right-visible.right;
6190 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006192 if (localLOGV) {
6193 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6194 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6195 Log.v(TAG, "Resolving (mRequestedWidth="
6196 + mRequestedWidth + ", mRequestedheight="
6197 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6198 + "): frame=" + mFrame.toShortString()
6199 + " ci=" + contentInsets.toShortString()
6200 + " vi=" + visibleInsets.toShortString());
6201 //}
6202 }
6203 }
Romain Guy06882f82009-06-10 13:36:04 -07006204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 public Rect getFrameLw() {
6206 return mFrame;
6207 }
6208
6209 public Rect getShownFrameLw() {
6210 return mShownFrame;
6211 }
6212
6213 public Rect getDisplayFrameLw() {
6214 return mDisplayFrame;
6215 }
6216
6217 public Rect getContentFrameLw() {
6218 return mContentFrame;
6219 }
6220
6221 public Rect getVisibleFrameLw() {
6222 return mVisibleFrame;
6223 }
6224
6225 public boolean getGivenInsetsPendingLw() {
6226 return mGivenInsetsPending;
6227 }
6228
6229 public Rect getGivenContentInsetsLw() {
6230 return mGivenContentInsets;
6231 }
Romain Guy06882f82009-06-10 13:36:04 -07006232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 public Rect getGivenVisibleInsetsLw() {
6234 return mGivenVisibleInsets;
6235 }
Romain Guy06882f82009-06-10 13:36:04 -07006236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 public WindowManager.LayoutParams getAttrs() {
6238 return mAttrs;
6239 }
6240
6241 public int getSurfaceLayer() {
6242 return mLayer;
6243 }
Romain Guy06882f82009-06-10 13:36:04 -07006244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006245 public IApplicationToken getAppToken() {
6246 return mAppToken != null ? mAppToken.appToken : null;
6247 }
6248
6249 public boolean hasAppShownWindows() {
6250 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6251 }
6252
6253 public boolean hasAppStartingIcon() {
6254 return mAppToken != null ? (mAppToken.startingData != null) : false;
6255 }
6256
6257 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6258 return mAppToken != null ? mAppToken.startingWindow : null;
6259 }
6260
6261 public void setAnimation(Animation anim) {
6262 if (localLOGV) Log.v(
6263 TAG, "Setting animation in " + this + ": " + anim);
6264 mAnimating = false;
6265 mLocalAnimating = false;
6266 mAnimation = anim;
6267 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6268 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6269 }
6270
6271 public void clearAnimation() {
6272 if (mAnimation != null) {
6273 mAnimating = true;
6274 mLocalAnimating = false;
6275 mAnimation = null;
6276 }
6277 }
Romain Guy06882f82009-06-10 13:36:04 -07006278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 Surface createSurfaceLocked() {
6280 if (mSurface == null) {
6281 mDrawPending = true;
6282 mCommitDrawPending = false;
6283 mReadyToShow = false;
6284 if (mAppToken != null) {
6285 mAppToken.allDrawn = false;
6286 }
6287
6288 int flags = 0;
6289 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
6290 flags |= Surface.HARDWARE;
6291 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6292 flags |= Surface.GPU;
6293 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6294 flags |= Surface.PUSH_BUFFERS;
6295 }
6296
6297 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6298 flags |= Surface.SECURE;
6299 }
6300 if (DEBUG_VISIBILITY) Log.v(
6301 TAG, "Creating surface in session "
6302 + mSession.mSurfaceSession + " window " + this
6303 + " w=" + mFrame.width()
6304 + " h=" + mFrame.height() + " format="
6305 + mAttrs.format + " flags=" + flags);
6306
6307 int w = mFrame.width();
6308 int h = mFrame.height();
6309 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6310 // for a scaled surface, we always want the requested
6311 // size.
6312 w = mRequestedWidth;
6313 h = mRequestedHeight;
6314 }
6315
6316 try {
6317 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006318 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 0, w, h, mAttrs.format, flags);
6320 } catch (Surface.OutOfResourcesException e) {
6321 Log.w(TAG, "OutOfResourcesException creating surface");
6322 reclaimSomeSurfaceMemoryLocked(this, "create");
6323 return null;
6324 } catch (Exception e) {
6325 Log.e(TAG, "Exception creating surface", e);
6326 return null;
6327 }
Romain Guy06882f82009-06-10 13:36:04 -07006328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 if (localLOGV) Log.v(
6330 TAG, "Got surface: " + mSurface
6331 + ", set left=" + mFrame.left + " top=" + mFrame.top
6332 + ", animLayer=" + mAnimLayer);
6333 if (SHOW_TRANSACTIONS) {
6334 Log.i(TAG, ">>> OPEN TRANSACTION");
6335 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6336 + mAttrs.getTitle() + ") pos=(" +
6337 mFrame.left + "," + mFrame.top + ") (" +
6338 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6339 mAnimLayer + " HIDE");
6340 }
6341 Surface.openTransaction();
6342 try {
6343 try {
6344 mSurface.setPosition(mFrame.left, mFrame.top);
6345 mSurface.setLayer(mAnimLayer);
6346 mSurface.hide();
6347 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6348 mSurface.setFlags(Surface.SURFACE_DITHER,
6349 Surface.SURFACE_DITHER);
6350 }
6351 } catch (RuntimeException e) {
6352 Log.w(TAG, "Error creating surface in " + w, e);
6353 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6354 }
6355 mLastHidden = true;
6356 } finally {
6357 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6358 Surface.closeTransaction();
6359 }
6360 if (localLOGV) Log.v(
6361 TAG, "Created surface " + this);
6362 }
6363 return mSurface;
6364 }
Romain Guy06882f82009-06-10 13:36:04 -07006365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 void destroySurfaceLocked() {
6367 // Window is no longer on-screen, so can no longer receive
6368 // key events... if we were waiting for it to finish
6369 // handling a key event, the wait is over!
6370 mKeyWaiter.finishedKey(mSession, mClient, true,
6371 KeyWaiter.RETURN_NOTHING);
6372 mKeyWaiter.releasePendingPointerLocked(mSession);
6373 mKeyWaiter.releasePendingTrackballLocked(mSession);
6374
6375 if (mAppToken != null && this == mAppToken.startingWindow) {
6376 mAppToken.startingDisplayed = false;
6377 }
Romain Guy06882f82009-06-10 13:36:04 -07006378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006379 if (localLOGV) Log.v(
6380 TAG, "Window " + this
6381 + " destroying surface " + mSurface + ", session " + mSession);
6382 if (mSurface != null) {
6383 try {
6384 if (SHOW_TRANSACTIONS) {
6385 RuntimeException ex = new RuntimeException();
6386 ex.fillInStackTrace();
6387 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6388 + mAttrs.getTitle() + ")", ex);
6389 }
6390 mSurface.clear();
6391 } catch (RuntimeException e) {
6392 Log.w(TAG, "Exception thrown when destroying Window " + this
6393 + " surface " + mSurface + " session " + mSession
6394 + ": " + e.toString());
6395 }
6396 mSurface = null;
6397 mDrawPending = false;
6398 mCommitDrawPending = false;
6399 mReadyToShow = false;
6400
6401 int i = mChildWindows.size();
6402 while (i > 0) {
6403 i--;
6404 WindowState c = (WindowState)mChildWindows.get(i);
6405 c.mAttachedHidden = true;
6406 }
6407 }
6408 }
6409
6410 boolean finishDrawingLocked() {
6411 if (mDrawPending) {
6412 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6413 TAG, "finishDrawingLocked: " + mSurface);
6414 mCommitDrawPending = true;
6415 mDrawPending = false;
6416 return true;
6417 }
6418 return false;
6419 }
6420
6421 // This must be called while inside a transaction.
6422 void commitFinishDrawingLocked(long currentTime) {
6423 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6424 if (!mCommitDrawPending) {
6425 return;
6426 }
6427 mCommitDrawPending = false;
6428 mReadyToShow = true;
6429 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6430 final AppWindowToken atoken = mAppToken;
6431 if (atoken == null || atoken.allDrawn || starting) {
6432 performShowLocked();
6433 }
6434 }
6435
6436 // This must be called while inside a transaction.
6437 boolean performShowLocked() {
6438 if (DEBUG_VISIBILITY) {
6439 RuntimeException e = new RuntimeException();
6440 e.fillInStackTrace();
6441 Log.v(TAG, "performShow on " + this
6442 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6443 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6444 }
6445 if (mReadyToShow && isReadyForDisplay()) {
6446 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6447 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6448 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6449 + " during animation: policyVis=" + mPolicyVisibility
6450 + " attHidden=" + mAttachedHidden
6451 + " tok.hiddenRequested="
6452 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6453 + " tok.idden="
6454 + (mAppToken != null ? mAppToken.hidden : false)
6455 + " animating=" + mAnimating
6456 + " tok animating="
6457 + (mAppToken != null ? mAppToken.animating : false));
6458 if (!showSurfaceRobustlyLocked(this)) {
6459 return false;
6460 }
6461 mLastAlpha = -1;
6462 mHasDrawn = true;
6463 mLastHidden = false;
6464 mReadyToShow = false;
6465 enableScreenIfNeededLocked();
6466
6467 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006469 int i = mChildWindows.size();
6470 while (i > 0) {
6471 i--;
6472 WindowState c = (WindowState)mChildWindows.get(i);
6473 if (c.mSurface != null && c.mAttachedHidden) {
6474 c.mAttachedHidden = false;
6475 c.performShowLocked();
6476 }
6477 }
Romain Guy06882f82009-06-10 13:36:04 -07006478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 if (mAttrs.type != TYPE_APPLICATION_STARTING
6480 && mAppToken != null) {
6481 mAppToken.firstWindowDrawn = true;
6482 if (mAnimation == null && mAppToken.startingData != null) {
6483 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6484 + mToken
6485 + ": first real window is shown, no animation");
6486 mFinishedStarting.add(mAppToken);
6487 mH.sendEmptyMessage(H.FINISHED_STARTING);
6488 }
6489 mAppToken.updateReportedVisibilityLocked();
6490 }
6491 }
6492 return true;
6493 }
Romain Guy06882f82009-06-10 13:36:04 -07006494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 // This must be called while inside a transaction. Returns true if
6496 // there is more animation to run.
6497 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6498 if (!mDisplayFrozen) {
6499 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6502 mHasTransformation = true;
6503 mHasLocalTransformation = true;
6504 if (!mLocalAnimating) {
6505 if (DEBUG_ANIM) Log.v(
6506 TAG, "Starting animation in " + this +
6507 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6508 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6509 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6510 mAnimation.setStartTime(currentTime);
6511 mLocalAnimating = true;
6512 mAnimating = true;
6513 }
6514 mTransformation.clear();
6515 final boolean more = mAnimation.getTransformation(
6516 currentTime, mTransformation);
6517 if (DEBUG_ANIM) Log.v(
6518 TAG, "Stepped animation in " + this +
6519 ": more=" + more + ", xform=" + mTransformation);
6520 if (more) {
6521 // we're not done!
6522 return true;
6523 }
6524 if (DEBUG_ANIM) Log.v(
6525 TAG, "Finished animation in " + this +
6526 " @ " + currentTime);
6527 mAnimation = null;
6528 //WindowManagerService.this.dump();
6529 }
6530 mHasLocalTransformation = false;
6531 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6532 && mAppToken.hasTransformation) {
6533 // When our app token is animating, we kind-of pretend like
6534 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6535 // part of this check means that we will only do this if
6536 // our window is not currently exiting, or it is not
6537 // locally animating itself. The idea being that one that
6538 // is exiting and doing a local animation should be removed
6539 // once that animation is done.
6540 mAnimating = true;
6541 mHasTransformation = true;
6542 mTransformation.clear();
6543 return false;
6544 } else if (mHasTransformation) {
6545 // Little trick to get through the path below to act like
6546 // we have finished an animation.
6547 mAnimating = true;
6548 } else if (isAnimating()) {
6549 mAnimating = true;
6550 }
6551 } else if (mAnimation != null) {
6552 // If the display is frozen, and there is a pending animation,
6553 // clear it and make sure we run the cleanup code.
6554 mAnimating = true;
6555 mLocalAnimating = true;
6556 mAnimation = null;
6557 }
Romain Guy06882f82009-06-10 13:36:04 -07006558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 if (!mAnimating && !mLocalAnimating) {
6560 return false;
6561 }
6562
6563 if (DEBUG_ANIM) Log.v(
6564 TAG, "Animation done in " + this + ": exiting=" + mExiting
6565 + ", reportedVisible="
6566 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006568 mAnimating = false;
6569 mLocalAnimating = false;
6570 mAnimation = null;
6571 mAnimLayer = mLayer;
6572 if (mIsImWindow) {
6573 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006574 } else if (mIsWallpaper) {
6575 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006576 }
6577 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6578 + " anim layer: " + mAnimLayer);
6579 mHasTransformation = false;
6580 mHasLocalTransformation = false;
6581 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6582 mTransformation.clear();
6583 if (mHasDrawn
6584 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6585 && mAppToken != null
6586 && mAppToken.firstWindowDrawn
6587 && mAppToken.startingData != null) {
6588 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6589 + mToken + ": first real window done animating");
6590 mFinishedStarting.add(mAppToken);
6591 mH.sendEmptyMessage(H.FINISHED_STARTING);
6592 }
Romain Guy06882f82009-06-10 13:36:04 -07006593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 finishExit();
6595
6596 if (mAppToken != null) {
6597 mAppToken.updateReportedVisibilityLocked();
6598 }
6599
6600 return false;
6601 }
6602
6603 void finishExit() {
6604 if (DEBUG_ANIM) Log.v(
6605 TAG, "finishExit in " + this
6606 + ": exiting=" + mExiting
6607 + " remove=" + mRemoveOnExit
6608 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006610 final int N = mChildWindows.size();
6611 for (int i=0; i<N; i++) {
6612 ((WindowState)mChildWindows.get(i)).finishExit();
6613 }
Romain Guy06882f82009-06-10 13:36:04 -07006614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 if (!mExiting) {
6616 return;
6617 }
Romain Guy06882f82009-06-10 13:36:04 -07006618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006619 if (isWindowAnimating()) {
6620 return;
6621 }
6622
6623 if (localLOGV) Log.v(
6624 TAG, "Exit animation finished in " + this
6625 + ": remove=" + mRemoveOnExit);
6626 if (mSurface != null) {
6627 mDestroySurface.add(this);
6628 mDestroying = true;
6629 if (SHOW_TRANSACTIONS) Log.i(
6630 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6631 try {
6632 mSurface.hide();
6633 } catch (RuntimeException e) {
6634 Log.w(TAG, "Error hiding surface in " + this, e);
6635 }
6636 mLastHidden = true;
6637 mKeyWaiter.releasePendingPointerLocked(mSession);
6638 }
6639 mExiting = false;
6640 if (mRemoveOnExit) {
6641 mPendingRemove.add(this);
6642 mRemoveOnExit = false;
6643 }
6644 }
Romain Guy06882f82009-06-10 13:36:04 -07006645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006646 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6647 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6648 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6649 if (dtdx < -.000001f || dtdx > .000001f) return false;
6650 if (dsdy < -.000001f || dsdy > .000001f) return false;
6651 return true;
6652 }
Romain Guy06882f82009-06-10 13:36:04 -07006653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006654 void computeShownFrameLocked() {
6655 final boolean selfTransformation = mHasLocalTransformation;
6656 Transformation attachedTransformation =
6657 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6658 ? mAttachedWindow.mTransformation : null;
6659 Transformation appTransformation =
6660 (mAppToken != null && mAppToken.hasTransformation)
6661 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006662
6663 // Wallpapers are animated based on the "real" window they
6664 // are currently targeting.
6665 if (mAttrs.type == TYPE_WALLPAPER && mWallpaperTarget != null) {
6666 if (mWallpaperTarget.mHasLocalTransformation) {
6667 attachedTransformation = mWallpaperTarget.mTransformation;
6668 }
6669 if (mWallpaperTarget.mAppToken != null &&
6670 mWallpaperTarget.mAppToken.hasTransformation) {
6671 appTransformation = mWallpaperTarget.mAppToken.transformation;
6672 }
6673 }
6674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006675 if (selfTransformation || attachedTransformation != null
6676 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006677 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 final Rect frame = mFrame;
6679 final float tmpFloats[] = mTmpFloats;
6680 final Matrix tmpMatrix = mTmpMatrix;
6681
6682 // Compute the desired transformation.
6683 tmpMatrix.setTranslate(frame.left, frame.top);
6684 if (selfTransformation) {
6685 tmpMatrix.preConcat(mTransformation.getMatrix());
6686 }
6687 if (attachedTransformation != null) {
6688 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6689 }
6690 if (appTransformation != null) {
6691 tmpMatrix.preConcat(appTransformation.getMatrix());
6692 }
6693
6694 // "convert" it into SurfaceFlinger's format
6695 // (a 2x2 matrix + an offset)
6696 // Here we must not transform the position of the surface
6697 // since it is already included in the transformation.
6698 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006700 tmpMatrix.getValues(tmpFloats);
6701 mDsDx = tmpFloats[Matrix.MSCALE_X];
6702 mDtDx = tmpFloats[Matrix.MSKEW_X];
6703 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6704 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6705 int x = (int)tmpFloats[Matrix.MTRANS_X];
6706 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6707 int w = frame.width();
6708 int h = frame.height();
6709 mShownFrame.set(x, y, x+w, y+h);
6710
6711 // Now set the alpha... but because our current hardware
6712 // can't do alpha transformation on a non-opaque surface,
6713 // turn it off if we are running an animation that is also
6714 // transforming since it is more important to have that
6715 // animation be smooth.
6716 mShownAlpha = mAlpha;
6717 if (!mLimitedAlphaCompositing
6718 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6719 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6720 && x == frame.left && y == frame.top))) {
6721 //Log.i(TAG, "Applying alpha transform");
6722 if (selfTransformation) {
6723 mShownAlpha *= mTransformation.getAlpha();
6724 }
6725 if (attachedTransformation != null) {
6726 mShownAlpha *= attachedTransformation.getAlpha();
6727 }
6728 if (appTransformation != null) {
6729 mShownAlpha *= appTransformation.getAlpha();
6730 }
6731 } else {
6732 //Log.i(TAG, "Not applying alpha transform");
6733 }
Romain Guy06882f82009-06-10 13:36:04 -07006734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006735 if (localLOGV) Log.v(
6736 TAG, "Continuing animation in " + this +
6737 ": " + mShownFrame +
6738 ", alpha=" + mTransformation.getAlpha());
6739 return;
6740 }
Romain Guy06882f82009-06-10 13:36:04 -07006741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 mShownFrame.set(mFrame);
6743 mShownAlpha = mAlpha;
6744 mDsDx = 1;
6745 mDtDx = 0;
6746 mDsDy = 0;
6747 mDtDy = 1;
6748 }
Romain Guy06882f82009-06-10 13:36:04 -07006749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006750 /**
6751 * Is this window visible? It is not visible if there is no
6752 * surface, or we are in the process of running an exit animation
6753 * that will remove the surface, or its app token has been hidden.
6754 */
6755 public boolean isVisibleLw() {
6756 final AppWindowToken atoken = mAppToken;
6757 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6758 && (atoken == null || !atoken.hiddenRequested)
6759 && !mExiting && !mDestroying;
6760 }
6761
6762 /**
6763 * Is this window visible, ignoring its app token? It is not visible
6764 * if there is no surface, or we are in the process of running an exit animation
6765 * that will remove the surface.
6766 */
6767 public boolean isWinVisibleLw() {
6768 final AppWindowToken atoken = mAppToken;
6769 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6770 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6771 && !mExiting && !mDestroying;
6772 }
6773
6774 /**
6775 * The same as isVisible(), but follows the current hidden state of
6776 * the associated app token, not the pending requested hidden state.
6777 */
6778 boolean isVisibleNow() {
6779 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006780 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 }
6782
6783 /**
6784 * Same as isVisible(), but we also count it as visible between the
6785 * call to IWindowSession.add() and the first relayout().
6786 */
6787 boolean isVisibleOrAdding() {
6788 final AppWindowToken atoken = mAppToken;
6789 return (mSurface != null
6790 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6791 && mPolicyVisibility && !mAttachedHidden
6792 && (atoken == null || !atoken.hiddenRequested)
6793 && !mExiting && !mDestroying;
6794 }
6795
6796 /**
6797 * Is this window currently on-screen? It is on-screen either if it
6798 * is visible or it is currently running an animation before no longer
6799 * being visible.
6800 */
6801 boolean isOnScreen() {
6802 final AppWindowToken atoken = mAppToken;
6803 if (atoken != null) {
6804 return mSurface != null && mPolicyVisibility && !mDestroying
6805 && ((!mAttachedHidden && !atoken.hiddenRequested)
6806 || mAnimating || atoken.animating);
6807 } else {
6808 return mSurface != null && mPolicyVisibility && !mDestroying
6809 && (!mAttachedHidden || mAnimating);
6810 }
6811 }
Romain Guy06882f82009-06-10 13:36:04 -07006812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006813 /**
6814 * Like isOnScreen(), but we don't return true if the window is part
6815 * of a transition that has not yet been started.
6816 */
6817 boolean isReadyForDisplay() {
6818 final AppWindowToken atoken = mAppToken;
6819 final boolean animating = atoken != null ? atoken.animating : false;
6820 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006821 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 || mAnimating || animating);
6823 }
6824
6825 /** Is the window or its container currently animating? */
6826 boolean isAnimating() {
6827 final WindowState attached = mAttachedWindow;
6828 final AppWindowToken atoken = mAppToken;
6829 return mAnimation != null
6830 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006831 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832 (atoken.animation != null
6833 || atoken.inPendingTransaction));
6834 }
6835
6836 /** Is this window currently animating? */
6837 boolean isWindowAnimating() {
6838 return mAnimation != null;
6839 }
6840
6841 /**
6842 * Like isOnScreen, but returns false if the surface hasn't yet
6843 * been drawn.
6844 */
6845 public boolean isDisplayedLw() {
6846 final AppWindowToken atoken = mAppToken;
6847 return mSurface != null && mPolicyVisibility && !mDestroying
6848 && !mDrawPending && !mCommitDrawPending
6849 && ((!mAttachedHidden &&
6850 (atoken == null || !atoken.hiddenRequested))
6851 || mAnimating);
6852 }
6853
6854 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6855 boolean shownFrame, boolean onlyOpaque) {
6856 if (mSurface == null) {
6857 return false;
6858 }
6859 if (mAppToken != null && !mAppToken.appFullscreen) {
6860 return false;
6861 }
6862 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6863 return false;
6864 }
6865 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006866
6867 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
6868 return frame.left <= mCompatibleScreenFrame.left &&
6869 frame.top <= mCompatibleScreenFrame.top &&
6870 frame.right >= mCompatibleScreenFrame.right &&
6871 frame.bottom >= mCompatibleScreenFrame.bottom;
6872 } else {
6873 return frame.left <= 0 && frame.top <= 0
6874 && frame.right >= screenWidth
6875 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006877 }
Romain Guy06882f82009-06-10 13:36:04 -07006878
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006879 /**
6880 * Return true if the window is opaque and fully drawn.
6881 */
6882 boolean isOpaqueDrawn() {
6883 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
6884 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
6885 }
6886
6887 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
6888 return
6889 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006890 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
6891 // only if it's visible
6892 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006893 // and only if the application fills the compatible screen
6894 mFrame.left <= mCompatibleScreenFrame.left &&
6895 mFrame.top <= mCompatibleScreenFrame.top &&
6896 mFrame.right >= mCompatibleScreenFrame.right &&
6897 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006898 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006899 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006900 }
6901
6902 boolean isFullscreen(int screenWidth, int screenHeight) {
6903 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006904 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006905 }
6906
6907 void removeLocked() {
6908 if (mAttachedWindow != null) {
6909 mAttachedWindow.mChildWindows.remove(this);
6910 }
6911 destroySurfaceLocked();
6912 mSession.windowRemovedLocked();
6913 try {
6914 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6915 } catch (RuntimeException e) {
6916 // Ignore if it has already been removed (usually because
6917 // we are doing this as part of processing a death note.)
6918 }
6919 }
6920
6921 private class DeathRecipient implements IBinder.DeathRecipient {
6922 public void binderDied() {
6923 try {
6924 synchronized(mWindowMap) {
6925 WindowState win = windowForClientLocked(mSession, mClient);
6926 Log.i(TAG, "WIN DEATH: " + win);
6927 if (win != null) {
6928 removeWindowLocked(mSession, win);
6929 }
6930 }
6931 } catch (IllegalArgumentException ex) {
6932 // This will happen if the window has already been
6933 // removed.
6934 }
6935 }
6936 }
6937
6938 /** Returns true if this window desires key events. */
6939 public final boolean canReceiveKeys() {
6940 return isVisibleOrAdding()
6941 && (mViewVisibility == View.VISIBLE)
6942 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6943 }
6944
6945 public boolean hasDrawnLw() {
6946 return mHasDrawn;
6947 }
6948
6949 public boolean showLw(boolean doAnimation) {
6950 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6951 mPolicyVisibility = true;
6952 mPolicyVisibilityAfterAnim = true;
6953 if (doAnimation) {
6954 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6955 }
6956 requestAnimationLocked(0);
6957 return true;
6958 }
6959 return false;
6960 }
6961
6962 public boolean hideLw(boolean doAnimation) {
6963 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6964 : mPolicyVisibility;
6965 if (current) {
6966 if (doAnimation) {
6967 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6968 if (mAnimation == null) {
6969 doAnimation = false;
6970 }
6971 }
6972 if (doAnimation) {
6973 mPolicyVisibilityAfterAnim = false;
6974 } else {
6975 mPolicyVisibilityAfterAnim = false;
6976 mPolicyVisibility = false;
6977 }
6978 requestAnimationLocked(0);
6979 return true;
6980 }
6981 return false;
6982 }
6983
6984 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006985 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07006986
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006987 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6988 pw.print(" mClient="); pw.println(mClient.asBinder());
6989 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6990 if (mAttachedWindow != null || mLayoutAttached) {
6991 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6992 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6993 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006994 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
6995 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
6996 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006997 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
6998 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006999 }
7000 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7001 pw.print(" mSubLayer="); pw.print(mSubLayer);
7002 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7003 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7004 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7005 pw.print("="); pw.print(mAnimLayer);
7006 pw.print(" mLastLayer="); pw.println(mLastLayer);
7007 if (mSurface != null) {
7008 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7009 }
7010 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7011 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7012 if (mAppToken != null) {
7013 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7014 }
7015 if (mTargetAppToken != null) {
7016 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7017 }
7018 pw.print(prefix); pw.print("mViewVisibility=0x");
7019 pw.print(Integer.toHexString(mViewVisibility));
7020 pw.print(" mLastHidden="); pw.print(mLastHidden);
7021 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
7022 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7023 pw.print(prefix); pw.print("mPolicyVisibility=");
7024 pw.print(mPolicyVisibility);
7025 pw.print(" mPolicyVisibilityAfterAnim=");
7026 pw.print(mPolicyVisibilityAfterAnim);
7027 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7028 }
7029 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
7030 pw.print(" h="); pw.print(mRequestedHeight);
7031 pw.print(" x="); pw.print(mReqXPos);
7032 pw.print(" y="); pw.println(mReqYPos);
7033 pw.print(prefix); pw.print("mGivenContentInsets=");
7034 mGivenContentInsets.printShortString(pw);
7035 pw.print(" mGivenVisibleInsets=");
7036 mGivenVisibleInsets.printShortString(pw);
7037 pw.println();
7038 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7039 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7040 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7041 }
7042 pw.print(prefix); pw.print("mShownFrame=");
7043 mShownFrame.printShortString(pw);
7044 pw.print(" last="); mLastShownFrame.printShortString(pw);
7045 pw.println();
7046 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7047 pw.print(" last="); mLastFrame.printShortString(pw);
7048 pw.println();
7049 pw.print(prefix); pw.print("mContainingFrame=");
7050 mContainingFrame.printShortString(pw);
7051 pw.print(" mDisplayFrame=");
7052 mDisplayFrame.printShortString(pw);
7053 pw.println();
7054 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7055 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7056 pw.println();
7057 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7058 pw.print(" last="); mLastContentInsets.printShortString(pw);
7059 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7060 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7061 pw.println();
7062 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7063 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7064 pw.print(" mAlpha="); pw.print(mAlpha);
7065 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7066 }
7067 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7068 || mAnimation != null) {
7069 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7070 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7071 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7072 pw.print(" mAnimation="); pw.println(mAnimation);
7073 }
7074 if (mHasTransformation || mHasLocalTransformation) {
7075 pw.print(prefix); pw.print("XForm: has=");
7076 pw.print(mHasTransformation);
7077 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7078 pw.print(" "); mTransformation.printShortString(pw);
7079 pw.println();
7080 }
7081 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7082 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7083 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7084 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7085 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7086 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7087 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7088 pw.print(" mDestroying="); pw.print(mDestroying);
7089 pw.print(" mRemoved="); pw.println(mRemoved);
7090 }
7091 if (mOrientationChanging || mAppFreezing) {
7092 pw.print(prefix); pw.print("mOrientationChanging=");
7093 pw.print(mOrientationChanging);
7094 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7095 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007096 if (mHScale != 1 || mVScale != 1) {
7097 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7098 pw.print(" mVScale="); pw.println(mVScale);
7099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 }
7101
7102 @Override
7103 public String toString() {
7104 return "Window{"
7105 + Integer.toHexString(System.identityHashCode(this))
7106 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7107 }
7108 }
Romain Guy06882f82009-06-10 13:36:04 -07007109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 // -------------------------------------------------------------
7111 // Window Token State
7112 // -------------------------------------------------------------
7113
7114 class WindowToken {
7115 // The actual token.
7116 final IBinder token;
7117
7118 // The type of window this token is for, as per WindowManager.LayoutParams.
7119 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 // Set if this token was explicitly added by a client, so should
7122 // not be removed when all windows are removed.
7123 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007124
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007125 // For printing.
7126 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 // If this is an AppWindowToken, this is non-null.
7129 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007131 // All of the windows associated with this token.
7132 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7133
7134 // Is key dispatching paused for this token?
7135 boolean paused = false;
7136
7137 // Should this token's windows be hidden?
7138 boolean hidden;
7139
7140 // Temporary for finding which tokens no longer have visible windows.
7141 boolean hasVisible;
7142
7143 WindowToken(IBinder _token, int type, boolean _explicit) {
7144 token = _token;
7145 windowType = type;
7146 explicit = _explicit;
7147 }
7148
7149 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007150 pw.print(prefix); pw.print("token="); pw.println(token);
7151 pw.print(prefix); pw.print("windows="); pw.println(windows);
7152 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7153 pw.print(" hidden="); pw.print(hidden);
7154 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007155 }
7156
7157 @Override
7158 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007159 if (stringName == null) {
7160 StringBuilder sb = new StringBuilder();
7161 sb.append("WindowToken{");
7162 sb.append(Integer.toHexString(System.identityHashCode(this)));
7163 sb.append(" token="); sb.append(token); sb.append('}');
7164 stringName = sb.toString();
7165 }
7166 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 }
7168 };
7169
7170 class AppWindowToken extends WindowToken {
7171 // Non-null only for application tokens.
7172 final IApplicationToken appToken;
7173
7174 // All of the windows and child windows that are included in this
7175 // application token. Note this list is NOT sorted!
7176 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7177
7178 int groupId = -1;
7179 boolean appFullscreen;
7180 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007182 // These are used for determining when all windows associated with
7183 // an activity have been drawn, so they can be made visible together
7184 // at the same time.
7185 int lastTransactionSequence = mTransactionSequence-1;
7186 int numInterestingWindows;
7187 int numDrawnWindows;
7188 boolean inPendingTransaction;
7189 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 // Is this token going to be hidden in a little while? If so, it
7192 // won't be taken into account for setting the screen orientation.
7193 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007195 // Is this window's surface needed? This is almost like hidden, except
7196 // it will sometimes be true a little earlier: when the token has
7197 // been shown, but is still waiting for its app transition to execute
7198 // before making its windows shown.
7199 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007201 // Have we told the window clients to hide themselves?
7202 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007204 // Last visibility state we reported to the app token.
7205 boolean reportedVisible;
7206
7207 // Set to true when the token has been removed from the window mgr.
7208 boolean removed;
7209
7210 // Have we been asked to have this token keep the screen frozen?
7211 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007213 boolean animating;
7214 Animation animation;
7215 boolean hasTransformation;
7216 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007218 // Offset to the window of all layers in the token, for use by
7219 // AppWindowToken animations.
7220 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007222 // Information about an application starting window if displayed.
7223 StartingData startingData;
7224 WindowState startingWindow;
7225 View startingView;
7226 boolean startingDisplayed;
7227 boolean startingMoved;
7228 boolean firstWindowDrawn;
7229
7230 AppWindowToken(IApplicationToken _token) {
7231 super(_token.asBinder(),
7232 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7233 appWindowToken = this;
7234 appToken = _token;
7235 }
Romain Guy06882f82009-06-10 13:36:04 -07007236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007237 public void setAnimation(Animation anim) {
7238 if (localLOGV) Log.v(
7239 TAG, "Setting animation in " + this + ": " + anim);
7240 animation = anim;
7241 animating = false;
7242 anim.restrictDuration(MAX_ANIMATION_DURATION);
7243 anim.scaleCurrentDuration(mTransitionAnimationScale);
7244 int zorder = anim.getZAdjustment();
7245 int adj = 0;
7246 if (zorder == Animation.ZORDER_TOP) {
7247 adj = TYPE_LAYER_OFFSET;
7248 } else if (zorder == Animation.ZORDER_BOTTOM) {
7249 adj = -TYPE_LAYER_OFFSET;
7250 }
Romain Guy06882f82009-06-10 13:36:04 -07007251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007252 if (animLayerAdjustment != adj) {
7253 animLayerAdjustment = adj;
7254 updateLayers();
7255 }
7256 }
Romain Guy06882f82009-06-10 13:36:04 -07007257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007258 public void setDummyAnimation() {
7259 if (animation == null) {
7260 if (localLOGV) Log.v(
7261 TAG, "Setting dummy animation in " + this);
7262 animation = sDummyAnimation;
7263 }
7264 }
7265
7266 public void clearAnimation() {
7267 if (animation != null) {
7268 animation = null;
7269 animating = true;
7270 }
7271 }
Romain Guy06882f82009-06-10 13:36:04 -07007272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007273 void updateLayers() {
7274 final int N = allAppWindows.size();
7275 final int adj = animLayerAdjustment;
7276 for (int i=0; i<N; i++) {
7277 WindowState w = allAppWindows.get(i);
7278 w.mAnimLayer = w.mLayer + adj;
7279 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7280 + w.mAnimLayer);
7281 if (w == mInputMethodTarget) {
7282 setInputMethodAnimLayerAdjustment(adj);
7283 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007284 if (w == mWallpaperTarget) {
7285 setWallpaperAnimLayerAdjustment(adj);
7286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 }
7288 }
Romain Guy06882f82009-06-10 13:36:04 -07007289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 void sendAppVisibilityToClients() {
7291 final int N = allAppWindows.size();
7292 for (int i=0; i<N; i++) {
7293 WindowState win = allAppWindows.get(i);
7294 if (win == startingWindow && clientHidden) {
7295 // Don't hide the starting window.
7296 continue;
7297 }
7298 try {
7299 if (DEBUG_VISIBILITY) Log.v(TAG,
7300 "Setting visibility of " + win + ": " + (!clientHidden));
7301 win.mClient.dispatchAppVisibility(!clientHidden);
7302 } catch (RemoteException e) {
7303 }
7304 }
7305 }
Romain Guy06882f82009-06-10 13:36:04 -07007306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007307 void showAllWindowsLocked() {
7308 final int NW = allAppWindows.size();
7309 for (int i=0; i<NW; i++) {
7310 WindowState w = allAppWindows.get(i);
7311 if (DEBUG_VISIBILITY) Log.v(TAG,
7312 "performing show on: " + w);
7313 w.performShowLocked();
7314 }
7315 }
Romain Guy06882f82009-06-10 13:36:04 -07007316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 // This must be called while inside a transaction.
7318 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7319 if (!mDisplayFrozen) {
7320 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007322 if (animation == sDummyAnimation) {
7323 // This guy is going to animate, but not yet. For now count
7324 // it is not animating for purposes of scheduling transactions;
7325 // when it is really time to animate, this will be set to
7326 // a real animation and the next call will execute normally.
7327 return false;
7328 }
Romain Guy06882f82009-06-10 13:36:04 -07007329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007330 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7331 if (!animating) {
7332 if (DEBUG_ANIM) Log.v(
7333 TAG, "Starting animation in " + this +
7334 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7335 + " scale=" + mTransitionAnimationScale
7336 + " allDrawn=" + allDrawn + " animating=" + animating);
7337 animation.initialize(dw, dh, dw, dh);
7338 animation.setStartTime(currentTime);
7339 animating = true;
7340 }
7341 transformation.clear();
7342 final boolean more = animation.getTransformation(
7343 currentTime, transformation);
7344 if (DEBUG_ANIM) Log.v(
7345 TAG, "Stepped animation in " + this +
7346 ": more=" + more + ", xform=" + transformation);
7347 if (more) {
7348 // we're done!
7349 hasTransformation = true;
7350 return true;
7351 }
7352 if (DEBUG_ANIM) Log.v(
7353 TAG, "Finished animation in " + this +
7354 " @ " + currentTime);
7355 animation = null;
7356 }
7357 } else if (animation != null) {
7358 // If the display is frozen, and there is a pending animation,
7359 // clear it and make sure we run the cleanup code.
7360 animating = true;
7361 animation = null;
7362 }
7363
7364 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007366 if (!animating) {
7367 return false;
7368 }
7369
7370 clearAnimation();
7371 animating = false;
7372 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7373 moveInputMethodWindowsIfNeededLocked(true);
7374 }
Romain Guy06882f82009-06-10 13:36:04 -07007375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 if (DEBUG_ANIM) Log.v(
7377 TAG, "Animation done in " + this
7378 + ": reportedVisible=" + reportedVisible);
7379
7380 transformation.clear();
7381 if (animLayerAdjustment != 0) {
7382 animLayerAdjustment = 0;
7383 updateLayers();
7384 }
Romain Guy06882f82009-06-10 13:36:04 -07007385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 final int N = windows.size();
7387 for (int i=0; i<N; i++) {
7388 ((WindowState)windows.get(i)).finishExit();
7389 }
7390 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 return false;
7393 }
7394
7395 void updateReportedVisibilityLocked() {
7396 if (appToken == null) {
7397 return;
7398 }
Romain Guy06882f82009-06-10 13:36:04 -07007399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007400 int numInteresting = 0;
7401 int numVisible = 0;
7402 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007404 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7405 final int N = allAppWindows.size();
7406 for (int i=0; i<N; i++) {
7407 WindowState win = allAppWindows.get(i);
7408 if (win == startingWindow || win.mAppFreezing) {
7409 continue;
7410 }
7411 if (DEBUG_VISIBILITY) {
7412 Log.v(TAG, "Win " + win + ": isDisplayed="
7413 + win.isDisplayedLw()
7414 + ", isAnimating=" + win.isAnimating());
7415 if (!win.isDisplayedLw()) {
7416 Log.v(TAG, "Not displayed: s=" + win.mSurface
7417 + " pv=" + win.mPolicyVisibility
7418 + " dp=" + win.mDrawPending
7419 + " cdp=" + win.mCommitDrawPending
7420 + " ah=" + win.mAttachedHidden
7421 + " th="
7422 + (win.mAppToken != null
7423 ? win.mAppToken.hiddenRequested : false)
7424 + " a=" + win.mAnimating);
7425 }
7426 }
7427 numInteresting++;
7428 if (win.isDisplayedLw()) {
7429 if (!win.isAnimating()) {
7430 numVisible++;
7431 }
7432 nowGone = false;
7433 } else if (win.isAnimating()) {
7434 nowGone = false;
7435 }
7436 }
Romain Guy06882f82009-06-10 13:36:04 -07007437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007438 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7439 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7440 + numInteresting + " visible=" + numVisible);
7441 if (nowVisible != reportedVisible) {
7442 if (DEBUG_VISIBILITY) Log.v(
7443 TAG, "Visibility changed in " + this
7444 + ": vis=" + nowVisible);
7445 reportedVisible = nowVisible;
7446 Message m = mH.obtainMessage(
7447 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7448 nowVisible ? 1 : 0,
7449 nowGone ? 1 : 0,
7450 this);
7451 mH.sendMessage(m);
7452 }
7453 }
Romain Guy06882f82009-06-10 13:36:04 -07007454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007455 void dump(PrintWriter pw, String prefix) {
7456 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007457 if (appToken != null) {
7458 pw.print(prefix); pw.println("app=true");
7459 }
7460 if (allAppWindows.size() > 0) {
7461 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7462 }
7463 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7464 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7465 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7466 pw.print(" clientHidden="); pw.print(clientHidden);
7467 pw.print(" willBeHidden="); pw.print(willBeHidden);
7468 pw.print(" reportedVisible="); pw.println(reportedVisible);
7469 if (paused || freezingScreen) {
7470 pw.print(prefix); pw.print("paused="); pw.print(paused);
7471 pw.print(" freezingScreen="); pw.println(freezingScreen);
7472 }
7473 if (numInterestingWindows != 0 || numDrawnWindows != 0
7474 || inPendingTransaction || allDrawn) {
7475 pw.print(prefix); pw.print("numInterestingWindows=");
7476 pw.print(numInterestingWindows);
7477 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7478 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7479 pw.print(" allDrawn="); pw.println(allDrawn);
7480 }
7481 if (animating || animation != null) {
7482 pw.print(prefix); pw.print("animating="); pw.print(animating);
7483 pw.print(" animation="); pw.println(animation);
7484 }
7485 if (animLayerAdjustment != 0) {
7486 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7487 }
7488 if (hasTransformation) {
7489 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7490 pw.print(" transformation="); transformation.printShortString(pw);
7491 pw.println();
7492 }
7493 if (startingData != null || removed || firstWindowDrawn) {
7494 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7495 pw.print(" removed="); pw.print(removed);
7496 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7497 }
7498 if (startingWindow != null || startingView != null
7499 || startingDisplayed || startingMoved) {
7500 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7501 pw.print(" startingView="); pw.print(startingView);
7502 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7503 pw.print(" startingMoved"); pw.println(startingMoved);
7504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 }
7506
7507 @Override
7508 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007509 if (stringName == null) {
7510 StringBuilder sb = new StringBuilder();
7511 sb.append("AppWindowToken{");
7512 sb.append(Integer.toHexString(System.identityHashCode(this)));
7513 sb.append(" token="); sb.append(token); sb.append('}');
7514 stringName = sb.toString();
7515 }
7516 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007517 }
7518 }
Romain Guy06882f82009-06-10 13:36:04 -07007519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 public static WindowManager.LayoutParams findAnimations(
7521 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007522 ArrayList<AppWindowToken> openingTokenList1,
7523 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007524 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007525
7526 // First, check if there is a compatible window in opening/closing
7527 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007528 WindowManager.LayoutParams animParams = null;
7529 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007530 animParams = findCompatibleWindowParams(openingTokenList1);
7531 if (animParams == null) {
7532 animParams = findCompatibleWindowParams(closingTokenList2);
7533 }
7534 if (animParams != null) {
7535 return animParams;
7536 }
7537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007538 //Log.i(TAG, "Looking for animations...");
7539 for (int i=order.size()-1; i>=0; i--) {
7540 AppWindowToken wtoken = order.get(i);
7541 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007542 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007543 int j = wtoken.windows.size();
7544 while (j > 0) {
7545 j--;
7546 WindowState win = wtoken.windows.get(j);
7547 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7548 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7549 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7550 //Log.i(TAG, "Found base or application window, done!");
7551 if (wtoken.appFullscreen) {
7552 return win.mAttrs;
7553 }
7554 if (animSrc < 2) {
7555 animParams = win.mAttrs;
7556 animSrc = 2;
7557 }
7558 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7559 //Log.i(TAG, "Found normal window, we may use this...");
7560 animParams = win.mAttrs;
7561 animSrc = 1;
7562 }
7563 }
7564 }
7565 }
Romain Guy06882f82009-06-10 13:36:04 -07007566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007567 return animParams;
7568 }
Romain Guy06882f82009-06-10 13:36:04 -07007569
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007570 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7571 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7572 AppWindowToken wtoken = tokenList.get(appCount);
7573 // Just checking one window is sufficient as all windows have the compatible flag
7574 // if the application is in compatibility mode.
7575 if (wtoken.windows.size() > 0) {
7576 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7577 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7578 return params;
7579 }
7580 }
7581 }
7582 return null;
7583 }
7584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007585 // -------------------------------------------------------------
7586 // DummyAnimation
7587 // -------------------------------------------------------------
7588
7589 // This is an animation that does nothing: it just immediately finishes
7590 // itself every time it is called. It is used as a stub animation in cases
7591 // where we want to synchronize multiple things that may be animating.
7592 static final class DummyAnimation extends Animation {
7593 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7594 return false;
7595 }
7596 }
7597 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007599 // -------------------------------------------------------------
7600 // Async Handler
7601 // -------------------------------------------------------------
7602
7603 static final class StartingData {
7604 final String pkg;
7605 final int theme;
7606 final CharSequence nonLocalizedLabel;
7607 final int labelRes;
7608 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007610 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7611 int _labelRes, int _icon) {
7612 pkg = _pkg;
7613 theme = _theme;
7614 nonLocalizedLabel = _nonLocalizedLabel;
7615 labelRes = _labelRes;
7616 icon = _icon;
7617 }
7618 }
7619
7620 private final class H extends Handler {
7621 public static final int REPORT_FOCUS_CHANGE = 2;
7622 public static final int REPORT_LOSING_FOCUS = 3;
7623 public static final int ANIMATE = 4;
7624 public static final int ADD_STARTING = 5;
7625 public static final int REMOVE_STARTING = 6;
7626 public static final int FINISHED_STARTING = 7;
7627 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007628 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7629 public static final int HOLD_SCREEN_CHANGED = 12;
7630 public static final int APP_TRANSITION_TIMEOUT = 13;
7631 public static final int PERSIST_ANIMATION_SCALE = 14;
7632 public static final int FORCE_GC = 15;
7633 public static final int ENABLE_SCREEN = 16;
7634 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007635 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007637 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007639 public H() {
7640 }
Romain Guy06882f82009-06-10 13:36:04 -07007641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007642 @Override
7643 public void handleMessage(Message msg) {
7644 switch (msg.what) {
7645 case REPORT_FOCUS_CHANGE: {
7646 WindowState lastFocus;
7647 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 synchronized(mWindowMap) {
7650 lastFocus = mLastFocus;
7651 newFocus = mCurrentFocus;
7652 if (lastFocus == newFocus) {
7653 // Focus is not changing, so nothing to do.
7654 return;
7655 }
7656 mLastFocus = newFocus;
7657 //Log.i(TAG, "Focus moving from " + lastFocus
7658 // + " to " + newFocus);
7659 if (newFocus != null && lastFocus != null
7660 && !newFocus.isDisplayedLw()) {
7661 //Log.i(TAG, "Delaying loss of focus...");
7662 mLosingFocus.add(lastFocus);
7663 lastFocus = null;
7664 }
7665 }
7666
7667 if (lastFocus != newFocus) {
7668 //System.out.println("Changing focus from " + lastFocus
7669 // + " to " + newFocus);
7670 if (newFocus != null) {
7671 try {
7672 //Log.i(TAG, "Gaining focus: " + newFocus);
7673 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7674 } catch (RemoteException e) {
7675 // Ignore if process has died.
7676 }
7677 }
7678
7679 if (lastFocus != null) {
7680 try {
7681 //Log.i(TAG, "Losing focus: " + lastFocus);
7682 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7683 } catch (RemoteException e) {
7684 // Ignore if process has died.
7685 }
7686 }
7687 }
7688 } break;
7689
7690 case REPORT_LOSING_FOCUS: {
7691 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693 synchronized(mWindowMap) {
7694 losers = mLosingFocus;
7695 mLosingFocus = new ArrayList<WindowState>();
7696 }
7697
7698 final int N = losers.size();
7699 for (int i=0; i<N; i++) {
7700 try {
7701 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7702 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7703 } catch (RemoteException e) {
7704 // Ignore if process has died.
7705 }
7706 }
7707 } break;
7708
7709 case ANIMATE: {
7710 synchronized(mWindowMap) {
7711 mAnimationPending = false;
7712 performLayoutAndPlaceSurfacesLocked();
7713 }
7714 } break;
7715
7716 case ADD_STARTING: {
7717 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7718 final StartingData sd = wtoken.startingData;
7719
7720 if (sd == null) {
7721 // Animation has been canceled... do nothing.
7722 return;
7723 }
Romain Guy06882f82009-06-10 13:36:04 -07007724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7726 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007728 View view = null;
7729 try {
7730 view = mPolicy.addStartingWindow(
7731 wtoken.token, sd.pkg,
7732 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7733 sd.icon);
7734 } catch (Exception e) {
7735 Log.w(TAG, "Exception when adding starting window", e);
7736 }
7737
7738 if (view != null) {
7739 boolean abort = false;
7740
7741 synchronized(mWindowMap) {
7742 if (wtoken.removed || wtoken.startingData == null) {
7743 // If the window was successfully added, then
7744 // we need to remove it.
7745 if (wtoken.startingWindow != null) {
7746 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7747 "Aborted starting " + wtoken
7748 + ": removed=" + wtoken.removed
7749 + " startingData=" + wtoken.startingData);
7750 wtoken.startingWindow = null;
7751 wtoken.startingData = null;
7752 abort = true;
7753 }
7754 } else {
7755 wtoken.startingView = view;
7756 }
7757 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7758 "Added starting " + wtoken
7759 + ": startingWindow="
7760 + wtoken.startingWindow + " startingView="
7761 + wtoken.startingView);
7762 }
7763
7764 if (abort) {
7765 try {
7766 mPolicy.removeStartingWindow(wtoken.token, view);
7767 } catch (Exception e) {
7768 Log.w(TAG, "Exception when removing starting window", e);
7769 }
7770 }
7771 }
7772 } break;
7773
7774 case REMOVE_STARTING: {
7775 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7776 IBinder token = null;
7777 View view = null;
7778 synchronized (mWindowMap) {
7779 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7780 + wtoken + ": startingWindow="
7781 + wtoken.startingWindow + " startingView="
7782 + wtoken.startingView);
7783 if (wtoken.startingWindow != null) {
7784 view = wtoken.startingView;
7785 token = wtoken.token;
7786 wtoken.startingData = null;
7787 wtoken.startingView = null;
7788 wtoken.startingWindow = null;
7789 }
7790 }
7791 if (view != null) {
7792 try {
7793 mPolicy.removeStartingWindow(token, view);
7794 } catch (Exception e) {
7795 Log.w(TAG, "Exception when removing starting window", e);
7796 }
7797 }
7798 } break;
7799
7800 case FINISHED_STARTING: {
7801 IBinder token = null;
7802 View view = null;
7803 while (true) {
7804 synchronized (mWindowMap) {
7805 final int N = mFinishedStarting.size();
7806 if (N <= 0) {
7807 break;
7808 }
7809 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7810
7811 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7812 "Finished starting " + wtoken
7813 + ": startingWindow=" + wtoken.startingWindow
7814 + " startingView=" + wtoken.startingView);
7815
7816 if (wtoken.startingWindow == null) {
7817 continue;
7818 }
7819
7820 view = wtoken.startingView;
7821 token = wtoken.token;
7822 wtoken.startingData = null;
7823 wtoken.startingView = null;
7824 wtoken.startingWindow = null;
7825 }
7826
7827 try {
7828 mPolicy.removeStartingWindow(token, view);
7829 } catch (Exception e) {
7830 Log.w(TAG, "Exception when removing starting window", e);
7831 }
7832 }
7833 } break;
7834
7835 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7836 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7837
7838 boolean nowVisible = msg.arg1 != 0;
7839 boolean nowGone = msg.arg2 != 0;
7840
7841 try {
7842 if (DEBUG_VISIBILITY) Log.v(
7843 TAG, "Reporting visible in " + wtoken
7844 + " visible=" + nowVisible
7845 + " gone=" + nowGone);
7846 if (nowVisible) {
7847 wtoken.appToken.windowsVisible();
7848 } else {
7849 wtoken.appToken.windowsGone();
7850 }
7851 } catch (RemoteException ex) {
7852 }
7853 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007855 case WINDOW_FREEZE_TIMEOUT: {
7856 synchronized (mWindowMap) {
7857 Log.w(TAG, "Window freeze timeout expired.");
7858 int i = mWindows.size();
7859 while (i > 0) {
7860 i--;
7861 WindowState w = (WindowState)mWindows.get(i);
7862 if (w.mOrientationChanging) {
7863 w.mOrientationChanging = false;
7864 Log.w(TAG, "Force clearing orientation change: " + w);
7865 }
7866 }
7867 performLayoutAndPlaceSurfacesLocked();
7868 }
7869 break;
7870 }
Romain Guy06882f82009-06-10 13:36:04 -07007871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 case HOLD_SCREEN_CHANGED: {
7873 Session oldHold;
7874 Session newHold;
7875 synchronized (mWindowMap) {
7876 oldHold = mLastReportedHold;
7877 newHold = (Session)msg.obj;
7878 mLastReportedHold = newHold;
7879 }
Romain Guy06882f82009-06-10 13:36:04 -07007880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007881 if (oldHold != newHold) {
7882 try {
7883 if (oldHold != null) {
7884 mBatteryStats.noteStopWakelock(oldHold.mUid,
7885 "window",
7886 BatteryStats.WAKE_TYPE_WINDOW);
7887 }
7888 if (newHold != null) {
7889 mBatteryStats.noteStartWakelock(newHold.mUid,
7890 "window",
7891 BatteryStats.WAKE_TYPE_WINDOW);
7892 }
7893 } catch (RemoteException e) {
7894 }
7895 }
7896 break;
7897 }
Romain Guy06882f82009-06-10 13:36:04 -07007898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007899 case APP_TRANSITION_TIMEOUT: {
7900 synchronized (mWindowMap) {
7901 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7902 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7903 "*** APP TRANSITION TIMEOUT");
7904 mAppTransitionReady = true;
7905 mAppTransitionTimeout = true;
7906 performLayoutAndPlaceSurfacesLocked();
7907 }
7908 }
7909 break;
7910 }
Romain Guy06882f82009-06-10 13:36:04 -07007911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 case PERSIST_ANIMATION_SCALE: {
7913 Settings.System.putFloat(mContext.getContentResolver(),
7914 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7915 Settings.System.putFloat(mContext.getContentResolver(),
7916 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7917 break;
7918 }
Romain Guy06882f82009-06-10 13:36:04 -07007919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 case FORCE_GC: {
7921 synchronized(mWindowMap) {
7922 if (mAnimationPending) {
7923 // If we are animating, don't do the gc now but
7924 // delay a bit so we don't interrupt the animation.
7925 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7926 2000);
7927 return;
7928 }
7929 // If we are currently rotating the display, it will
7930 // schedule a new message when done.
7931 if (mDisplayFrozen) {
7932 return;
7933 }
7934 mFreezeGcPending = 0;
7935 }
7936 Runtime.getRuntime().gc();
7937 break;
7938 }
Romain Guy06882f82009-06-10 13:36:04 -07007939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007940 case ENABLE_SCREEN: {
7941 performEnableScreen();
7942 break;
7943 }
Romain Guy06882f82009-06-10 13:36:04 -07007944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007945 case APP_FREEZE_TIMEOUT: {
7946 synchronized (mWindowMap) {
7947 Log.w(TAG, "App freeze timeout expired.");
7948 int i = mAppTokens.size();
7949 while (i > 0) {
7950 i--;
7951 AppWindowToken tok = mAppTokens.get(i);
7952 if (tok.freezingScreen) {
7953 Log.w(TAG, "Force clearing freeze: " + tok);
7954 unsetAppFreezingScreenLocked(tok, true, true);
7955 }
7956 }
7957 }
7958 break;
7959 }
Romain Guy06882f82009-06-10 13:36:04 -07007960
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007961 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07007962 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007963 sendNewConfiguration();
7964 }
7965 break;
7966 }
Romain Guy06882f82009-06-10 13:36:04 -07007967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007968 }
7969 }
7970 }
7971
7972 // -------------------------------------------------------------
7973 // IWindowManager API
7974 // -------------------------------------------------------------
7975
7976 public IWindowSession openSession(IInputMethodClient client,
7977 IInputContext inputContext) {
7978 if (client == null) throw new IllegalArgumentException("null client");
7979 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7980 return new Session(client, inputContext);
7981 }
7982
7983 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7984 synchronized (mWindowMap) {
7985 // The focus for the client is the window immediately below
7986 // where we would place the input method window.
7987 int idx = findDesiredInputMethodWindowIndexLocked(false);
7988 WindowState imFocus;
7989 if (idx > 0) {
7990 imFocus = (WindowState)mWindows.get(idx-1);
7991 if (imFocus != null) {
7992 if (imFocus.mSession.mClient != null &&
7993 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7994 return true;
7995 }
7996 }
7997 }
7998 }
7999 return false;
8000 }
Romain Guy06882f82009-06-10 13:36:04 -07008001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008002 // -------------------------------------------------------------
8003 // Internals
8004 // -------------------------------------------------------------
8005
8006 final WindowState windowForClientLocked(Session session, IWindow client) {
8007 return windowForClientLocked(session, client.asBinder());
8008 }
Romain Guy06882f82009-06-10 13:36:04 -07008009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008010 final WindowState windowForClientLocked(Session session, IBinder client) {
8011 WindowState win = mWindowMap.get(client);
8012 if (localLOGV) Log.v(
8013 TAG, "Looking up client " + client + ": " + win);
8014 if (win == null) {
8015 RuntimeException ex = new RuntimeException();
8016 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8017 return null;
8018 }
8019 if (session != null && win.mSession != session) {
8020 RuntimeException ex = new RuntimeException();
8021 Log.w(TAG, "Requested window " + client + " is in session " +
8022 win.mSession + ", not " + session, ex);
8023 return null;
8024 }
8025
8026 return win;
8027 }
8028
8029 private final void assignLayersLocked() {
8030 int N = mWindows.size();
8031 int curBaseLayer = 0;
8032 int curLayer = 0;
8033 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008035 for (i=0; i<N; i++) {
8036 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008037 if (w.mBaseLayer == curBaseLayer || w.mIsFloatingLayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008038 curLayer += WINDOW_LAYER_MULTIPLIER;
8039 w.mLayer = curLayer;
8040 } else {
8041 curBaseLayer = curLayer = w.mBaseLayer;
8042 w.mLayer = curLayer;
8043 }
8044 if (w.mTargetAppToken != null) {
8045 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8046 } else if (w.mAppToken != null) {
8047 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8048 } else {
8049 w.mAnimLayer = w.mLayer;
8050 }
8051 if (w.mIsImWindow) {
8052 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008053 } else if (w.mIsWallpaper) {
8054 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008055 }
8056 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8057 + w.mAnimLayer);
8058 //System.out.println(
8059 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8060 }
8061 }
8062
8063 private boolean mInLayout = false;
8064 private final void performLayoutAndPlaceSurfacesLocked() {
8065 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008066 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008067 throw new RuntimeException("Recursive call!");
8068 }
8069 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8070 return;
8071 }
8072
8073 boolean recoveringMemory = false;
8074 if (mForceRemoves != null) {
8075 recoveringMemory = true;
8076 // Wait a little it for things to settle down, and off we go.
8077 for (int i=0; i<mForceRemoves.size(); i++) {
8078 WindowState ws = mForceRemoves.get(i);
8079 Log.i(TAG, "Force removing: " + ws);
8080 removeWindowInnerLocked(ws.mSession, ws);
8081 }
8082 mForceRemoves = null;
8083 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8084 Object tmp = new Object();
8085 synchronized (tmp) {
8086 try {
8087 tmp.wait(250);
8088 } catch (InterruptedException e) {
8089 }
8090 }
8091 }
Romain Guy06882f82009-06-10 13:36:04 -07008092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008093 mInLayout = true;
8094 try {
8095 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008097 int i = mPendingRemove.size()-1;
8098 if (i >= 0) {
8099 while (i >= 0) {
8100 WindowState w = mPendingRemove.get(i);
8101 removeWindowInnerLocked(w.mSession, w);
8102 i--;
8103 }
8104 mPendingRemove.clear();
8105
8106 mInLayout = false;
8107 assignLayersLocked();
8108 mLayoutNeeded = true;
8109 performLayoutAndPlaceSurfacesLocked();
8110
8111 } else {
8112 mInLayout = false;
8113 if (mLayoutNeeded) {
8114 requestAnimationLocked(0);
8115 }
8116 }
8117 } catch (RuntimeException e) {
8118 mInLayout = false;
8119 Log.e(TAG, "Unhandled exception while layout out windows", e);
8120 }
8121 }
8122
8123 private final void performLayoutLockedInner() {
8124 final int dw = mDisplay.getWidth();
8125 final int dh = mDisplay.getHeight();
8126
8127 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008128 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 int i;
8130
8131 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008132
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008133 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008134 mPolicy.beginLayoutLw(dw, dh);
8135
8136 // First perform layout of any root windows (not attached
8137 // to another window).
8138 int topAttached = -1;
8139 for (i = N-1; i >= 0; i--) {
8140 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008141
8142 // Don't do layout of a window if it is not visible, or
8143 // soon won't be visible, to avoid wasting time and funky
8144 // changes while a window is animating away.
8145 final AppWindowToken atoken = win.mAppToken;
8146 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008147 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008148 || win.mRootToken.hidden
8149 || (atoken != null && atoken.hiddenRequested)
8150 || !win.mPolicyVisibility
8151 || win.mAttachedHidden
8152 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008153
8154 // If this view is GONE, then skip it -- keep the current
8155 // frame, and let the caller know so they can ignore it
8156 // if they want. (We do the normal layout for INVISIBLE
8157 // windows, since that means "perform layout as normal,
8158 // just don't display").
8159 if (!gone || !win.mHaveFrame) {
8160 if (!win.mLayoutAttached) {
8161 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8162 } else {
8163 if (topAttached < 0) topAttached = i;
8164 }
8165 }
8166 }
Romain Guy06882f82009-06-10 13:36:04 -07008167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008168 // Now perform layout of attached windows, which usually
8169 // depend on the position of the window they are attached to.
8170 // XXX does not deal with windows that are attached to windows
8171 // that are themselves attached.
8172 for (i = topAttached; i >= 0; i--) {
8173 WindowState win = (WindowState) mWindows.get(i);
8174
8175 // If this view is GONE, then skip it -- keep the current
8176 // frame, and let the caller know so they can ignore it
8177 // if they want. (We do the normal layout for INVISIBLE
8178 // windows, since that means "perform layout as normal,
8179 // just don't display").
8180 if (win.mLayoutAttached) {
8181 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8182 || !win.mHaveFrame) {
8183 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8184 }
8185 }
8186 }
8187
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008188 if (!mPolicy.finishLayoutLw()) {
8189 mLayoutNeeded = false;
8190 } else if (repeats > 2) {
8191 Log.w(TAG, "Layout repeat aborted after too many iterations");
8192 mLayoutNeeded = false;
8193 } else {
8194 repeats++;
8195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008196 }
8197 }
Romain Guy06882f82009-06-10 13:36:04 -07008198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008199 private final void performLayoutAndPlaceSurfacesLockedInner(
8200 boolean recoveringMemory) {
8201 final long currentTime = SystemClock.uptimeMillis();
8202 final int dw = mDisplay.getWidth();
8203 final int dh = mDisplay.getHeight();
8204
8205 final int N = mWindows.size();
8206 int i;
8207
8208 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008209 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211 if (mFxSession == null) {
8212 mFxSession = new SurfaceSession();
8213 }
Romain Guy06882f82009-06-10 13:36:04 -07008214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008215 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8216
8217 // Initialize state of exiting tokens.
8218 for (i=mExitingTokens.size()-1; i>=0; i--) {
8219 mExitingTokens.get(i).hasVisible = false;
8220 }
8221
8222 // Initialize state of exiting applications.
8223 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8224 mExitingAppTokens.get(i).hasVisible = false;
8225 }
8226
8227 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008228 boolean orientationChangeComplete = true;
8229 Session holdScreen = null;
8230 float screenBrightness = -1;
8231 boolean focusDisplayed = false;
8232 boolean animating = false;
8233
8234 Surface.openTransaction();
8235 try {
8236 boolean restart;
8237
8238 do {
8239 final int transactionSequence = ++mTransactionSequence;
8240
8241 // Update animations of all applications, including those
8242 // associated with exiting/removed apps
8243 boolean tokensAnimating = false;
8244 final int NAT = mAppTokens.size();
8245 for (i=0; i<NAT; i++) {
8246 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8247 tokensAnimating = true;
8248 }
8249 }
8250 final int NEAT = mExitingAppTokens.size();
8251 for (i=0; i<NEAT; i++) {
8252 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8253 tokensAnimating = true;
8254 }
8255 }
8256
8257 animating = tokensAnimating;
8258 restart = false;
8259
8260 boolean tokenMayBeDrawn = false;
8261
8262 mPolicy.beginAnimationLw(dw, dh);
8263
8264 for (i=N-1; i>=0; i--) {
8265 WindowState w = (WindowState)mWindows.get(i);
8266
8267 final WindowManager.LayoutParams attrs = w.mAttrs;
8268
8269 if (w.mSurface != null) {
8270 // Execute animation.
8271 w.commitFinishDrawingLocked(currentTime);
8272 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8273 animating = true;
8274 //w.dump(" ");
8275 }
8276
8277 mPolicy.animatingWindowLw(w, attrs);
8278 }
8279
8280 final AppWindowToken atoken = w.mAppToken;
8281 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8282 if (atoken.lastTransactionSequence != transactionSequence) {
8283 atoken.lastTransactionSequence = transactionSequence;
8284 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8285 atoken.startingDisplayed = false;
8286 }
8287 if ((w.isOnScreen() || w.mAttrs.type
8288 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8289 && !w.mExiting && !w.mDestroying) {
8290 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8291 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8292 + w.isDisplayedLw()
8293 + ", isAnimating=" + w.isAnimating());
8294 if (!w.isDisplayedLw()) {
8295 Log.v(TAG, "Not displayed: s=" + w.mSurface
8296 + " pv=" + w.mPolicyVisibility
8297 + " dp=" + w.mDrawPending
8298 + " cdp=" + w.mCommitDrawPending
8299 + " ah=" + w.mAttachedHidden
8300 + " th=" + atoken.hiddenRequested
8301 + " a=" + w.mAnimating);
8302 }
8303 }
8304 if (w != atoken.startingWindow) {
8305 if (!atoken.freezingScreen || !w.mAppFreezing) {
8306 atoken.numInterestingWindows++;
8307 if (w.isDisplayedLw()) {
8308 atoken.numDrawnWindows++;
8309 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8310 "tokenMayBeDrawn: " + atoken
8311 + " freezingScreen=" + atoken.freezingScreen
8312 + " mAppFreezing=" + w.mAppFreezing);
8313 tokenMayBeDrawn = true;
8314 }
8315 }
8316 } else if (w.isDisplayedLw()) {
8317 atoken.startingDisplayed = true;
8318 }
8319 }
8320 } else if (w.mReadyToShow) {
8321 w.performShowLocked();
8322 }
8323 }
8324
8325 if (mPolicy.finishAnimationLw()) {
8326 restart = true;
8327 }
8328
8329 if (tokenMayBeDrawn) {
8330 // See if any windows have been drawn, so they (and others
8331 // associated with them) can now be shown.
8332 final int NT = mTokenList.size();
8333 for (i=0; i<NT; i++) {
8334 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8335 if (wtoken == null) {
8336 continue;
8337 }
8338 if (wtoken.freezingScreen) {
8339 int numInteresting = wtoken.numInterestingWindows;
8340 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8341 if (DEBUG_VISIBILITY) Log.v(TAG,
8342 "allDrawn: " + wtoken
8343 + " interesting=" + numInteresting
8344 + " drawn=" + wtoken.numDrawnWindows);
8345 wtoken.showAllWindowsLocked();
8346 unsetAppFreezingScreenLocked(wtoken, false, true);
8347 orientationChangeComplete = true;
8348 }
8349 } else if (!wtoken.allDrawn) {
8350 int numInteresting = wtoken.numInterestingWindows;
8351 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8352 if (DEBUG_VISIBILITY) Log.v(TAG,
8353 "allDrawn: " + wtoken
8354 + " interesting=" + numInteresting
8355 + " drawn=" + wtoken.numDrawnWindows);
8356 wtoken.allDrawn = true;
8357 restart = true;
8358
8359 // We can now show all of the drawn windows!
8360 if (!mOpeningApps.contains(wtoken)) {
8361 wtoken.showAllWindowsLocked();
8362 }
8363 }
8364 }
8365 }
8366 }
8367
8368 // If we are ready to perform an app transition, check through
8369 // all of the app tokens to be shown and see if they are ready
8370 // to go.
8371 if (mAppTransitionReady) {
8372 int NN = mOpeningApps.size();
8373 boolean goodToGo = true;
8374 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8375 "Checking " + NN + " opening apps (frozen="
8376 + mDisplayFrozen + " timeout="
8377 + mAppTransitionTimeout + ")...");
8378 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8379 // If the display isn't frozen, wait to do anything until
8380 // all of the apps are ready. Otherwise just go because
8381 // we'll unfreeze the display when everyone is ready.
8382 for (i=0; i<NN && goodToGo; i++) {
8383 AppWindowToken wtoken = mOpeningApps.get(i);
8384 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8385 "Check opening app" + wtoken + ": allDrawn="
8386 + wtoken.allDrawn + " startingDisplayed="
8387 + wtoken.startingDisplayed);
8388 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8389 && !wtoken.startingMoved) {
8390 goodToGo = false;
8391 }
8392 }
8393 }
8394 if (goodToGo) {
8395 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8396 int transit = mNextAppTransition;
8397 if (mSkipAppTransitionAnimation) {
8398 transit = WindowManagerPolicy.TRANSIT_NONE;
8399 }
8400 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8401 mAppTransitionReady = false;
8402 mAppTransitionTimeout = false;
8403 mStartingIconInTransition = false;
8404 mSkipAppTransitionAnimation = false;
8405
8406 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8407
8408 // We need to figure out which animation to use...
8409 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8410 mOpeningApps, mClosingApps);
8411
8412 NN = mOpeningApps.size();
8413 for (i=0; i<NN; i++) {
8414 AppWindowToken wtoken = mOpeningApps.get(i);
8415 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8416 "Now opening app" + wtoken);
8417 wtoken.reportedVisible = false;
8418 wtoken.inPendingTransaction = false;
8419 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8420 wtoken.updateReportedVisibilityLocked();
8421 wtoken.showAllWindowsLocked();
8422 }
8423 NN = mClosingApps.size();
8424 for (i=0; i<NN; i++) {
8425 AppWindowToken wtoken = mClosingApps.get(i);
8426 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8427 "Now closing app" + wtoken);
8428 wtoken.inPendingTransaction = false;
8429 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8430 wtoken.updateReportedVisibilityLocked();
8431 // Force the allDrawn flag, because we want to start
8432 // this guy's animations regardless of whether it's
8433 // gotten drawn.
8434 wtoken.allDrawn = true;
8435 }
8436
8437 mOpeningApps.clear();
8438 mClosingApps.clear();
8439
8440 // This has changed the visibility of windows, so perform
8441 // a new layout to get them all up-to-date.
8442 mLayoutNeeded = true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008443 adjustWallpaperWindowsLocked();
Dianne Hackborn20583ff2009-07-27 21:51:05 -07008444 if (!moveInputMethodWindowsIfNeededLocked(true)) {
8445 assignLayersLocked();
8446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008447 performLayoutLockedInner();
8448 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8449
8450 restart = true;
8451 }
8452 }
8453 } while (restart);
8454
8455 // THIRD LOOP: Update the surfaces of all windows.
8456
8457 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8458
8459 boolean obscured = false;
8460 boolean blurring = false;
8461 boolean dimming = false;
8462 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008463 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008464 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008465
8466 for (i=N-1; i>=0; i--) {
8467 WindowState w = (WindowState)mWindows.get(i);
8468
8469 boolean displayed = false;
8470 final WindowManager.LayoutParams attrs = w.mAttrs;
8471 final int attrFlags = attrs.flags;
8472
8473 if (w.mSurface != null) {
8474 w.computeShownFrameLocked();
8475 if (localLOGV) Log.v(
8476 TAG, "Placing surface #" + i + " " + w.mSurface
8477 + ": new=" + w.mShownFrame + ", old="
8478 + w.mLastShownFrame);
8479
8480 boolean resize;
8481 int width, height;
8482 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8483 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8484 w.mLastRequestedHeight != w.mRequestedHeight;
8485 // for a scaled surface, we just want to use
8486 // the requested size.
8487 width = w.mRequestedWidth;
8488 height = w.mRequestedHeight;
8489 w.mLastRequestedWidth = width;
8490 w.mLastRequestedHeight = height;
8491 w.mLastShownFrame.set(w.mShownFrame);
8492 try {
8493 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8494 } catch (RuntimeException e) {
8495 Log.w(TAG, "Error positioning surface in " + w, e);
8496 if (!recoveringMemory) {
8497 reclaimSomeSurfaceMemoryLocked(w, "position");
8498 }
8499 }
8500 } else {
8501 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8502 width = w.mShownFrame.width();
8503 height = w.mShownFrame.height();
8504 w.mLastShownFrame.set(w.mShownFrame);
8505 if (resize) {
8506 if (SHOW_TRANSACTIONS) Log.i(
8507 TAG, " SURFACE " + w.mSurface + ": ("
8508 + w.mShownFrame.left + ","
8509 + w.mShownFrame.top + ") ("
8510 + w.mShownFrame.width() + "x"
8511 + w.mShownFrame.height() + ")");
8512 }
8513 }
8514
8515 if (resize) {
8516 if (width < 1) width = 1;
8517 if (height < 1) height = 1;
8518 if (w.mSurface != null) {
8519 try {
8520 w.mSurface.setSize(width, height);
8521 w.mSurface.setPosition(w.mShownFrame.left,
8522 w.mShownFrame.top);
8523 } catch (RuntimeException e) {
8524 // If something goes wrong with the surface (such
8525 // as running out of memory), don't take down the
8526 // entire system.
8527 Log.e(TAG, "Failure updating surface of " + w
8528 + "size=(" + width + "x" + height
8529 + "), pos=(" + w.mShownFrame.left
8530 + "," + w.mShownFrame.top + ")", e);
8531 if (!recoveringMemory) {
8532 reclaimSomeSurfaceMemoryLocked(w, "size");
8533 }
8534 }
8535 }
8536 }
8537 if (!w.mAppFreezing) {
8538 w.mContentInsetsChanged =
8539 !w.mLastContentInsets.equals(w.mContentInsets);
8540 w.mVisibleInsetsChanged =
8541 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008542 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 || w.mContentInsetsChanged
8544 || w.mVisibleInsetsChanged) {
8545 w.mLastFrame.set(w.mFrame);
8546 w.mLastContentInsets.set(w.mContentInsets);
8547 w.mLastVisibleInsets.set(w.mVisibleInsets);
8548 // If the orientation is changing, then we need to
8549 // hold off on unfreezing the display until this
8550 // window has been redrawn; to do that, we need
8551 // to go through the process of getting informed
8552 // by the application when it has finished drawing.
8553 if (w.mOrientationChanging) {
8554 if (DEBUG_ORIENTATION) Log.v(TAG,
8555 "Orientation start waiting for draw in "
8556 + w + ", surface " + w.mSurface);
8557 w.mDrawPending = true;
8558 w.mCommitDrawPending = false;
8559 w.mReadyToShow = false;
8560 if (w.mAppToken != null) {
8561 w.mAppToken.allDrawn = false;
8562 }
8563 }
Romain Guy06882f82009-06-10 13:36:04 -07008564 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008565 "Resizing window " + w + " to " + w.mFrame);
8566 mResizingWindows.add(w);
8567 } else if (w.mOrientationChanging) {
8568 if (!w.mDrawPending && !w.mCommitDrawPending) {
8569 if (DEBUG_ORIENTATION) Log.v(TAG,
8570 "Orientation not waiting for draw in "
8571 + w + ", surface " + w.mSurface);
8572 w.mOrientationChanging = false;
8573 }
8574 }
8575 }
8576
8577 if (w.mAttachedHidden) {
8578 if (!w.mLastHidden) {
8579 //dump();
8580 w.mLastHidden = true;
8581 if (SHOW_TRANSACTIONS) Log.i(
8582 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8583 if (w.mSurface != null) {
8584 try {
8585 w.mSurface.hide();
8586 } catch (RuntimeException e) {
8587 Log.w(TAG, "Exception hiding surface in " + w);
8588 }
8589 }
8590 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8591 }
8592 // If we are waiting for this window to handle an
8593 // orientation change, well, it is hidden, so
8594 // doesn't really matter. Note that this does
8595 // introduce a potential glitch if the window
8596 // becomes unhidden before it has drawn for the
8597 // new orientation.
8598 if (w.mOrientationChanging) {
8599 w.mOrientationChanging = false;
8600 if (DEBUG_ORIENTATION) Log.v(TAG,
8601 "Orientation change skips hidden " + w);
8602 }
8603 } else if (!w.isReadyForDisplay()) {
8604 if (!w.mLastHidden) {
8605 //dump();
8606 w.mLastHidden = true;
8607 if (SHOW_TRANSACTIONS) Log.i(
8608 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8609 if (w.mSurface != null) {
8610 try {
8611 w.mSurface.hide();
8612 } catch (RuntimeException e) {
8613 Log.w(TAG, "Exception exception hiding surface in " + w);
8614 }
8615 }
8616 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8617 }
8618 // If we are waiting for this window to handle an
8619 // orientation change, well, it is hidden, so
8620 // doesn't really matter. Note that this does
8621 // introduce a potential glitch if the window
8622 // becomes unhidden before it has drawn for the
8623 // new orientation.
8624 if (w.mOrientationChanging) {
8625 w.mOrientationChanging = false;
8626 if (DEBUG_ORIENTATION) Log.v(TAG,
8627 "Orientation change skips hidden " + w);
8628 }
8629 } else if (w.mLastLayer != w.mAnimLayer
8630 || w.mLastAlpha != w.mShownAlpha
8631 || w.mLastDsDx != w.mDsDx
8632 || w.mLastDtDx != w.mDtDx
8633 || w.mLastDsDy != w.mDsDy
8634 || w.mLastDtDy != w.mDtDy
8635 || w.mLastHScale != w.mHScale
8636 || w.mLastVScale != w.mVScale
8637 || w.mLastHidden) {
8638 displayed = true;
8639 w.mLastAlpha = w.mShownAlpha;
8640 w.mLastLayer = w.mAnimLayer;
8641 w.mLastDsDx = w.mDsDx;
8642 w.mLastDtDx = w.mDtDx;
8643 w.mLastDsDy = w.mDsDy;
8644 w.mLastDtDy = w.mDtDy;
8645 w.mLastHScale = w.mHScale;
8646 w.mLastVScale = w.mVScale;
8647 if (SHOW_TRANSACTIONS) Log.i(
8648 TAG, " SURFACE " + w.mSurface + ": alpha="
8649 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8650 if (w.mSurface != null) {
8651 try {
8652 w.mSurface.setAlpha(w.mShownAlpha);
8653 w.mSurface.setLayer(w.mAnimLayer);
8654 w.mSurface.setMatrix(
8655 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8656 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8657 } catch (RuntimeException e) {
8658 Log.w(TAG, "Error updating surface in " + w, e);
8659 if (!recoveringMemory) {
8660 reclaimSomeSurfaceMemoryLocked(w, "update");
8661 }
8662 }
8663 }
8664
8665 if (w.mLastHidden && !w.mDrawPending
8666 && !w.mCommitDrawPending
8667 && !w.mReadyToShow) {
8668 if (SHOW_TRANSACTIONS) Log.i(
8669 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8670 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8671 + " during relayout");
8672 if (showSurfaceRobustlyLocked(w)) {
8673 w.mHasDrawn = true;
8674 w.mLastHidden = false;
8675 } else {
8676 w.mOrientationChanging = false;
8677 }
8678 }
8679 if (w.mSurface != null) {
8680 w.mToken.hasVisible = true;
8681 }
8682 } else {
8683 displayed = true;
8684 }
8685
8686 if (displayed) {
8687 if (!covered) {
8688 if (attrs.width == LayoutParams.FILL_PARENT
8689 && attrs.height == LayoutParams.FILL_PARENT) {
8690 covered = true;
8691 }
8692 }
8693 if (w.mOrientationChanging) {
8694 if (w.mDrawPending || w.mCommitDrawPending) {
8695 orientationChangeComplete = false;
8696 if (DEBUG_ORIENTATION) Log.v(TAG,
8697 "Orientation continue waiting for draw in " + w);
8698 } else {
8699 w.mOrientationChanging = false;
8700 if (DEBUG_ORIENTATION) Log.v(TAG,
8701 "Orientation change complete in " + w);
8702 }
8703 }
8704 w.mToken.hasVisible = true;
8705 }
8706 } else if (w.mOrientationChanging) {
8707 if (DEBUG_ORIENTATION) Log.v(TAG,
8708 "Orientation change skips hidden " + w);
8709 w.mOrientationChanging = false;
8710 }
8711
8712 final boolean canBeSeen = w.isDisplayedLw();
8713
8714 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8715 focusDisplayed = true;
8716 }
8717
8718 // Update effect.
8719 if (!obscured) {
8720 if (w.mSurface != null) {
8721 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8722 holdScreen = w.mSession;
8723 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008724 if (!syswin && w.mAttrs.screenBrightness >= 0
8725 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008726 screenBrightness = w.mAttrs.screenBrightness;
8727 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008728 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8729 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8730 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8731 syswin = true;
8732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008733 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008734
8735 boolean opaqueDrawn = w.isOpaqueDrawn();
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008736 if ((opaqueDrawn && w.isFullscreen(dw, dh))
8737 || attrs.type == TYPE_WALLPAPER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008738 // This window completely covers everything behind it,
8739 // so we want to leave all of them as unblurred (for
8740 // performance reasons).
8741 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008742 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8743 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008744 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008745 obscured = true;
8746 if (mBackgroundFillerSurface == null) {
8747 try {
8748 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8749 0, dw, dh,
8750 PixelFormat.OPAQUE,
8751 Surface.FX_SURFACE_NORMAL);
8752 } catch (Exception e) {
8753 Log.e(TAG, "Exception creating filler surface", e);
8754 }
8755 }
8756 try {
8757 mBackgroundFillerSurface.setPosition(0, 0);
8758 mBackgroundFillerSurface.setSize(dw, dh);
8759 // Using the same layer as Dim because they will never be shown at the
8760 // same time.
8761 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8762 mBackgroundFillerSurface.show();
8763 } catch (RuntimeException e) {
8764 Log.e(TAG, "Exception showing filler surface");
8765 }
8766 backgroundFillerShown = true;
8767 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008768 } else if (canBeSeen && !obscured &&
8769 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8770 if (localLOGV) Log.v(TAG, "Win " + w
8771 + ": blurring=" + blurring
8772 + " obscured=" + obscured
8773 + " displayed=" + displayed);
8774 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8775 if (!dimming) {
8776 //Log.i(TAG, "DIM BEHIND: " + w);
8777 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008778 if (mDimAnimator == null) {
8779 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008780 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008781 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008782 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008783 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008784 }
8785 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8786 if (!blurring) {
8787 //Log.i(TAG, "BLUR BEHIND: " + w);
8788 blurring = true;
8789 mBlurShown = true;
8790 if (mBlurSurface == null) {
8791 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8792 + mBlurSurface + ": CREATE");
8793 try {
Romain Guy06882f82009-06-10 13:36:04 -07008794 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008795 -1, 16, 16,
8796 PixelFormat.OPAQUE,
8797 Surface.FX_SURFACE_BLUR);
8798 } catch (Exception e) {
8799 Log.e(TAG, "Exception creating Blur surface", e);
8800 }
8801 }
8802 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8803 + mBlurSurface + ": SHOW pos=(0,0) (" +
8804 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8805 if (mBlurSurface != null) {
8806 mBlurSurface.setPosition(0, 0);
8807 mBlurSurface.setSize(dw, dh);
8808 try {
8809 mBlurSurface.show();
8810 } catch (RuntimeException e) {
8811 Log.w(TAG, "Failure showing blur surface", e);
8812 }
8813 }
8814 }
8815 mBlurSurface.setLayer(w.mAnimLayer-2);
8816 }
8817 }
8818 }
8819 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008820
8821 if (backgroundFillerShown == false && mBackgroundFillerShown) {
8822 mBackgroundFillerShown = false;
8823 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
8824 try {
8825 mBackgroundFillerSurface.hide();
8826 } catch (RuntimeException e) {
8827 Log.e(TAG, "Exception hiding filler surface", e);
8828 }
8829 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008830
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008831 if (mDimAnimator != null && mDimAnimator.mDimShown) {
8832 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008833 }
Romain Guy06882f82009-06-10 13:36:04 -07008834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008835 if (!blurring && mBlurShown) {
8836 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8837 + ": HIDE");
8838 try {
8839 mBlurSurface.hide();
8840 } catch (IllegalArgumentException e) {
8841 Log.w(TAG, "Illegal argument exception hiding blur surface");
8842 }
8843 mBlurShown = false;
8844 }
8845
8846 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8847 } catch (RuntimeException e) {
8848 Log.e(TAG, "Unhandled exception in Window Manager", e);
8849 }
8850
8851 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8854 "With display frozen, orientationChangeComplete="
8855 + orientationChangeComplete);
8856 if (orientationChangeComplete) {
8857 if (mWindowsFreezingScreen) {
8858 mWindowsFreezingScreen = false;
8859 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8860 }
8861 if (mAppsFreezingScreen == 0) {
8862 stopFreezingDisplayLocked();
8863 }
8864 }
Romain Guy06882f82009-06-10 13:36:04 -07008865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008866 i = mResizingWindows.size();
8867 if (i > 0) {
8868 do {
8869 i--;
8870 WindowState win = mResizingWindows.get(i);
8871 try {
8872 win.mClient.resized(win.mFrame.width(),
8873 win.mFrame.height(), win.mLastContentInsets,
8874 win.mLastVisibleInsets, win.mDrawPending);
8875 win.mContentInsetsChanged = false;
8876 win.mVisibleInsetsChanged = false;
8877 } catch (RemoteException e) {
8878 win.mOrientationChanging = false;
8879 }
8880 } while (i > 0);
8881 mResizingWindows.clear();
8882 }
Romain Guy06882f82009-06-10 13:36:04 -07008883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008884 // Destroy the surface of any windows that are no longer visible.
8885 i = mDestroySurface.size();
8886 if (i > 0) {
8887 do {
8888 i--;
8889 WindowState win = mDestroySurface.get(i);
8890 win.mDestroying = false;
8891 if (mInputMethodWindow == win) {
8892 mInputMethodWindow = null;
8893 }
8894 win.destroySurfaceLocked();
8895 } while (i > 0);
8896 mDestroySurface.clear();
8897 }
8898
8899 // Time to remove any exiting tokens?
8900 for (i=mExitingTokens.size()-1; i>=0; i--) {
8901 WindowToken token = mExitingTokens.get(i);
8902 if (!token.hasVisible) {
8903 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008904 if (token.windowType == TYPE_WALLPAPER) {
8905 mWallpaperTokens.remove(token);
8906 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008907 }
8908 }
8909
8910 // Time to remove any exiting applications?
8911 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8912 AppWindowToken token = mExitingAppTokens.get(i);
8913 if (!token.hasVisible && !mClosingApps.contains(token)) {
8914 mAppTokens.remove(token);
8915 mExitingAppTokens.remove(i);
8916 }
8917 }
8918
8919 if (focusDisplayed) {
8920 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8921 }
8922 if (animating) {
8923 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8924 }
8925 mQueue.setHoldScreenLocked(holdScreen != null);
8926 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8927 mPowerManager.setScreenBrightnessOverride(-1);
8928 } else {
8929 mPowerManager.setScreenBrightnessOverride((int)
8930 (screenBrightness * Power.BRIGHTNESS_ON));
8931 }
8932 if (holdScreen != mHoldingScreenOn) {
8933 mHoldingScreenOn = holdScreen;
8934 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8935 mH.sendMessage(m);
8936 }
8937 }
8938
8939 void requestAnimationLocked(long delay) {
8940 if (!mAnimationPending) {
8941 mAnimationPending = true;
8942 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8943 }
8944 }
Romain Guy06882f82009-06-10 13:36:04 -07008945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008946 /**
8947 * Have the surface flinger show a surface, robustly dealing with
8948 * error conditions. In particular, if there is not enough memory
8949 * to show the surface, then we will try to get rid of other surfaces
8950 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07008951 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 * @return Returns true if the surface was successfully shown.
8953 */
8954 boolean showSurfaceRobustlyLocked(WindowState win) {
8955 try {
8956 if (win.mSurface != null) {
8957 win.mSurface.show();
8958 }
8959 return true;
8960 } catch (RuntimeException e) {
8961 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8962 }
Romain Guy06882f82009-06-10 13:36:04 -07008963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008964 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07008965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008966 return false;
8967 }
Romain Guy06882f82009-06-10 13:36:04 -07008968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008969 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8970 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07008971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008972 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8973 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07008974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008975 if (mForceRemoves == null) {
8976 mForceRemoves = new ArrayList<WindowState>();
8977 }
Romain Guy06882f82009-06-10 13:36:04 -07008978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008979 long callingIdentity = Binder.clearCallingIdentity();
8980 try {
8981 // There was some problem... first, do a sanity check of the
8982 // window list to make sure we haven't left any dangling surfaces
8983 // around.
8984 int N = mWindows.size();
8985 boolean leakedSurface = false;
8986 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8987 for (int i=0; i<N; i++) {
8988 WindowState ws = (WindowState)mWindows.get(i);
8989 if (ws.mSurface != null) {
8990 if (!mSessions.contains(ws.mSession)) {
8991 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8992 + ws + " surface=" + ws.mSurface
8993 + " token=" + win.mToken
8994 + " pid=" + ws.mSession.mPid
8995 + " uid=" + ws.mSession.mUid);
8996 ws.mSurface.clear();
8997 ws.mSurface = null;
8998 mForceRemoves.add(ws);
8999 i--;
9000 N--;
9001 leakedSurface = true;
9002 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9003 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9004 + ws + " surface=" + ws.mSurface
9005 + " token=" + win.mAppToken);
9006 ws.mSurface.clear();
9007 ws.mSurface = null;
9008 leakedSurface = true;
9009 }
9010 }
9011 }
Romain Guy06882f82009-06-10 13:36:04 -07009012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009013 boolean killedApps = false;
9014 if (!leakedSurface) {
9015 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9016 SparseIntArray pidCandidates = new SparseIntArray();
9017 for (int i=0; i<N; i++) {
9018 WindowState ws = (WindowState)mWindows.get(i);
9019 if (ws.mSurface != null) {
9020 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9021 }
9022 }
9023 if (pidCandidates.size() > 0) {
9024 int[] pids = new int[pidCandidates.size()];
9025 for (int i=0; i<pids.length; i++) {
9026 pids[i] = pidCandidates.keyAt(i);
9027 }
9028 try {
9029 if (mActivityManager.killPidsForMemory(pids)) {
9030 killedApps = true;
9031 }
9032 } catch (RemoteException e) {
9033 }
9034 }
9035 }
Romain Guy06882f82009-06-10 13:36:04 -07009036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009037 if (leakedSurface || killedApps) {
9038 // We managed to reclaim some memory, so get rid of the trouble
9039 // surface and ask the app to request another one.
9040 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9041 if (surface != null) {
9042 surface.clear();
9043 win.mSurface = null;
9044 }
Romain Guy06882f82009-06-10 13:36:04 -07009045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009046 try {
9047 win.mClient.dispatchGetNewSurface();
9048 } catch (RemoteException e) {
9049 }
9050 }
9051 } finally {
9052 Binder.restoreCallingIdentity(callingIdentity);
9053 }
9054 }
Romain Guy06882f82009-06-10 13:36:04 -07009055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009056 private boolean updateFocusedWindowLocked(int mode) {
9057 WindowState newFocus = computeFocusedWindowLocked();
9058 if (mCurrentFocus != newFocus) {
9059 // This check makes sure that we don't already have the focus
9060 // change message pending.
9061 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9062 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9063 if (localLOGV) Log.v(
9064 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9065 final WindowState oldFocus = mCurrentFocus;
9066 mCurrentFocus = newFocus;
9067 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009069 final WindowState imWindow = mInputMethodWindow;
9070 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009071 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009072 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009073 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9074 mLayoutNeeded = true;
9075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009076 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9077 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009078 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9079 // Client will do the layout, but we need to assign layers
9080 // for handleNewWindowLocked() below.
9081 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082 }
9083 }
Romain Guy06882f82009-06-10 13:36:04 -07009084
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009085 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9086 mKeyWaiter.handleNewWindowLocked(newFocus);
9087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009088 return true;
9089 }
9090 return false;
9091 }
9092
9093 private WindowState computeFocusedWindowLocked() {
9094 WindowState result = null;
9095 WindowState win;
9096
9097 int i = mWindows.size() - 1;
9098 int nextAppIndex = mAppTokens.size()-1;
9099 WindowToken nextApp = nextAppIndex >= 0
9100 ? mAppTokens.get(nextAppIndex) : null;
9101
9102 while (i >= 0) {
9103 win = (WindowState)mWindows.get(i);
9104
9105 if (localLOGV || DEBUG_FOCUS) Log.v(
9106 TAG, "Looking for focus: " + i
9107 + " = " + win
9108 + ", flags=" + win.mAttrs.flags
9109 + ", canReceive=" + win.canReceiveKeys());
9110
9111 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009113 // If this window's application has been removed, just skip it.
9114 if (thisApp != null && thisApp.removed) {
9115 i--;
9116 continue;
9117 }
Romain Guy06882f82009-06-10 13:36:04 -07009118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009119 // If there is a focused app, don't allow focus to go to any
9120 // windows below it. If this is an application window, step
9121 // through the app tokens until we find its app.
9122 if (thisApp != null && nextApp != null && thisApp != nextApp
9123 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9124 int origAppIndex = nextAppIndex;
9125 while (nextAppIndex > 0) {
9126 if (nextApp == mFocusedApp) {
9127 // Whoops, we are below the focused app... no focus
9128 // for you!
9129 if (localLOGV || DEBUG_FOCUS) Log.v(
9130 TAG, "Reached focused app: " + mFocusedApp);
9131 return null;
9132 }
9133 nextAppIndex--;
9134 nextApp = mAppTokens.get(nextAppIndex);
9135 if (nextApp == thisApp) {
9136 break;
9137 }
9138 }
9139 if (thisApp != nextApp) {
9140 // Uh oh, the app token doesn't exist! This shouldn't
9141 // happen, but if it does we can get totally hosed...
9142 // so restart at the original app.
9143 nextAppIndex = origAppIndex;
9144 nextApp = mAppTokens.get(nextAppIndex);
9145 }
9146 }
9147
9148 // Dispatch to this window if it is wants key events.
9149 if (win.canReceiveKeys()) {
9150 if (DEBUG_FOCUS) Log.v(
9151 TAG, "Found focus @ " + i + " = " + win);
9152 result = win;
9153 break;
9154 }
9155
9156 i--;
9157 }
9158
9159 return result;
9160 }
9161
9162 private void startFreezingDisplayLocked() {
9163 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009164 // Freezing the display also suspends key event delivery, to
9165 // keep events from going astray while the display is reconfigured.
9166 // If someone has changed orientation again while the screen is
9167 // still frozen, the events will continue to be blocked while the
9168 // successive orientation change is processed. To prevent spurious
9169 // ANRs, we reset the event dispatch timeout in this case.
9170 synchronized (mKeyWaiter) {
9171 mKeyWaiter.mWasFrozen = true;
9172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 return;
9174 }
Romain Guy06882f82009-06-10 13:36:04 -07009175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009176 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009178 long now = SystemClock.uptimeMillis();
9179 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9180 if (mFreezeGcPending != 0) {
9181 if (now > (mFreezeGcPending+1000)) {
9182 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9183 mH.removeMessages(H.FORCE_GC);
9184 Runtime.getRuntime().gc();
9185 mFreezeGcPending = now;
9186 }
9187 } else {
9188 mFreezeGcPending = now;
9189 }
Romain Guy06882f82009-06-10 13:36:04 -07009190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009191 mDisplayFrozen = true;
9192 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9193 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9194 mAppTransitionReady = true;
9195 }
Romain Guy06882f82009-06-10 13:36:04 -07009196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009197 if (PROFILE_ORIENTATION) {
9198 File file = new File("/data/system/frozen");
9199 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9200 }
9201 Surface.freezeDisplay(0);
9202 }
Romain Guy06882f82009-06-10 13:36:04 -07009203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009204 private void stopFreezingDisplayLocked() {
9205 if (!mDisplayFrozen) {
9206 return;
9207 }
Romain Guy06882f82009-06-10 13:36:04 -07009208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009209 mDisplayFrozen = false;
9210 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9211 if (PROFILE_ORIENTATION) {
9212 Debug.stopMethodTracing();
9213 }
9214 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009215
Chris Tate2ad63a92009-03-25 17:36:48 -07009216 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9217 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009218 synchronized (mKeyWaiter) {
9219 mKeyWaiter.mWasFrozen = true;
9220 mKeyWaiter.notifyAll();
9221 }
9222
9223 // A little kludge: a lot could have happened while the
9224 // display was frozen, so now that we are coming back we
9225 // do a gc so that any remote references the system
9226 // processes holds on others can be released if they are
9227 // no longer needed.
9228 mH.removeMessages(H.FORCE_GC);
9229 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9230 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009232 mScreenFrozenLock.release();
9233 }
Romain Guy06882f82009-06-10 13:36:04 -07009234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009235 @Override
9236 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9237 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9238 != PackageManager.PERMISSION_GRANTED) {
9239 pw.println("Permission Denial: can't dump WindowManager from from pid="
9240 + Binder.getCallingPid()
9241 + ", uid=" + Binder.getCallingUid());
9242 return;
9243 }
Romain Guy06882f82009-06-10 13:36:04 -07009244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009245 synchronized(mWindowMap) {
9246 pw.println("Current Window Manager state:");
9247 for (int i=mWindows.size()-1; i>=0; i--) {
9248 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009249 pw.print(" Window #"); pw.print(i); pw.print(' ');
9250 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009251 w.dump(pw, " ");
9252 }
9253 if (mInputMethodDialogs.size() > 0) {
9254 pw.println(" ");
9255 pw.println(" Input method dialogs:");
9256 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9257 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009258 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009259 }
9260 }
9261 if (mPendingRemove.size() > 0) {
9262 pw.println(" ");
9263 pw.println(" Remove pending for:");
9264 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9265 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009266 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9267 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009268 w.dump(pw, " ");
9269 }
9270 }
9271 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9272 pw.println(" ");
9273 pw.println(" Windows force removing:");
9274 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9275 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009276 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9277 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009278 w.dump(pw, " ");
9279 }
9280 }
9281 if (mDestroySurface.size() > 0) {
9282 pw.println(" ");
9283 pw.println(" Windows waiting to destroy their surface:");
9284 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9285 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009286 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9287 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009288 w.dump(pw, " ");
9289 }
9290 }
9291 if (mLosingFocus.size() > 0) {
9292 pw.println(" ");
9293 pw.println(" Windows losing focus:");
9294 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9295 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009296 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9297 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009298 w.dump(pw, " ");
9299 }
9300 }
9301 if (mSessions.size() > 0) {
9302 pw.println(" ");
9303 pw.println(" All active sessions:");
9304 Iterator<Session> it = mSessions.iterator();
9305 while (it.hasNext()) {
9306 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009307 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009308 s.dump(pw, " ");
9309 }
9310 }
9311 if (mTokenMap.size() > 0) {
9312 pw.println(" ");
9313 pw.println(" All tokens:");
9314 Iterator<WindowToken> it = mTokenMap.values().iterator();
9315 while (it.hasNext()) {
9316 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009317 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009318 token.dump(pw, " ");
9319 }
9320 }
9321 if (mTokenList.size() > 0) {
9322 pw.println(" ");
9323 pw.println(" Window token list:");
9324 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009325 pw.print(" #"); pw.print(i); pw.print(": ");
9326 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 }
9328 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009329 if (mWallpaperTokens.size() > 0) {
9330 pw.println(" ");
9331 pw.println(" Wallpaper tokens:");
9332 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
9333 WindowToken token = mWallpaperTokens.get(i);
9334 pw.print(" Wallpaper #"); pw.print(i);
9335 pw.print(' '); pw.print(token); pw.println(':');
9336 token.dump(pw, " ");
9337 }
9338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339 if (mAppTokens.size() > 0) {
9340 pw.println(" ");
9341 pw.println(" Application tokens in Z order:");
9342 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009343 pw.print(" App #"); pw.print(i); pw.print(": ");
9344 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009345 }
9346 }
9347 if (mFinishedStarting.size() > 0) {
9348 pw.println(" ");
9349 pw.println(" Finishing start of application tokens:");
9350 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9351 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009352 pw.print(" Finished Starting #"); pw.print(i);
9353 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009354 token.dump(pw, " ");
9355 }
9356 }
9357 if (mExitingTokens.size() > 0) {
9358 pw.println(" ");
9359 pw.println(" Exiting tokens:");
9360 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9361 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009362 pw.print(" Exiting #"); pw.print(i);
9363 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009364 token.dump(pw, " ");
9365 }
9366 }
9367 if (mExitingAppTokens.size() > 0) {
9368 pw.println(" ");
9369 pw.println(" Exiting application tokens:");
9370 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9371 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009372 pw.print(" Exiting App #"); pw.print(i);
9373 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009374 token.dump(pw, " ");
9375 }
9376 }
9377 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009378 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9379 pw.print(" mLastFocus="); pw.println(mLastFocus);
9380 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9381 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9382 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9383 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9384 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9385 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9386 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9387 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009388 if (mDimAnimator != null) {
9389 mDimAnimator.printTo(pw);
9390 } else {
9391 pw.print( " no DimAnimator ");
9392 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009393 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009394 pw.print(mInputMethodAnimLayerAdjustment);
9395 pw.print(" mWallpaperAnimLayerAdjustment=");
9396 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009397 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9398 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9399 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9400 pw.print(" mRotation="); pw.print(mRotation);
9401 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9402 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9403 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9404 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9405 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9406 pw.print(" mNextAppTransition=0x");
9407 pw.print(Integer.toHexString(mNextAppTransition));
9408 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9409 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9410 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9411 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9412 if (mOpeningApps.size() > 0) {
9413 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9414 }
9415 if (mClosingApps.size() > 0) {
9416 pw.print(" mClosingApps="); pw.println(mClosingApps);
9417 }
9418 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9419 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009420 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009421 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9422 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9423 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9424 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9425 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9426 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 }
9428 }
9429
9430 public void monitor() {
9431 synchronized (mWindowMap) { }
9432 synchronized (mKeyguardDisabled) { }
9433 synchronized (mKeyWaiter) { }
9434 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009435
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07009436 public void virtualKeyFeedback(KeyEvent event) {
9437 mPolicy.keyFeedbackFromInput(event);
9438 }
9439
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009440 /**
9441 * DimAnimator class that controls the dim animation. This holds the surface and
9442 * all state used for dim animation.
9443 */
9444 private static class DimAnimator {
9445 Surface mDimSurface;
9446 boolean mDimShown = false;
9447 float mDimCurrentAlpha;
9448 float mDimTargetAlpha;
9449 float mDimDeltaPerMs;
9450 long mLastDimAnimTime;
9451
9452 DimAnimator (SurfaceSession session) {
9453 if (mDimSurface == null) {
9454 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9455 + mDimSurface + ": CREATE");
9456 try {
9457 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9458 Surface.FX_SURFACE_DIM);
9459 } catch (Exception e) {
9460 Log.e(TAG, "Exception creating Dim surface", e);
9461 }
9462 }
9463 }
9464
9465 /**
9466 * Show the dim surface.
9467 */
9468 void show(int dw, int dh) {
9469 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9470 dw + "x" + dh + ")");
9471 mDimShown = true;
9472 try {
9473 mDimSurface.setPosition(0, 0);
9474 mDimSurface.setSize(dw, dh);
9475 mDimSurface.show();
9476 } catch (RuntimeException e) {
9477 Log.w(TAG, "Failure showing dim surface", e);
9478 }
9479 }
9480
9481 /**
9482 * Set's the dim surface's layer and update dim parameters that will be used in
9483 * {@link updateSurface} after all windows are examined.
9484 */
9485 void updateParameters(WindowState w, long currentTime) {
9486 mDimSurface.setLayer(w.mAnimLayer-1);
9487
9488 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9489 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9490 if (mDimTargetAlpha != target) {
9491 // If the desired dim level has changed, then
9492 // start an animation to it.
9493 mLastDimAnimTime = currentTime;
9494 long duration = (w.mAnimating && w.mAnimation != null)
9495 ? w.mAnimation.computeDurationHint()
9496 : DEFAULT_DIM_DURATION;
9497 if (target > mDimTargetAlpha) {
9498 // This is happening behind the activity UI,
9499 // so we can make it run a little longer to
9500 // give a stronger impression without disrupting
9501 // the user.
9502 duration *= DIM_DURATION_MULTIPLIER;
9503 }
9504 if (duration < 1) {
9505 // Don't divide by zero
9506 duration = 1;
9507 }
9508 mDimTargetAlpha = target;
9509 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9510 }
9511 }
9512
9513 /**
9514 * Updating the surface's alpha. Returns true if the animation continues, or returns
9515 * false when the animation is finished and the dim surface is hidden.
9516 */
9517 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9518 if (!dimming) {
9519 if (mDimTargetAlpha != 0) {
9520 mLastDimAnimTime = currentTime;
9521 mDimTargetAlpha = 0;
9522 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9523 }
9524 }
9525
9526 boolean animating = false;
9527 if (mLastDimAnimTime != 0) {
9528 mDimCurrentAlpha += mDimDeltaPerMs
9529 * (currentTime-mLastDimAnimTime);
9530 boolean more = true;
9531 if (displayFrozen) {
9532 // If the display is frozen, there is no reason to animate.
9533 more = false;
9534 } else if (mDimDeltaPerMs > 0) {
9535 if (mDimCurrentAlpha > mDimTargetAlpha) {
9536 more = false;
9537 }
9538 } else if (mDimDeltaPerMs < 0) {
9539 if (mDimCurrentAlpha < mDimTargetAlpha) {
9540 more = false;
9541 }
9542 } else {
9543 more = false;
9544 }
9545
9546 // Do we need to continue animating?
9547 if (more) {
9548 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9549 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9550 mLastDimAnimTime = currentTime;
9551 mDimSurface.setAlpha(mDimCurrentAlpha);
9552 animating = true;
9553 } else {
9554 mDimCurrentAlpha = mDimTargetAlpha;
9555 mLastDimAnimTime = 0;
9556 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9557 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9558 mDimSurface.setAlpha(mDimCurrentAlpha);
9559 if (!dimming) {
9560 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9561 + ": HIDE");
9562 try {
9563 mDimSurface.hide();
9564 } catch (RuntimeException e) {
9565 Log.w(TAG, "Illegal argument exception hiding dim surface");
9566 }
9567 mDimShown = false;
9568 }
9569 }
9570 }
9571 return animating;
9572 }
9573
9574 public void printTo(PrintWriter pw) {
9575 pw.print(" mDimShown="); pw.print(mDimShown);
9576 pw.print(" current="); pw.print(mDimCurrentAlpha);
9577 pw.print(" target="); pw.print(mDimTargetAlpha);
9578 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9579 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9580 }
9581 }
9582
9583 /**
9584 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9585 * This is used for opening/closing transition for apps in compatible mode.
9586 */
9587 private static class FadeInOutAnimation extends Animation {
9588 int mWidth;
9589 boolean mFadeIn;
9590
9591 public FadeInOutAnimation(boolean fadeIn) {
9592 setInterpolator(new AccelerateInterpolator());
9593 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9594 mFadeIn = fadeIn;
9595 }
9596
9597 @Override
9598 protected void applyTransformation(float interpolatedTime, Transformation t) {
9599 float x = interpolatedTime;
9600 if (!mFadeIn) {
9601 x = 1.0f - x; // reverse the interpolation for fade out
9602 }
9603 if (x < 0.5) {
9604 // move the window out of the screen.
9605 t.getMatrix().setTranslate(mWidth, 0);
9606 } else {
9607 t.getMatrix().setTranslate(0, 0);// show
9608 t.setAlpha((x - 0.5f) * 2);
9609 }
9610 }
9611
9612 @Override
9613 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9614 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9615 mWidth = width;
9616 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009617
9618 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07009619 public int getZAdjustment() {
9620 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009621 }
9622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009623}