blob: 641f251da5eb7042b12aef28aa733362d0ffd0c1 [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
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001226 final int dw = mDisplay.getWidth();
1227 final int dh = mDisplay.getHeight();
1228
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001229 // Start stepping backwards from here, ensuring that our wallpaper windows
1230 // are correctly placed.
1231 int curTokenIndex = mWallpaperTokens.size();
1232 while (curTokenIndex > 0) {
1233 curTokenIndex--;
1234 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1235 int curWallpaperIndex = token.windows.size();
1236 while (curWallpaperIndex > 0) {
1237 curWallpaperIndex--;
1238 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001239
1240 // First, make sure the client has the current visibility
1241 // state.
1242 if (wallpaper.mWallpaperVisible != visible) {
1243 wallpaper.mWallpaperVisible = visible;
1244 try {
1245 if (DEBUG_VISIBILITY) Log.v(TAG,
1246 "Setting visibility of wallpaper " + wallpaper
1247 + ": " + visible);
1248 wallpaper.mClient.dispatchAppVisibility(visible);
1249 } catch (RemoteException e) {
1250 }
1251 }
1252
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001253 if (visible) {
1254 updateWallpaperOffsetLocked(mWallpaperTarget, wallpaper, dw, dh);
1255 }
1256
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001257 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
1258 if (DEBUG_LAYERS) Log.v(TAG, "Wallpaper win " + wallpaper
1259 + " anim layer: " + wallpaper.mAnimLayer);
1260
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001261 // First, if this window is at the current index, then all
1262 // is well.
1263 if (wallpaper == w) {
1264 i--;
1265 w = i > 0 ? (WindowState)localmWindows.get(i-1) : null;
1266 continue;
1267 }
1268
1269 // The window didn't match... the current wallpaper window,
1270 // wherever it is, is in the wrong place, so make sure it is
1271 // not in the list.
1272 int oldIndex = localmWindows.indexOf(wallpaper);
1273 if (oldIndex >= 0) {
1274 localmWindows.remove(oldIndex);
1275 if (oldIndex < i) {
1276 i--;
1277 }
1278 }
1279
1280 // Now stick it in.
1281 localmWindows.add(i, wallpaper);
1282 changed = true;
1283 }
1284 }
1285
1286 return changed;
1287 }
1288
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001289 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001290 if (DEBUG_LAYERS) Log.v(TAG, "Setting wallpaper layer adj to " + adj);
1291 mWallpaperAnimLayerAdjustment = adj;
1292 int curTokenIndex = mWallpaperTokens.size();
1293 while (curTokenIndex > 0) {
1294 curTokenIndex--;
1295 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1296 int curWallpaperIndex = token.windows.size();
1297 while (curWallpaperIndex > 0) {
1298 curWallpaperIndex--;
1299 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1300 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
1301 if (DEBUG_LAYERS) Log.v(TAG, "Wallpaper win " + wallpaper
1302 + " anim layer: " + wallpaper.mAnimLayer);
1303 }
1304 }
1305 }
1306
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001307 boolean updateWallpaperOffsetLocked(WindowState target,
1308 WindowState wallpaperWin, int dw, int dh) {
1309 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1310 int offset = availw > 0 ? -(int)(availw*target.mWallpaperX+.5f) : 0;
1311 boolean changed = wallpaperWin.mXOffset != offset;
1312 if (changed) {
1313 wallpaperWin.mXOffset = offset;
1314 }
1315 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1316 offset = availh > 0 ? -(int)(availh*target.mWallpaperY+.5f) : 0;
1317 if (wallpaperWin.mYOffset != offset) {
1318 changed = true;
1319 wallpaperWin.mYOffset = offset;
1320 }
1321 return changed;
1322 }
1323
1324 boolean updateWallpaperOffsetLocked() {
1325 final int dw = mDisplay.getWidth();
1326 final int dh = mDisplay.getHeight();
1327
1328 boolean changed = false;
1329
1330 WindowState target = mWallpaperTarget;
1331 if (target != null) {
1332 int curTokenIndex = mWallpaperTokens.size();
1333 while (curTokenIndex > 0) {
1334 curTokenIndex--;
1335 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1336 int curWallpaperIndex = token.windows.size();
1337 while (curWallpaperIndex > 0) {
1338 curWallpaperIndex--;
1339 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1340 if (updateWallpaperOffsetLocked(target, wallpaper, dw, dh)) {
1341 wallpaper.computeShownFrameLocked();
1342 changed = true;
1343 }
1344 }
1345 }
1346 }
1347
1348 return changed;
1349 }
1350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 public int addWindow(Session session, IWindow client,
1352 WindowManager.LayoutParams attrs, int viewVisibility,
1353 Rect outContentInsets) {
1354 int res = mPolicy.checkAddPermission(attrs);
1355 if (res != WindowManagerImpl.ADD_OKAY) {
1356 return res;
1357 }
Romain Guy06882f82009-06-10 13:36:04 -07001358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 boolean reportNewConfig = false;
1360 WindowState attachedWindow = null;
1361 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 synchronized(mWindowMap) {
1364 // Instantiating a Display requires talking with the simulator,
1365 // so don't do it until we know the system is mostly up and
1366 // running.
1367 if (mDisplay == null) {
1368 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1369 mDisplay = wm.getDefaultDisplay();
1370 mQueue.setDisplay(mDisplay);
1371 reportNewConfig = true;
1372 }
Romain Guy06882f82009-06-10 13:36:04 -07001373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 if (mWindowMap.containsKey(client.asBinder())) {
1375 Log.w(TAG, "Window " + client + " is already added");
1376 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1377 }
1378
1379 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001380 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 if (attachedWindow == null) {
1382 Log.w(TAG, "Attempted to add window with token that is not a window: "
1383 + attrs.token + ". Aborting.");
1384 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1385 }
1386 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1387 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1388 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1389 + attrs.token + ". Aborting.");
1390 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1391 }
1392 }
1393
1394 boolean addToken = false;
1395 WindowToken token = mTokenMap.get(attrs.token);
1396 if (token == null) {
1397 if (attrs.type >= FIRST_APPLICATION_WINDOW
1398 && attrs.type <= LAST_APPLICATION_WINDOW) {
1399 Log.w(TAG, "Attempted to add application window with unknown token "
1400 + attrs.token + ". Aborting.");
1401 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1402 }
1403 if (attrs.type == TYPE_INPUT_METHOD) {
1404 Log.w(TAG, "Attempted to add input method window with unknown token "
1405 + attrs.token + ". Aborting.");
1406 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1407 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001408 if (attrs.type == TYPE_WALLPAPER) {
1409 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1410 + attrs.token + ". Aborting.");
1411 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 token = new WindowToken(attrs.token, -1, false);
1414 addToken = true;
1415 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1416 && attrs.type <= LAST_APPLICATION_WINDOW) {
1417 AppWindowToken atoken = token.appWindowToken;
1418 if (atoken == null) {
1419 Log.w(TAG, "Attempted to add window with non-application token "
1420 + token + ". Aborting.");
1421 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1422 } else if (atoken.removed) {
1423 Log.w(TAG, "Attempted to add window with exiting application token "
1424 + token + ". Aborting.");
1425 return WindowManagerImpl.ADD_APP_EXITING;
1426 }
1427 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1428 // No need for this guy!
1429 if (localLOGV) Log.v(
1430 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1431 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1432 }
1433 } else if (attrs.type == TYPE_INPUT_METHOD) {
1434 if (token.windowType != TYPE_INPUT_METHOD) {
1435 Log.w(TAG, "Attempted to add input method window with bad token "
1436 + attrs.token + ". Aborting.");
1437 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1438 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001439 } else if (attrs.type == TYPE_WALLPAPER) {
1440 if (token.windowType != TYPE_WALLPAPER) {
1441 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1442 + attrs.token + ". Aborting.");
1443 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 }
1446
1447 win = new WindowState(session, client, token,
1448 attachedWindow, attrs, viewVisibility);
1449 if (win.mDeathRecipient == null) {
1450 // Client has apparently died, so there is no reason to
1451 // continue.
1452 Log.w(TAG, "Adding window client " + client.asBinder()
1453 + " that is dead, aborting.");
1454 return WindowManagerImpl.ADD_APP_EXITING;
1455 }
1456
1457 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 res = mPolicy.prepareAddWindowLw(win, attrs);
1460 if (res != WindowManagerImpl.ADD_OKAY) {
1461 return res;
1462 }
1463
1464 // From now on, no exceptions or errors allowed!
1465
1466 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 if (addToken) {
1471 mTokenMap.put(attrs.token, token);
1472 mTokenList.add(token);
1473 }
1474 win.attach();
1475 mWindowMap.put(client.asBinder(), win);
1476
1477 if (attrs.type == TYPE_APPLICATION_STARTING &&
1478 token.appWindowToken != null) {
1479 token.appWindowToken.startingWindow = win;
1480 }
1481
1482 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 if (attrs.type == TYPE_INPUT_METHOD) {
1485 mInputMethodWindow = win;
1486 addInputMethodWindowToListLocked(win);
1487 imMayMove = false;
1488 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1489 mInputMethodDialogs.add(win);
1490 addWindowToListInOrderLocked(win, true);
1491 adjustInputMethodDialogsLocked();
1492 imMayMove = false;
1493 } else {
1494 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001495 if (attrs.type == TYPE_WALLPAPER ||
1496 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1497 adjustWallpaperWindowsLocked();
1498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 }
Romain Guy06882f82009-06-10 13:36:04 -07001500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 if (mInTouchMode) {
1506 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1507 }
1508 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1509 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1510 }
Romain Guy06882f82009-06-10 13:36:04 -07001511
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001512 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001514 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1515 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 imMayMove = false;
1517 }
1518 }
Romain Guy06882f82009-06-10 13:36:04 -07001519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001521 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
Romain Guy06882f82009-06-10 13:36:04 -07001523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 assignLayersLocked();
1525 // Don't do layout here, the window must call
1526 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 //dump();
1529
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001530 if (focusChanged) {
1531 if (mCurrentFocus != null) {
1532 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1533 }
1534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 if (localLOGV) Log.v(
1536 TAG, "New client " + client.asBinder()
1537 + ": window=" + win);
1538 }
1539
1540 // sendNewConfiguration() checks caller permissions so we must call it with
1541 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1542 // identity anyway, so it's safe to just clear & restore around this whole
1543 // block.
1544 final long origId = Binder.clearCallingIdentity();
1545 if (reportNewConfig) {
1546 sendNewConfiguration();
1547 } else {
1548 // Update Orientation after adding a window, only if the window needs to be
1549 // displayed right away
1550 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001551 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 sendNewConfiguration();
1553 }
1554 }
1555 }
1556 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 return res;
1559 }
Romain Guy06882f82009-06-10 13:36:04 -07001560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 public void removeWindow(Session session, IWindow client) {
1562 synchronized(mWindowMap) {
1563 WindowState win = windowForClientLocked(session, client);
1564 if (win == null) {
1565 return;
1566 }
1567 removeWindowLocked(session, win);
1568 }
1569 }
Romain Guy06882f82009-06-10 13:36:04 -07001570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 public void removeWindowLocked(Session session, WindowState win) {
1572
1573 if (localLOGV || DEBUG_FOCUS) Log.v(
1574 TAG, "Remove " + win + " client="
1575 + Integer.toHexString(System.identityHashCode(
1576 win.mClient.asBinder()))
1577 + ", surface=" + win.mSurface);
1578
1579 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 if (DEBUG_APP_TRANSITIONS) Log.v(
1582 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1583 + " mExiting=" + win.mExiting
1584 + " isAnimating=" + win.isAnimating()
1585 + " app-animation="
1586 + (win.mAppToken != null ? win.mAppToken.animation : null)
1587 + " inPendingTransaction="
1588 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1589 + " mDisplayFrozen=" + mDisplayFrozen);
1590 // Visibility of the removed window. Will be used later to update orientation later on.
1591 boolean wasVisible = false;
1592 // First, see if we need to run an animation. If we do, we have
1593 // to hold off on removing the window until the animation is done.
1594 // If the display is frozen, just remove immediately, since the
1595 // animation wouldn't be seen.
1596 if (win.mSurface != null && !mDisplayFrozen) {
1597 // If we are not currently running the exit animation, we
1598 // need to see about starting one.
1599 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1602 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1603 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1604 }
1605 // Try starting an animation.
1606 if (applyAnimationLocked(win, transit, false)) {
1607 win.mExiting = true;
1608 }
1609 }
1610 if (win.mExiting || win.isAnimating()) {
1611 // The exit animation is running... wait for it!
1612 //Log.i(TAG, "*** Running exit animation...");
1613 win.mExiting = true;
1614 win.mRemoveOnExit = true;
1615 mLayoutNeeded = true;
1616 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1617 performLayoutAndPlaceSurfacesLocked();
1618 if (win.mAppToken != null) {
1619 win.mAppToken.updateReportedVisibilityLocked();
1620 }
1621 //dump();
1622 Binder.restoreCallingIdentity(origId);
1623 return;
1624 }
1625 }
1626
1627 removeWindowInnerLocked(session, win);
1628 // Removing a visible window will effect the computed orientation
1629 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001630 if (wasVisible && computeForcedAppOrientationLocked()
1631 != mForcedAppOrientation) {
1632 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 }
1634 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1635 Binder.restoreCallingIdentity(origId);
1636 }
Romain Guy06882f82009-06-10 13:36:04 -07001637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 private void removeWindowInnerLocked(Session session, WindowState win) {
1639 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1640 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 if (mInputMethodTarget == win) {
1645 moveInputMethodWindowsIfNeededLocked(false);
1646 }
Romain Guy06882f82009-06-10 13:36:04 -07001647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 mPolicy.removeWindowLw(win);
1649 win.removeLocked();
1650
1651 mWindowMap.remove(win.mClient.asBinder());
1652 mWindows.remove(win);
1653
1654 if (mInputMethodWindow == win) {
1655 mInputMethodWindow = null;
1656 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1657 mInputMethodDialogs.remove(win);
1658 }
Romain Guy06882f82009-06-10 13:36:04 -07001659
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001660 if (win.mAttrs.type == TYPE_WALLPAPER ||
1661 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1662 adjustWallpaperWindowsLocked();
1663 }
1664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 final WindowToken token = win.mToken;
1666 final AppWindowToken atoken = win.mAppToken;
1667 token.windows.remove(win);
1668 if (atoken != null) {
1669 atoken.allAppWindows.remove(win);
1670 }
1671 if (localLOGV) Log.v(
1672 TAG, "**** Removing window " + win + ": count="
1673 + token.windows.size());
1674 if (token.windows.size() == 0) {
1675 if (!token.explicit) {
1676 mTokenMap.remove(token.token);
1677 mTokenList.remove(token);
1678 } else if (atoken != null) {
1679 atoken.firstWindowDrawn = false;
1680 }
1681 }
1682
1683 if (atoken != null) {
1684 if (atoken.startingWindow == win) {
1685 atoken.startingWindow = null;
1686 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1687 // If this is the last window and we had requested a starting
1688 // transition window, well there is no point now.
1689 atoken.startingData = null;
1690 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1691 // If this is the last window except for a starting transition
1692 // window, we need to get rid of the starting transition.
1693 if (DEBUG_STARTING_WINDOW) {
1694 Log.v(TAG, "Schedule remove starting " + token
1695 + ": no more real windows");
1696 }
1697 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1698 mH.sendMessage(m);
1699 }
1700 }
Romain Guy06882f82009-06-10 13:36:04 -07001701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 if (!mInLayout) {
1703 assignLayersLocked();
1704 mLayoutNeeded = true;
1705 performLayoutAndPlaceSurfacesLocked();
1706 if (win.mAppToken != null) {
1707 win.mAppToken.updateReportedVisibilityLocked();
1708 }
1709 }
1710 }
1711
1712 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1713 long origId = Binder.clearCallingIdentity();
1714 try {
1715 synchronized (mWindowMap) {
1716 WindowState w = windowForClientLocked(session, client);
1717 if ((w != null) && (w.mSurface != null)) {
1718 Surface.openTransaction();
1719 try {
1720 w.mSurface.setTransparentRegionHint(region);
1721 } finally {
1722 Surface.closeTransaction();
1723 }
1724 }
1725 }
1726 } finally {
1727 Binder.restoreCallingIdentity(origId);
1728 }
1729 }
1730
1731 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001732 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 Rect visibleInsets) {
1734 long origId = Binder.clearCallingIdentity();
1735 try {
1736 synchronized (mWindowMap) {
1737 WindowState w = windowForClientLocked(session, client);
1738 if (w != null) {
1739 w.mGivenInsetsPending = false;
1740 w.mGivenContentInsets.set(contentInsets);
1741 w.mGivenVisibleInsets.set(visibleInsets);
1742 w.mTouchableInsets = touchableInsets;
1743 mLayoutNeeded = true;
1744 performLayoutAndPlaceSurfacesLocked();
1745 }
1746 }
1747 } finally {
1748 Binder.restoreCallingIdentity(origId);
1749 }
1750 }
Romain Guy06882f82009-06-10 13:36:04 -07001751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 public void getWindowDisplayFrame(Session session, IWindow client,
1753 Rect outDisplayFrame) {
1754 synchronized(mWindowMap) {
1755 WindowState win = windowForClientLocked(session, client);
1756 if (win == null) {
1757 outDisplayFrame.setEmpty();
1758 return;
1759 }
1760 outDisplayFrame.set(win.mDisplayFrame);
1761 }
1762 }
1763
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001764 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
1765 if (window.mWallpaperX != x || window.mWallpaperY != y) {
1766 window.mWallpaperX = x;
1767 window.mWallpaperY = y;
1768
1769 if (mWallpaperTarget == window) {
1770 if (updateWallpaperOffsetLocked()) {
1771 performLayoutAndPlaceSurfacesLocked();
1772 }
1773 }
1774 }
1775 }
1776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 public int relayoutWindow(Session session, IWindow client,
1778 WindowManager.LayoutParams attrs, int requestedWidth,
1779 int requestedHeight, int viewVisibility, boolean insetsPending,
1780 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1781 Surface outSurface) {
1782 boolean displayed = false;
1783 boolean inTouchMode;
1784 Configuration newConfig = null;
1785 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 synchronized(mWindowMap) {
1788 WindowState win = windowForClientLocked(session, client);
1789 if (win == null) {
1790 return 0;
1791 }
1792 win.mRequestedWidth = requestedWidth;
1793 win.mRequestedHeight = requestedHeight;
1794
1795 if (attrs != null) {
1796 mPolicy.adjustWindowParamsLw(attrs);
1797 }
Romain Guy06882f82009-06-10 13:36:04 -07001798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 int attrChanges = 0;
1800 int flagChanges = 0;
1801 if (attrs != null) {
1802 flagChanges = win.mAttrs.flags ^= attrs.flags;
1803 attrChanges = win.mAttrs.copyFrom(attrs);
1804 }
1805
1806 if (localLOGV) Log.v(
1807 TAG, "Relayout given client " + client.asBinder()
1808 + " (" + win.mAttrs.getTitle() + ")");
1809
1810
1811 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1812 win.mAlpha = attrs.alpha;
1813 }
1814
1815 final boolean scaledWindow =
1816 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1817
1818 if (scaledWindow) {
1819 // requested{Width|Height} Surface's physical size
1820 // attrs.{width|height} Size on screen
1821 win.mHScale = (attrs.width != requestedWidth) ?
1822 (attrs.width / (float)requestedWidth) : 1.0f;
1823 win.mVScale = (attrs.height != requestedHeight) ?
1824 (attrs.height / (float)requestedHeight) : 1.0f;
1825 }
1826
1827 boolean imMayMove = (flagChanges&(
1828 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1829 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 boolean focusMayChange = win.mViewVisibility != viewVisibility
1832 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1833 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001834
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001835 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
1836 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
1837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 win.mRelayoutCalled = true;
1839 final int oldVisibility = win.mViewVisibility;
1840 win.mViewVisibility = viewVisibility;
1841 if (viewVisibility == View.VISIBLE &&
1842 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1843 displayed = !win.isVisibleLw();
1844 if (win.mExiting) {
1845 win.mExiting = false;
1846 win.mAnimation = null;
1847 }
1848 if (win.mDestroying) {
1849 win.mDestroying = false;
1850 mDestroySurface.remove(win);
1851 }
1852 if (oldVisibility == View.GONE) {
1853 win.mEnterAnimationPending = true;
1854 }
1855 if (displayed && win.mSurface != null && !win.mDrawPending
1856 && !win.mCommitDrawPending && !mDisplayFrozen) {
1857 applyEnterAnimationLocked(win);
1858 }
1859 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1860 // To change the format, we need to re-build the surface.
1861 win.destroySurfaceLocked();
1862 displayed = true;
1863 }
1864 try {
1865 Surface surface = win.createSurfaceLocked();
1866 if (surface != null) {
1867 outSurface.copyFrom(surface);
1868 } else {
1869 outSurface.clear();
1870 }
1871 } catch (Exception e) {
1872 Log.w(TAG, "Exception thrown when creating surface for client "
1873 + client + " (" + win.mAttrs.getTitle() + ")",
1874 e);
1875 Binder.restoreCallingIdentity(origId);
1876 return 0;
1877 }
1878 if (displayed) {
1879 focusMayChange = true;
1880 }
1881 if (win.mAttrs.type == TYPE_INPUT_METHOD
1882 && mInputMethodWindow == null) {
1883 mInputMethodWindow = win;
1884 imMayMove = true;
1885 }
1886 } else {
1887 win.mEnterAnimationPending = false;
1888 if (win.mSurface != null) {
1889 // If we are not currently running the exit animation, we
1890 // need to see about starting one.
1891 if (!win.mExiting) {
1892 // Try starting an animation; if there isn't one, we
1893 // can destroy the surface right away.
1894 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1895 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1896 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1897 }
1898 if (win.isWinVisibleLw() &&
1899 applyAnimationLocked(win, transit, false)) {
1900 win.mExiting = true;
1901 mKeyWaiter.finishedKey(session, client, true,
1902 KeyWaiter.RETURN_NOTHING);
1903 } else if (win.isAnimating()) {
1904 // Currently in a hide animation... turn this into
1905 // an exit.
1906 win.mExiting = true;
1907 } else {
1908 if (mInputMethodWindow == win) {
1909 mInputMethodWindow = null;
1910 }
1911 win.destroySurfaceLocked();
1912 }
1913 }
1914 }
1915 outSurface.clear();
1916 }
1917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 if (focusMayChange) {
1919 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1920 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 imMayMove = false;
1922 }
1923 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1924 }
Romain Guy06882f82009-06-10 13:36:04 -07001925
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001926 // updateFocusedWindowLocked() already assigned layers so we only need to
1927 // reassign them at this point if the IM window state gets shuffled
1928 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 if (imMayMove) {
1931 if (moveInputMethodWindowsIfNeededLocked(false)) {
1932 assignLayers = true;
1933 }
1934 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001935 if (wallpaperMayMove) {
1936 if (adjustWallpaperWindowsLocked()) {
1937 assignLayers = true;
1938 }
1939 }
Romain Guy06882f82009-06-10 13:36:04 -07001940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 mLayoutNeeded = true;
1942 win.mGivenInsetsPending = insetsPending;
1943 if (assignLayers) {
1944 assignLayersLocked();
1945 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001946 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 performLayoutAndPlaceSurfacesLocked();
1948 if (win.mAppToken != null) {
1949 win.mAppToken.updateReportedVisibilityLocked();
1950 }
1951 outFrame.set(win.mFrame);
1952 outContentInsets.set(win.mContentInsets);
1953 outVisibleInsets.set(win.mVisibleInsets);
1954 if (localLOGV) Log.v(
1955 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001956 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 + ", requestedHeight=" + requestedHeight
1958 + ", viewVisibility=" + viewVisibility
1959 + "\nRelayout returning frame=" + outFrame
1960 + ", surface=" + outSurface);
1961
1962 if (localLOGV || DEBUG_FOCUS) Log.v(
1963 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1964
1965 inTouchMode = mInTouchMode;
1966 }
1967
1968 if (newConfig != null) {
1969 sendNewConfiguration();
1970 }
Romain Guy06882f82009-06-10 13:36:04 -07001971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1975 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1976 }
1977
1978 public void finishDrawingWindow(Session session, IWindow client) {
1979 final long origId = Binder.clearCallingIdentity();
1980 synchronized(mWindowMap) {
1981 WindowState win = windowForClientLocked(session, client);
1982 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001983 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1984 adjustWallpaperWindowsLocked();
1985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 mLayoutNeeded = true;
1987 performLayoutAndPlaceSurfacesLocked();
1988 }
1989 }
1990 Binder.restoreCallingIdentity(origId);
1991 }
1992
1993 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1994 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1995 + (lp != null ? lp.packageName : null)
1996 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1997 if (lp != null && lp.windowAnimations != 0) {
1998 // If this is a system resource, don't try to load it from the
1999 // application resources. It is nice to avoid loading application
2000 // resources if we can.
2001 String packageName = lp.packageName != null ? lp.packageName : "android";
2002 int resId = lp.windowAnimations;
2003 if ((resId&0xFF000000) == 0x01000000) {
2004 packageName = "android";
2005 }
2006 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2007 + packageName);
2008 return AttributeCache.instance().get(packageName, resId,
2009 com.android.internal.R.styleable.WindowAnimation);
2010 }
2011 return null;
2012 }
Romain Guy06882f82009-06-10 13:36:04 -07002013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 private void applyEnterAnimationLocked(WindowState win) {
2015 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2016 if (win.mEnterAnimationPending) {
2017 win.mEnterAnimationPending = false;
2018 transit = WindowManagerPolicy.TRANSIT_ENTER;
2019 }
2020
2021 applyAnimationLocked(win, transit, true);
2022 }
2023
2024 private boolean applyAnimationLocked(WindowState win,
2025 int transit, boolean isEntrance) {
2026 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2027 // If we are trying to apply an animation, but already running
2028 // an animation of the same type, then just leave that one alone.
2029 return true;
2030 }
Romain Guy06882f82009-06-10 13:36:04 -07002031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 // Only apply an animation if the display isn't frozen. If it is
2033 // frozen, there is no reason to animate and it can cause strange
2034 // artifacts when we unfreeze the display if some different animation
2035 // is running.
2036 if (!mDisplayFrozen) {
2037 int anim = mPolicy.selectAnimationLw(win, transit);
2038 int attr = -1;
2039 Animation a = null;
2040 if (anim != 0) {
2041 a = AnimationUtils.loadAnimation(mContext, anim);
2042 } else {
2043 switch (transit) {
2044 case WindowManagerPolicy.TRANSIT_ENTER:
2045 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2046 break;
2047 case WindowManagerPolicy.TRANSIT_EXIT:
2048 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2049 break;
2050 case WindowManagerPolicy.TRANSIT_SHOW:
2051 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2052 break;
2053 case WindowManagerPolicy.TRANSIT_HIDE:
2054 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2055 break;
2056 }
2057 if (attr >= 0) {
2058 a = loadAnimation(win.mAttrs, attr);
2059 }
2060 }
2061 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2062 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2063 + " mAnimation=" + win.mAnimation
2064 + " isEntrance=" + isEntrance);
2065 if (a != null) {
2066 if (DEBUG_ANIM) {
2067 RuntimeException e = new RuntimeException();
2068 e.fillInStackTrace();
2069 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2070 }
2071 win.setAnimation(a);
2072 win.mAnimationIsEntrance = isEntrance;
2073 }
2074 } else {
2075 win.clearAnimation();
2076 }
2077
2078 return win.mAnimation != null;
2079 }
2080
2081 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2082 int anim = 0;
2083 Context context = mContext;
2084 if (animAttr >= 0) {
2085 AttributeCache.Entry ent = getCachedAnimations(lp);
2086 if (ent != null) {
2087 context = ent.context;
2088 anim = ent.array.getResourceId(animAttr, 0);
2089 }
2090 }
2091 if (anim != 0) {
2092 return AnimationUtils.loadAnimation(context, anim);
2093 }
2094 return null;
2095 }
Romain Guy06882f82009-06-10 13:36:04 -07002096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 private boolean applyAnimationLocked(AppWindowToken wtoken,
2098 WindowManager.LayoutParams lp, int transit, boolean enter) {
2099 // Only apply an animation if the display isn't frozen. If it is
2100 // frozen, there is no reason to animate and it can cause strange
2101 // artifacts when we unfreeze the display if some different animation
2102 // is running.
2103 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002104 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002105 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002106 a = new FadeInOutAnimation(enter);
2107 if (DEBUG_ANIM) Log.v(TAG,
2108 "applying FadeInOutAnimation for a window in compatibility mode");
2109 } else {
2110 int animAttr = 0;
2111 switch (transit) {
2112 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2113 animAttr = enter
2114 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2115 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2116 break;
2117 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2118 animAttr = enter
2119 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2120 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2121 break;
2122 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2123 animAttr = enter
2124 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2125 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2126 break;
2127 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2128 animAttr = enter
2129 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2130 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2131 break;
2132 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2133 animAttr = enter
2134 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2135 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2136 break;
2137 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2138 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002139 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002140 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2141 break;
2142 }
2143 a = loadAnimation(lp, animAttr);
2144 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2145 + " anim=" + a
2146 + " animAttr=0x" + Integer.toHexString(animAttr)
2147 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 if (a != null) {
2150 if (DEBUG_ANIM) {
2151 RuntimeException e = new RuntimeException();
2152 e.fillInStackTrace();
2153 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2154 }
2155 wtoken.setAnimation(a);
2156 }
2157 } else {
2158 wtoken.clearAnimation();
2159 }
2160
2161 return wtoken.animation != null;
2162 }
2163
2164 // -------------------------------------------------------------
2165 // Application Window Tokens
2166 // -------------------------------------------------------------
2167
2168 public void validateAppTokens(List tokens) {
2169 int v = tokens.size()-1;
2170 int m = mAppTokens.size()-1;
2171 while (v >= 0 && m >= 0) {
2172 AppWindowToken wtoken = mAppTokens.get(m);
2173 if (wtoken.removed) {
2174 m--;
2175 continue;
2176 }
2177 if (tokens.get(v) != wtoken.token) {
2178 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2179 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2180 }
2181 v--;
2182 m--;
2183 }
2184 while (v >= 0) {
2185 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2186 v--;
2187 }
2188 while (m >= 0) {
2189 AppWindowToken wtoken = mAppTokens.get(m);
2190 if (!wtoken.removed) {
2191 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2192 }
2193 m--;
2194 }
2195 }
2196
2197 boolean checkCallingPermission(String permission, String func) {
2198 // Quick check: if the calling permission is me, it's all okay.
2199 if (Binder.getCallingPid() == Process.myPid()) {
2200 return true;
2201 }
Romain Guy06882f82009-06-10 13:36:04 -07002202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 if (mContext.checkCallingPermission(permission)
2204 == PackageManager.PERMISSION_GRANTED) {
2205 return true;
2206 }
2207 String msg = "Permission Denial: " + func + " from pid="
2208 + Binder.getCallingPid()
2209 + ", uid=" + Binder.getCallingUid()
2210 + " requires " + permission;
2211 Log.w(TAG, msg);
2212 return false;
2213 }
Romain Guy06882f82009-06-10 13:36:04 -07002214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 AppWindowToken findAppWindowToken(IBinder token) {
2216 WindowToken wtoken = mTokenMap.get(token);
2217 if (wtoken == null) {
2218 return null;
2219 }
2220 return wtoken.appWindowToken;
2221 }
Romain Guy06882f82009-06-10 13:36:04 -07002222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 public void addWindowToken(IBinder token, int type) {
2224 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2225 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002226 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 }
Romain Guy06882f82009-06-10 13:36:04 -07002228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 synchronized(mWindowMap) {
2230 WindowToken wtoken = mTokenMap.get(token);
2231 if (wtoken != null) {
2232 Log.w(TAG, "Attempted to add existing input method token: " + token);
2233 return;
2234 }
2235 wtoken = new WindowToken(token, type, true);
2236 mTokenMap.put(token, wtoken);
2237 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002238 if (type == TYPE_WALLPAPER) {
2239 mWallpaperTokens.add(wtoken);
2240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 }
2242 }
Romain Guy06882f82009-06-10 13:36:04 -07002243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 public void removeWindowToken(IBinder token) {
2245 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2246 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002247 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 }
2249
2250 final long origId = Binder.clearCallingIdentity();
2251 synchronized(mWindowMap) {
2252 WindowToken wtoken = mTokenMap.remove(token);
2253 mTokenList.remove(wtoken);
2254 if (wtoken != null) {
2255 boolean delayed = false;
2256 if (!wtoken.hidden) {
2257 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 final int N = wtoken.windows.size();
2260 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 for (int i=0; i<N; i++) {
2263 WindowState win = wtoken.windows.get(i);
2264
2265 if (win.isAnimating()) {
2266 delayed = true;
2267 }
Romain Guy06882f82009-06-10 13:36:04 -07002268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 if (win.isVisibleNow()) {
2270 applyAnimationLocked(win,
2271 WindowManagerPolicy.TRANSIT_EXIT, false);
2272 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2273 KeyWaiter.RETURN_NOTHING);
2274 changed = true;
2275 }
2276 }
2277
2278 if (changed) {
2279 mLayoutNeeded = true;
2280 performLayoutAndPlaceSurfacesLocked();
2281 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2282 }
Romain Guy06882f82009-06-10 13:36:04 -07002283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 if (delayed) {
2285 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002286 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2287 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
2289 }
Romain Guy06882f82009-06-10 13:36:04 -07002290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 } else {
2292 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2293 }
2294 }
2295 Binder.restoreCallingIdentity(origId);
2296 }
2297
2298 public void addAppToken(int addPos, IApplicationToken token,
2299 int groupId, int requestedOrientation, boolean fullscreen) {
2300 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2301 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002302 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
Romain Guy06882f82009-06-10 13:36:04 -07002304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 synchronized(mWindowMap) {
2306 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2307 if (wtoken != null) {
2308 Log.w(TAG, "Attempted to add existing app token: " + token);
2309 return;
2310 }
2311 wtoken = new AppWindowToken(token);
2312 wtoken.groupId = groupId;
2313 wtoken.appFullscreen = fullscreen;
2314 wtoken.requestedOrientation = requestedOrientation;
2315 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002316 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 mTokenMap.put(token.asBinder(), wtoken);
2318 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 // Application tokens start out hidden.
2321 wtoken.hidden = true;
2322 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 //dump();
2325 }
2326 }
Romain Guy06882f82009-06-10 13:36:04 -07002327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 public void setAppGroupId(IBinder token, int groupId) {
2329 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2330 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002331 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 }
2333
2334 synchronized(mWindowMap) {
2335 AppWindowToken wtoken = findAppWindowToken(token);
2336 if (wtoken == null) {
2337 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2338 return;
2339 }
2340 wtoken.groupId = groupId;
2341 }
2342 }
Romain Guy06882f82009-06-10 13:36:04 -07002343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 public int getOrientationFromWindowsLocked() {
2345 int pos = mWindows.size() - 1;
2346 while (pos >= 0) {
2347 WindowState wtoken = (WindowState) mWindows.get(pos);
2348 pos--;
2349 if (wtoken.mAppToken != null) {
2350 // We hit an application window. so the orientation will be determined by the
2351 // app window. No point in continuing further.
2352 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2353 }
2354 if (!wtoken.isVisibleLw()) {
2355 continue;
2356 }
2357 int req = wtoken.mAttrs.screenOrientation;
2358 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2359 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2360 continue;
2361 } else {
2362 return req;
2363 }
2364 }
2365 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2366 }
Romain Guy06882f82009-06-10 13:36:04 -07002367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 public int getOrientationFromAppTokensLocked() {
2369 int pos = mAppTokens.size() - 1;
2370 int curGroup = 0;
2371 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002372 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002374 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 while (pos >= 0) {
2376 AppWindowToken wtoken = mAppTokens.get(pos);
2377 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002378 // if we're about to tear down this window and not seek for
2379 // the behind activity, don't use it for orientation
2380 if (!findingBehind
2381 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002382 continue;
2383 }
2384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 if (!haveGroup) {
2386 // We ignore any hidden applications on the top.
2387 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2388 continue;
2389 }
2390 haveGroup = true;
2391 curGroup = wtoken.groupId;
2392 lastOrientation = wtoken.requestedOrientation;
2393 } else if (curGroup != wtoken.groupId) {
2394 // If we have hit a new application group, and the bottom
2395 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002396 // the orientation behind it, and the last app was
2397 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002399 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2400 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 return lastOrientation;
2402 }
2403 }
2404 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002405 // If this application is fullscreen, and didn't explicitly say
2406 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002408 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002409 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002410 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 return or;
2412 }
2413 // If this application has requested an explicit orientation,
2414 // then use it.
2415 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2416 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2417 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2418 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2419 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2420 return or;
2421 }
Owen Lin3413b892009-05-01 17:12:32 -07002422 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 }
2424 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2425 }
Romain Guy06882f82009-06-10 13:36:04 -07002426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002428 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002429 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2430 "updateOrientationFromAppTokens()")) {
2431 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2432 }
2433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 Configuration config;
2435 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002436 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2437 freezeThisOneIfNeeded);
2438 Binder.restoreCallingIdentity(ident);
2439 return config;
2440 }
2441
2442 Configuration updateOrientationFromAppTokensUnchecked(
2443 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2444 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002446 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
2448 if (config != null) {
2449 mLayoutNeeded = true;
2450 performLayoutAndPlaceSurfacesLocked();
2451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 return config;
2453 }
Romain Guy06882f82009-06-10 13:36:04 -07002454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 /*
2456 * The orientation is computed from non-application windows first. If none of
2457 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002458 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2460 * android.os.IBinder)
2461 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002462 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002463 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 long ident = Binder.clearCallingIdentity();
2466 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002467 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 if (req != mForcedAppOrientation) {
2470 changed = true;
2471 mForcedAppOrientation = req;
2472 //send a message to Policy indicating orientation change to take
2473 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002474 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 }
Romain Guy06882f82009-06-10 13:36:04 -07002476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 if (changed) {
2478 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002479 WindowManagerPolicy.USE_LAST_ROTATION,
2480 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 if (changed) {
2482 if (freezeThisOneIfNeeded != null) {
2483 AppWindowToken wtoken = findAppWindowToken(
2484 freezeThisOneIfNeeded);
2485 if (wtoken != null) {
2486 startAppFreezingScreenLocked(wtoken,
2487 ActivityInfo.CONFIG_ORIENTATION);
2488 }
2489 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002490 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 }
2492 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002493
2494 // No obvious action we need to take, but if our current
2495 // state mismatches the activity maanager's, update it
2496 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002497 mTempConfiguration.setToDefaults();
2498 if (computeNewConfigurationLocked(mTempConfiguration)) {
2499 if (appConfig.diff(mTempConfiguration) != 0) {
2500 Log.i(TAG, "Config changed: " + mTempConfiguration);
2501 return new Configuration(mTempConfiguration);
2502 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002503 }
2504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 } finally {
2506 Binder.restoreCallingIdentity(ident);
2507 }
Romain Guy06882f82009-06-10 13:36:04 -07002508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 return null;
2510 }
Romain Guy06882f82009-06-10 13:36:04 -07002511
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002512 int computeForcedAppOrientationLocked() {
2513 int req = getOrientationFromWindowsLocked();
2514 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2515 req = getOrientationFromAppTokensLocked();
2516 }
2517 return req;
2518 }
Romain Guy06882f82009-06-10 13:36:04 -07002519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2521 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2522 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002523 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 }
Romain Guy06882f82009-06-10 13:36:04 -07002525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 synchronized(mWindowMap) {
2527 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2528 if (wtoken == null) {
2529 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2530 return;
2531 }
Romain Guy06882f82009-06-10 13:36:04 -07002532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 wtoken.requestedOrientation = requestedOrientation;
2534 }
2535 }
Romain Guy06882f82009-06-10 13:36:04 -07002536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 public int getAppOrientation(IApplicationToken token) {
2538 synchronized(mWindowMap) {
2539 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2540 if (wtoken == null) {
2541 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2542 }
Romain Guy06882f82009-06-10 13:36:04 -07002543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 return wtoken.requestedOrientation;
2545 }
2546 }
Romain Guy06882f82009-06-10 13:36:04 -07002547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2549 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2550 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002551 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 }
2553
2554 synchronized(mWindowMap) {
2555 boolean changed = false;
2556 if (token == null) {
2557 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2558 changed = mFocusedApp != null;
2559 mFocusedApp = null;
2560 mKeyWaiter.tickle();
2561 } else {
2562 AppWindowToken newFocus = findAppWindowToken(token);
2563 if (newFocus == null) {
2564 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2565 return;
2566 }
2567 changed = mFocusedApp != newFocus;
2568 mFocusedApp = newFocus;
2569 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2570 mKeyWaiter.tickle();
2571 }
2572
2573 if (moveFocusNow && changed) {
2574 final long origId = Binder.clearCallingIdentity();
2575 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2576 Binder.restoreCallingIdentity(origId);
2577 }
2578 }
2579 }
2580
2581 public void prepareAppTransition(int transit) {
2582 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2583 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002584 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 }
Romain Guy06882f82009-06-10 13:36:04 -07002586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 synchronized(mWindowMap) {
2588 if (DEBUG_APP_TRANSITIONS) Log.v(
2589 TAG, "Prepare app transition: transit=" + transit
2590 + " mNextAppTransition=" + mNextAppTransition);
2591 if (!mDisplayFrozen) {
2592 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2593 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002594 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2595 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2596 // Opening a new task always supersedes a close for the anim.
2597 mNextAppTransition = transit;
2598 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2599 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2600 // Opening a new activity always supersedes a close for the anim.
2601 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 }
2603 mAppTransitionReady = false;
2604 mAppTransitionTimeout = false;
2605 mStartingIconInTransition = false;
2606 mSkipAppTransitionAnimation = false;
2607 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2608 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2609 5000);
2610 }
2611 }
2612 }
2613
2614 public int getPendingAppTransition() {
2615 return mNextAppTransition;
2616 }
Romain Guy06882f82009-06-10 13:36:04 -07002617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 public void executeAppTransition() {
2619 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2620 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002621 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 }
Romain Guy06882f82009-06-10 13:36:04 -07002623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 synchronized(mWindowMap) {
2625 if (DEBUG_APP_TRANSITIONS) Log.v(
2626 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2627 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2628 mAppTransitionReady = true;
2629 final long origId = Binder.clearCallingIdentity();
2630 performLayoutAndPlaceSurfacesLocked();
2631 Binder.restoreCallingIdentity(origId);
2632 }
2633 }
2634 }
2635
2636 public void setAppStartingWindow(IBinder token, String pkg,
2637 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2638 IBinder transferFrom, boolean createIfNeeded) {
2639 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2640 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002641 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 }
2643
2644 synchronized(mWindowMap) {
2645 if (DEBUG_STARTING_WINDOW) Log.v(
2646 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2647 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 AppWindowToken wtoken = findAppWindowToken(token);
2650 if (wtoken == null) {
2651 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2652 return;
2653 }
2654
2655 // If the display is frozen, we won't do anything until the
2656 // actual window is displayed so there is no reason to put in
2657 // the starting window.
2658 if (mDisplayFrozen) {
2659 return;
2660 }
Romain Guy06882f82009-06-10 13:36:04 -07002661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 if (wtoken.startingData != null) {
2663 return;
2664 }
Romain Guy06882f82009-06-10 13:36:04 -07002665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 if (transferFrom != null) {
2667 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2668 if (ttoken != null) {
2669 WindowState startingWindow = ttoken.startingWindow;
2670 if (startingWindow != null) {
2671 if (mStartingIconInTransition) {
2672 // In this case, the starting icon has already
2673 // been displayed, so start letting windows get
2674 // shown immediately without any more transitions.
2675 mSkipAppTransitionAnimation = true;
2676 }
2677 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2678 "Moving existing starting from " + ttoken
2679 + " to " + wtoken);
2680 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 // Transfer the starting window over to the new
2683 // token.
2684 wtoken.startingData = ttoken.startingData;
2685 wtoken.startingView = ttoken.startingView;
2686 wtoken.startingWindow = startingWindow;
2687 ttoken.startingData = null;
2688 ttoken.startingView = null;
2689 ttoken.startingWindow = null;
2690 ttoken.startingMoved = true;
2691 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002692 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 startingWindow.mAppToken = wtoken;
2694 mWindows.remove(startingWindow);
2695 ttoken.windows.remove(startingWindow);
2696 ttoken.allAppWindows.remove(startingWindow);
2697 addWindowToListInOrderLocked(startingWindow, true);
2698 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 // Propagate other interesting state between the
2701 // tokens. If the old token is displayed, we should
2702 // immediately force the new one to be displayed. If
2703 // it is animating, we need to move that animation to
2704 // the new one.
2705 if (ttoken.allDrawn) {
2706 wtoken.allDrawn = true;
2707 }
2708 if (ttoken.firstWindowDrawn) {
2709 wtoken.firstWindowDrawn = true;
2710 }
2711 if (!ttoken.hidden) {
2712 wtoken.hidden = false;
2713 wtoken.hiddenRequested = false;
2714 wtoken.willBeHidden = false;
2715 }
2716 if (wtoken.clientHidden != ttoken.clientHidden) {
2717 wtoken.clientHidden = ttoken.clientHidden;
2718 wtoken.sendAppVisibilityToClients();
2719 }
2720 if (ttoken.animation != null) {
2721 wtoken.animation = ttoken.animation;
2722 wtoken.animating = ttoken.animating;
2723 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2724 ttoken.animation = null;
2725 ttoken.animLayerAdjustment = 0;
2726 wtoken.updateLayers();
2727 ttoken.updateLayers();
2728 }
Romain Guy06882f82009-06-10 13:36:04 -07002729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 mLayoutNeeded = true;
2732 performLayoutAndPlaceSurfacesLocked();
2733 Binder.restoreCallingIdentity(origId);
2734 return;
2735 } else if (ttoken.startingData != null) {
2736 // The previous app was getting ready to show a
2737 // starting window, but hasn't yet done so. Steal it!
2738 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2739 "Moving pending starting from " + ttoken
2740 + " to " + wtoken);
2741 wtoken.startingData = ttoken.startingData;
2742 ttoken.startingData = null;
2743 ttoken.startingMoved = true;
2744 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2745 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2746 // want to process the message ASAP, before any other queued
2747 // messages.
2748 mH.sendMessageAtFrontOfQueue(m);
2749 return;
2750 }
2751 }
2752 }
2753
2754 // There is no existing starting window, and the caller doesn't
2755 // want us to create one, so that's it!
2756 if (!createIfNeeded) {
2757 return;
2758 }
Romain Guy06882f82009-06-10 13:36:04 -07002759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 mStartingIconInTransition = true;
2761 wtoken.startingData = new StartingData(
2762 pkg, theme, nonLocalizedLabel,
2763 labelRes, icon);
2764 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2765 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2766 // want to process the message ASAP, before any other queued
2767 // messages.
2768 mH.sendMessageAtFrontOfQueue(m);
2769 }
2770 }
2771
2772 public void setAppWillBeHidden(IBinder token) {
2773 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2774 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002775 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 }
2777
2778 AppWindowToken wtoken;
2779
2780 synchronized(mWindowMap) {
2781 wtoken = findAppWindowToken(token);
2782 if (wtoken == null) {
2783 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2784 return;
2785 }
2786 wtoken.willBeHidden = true;
2787 }
2788 }
Romain Guy06882f82009-06-10 13:36:04 -07002789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2791 boolean visible, int transit, boolean performLayout) {
2792 boolean delayed = false;
2793
2794 if (wtoken.clientHidden == visible) {
2795 wtoken.clientHidden = !visible;
2796 wtoken.sendAppVisibilityToClients();
2797 }
Romain Guy06882f82009-06-10 13:36:04 -07002798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 wtoken.willBeHidden = false;
2800 if (wtoken.hidden == visible) {
2801 final int N = wtoken.allAppWindows.size();
2802 boolean changed = false;
2803 if (DEBUG_APP_TRANSITIONS) Log.v(
2804 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2805 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2810 if (wtoken.animation == sDummyAnimation) {
2811 wtoken.animation = null;
2812 }
2813 applyAnimationLocked(wtoken, lp, transit, visible);
2814 changed = true;
2815 if (wtoken.animation != null) {
2816 delayed = runningAppAnimation = true;
2817 }
2818 }
Romain Guy06882f82009-06-10 13:36:04 -07002819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 for (int i=0; i<N; i++) {
2821 WindowState win = wtoken.allAppWindows.get(i);
2822 if (win == wtoken.startingWindow) {
2823 continue;
2824 }
2825
2826 if (win.isAnimating()) {
2827 delayed = true;
2828 }
Romain Guy06882f82009-06-10 13:36:04 -07002829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2831 //win.dump(" ");
2832 if (visible) {
2833 if (!win.isVisibleNow()) {
2834 if (!runningAppAnimation) {
2835 applyAnimationLocked(win,
2836 WindowManagerPolicy.TRANSIT_ENTER, true);
2837 }
2838 changed = true;
2839 }
2840 } else if (win.isVisibleNow()) {
2841 if (!runningAppAnimation) {
2842 applyAnimationLocked(win,
2843 WindowManagerPolicy.TRANSIT_EXIT, false);
2844 }
2845 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2846 KeyWaiter.RETURN_NOTHING);
2847 changed = true;
2848 }
2849 }
2850
2851 wtoken.hidden = wtoken.hiddenRequested = !visible;
2852 if (!visible) {
2853 unsetAppFreezingScreenLocked(wtoken, true, true);
2854 } else {
2855 // If we are being set visible, and the starting window is
2856 // not yet displayed, then make sure it doesn't get displayed.
2857 WindowState swin = wtoken.startingWindow;
2858 if (swin != null && (swin.mDrawPending
2859 || swin.mCommitDrawPending)) {
2860 swin.mPolicyVisibility = false;
2861 swin.mPolicyVisibilityAfterAnim = false;
2862 }
2863 }
Romain Guy06882f82009-06-10 13:36:04 -07002864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2866 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2867 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 if (changed && performLayout) {
2870 mLayoutNeeded = true;
2871 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 performLayoutAndPlaceSurfacesLocked();
2873 }
2874 }
2875
2876 if (wtoken.animation != null) {
2877 delayed = true;
2878 }
Romain Guy06882f82009-06-10 13:36:04 -07002879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 return delayed;
2881 }
2882
2883 public void setAppVisibility(IBinder token, boolean visible) {
2884 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2885 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002886 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 }
2888
2889 AppWindowToken wtoken;
2890
2891 synchronized(mWindowMap) {
2892 wtoken = findAppWindowToken(token);
2893 if (wtoken == null) {
2894 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2895 return;
2896 }
2897
2898 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2899 RuntimeException e = new RuntimeException();
2900 e.fillInStackTrace();
2901 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2902 + "): mNextAppTransition=" + mNextAppTransition
2903 + " hidden=" + wtoken.hidden
2904 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2905 }
Romain Guy06882f82009-06-10 13:36:04 -07002906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 // If we are preparing an app transition, then delay changing
2908 // the visibility of this token until we execute that transition.
2909 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2910 // Already in requested state, don't do anything more.
2911 if (wtoken.hiddenRequested != visible) {
2912 return;
2913 }
2914 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 if (DEBUG_APP_TRANSITIONS) Log.v(
2917 TAG, "Setting dummy animation on: " + wtoken);
2918 wtoken.setDummyAnimation();
2919 mOpeningApps.remove(wtoken);
2920 mClosingApps.remove(wtoken);
2921 wtoken.inPendingTransaction = true;
2922 if (visible) {
2923 mOpeningApps.add(wtoken);
2924 wtoken.allDrawn = false;
2925 wtoken.startingDisplayed = false;
2926 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 if (wtoken.clientHidden) {
2929 // In the case where we are making an app visible
2930 // but holding off for a transition, we still need
2931 // to tell the client to make its windows visible so
2932 // they get drawn. Otherwise, we will wait on
2933 // performing the transition until all windows have
2934 // been drawn, they never will be, and we are sad.
2935 wtoken.clientHidden = false;
2936 wtoken.sendAppVisibilityToClients();
2937 }
2938 } else {
2939 mClosingApps.add(wtoken);
2940 }
2941 return;
2942 }
Romain Guy06882f82009-06-10 13:36:04 -07002943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 final long origId = Binder.clearCallingIdentity();
2945 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2946 wtoken.updateReportedVisibilityLocked();
2947 Binder.restoreCallingIdentity(origId);
2948 }
2949 }
2950
2951 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2952 boolean unfreezeSurfaceNow, boolean force) {
2953 if (wtoken.freezingScreen) {
2954 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2955 + " force=" + force);
2956 final int N = wtoken.allAppWindows.size();
2957 boolean unfrozeWindows = false;
2958 for (int i=0; i<N; i++) {
2959 WindowState w = wtoken.allAppWindows.get(i);
2960 if (w.mAppFreezing) {
2961 w.mAppFreezing = false;
2962 if (w.mSurface != null && !w.mOrientationChanging) {
2963 w.mOrientationChanging = true;
2964 }
2965 unfrozeWindows = true;
2966 }
2967 }
2968 if (force || unfrozeWindows) {
2969 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2970 wtoken.freezingScreen = false;
2971 mAppsFreezingScreen--;
2972 }
2973 if (unfreezeSurfaceNow) {
2974 if (unfrozeWindows) {
2975 mLayoutNeeded = true;
2976 performLayoutAndPlaceSurfacesLocked();
2977 }
2978 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2979 stopFreezingDisplayLocked();
2980 }
2981 }
2982 }
2983 }
Romain Guy06882f82009-06-10 13:36:04 -07002984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2986 int configChanges) {
2987 if (DEBUG_ORIENTATION) {
2988 RuntimeException e = new RuntimeException();
2989 e.fillInStackTrace();
2990 Log.i(TAG, "Set freezing of " + wtoken.appToken
2991 + ": hidden=" + wtoken.hidden + " freezing="
2992 + wtoken.freezingScreen, e);
2993 }
2994 if (!wtoken.hiddenRequested) {
2995 if (!wtoken.freezingScreen) {
2996 wtoken.freezingScreen = true;
2997 mAppsFreezingScreen++;
2998 if (mAppsFreezingScreen == 1) {
2999 startFreezingDisplayLocked();
3000 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3001 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3002 5000);
3003 }
3004 }
3005 final int N = wtoken.allAppWindows.size();
3006 for (int i=0; i<N; i++) {
3007 WindowState w = wtoken.allAppWindows.get(i);
3008 w.mAppFreezing = true;
3009 }
3010 }
3011 }
Romain Guy06882f82009-06-10 13:36:04 -07003012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 public void startAppFreezingScreen(IBinder token, int configChanges) {
3014 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3015 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003016 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 }
3018
3019 synchronized(mWindowMap) {
3020 if (configChanges == 0 && !mDisplayFrozen) {
3021 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3022 return;
3023 }
Romain Guy06882f82009-06-10 13:36:04 -07003024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 AppWindowToken wtoken = findAppWindowToken(token);
3026 if (wtoken == null || wtoken.appToken == null) {
3027 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3028 return;
3029 }
3030 final long origId = Binder.clearCallingIdentity();
3031 startAppFreezingScreenLocked(wtoken, configChanges);
3032 Binder.restoreCallingIdentity(origId);
3033 }
3034 }
Romain Guy06882f82009-06-10 13:36:04 -07003035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 public void stopAppFreezingScreen(IBinder token, boolean force) {
3037 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3038 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003039 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 }
3041
3042 synchronized(mWindowMap) {
3043 AppWindowToken wtoken = findAppWindowToken(token);
3044 if (wtoken == null || wtoken.appToken == null) {
3045 return;
3046 }
3047 final long origId = Binder.clearCallingIdentity();
3048 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3049 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3050 unsetAppFreezingScreenLocked(wtoken, true, force);
3051 Binder.restoreCallingIdentity(origId);
3052 }
3053 }
Romain Guy06882f82009-06-10 13:36:04 -07003054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 public void removeAppToken(IBinder token) {
3056 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3057 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003058 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 }
3060
3061 AppWindowToken wtoken = null;
3062 AppWindowToken startingToken = null;
3063 boolean delayed = false;
3064
3065 final long origId = Binder.clearCallingIdentity();
3066 synchronized(mWindowMap) {
3067 WindowToken basewtoken = mTokenMap.remove(token);
3068 mTokenList.remove(basewtoken);
3069 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3070 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3071 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3072 wtoken.inPendingTransaction = false;
3073 mOpeningApps.remove(wtoken);
3074 if (mClosingApps.contains(wtoken)) {
3075 delayed = true;
3076 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3077 mClosingApps.add(wtoken);
3078 delayed = true;
3079 }
3080 if (DEBUG_APP_TRANSITIONS) Log.v(
3081 TAG, "Removing app " + wtoken + " delayed=" + delayed
3082 + " animation=" + wtoken.animation
3083 + " animating=" + wtoken.animating);
3084 if (delayed) {
3085 // set the token aside because it has an active animation to be finished
3086 mExitingAppTokens.add(wtoken);
3087 }
3088 mAppTokens.remove(wtoken);
3089 wtoken.removed = true;
3090 if (wtoken.startingData != null) {
3091 startingToken = wtoken;
3092 }
3093 unsetAppFreezingScreenLocked(wtoken, true, true);
3094 if (mFocusedApp == wtoken) {
3095 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3096 mFocusedApp = null;
3097 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3098 mKeyWaiter.tickle();
3099 }
3100 } else {
3101 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3102 }
Romain Guy06882f82009-06-10 13:36:04 -07003103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 if (!delayed && wtoken != null) {
3105 wtoken.updateReportedVisibilityLocked();
3106 }
3107 }
3108 Binder.restoreCallingIdentity(origId);
3109
3110 if (startingToken != null) {
3111 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3112 + startingToken + ": app token removed");
3113 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3114 mH.sendMessage(m);
3115 }
3116 }
3117
3118 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3119 final int NW = token.windows.size();
3120 for (int i=0; i<NW; i++) {
3121 WindowState win = token.windows.get(i);
3122 mWindows.remove(win);
3123 int j = win.mChildWindows.size();
3124 while (j > 0) {
3125 j--;
3126 mWindows.remove(win.mChildWindows.get(j));
3127 }
3128 }
3129 return NW > 0;
3130 }
3131
3132 void dumpAppTokensLocked() {
3133 for (int i=mAppTokens.size()-1; i>=0; i--) {
3134 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3135 }
3136 }
Romain Guy06882f82009-06-10 13:36:04 -07003137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 void dumpWindowsLocked() {
3139 for (int i=mWindows.size()-1; i>=0; i--) {
3140 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3141 }
3142 }
Romain Guy06882f82009-06-10 13:36:04 -07003143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 private int findWindowOffsetLocked(int tokenPos) {
3145 final int NW = mWindows.size();
3146
3147 if (tokenPos >= mAppTokens.size()) {
3148 int i = NW;
3149 while (i > 0) {
3150 i--;
3151 WindowState win = (WindowState)mWindows.get(i);
3152 if (win.getAppToken() != null) {
3153 return i+1;
3154 }
3155 }
3156 }
3157
3158 while (tokenPos > 0) {
3159 // Find the first app token below the new position that has
3160 // a window displayed.
3161 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3162 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3163 + tokenPos + " -- " + wtoken.token);
3164 int i = wtoken.windows.size();
3165 while (i > 0) {
3166 i--;
3167 WindowState win = wtoken.windows.get(i);
3168 int j = win.mChildWindows.size();
3169 while (j > 0) {
3170 j--;
3171 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3172 if (cwin.mSubLayer >= 0 ) {
3173 for (int pos=NW-1; pos>=0; pos--) {
3174 if (mWindows.get(pos) == cwin) {
3175 if (DEBUG_REORDER) Log.v(TAG,
3176 "Found child win @" + (pos+1));
3177 return pos+1;
3178 }
3179 }
3180 }
3181 }
3182 for (int pos=NW-1; pos>=0; pos--) {
3183 if (mWindows.get(pos) == win) {
3184 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3185 return pos+1;
3186 }
3187 }
3188 }
3189 tokenPos--;
3190 }
3191
3192 return 0;
3193 }
3194
3195 private final int reAddWindowLocked(int index, WindowState win) {
3196 final int NCW = win.mChildWindows.size();
3197 boolean added = false;
3198 for (int j=0; j<NCW; j++) {
3199 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3200 if (!added && cwin.mSubLayer >= 0) {
3201 mWindows.add(index, win);
3202 index++;
3203 added = true;
3204 }
3205 mWindows.add(index, cwin);
3206 index++;
3207 }
3208 if (!added) {
3209 mWindows.add(index, win);
3210 index++;
3211 }
3212 return index;
3213 }
Romain Guy06882f82009-06-10 13:36:04 -07003214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3216 final int NW = token.windows.size();
3217 for (int i=0; i<NW; i++) {
3218 index = reAddWindowLocked(index, token.windows.get(i));
3219 }
3220 return index;
3221 }
3222
3223 public void moveAppToken(int index, IBinder token) {
3224 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3225 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003226 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
3228
3229 synchronized(mWindowMap) {
3230 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3231 if (DEBUG_REORDER) dumpAppTokensLocked();
3232 final AppWindowToken wtoken = findAppWindowToken(token);
3233 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3234 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3235 + token + " (" + wtoken + ")");
3236 return;
3237 }
3238 mAppTokens.add(index, wtoken);
3239 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3240 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 final long origId = Binder.clearCallingIdentity();
3243 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3244 if (DEBUG_REORDER) dumpWindowsLocked();
3245 if (tmpRemoveAppWindowsLocked(wtoken)) {
3246 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3247 if (DEBUG_REORDER) dumpWindowsLocked();
3248 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3249 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3250 if (DEBUG_REORDER) dumpWindowsLocked();
3251 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 mLayoutNeeded = true;
3253 performLayoutAndPlaceSurfacesLocked();
3254 }
3255 Binder.restoreCallingIdentity(origId);
3256 }
3257 }
3258
3259 private void removeAppTokensLocked(List<IBinder> tokens) {
3260 // XXX This should be done more efficiently!
3261 // (take advantage of the fact that both lists should be
3262 // ordered in the same way.)
3263 int N = tokens.size();
3264 for (int i=0; i<N; i++) {
3265 IBinder token = tokens.get(i);
3266 final AppWindowToken wtoken = findAppWindowToken(token);
3267 if (!mAppTokens.remove(wtoken)) {
3268 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3269 + token + " (" + wtoken + ")");
3270 i--;
3271 N--;
3272 }
3273 }
3274 }
3275
3276 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3277 // First remove all of the windows from the list.
3278 final int N = tokens.size();
3279 int i;
3280 for (i=0; i<N; i++) {
3281 WindowToken token = mTokenMap.get(tokens.get(i));
3282 if (token != null) {
3283 tmpRemoveAppWindowsLocked(token);
3284 }
3285 }
3286
3287 // Where to start adding?
3288 int pos = findWindowOffsetLocked(tokenPos);
3289
3290 // And now add them back at the correct place.
3291 for (i=0; i<N; i++) {
3292 WindowToken token = mTokenMap.get(tokens.get(i));
3293 if (token != null) {
3294 pos = reAddAppWindowsLocked(pos, token);
3295 }
3296 }
3297
3298 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 mLayoutNeeded = true;
3300 performLayoutAndPlaceSurfacesLocked();
3301
3302 //dump();
3303 }
3304
3305 public void moveAppTokensToTop(List<IBinder> tokens) {
3306 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3307 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003308 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 }
3310
3311 final long origId = Binder.clearCallingIdentity();
3312 synchronized(mWindowMap) {
3313 removeAppTokensLocked(tokens);
3314 final int N = tokens.size();
3315 for (int i=0; i<N; i++) {
3316 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3317 if (wt != null) {
3318 mAppTokens.add(wt);
3319 }
3320 }
3321 moveAppWindowsLocked(tokens, mAppTokens.size());
3322 }
3323 Binder.restoreCallingIdentity(origId);
3324 }
3325
3326 public void moveAppTokensToBottom(List<IBinder> tokens) {
3327 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3328 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003329 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 }
3331
3332 final long origId = Binder.clearCallingIdentity();
3333 synchronized(mWindowMap) {
3334 removeAppTokensLocked(tokens);
3335 final int N = tokens.size();
3336 int pos = 0;
3337 for (int i=0; i<N; i++) {
3338 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3339 if (wt != null) {
3340 mAppTokens.add(pos, wt);
3341 pos++;
3342 }
3343 }
3344 moveAppWindowsLocked(tokens, 0);
3345 }
3346 Binder.restoreCallingIdentity(origId);
3347 }
3348
3349 // -------------------------------------------------------------
3350 // Misc IWindowSession methods
3351 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 public void disableKeyguard(IBinder token, String tag) {
3354 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3355 != PackageManager.PERMISSION_GRANTED) {
3356 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3357 }
3358 mKeyguardDisabled.acquire(token, tag);
3359 }
3360
3361 public void reenableKeyguard(IBinder token) {
3362 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3363 != PackageManager.PERMISSION_GRANTED) {
3364 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3365 }
3366 synchronized (mKeyguardDisabled) {
3367 mKeyguardDisabled.release(token);
3368
3369 if (!mKeyguardDisabled.isAcquired()) {
3370 // if we are the last one to reenable the keyguard wait until
3371 // we have actaully finished reenabling until returning
3372 mWaitingUntilKeyguardReenabled = true;
3373 while (mWaitingUntilKeyguardReenabled) {
3374 try {
3375 mKeyguardDisabled.wait();
3376 } catch (InterruptedException e) {
3377 Thread.currentThread().interrupt();
3378 }
3379 }
3380 }
3381 }
3382 }
3383
3384 /**
3385 * @see android.app.KeyguardManager#exitKeyguardSecurely
3386 */
3387 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3388 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3389 != PackageManager.PERMISSION_GRANTED) {
3390 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3391 }
3392 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3393 public void onKeyguardExitResult(boolean success) {
3394 try {
3395 callback.onKeyguardExitResult(success);
3396 } catch (RemoteException e) {
3397 // Client has died, we don't care.
3398 }
3399 }
3400 });
3401 }
3402
3403 public boolean inKeyguardRestrictedInputMode() {
3404 return mPolicy.inKeyguardRestrictedKeyInputMode();
3405 }
Romain Guy06882f82009-06-10 13:36:04 -07003406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 static float fixScale(float scale) {
3408 if (scale < 0) scale = 0;
3409 else if (scale > 20) scale = 20;
3410 return Math.abs(scale);
3411 }
Romain Guy06882f82009-06-10 13:36:04 -07003412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 public void setAnimationScale(int which, float scale) {
3414 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3415 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003416 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 }
3418
3419 if (scale < 0) scale = 0;
3420 else if (scale > 20) scale = 20;
3421 scale = Math.abs(scale);
3422 switch (which) {
3423 case 0: mWindowAnimationScale = fixScale(scale); break;
3424 case 1: mTransitionAnimationScale = fixScale(scale); break;
3425 }
Romain Guy06882f82009-06-10 13:36:04 -07003426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 // Persist setting
3428 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3429 }
Romain Guy06882f82009-06-10 13:36:04 -07003430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 public void setAnimationScales(float[] scales) {
3432 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3433 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003434 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 }
3436
3437 if (scales != null) {
3438 if (scales.length >= 1) {
3439 mWindowAnimationScale = fixScale(scales[0]);
3440 }
3441 if (scales.length >= 2) {
3442 mTransitionAnimationScale = fixScale(scales[1]);
3443 }
3444 }
Romain Guy06882f82009-06-10 13:36:04 -07003445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 // Persist setting
3447 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3448 }
Romain Guy06882f82009-06-10 13:36:04 -07003449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 public float getAnimationScale(int which) {
3451 switch (which) {
3452 case 0: return mWindowAnimationScale;
3453 case 1: return mTransitionAnimationScale;
3454 }
3455 return 0;
3456 }
Romain Guy06882f82009-06-10 13:36:04 -07003457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 public float[] getAnimationScales() {
3459 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3460 }
Romain Guy06882f82009-06-10 13:36:04 -07003461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 public int getSwitchState(int sw) {
3463 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3464 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003465 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 }
3467 return KeyInputQueue.getSwitchState(sw);
3468 }
Romain Guy06882f82009-06-10 13:36:04 -07003469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 public int getSwitchStateForDevice(int devid, int sw) {
3471 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3472 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003473 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 }
3475 return KeyInputQueue.getSwitchState(devid, sw);
3476 }
Romain Guy06882f82009-06-10 13:36:04 -07003477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 public int getScancodeState(int sw) {
3479 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3480 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003481 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 }
3483 return KeyInputQueue.getScancodeState(sw);
3484 }
Romain Guy06882f82009-06-10 13:36:04 -07003485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 public int getScancodeStateForDevice(int devid, int sw) {
3487 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3488 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003489 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 }
3491 return KeyInputQueue.getScancodeState(devid, sw);
3492 }
Romain Guy06882f82009-06-10 13:36:04 -07003493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 public int getKeycodeState(int sw) {
3495 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3496 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003497 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 }
3499 return KeyInputQueue.getKeycodeState(sw);
3500 }
Romain Guy06882f82009-06-10 13:36:04 -07003501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 public int getKeycodeStateForDevice(int devid, int sw) {
3503 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3504 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003505 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 }
3507 return KeyInputQueue.getKeycodeState(devid, sw);
3508 }
Romain Guy06882f82009-06-10 13:36:04 -07003509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3511 return KeyInputQueue.hasKeys(keycodes, keyExists);
3512 }
Romain Guy06882f82009-06-10 13:36:04 -07003513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 public void enableScreenAfterBoot() {
3515 synchronized(mWindowMap) {
3516 if (mSystemBooted) {
3517 return;
3518 }
3519 mSystemBooted = true;
3520 }
Romain Guy06882f82009-06-10 13:36:04 -07003521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 performEnableScreen();
3523 }
Romain Guy06882f82009-06-10 13:36:04 -07003524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 public void enableScreenIfNeededLocked() {
3526 if (mDisplayEnabled) {
3527 return;
3528 }
3529 if (!mSystemBooted) {
3530 return;
3531 }
3532 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3533 }
Romain Guy06882f82009-06-10 13:36:04 -07003534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 public void performEnableScreen() {
3536 synchronized(mWindowMap) {
3537 if (mDisplayEnabled) {
3538 return;
3539 }
3540 if (!mSystemBooted) {
3541 return;
3542 }
Romain Guy06882f82009-06-10 13:36:04 -07003543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 // Don't enable the screen until all existing windows
3545 // have been drawn.
3546 final int N = mWindows.size();
3547 for (int i=0; i<N; i++) {
3548 WindowState w = (WindowState)mWindows.get(i);
3549 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3550 return;
3551 }
3552 }
Romain Guy06882f82009-06-10 13:36:04 -07003553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 mDisplayEnabled = true;
3555 if (false) {
3556 Log.i(TAG, "ENABLING SCREEN!");
3557 StringWriter sw = new StringWriter();
3558 PrintWriter pw = new PrintWriter(sw);
3559 this.dump(null, pw, null);
3560 Log.i(TAG, sw.toString());
3561 }
3562 try {
3563 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3564 if (surfaceFlinger != null) {
3565 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3566 Parcel data = Parcel.obtain();
3567 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3568 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3569 data, null, 0);
3570 data.recycle();
3571 }
3572 } catch (RemoteException ex) {
3573 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3574 }
3575 }
Romain Guy06882f82009-06-10 13:36:04 -07003576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003580 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3581 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 }
Romain Guy06882f82009-06-10 13:36:04 -07003583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 public void setInTouchMode(boolean mode) {
3585 synchronized(mWindowMap) {
3586 mInTouchMode = mode;
3587 }
3588 }
3589
Romain Guy06882f82009-06-10 13:36:04 -07003590 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003591 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003593 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003594 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 }
3596
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003597 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 }
Romain Guy06882f82009-06-10 13:36:04 -07003599
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003600 public void setRotationUnchecked(int rotation,
3601 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 if(DEBUG_ORIENTATION) Log.v(TAG,
3603 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 long origId = Binder.clearCallingIdentity();
3606 boolean changed;
3607 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003608 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 }
Romain Guy06882f82009-06-10 13:36:04 -07003610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 if (changed) {
3612 sendNewConfiguration();
3613 synchronized(mWindowMap) {
3614 mLayoutNeeded = true;
3615 performLayoutAndPlaceSurfacesLocked();
3616 }
3617 } else if (alwaysSendConfiguration) {
3618 //update configuration ignoring orientation change
3619 sendNewConfiguration();
3620 }
Romain Guy06882f82009-06-10 13:36:04 -07003621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 Binder.restoreCallingIdentity(origId);
3623 }
Romain Guy06882f82009-06-10 13:36:04 -07003624
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003625 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 boolean changed;
3627 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3628 rotation = mRequestedRotation;
3629 } else {
3630 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003631 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 }
3633 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003634 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 mRotation, mDisplayEnabled);
3636 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3637 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003640 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 "Rotation changed to " + rotation
3642 + " from " + mRotation
3643 + " (forceApp=" + mForcedAppOrientation
3644 + ", req=" + mRequestedRotation + ")");
3645 mRotation = rotation;
3646 mWindowsFreezingScreen = true;
3647 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3648 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3649 2000);
3650 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003651 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 mQueue.setOrientation(rotation);
3653 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003654 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 }
3656 for (int i=mWindows.size()-1; i>=0; i--) {
3657 WindowState w = (WindowState)mWindows.get(i);
3658 if (w.mSurface != null) {
3659 w.mOrientationChanging = true;
3660 }
3661 }
3662 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3663 try {
3664 mRotationWatchers.get(i).onRotationChanged(rotation);
3665 } catch (RemoteException e) {
3666 }
3667 }
3668 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 return changed;
3671 }
Romain Guy06882f82009-06-10 13:36:04 -07003672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 public int getRotation() {
3674 return mRotation;
3675 }
3676
3677 public int watchRotation(IRotationWatcher watcher) {
3678 final IBinder watcherBinder = watcher.asBinder();
3679 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3680 public void binderDied() {
3681 synchronized (mWindowMap) {
3682 for (int i=0; i<mRotationWatchers.size(); i++) {
3683 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003684 IRotationWatcher removed = mRotationWatchers.remove(i);
3685 if (removed != null) {
3686 removed.asBinder().unlinkToDeath(this, 0);
3687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 i--;
3689 }
3690 }
3691 }
3692 }
3693 };
Romain Guy06882f82009-06-10 13:36:04 -07003694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 synchronized (mWindowMap) {
3696 try {
3697 watcher.asBinder().linkToDeath(dr, 0);
3698 mRotationWatchers.add(watcher);
3699 } catch (RemoteException e) {
3700 // Client died, no cleanup needed.
3701 }
Romain Guy06882f82009-06-10 13:36:04 -07003702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 return mRotation;
3704 }
3705 }
3706
3707 /**
3708 * Starts the view server on the specified port.
3709 *
3710 * @param port The port to listener to.
3711 *
3712 * @return True if the server was successfully started, false otherwise.
3713 *
3714 * @see com.android.server.ViewServer
3715 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3716 */
3717 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003718 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 return false;
3720 }
3721
3722 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3723 return false;
3724 }
3725
3726 if (port < 1024) {
3727 return false;
3728 }
3729
3730 if (mViewServer != null) {
3731 if (!mViewServer.isRunning()) {
3732 try {
3733 return mViewServer.start();
3734 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003735 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 }
3737 }
3738 return false;
3739 }
3740
3741 try {
3742 mViewServer = new ViewServer(this, port);
3743 return mViewServer.start();
3744 } catch (IOException e) {
3745 Log.w(TAG, "View server did not start");
3746 }
3747 return false;
3748 }
3749
Romain Guy06882f82009-06-10 13:36:04 -07003750 private boolean isSystemSecure() {
3751 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3752 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3753 }
3754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 /**
3756 * Stops the view server if it exists.
3757 *
3758 * @return True if the server stopped, false if it wasn't started or
3759 * couldn't be stopped.
3760 *
3761 * @see com.android.server.ViewServer
3762 */
3763 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003764 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 return false;
3766 }
3767
3768 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3769 return false;
3770 }
3771
3772 if (mViewServer != null) {
3773 return mViewServer.stop();
3774 }
3775 return false;
3776 }
3777
3778 /**
3779 * Indicates whether the view server is running.
3780 *
3781 * @return True if the server is running, false otherwise.
3782 *
3783 * @see com.android.server.ViewServer
3784 */
3785 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003786 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 return false;
3788 }
3789
3790 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3791 return false;
3792 }
3793
3794 return mViewServer != null && mViewServer.isRunning();
3795 }
3796
3797 /**
3798 * Lists all availble windows in the system. The listing is written in the
3799 * specified Socket's output stream with the following syntax:
3800 * windowHashCodeInHexadecimal windowName
3801 * Each line of the ouput represents a different window.
3802 *
3803 * @param client The remote client to send the listing to.
3804 * @return False if an error occured, true otherwise.
3805 */
3806 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003807 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 return false;
3809 }
3810
3811 boolean result = true;
3812
3813 Object[] windows;
3814 synchronized (mWindowMap) {
3815 windows = new Object[mWindows.size()];
3816 //noinspection unchecked
3817 windows = mWindows.toArray(windows);
3818 }
3819
3820 BufferedWriter out = null;
3821
3822 // Any uncaught exception will crash the system process
3823 try {
3824 OutputStream clientStream = client.getOutputStream();
3825 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3826
3827 final int count = windows.length;
3828 for (int i = 0; i < count; i++) {
3829 final WindowState w = (WindowState) windows[i];
3830 out.write(Integer.toHexString(System.identityHashCode(w)));
3831 out.write(' ');
3832 out.append(w.mAttrs.getTitle());
3833 out.write('\n');
3834 }
3835
3836 out.write("DONE.\n");
3837 out.flush();
3838 } catch (Exception e) {
3839 result = false;
3840 } finally {
3841 if (out != null) {
3842 try {
3843 out.close();
3844 } catch (IOException e) {
3845 result = false;
3846 }
3847 }
3848 }
3849
3850 return result;
3851 }
3852
3853 /**
3854 * Sends a command to a target window. The result of the command, if any, will be
3855 * written in the output stream of the specified socket.
3856 *
3857 * The parameters must follow this syntax:
3858 * windowHashcode extra
3859 *
3860 * Where XX is the length in characeters of the windowTitle.
3861 *
3862 * The first parameter is the target window. The window with the specified hashcode
3863 * will be the target. If no target can be found, nothing happens. The extra parameters
3864 * will be delivered to the target window and as parameters to the command itself.
3865 *
3866 * @param client The remote client to sent the result, if any, to.
3867 * @param command The command to execute.
3868 * @param parameters The command parameters.
3869 *
3870 * @return True if the command was successfully delivered, false otherwise. This does
3871 * not indicate whether the command itself was successful.
3872 */
3873 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003874 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 return false;
3876 }
3877
3878 boolean success = true;
3879 Parcel data = null;
3880 Parcel reply = null;
3881
3882 // Any uncaught exception will crash the system process
3883 try {
3884 // Find the hashcode of the window
3885 int index = parameters.indexOf(' ');
3886 if (index == -1) {
3887 index = parameters.length();
3888 }
3889 final String code = parameters.substring(0, index);
3890 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3891
3892 // Extract the command's parameter after the window description
3893 if (index < parameters.length()) {
3894 parameters = parameters.substring(index + 1);
3895 } else {
3896 parameters = "";
3897 }
3898
3899 final WindowManagerService.WindowState window = findWindow(hashCode);
3900 if (window == null) {
3901 return false;
3902 }
3903
3904 data = Parcel.obtain();
3905 data.writeInterfaceToken("android.view.IWindow");
3906 data.writeString(command);
3907 data.writeString(parameters);
3908 data.writeInt(1);
3909 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3910
3911 reply = Parcel.obtain();
3912
3913 final IBinder binder = window.mClient.asBinder();
3914 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3915 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3916
3917 reply.readException();
3918
3919 } catch (Exception e) {
3920 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3921 success = false;
3922 } finally {
3923 if (data != null) {
3924 data.recycle();
3925 }
3926 if (reply != null) {
3927 reply.recycle();
3928 }
3929 }
3930
3931 return success;
3932 }
3933
3934 private WindowState findWindow(int hashCode) {
3935 if (hashCode == -1) {
3936 return getFocusedWindow();
3937 }
3938
3939 synchronized (mWindowMap) {
3940 final ArrayList windows = mWindows;
3941 final int count = windows.size();
3942
3943 for (int i = 0; i < count; i++) {
3944 WindowState w = (WindowState) windows.get(i);
3945 if (System.identityHashCode(w) == hashCode) {
3946 return w;
3947 }
3948 }
3949 }
3950
3951 return null;
3952 }
3953
3954 /*
3955 * Instruct the Activity Manager to fetch the current configuration and broadcast
3956 * that to config-changed listeners if appropriate.
3957 */
3958 void sendNewConfiguration() {
3959 try {
3960 mActivityManager.updateConfiguration(null);
3961 } catch (RemoteException e) {
3962 }
3963 }
Romain Guy06882f82009-06-10 13:36:04 -07003964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003965 public Configuration computeNewConfiguration() {
3966 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003967 return computeNewConfigurationLocked();
3968 }
3969 }
Romain Guy06882f82009-06-10 13:36:04 -07003970
Dianne Hackbornc485a602009-03-24 22:39:49 -07003971 Configuration computeNewConfigurationLocked() {
3972 Configuration config = new Configuration();
3973 if (!computeNewConfigurationLocked(config)) {
3974 return null;
3975 }
3976 Log.i(TAG, "Config changed: " + config);
3977 long now = SystemClock.uptimeMillis();
3978 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3979 if (mFreezeGcPending != 0) {
3980 if (now > (mFreezeGcPending+1000)) {
3981 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3982 mH.removeMessages(H.FORCE_GC);
3983 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984 mFreezeGcPending = now;
3985 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003986 } else {
3987 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003989 return config;
3990 }
Romain Guy06882f82009-06-10 13:36:04 -07003991
Dianne Hackbornc485a602009-03-24 22:39:49 -07003992 boolean computeNewConfigurationLocked(Configuration config) {
3993 if (mDisplay == null) {
3994 return false;
3995 }
3996 mQueue.getInputConfiguration(config);
3997 final int dw = mDisplay.getWidth();
3998 final int dh = mDisplay.getHeight();
3999 int orientation = Configuration.ORIENTATION_SQUARE;
4000 if (dw < dh) {
4001 orientation = Configuration.ORIENTATION_PORTRAIT;
4002 } else if (dw > dh) {
4003 orientation = Configuration.ORIENTATION_LANDSCAPE;
4004 }
4005 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004006
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004007 DisplayMetrics dm = new DisplayMetrics();
4008 mDisplay.getMetrics(dm);
4009 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4010
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004011 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004012 // Note we only do this once because at this point we don't
4013 // expect the screen to change in this way at runtime, and want
4014 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004015 int longSize = dw;
4016 int shortSize = dh;
4017 if (longSize < shortSize) {
4018 int tmp = longSize;
4019 longSize = shortSize;
4020 shortSize = tmp;
4021 }
4022 longSize = (int)(longSize/dm.density);
4023 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004024
Dianne Hackborn723738c2009-06-25 19:48:04 -07004025 // These semi-magic numbers define our compatibility modes for
4026 // applications with different screens. Don't change unless you
4027 // make sure to test lots and lots of apps!
4028 if (longSize < 470) {
4029 // This is shorter than an HVGA normal density screen (which
4030 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004031 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4032 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004033 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004034 // Is this a large screen?
4035 if (longSize > 640 && shortSize >= 480) {
4036 // VGA or larger screens at medium density are the point
4037 // at which we consider it to be a large screen.
4038 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4039 } else {
4040 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4041
4042 // If this screen is wider than normal HVGA, or taller
4043 // than FWVGA, then for old apps we want to run in size
4044 // compatibility mode.
4045 if (shortSize > 321 || longSize > 570) {
4046 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4047 }
4048 }
4049
4050 // Is this a long screen?
4051 if (((longSize*3)/5) >= (shortSize-1)) {
4052 // Anything wider than WVGA (5:3) is considering to be long.
4053 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4054 } else {
4055 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4056 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004057 }
4058 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004059 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004060
Dianne Hackbornc485a602009-03-24 22:39:49 -07004061 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4062 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4063 mPolicy.adjustConfigurationLw(config);
4064 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 }
Romain Guy06882f82009-06-10 13:36:04 -07004066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 // -------------------------------------------------------------
4068 // Input Events and Focus Management
4069 // -------------------------------------------------------------
4070
4071 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004072 long curTime = SystemClock.uptimeMillis();
4073
Michael Chane10de972009-05-18 11:24:50 -07004074 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004075 if (mLastTouchEventType == eventType &&
4076 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4077 return;
4078 }
4079 mLastUserActivityCallTime = curTime;
4080 mLastTouchEventType = eventType;
4081 }
4082
4083 if (targetWin == null
4084 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4085 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 }
4087 }
4088
4089 // tells if it's a cheek event or not -- this function is stateful
4090 private static final int EVENT_NONE = 0;
4091 private static final int EVENT_UNKNOWN = 0;
4092 private static final int EVENT_CHEEK = 0;
4093 private static final int EVENT_IGNORE_DURATION = 300; // ms
4094 private static final float CHEEK_THRESHOLD = 0.6f;
4095 private int mEventState = EVENT_NONE;
4096 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 private int eventType(MotionEvent ev) {
4099 float size = ev.getSize();
4100 switch (ev.getAction()) {
4101 case MotionEvent.ACTION_DOWN:
4102 mEventSize = size;
4103 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4104 case MotionEvent.ACTION_UP:
4105 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004106 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 case MotionEvent.ACTION_MOVE:
4108 final int N = ev.getHistorySize();
4109 if (size > mEventSize) mEventSize = size;
4110 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4111 for (int i=0; i<N; i++) {
4112 size = ev.getHistoricalSize(i);
4113 if (size > mEventSize) mEventSize = size;
4114 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4115 }
4116 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4117 return TOUCH_EVENT;
4118 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004119 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120 }
4121 default:
4122 // not good
4123 return OTHER_EVENT;
4124 }
4125 }
4126
4127 /**
4128 * @return Returns true if event was dispatched, false if it was dropped for any reason
4129 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004130 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4132 "dispatchPointer " + ev);
4133
Michael Chan53071d62009-05-13 17:29:48 -07004134 if (MEASURE_LATENCY) {
4135 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4136 }
4137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004139 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140
Michael Chan53071d62009-05-13 17:29:48 -07004141 if (MEASURE_LATENCY) {
4142 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4143 }
4144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 if (action == MotionEvent.ACTION_UP) {
4148 // let go of our target
4149 mKeyWaiter.mMotionTarget = null;
4150 mPowerManager.logPointerUpEvent();
4151 } else if (action == MotionEvent.ACTION_DOWN) {
4152 mPowerManager.logPointerDownEvent();
4153 }
4154
4155 if (targetObj == null) {
4156 // In this case we are either dropping the event, or have received
4157 // a move or up without a down. It is common to receive move
4158 // events in such a way, since this means the user is moving the
4159 // pointer without actually pressing down. All other cases should
4160 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004161 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4163 }
4164 if (qev != null) {
4165 mQueue.recycleEvent(qev);
4166 }
4167 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004168 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 }
4170 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4171 if (qev != null) {
4172 mQueue.recycleEvent(qev);
4173 }
4174 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004175 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 }
Romain Guy06882f82009-06-10 13:36:04 -07004177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004181 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182
4183 //Log.i(TAG, "Sending " + ev + " to " + target);
4184
4185 if (uid != 0 && uid != target.mSession.mUid) {
4186 if (mContext.checkPermission(
4187 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4188 != PackageManager.PERMISSION_GRANTED) {
4189 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4190 + pid + " uid " + uid + " to window " + target
4191 + " owned by uid " + target.mSession.mUid);
4192 if (qev != null) {
4193 mQueue.recycleEvent(qev);
4194 }
4195 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004196 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 }
4198 }
4199
Michael Chan53071d62009-05-13 17:29:48 -07004200 if (MEASURE_LATENCY) {
4201 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4202 }
4203
Romain Guy06882f82009-06-10 13:36:04 -07004204 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4206 //target wants to ignore fat touch events
4207 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4208 //explicit flag to return without processing event further
4209 boolean returnFlag = false;
4210 if((action == MotionEvent.ACTION_DOWN)) {
4211 mFatTouch = false;
4212 if(cheekPress) {
4213 mFatTouch = true;
4214 returnFlag = true;
4215 }
4216 } else {
4217 if(action == MotionEvent.ACTION_UP) {
4218 if(mFatTouch) {
4219 //earlier even was invalid doesnt matter if current up is cheekpress or not
4220 mFatTouch = false;
4221 returnFlag = true;
4222 } else if(cheekPress) {
4223 //cancel the earlier event
4224 ev.setAction(MotionEvent.ACTION_CANCEL);
4225 action = MotionEvent.ACTION_CANCEL;
4226 }
4227 } else if(action == MotionEvent.ACTION_MOVE) {
4228 if(mFatTouch) {
4229 //two cases here
4230 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004231 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 returnFlag = true;
4233 } else if(cheekPress) {
4234 //valid down followed by invalid moves
4235 //an invalid move have to cancel earlier action
4236 ev.setAction(MotionEvent.ACTION_CANCEL);
4237 action = MotionEvent.ACTION_CANCEL;
4238 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4239 //note that the subsequent invalid moves will not get here
4240 mFatTouch = true;
4241 }
4242 }
4243 } //else if action
4244 if(returnFlag) {
4245 //recycle que, ev
4246 if (qev != null) {
4247 mQueue.recycleEvent(qev);
4248 }
4249 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004250 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 }
4252 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004253
Michael Chan9f028e62009-08-04 17:37:46 -07004254 // Enable this for testing the "right" value
4255 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004256 int max_events_per_sec = 35;
4257 try {
4258 max_events_per_sec = Integer.parseInt(SystemProperties
4259 .get("windowsmgr.max_events_per_sec"));
4260 if (max_events_per_sec < 1) {
4261 max_events_per_sec = 35;
4262 }
4263 } catch (NumberFormatException e) {
4264 }
4265 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4266 }
4267
4268 /*
4269 * Throttle events to minimize CPU usage when there's a flood of events
4270 * e.g. constant contact with the screen
4271 */
4272 if (action == MotionEvent.ACTION_MOVE) {
4273 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4274 long now = SystemClock.uptimeMillis();
4275 if (now < nextEventTime) {
4276 try {
4277 Thread.sleep(nextEventTime - now);
4278 } catch (InterruptedException e) {
4279 }
4280 mLastTouchEventTime = nextEventTime;
4281 } else {
4282 mLastTouchEventTime = now;
4283 }
4284 }
4285
Michael Chan53071d62009-05-13 17:29:48 -07004286 if (MEASURE_LATENCY) {
4287 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4288 }
4289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004290 synchronized(mWindowMap) {
4291 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4292 mKeyWaiter.bindTargetWindowLocked(target,
4293 KeyWaiter.RETURN_PENDING_POINTER, qev);
4294 ev = null;
4295 } else {
4296 if (action == MotionEvent.ACTION_DOWN) {
4297 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4298 if (out != null) {
4299 MotionEvent oev = MotionEvent.obtain(ev);
4300 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4301 do {
4302 final Rect frame = out.mFrame;
4303 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4304 try {
4305 out.mClient.dispatchPointer(oev, eventTime);
4306 } catch (android.os.RemoteException e) {
4307 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4308 }
4309 oev.offsetLocation((float)frame.left, (float)frame.top);
4310 out = out.mNextOutsideTouch;
4311 } while (out != null);
4312 mKeyWaiter.mOutsideTouchTargets = null;
4313 }
4314 }
4315 final Rect frame = target.mFrame;
4316 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4317 mKeyWaiter.bindTargetWindowLocked(target);
4318 }
4319 }
Romain Guy06882f82009-06-10 13:36:04 -07004320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 // finally offset the event to the target's coordinate system and
4322 // dispatch the event.
4323 try {
4324 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4325 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4326 }
Michael Chan53071d62009-05-13 17:29:48 -07004327
4328 if (MEASURE_LATENCY) {
4329 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4330 }
4331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332 target.mClient.dispatchPointer(ev, eventTime);
Michael Chan53071d62009-05-13 17:29:48 -07004333
4334 if (MEASURE_LATENCY) {
4335 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4336 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004337 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 } catch (android.os.RemoteException e) {
4339 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4340 mKeyWaiter.mMotionTarget = null;
4341 try {
4342 removeWindow(target.mSession, target.mClient);
4343 } catch (java.util.NoSuchElementException ex) {
4344 // This will happen if the window has already been
4345 // removed.
4346 }
4347 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004348 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 }
Romain Guy06882f82009-06-10 13:36:04 -07004350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 /**
4352 * @return Returns true if event was dispatched, false if it was dropped for any reason
4353 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004354 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 if (DEBUG_INPUT) Log.v(
4356 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004359 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 if (focusObj == null) {
4361 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4362 if (qev != null) {
4363 mQueue.recycleEvent(qev);
4364 }
4365 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004366 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 }
4368 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4369 if (qev != null) {
4370 mQueue.recycleEvent(qev);
4371 }
4372 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004373 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 }
Romain Guy06882f82009-06-10 13:36:04 -07004375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 if (uid != 0 && uid != focus.mSession.mUid) {
4379 if (mContext.checkPermission(
4380 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4381 != PackageManager.PERMISSION_GRANTED) {
4382 Log.w(TAG, "Permission denied: injecting key event from pid "
4383 + pid + " uid " + uid + " to window " + focus
4384 + " owned by uid " + focus.mSession.mUid);
4385 if (qev != null) {
4386 mQueue.recycleEvent(qev);
4387 }
4388 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004389 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 }
4391 }
Romain Guy06882f82009-06-10 13:36:04 -07004392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 synchronized(mWindowMap) {
4396 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4397 mKeyWaiter.bindTargetWindowLocked(focus,
4398 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4399 // We don't deliver movement events to the client, we hold
4400 // them and wait for them to call back.
4401 ev = null;
4402 } else {
4403 mKeyWaiter.bindTargetWindowLocked(focus);
4404 }
4405 }
Romain Guy06882f82009-06-10 13:36:04 -07004406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 try {
4408 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004409 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 } catch (android.os.RemoteException e) {
4411 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4412 try {
4413 removeWindow(focus.mSession, focus.mClient);
4414 } catch (java.util.NoSuchElementException ex) {
4415 // This will happen if the window has already been
4416 // removed.
4417 }
4418 }
Romain Guy06882f82009-06-10 13:36:04 -07004419
Dianne Hackborncfaef692009-06-15 14:24:44 -07004420 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 }
Romain Guy06882f82009-06-10 13:36:04 -07004422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 /**
4424 * @return Returns true if event was dispatched, false if it was dropped for any reason
4425 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004426 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4428
4429 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004430 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 if (focusObj == null) {
4432 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004433 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 }
4435 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004436 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437 }
Romain Guy06882f82009-06-10 13:36:04 -07004438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 if (DEBUG_INPUT) Log.v(
4442 TAG, "Dispatching to " + focus + ": " + event);
4443
4444 if (uid != 0 && uid != focus.mSession.mUid) {
4445 if (mContext.checkPermission(
4446 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4447 != PackageManager.PERMISSION_GRANTED) {
4448 Log.w(TAG, "Permission denied: injecting key event from pid "
4449 + pid + " uid " + uid + " to window " + focus
4450 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004451 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 }
4453 }
Romain Guy06882f82009-06-10 13:36:04 -07004454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 synchronized(mWindowMap) {
4456 mKeyWaiter.bindTargetWindowLocked(focus);
4457 }
4458
4459 // NOSHIP extra state logging
4460 mKeyWaiter.recordDispatchState(event, focus);
4461 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 try {
4464 if (DEBUG_INPUT || DEBUG_FOCUS) {
4465 Log.v(TAG, "Delivering key " + event.getKeyCode()
4466 + " to " + focus);
4467 }
4468 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004469 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004470 } catch (android.os.RemoteException e) {
4471 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4472 try {
4473 removeWindow(focus.mSession, focus.mClient);
4474 } catch (java.util.NoSuchElementException ex) {
4475 // This will happen if the window has already been
4476 // removed.
4477 }
4478 }
Romain Guy06882f82009-06-10 13:36:04 -07004479
Dianne Hackborncfaef692009-06-15 14:24:44 -07004480 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004481 }
Romain Guy06882f82009-06-10 13:36:04 -07004482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 public void pauseKeyDispatching(IBinder _token) {
4484 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4485 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004486 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 }
4488
4489 synchronized (mWindowMap) {
4490 WindowToken token = mTokenMap.get(_token);
4491 if (token != null) {
4492 mKeyWaiter.pauseDispatchingLocked(token);
4493 }
4494 }
4495 }
4496
4497 public void resumeKeyDispatching(IBinder _token) {
4498 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4499 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004500 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 }
4502
4503 synchronized (mWindowMap) {
4504 WindowToken token = mTokenMap.get(_token);
4505 if (token != null) {
4506 mKeyWaiter.resumeDispatchingLocked(token);
4507 }
4508 }
4509 }
4510
4511 public void setEventDispatching(boolean enabled) {
4512 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4513 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004514 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 }
4516
4517 synchronized (mWindowMap) {
4518 mKeyWaiter.setEventDispatchingLocked(enabled);
4519 }
4520 }
Romain Guy06882f82009-06-10 13:36:04 -07004521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 /**
4523 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004524 *
4525 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004526 * {@link SystemClock#uptimeMillis()} as the timebase.)
4527 * @param sync If true, wait for the event to be completed before returning to the caller.
4528 * @return Returns true if event was dispatched, false if it was dropped for any reason
4529 */
4530 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4531 long downTime = ev.getDownTime();
4532 long eventTime = ev.getEventTime();
4533
4534 int action = ev.getAction();
4535 int code = ev.getKeyCode();
4536 int repeatCount = ev.getRepeatCount();
4537 int metaState = ev.getMetaState();
4538 int deviceId = ev.getDeviceId();
4539 int scancode = ev.getScanCode();
4540
4541 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4542 if (downTime == 0) downTime = eventTime;
4543
4544 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004545 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004547 final int pid = Binder.getCallingPid();
4548 final int uid = Binder.getCallingUid();
4549 final long ident = Binder.clearCallingIdentity();
4550 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004552 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004553 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004554 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004555 switch (result) {
4556 case INJECT_NO_PERMISSION:
4557 throw new SecurityException(
4558 "Injecting to another application requires INJECT_EVENT permission");
4559 case INJECT_SUCCEEDED:
4560 return true;
4561 }
4562 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 }
4564
4565 /**
4566 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004567 *
4568 * @param ev A motion event describing the pointer (touch) action. (As noted in
4569 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570 * {@link SystemClock#uptimeMillis()} as the timebase.)
4571 * @param sync If true, wait for the event to be completed before returning to the caller.
4572 * @return Returns true if event was dispatched, false if it was dropped for any reason
4573 */
4574 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004575 final int pid = Binder.getCallingPid();
4576 final int uid = Binder.getCallingUid();
4577 final long ident = Binder.clearCallingIdentity();
4578 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004580 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004582 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004583 switch (result) {
4584 case INJECT_NO_PERMISSION:
4585 throw new SecurityException(
4586 "Injecting to another application requires INJECT_EVENT permission");
4587 case INJECT_SUCCEEDED:
4588 return true;
4589 }
4590 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 }
Romain Guy06882f82009-06-10 13:36:04 -07004592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 /**
4594 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004595 *
4596 * @param ev A motion event describing the trackball action. (As noted in
4597 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 * {@link SystemClock#uptimeMillis()} as the timebase.)
4599 * @param sync If true, wait for the event to be completed before returning to the caller.
4600 * @return Returns true if event was dispatched, false if it was dropped for any reason
4601 */
4602 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004603 final int pid = Binder.getCallingPid();
4604 final int uid = Binder.getCallingUid();
4605 final long ident = Binder.clearCallingIdentity();
4606 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004608 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004609 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004610 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004611 switch (result) {
4612 case INJECT_NO_PERMISSION:
4613 throw new SecurityException(
4614 "Injecting to another application requires INJECT_EVENT permission");
4615 case INJECT_SUCCEEDED:
4616 return true;
4617 }
4618 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004619 }
Romain Guy06882f82009-06-10 13:36:04 -07004620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004621 private WindowState getFocusedWindow() {
4622 synchronized (mWindowMap) {
4623 return getFocusedWindowLocked();
4624 }
4625 }
4626
4627 private WindowState getFocusedWindowLocked() {
4628 return mCurrentFocus;
4629 }
Romain Guy06882f82009-06-10 13:36:04 -07004630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004631 /**
4632 * This class holds the state for dispatching key events. This state
4633 * is protected by the KeyWaiter instance, NOT by the window lock. You
4634 * can be holding the main window lock while acquire the KeyWaiter lock,
4635 * but not the other way around.
4636 */
4637 final class KeyWaiter {
4638 // NOSHIP debugging
4639 public class DispatchState {
4640 private KeyEvent event;
4641 private WindowState focus;
4642 private long time;
4643 private WindowState lastWin;
4644 private IBinder lastBinder;
4645 private boolean finished;
4646 private boolean gotFirstWindow;
4647 private boolean eventDispatching;
4648 private long timeToSwitch;
4649 private boolean wasFrozen;
4650 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004651 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4654 focus = theFocus;
4655 event = theEvent;
4656 time = System.currentTimeMillis();
4657 // snapshot KeyWaiter state
4658 lastWin = mLastWin;
4659 lastBinder = mLastBinder;
4660 finished = mFinished;
4661 gotFirstWindow = mGotFirstWindow;
4662 eventDispatching = mEventDispatching;
4663 timeToSwitch = mTimeToSwitch;
4664 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004665 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 // cache the paused state at ctor time as well
4667 if (theFocus == null || theFocus.mToken == null) {
4668 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4669 focusPaused = false;
4670 } else {
4671 focusPaused = theFocus.mToken.paused;
4672 }
4673 }
Romain Guy06882f82009-06-10 13:36:04 -07004674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 public String toString() {
4676 return "{{" + event + " to " + focus + " @ " + time
4677 + " lw=" + lastWin + " lb=" + lastBinder
4678 + " fin=" + finished + " gfw=" + gotFirstWindow
4679 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004680 + " wf=" + wasFrozen + " fp=" + focusPaused
4681 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682 }
4683 };
4684 private DispatchState mDispatchState = null;
4685 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4686 mDispatchState = new DispatchState(theEvent, theFocus);
4687 }
4688 // END NOSHIP
4689
4690 public static final int RETURN_NOTHING = 0;
4691 public static final int RETURN_PENDING_POINTER = 1;
4692 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 final Object SKIP_TARGET_TOKEN = new Object();
4695 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 private WindowState mLastWin = null;
4698 private IBinder mLastBinder = null;
4699 private boolean mFinished = true;
4700 private boolean mGotFirstWindow = false;
4701 private boolean mEventDispatching = true;
4702 private long mTimeToSwitch = 0;
4703 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 // Target of Motion events
4706 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004708 // Windows above the target who would like to receive an "outside"
4709 // touch event for any down events outside of them.
4710 WindowState mOutsideTouchTargets;
4711
4712 /**
4713 * Wait for the last event dispatch to complete, then find the next
4714 * target that should receive the given event and wait for that one
4715 * to be ready to receive it.
4716 */
4717 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4718 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004719 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 long startTime = SystemClock.uptimeMillis();
4721 long keyDispatchingTimeout = 5 * 1000;
4722 long waitedFor = 0;
4723
4724 while (true) {
4725 // Figure out which window we care about. It is either the
4726 // last window we are waiting to have process the event or,
4727 // if none, then the next window we think the event should go
4728 // to. Note: we retrieve mLastWin outside of the lock, so
4729 // it may change before we lock. Thus we must check it again.
4730 WindowState targetWin = mLastWin;
4731 boolean targetIsNew = targetWin == null;
4732 if (DEBUG_INPUT) Log.v(
4733 TAG, "waitForLastKey: mFinished=" + mFinished +
4734 ", mLastWin=" + mLastWin);
4735 if (targetIsNew) {
4736 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004737 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004738 if (target == SKIP_TARGET_TOKEN) {
4739 // The user has pressed a special key, and we are
4740 // dropping all pending events before it.
4741 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4742 + " " + nextMotion);
4743 return null;
4744 }
4745 if (target == CONSUMED_EVENT_TOKEN) {
4746 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4747 + " " + nextMotion);
4748 return target;
4749 }
4750 targetWin = (WindowState)target;
4751 }
Romain Guy06882f82009-06-10 13:36:04 -07004752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004753 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755 // Now: is it okay to send the next event to this window?
4756 synchronized (this) {
4757 // First: did we come here based on the last window not
4758 // being null, but it changed by the time we got here?
4759 // If so, try again.
4760 if (!targetIsNew && mLastWin == null) {
4761 continue;
4762 }
Romain Guy06882f82009-06-10 13:36:04 -07004763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004764 // We never dispatch events if not finished with the
4765 // last one, or the display is frozen.
4766 if (mFinished && !mDisplayFrozen) {
4767 // If event dispatching is disabled, then we
4768 // just consume the events.
4769 if (!mEventDispatching) {
4770 if (DEBUG_INPUT) Log.v(TAG,
4771 "Skipping event; dispatching disabled: "
4772 + nextKey + " " + nextMotion);
4773 return null;
4774 }
4775 if (targetWin != null) {
4776 // If this is a new target, and that target is not
4777 // paused or unresponsive, then all looks good to
4778 // handle the event.
4779 if (targetIsNew && !targetWin.mToken.paused) {
4780 return targetWin;
4781 }
Romain Guy06882f82009-06-10 13:36:04 -07004782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783 // If we didn't find a target window, and there is no
4784 // focused app window, then just eat the events.
4785 } else if (mFocusedApp == null) {
4786 if (DEBUG_INPUT) Log.v(TAG,
4787 "Skipping event; no focused app: "
4788 + nextKey + " " + nextMotion);
4789 return null;
4790 }
4791 }
Romain Guy06882f82009-06-10 13:36:04 -07004792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004793 if (DEBUG_INPUT) Log.v(
4794 TAG, "Waiting for last key in " + mLastBinder
4795 + " target=" + targetWin
4796 + " mFinished=" + mFinished
4797 + " mDisplayFrozen=" + mDisplayFrozen
4798 + " targetIsNew=" + targetIsNew
4799 + " paused="
4800 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004801 + " mFocusedApp=" + mFocusedApp
4802 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004804 targetApp = targetWin != null
4805 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 long curTimeout = keyDispatchingTimeout;
4808 if (mTimeToSwitch != 0) {
4809 long now = SystemClock.uptimeMillis();
4810 if (mTimeToSwitch <= now) {
4811 // If an app switch key has been pressed, and we have
4812 // waited too long for the current app to finish
4813 // processing keys, then wait no more!
4814 doFinishedKeyLocked(true);
4815 continue;
4816 }
4817 long switchTimeout = mTimeToSwitch - now;
4818 if (curTimeout > switchTimeout) {
4819 curTimeout = switchTimeout;
4820 }
4821 }
Romain Guy06882f82009-06-10 13:36:04 -07004822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 try {
4824 // after that continue
4825 // processing keys, so we don't get stuck.
4826 if (DEBUG_INPUT) Log.v(
4827 TAG, "Waiting for key dispatch: " + curTimeout);
4828 wait(curTimeout);
4829 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4830 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004831 + startTime + " switchTime=" + mTimeToSwitch
4832 + " target=" + targetWin + " mLW=" + mLastWin
4833 + " mLB=" + mLastBinder + " fin=" + mFinished
4834 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004835 } catch (InterruptedException e) {
4836 }
4837 }
4838
4839 // If we were frozen during configuration change, restart the
4840 // timeout checks from now; otherwise look at whether we timed
4841 // out before awakening.
4842 if (mWasFrozen) {
4843 waitedFor = 0;
4844 mWasFrozen = false;
4845 } else {
4846 waitedFor = SystemClock.uptimeMillis() - startTime;
4847 }
4848
4849 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4850 IApplicationToken at = null;
4851 synchronized (this) {
4852 Log.w(TAG, "Key dispatching timed out sending to " +
4853 (targetWin != null ? targetWin.mAttrs.getTitle()
4854 : "<null>"));
4855 // NOSHIP debugging
4856 Log.w(TAG, "Dispatch state: " + mDispatchState);
4857 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4858 // END NOSHIP
4859 //dump();
4860 if (targetWin != null) {
4861 at = targetWin.getAppToken();
4862 } else if (targetApp != null) {
4863 at = targetApp.appToken;
4864 }
4865 }
4866
4867 boolean abort = true;
4868 if (at != null) {
4869 try {
4870 long timeout = at.getKeyDispatchingTimeout();
4871 if (timeout > waitedFor) {
4872 // we did not wait the proper amount of time for this application.
4873 // set the timeout to be the real timeout and wait again.
4874 keyDispatchingTimeout = timeout - waitedFor;
4875 continue;
4876 } else {
4877 abort = at.keyDispatchingTimedOut();
4878 }
4879 } catch (RemoteException ex) {
4880 }
4881 }
4882
4883 synchronized (this) {
4884 if (abort && (mLastWin == targetWin || targetWin == null)) {
4885 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004886 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 if (DEBUG_INPUT) Log.v(TAG,
4888 "Window " + mLastWin +
4889 " timed out on key input");
4890 if (mLastWin.mToken.paused) {
4891 Log.w(TAG, "Un-pausing dispatching to this window");
4892 mLastWin.mToken.paused = false;
4893 }
4894 }
4895 if (mMotionTarget == targetWin) {
4896 mMotionTarget = null;
4897 }
4898 mLastWin = null;
4899 mLastBinder = null;
4900 if (failIfTimeout || targetWin == null) {
4901 return null;
4902 }
4903 } else {
4904 Log.w(TAG, "Continuing to wait for key to be dispatched");
4905 startTime = SystemClock.uptimeMillis();
4906 }
4907 }
4908 }
4909 }
4910 }
Romain Guy06882f82009-06-10 13:36:04 -07004911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004913 MotionEvent nextMotion, boolean isPointerEvent,
4914 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004915 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004917 if (nextKey != null) {
4918 // Find the target window for a normal key event.
4919 final int keycode = nextKey.getKeyCode();
4920 final int repeatCount = nextKey.getRepeatCount();
4921 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4922 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004925 if (callingUid == 0 ||
4926 mContext.checkPermission(
4927 android.Manifest.permission.INJECT_EVENTS,
4928 callingPid, callingUid)
4929 == PackageManager.PERMISSION_GRANTED) {
4930 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07004931 nextKey.getMetaState(), down, repeatCount,
4932 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 Log.w(TAG, "Event timeout during app switch: dropping "
4935 + nextKey);
4936 return SKIP_TARGET_TOKEN;
4937 }
Romain Guy06882f82009-06-10 13:36:04 -07004938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 WindowState focus = null;
4942 synchronized(mWindowMap) {
4943 focus = getFocusedWindowLocked();
4944 }
Romain Guy06882f82009-06-10 13:36:04 -07004945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004946 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004947
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004948 if (callingUid == 0 ||
4949 (focus != null && callingUid == focus.mSession.mUid) ||
4950 mContext.checkPermission(
4951 android.Manifest.permission.INJECT_EVENTS,
4952 callingPid, callingUid)
4953 == PackageManager.PERMISSION_GRANTED) {
4954 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07004955 keycode, nextKey.getMetaState(), down, repeatCount,
4956 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004957 return CONSUMED_EVENT_TOKEN;
4958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 }
Romain Guy06882f82009-06-10 13:36:04 -07004960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004961 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 } else if (!isPointerEvent) {
4964 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4965 if (!dispatch) {
4966 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4967 + nextMotion);
4968 return SKIP_TARGET_TOKEN;
4969 }
Romain Guy06882f82009-06-10 13:36:04 -07004970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004971 WindowState focus = null;
4972 synchronized(mWindowMap) {
4973 focus = getFocusedWindowLocked();
4974 }
Romain Guy06882f82009-06-10 13:36:04 -07004975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4977 return focus;
4978 }
Romain Guy06882f82009-06-10 13:36:04 -07004979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980 if (nextMotion == null) {
4981 return SKIP_TARGET_TOKEN;
4982 }
Romain Guy06882f82009-06-10 13:36:04 -07004983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004984 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4985 KeyEvent.KEYCODE_UNKNOWN);
4986 if (!dispatch) {
4987 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4988 + nextMotion);
4989 return SKIP_TARGET_TOKEN;
4990 }
Romain Guy06882f82009-06-10 13:36:04 -07004991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004992 // Find the target window for a pointer event.
4993 int action = nextMotion.getAction();
4994 final float xf = nextMotion.getX();
4995 final float yf = nextMotion.getY();
4996 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004998 final boolean screenWasOff = qev != null
4999 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005003 synchronized(mWindowMap) {
5004 synchronized (this) {
5005 if (action == MotionEvent.ACTION_DOWN) {
5006 if (mMotionTarget != null) {
5007 // this is weird, we got a pen down, but we thought it was
5008 // already down!
5009 // XXX: We should probably send an ACTION_UP to the current
5010 // target.
5011 Log.w(TAG, "Pointer down received while already down in: "
5012 + mMotionTarget);
5013 mMotionTarget = null;
5014 }
Romain Guy06882f82009-06-10 13:36:04 -07005015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 // ACTION_DOWN is special, because we need to lock next events to
5017 // the window we'll land onto.
5018 final int x = (int)xf;
5019 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005021 final ArrayList windows = mWindows;
5022 final int N = windows.size();
5023 WindowState topErrWindow = null;
5024 final Rect tmpRect = mTempRect;
5025 for (int i=N-1; i>=0; i--) {
5026 WindowState child = (WindowState)windows.get(i);
5027 //Log.i(TAG, "Checking dispatch to: " + child);
5028 final int flags = child.mAttrs.flags;
5029 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5030 if (topErrWindow == null) {
5031 topErrWindow = child;
5032 }
5033 }
5034 if (!child.isVisibleLw()) {
5035 //Log.i(TAG, "Not visible!");
5036 continue;
5037 }
5038 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5039 //Log.i(TAG, "Not touchable!");
5040 if ((flags & WindowManager.LayoutParams
5041 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5042 child.mNextOutsideTouch = mOutsideTouchTargets;
5043 mOutsideTouchTargets = child;
5044 }
5045 continue;
5046 }
5047 tmpRect.set(child.mFrame);
5048 if (child.mTouchableInsets == ViewTreeObserver
5049 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5050 // The touch is inside of the window if it is
5051 // inside the frame, AND the content part of that
5052 // frame that was given by the application.
5053 tmpRect.left += child.mGivenContentInsets.left;
5054 tmpRect.top += child.mGivenContentInsets.top;
5055 tmpRect.right -= child.mGivenContentInsets.right;
5056 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5057 } else if (child.mTouchableInsets == ViewTreeObserver
5058 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5059 // The touch is inside of the window if it is
5060 // inside the frame, AND the visible part of that
5061 // frame that was given by the application.
5062 tmpRect.left += child.mGivenVisibleInsets.left;
5063 tmpRect.top += child.mGivenVisibleInsets.top;
5064 tmpRect.right -= child.mGivenVisibleInsets.right;
5065 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5066 }
5067 final int touchFlags = flags &
5068 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5069 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5070 if (tmpRect.contains(x, y) || touchFlags == 0) {
5071 //Log.i(TAG, "Using this target!");
5072 if (!screenWasOff || (flags &
5073 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5074 mMotionTarget = child;
5075 } else {
5076 //Log.i(TAG, "Waking, skip!");
5077 mMotionTarget = null;
5078 }
5079 break;
5080 }
Romain Guy06882f82009-06-10 13:36:04 -07005081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 if ((flags & WindowManager.LayoutParams
5083 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5084 child.mNextOutsideTouch = mOutsideTouchTargets;
5085 mOutsideTouchTargets = child;
5086 //Log.i(TAG, "Adding to outside target list: " + child);
5087 }
5088 }
5089
5090 // if there's an error window but it's not accepting
5091 // focus (typically because it is not yet visible) just
5092 // wait for it -- any other focused window may in fact
5093 // be in ANR state.
5094 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5095 mMotionTarget = null;
5096 }
5097 }
Romain Guy06882f82009-06-10 13:36:04 -07005098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099 target = mMotionTarget;
5100 }
5101 }
Romain Guy06882f82009-06-10 13:36:04 -07005102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 // Pointer events are a little different -- if there isn't a
5106 // target found for any event, then just drop it.
5107 return target != null ? target : SKIP_TARGET_TOKEN;
5108 }
Romain Guy06882f82009-06-10 13:36:04 -07005109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 boolean checkShouldDispatchKey(int keycode) {
5111 synchronized (this) {
5112 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5113 mTimeToSwitch = 0;
5114 return true;
5115 }
5116 if (mTimeToSwitch != 0
5117 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5118 return false;
5119 }
5120 return true;
5121 }
5122 }
Romain Guy06882f82009-06-10 13:36:04 -07005123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 void bindTargetWindowLocked(WindowState win,
5125 int pendingWhat, QueuedEvent pendingMotion) {
5126 synchronized (this) {
5127 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5128 }
5129 }
Romain Guy06882f82009-06-10 13:36:04 -07005130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131 void bindTargetWindowLocked(WindowState win) {
5132 synchronized (this) {
5133 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5134 }
5135 }
5136
5137 void bindTargetWindowLockedLocked(WindowState win,
5138 int pendingWhat, QueuedEvent pendingMotion) {
5139 mLastWin = win;
5140 mLastBinder = win.mClient.asBinder();
5141 mFinished = false;
5142 if (pendingMotion != null) {
5143 final Session s = win.mSession;
5144 if (pendingWhat == RETURN_PENDING_POINTER) {
5145 releasePendingPointerLocked(s);
5146 s.mPendingPointerMove = pendingMotion;
5147 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005148 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 "bindTargetToWindow " + s.mPendingPointerMove);
5150 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5151 releasePendingTrackballLocked(s);
5152 s.mPendingTrackballMove = pendingMotion;
5153 s.mPendingTrackballWindow = win;
5154 }
5155 }
5156 }
Romain Guy06882f82009-06-10 13:36:04 -07005157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005158 void releasePendingPointerLocked(Session s) {
5159 if (DEBUG_INPUT) Log.v(TAG,
5160 "releasePendingPointer " + s.mPendingPointerMove);
5161 if (s.mPendingPointerMove != null) {
5162 mQueue.recycleEvent(s.mPendingPointerMove);
5163 s.mPendingPointerMove = null;
5164 }
5165 }
Romain Guy06882f82009-06-10 13:36:04 -07005166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 void releasePendingTrackballLocked(Session s) {
5168 if (s.mPendingTrackballMove != null) {
5169 mQueue.recycleEvent(s.mPendingTrackballMove);
5170 s.mPendingTrackballMove = null;
5171 }
5172 }
Romain Guy06882f82009-06-10 13:36:04 -07005173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005174 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5175 int returnWhat) {
5176 if (DEBUG_INPUT) Log.v(
5177 TAG, "finishedKey: client=" + client + ", force=" + force);
5178
5179 if (client == null) {
5180 return null;
5181 }
5182
5183 synchronized (this) {
5184 if (DEBUG_INPUT) Log.v(
5185 TAG, "finishedKey: client=" + client.asBinder()
5186 + ", force=" + force + ", last=" + mLastBinder
5187 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5188
5189 QueuedEvent qev = null;
5190 WindowState win = null;
5191 if (returnWhat == RETURN_PENDING_POINTER) {
5192 qev = session.mPendingPointerMove;
5193 win = session.mPendingPointerWindow;
5194 session.mPendingPointerMove = null;
5195 session.mPendingPointerWindow = null;
5196 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5197 qev = session.mPendingTrackballMove;
5198 win = session.mPendingTrackballWindow;
5199 session.mPendingTrackballMove = null;
5200 session.mPendingTrackballWindow = null;
5201 }
Romain Guy06882f82009-06-10 13:36:04 -07005202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 if (mLastBinder == client.asBinder()) {
5204 if (DEBUG_INPUT) Log.v(
5205 TAG, "finishedKey: last paused="
5206 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5207 if (mLastWin != null && (!mLastWin.mToken.paused || force
5208 || !mEventDispatching)) {
5209 doFinishedKeyLocked(false);
5210 } else {
5211 // Make sure to wake up anyone currently waiting to
5212 // dispatch a key, so they can re-evaluate their
5213 // current situation.
5214 mFinished = true;
5215 notifyAll();
5216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 }
Romain Guy06882f82009-06-10 13:36:04 -07005218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 if (qev != null) {
5220 MotionEvent res = (MotionEvent)qev.event;
5221 if (DEBUG_INPUT) Log.v(TAG,
5222 "Returning pending motion: " + res);
5223 mQueue.recycleEvent(qev);
5224 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5225 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5226 }
5227 return res;
5228 }
5229 return null;
5230 }
5231 }
5232
5233 void tickle() {
5234 synchronized (this) {
5235 notifyAll();
5236 }
5237 }
Romain Guy06882f82009-06-10 13:36:04 -07005238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 void handleNewWindowLocked(WindowState newWindow) {
5240 if (!newWindow.canReceiveKeys()) {
5241 return;
5242 }
5243 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005244 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 TAG, "New key dispatch window: win="
5246 + newWindow.mClient.asBinder()
5247 + ", last=" + mLastBinder
5248 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5249 + "), finished=" + mFinished + ", paused="
5250 + newWindow.mToken.paused);
5251
5252 // Displaying a window implicitly causes dispatching to
5253 // be unpaused. (This is to protect against bugs if someone
5254 // pauses dispatching but forgets to resume.)
5255 newWindow.mToken.paused = false;
5256
5257 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258
5259 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5260 if (DEBUG_INPUT) Log.v(TAG,
5261 "New SYSTEM_ERROR window; resetting state");
5262 mLastWin = null;
5263 mLastBinder = null;
5264 mMotionTarget = null;
5265 mFinished = true;
5266 } else if (mLastWin != null) {
5267 // If the new window is above the window we are
5268 // waiting on, then stop waiting and let key dispatching
5269 // start on the new guy.
5270 if (DEBUG_INPUT) Log.v(
5271 TAG, "Last win layer=" + mLastWin.mLayer
5272 + ", new win layer=" + newWindow.mLayer);
5273 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005274 // The new window is above the old; finish pending input to the last
5275 // window and start directing it to the new one.
5276 mLastWin.mToken.paused = false;
5277 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005278 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005279 // Either the new window is lower, so there is no need to wake key waiters,
5280 // or we just finished key input to the previous window, which implicitly
5281 // notified the key waiters. In both cases, we don't need to issue the
5282 // notification here.
5283 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 }
5285
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005286 // Now that we've put a new window state in place, make the event waiter
5287 // take notice and retarget its attentions.
5288 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 }
5290 }
5291
5292 void pauseDispatchingLocked(WindowToken token) {
5293 synchronized (this)
5294 {
5295 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5296 token.paused = true;
5297
5298 /*
5299 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5300 mPaused = true;
5301 } else {
5302 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005303 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005304 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005305 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005307 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 }
5309 }
5310 */
5311 }
5312 }
5313
5314 void resumeDispatchingLocked(WindowToken token) {
5315 synchronized (this) {
5316 if (token.paused) {
5317 if (DEBUG_INPUT) Log.v(
5318 TAG, "Resuming WindowToken " + token
5319 + ", last=" + mLastBinder
5320 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5321 + "), finished=" + mFinished + ", paused="
5322 + token.paused);
5323 token.paused = false;
5324 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5325 doFinishedKeyLocked(true);
5326 } else {
5327 notifyAll();
5328 }
5329 }
5330 }
5331 }
5332
5333 void setEventDispatchingLocked(boolean enabled) {
5334 synchronized (this) {
5335 mEventDispatching = enabled;
5336 notifyAll();
5337 }
5338 }
Romain Guy06882f82009-06-10 13:36:04 -07005339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340 void appSwitchComing() {
5341 synchronized (this) {
5342 // Don't wait for more than .5 seconds for app to finish
5343 // processing the pending events.
5344 long now = SystemClock.uptimeMillis() + 500;
5345 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5346 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5347 mTimeToSwitch = now;
5348 }
5349 notifyAll();
5350 }
5351 }
Romain Guy06882f82009-06-10 13:36:04 -07005352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005353 private final void doFinishedKeyLocked(boolean doRecycle) {
5354 if (mLastWin != null) {
5355 releasePendingPointerLocked(mLastWin.mSession);
5356 releasePendingTrackballLocked(mLastWin.mSession);
5357 }
Romain Guy06882f82009-06-10 13:36:04 -07005358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359 if (mLastWin == null || !mLastWin.mToken.paused
5360 || !mLastWin.isVisibleLw()) {
5361 // If the current window has been paused, we aren't -really-
5362 // finished... so let the waiters still wait.
5363 mLastWin = null;
5364 mLastBinder = null;
5365 }
5366 mFinished = true;
5367 notifyAll();
5368 }
5369 }
5370
5371 private class KeyQ extends KeyInputQueue
5372 implements KeyInputQueue.FilterCallback {
5373 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005376 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5378 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5379 "KEEP_SCREEN_ON_FLAG");
5380 mHoldingScreen.setReferenceCounted(false);
5381 }
5382
5383 @Override
5384 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5385 if (mPolicy.preprocessInputEventTq(event)) {
5386 return true;
5387 }
Romain Guy06882f82009-06-10 13:36:04 -07005388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 switch (event.type) {
5390 case RawInputEvent.EV_KEY: {
5391 // XXX begin hack
5392 if (DEBUG) {
5393 if (event.keycode == KeyEvent.KEYCODE_G) {
5394 if (event.value != 0) {
5395 // G down
5396 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5397 }
5398 return false;
5399 }
5400 if (event.keycode == KeyEvent.KEYCODE_D) {
5401 if (event.value != 0) {
5402 //dump();
5403 }
5404 return false;
5405 }
5406 }
5407 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 boolean screenIsOff = !mPowerManager.screenIsOn();
5410 boolean screenIsDim = !mPowerManager.screenIsBright();
5411 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005413 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5414 mPowerManager.goToSleep(event.when);
5415 }
5416
5417 if (screenIsOff) {
5418 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5419 }
5420 if (screenIsDim) {
5421 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5422 }
5423 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5424 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005425 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 }
Romain Guy06882f82009-06-10 13:36:04 -07005427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5429 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5430 filterQueue(this);
5431 mKeyWaiter.appSwitchComing();
5432 }
5433 return true;
5434 } else {
5435 return false;
5436 }
5437 }
Romain Guy06882f82009-06-10 13:36:04 -07005438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 case RawInputEvent.EV_REL: {
5440 boolean screenIsOff = !mPowerManager.screenIsOn();
5441 boolean screenIsDim = !mPowerManager.screenIsBright();
5442 if (screenIsOff) {
5443 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5444 device.classes, event)) {
5445 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5446 return false;
5447 }
5448 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5449 }
5450 if (screenIsDim) {
5451 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5452 }
5453 return true;
5454 }
Romain Guy06882f82009-06-10 13:36:04 -07005455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 case RawInputEvent.EV_ABS: {
5457 boolean screenIsOff = !mPowerManager.screenIsOn();
5458 boolean screenIsDim = !mPowerManager.screenIsBright();
5459 if (screenIsOff) {
5460 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5461 device.classes, event)) {
5462 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5463 return false;
5464 }
5465 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5466 }
5467 if (screenIsDim) {
5468 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5469 }
5470 return true;
5471 }
Romain Guy06882f82009-06-10 13:36:04 -07005472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 default:
5474 return true;
5475 }
5476 }
5477
5478 public int filterEvent(QueuedEvent ev) {
5479 switch (ev.classType) {
5480 case RawInputEvent.CLASS_KEYBOARD:
5481 KeyEvent ke = (KeyEvent)ev.event;
5482 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5483 Log.w(TAG, "Dropping movement key during app switch: "
5484 + ke.getKeyCode() + ", action=" + ke.getAction());
5485 return FILTER_REMOVE;
5486 }
5487 return FILTER_ABORT;
5488 default:
5489 return FILTER_KEEP;
5490 }
5491 }
Romain Guy06882f82009-06-10 13:36:04 -07005492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 /**
5494 * Must be called with the main window manager lock held.
5495 */
5496 void setHoldScreenLocked(boolean holding) {
5497 boolean state = mHoldingScreen.isHeld();
5498 if (holding != state) {
5499 if (holding) {
5500 mHoldingScreen.acquire();
5501 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005502 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 mHoldingScreen.release();
5504 }
5505 }
5506 }
Michael Chan53071d62009-05-13 17:29:48 -07005507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508
5509 public boolean detectSafeMode() {
5510 mSafeMode = mPolicy.detectSafeMode();
5511 return mSafeMode;
5512 }
Romain Guy06882f82009-06-10 13:36:04 -07005513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 public void systemReady() {
5515 mPolicy.systemReady();
5516 }
Romain Guy06882f82009-06-10 13:36:04 -07005517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 private final class InputDispatcherThread extends Thread {
5519 // Time to wait when there is nothing to do: 9999 seconds.
5520 static final int LONG_WAIT=9999*1000;
5521
5522 public InputDispatcherThread() {
5523 super("InputDispatcher");
5524 }
Romain Guy06882f82009-06-10 13:36:04 -07005525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005526 @Override
5527 public void run() {
5528 while (true) {
5529 try {
5530 process();
5531 } catch (Exception e) {
5532 Log.e(TAG, "Exception in input dispatcher", e);
5533 }
5534 }
5535 }
Romain Guy06882f82009-06-10 13:36:04 -07005536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 private void process() {
5538 android.os.Process.setThreadPriority(
5539 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005541 // The last key event we saw
5542 KeyEvent lastKey = null;
5543
5544 // Last keydown time for auto-repeating keys
5545 long lastKeyTime = SystemClock.uptimeMillis();
5546 long nextKeyTime = lastKeyTime+LONG_WAIT;
5547
Romain Guy06882f82009-06-10 13:36:04 -07005548 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005549 int keyRepeatCount = 0;
5550
5551 // Need to report that configuration has changed?
5552 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005554 while (true) {
5555 long curTime = SystemClock.uptimeMillis();
5556
5557 if (DEBUG_INPUT) Log.v(
5558 TAG, "Waiting for next key: now=" + curTime
5559 + ", repeat @ " + nextKeyTime);
5560
5561 // Retrieve next event, waiting only as long as the next
5562 // repeat timeout. If the configuration has changed, then
5563 // don't wait at all -- we'll report the change as soon as
5564 // we have processed all events.
5565 QueuedEvent ev = mQueue.getEvent(
5566 (int)((!configChanged && curTime < nextKeyTime)
5567 ? (nextKeyTime-curTime) : 0));
5568
5569 if (DEBUG_INPUT && ev != null) Log.v(
5570 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5571
Michael Chan53071d62009-05-13 17:29:48 -07005572 if (MEASURE_LATENCY) {
5573 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5574 }
5575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005576 try {
5577 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005578 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 int eventType;
5580 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5581 eventType = eventType((MotionEvent)ev.event);
5582 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5583 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5584 eventType = LocalPowerManager.BUTTON_EVENT;
5585 } else {
5586 eventType = LocalPowerManager.OTHER_EVENT;
5587 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005588 try {
Michael Chan53071d62009-05-13 17:29:48 -07005589 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005590 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005591 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005592 mBatteryStats.noteInputEvent();
5593 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005594 } catch (RemoteException e) {
5595 // Ignore
5596 }
Michael Chane10de972009-05-18 11:24:50 -07005597
5598 if (eventType != TOUCH_EVENT
5599 && eventType != LONG_TOUCH_EVENT
5600 && eventType != CHEEK_EVENT) {
5601 mPowerManager.userActivity(curTime, false,
5602 eventType, false);
5603 } else if (mLastTouchEventType != eventType
5604 || (curTime - mLastUserActivityCallTime)
5605 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5606 mLastUserActivityCallTime = curTime;
5607 mLastTouchEventType = eventType;
5608 mPowerManager.userActivity(curTime, false,
5609 eventType, false);
5610 }
5611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 switch (ev.classType) {
5613 case RawInputEvent.CLASS_KEYBOARD:
5614 KeyEvent ke = (KeyEvent)ev.event;
5615 if (ke.isDown()) {
5616 lastKey = ke;
5617 keyRepeatCount = 0;
5618 lastKeyTime = curTime;
5619 nextKeyTime = lastKeyTime
5620 + KEY_REPEAT_FIRST_DELAY;
5621 if (DEBUG_INPUT) Log.v(
5622 TAG, "Received key down: first repeat @ "
5623 + nextKeyTime);
5624 } else {
5625 lastKey = null;
5626 // Arbitrary long timeout.
5627 lastKeyTime = curTime;
5628 nextKeyTime = curTime + LONG_WAIT;
5629 if (DEBUG_INPUT) Log.v(
5630 TAG, "Received key up: ignore repeat @ "
5631 + nextKeyTime);
5632 }
5633 dispatchKey((KeyEvent)ev.event, 0, 0);
5634 mQueue.recycleEvent(ev);
5635 break;
5636 case RawInputEvent.CLASS_TOUCHSCREEN:
5637 //Log.i(TAG, "Read next event " + ev);
5638 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5639 break;
5640 case RawInputEvent.CLASS_TRACKBALL:
5641 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5642 break;
5643 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5644 configChanged = true;
5645 break;
5646 default:
5647 mQueue.recycleEvent(ev);
5648 break;
5649 }
Romain Guy06882f82009-06-10 13:36:04 -07005650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 } else if (configChanged) {
5652 configChanged = false;
5653 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655 } else if (lastKey != null) {
5656 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005658 // Timeout occurred while key was down. If it is at or
5659 // past the key repeat time, dispatch the repeat.
5660 if (DEBUG_INPUT) Log.v(
5661 TAG, "Key timeout: repeat=" + nextKeyTime
5662 + ", now=" + curTime);
5663 if (curTime < nextKeyTime) {
5664 continue;
5665 }
Romain Guy06882f82009-06-10 13:36:04 -07005666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 lastKeyTime = nextKeyTime;
5668 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5669 keyRepeatCount++;
5670 if (DEBUG_INPUT) Log.v(
5671 TAG, "Key repeat: count=" + keyRepeatCount
5672 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005673 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675 } else {
5676 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005678 lastKeyTime = curTime;
5679 nextKeyTime = curTime + LONG_WAIT;
5680 }
Romain Guy06882f82009-06-10 13:36:04 -07005681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682 } catch (Exception e) {
5683 Log.e(TAG,
5684 "Input thread received uncaught exception: " + e, e);
5685 }
5686 }
5687 }
5688 }
5689
5690 // -------------------------------------------------------------
5691 // Client Session State
5692 // -------------------------------------------------------------
5693
5694 private final class Session extends IWindowSession.Stub
5695 implements IBinder.DeathRecipient {
5696 final IInputMethodClient mClient;
5697 final IInputContext mInputContext;
5698 final int mUid;
5699 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005700 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 SurfaceSession mSurfaceSession;
5702 int mNumWindow = 0;
5703 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 /**
5706 * Current pointer move event being dispatched to client window... must
5707 * hold key lock to access.
5708 */
5709 QueuedEvent mPendingPointerMove;
5710 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 /**
5713 * Current trackball move event being dispatched to client window... must
5714 * hold key lock to access.
5715 */
5716 QueuedEvent mPendingTrackballMove;
5717 WindowState mPendingTrackballWindow;
5718
5719 public Session(IInputMethodClient client, IInputContext inputContext) {
5720 mClient = client;
5721 mInputContext = inputContext;
5722 mUid = Binder.getCallingUid();
5723 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005724 StringBuilder sb = new StringBuilder();
5725 sb.append("Session{");
5726 sb.append(Integer.toHexString(System.identityHashCode(this)));
5727 sb.append(" uid ");
5728 sb.append(mUid);
5729 sb.append("}");
5730 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 synchronized (mWindowMap) {
5733 if (mInputMethodManager == null && mHaveInputMethods) {
5734 IBinder b = ServiceManager.getService(
5735 Context.INPUT_METHOD_SERVICE);
5736 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5737 }
5738 }
5739 long ident = Binder.clearCallingIdentity();
5740 try {
5741 // Note: it is safe to call in to the input method manager
5742 // here because we are not holding our lock.
5743 if (mInputMethodManager != null) {
5744 mInputMethodManager.addClient(client, inputContext,
5745 mUid, mPid);
5746 } else {
5747 client.setUsingInputMethod(false);
5748 }
5749 client.asBinder().linkToDeath(this, 0);
5750 } catch (RemoteException e) {
5751 // The caller has died, so we can just forget about this.
5752 try {
5753 if (mInputMethodManager != null) {
5754 mInputMethodManager.removeClient(client);
5755 }
5756 } catch (RemoteException ee) {
5757 }
5758 } finally {
5759 Binder.restoreCallingIdentity(ident);
5760 }
5761 }
Romain Guy06882f82009-06-10 13:36:04 -07005762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 @Override
5764 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5765 throws RemoteException {
5766 try {
5767 return super.onTransact(code, data, reply, flags);
5768 } catch (RuntimeException e) {
5769 // Log all 'real' exceptions thrown to the caller
5770 if (!(e instanceof SecurityException)) {
5771 Log.e(TAG, "Window Session Crash", e);
5772 }
5773 throw e;
5774 }
5775 }
5776
5777 public void binderDied() {
5778 // Note: it is safe to call in to the input method manager
5779 // here because we are not holding our lock.
5780 try {
5781 if (mInputMethodManager != null) {
5782 mInputMethodManager.removeClient(mClient);
5783 }
5784 } catch (RemoteException e) {
5785 }
5786 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005787 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 mClientDead = true;
5789 killSessionLocked();
5790 }
5791 }
5792
5793 public int add(IWindow window, WindowManager.LayoutParams attrs,
5794 int viewVisibility, Rect outContentInsets) {
5795 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5796 }
Romain Guy06882f82009-06-10 13:36:04 -07005797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 public void remove(IWindow window) {
5799 removeWindow(this, window);
5800 }
Romain Guy06882f82009-06-10 13:36:04 -07005801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5803 int requestedWidth, int requestedHeight, int viewFlags,
5804 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5805 Rect outVisibleInsets, Surface outSurface) {
5806 return relayoutWindow(this, window, attrs,
5807 requestedWidth, requestedHeight, viewFlags, insetsPending,
5808 outFrame, outContentInsets, outVisibleInsets, outSurface);
5809 }
Romain Guy06882f82009-06-10 13:36:04 -07005810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 public void setTransparentRegion(IWindow window, Region region) {
5812 setTransparentRegionWindow(this, window, region);
5813 }
Romain Guy06882f82009-06-10 13:36:04 -07005814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005815 public void setInsets(IWindow window, int touchableInsets,
5816 Rect contentInsets, Rect visibleInsets) {
5817 setInsetsWindow(this, window, touchableInsets, contentInsets,
5818 visibleInsets);
5819 }
Romain Guy06882f82009-06-10 13:36:04 -07005820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005821 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5822 getWindowDisplayFrame(this, window, outDisplayFrame);
5823 }
Romain Guy06882f82009-06-10 13:36:04 -07005824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005825 public void finishDrawing(IWindow window) {
5826 if (localLOGV) Log.v(
5827 TAG, "IWindow finishDrawing called for " + window);
5828 finishDrawingWindow(this, window);
5829 }
5830
5831 public void finishKey(IWindow window) {
5832 if (localLOGV) Log.v(
5833 TAG, "IWindow finishKey called for " + window);
5834 mKeyWaiter.finishedKey(this, window, false,
5835 KeyWaiter.RETURN_NOTHING);
5836 }
5837
5838 public MotionEvent getPendingPointerMove(IWindow window) {
5839 if (localLOGV) Log.v(
5840 TAG, "IWindow getPendingMotionEvent called for " + window);
5841 return mKeyWaiter.finishedKey(this, window, false,
5842 KeyWaiter.RETURN_PENDING_POINTER);
5843 }
Romain Guy06882f82009-06-10 13:36:04 -07005844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005845 public MotionEvent getPendingTrackballMove(IWindow window) {
5846 if (localLOGV) Log.v(
5847 TAG, "IWindow getPendingMotionEvent called for " + window);
5848 return mKeyWaiter.finishedKey(this, window, false,
5849 KeyWaiter.RETURN_PENDING_TRACKBALL);
5850 }
5851
5852 public void setInTouchMode(boolean mode) {
5853 synchronized(mWindowMap) {
5854 mInTouchMode = mode;
5855 }
5856 }
5857
5858 public boolean getInTouchMode() {
5859 synchronized(mWindowMap) {
5860 return mInTouchMode;
5861 }
5862 }
5863
5864 public boolean performHapticFeedback(IWindow window, int effectId,
5865 boolean always) {
5866 synchronized(mWindowMap) {
5867 long ident = Binder.clearCallingIdentity();
5868 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005869 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005870 windowForClientLocked(this, window), effectId, always);
5871 } finally {
5872 Binder.restoreCallingIdentity(ident);
5873 }
5874 }
5875 }
Romain Guy06882f82009-06-10 13:36:04 -07005876
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07005877 public void setWallpaperPosition(IBinder window, float x, float y) {
5878 synchronized(mWindowMap) {
5879 long ident = Binder.clearCallingIdentity();
5880 try {
5881 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
5882 x, y);
5883 } finally {
5884 Binder.restoreCallingIdentity(ident);
5885 }
5886 }
5887 }
5888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005889 void windowAddedLocked() {
5890 if (mSurfaceSession == null) {
5891 if (localLOGV) Log.v(
5892 TAG, "First window added to " + this + ", creating SurfaceSession");
5893 mSurfaceSession = new SurfaceSession();
5894 mSessions.add(this);
5895 }
5896 mNumWindow++;
5897 }
5898
5899 void windowRemovedLocked() {
5900 mNumWindow--;
5901 killSessionLocked();
5902 }
Romain Guy06882f82009-06-10 13:36:04 -07005903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 void killSessionLocked() {
5905 if (mNumWindow <= 0 && mClientDead) {
5906 mSessions.remove(this);
5907 if (mSurfaceSession != null) {
5908 if (localLOGV) Log.v(
5909 TAG, "Last window removed from " + this
5910 + ", destroying " + mSurfaceSession);
5911 try {
5912 mSurfaceSession.kill();
5913 } catch (Exception e) {
5914 Log.w(TAG, "Exception thrown when killing surface session "
5915 + mSurfaceSession + " in session " + this
5916 + ": " + e.toString());
5917 }
5918 mSurfaceSession = null;
5919 }
5920 }
5921 }
Romain Guy06882f82009-06-10 13:36:04 -07005922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005924 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5925 pw.print(" mClientDead="); pw.print(mClientDead);
5926 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5927 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5928 pw.print(prefix);
5929 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5930 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5931 }
5932 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5933 pw.print(prefix);
5934 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5935 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005937 }
5938
5939 @Override
5940 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005941 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 }
5943 }
5944
5945 // -------------------------------------------------------------
5946 // Client Window State
5947 // -------------------------------------------------------------
5948
5949 private final class WindowState implements WindowManagerPolicy.WindowState {
5950 final Session mSession;
5951 final IWindow mClient;
5952 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005953 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 AppWindowToken mAppToken;
5955 AppWindowToken mTargetAppToken;
5956 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5957 final DeathRecipient mDeathRecipient;
5958 final WindowState mAttachedWindow;
5959 final ArrayList mChildWindows = new ArrayList();
5960 final int mBaseLayer;
5961 final int mSubLayer;
5962 final boolean mLayoutAttached;
5963 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07005964 final boolean mIsWallpaper;
5965 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 int mViewVisibility;
5967 boolean mPolicyVisibility = true;
5968 boolean mPolicyVisibilityAfterAnim = true;
5969 boolean mAppFreezing;
5970 Surface mSurface;
5971 boolean mAttachedHidden; // is our parent window hidden?
5972 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07005973 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 int mRequestedWidth;
5975 int mRequestedHeight;
5976 int mLastRequestedWidth;
5977 int mLastRequestedHeight;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07005978 int mXOffset;
5979 int mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 int mLayer;
5981 int mAnimLayer;
5982 int mLastLayer;
5983 boolean mHaveFrame;
5984
5985 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005987 // Actual frame shown on-screen (may be modified by animation)
5988 final Rect mShownFrame = new Rect();
5989 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 /**
5992 * Insets that determine the actually visible area
5993 */
5994 final Rect mVisibleInsets = new Rect();
5995 final Rect mLastVisibleInsets = new Rect();
5996 boolean mVisibleInsetsChanged;
5997
5998 /**
5999 * Insets that are covered by system windows
6000 */
6001 final Rect mContentInsets = new Rect();
6002 final Rect mLastContentInsets = new Rect();
6003 boolean mContentInsetsChanged;
6004
6005 /**
6006 * Set to true if we are waiting for this window to receive its
6007 * given internal insets before laying out other windows based on it.
6008 */
6009 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 /**
6012 * These are the content insets that were given during layout for
6013 * this window, to be applied to windows behind it.
6014 */
6015 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 /**
6018 * These are the visible insets that were given during layout for
6019 * this window, to be applied to windows behind it.
6020 */
6021 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 /**
6024 * Flag indicating whether the touchable region should be adjusted by
6025 * the visible insets; if false the area outside the visible insets is
6026 * NOT touchable, so we must use those to adjust the frame during hit
6027 * tests.
6028 */
6029 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 // Current transformation being applied.
6032 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6033 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6034 float mHScale=1, mVScale=1;
6035 float mLastHScale=1, mLastVScale=1;
6036 final Matrix mTmpMatrix = new Matrix();
6037
6038 // "Real" frame that the application sees.
6039 final Rect mFrame = new Rect();
6040 final Rect mLastFrame = new Rect();
6041
6042 final Rect mContainingFrame = new Rect();
6043 final Rect mDisplayFrame = new Rect();
6044 final Rect mContentFrame = new Rect();
6045 final Rect mVisibleFrame = new Rect();
6046
6047 float mShownAlpha = 1;
6048 float mAlpha = 1;
6049 float mLastAlpha = 1;
6050
6051 // Set to true if, when the window gets displayed, it should perform
6052 // an enter animation.
6053 boolean mEnterAnimationPending;
6054
6055 // Currently running animation.
6056 boolean mAnimating;
6057 boolean mLocalAnimating;
6058 Animation mAnimation;
6059 boolean mAnimationIsEntrance;
6060 boolean mHasTransformation;
6061 boolean mHasLocalTransformation;
6062 final Transformation mTransformation = new Transformation();
6063
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006064 float mWallpaperX = 0;
6065 float mWallpaperY = 0;
6066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 // This is set after IWindowSession.relayout() has been called at
6068 // least once for the window. It allows us to detect the situation
6069 // where we don't yet have a surface, but should have one soon, so
6070 // we can give the window focus before waiting for the relayout.
6071 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 // This is set after the Surface has been created but before the
6074 // window has been drawn. During this time the surface is hidden.
6075 boolean mDrawPending;
6076
6077 // This is set after the window has finished drawing for the first
6078 // time but before its surface is shown. The surface will be
6079 // displayed when the next layout is run.
6080 boolean mCommitDrawPending;
6081
6082 // This is set during the time after the window's drawing has been
6083 // committed, and before its surface is actually shown. It is used
6084 // to delay showing the surface until all windows in a token are ready
6085 // to be shown.
6086 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006088 // Set when the window has been shown in the screen the first time.
6089 boolean mHasDrawn;
6090
6091 // Currently running an exit animation?
6092 boolean mExiting;
6093
6094 // Currently on the mDestroySurface list?
6095 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006097 // Completely remove from window manager after exit animation?
6098 boolean mRemoveOnExit;
6099
6100 // Set when the orientation is changing and this window has not yet
6101 // been updated for the new orientation.
6102 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006104 // Is this window now (or just being) removed?
6105 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006107 WindowState(Session s, IWindow c, WindowToken token,
6108 WindowState attachedWindow, WindowManager.LayoutParams a,
6109 int viewVisibility) {
6110 mSession = s;
6111 mClient = c;
6112 mToken = token;
6113 mAttrs.copyFrom(a);
6114 mViewVisibility = viewVisibility;
6115 DeathRecipient deathRecipient = new DeathRecipient();
6116 mAlpha = a.alpha;
6117 if (localLOGV) Log.v(
6118 TAG, "Window " + this + " client=" + c.asBinder()
6119 + " token=" + token + " (" + mAttrs.token + ")");
6120 try {
6121 c.asBinder().linkToDeath(deathRecipient, 0);
6122 } catch (RemoteException e) {
6123 mDeathRecipient = null;
6124 mAttachedWindow = null;
6125 mLayoutAttached = false;
6126 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006127 mIsWallpaper = false;
6128 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006129 mBaseLayer = 0;
6130 mSubLayer = 0;
6131 return;
6132 }
6133 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006135 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6136 mAttrs.type <= LAST_SUB_WINDOW)) {
6137 // The multiplier here is to reserve space for multiple
6138 // windows in the same type layer.
6139 mBaseLayer = mPolicy.windowTypeToLayerLw(
6140 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6141 + TYPE_LAYER_OFFSET;
6142 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6143 mAttachedWindow = attachedWindow;
6144 mAttachedWindow.mChildWindows.add(this);
6145 mLayoutAttached = mAttrs.type !=
6146 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6147 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6148 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006149 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6150 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 } else {
6152 // The multiplier here is to reserve space for multiple
6153 // windows in the same type layer.
6154 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6155 * TYPE_LAYER_MULTIPLIER
6156 + TYPE_LAYER_OFFSET;
6157 mSubLayer = 0;
6158 mAttachedWindow = null;
6159 mLayoutAttached = false;
6160 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6161 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006162 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6163 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 }
6165
6166 WindowState appWin = this;
6167 while (appWin.mAttachedWindow != null) {
6168 appWin = mAttachedWindow;
6169 }
6170 WindowToken appToken = appWin.mToken;
6171 while (appToken.appWindowToken == null) {
6172 WindowToken parent = mTokenMap.get(appToken.token);
6173 if (parent == null || appToken == parent) {
6174 break;
6175 }
6176 appToken = parent;
6177 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006178 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 mAppToken = appToken.appWindowToken;
6180
6181 mSurface = null;
6182 mRequestedWidth = 0;
6183 mRequestedHeight = 0;
6184 mLastRequestedWidth = 0;
6185 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006186 mXOffset = 0;
6187 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 mLayer = 0;
6189 mAnimLayer = 0;
6190 mLastLayer = 0;
6191 }
6192
6193 void attach() {
6194 if (localLOGV) Log.v(
6195 TAG, "Attaching " + this + " token=" + mToken
6196 + ", list=" + mToken.windows);
6197 mSession.windowAddedLocked();
6198 }
6199
6200 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6201 mHaveFrame = true;
6202
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006203 final Rect container = mContainingFrame;
6204 container.set(pf);
6205
6206 final Rect display = mDisplayFrame;
6207 display.set(df);
6208
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006209 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006210 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006211 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6212 display.intersect(mCompatibleScreenFrame);
6213 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006214 }
6215
6216 final int pw = container.right - container.left;
6217 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218
6219 int w,h;
6220 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6221 w = mAttrs.width < 0 ? pw : mAttrs.width;
6222 h = mAttrs.height< 0 ? ph : mAttrs.height;
6223 } else {
6224 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6225 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6226 }
Romain Guy06882f82009-06-10 13:36:04 -07006227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006228 final Rect content = mContentFrame;
6229 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 final Rect visible = mVisibleFrame;
6232 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07006235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006236 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6237 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6238
6239 Gravity.apply(mAttrs.gravity, w, h, container,
6240 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6241 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6242
6243 //System.out.println("Out: " + mFrame);
6244
6245 // Now make sure the window fits in the overall display.
6246 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248 // Make sure the content and visible frames are inside of the
6249 // final window frame.
6250 if (content.left < frame.left) content.left = frame.left;
6251 if (content.top < frame.top) content.top = frame.top;
6252 if (content.right > frame.right) content.right = frame.right;
6253 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6254 if (visible.left < frame.left) visible.left = frame.left;
6255 if (visible.top < frame.top) visible.top = frame.top;
6256 if (visible.right > frame.right) visible.right = frame.right;
6257 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006259 final Rect contentInsets = mContentInsets;
6260 contentInsets.left = content.left-frame.left;
6261 contentInsets.top = content.top-frame.top;
6262 contentInsets.right = frame.right-content.right;
6263 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006265 final Rect visibleInsets = mVisibleInsets;
6266 visibleInsets.left = visible.left-frame.left;
6267 visibleInsets.top = visible.top-frame.top;
6268 visibleInsets.right = frame.right-visible.right;
6269 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 if (localLOGV) {
6272 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6273 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6274 Log.v(TAG, "Resolving (mRequestedWidth="
6275 + mRequestedWidth + ", mRequestedheight="
6276 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6277 + "): frame=" + mFrame.toShortString()
6278 + " ci=" + contentInsets.toShortString()
6279 + " vi=" + visibleInsets.toShortString());
6280 //}
6281 }
6282 }
Romain Guy06882f82009-06-10 13:36:04 -07006283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006284 public Rect getFrameLw() {
6285 return mFrame;
6286 }
6287
6288 public Rect getShownFrameLw() {
6289 return mShownFrame;
6290 }
6291
6292 public Rect getDisplayFrameLw() {
6293 return mDisplayFrame;
6294 }
6295
6296 public Rect getContentFrameLw() {
6297 return mContentFrame;
6298 }
6299
6300 public Rect getVisibleFrameLw() {
6301 return mVisibleFrame;
6302 }
6303
6304 public boolean getGivenInsetsPendingLw() {
6305 return mGivenInsetsPending;
6306 }
6307
6308 public Rect getGivenContentInsetsLw() {
6309 return mGivenContentInsets;
6310 }
Romain Guy06882f82009-06-10 13:36:04 -07006311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006312 public Rect getGivenVisibleInsetsLw() {
6313 return mGivenVisibleInsets;
6314 }
Romain Guy06882f82009-06-10 13:36:04 -07006315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 public WindowManager.LayoutParams getAttrs() {
6317 return mAttrs;
6318 }
6319
6320 public int getSurfaceLayer() {
6321 return mLayer;
6322 }
Romain Guy06882f82009-06-10 13:36:04 -07006323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324 public IApplicationToken getAppToken() {
6325 return mAppToken != null ? mAppToken.appToken : null;
6326 }
6327
6328 public boolean hasAppShownWindows() {
6329 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6330 }
6331
6332 public boolean hasAppStartingIcon() {
6333 return mAppToken != null ? (mAppToken.startingData != null) : false;
6334 }
6335
6336 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6337 return mAppToken != null ? mAppToken.startingWindow : null;
6338 }
6339
6340 public void setAnimation(Animation anim) {
6341 if (localLOGV) Log.v(
6342 TAG, "Setting animation in " + this + ": " + anim);
6343 mAnimating = false;
6344 mLocalAnimating = false;
6345 mAnimation = anim;
6346 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6347 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6348 }
6349
6350 public void clearAnimation() {
6351 if (mAnimation != null) {
6352 mAnimating = true;
6353 mLocalAnimating = false;
6354 mAnimation = null;
6355 }
6356 }
Romain Guy06882f82009-06-10 13:36:04 -07006357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006358 Surface createSurfaceLocked() {
6359 if (mSurface == null) {
6360 mDrawPending = true;
6361 mCommitDrawPending = false;
6362 mReadyToShow = false;
6363 if (mAppToken != null) {
6364 mAppToken.allDrawn = false;
6365 }
6366
6367 int flags = 0;
6368 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
6369 flags |= Surface.HARDWARE;
6370 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6371 flags |= Surface.GPU;
6372 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6373 flags |= Surface.PUSH_BUFFERS;
6374 }
6375
6376 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6377 flags |= Surface.SECURE;
6378 }
6379 if (DEBUG_VISIBILITY) Log.v(
6380 TAG, "Creating surface in session "
6381 + mSession.mSurfaceSession + " window " + this
6382 + " w=" + mFrame.width()
6383 + " h=" + mFrame.height() + " format="
6384 + mAttrs.format + " flags=" + flags);
6385
6386 int w = mFrame.width();
6387 int h = mFrame.height();
6388 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6389 // for a scaled surface, we always want the requested
6390 // size.
6391 w = mRequestedWidth;
6392 h = mRequestedHeight;
6393 }
6394
6395 try {
6396 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006397 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006398 0, w, h, mAttrs.format, flags);
6399 } catch (Surface.OutOfResourcesException e) {
6400 Log.w(TAG, "OutOfResourcesException creating surface");
6401 reclaimSomeSurfaceMemoryLocked(this, "create");
6402 return null;
6403 } catch (Exception e) {
6404 Log.e(TAG, "Exception creating surface", e);
6405 return null;
6406 }
Romain Guy06882f82009-06-10 13:36:04 -07006407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006408 if (localLOGV) Log.v(
6409 TAG, "Got surface: " + mSurface
6410 + ", set left=" + mFrame.left + " top=" + mFrame.top
6411 + ", animLayer=" + mAnimLayer);
6412 if (SHOW_TRANSACTIONS) {
6413 Log.i(TAG, ">>> OPEN TRANSACTION");
6414 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6415 + mAttrs.getTitle() + ") pos=(" +
6416 mFrame.left + "," + mFrame.top + ") (" +
6417 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6418 mAnimLayer + " HIDE");
6419 }
6420 Surface.openTransaction();
6421 try {
6422 try {
6423 mSurface.setPosition(mFrame.left, mFrame.top);
6424 mSurface.setLayer(mAnimLayer);
6425 mSurface.hide();
6426 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6427 mSurface.setFlags(Surface.SURFACE_DITHER,
6428 Surface.SURFACE_DITHER);
6429 }
6430 } catch (RuntimeException e) {
6431 Log.w(TAG, "Error creating surface in " + w, e);
6432 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6433 }
6434 mLastHidden = true;
6435 } finally {
6436 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6437 Surface.closeTransaction();
6438 }
6439 if (localLOGV) Log.v(
6440 TAG, "Created surface " + this);
6441 }
6442 return mSurface;
6443 }
Romain Guy06882f82009-06-10 13:36:04 -07006444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006445 void destroySurfaceLocked() {
6446 // Window is no longer on-screen, so can no longer receive
6447 // key events... if we were waiting for it to finish
6448 // handling a key event, the wait is over!
6449 mKeyWaiter.finishedKey(mSession, mClient, true,
6450 KeyWaiter.RETURN_NOTHING);
6451 mKeyWaiter.releasePendingPointerLocked(mSession);
6452 mKeyWaiter.releasePendingTrackballLocked(mSession);
6453
6454 if (mAppToken != null && this == mAppToken.startingWindow) {
6455 mAppToken.startingDisplayed = false;
6456 }
Romain Guy06882f82009-06-10 13:36:04 -07006457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 if (localLOGV) Log.v(
6459 TAG, "Window " + this
6460 + " destroying surface " + mSurface + ", session " + mSession);
6461 if (mSurface != null) {
6462 try {
6463 if (SHOW_TRANSACTIONS) {
6464 RuntimeException ex = new RuntimeException();
6465 ex.fillInStackTrace();
6466 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6467 + mAttrs.getTitle() + ")", ex);
6468 }
6469 mSurface.clear();
6470 } catch (RuntimeException e) {
6471 Log.w(TAG, "Exception thrown when destroying Window " + this
6472 + " surface " + mSurface + " session " + mSession
6473 + ": " + e.toString());
6474 }
6475 mSurface = null;
6476 mDrawPending = false;
6477 mCommitDrawPending = false;
6478 mReadyToShow = false;
6479
6480 int i = mChildWindows.size();
6481 while (i > 0) {
6482 i--;
6483 WindowState c = (WindowState)mChildWindows.get(i);
6484 c.mAttachedHidden = true;
6485 }
6486 }
6487 }
6488
6489 boolean finishDrawingLocked() {
6490 if (mDrawPending) {
6491 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6492 TAG, "finishDrawingLocked: " + mSurface);
6493 mCommitDrawPending = true;
6494 mDrawPending = false;
6495 return true;
6496 }
6497 return false;
6498 }
6499
6500 // This must be called while inside a transaction.
6501 void commitFinishDrawingLocked(long currentTime) {
6502 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6503 if (!mCommitDrawPending) {
6504 return;
6505 }
6506 mCommitDrawPending = false;
6507 mReadyToShow = true;
6508 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6509 final AppWindowToken atoken = mAppToken;
6510 if (atoken == null || atoken.allDrawn || starting) {
6511 performShowLocked();
6512 }
6513 }
6514
6515 // This must be called while inside a transaction.
6516 boolean performShowLocked() {
6517 if (DEBUG_VISIBILITY) {
6518 RuntimeException e = new RuntimeException();
6519 e.fillInStackTrace();
6520 Log.v(TAG, "performShow on " + this
6521 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6522 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6523 }
6524 if (mReadyToShow && isReadyForDisplay()) {
6525 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6526 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6527 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6528 + " during animation: policyVis=" + mPolicyVisibility
6529 + " attHidden=" + mAttachedHidden
6530 + " tok.hiddenRequested="
6531 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6532 + " tok.idden="
6533 + (mAppToken != null ? mAppToken.hidden : false)
6534 + " animating=" + mAnimating
6535 + " tok animating="
6536 + (mAppToken != null ? mAppToken.animating : false));
6537 if (!showSurfaceRobustlyLocked(this)) {
6538 return false;
6539 }
6540 mLastAlpha = -1;
6541 mHasDrawn = true;
6542 mLastHidden = false;
6543 mReadyToShow = false;
6544 enableScreenIfNeededLocked();
6545
6546 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006548 int i = mChildWindows.size();
6549 while (i > 0) {
6550 i--;
6551 WindowState c = (WindowState)mChildWindows.get(i);
6552 if (c.mSurface != null && c.mAttachedHidden) {
6553 c.mAttachedHidden = false;
6554 c.performShowLocked();
6555 }
6556 }
Romain Guy06882f82009-06-10 13:36:04 -07006557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006558 if (mAttrs.type != TYPE_APPLICATION_STARTING
6559 && mAppToken != null) {
6560 mAppToken.firstWindowDrawn = true;
6561 if (mAnimation == null && mAppToken.startingData != null) {
6562 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6563 + mToken
6564 + ": first real window is shown, no animation");
6565 mFinishedStarting.add(mAppToken);
6566 mH.sendEmptyMessage(H.FINISHED_STARTING);
6567 }
6568 mAppToken.updateReportedVisibilityLocked();
6569 }
6570 }
6571 return true;
6572 }
Romain Guy06882f82009-06-10 13:36:04 -07006573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 // This must be called while inside a transaction. Returns true if
6575 // there is more animation to run.
6576 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6577 if (!mDisplayFrozen) {
6578 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6581 mHasTransformation = true;
6582 mHasLocalTransformation = true;
6583 if (!mLocalAnimating) {
6584 if (DEBUG_ANIM) Log.v(
6585 TAG, "Starting animation in " + this +
6586 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6587 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6588 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6589 mAnimation.setStartTime(currentTime);
6590 mLocalAnimating = true;
6591 mAnimating = true;
6592 }
6593 mTransformation.clear();
6594 final boolean more = mAnimation.getTransformation(
6595 currentTime, mTransformation);
6596 if (DEBUG_ANIM) Log.v(
6597 TAG, "Stepped animation in " + this +
6598 ": more=" + more + ", xform=" + mTransformation);
6599 if (more) {
6600 // we're not done!
6601 return true;
6602 }
6603 if (DEBUG_ANIM) Log.v(
6604 TAG, "Finished animation in " + this +
6605 " @ " + currentTime);
6606 mAnimation = null;
6607 //WindowManagerService.this.dump();
6608 }
6609 mHasLocalTransformation = false;
6610 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6611 && mAppToken.hasTransformation) {
6612 // When our app token is animating, we kind-of pretend like
6613 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6614 // part of this check means that we will only do this if
6615 // our window is not currently exiting, or it is not
6616 // locally animating itself. The idea being that one that
6617 // is exiting and doing a local animation should be removed
6618 // once that animation is done.
6619 mAnimating = true;
6620 mHasTransformation = true;
6621 mTransformation.clear();
6622 return false;
6623 } else if (mHasTransformation) {
6624 // Little trick to get through the path below to act like
6625 // we have finished an animation.
6626 mAnimating = true;
6627 } else if (isAnimating()) {
6628 mAnimating = true;
6629 }
6630 } else if (mAnimation != null) {
6631 // If the display is frozen, and there is a pending animation,
6632 // clear it and make sure we run the cleanup code.
6633 mAnimating = true;
6634 mLocalAnimating = true;
6635 mAnimation = null;
6636 }
Romain Guy06882f82009-06-10 13:36:04 -07006637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638 if (!mAnimating && !mLocalAnimating) {
6639 return false;
6640 }
6641
6642 if (DEBUG_ANIM) Log.v(
6643 TAG, "Animation done in " + this + ": exiting=" + mExiting
6644 + ", reportedVisible="
6645 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006647 mAnimating = false;
6648 mLocalAnimating = false;
6649 mAnimation = null;
6650 mAnimLayer = mLayer;
6651 if (mIsImWindow) {
6652 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006653 } else if (mIsWallpaper) {
6654 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006655 }
6656 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6657 + " anim layer: " + mAnimLayer);
6658 mHasTransformation = false;
6659 mHasLocalTransformation = false;
6660 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6661 mTransformation.clear();
6662 if (mHasDrawn
6663 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6664 && mAppToken != null
6665 && mAppToken.firstWindowDrawn
6666 && mAppToken.startingData != null) {
6667 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6668 + mToken + ": first real window done animating");
6669 mFinishedStarting.add(mAppToken);
6670 mH.sendEmptyMessage(H.FINISHED_STARTING);
6671 }
Romain Guy06882f82009-06-10 13:36:04 -07006672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 finishExit();
6674
6675 if (mAppToken != null) {
6676 mAppToken.updateReportedVisibilityLocked();
6677 }
6678
6679 return false;
6680 }
6681
6682 void finishExit() {
6683 if (DEBUG_ANIM) Log.v(
6684 TAG, "finishExit in " + this
6685 + ": exiting=" + mExiting
6686 + " remove=" + mRemoveOnExit
6687 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 final int N = mChildWindows.size();
6690 for (int i=0; i<N; i++) {
6691 ((WindowState)mChildWindows.get(i)).finishExit();
6692 }
Romain Guy06882f82009-06-10 13:36:04 -07006693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006694 if (!mExiting) {
6695 return;
6696 }
Romain Guy06882f82009-06-10 13:36:04 -07006697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006698 if (isWindowAnimating()) {
6699 return;
6700 }
6701
6702 if (localLOGV) Log.v(
6703 TAG, "Exit animation finished in " + this
6704 + ": remove=" + mRemoveOnExit);
6705 if (mSurface != null) {
6706 mDestroySurface.add(this);
6707 mDestroying = true;
6708 if (SHOW_TRANSACTIONS) Log.i(
6709 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6710 try {
6711 mSurface.hide();
6712 } catch (RuntimeException e) {
6713 Log.w(TAG, "Error hiding surface in " + this, e);
6714 }
6715 mLastHidden = true;
6716 mKeyWaiter.releasePendingPointerLocked(mSession);
6717 }
6718 mExiting = false;
6719 if (mRemoveOnExit) {
6720 mPendingRemove.add(this);
6721 mRemoveOnExit = false;
6722 }
6723 }
Romain Guy06882f82009-06-10 13:36:04 -07006724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006725 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6726 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6727 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6728 if (dtdx < -.000001f || dtdx > .000001f) return false;
6729 if (dsdy < -.000001f || dsdy > .000001f) return false;
6730 return true;
6731 }
Romain Guy06882f82009-06-10 13:36:04 -07006732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006733 void computeShownFrameLocked() {
6734 final boolean selfTransformation = mHasLocalTransformation;
6735 Transformation attachedTransformation =
6736 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6737 ? mAttachedWindow.mTransformation : null;
6738 Transformation appTransformation =
6739 (mAppToken != null && mAppToken.hasTransformation)
6740 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006741
6742 // Wallpapers are animated based on the "real" window they
6743 // are currently targeting.
6744 if (mAttrs.type == TYPE_WALLPAPER && mWallpaperTarget != null) {
6745 if (mWallpaperTarget.mHasLocalTransformation) {
6746 attachedTransformation = mWallpaperTarget.mTransformation;
6747 }
6748 if (mWallpaperTarget.mAppToken != null &&
6749 mWallpaperTarget.mAppToken.hasTransformation) {
6750 appTransformation = mWallpaperTarget.mAppToken.transformation;
6751 }
6752 }
6753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 if (selfTransformation || attachedTransformation != null
6755 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006756 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006757 final Rect frame = mFrame;
6758 final float tmpFloats[] = mTmpFloats;
6759 final Matrix tmpMatrix = mTmpMatrix;
6760
6761 // Compute the desired transformation.
6762 tmpMatrix.setTranslate(frame.left, frame.top);
6763 if (selfTransformation) {
6764 tmpMatrix.preConcat(mTransformation.getMatrix());
6765 }
6766 if (attachedTransformation != null) {
6767 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6768 }
6769 if (appTransformation != null) {
6770 tmpMatrix.preConcat(appTransformation.getMatrix());
6771 }
6772
6773 // "convert" it into SurfaceFlinger's format
6774 // (a 2x2 matrix + an offset)
6775 // Here we must not transform the position of the surface
6776 // since it is already included in the transformation.
6777 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006779 tmpMatrix.getValues(tmpFloats);
6780 mDsDx = tmpFloats[Matrix.MSCALE_X];
6781 mDtDx = tmpFloats[Matrix.MSKEW_X];
6782 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6783 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006784 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
6785 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006786 int w = frame.width();
6787 int h = frame.height();
6788 mShownFrame.set(x, y, x+w, y+h);
6789
6790 // Now set the alpha... but because our current hardware
6791 // can't do alpha transformation on a non-opaque surface,
6792 // turn it off if we are running an animation that is also
6793 // transforming since it is more important to have that
6794 // animation be smooth.
6795 mShownAlpha = mAlpha;
6796 if (!mLimitedAlphaCompositing
6797 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6798 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6799 && x == frame.left && y == frame.top))) {
6800 //Log.i(TAG, "Applying alpha transform");
6801 if (selfTransformation) {
6802 mShownAlpha *= mTransformation.getAlpha();
6803 }
6804 if (attachedTransformation != null) {
6805 mShownAlpha *= attachedTransformation.getAlpha();
6806 }
6807 if (appTransformation != null) {
6808 mShownAlpha *= appTransformation.getAlpha();
6809 }
6810 } else {
6811 //Log.i(TAG, "Not applying alpha transform");
6812 }
Romain Guy06882f82009-06-10 13:36:04 -07006813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814 if (localLOGV) Log.v(
6815 TAG, "Continuing animation in " + this +
6816 ": " + mShownFrame +
6817 ", alpha=" + mTransformation.getAlpha());
6818 return;
6819 }
Romain Guy06882f82009-06-10 13:36:04 -07006820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006821 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006822 if (mXOffset != 0 || mYOffset != 0) {
6823 mShownFrame.offset(mXOffset, mYOffset);
6824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006825 mShownAlpha = mAlpha;
6826 mDsDx = 1;
6827 mDtDx = 0;
6828 mDsDy = 0;
6829 mDtDy = 1;
6830 }
Romain Guy06882f82009-06-10 13:36:04 -07006831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832 /**
6833 * Is this window visible? It is not visible if there is no
6834 * surface, or we are in the process of running an exit animation
6835 * that will remove the surface, or its app token has been hidden.
6836 */
6837 public boolean isVisibleLw() {
6838 final AppWindowToken atoken = mAppToken;
6839 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6840 && (atoken == null || !atoken.hiddenRequested)
6841 && !mExiting && !mDestroying;
6842 }
6843
6844 /**
6845 * Is this window visible, ignoring its app token? It is not visible
6846 * if there is no surface, or we are in the process of running an exit animation
6847 * that will remove the surface.
6848 */
6849 public boolean isWinVisibleLw() {
6850 final AppWindowToken atoken = mAppToken;
6851 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6852 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6853 && !mExiting && !mDestroying;
6854 }
6855
6856 /**
6857 * The same as isVisible(), but follows the current hidden state of
6858 * the associated app token, not the pending requested hidden state.
6859 */
6860 boolean isVisibleNow() {
6861 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006862 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 }
6864
6865 /**
6866 * Same as isVisible(), but we also count it as visible between the
6867 * call to IWindowSession.add() and the first relayout().
6868 */
6869 boolean isVisibleOrAdding() {
6870 final AppWindowToken atoken = mAppToken;
6871 return (mSurface != null
6872 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6873 && mPolicyVisibility && !mAttachedHidden
6874 && (atoken == null || !atoken.hiddenRequested)
6875 && !mExiting && !mDestroying;
6876 }
6877
6878 /**
6879 * Is this window currently on-screen? It is on-screen either if it
6880 * is visible or it is currently running an animation before no longer
6881 * being visible.
6882 */
6883 boolean isOnScreen() {
6884 final AppWindowToken atoken = mAppToken;
6885 if (atoken != null) {
6886 return mSurface != null && mPolicyVisibility && !mDestroying
6887 && ((!mAttachedHidden && !atoken.hiddenRequested)
6888 || mAnimating || atoken.animating);
6889 } else {
6890 return mSurface != null && mPolicyVisibility && !mDestroying
6891 && (!mAttachedHidden || mAnimating);
6892 }
6893 }
Romain Guy06882f82009-06-10 13:36:04 -07006894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895 /**
6896 * Like isOnScreen(), but we don't return true if the window is part
6897 * of a transition that has not yet been started.
6898 */
6899 boolean isReadyForDisplay() {
6900 final AppWindowToken atoken = mAppToken;
6901 final boolean animating = atoken != null ? atoken.animating : false;
6902 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006903 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 || mAnimating || animating);
6905 }
6906
6907 /** Is the window or its container currently animating? */
6908 boolean isAnimating() {
6909 final WindowState attached = mAttachedWindow;
6910 final AppWindowToken atoken = mAppToken;
6911 return mAnimation != null
6912 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006913 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006914 (atoken.animation != null
6915 || atoken.inPendingTransaction));
6916 }
6917
6918 /** Is this window currently animating? */
6919 boolean isWindowAnimating() {
6920 return mAnimation != null;
6921 }
6922
6923 /**
6924 * Like isOnScreen, but returns false if the surface hasn't yet
6925 * been drawn.
6926 */
6927 public boolean isDisplayedLw() {
6928 final AppWindowToken atoken = mAppToken;
6929 return mSurface != null && mPolicyVisibility && !mDestroying
6930 && !mDrawPending && !mCommitDrawPending
6931 && ((!mAttachedHidden &&
6932 (atoken == null || !atoken.hiddenRequested))
6933 || mAnimating);
6934 }
6935
6936 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6937 boolean shownFrame, boolean onlyOpaque) {
6938 if (mSurface == null) {
6939 return false;
6940 }
6941 if (mAppToken != null && !mAppToken.appFullscreen) {
6942 return false;
6943 }
6944 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6945 return false;
6946 }
6947 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006948
6949 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
6950 return frame.left <= mCompatibleScreenFrame.left &&
6951 frame.top <= mCompatibleScreenFrame.top &&
6952 frame.right >= mCompatibleScreenFrame.right &&
6953 frame.bottom >= mCompatibleScreenFrame.bottom;
6954 } else {
6955 return frame.left <= 0 && frame.top <= 0
6956 && frame.right >= screenWidth
6957 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 }
Romain Guy06882f82009-06-10 13:36:04 -07006960
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006961 /**
6962 * Return true if the window is opaque and fully drawn.
6963 */
6964 boolean isOpaqueDrawn() {
6965 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
6966 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
6967 }
6968
6969 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
6970 return
6971 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006972 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
6973 // only if it's visible
6974 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006975 // and only if the application fills the compatible screen
6976 mFrame.left <= mCompatibleScreenFrame.left &&
6977 mFrame.top <= mCompatibleScreenFrame.top &&
6978 mFrame.right >= mCompatibleScreenFrame.right &&
6979 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006980 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006981 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006982 }
6983
6984 boolean isFullscreen(int screenWidth, int screenHeight) {
6985 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006986 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006987 }
6988
6989 void removeLocked() {
6990 if (mAttachedWindow != null) {
6991 mAttachedWindow.mChildWindows.remove(this);
6992 }
6993 destroySurfaceLocked();
6994 mSession.windowRemovedLocked();
6995 try {
6996 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6997 } catch (RuntimeException e) {
6998 // Ignore if it has already been removed (usually because
6999 // we are doing this as part of processing a death note.)
7000 }
7001 }
7002
7003 private class DeathRecipient implements IBinder.DeathRecipient {
7004 public void binderDied() {
7005 try {
7006 synchronized(mWindowMap) {
7007 WindowState win = windowForClientLocked(mSession, mClient);
7008 Log.i(TAG, "WIN DEATH: " + win);
7009 if (win != null) {
7010 removeWindowLocked(mSession, win);
7011 }
7012 }
7013 } catch (IllegalArgumentException ex) {
7014 // This will happen if the window has already been
7015 // removed.
7016 }
7017 }
7018 }
7019
7020 /** Returns true if this window desires key events. */
7021 public final boolean canReceiveKeys() {
7022 return isVisibleOrAdding()
7023 && (mViewVisibility == View.VISIBLE)
7024 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7025 }
7026
7027 public boolean hasDrawnLw() {
7028 return mHasDrawn;
7029 }
7030
7031 public boolean showLw(boolean doAnimation) {
7032 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7033 mPolicyVisibility = true;
7034 mPolicyVisibilityAfterAnim = true;
7035 if (doAnimation) {
7036 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7037 }
7038 requestAnimationLocked(0);
7039 return true;
7040 }
7041 return false;
7042 }
7043
7044 public boolean hideLw(boolean doAnimation) {
7045 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7046 : mPolicyVisibility;
7047 if (current) {
7048 if (doAnimation) {
7049 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7050 if (mAnimation == null) {
7051 doAnimation = false;
7052 }
7053 }
7054 if (doAnimation) {
7055 mPolicyVisibilityAfterAnim = false;
7056 } else {
7057 mPolicyVisibilityAfterAnim = false;
7058 mPolicyVisibility = false;
7059 }
7060 requestAnimationLocked(0);
7061 return true;
7062 }
7063 return false;
7064 }
7065
7066 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007067 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007068
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007069 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7070 pw.print(" mClient="); pw.println(mClient.asBinder());
7071 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7072 if (mAttachedWindow != null || mLayoutAttached) {
7073 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7074 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7075 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007076 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7077 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7078 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007079 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7080 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007081 }
7082 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7083 pw.print(" mSubLayer="); pw.print(mSubLayer);
7084 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7085 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7086 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7087 pw.print("="); pw.print(mAnimLayer);
7088 pw.print(" mLastLayer="); pw.println(mLastLayer);
7089 if (mSurface != null) {
7090 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7091 }
7092 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7093 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7094 if (mAppToken != null) {
7095 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7096 }
7097 if (mTargetAppToken != null) {
7098 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7099 }
7100 pw.print(prefix); pw.print("mViewVisibility=0x");
7101 pw.print(Integer.toHexString(mViewVisibility));
7102 pw.print(" mLastHidden="); pw.print(mLastHidden);
7103 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
7104 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7105 pw.print(prefix); pw.print("mPolicyVisibility=");
7106 pw.print(mPolicyVisibility);
7107 pw.print(" mPolicyVisibilityAfterAnim=");
7108 pw.print(mPolicyVisibilityAfterAnim);
7109 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7110 }
7111 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007112 pw.print(" h="); pw.println(mRequestedHeight);
7113 if (mXOffset != 0 || mYOffset != 0) {
7114 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7115 pw.print(" y="); pw.println(mYOffset);
7116 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007117 pw.print(prefix); pw.print("mGivenContentInsets=");
7118 mGivenContentInsets.printShortString(pw);
7119 pw.print(" mGivenVisibleInsets=");
7120 mGivenVisibleInsets.printShortString(pw);
7121 pw.println();
7122 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7123 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7124 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7125 }
7126 pw.print(prefix); pw.print("mShownFrame=");
7127 mShownFrame.printShortString(pw);
7128 pw.print(" last="); mLastShownFrame.printShortString(pw);
7129 pw.println();
7130 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7131 pw.print(" last="); mLastFrame.printShortString(pw);
7132 pw.println();
7133 pw.print(prefix); pw.print("mContainingFrame=");
7134 mContainingFrame.printShortString(pw);
7135 pw.print(" mDisplayFrame=");
7136 mDisplayFrame.printShortString(pw);
7137 pw.println();
7138 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7139 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7140 pw.println();
7141 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7142 pw.print(" last="); mLastContentInsets.printShortString(pw);
7143 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7144 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7145 pw.println();
7146 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7147 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7148 pw.print(" mAlpha="); pw.print(mAlpha);
7149 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7150 }
7151 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7152 || mAnimation != null) {
7153 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7154 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7155 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7156 pw.print(" mAnimation="); pw.println(mAnimation);
7157 }
7158 if (mHasTransformation || mHasLocalTransformation) {
7159 pw.print(prefix); pw.print("XForm: has=");
7160 pw.print(mHasTransformation);
7161 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7162 pw.print(" "); mTransformation.printShortString(pw);
7163 pw.println();
7164 }
7165 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7166 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7167 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7168 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7169 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7170 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7171 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7172 pw.print(" mDestroying="); pw.print(mDestroying);
7173 pw.print(" mRemoved="); pw.println(mRemoved);
7174 }
7175 if (mOrientationChanging || mAppFreezing) {
7176 pw.print(prefix); pw.print("mOrientationChanging=");
7177 pw.print(mOrientationChanging);
7178 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7179 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007180 if (mHScale != 1 || mVScale != 1) {
7181 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7182 pw.print(" mVScale="); pw.println(mVScale);
7183 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007184 if (mWallpaperX != 0 || mWallpaperY != 0) {
7185 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7186 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188 }
7189
7190 @Override
7191 public String toString() {
7192 return "Window{"
7193 + Integer.toHexString(System.identityHashCode(this))
7194 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7195 }
7196 }
Romain Guy06882f82009-06-10 13:36:04 -07007197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007198 // -------------------------------------------------------------
7199 // Window Token State
7200 // -------------------------------------------------------------
7201
7202 class WindowToken {
7203 // The actual token.
7204 final IBinder token;
7205
7206 // The type of window this token is for, as per WindowManager.LayoutParams.
7207 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 // Set if this token was explicitly added by a client, so should
7210 // not be removed when all windows are removed.
7211 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007212
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007213 // For printing.
7214 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 // If this is an AppWindowToken, this is non-null.
7217 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 // All of the windows associated with this token.
7220 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7221
7222 // Is key dispatching paused for this token?
7223 boolean paused = false;
7224
7225 // Should this token's windows be hidden?
7226 boolean hidden;
7227
7228 // Temporary for finding which tokens no longer have visible windows.
7229 boolean hasVisible;
7230
7231 WindowToken(IBinder _token, int type, boolean _explicit) {
7232 token = _token;
7233 windowType = type;
7234 explicit = _explicit;
7235 }
7236
7237 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007238 pw.print(prefix); pw.print("token="); pw.println(token);
7239 pw.print(prefix); pw.print("windows="); pw.println(windows);
7240 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7241 pw.print(" hidden="); pw.print(hidden);
7242 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007243 }
7244
7245 @Override
7246 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007247 if (stringName == null) {
7248 StringBuilder sb = new StringBuilder();
7249 sb.append("WindowToken{");
7250 sb.append(Integer.toHexString(System.identityHashCode(this)));
7251 sb.append(" token="); sb.append(token); sb.append('}');
7252 stringName = sb.toString();
7253 }
7254 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007255 }
7256 };
7257
7258 class AppWindowToken extends WindowToken {
7259 // Non-null only for application tokens.
7260 final IApplicationToken appToken;
7261
7262 // All of the windows and child windows that are included in this
7263 // application token. Note this list is NOT sorted!
7264 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7265
7266 int groupId = -1;
7267 boolean appFullscreen;
7268 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 // These are used for determining when all windows associated with
7271 // an activity have been drawn, so they can be made visible together
7272 // at the same time.
7273 int lastTransactionSequence = mTransactionSequence-1;
7274 int numInterestingWindows;
7275 int numDrawnWindows;
7276 boolean inPendingTransaction;
7277 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 // Is this token going to be hidden in a little while? If so, it
7280 // won't be taken into account for setting the screen orientation.
7281 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007283 // Is this window's surface needed? This is almost like hidden, except
7284 // it will sometimes be true a little earlier: when the token has
7285 // been shown, but is still waiting for its app transition to execute
7286 // before making its windows shown.
7287 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007289 // Have we told the window clients to hide themselves?
7290 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007292 // Last visibility state we reported to the app token.
7293 boolean reportedVisible;
7294
7295 // Set to true when the token has been removed from the window mgr.
7296 boolean removed;
7297
7298 // Have we been asked to have this token keep the screen frozen?
7299 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007301 boolean animating;
7302 Animation animation;
7303 boolean hasTransformation;
7304 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007306 // Offset to the window of all layers in the token, for use by
7307 // AppWindowToken animations.
7308 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007310 // Information about an application starting window if displayed.
7311 StartingData startingData;
7312 WindowState startingWindow;
7313 View startingView;
7314 boolean startingDisplayed;
7315 boolean startingMoved;
7316 boolean firstWindowDrawn;
7317
7318 AppWindowToken(IApplicationToken _token) {
7319 super(_token.asBinder(),
7320 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7321 appWindowToken = this;
7322 appToken = _token;
7323 }
Romain Guy06882f82009-06-10 13:36:04 -07007324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 public void setAnimation(Animation anim) {
7326 if (localLOGV) Log.v(
7327 TAG, "Setting animation in " + this + ": " + anim);
7328 animation = anim;
7329 animating = false;
7330 anim.restrictDuration(MAX_ANIMATION_DURATION);
7331 anim.scaleCurrentDuration(mTransitionAnimationScale);
7332 int zorder = anim.getZAdjustment();
7333 int adj = 0;
7334 if (zorder == Animation.ZORDER_TOP) {
7335 adj = TYPE_LAYER_OFFSET;
7336 } else if (zorder == Animation.ZORDER_BOTTOM) {
7337 adj = -TYPE_LAYER_OFFSET;
7338 }
Romain Guy06882f82009-06-10 13:36:04 -07007339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007340 if (animLayerAdjustment != adj) {
7341 animLayerAdjustment = adj;
7342 updateLayers();
7343 }
7344 }
Romain Guy06882f82009-06-10 13:36:04 -07007345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007346 public void setDummyAnimation() {
7347 if (animation == null) {
7348 if (localLOGV) Log.v(
7349 TAG, "Setting dummy animation in " + this);
7350 animation = sDummyAnimation;
7351 }
7352 }
7353
7354 public void clearAnimation() {
7355 if (animation != null) {
7356 animation = null;
7357 animating = true;
7358 }
7359 }
Romain Guy06882f82009-06-10 13:36:04 -07007360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007361 void updateLayers() {
7362 final int N = allAppWindows.size();
7363 final int adj = animLayerAdjustment;
7364 for (int i=0; i<N; i++) {
7365 WindowState w = allAppWindows.get(i);
7366 w.mAnimLayer = w.mLayer + adj;
7367 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7368 + w.mAnimLayer);
7369 if (w == mInputMethodTarget) {
7370 setInputMethodAnimLayerAdjustment(adj);
7371 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007372 if (w == mWallpaperTarget) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007373 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 }
7376 }
Romain Guy06882f82009-06-10 13:36:04 -07007377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007378 void sendAppVisibilityToClients() {
7379 final int N = allAppWindows.size();
7380 for (int i=0; i<N; i++) {
7381 WindowState win = allAppWindows.get(i);
7382 if (win == startingWindow && clientHidden) {
7383 // Don't hide the starting window.
7384 continue;
7385 }
7386 try {
7387 if (DEBUG_VISIBILITY) Log.v(TAG,
7388 "Setting visibility of " + win + ": " + (!clientHidden));
7389 win.mClient.dispatchAppVisibility(!clientHidden);
7390 } catch (RemoteException e) {
7391 }
7392 }
7393 }
Romain Guy06882f82009-06-10 13:36:04 -07007394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007395 void showAllWindowsLocked() {
7396 final int NW = allAppWindows.size();
7397 for (int i=0; i<NW; i++) {
7398 WindowState w = allAppWindows.get(i);
7399 if (DEBUG_VISIBILITY) Log.v(TAG,
7400 "performing show on: " + w);
7401 w.performShowLocked();
7402 }
7403 }
Romain Guy06882f82009-06-10 13:36:04 -07007404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007405 // This must be called while inside a transaction.
7406 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7407 if (!mDisplayFrozen) {
7408 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007410 if (animation == sDummyAnimation) {
7411 // This guy is going to animate, but not yet. For now count
7412 // it is not animating for purposes of scheduling transactions;
7413 // when it is really time to animate, this will be set to
7414 // a real animation and the next call will execute normally.
7415 return false;
7416 }
Romain Guy06882f82009-06-10 13:36:04 -07007417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007418 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7419 if (!animating) {
7420 if (DEBUG_ANIM) Log.v(
7421 TAG, "Starting animation in " + this +
7422 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7423 + " scale=" + mTransitionAnimationScale
7424 + " allDrawn=" + allDrawn + " animating=" + animating);
7425 animation.initialize(dw, dh, dw, dh);
7426 animation.setStartTime(currentTime);
7427 animating = true;
7428 }
7429 transformation.clear();
7430 final boolean more = animation.getTransformation(
7431 currentTime, transformation);
7432 if (DEBUG_ANIM) Log.v(
7433 TAG, "Stepped animation in " + this +
7434 ": more=" + more + ", xform=" + transformation);
7435 if (more) {
7436 // we're done!
7437 hasTransformation = true;
7438 return true;
7439 }
7440 if (DEBUG_ANIM) Log.v(
7441 TAG, "Finished animation in " + this +
7442 " @ " + currentTime);
7443 animation = null;
7444 }
7445 } else if (animation != null) {
7446 // If the display is frozen, and there is a pending animation,
7447 // clear it and make sure we run the cleanup code.
7448 animating = true;
7449 animation = null;
7450 }
7451
7452 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007454 if (!animating) {
7455 return false;
7456 }
7457
7458 clearAnimation();
7459 animating = false;
7460 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7461 moveInputMethodWindowsIfNeededLocked(true);
7462 }
Romain Guy06882f82009-06-10 13:36:04 -07007463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007464 if (DEBUG_ANIM) Log.v(
7465 TAG, "Animation done in " + this
7466 + ": reportedVisible=" + reportedVisible);
7467
7468 transformation.clear();
7469 if (animLayerAdjustment != 0) {
7470 animLayerAdjustment = 0;
7471 updateLayers();
7472 }
Romain Guy06882f82009-06-10 13:36:04 -07007473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 final int N = windows.size();
7475 for (int i=0; i<N; i++) {
7476 ((WindowState)windows.get(i)).finishExit();
7477 }
7478 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 return false;
7481 }
7482
7483 void updateReportedVisibilityLocked() {
7484 if (appToken == null) {
7485 return;
7486 }
Romain Guy06882f82009-06-10 13:36:04 -07007487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007488 int numInteresting = 0;
7489 int numVisible = 0;
7490 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007492 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7493 final int N = allAppWindows.size();
7494 for (int i=0; i<N; i++) {
7495 WindowState win = allAppWindows.get(i);
7496 if (win == startingWindow || win.mAppFreezing) {
7497 continue;
7498 }
7499 if (DEBUG_VISIBILITY) {
7500 Log.v(TAG, "Win " + win + ": isDisplayed="
7501 + win.isDisplayedLw()
7502 + ", isAnimating=" + win.isAnimating());
7503 if (!win.isDisplayedLw()) {
7504 Log.v(TAG, "Not displayed: s=" + win.mSurface
7505 + " pv=" + win.mPolicyVisibility
7506 + " dp=" + win.mDrawPending
7507 + " cdp=" + win.mCommitDrawPending
7508 + " ah=" + win.mAttachedHidden
7509 + " th="
7510 + (win.mAppToken != null
7511 ? win.mAppToken.hiddenRequested : false)
7512 + " a=" + win.mAnimating);
7513 }
7514 }
7515 numInteresting++;
7516 if (win.isDisplayedLw()) {
7517 if (!win.isAnimating()) {
7518 numVisible++;
7519 }
7520 nowGone = false;
7521 } else if (win.isAnimating()) {
7522 nowGone = false;
7523 }
7524 }
Romain Guy06882f82009-06-10 13:36:04 -07007525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007526 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7527 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7528 + numInteresting + " visible=" + numVisible);
7529 if (nowVisible != reportedVisible) {
7530 if (DEBUG_VISIBILITY) Log.v(
7531 TAG, "Visibility changed in " + this
7532 + ": vis=" + nowVisible);
7533 reportedVisible = nowVisible;
7534 Message m = mH.obtainMessage(
7535 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7536 nowVisible ? 1 : 0,
7537 nowGone ? 1 : 0,
7538 this);
7539 mH.sendMessage(m);
7540 }
7541 }
Romain Guy06882f82009-06-10 13:36:04 -07007542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007543 void dump(PrintWriter pw, String prefix) {
7544 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007545 if (appToken != null) {
7546 pw.print(prefix); pw.println("app=true");
7547 }
7548 if (allAppWindows.size() > 0) {
7549 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7550 }
7551 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7552 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7553 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7554 pw.print(" clientHidden="); pw.print(clientHidden);
7555 pw.print(" willBeHidden="); pw.print(willBeHidden);
7556 pw.print(" reportedVisible="); pw.println(reportedVisible);
7557 if (paused || freezingScreen) {
7558 pw.print(prefix); pw.print("paused="); pw.print(paused);
7559 pw.print(" freezingScreen="); pw.println(freezingScreen);
7560 }
7561 if (numInterestingWindows != 0 || numDrawnWindows != 0
7562 || inPendingTransaction || allDrawn) {
7563 pw.print(prefix); pw.print("numInterestingWindows=");
7564 pw.print(numInterestingWindows);
7565 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7566 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7567 pw.print(" allDrawn="); pw.println(allDrawn);
7568 }
7569 if (animating || animation != null) {
7570 pw.print(prefix); pw.print("animating="); pw.print(animating);
7571 pw.print(" animation="); pw.println(animation);
7572 }
7573 if (animLayerAdjustment != 0) {
7574 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7575 }
7576 if (hasTransformation) {
7577 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7578 pw.print(" transformation="); transformation.printShortString(pw);
7579 pw.println();
7580 }
7581 if (startingData != null || removed || firstWindowDrawn) {
7582 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7583 pw.print(" removed="); pw.print(removed);
7584 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7585 }
7586 if (startingWindow != null || startingView != null
7587 || startingDisplayed || startingMoved) {
7588 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7589 pw.print(" startingView="); pw.print(startingView);
7590 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7591 pw.print(" startingMoved"); pw.println(startingMoved);
7592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007593 }
7594
7595 @Override
7596 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007597 if (stringName == null) {
7598 StringBuilder sb = new StringBuilder();
7599 sb.append("AppWindowToken{");
7600 sb.append(Integer.toHexString(System.identityHashCode(this)));
7601 sb.append(" token="); sb.append(token); sb.append('}');
7602 stringName = sb.toString();
7603 }
7604 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007605 }
7606 }
Romain Guy06882f82009-06-10 13:36:04 -07007607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007608 public static WindowManager.LayoutParams findAnimations(
7609 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007610 ArrayList<AppWindowToken> openingTokenList1,
7611 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007613
7614 // First, check if there is a compatible window in opening/closing
7615 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007616 WindowManager.LayoutParams animParams = null;
7617 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007618 animParams = findCompatibleWindowParams(openingTokenList1);
7619 if (animParams == null) {
7620 animParams = findCompatibleWindowParams(closingTokenList2);
7621 }
7622 if (animParams != null) {
7623 return animParams;
7624 }
7625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 //Log.i(TAG, "Looking for animations...");
7627 for (int i=order.size()-1; i>=0; i--) {
7628 AppWindowToken wtoken = order.get(i);
7629 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007630 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007631 int j = wtoken.windows.size();
7632 while (j > 0) {
7633 j--;
7634 WindowState win = wtoken.windows.get(j);
7635 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7636 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7637 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7638 //Log.i(TAG, "Found base or application window, done!");
7639 if (wtoken.appFullscreen) {
7640 return win.mAttrs;
7641 }
7642 if (animSrc < 2) {
7643 animParams = win.mAttrs;
7644 animSrc = 2;
7645 }
7646 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7647 //Log.i(TAG, "Found normal window, we may use this...");
7648 animParams = win.mAttrs;
7649 animSrc = 1;
7650 }
7651 }
7652 }
7653 }
Romain Guy06882f82009-06-10 13:36:04 -07007654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007655 return animParams;
7656 }
Romain Guy06882f82009-06-10 13:36:04 -07007657
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007658 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7659 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7660 AppWindowToken wtoken = tokenList.get(appCount);
7661 // Just checking one window is sufficient as all windows have the compatible flag
7662 // if the application is in compatibility mode.
7663 if (wtoken.windows.size() > 0) {
7664 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7665 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7666 return params;
7667 }
7668 }
7669 }
7670 return null;
7671 }
7672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007673 // -------------------------------------------------------------
7674 // DummyAnimation
7675 // -------------------------------------------------------------
7676
7677 // This is an animation that does nothing: it just immediately finishes
7678 // itself every time it is called. It is used as a stub animation in cases
7679 // where we want to synchronize multiple things that may be animating.
7680 static final class DummyAnimation extends Animation {
7681 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7682 return false;
7683 }
7684 }
7685 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007687 // -------------------------------------------------------------
7688 // Async Handler
7689 // -------------------------------------------------------------
7690
7691 static final class StartingData {
7692 final String pkg;
7693 final int theme;
7694 final CharSequence nonLocalizedLabel;
7695 final int labelRes;
7696 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007698 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7699 int _labelRes, int _icon) {
7700 pkg = _pkg;
7701 theme = _theme;
7702 nonLocalizedLabel = _nonLocalizedLabel;
7703 labelRes = _labelRes;
7704 icon = _icon;
7705 }
7706 }
7707
7708 private final class H extends Handler {
7709 public static final int REPORT_FOCUS_CHANGE = 2;
7710 public static final int REPORT_LOSING_FOCUS = 3;
7711 public static final int ANIMATE = 4;
7712 public static final int ADD_STARTING = 5;
7713 public static final int REMOVE_STARTING = 6;
7714 public static final int FINISHED_STARTING = 7;
7715 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7717 public static final int HOLD_SCREEN_CHANGED = 12;
7718 public static final int APP_TRANSITION_TIMEOUT = 13;
7719 public static final int PERSIST_ANIMATION_SCALE = 14;
7720 public static final int FORCE_GC = 15;
7721 public static final int ENABLE_SCREEN = 16;
7722 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007723 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007727 public H() {
7728 }
Romain Guy06882f82009-06-10 13:36:04 -07007729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007730 @Override
7731 public void handleMessage(Message msg) {
7732 switch (msg.what) {
7733 case REPORT_FOCUS_CHANGE: {
7734 WindowState lastFocus;
7735 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737 synchronized(mWindowMap) {
7738 lastFocus = mLastFocus;
7739 newFocus = mCurrentFocus;
7740 if (lastFocus == newFocus) {
7741 // Focus is not changing, so nothing to do.
7742 return;
7743 }
7744 mLastFocus = newFocus;
7745 //Log.i(TAG, "Focus moving from " + lastFocus
7746 // + " to " + newFocus);
7747 if (newFocus != null && lastFocus != null
7748 && !newFocus.isDisplayedLw()) {
7749 //Log.i(TAG, "Delaying loss of focus...");
7750 mLosingFocus.add(lastFocus);
7751 lastFocus = null;
7752 }
7753 }
7754
7755 if (lastFocus != newFocus) {
7756 //System.out.println("Changing focus from " + lastFocus
7757 // + " to " + newFocus);
7758 if (newFocus != null) {
7759 try {
7760 //Log.i(TAG, "Gaining focus: " + newFocus);
7761 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7762 } catch (RemoteException e) {
7763 // Ignore if process has died.
7764 }
7765 }
7766
7767 if (lastFocus != null) {
7768 try {
7769 //Log.i(TAG, "Losing focus: " + lastFocus);
7770 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7771 } catch (RemoteException e) {
7772 // Ignore if process has died.
7773 }
7774 }
7775 }
7776 } break;
7777
7778 case REPORT_LOSING_FOCUS: {
7779 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007781 synchronized(mWindowMap) {
7782 losers = mLosingFocus;
7783 mLosingFocus = new ArrayList<WindowState>();
7784 }
7785
7786 final int N = losers.size();
7787 for (int i=0; i<N; i++) {
7788 try {
7789 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7790 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7791 } catch (RemoteException e) {
7792 // Ignore if process has died.
7793 }
7794 }
7795 } break;
7796
7797 case ANIMATE: {
7798 synchronized(mWindowMap) {
7799 mAnimationPending = false;
7800 performLayoutAndPlaceSurfacesLocked();
7801 }
7802 } break;
7803
7804 case ADD_STARTING: {
7805 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7806 final StartingData sd = wtoken.startingData;
7807
7808 if (sd == null) {
7809 // Animation has been canceled... do nothing.
7810 return;
7811 }
Romain Guy06882f82009-06-10 13:36:04 -07007812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7814 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007816 View view = null;
7817 try {
7818 view = mPolicy.addStartingWindow(
7819 wtoken.token, sd.pkg,
7820 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7821 sd.icon);
7822 } catch (Exception e) {
7823 Log.w(TAG, "Exception when adding starting window", e);
7824 }
7825
7826 if (view != null) {
7827 boolean abort = false;
7828
7829 synchronized(mWindowMap) {
7830 if (wtoken.removed || wtoken.startingData == null) {
7831 // If the window was successfully added, then
7832 // we need to remove it.
7833 if (wtoken.startingWindow != null) {
7834 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7835 "Aborted starting " + wtoken
7836 + ": removed=" + wtoken.removed
7837 + " startingData=" + wtoken.startingData);
7838 wtoken.startingWindow = null;
7839 wtoken.startingData = null;
7840 abort = true;
7841 }
7842 } else {
7843 wtoken.startingView = view;
7844 }
7845 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7846 "Added starting " + wtoken
7847 + ": startingWindow="
7848 + wtoken.startingWindow + " startingView="
7849 + wtoken.startingView);
7850 }
7851
7852 if (abort) {
7853 try {
7854 mPolicy.removeStartingWindow(wtoken.token, view);
7855 } catch (Exception e) {
7856 Log.w(TAG, "Exception when removing starting window", e);
7857 }
7858 }
7859 }
7860 } break;
7861
7862 case REMOVE_STARTING: {
7863 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7864 IBinder token = null;
7865 View view = null;
7866 synchronized (mWindowMap) {
7867 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7868 + wtoken + ": startingWindow="
7869 + wtoken.startingWindow + " startingView="
7870 + wtoken.startingView);
7871 if (wtoken.startingWindow != null) {
7872 view = wtoken.startingView;
7873 token = wtoken.token;
7874 wtoken.startingData = null;
7875 wtoken.startingView = null;
7876 wtoken.startingWindow = null;
7877 }
7878 }
7879 if (view != null) {
7880 try {
7881 mPolicy.removeStartingWindow(token, view);
7882 } catch (Exception e) {
7883 Log.w(TAG, "Exception when removing starting window", e);
7884 }
7885 }
7886 } break;
7887
7888 case FINISHED_STARTING: {
7889 IBinder token = null;
7890 View view = null;
7891 while (true) {
7892 synchronized (mWindowMap) {
7893 final int N = mFinishedStarting.size();
7894 if (N <= 0) {
7895 break;
7896 }
7897 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7898
7899 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7900 "Finished starting " + wtoken
7901 + ": startingWindow=" + wtoken.startingWindow
7902 + " startingView=" + wtoken.startingView);
7903
7904 if (wtoken.startingWindow == null) {
7905 continue;
7906 }
7907
7908 view = wtoken.startingView;
7909 token = wtoken.token;
7910 wtoken.startingData = null;
7911 wtoken.startingView = null;
7912 wtoken.startingWindow = null;
7913 }
7914
7915 try {
7916 mPolicy.removeStartingWindow(token, view);
7917 } catch (Exception e) {
7918 Log.w(TAG, "Exception when removing starting window", e);
7919 }
7920 }
7921 } break;
7922
7923 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7924 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7925
7926 boolean nowVisible = msg.arg1 != 0;
7927 boolean nowGone = msg.arg2 != 0;
7928
7929 try {
7930 if (DEBUG_VISIBILITY) Log.v(
7931 TAG, "Reporting visible in " + wtoken
7932 + " visible=" + nowVisible
7933 + " gone=" + nowGone);
7934 if (nowVisible) {
7935 wtoken.appToken.windowsVisible();
7936 } else {
7937 wtoken.appToken.windowsGone();
7938 }
7939 } catch (RemoteException ex) {
7940 }
7941 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 case WINDOW_FREEZE_TIMEOUT: {
7944 synchronized (mWindowMap) {
7945 Log.w(TAG, "Window freeze timeout expired.");
7946 int i = mWindows.size();
7947 while (i > 0) {
7948 i--;
7949 WindowState w = (WindowState)mWindows.get(i);
7950 if (w.mOrientationChanging) {
7951 w.mOrientationChanging = false;
7952 Log.w(TAG, "Force clearing orientation change: " + w);
7953 }
7954 }
7955 performLayoutAndPlaceSurfacesLocked();
7956 }
7957 break;
7958 }
Romain Guy06882f82009-06-10 13:36:04 -07007959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007960 case HOLD_SCREEN_CHANGED: {
7961 Session oldHold;
7962 Session newHold;
7963 synchronized (mWindowMap) {
7964 oldHold = mLastReportedHold;
7965 newHold = (Session)msg.obj;
7966 mLastReportedHold = newHold;
7967 }
Romain Guy06882f82009-06-10 13:36:04 -07007968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007969 if (oldHold != newHold) {
7970 try {
7971 if (oldHold != null) {
7972 mBatteryStats.noteStopWakelock(oldHold.mUid,
7973 "window",
7974 BatteryStats.WAKE_TYPE_WINDOW);
7975 }
7976 if (newHold != null) {
7977 mBatteryStats.noteStartWakelock(newHold.mUid,
7978 "window",
7979 BatteryStats.WAKE_TYPE_WINDOW);
7980 }
7981 } catch (RemoteException e) {
7982 }
7983 }
7984 break;
7985 }
Romain Guy06882f82009-06-10 13:36:04 -07007986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007987 case APP_TRANSITION_TIMEOUT: {
7988 synchronized (mWindowMap) {
7989 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7990 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7991 "*** APP TRANSITION TIMEOUT");
7992 mAppTransitionReady = true;
7993 mAppTransitionTimeout = true;
7994 performLayoutAndPlaceSurfacesLocked();
7995 }
7996 }
7997 break;
7998 }
Romain Guy06882f82009-06-10 13:36:04 -07007999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000 case PERSIST_ANIMATION_SCALE: {
8001 Settings.System.putFloat(mContext.getContentResolver(),
8002 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8003 Settings.System.putFloat(mContext.getContentResolver(),
8004 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8005 break;
8006 }
Romain Guy06882f82009-06-10 13:36:04 -07008007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 case FORCE_GC: {
8009 synchronized(mWindowMap) {
8010 if (mAnimationPending) {
8011 // If we are animating, don't do the gc now but
8012 // delay a bit so we don't interrupt the animation.
8013 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8014 2000);
8015 return;
8016 }
8017 // If we are currently rotating the display, it will
8018 // schedule a new message when done.
8019 if (mDisplayFrozen) {
8020 return;
8021 }
8022 mFreezeGcPending = 0;
8023 }
8024 Runtime.getRuntime().gc();
8025 break;
8026 }
Romain Guy06882f82009-06-10 13:36:04 -07008027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008028 case ENABLE_SCREEN: {
8029 performEnableScreen();
8030 break;
8031 }
Romain Guy06882f82009-06-10 13:36:04 -07008032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008033 case APP_FREEZE_TIMEOUT: {
8034 synchronized (mWindowMap) {
8035 Log.w(TAG, "App freeze timeout expired.");
8036 int i = mAppTokens.size();
8037 while (i > 0) {
8038 i--;
8039 AppWindowToken tok = mAppTokens.get(i);
8040 if (tok.freezingScreen) {
8041 Log.w(TAG, "Force clearing freeze: " + tok);
8042 unsetAppFreezingScreenLocked(tok, true, true);
8043 }
8044 }
8045 }
8046 break;
8047 }
Romain Guy06882f82009-06-10 13:36:04 -07008048
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008049 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008050 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008051 sendNewConfiguration();
8052 }
8053 break;
8054 }
Romain Guy06882f82009-06-10 13:36:04 -07008055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056 }
8057 }
8058 }
8059
8060 // -------------------------------------------------------------
8061 // IWindowManager API
8062 // -------------------------------------------------------------
8063
8064 public IWindowSession openSession(IInputMethodClient client,
8065 IInputContext inputContext) {
8066 if (client == null) throw new IllegalArgumentException("null client");
8067 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8068 return new Session(client, inputContext);
8069 }
8070
8071 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8072 synchronized (mWindowMap) {
8073 // The focus for the client is the window immediately below
8074 // where we would place the input method window.
8075 int idx = findDesiredInputMethodWindowIndexLocked(false);
8076 WindowState imFocus;
8077 if (idx > 0) {
8078 imFocus = (WindowState)mWindows.get(idx-1);
8079 if (imFocus != null) {
8080 if (imFocus.mSession.mClient != null &&
8081 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8082 return true;
8083 }
8084 }
8085 }
8086 }
8087 return false;
8088 }
Romain Guy06882f82009-06-10 13:36:04 -07008089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 // -------------------------------------------------------------
8091 // Internals
8092 // -------------------------------------------------------------
8093
8094 final WindowState windowForClientLocked(Session session, IWindow client) {
8095 return windowForClientLocked(session, client.asBinder());
8096 }
Romain Guy06882f82009-06-10 13:36:04 -07008097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008098 final WindowState windowForClientLocked(Session session, IBinder client) {
8099 WindowState win = mWindowMap.get(client);
8100 if (localLOGV) Log.v(
8101 TAG, "Looking up client " + client + ": " + win);
8102 if (win == null) {
8103 RuntimeException ex = new RuntimeException();
8104 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8105 return null;
8106 }
8107 if (session != null && win.mSession != session) {
8108 RuntimeException ex = new RuntimeException();
8109 Log.w(TAG, "Requested window " + client + " is in session " +
8110 win.mSession + ", not " + session, ex);
8111 return null;
8112 }
8113
8114 return win;
8115 }
8116
8117 private final void assignLayersLocked() {
8118 int N = mWindows.size();
8119 int curBaseLayer = 0;
8120 int curLayer = 0;
8121 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008123 for (i=0; i<N; i++) {
8124 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008125 if (w.mBaseLayer == curBaseLayer || w.mIsFloatingLayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008126 curLayer += WINDOW_LAYER_MULTIPLIER;
8127 w.mLayer = curLayer;
8128 } else {
8129 curBaseLayer = curLayer = w.mBaseLayer;
8130 w.mLayer = curLayer;
8131 }
8132 if (w.mTargetAppToken != null) {
8133 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8134 } else if (w.mAppToken != null) {
8135 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8136 } else {
8137 w.mAnimLayer = w.mLayer;
8138 }
8139 if (w.mIsImWindow) {
8140 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008141 } else if (w.mIsWallpaper) {
8142 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008143 }
8144 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8145 + w.mAnimLayer);
8146 //System.out.println(
8147 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8148 }
8149 }
8150
8151 private boolean mInLayout = false;
8152 private final void performLayoutAndPlaceSurfacesLocked() {
8153 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008154 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008155 throw new RuntimeException("Recursive call!");
8156 }
8157 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8158 return;
8159 }
8160
8161 boolean recoveringMemory = false;
8162 if (mForceRemoves != null) {
8163 recoveringMemory = true;
8164 // Wait a little it for things to settle down, and off we go.
8165 for (int i=0; i<mForceRemoves.size(); i++) {
8166 WindowState ws = mForceRemoves.get(i);
8167 Log.i(TAG, "Force removing: " + ws);
8168 removeWindowInnerLocked(ws.mSession, ws);
8169 }
8170 mForceRemoves = null;
8171 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8172 Object tmp = new Object();
8173 synchronized (tmp) {
8174 try {
8175 tmp.wait(250);
8176 } catch (InterruptedException e) {
8177 }
8178 }
8179 }
Romain Guy06882f82009-06-10 13:36:04 -07008180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008181 mInLayout = true;
8182 try {
8183 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008185 int i = mPendingRemove.size()-1;
8186 if (i >= 0) {
8187 while (i >= 0) {
8188 WindowState w = mPendingRemove.get(i);
8189 removeWindowInnerLocked(w.mSession, w);
8190 i--;
8191 }
8192 mPendingRemove.clear();
8193
8194 mInLayout = false;
8195 assignLayersLocked();
8196 mLayoutNeeded = true;
8197 performLayoutAndPlaceSurfacesLocked();
8198
8199 } else {
8200 mInLayout = false;
8201 if (mLayoutNeeded) {
8202 requestAnimationLocked(0);
8203 }
8204 }
8205 } catch (RuntimeException e) {
8206 mInLayout = false;
8207 Log.e(TAG, "Unhandled exception while layout out windows", e);
8208 }
8209 }
8210
8211 private final void performLayoutLockedInner() {
8212 final int dw = mDisplay.getWidth();
8213 final int dh = mDisplay.getHeight();
8214
8215 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008216 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008217 int i;
8218
8219 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008220
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008221 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008222 mPolicy.beginLayoutLw(dw, dh);
8223
8224 // First perform layout of any root windows (not attached
8225 // to another window).
8226 int topAttached = -1;
8227 for (i = N-1; i >= 0; i--) {
8228 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008229
8230 // Don't do layout of a window if it is not visible, or
8231 // soon won't be visible, to avoid wasting time and funky
8232 // changes while a window is animating away.
8233 final AppWindowToken atoken = win.mAppToken;
8234 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008236 || win.mRootToken.hidden
8237 || (atoken != null && atoken.hiddenRequested)
8238 || !win.mPolicyVisibility
8239 || win.mAttachedHidden
8240 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008241
8242 // If this view is GONE, then skip it -- keep the current
8243 // frame, and let the caller know so they can ignore it
8244 // if they want. (We do the normal layout for INVISIBLE
8245 // windows, since that means "perform layout as normal,
8246 // just don't display").
8247 if (!gone || !win.mHaveFrame) {
8248 if (!win.mLayoutAttached) {
8249 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8250 } else {
8251 if (topAttached < 0) topAttached = i;
8252 }
8253 }
8254 }
Romain Guy06882f82009-06-10 13:36:04 -07008255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 // Now perform layout of attached windows, which usually
8257 // depend on the position of the window they are attached to.
8258 // XXX does not deal with windows that are attached to windows
8259 // that are themselves attached.
8260 for (i = topAttached; i >= 0; i--) {
8261 WindowState win = (WindowState) mWindows.get(i);
8262
8263 // If this view is GONE, then skip it -- keep the current
8264 // frame, and let the caller know so they can ignore it
8265 // if they want. (We do the normal layout for INVISIBLE
8266 // windows, since that means "perform layout as normal,
8267 // just don't display").
8268 if (win.mLayoutAttached) {
8269 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8270 || !win.mHaveFrame) {
8271 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8272 }
8273 }
8274 }
8275
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008276 if (!mPolicy.finishLayoutLw()) {
8277 mLayoutNeeded = false;
8278 } else if (repeats > 2) {
8279 Log.w(TAG, "Layout repeat aborted after too many iterations");
8280 mLayoutNeeded = false;
8281 } else {
8282 repeats++;
8283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008284 }
8285 }
Romain Guy06882f82009-06-10 13:36:04 -07008286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008287 private final void performLayoutAndPlaceSurfacesLockedInner(
8288 boolean recoveringMemory) {
8289 final long currentTime = SystemClock.uptimeMillis();
8290 final int dw = mDisplay.getWidth();
8291 final int dh = mDisplay.getHeight();
8292
8293 final int N = mWindows.size();
8294 int i;
8295
8296 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008299 if (mFxSession == null) {
8300 mFxSession = new SurfaceSession();
8301 }
Romain Guy06882f82009-06-10 13:36:04 -07008302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008303 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8304
8305 // Initialize state of exiting tokens.
8306 for (i=mExitingTokens.size()-1; i>=0; i--) {
8307 mExitingTokens.get(i).hasVisible = false;
8308 }
8309
8310 // Initialize state of exiting applications.
8311 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8312 mExitingAppTokens.get(i).hasVisible = false;
8313 }
8314
8315 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008316 boolean orientationChangeComplete = true;
8317 Session holdScreen = null;
8318 float screenBrightness = -1;
8319 boolean focusDisplayed = false;
8320 boolean animating = false;
8321
8322 Surface.openTransaction();
8323 try {
8324 boolean restart;
8325
8326 do {
8327 final int transactionSequence = ++mTransactionSequence;
8328
8329 // Update animations of all applications, including those
8330 // associated with exiting/removed apps
8331 boolean tokensAnimating = false;
8332 final int NAT = mAppTokens.size();
8333 for (i=0; i<NAT; i++) {
8334 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8335 tokensAnimating = true;
8336 }
8337 }
8338 final int NEAT = mExitingAppTokens.size();
8339 for (i=0; i<NEAT; i++) {
8340 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8341 tokensAnimating = true;
8342 }
8343 }
8344
8345 animating = tokensAnimating;
8346 restart = false;
8347
8348 boolean tokenMayBeDrawn = false;
8349
8350 mPolicy.beginAnimationLw(dw, dh);
8351
8352 for (i=N-1; i>=0; i--) {
8353 WindowState w = (WindowState)mWindows.get(i);
8354
8355 final WindowManager.LayoutParams attrs = w.mAttrs;
8356
8357 if (w.mSurface != null) {
8358 // Execute animation.
8359 w.commitFinishDrawingLocked(currentTime);
8360 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8361 animating = true;
8362 //w.dump(" ");
8363 }
8364
8365 mPolicy.animatingWindowLw(w, attrs);
8366 }
8367
8368 final AppWindowToken atoken = w.mAppToken;
8369 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8370 if (atoken.lastTransactionSequence != transactionSequence) {
8371 atoken.lastTransactionSequence = transactionSequence;
8372 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8373 atoken.startingDisplayed = false;
8374 }
8375 if ((w.isOnScreen() || w.mAttrs.type
8376 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8377 && !w.mExiting && !w.mDestroying) {
8378 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8379 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8380 + w.isDisplayedLw()
8381 + ", isAnimating=" + w.isAnimating());
8382 if (!w.isDisplayedLw()) {
8383 Log.v(TAG, "Not displayed: s=" + w.mSurface
8384 + " pv=" + w.mPolicyVisibility
8385 + " dp=" + w.mDrawPending
8386 + " cdp=" + w.mCommitDrawPending
8387 + " ah=" + w.mAttachedHidden
8388 + " th=" + atoken.hiddenRequested
8389 + " a=" + w.mAnimating);
8390 }
8391 }
8392 if (w != atoken.startingWindow) {
8393 if (!atoken.freezingScreen || !w.mAppFreezing) {
8394 atoken.numInterestingWindows++;
8395 if (w.isDisplayedLw()) {
8396 atoken.numDrawnWindows++;
8397 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8398 "tokenMayBeDrawn: " + atoken
8399 + " freezingScreen=" + atoken.freezingScreen
8400 + " mAppFreezing=" + w.mAppFreezing);
8401 tokenMayBeDrawn = true;
8402 }
8403 }
8404 } else if (w.isDisplayedLw()) {
8405 atoken.startingDisplayed = true;
8406 }
8407 }
8408 } else if (w.mReadyToShow) {
8409 w.performShowLocked();
8410 }
8411 }
8412
8413 if (mPolicy.finishAnimationLw()) {
8414 restart = true;
8415 }
8416
8417 if (tokenMayBeDrawn) {
8418 // See if any windows have been drawn, so they (and others
8419 // associated with them) can now be shown.
8420 final int NT = mTokenList.size();
8421 for (i=0; i<NT; i++) {
8422 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8423 if (wtoken == null) {
8424 continue;
8425 }
8426 if (wtoken.freezingScreen) {
8427 int numInteresting = wtoken.numInterestingWindows;
8428 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8429 if (DEBUG_VISIBILITY) Log.v(TAG,
8430 "allDrawn: " + wtoken
8431 + " interesting=" + numInteresting
8432 + " drawn=" + wtoken.numDrawnWindows);
8433 wtoken.showAllWindowsLocked();
8434 unsetAppFreezingScreenLocked(wtoken, false, true);
8435 orientationChangeComplete = true;
8436 }
8437 } else if (!wtoken.allDrawn) {
8438 int numInteresting = wtoken.numInterestingWindows;
8439 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8440 if (DEBUG_VISIBILITY) Log.v(TAG,
8441 "allDrawn: " + wtoken
8442 + " interesting=" + numInteresting
8443 + " drawn=" + wtoken.numDrawnWindows);
8444 wtoken.allDrawn = true;
8445 restart = true;
8446
8447 // We can now show all of the drawn windows!
8448 if (!mOpeningApps.contains(wtoken)) {
8449 wtoken.showAllWindowsLocked();
8450 }
8451 }
8452 }
8453 }
8454 }
8455
8456 // If we are ready to perform an app transition, check through
8457 // all of the app tokens to be shown and see if they are ready
8458 // to go.
8459 if (mAppTransitionReady) {
8460 int NN = mOpeningApps.size();
8461 boolean goodToGo = true;
8462 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8463 "Checking " + NN + " opening apps (frozen="
8464 + mDisplayFrozen + " timeout="
8465 + mAppTransitionTimeout + ")...");
8466 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8467 // If the display isn't frozen, wait to do anything until
8468 // all of the apps are ready. Otherwise just go because
8469 // we'll unfreeze the display when everyone is ready.
8470 for (i=0; i<NN && goodToGo; i++) {
8471 AppWindowToken wtoken = mOpeningApps.get(i);
8472 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8473 "Check opening app" + wtoken + ": allDrawn="
8474 + wtoken.allDrawn + " startingDisplayed="
8475 + wtoken.startingDisplayed);
8476 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8477 && !wtoken.startingMoved) {
8478 goodToGo = false;
8479 }
8480 }
8481 }
8482 if (goodToGo) {
8483 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8484 int transit = mNextAppTransition;
8485 if (mSkipAppTransitionAnimation) {
8486 transit = WindowManagerPolicy.TRANSIT_NONE;
8487 }
8488 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8489 mAppTransitionReady = false;
8490 mAppTransitionTimeout = false;
8491 mStartingIconInTransition = false;
8492 mSkipAppTransitionAnimation = false;
8493
8494 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8495
8496 // We need to figure out which animation to use...
8497 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8498 mOpeningApps, mClosingApps);
8499
8500 NN = mOpeningApps.size();
8501 for (i=0; i<NN; i++) {
8502 AppWindowToken wtoken = mOpeningApps.get(i);
8503 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8504 "Now opening app" + wtoken);
8505 wtoken.reportedVisible = false;
8506 wtoken.inPendingTransaction = false;
8507 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8508 wtoken.updateReportedVisibilityLocked();
8509 wtoken.showAllWindowsLocked();
8510 }
8511 NN = mClosingApps.size();
8512 for (i=0; i<NN; i++) {
8513 AppWindowToken wtoken = mClosingApps.get(i);
8514 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8515 "Now closing app" + wtoken);
8516 wtoken.inPendingTransaction = false;
8517 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8518 wtoken.updateReportedVisibilityLocked();
8519 // Force the allDrawn flag, because we want to start
8520 // this guy's animations regardless of whether it's
8521 // gotten drawn.
8522 wtoken.allDrawn = true;
8523 }
8524
8525 mOpeningApps.clear();
8526 mClosingApps.clear();
8527
8528 // This has changed the visibility of windows, so perform
8529 // a new layout to get them all up-to-date.
8530 mLayoutNeeded = true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008531 adjustWallpaperWindowsLocked();
Dianne Hackborn20583ff2009-07-27 21:51:05 -07008532 if (!moveInputMethodWindowsIfNeededLocked(true)) {
8533 assignLayersLocked();
8534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008535 performLayoutLockedInner();
8536 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8537
8538 restart = true;
8539 }
8540 }
8541 } while (restart);
8542
8543 // THIRD LOOP: Update the surfaces of all windows.
8544
8545 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8546
8547 boolean obscured = false;
8548 boolean blurring = false;
8549 boolean dimming = false;
8550 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008551 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008552 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553
8554 for (i=N-1; i>=0; i--) {
8555 WindowState w = (WindowState)mWindows.get(i);
8556
8557 boolean displayed = false;
8558 final WindowManager.LayoutParams attrs = w.mAttrs;
8559 final int attrFlags = attrs.flags;
8560
8561 if (w.mSurface != null) {
8562 w.computeShownFrameLocked();
8563 if (localLOGV) Log.v(
8564 TAG, "Placing surface #" + i + " " + w.mSurface
8565 + ": new=" + w.mShownFrame + ", old="
8566 + w.mLastShownFrame);
8567
8568 boolean resize;
8569 int width, height;
8570 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8571 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8572 w.mLastRequestedHeight != w.mRequestedHeight;
8573 // for a scaled surface, we just want to use
8574 // the requested size.
8575 width = w.mRequestedWidth;
8576 height = w.mRequestedHeight;
8577 w.mLastRequestedWidth = width;
8578 w.mLastRequestedHeight = height;
8579 w.mLastShownFrame.set(w.mShownFrame);
8580 try {
8581 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8582 } catch (RuntimeException e) {
8583 Log.w(TAG, "Error positioning surface in " + w, e);
8584 if (!recoveringMemory) {
8585 reclaimSomeSurfaceMemoryLocked(w, "position");
8586 }
8587 }
8588 } else {
8589 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8590 width = w.mShownFrame.width();
8591 height = w.mShownFrame.height();
8592 w.mLastShownFrame.set(w.mShownFrame);
8593 if (resize) {
8594 if (SHOW_TRANSACTIONS) Log.i(
8595 TAG, " SURFACE " + w.mSurface + ": ("
8596 + w.mShownFrame.left + ","
8597 + w.mShownFrame.top + ") ("
8598 + w.mShownFrame.width() + "x"
8599 + w.mShownFrame.height() + ")");
8600 }
8601 }
8602
8603 if (resize) {
8604 if (width < 1) width = 1;
8605 if (height < 1) height = 1;
8606 if (w.mSurface != null) {
8607 try {
8608 w.mSurface.setSize(width, height);
8609 w.mSurface.setPosition(w.mShownFrame.left,
8610 w.mShownFrame.top);
8611 } catch (RuntimeException e) {
8612 // If something goes wrong with the surface (such
8613 // as running out of memory), don't take down the
8614 // entire system.
8615 Log.e(TAG, "Failure updating surface of " + w
8616 + "size=(" + width + "x" + height
8617 + "), pos=(" + w.mShownFrame.left
8618 + "," + w.mShownFrame.top + ")", e);
8619 if (!recoveringMemory) {
8620 reclaimSomeSurfaceMemoryLocked(w, "size");
8621 }
8622 }
8623 }
8624 }
8625 if (!w.mAppFreezing) {
8626 w.mContentInsetsChanged =
8627 !w.mLastContentInsets.equals(w.mContentInsets);
8628 w.mVisibleInsetsChanged =
8629 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008630 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008631 || w.mContentInsetsChanged
8632 || w.mVisibleInsetsChanged) {
8633 w.mLastFrame.set(w.mFrame);
8634 w.mLastContentInsets.set(w.mContentInsets);
8635 w.mLastVisibleInsets.set(w.mVisibleInsets);
8636 // If the orientation is changing, then we need to
8637 // hold off on unfreezing the display until this
8638 // window has been redrawn; to do that, we need
8639 // to go through the process of getting informed
8640 // by the application when it has finished drawing.
8641 if (w.mOrientationChanging) {
8642 if (DEBUG_ORIENTATION) Log.v(TAG,
8643 "Orientation start waiting for draw in "
8644 + w + ", surface " + w.mSurface);
8645 w.mDrawPending = true;
8646 w.mCommitDrawPending = false;
8647 w.mReadyToShow = false;
8648 if (w.mAppToken != null) {
8649 w.mAppToken.allDrawn = false;
8650 }
8651 }
Romain Guy06882f82009-06-10 13:36:04 -07008652 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008653 "Resizing window " + w + " to " + w.mFrame);
8654 mResizingWindows.add(w);
8655 } else if (w.mOrientationChanging) {
8656 if (!w.mDrawPending && !w.mCommitDrawPending) {
8657 if (DEBUG_ORIENTATION) Log.v(TAG,
8658 "Orientation not waiting for draw in "
8659 + w + ", surface " + w.mSurface);
8660 w.mOrientationChanging = false;
8661 }
8662 }
8663 }
8664
8665 if (w.mAttachedHidden) {
8666 if (!w.mLastHidden) {
8667 //dump();
8668 w.mLastHidden = true;
8669 if (SHOW_TRANSACTIONS) Log.i(
8670 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8671 if (w.mSurface != null) {
8672 try {
8673 w.mSurface.hide();
8674 } catch (RuntimeException e) {
8675 Log.w(TAG, "Exception hiding surface in " + w);
8676 }
8677 }
8678 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8679 }
8680 // If we are waiting for this window to handle an
8681 // orientation change, well, it is hidden, so
8682 // doesn't really matter. Note that this does
8683 // introduce a potential glitch if the window
8684 // becomes unhidden before it has drawn for the
8685 // new orientation.
8686 if (w.mOrientationChanging) {
8687 w.mOrientationChanging = false;
8688 if (DEBUG_ORIENTATION) Log.v(TAG,
8689 "Orientation change skips hidden " + w);
8690 }
8691 } else if (!w.isReadyForDisplay()) {
8692 if (!w.mLastHidden) {
8693 //dump();
8694 w.mLastHidden = true;
8695 if (SHOW_TRANSACTIONS) Log.i(
8696 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8697 if (w.mSurface != null) {
8698 try {
8699 w.mSurface.hide();
8700 } catch (RuntimeException e) {
8701 Log.w(TAG, "Exception exception hiding surface in " + w);
8702 }
8703 }
8704 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8705 }
8706 // If we are waiting for this window to handle an
8707 // orientation change, well, it is hidden, so
8708 // doesn't really matter. Note that this does
8709 // introduce a potential glitch if the window
8710 // becomes unhidden before it has drawn for the
8711 // new orientation.
8712 if (w.mOrientationChanging) {
8713 w.mOrientationChanging = false;
8714 if (DEBUG_ORIENTATION) Log.v(TAG,
8715 "Orientation change skips hidden " + w);
8716 }
8717 } else if (w.mLastLayer != w.mAnimLayer
8718 || w.mLastAlpha != w.mShownAlpha
8719 || w.mLastDsDx != w.mDsDx
8720 || w.mLastDtDx != w.mDtDx
8721 || w.mLastDsDy != w.mDsDy
8722 || w.mLastDtDy != w.mDtDy
8723 || w.mLastHScale != w.mHScale
8724 || w.mLastVScale != w.mVScale
8725 || w.mLastHidden) {
8726 displayed = true;
8727 w.mLastAlpha = w.mShownAlpha;
8728 w.mLastLayer = w.mAnimLayer;
8729 w.mLastDsDx = w.mDsDx;
8730 w.mLastDtDx = w.mDtDx;
8731 w.mLastDsDy = w.mDsDy;
8732 w.mLastDtDy = w.mDtDy;
8733 w.mLastHScale = w.mHScale;
8734 w.mLastVScale = w.mVScale;
8735 if (SHOW_TRANSACTIONS) Log.i(
8736 TAG, " SURFACE " + w.mSurface + ": alpha="
8737 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8738 if (w.mSurface != null) {
8739 try {
8740 w.mSurface.setAlpha(w.mShownAlpha);
8741 w.mSurface.setLayer(w.mAnimLayer);
8742 w.mSurface.setMatrix(
8743 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8744 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8745 } catch (RuntimeException e) {
8746 Log.w(TAG, "Error updating surface in " + w, e);
8747 if (!recoveringMemory) {
8748 reclaimSomeSurfaceMemoryLocked(w, "update");
8749 }
8750 }
8751 }
8752
8753 if (w.mLastHidden && !w.mDrawPending
8754 && !w.mCommitDrawPending
8755 && !w.mReadyToShow) {
8756 if (SHOW_TRANSACTIONS) Log.i(
8757 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8758 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8759 + " during relayout");
8760 if (showSurfaceRobustlyLocked(w)) {
8761 w.mHasDrawn = true;
8762 w.mLastHidden = false;
8763 } else {
8764 w.mOrientationChanging = false;
8765 }
8766 }
8767 if (w.mSurface != null) {
8768 w.mToken.hasVisible = true;
8769 }
8770 } else {
8771 displayed = true;
8772 }
8773
8774 if (displayed) {
8775 if (!covered) {
8776 if (attrs.width == LayoutParams.FILL_PARENT
8777 && attrs.height == LayoutParams.FILL_PARENT) {
8778 covered = true;
8779 }
8780 }
8781 if (w.mOrientationChanging) {
8782 if (w.mDrawPending || w.mCommitDrawPending) {
8783 orientationChangeComplete = false;
8784 if (DEBUG_ORIENTATION) Log.v(TAG,
8785 "Orientation continue waiting for draw in " + w);
8786 } else {
8787 w.mOrientationChanging = false;
8788 if (DEBUG_ORIENTATION) Log.v(TAG,
8789 "Orientation change complete in " + w);
8790 }
8791 }
8792 w.mToken.hasVisible = true;
8793 }
8794 } else if (w.mOrientationChanging) {
8795 if (DEBUG_ORIENTATION) Log.v(TAG,
8796 "Orientation change skips hidden " + w);
8797 w.mOrientationChanging = false;
8798 }
8799
8800 final boolean canBeSeen = w.isDisplayedLw();
8801
8802 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8803 focusDisplayed = true;
8804 }
8805
8806 // Update effect.
8807 if (!obscured) {
8808 if (w.mSurface != null) {
8809 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8810 holdScreen = w.mSession;
8811 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008812 if (!syswin && w.mAttrs.screenBrightness >= 0
8813 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 screenBrightness = w.mAttrs.screenBrightness;
8815 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008816 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8817 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8818 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8819 syswin = true;
8820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008821 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008822
8823 boolean opaqueDrawn = w.isOpaqueDrawn();
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008824 if ((opaqueDrawn && w.isFullscreen(dw, dh))
8825 || attrs.type == TYPE_WALLPAPER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008826 // This window completely covers everything behind it,
8827 // so we want to leave all of them as unblurred (for
8828 // performance reasons).
8829 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008830 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8831 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008832 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008833 obscured = true;
8834 if (mBackgroundFillerSurface == null) {
8835 try {
8836 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8837 0, dw, dh,
8838 PixelFormat.OPAQUE,
8839 Surface.FX_SURFACE_NORMAL);
8840 } catch (Exception e) {
8841 Log.e(TAG, "Exception creating filler surface", e);
8842 }
8843 }
8844 try {
8845 mBackgroundFillerSurface.setPosition(0, 0);
8846 mBackgroundFillerSurface.setSize(dw, dh);
8847 // Using the same layer as Dim because they will never be shown at the
8848 // same time.
8849 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8850 mBackgroundFillerSurface.show();
8851 } catch (RuntimeException e) {
8852 Log.e(TAG, "Exception showing filler surface");
8853 }
8854 backgroundFillerShown = true;
8855 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008856 } else if (canBeSeen && !obscured &&
8857 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8858 if (localLOGV) Log.v(TAG, "Win " + w
8859 + ": blurring=" + blurring
8860 + " obscured=" + obscured
8861 + " displayed=" + displayed);
8862 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8863 if (!dimming) {
8864 //Log.i(TAG, "DIM BEHIND: " + w);
8865 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008866 if (mDimAnimator == null) {
8867 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008868 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008869 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008870 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008871 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 }
8873 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8874 if (!blurring) {
8875 //Log.i(TAG, "BLUR BEHIND: " + w);
8876 blurring = true;
8877 mBlurShown = true;
8878 if (mBlurSurface == null) {
8879 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8880 + mBlurSurface + ": CREATE");
8881 try {
Romain Guy06882f82009-06-10 13:36:04 -07008882 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008883 -1, 16, 16,
8884 PixelFormat.OPAQUE,
8885 Surface.FX_SURFACE_BLUR);
8886 } catch (Exception e) {
8887 Log.e(TAG, "Exception creating Blur surface", e);
8888 }
8889 }
8890 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8891 + mBlurSurface + ": SHOW pos=(0,0) (" +
8892 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8893 if (mBlurSurface != null) {
8894 mBlurSurface.setPosition(0, 0);
8895 mBlurSurface.setSize(dw, dh);
8896 try {
8897 mBlurSurface.show();
8898 } catch (RuntimeException e) {
8899 Log.w(TAG, "Failure showing blur surface", e);
8900 }
8901 }
8902 }
8903 mBlurSurface.setLayer(w.mAnimLayer-2);
8904 }
8905 }
8906 }
8907 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008908
8909 if (backgroundFillerShown == false && mBackgroundFillerShown) {
8910 mBackgroundFillerShown = false;
8911 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
8912 try {
8913 mBackgroundFillerSurface.hide();
8914 } catch (RuntimeException e) {
8915 Log.e(TAG, "Exception hiding filler surface", e);
8916 }
8917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008918
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008919 if (mDimAnimator != null && mDimAnimator.mDimShown) {
8920 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008921 }
Romain Guy06882f82009-06-10 13:36:04 -07008922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 if (!blurring && mBlurShown) {
8924 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8925 + ": HIDE");
8926 try {
8927 mBlurSurface.hide();
8928 } catch (IllegalArgumentException e) {
8929 Log.w(TAG, "Illegal argument exception hiding blur surface");
8930 }
8931 mBlurShown = false;
8932 }
8933
8934 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8935 } catch (RuntimeException e) {
8936 Log.e(TAG, "Unhandled exception in Window Manager", e);
8937 }
8938
8939 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008941 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8942 "With display frozen, orientationChangeComplete="
8943 + orientationChangeComplete);
8944 if (orientationChangeComplete) {
8945 if (mWindowsFreezingScreen) {
8946 mWindowsFreezingScreen = false;
8947 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8948 }
8949 if (mAppsFreezingScreen == 0) {
8950 stopFreezingDisplayLocked();
8951 }
8952 }
Romain Guy06882f82009-06-10 13:36:04 -07008953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008954 i = mResizingWindows.size();
8955 if (i > 0) {
8956 do {
8957 i--;
8958 WindowState win = mResizingWindows.get(i);
8959 try {
8960 win.mClient.resized(win.mFrame.width(),
8961 win.mFrame.height(), win.mLastContentInsets,
8962 win.mLastVisibleInsets, win.mDrawPending);
8963 win.mContentInsetsChanged = false;
8964 win.mVisibleInsetsChanged = false;
8965 } catch (RemoteException e) {
8966 win.mOrientationChanging = false;
8967 }
8968 } while (i > 0);
8969 mResizingWindows.clear();
8970 }
Romain Guy06882f82009-06-10 13:36:04 -07008971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008972 // Destroy the surface of any windows that are no longer visible.
8973 i = mDestroySurface.size();
8974 if (i > 0) {
8975 do {
8976 i--;
8977 WindowState win = mDestroySurface.get(i);
8978 win.mDestroying = false;
8979 if (mInputMethodWindow == win) {
8980 mInputMethodWindow = null;
8981 }
8982 win.destroySurfaceLocked();
8983 } while (i > 0);
8984 mDestroySurface.clear();
8985 }
8986
8987 // Time to remove any exiting tokens?
8988 for (i=mExitingTokens.size()-1; i>=0; i--) {
8989 WindowToken token = mExitingTokens.get(i);
8990 if (!token.hasVisible) {
8991 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008992 if (token.windowType == TYPE_WALLPAPER) {
8993 mWallpaperTokens.remove(token);
8994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008995 }
8996 }
8997
8998 // Time to remove any exiting applications?
8999 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9000 AppWindowToken token = mExitingAppTokens.get(i);
9001 if (!token.hasVisible && !mClosingApps.contains(token)) {
9002 mAppTokens.remove(token);
9003 mExitingAppTokens.remove(i);
9004 }
9005 }
9006
9007 if (focusDisplayed) {
9008 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9009 }
9010 if (animating) {
9011 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9012 }
9013 mQueue.setHoldScreenLocked(holdScreen != null);
9014 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9015 mPowerManager.setScreenBrightnessOverride(-1);
9016 } else {
9017 mPowerManager.setScreenBrightnessOverride((int)
9018 (screenBrightness * Power.BRIGHTNESS_ON));
9019 }
9020 if (holdScreen != mHoldingScreenOn) {
9021 mHoldingScreenOn = holdScreen;
9022 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9023 mH.sendMessage(m);
9024 }
9025 }
9026
9027 void requestAnimationLocked(long delay) {
9028 if (!mAnimationPending) {
9029 mAnimationPending = true;
9030 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9031 }
9032 }
Romain Guy06882f82009-06-10 13:36:04 -07009033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034 /**
9035 * Have the surface flinger show a surface, robustly dealing with
9036 * error conditions. In particular, if there is not enough memory
9037 * to show the surface, then we will try to get rid of other surfaces
9038 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009039 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009040 * @return Returns true if the surface was successfully shown.
9041 */
9042 boolean showSurfaceRobustlyLocked(WindowState win) {
9043 try {
9044 if (win.mSurface != null) {
9045 win.mSurface.show();
9046 }
9047 return true;
9048 } catch (RuntimeException e) {
9049 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9050 }
Romain Guy06882f82009-06-10 13:36:04 -07009051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009052 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009054 return false;
9055 }
Romain Guy06882f82009-06-10 13:36:04 -07009056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009057 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9058 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009060 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9061 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009063 if (mForceRemoves == null) {
9064 mForceRemoves = new ArrayList<WindowState>();
9065 }
Romain Guy06882f82009-06-10 13:36:04 -07009066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009067 long callingIdentity = Binder.clearCallingIdentity();
9068 try {
9069 // There was some problem... first, do a sanity check of the
9070 // window list to make sure we haven't left any dangling surfaces
9071 // around.
9072 int N = mWindows.size();
9073 boolean leakedSurface = false;
9074 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9075 for (int i=0; i<N; i++) {
9076 WindowState ws = (WindowState)mWindows.get(i);
9077 if (ws.mSurface != null) {
9078 if (!mSessions.contains(ws.mSession)) {
9079 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9080 + ws + " surface=" + ws.mSurface
9081 + " token=" + win.mToken
9082 + " pid=" + ws.mSession.mPid
9083 + " uid=" + ws.mSession.mUid);
9084 ws.mSurface.clear();
9085 ws.mSurface = null;
9086 mForceRemoves.add(ws);
9087 i--;
9088 N--;
9089 leakedSurface = true;
9090 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9091 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9092 + ws + " surface=" + ws.mSurface
9093 + " token=" + win.mAppToken);
9094 ws.mSurface.clear();
9095 ws.mSurface = null;
9096 leakedSurface = true;
9097 }
9098 }
9099 }
Romain Guy06882f82009-06-10 13:36:04 -07009100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009101 boolean killedApps = false;
9102 if (!leakedSurface) {
9103 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9104 SparseIntArray pidCandidates = new SparseIntArray();
9105 for (int i=0; i<N; i++) {
9106 WindowState ws = (WindowState)mWindows.get(i);
9107 if (ws.mSurface != null) {
9108 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9109 }
9110 }
9111 if (pidCandidates.size() > 0) {
9112 int[] pids = new int[pidCandidates.size()];
9113 for (int i=0; i<pids.length; i++) {
9114 pids[i] = pidCandidates.keyAt(i);
9115 }
9116 try {
9117 if (mActivityManager.killPidsForMemory(pids)) {
9118 killedApps = true;
9119 }
9120 } catch (RemoteException e) {
9121 }
9122 }
9123 }
Romain Guy06882f82009-06-10 13:36:04 -07009124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009125 if (leakedSurface || killedApps) {
9126 // We managed to reclaim some memory, so get rid of the trouble
9127 // surface and ask the app to request another one.
9128 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9129 if (surface != null) {
9130 surface.clear();
9131 win.mSurface = null;
9132 }
Romain Guy06882f82009-06-10 13:36:04 -07009133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009134 try {
9135 win.mClient.dispatchGetNewSurface();
9136 } catch (RemoteException e) {
9137 }
9138 }
9139 } finally {
9140 Binder.restoreCallingIdentity(callingIdentity);
9141 }
9142 }
Romain Guy06882f82009-06-10 13:36:04 -07009143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009144 private boolean updateFocusedWindowLocked(int mode) {
9145 WindowState newFocus = computeFocusedWindowLocked();
9146 if (mCurrentFocus != newFocus) {
9147 // This check makes sure that we don't already have the focus
9148 // change message pending.
9149 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9150 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9151 if (localLOGV) Log.v(
9152 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9153 final WindowState oldFocus = mCurrentFocus;
9154 mCurrentFocus = newFocus;
9155 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157 final WindowState imWindow = mInputMethodWindow;
9158 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009159 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009160 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009161 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9162 mLayoutNeeded = true;
9163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009164 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9165 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009166 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9167 // Client will do the layout, but we need to assign layers
9168 // for handleNewWindowLocked() below.
9169 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009170 }
9171 }
Romain Guy06882f82009-06-10 13:36:04 -07009172
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009173 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9174 mKeyWaiter.handleNewWindowLocked(newFocus);
9175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009176 return true;
9177 }
9178 return false;
9179 }
9180
9181 private WindowState computeFocusedWindowLocked() {
9182 WindowState result = null;
9183 WindowState win;
9184
9185 int i = mWindows.size() - 1;
9186 int nextAppIndex = mAppTokens.size()-1;
9187 WindowToken nextApp = nextAppIndex >= 0
9188 ? mAppTokens.get(nextAppIndex) : null;
9189
9190 while (i >= 0) {
9191 win = (WindowState)mWindows.get(i);
9192
9193 if (localLOGV || DEBUG_FOCUS) Log.v(
9194 TAG, "Looking for focus: " + i
9195 + " = " + win
9196 + ", flags=" + win.mAttrs.flags
9197 + ", canReceive=" + win.canReceiveKeys());
9198
9199 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009201 // If this window's application has been removed, just skip it.
9202 if (thisApp != null && thisApp.removed) {
9203 i--;
9204 continue;
9205 }
Romain Guy06882f82009-06-10 13:36:04 -07009206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009207 // If there is a focused app, don't allow focus to go to any
9208 // windows below it. If this is an application window, step
9209 // through the app tokens until we find its app.
9210 if (thisApp != null && nextApp != null && thisApp != nextApp
9211 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9212 int origAppIndex = nextAppIndex;
9213 while (nextAppIndex > 0) {
9214 if (nextApp == mFocusedApp) {
9215 // Whoops, we are below the focused app... no focus
9216 // for you!
9217 if (localLOGV || DEBUG_FOCUS) Log.v(
9218 TAG, "Reached focused app: " + mFocusedApp);
9219 return null;
9220 }
9221 nextAppIndex--;
9222 nextApp = mAppTokens.get(nextAppIndex);
9223 if (nextApp == thisApp) {
9224 break;
9225 }
9226 }
9227 if (thisApp != nextApp) {
9228 // Uh oh, the app token doesn't exist! This shouldn't
9229 // happen, but if it does we can get totally hosed...
9230 // so restart at the original app.
9231 nextAppIndex = origAppIndex;
9232 nextApp = mAppTokens.get(nextAppIndex);
9233 }
9234 }
9235
9236 // Dispatch to this window if it is wants key events.
9237 if (win.canReceiveKeys()) {
9238 if (DEBUG_FOCUS) Log.v(
9239 TAG, "Found focus @ " + i + " = " + win);
9240 result = win;
9241 break;
9242 }
9243
9244 i--;
9245 }
9246
9247 return result;
9248 }
9249
9250 private void startFreezingDisplayLocked() {
9251 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009252 // Freezing the display also suspends key event delivery, to
9253 // keep events from going astray while the display is reconfigured.
9254 // If someone has changed orientation again while the screen is
9255 // still frozen, the events will continue to be blocked while the
9256 // successive orientation change is processed. To prevent spurious
9257 // ANRs, we reset the event dispatch timeout in this case.
9258 synchronized (mKeyWaiter) {
9259 mKeyWaiter.mWasFrozen = true;
9260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009261 return;
9262 }
Romain Guy06882f82009-06-10 13:36:04 -07009263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009264 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266 long now = SystemClock.uptimeMillis();
9267 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9268 if (mFreezeGcPending != 0) {
9269 if (now > (mFreezeGcPending+1000)) {
9270 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9271 mH.removeMessages(H.FORCE_GC);
9272 Runtime.getRuntime().gc();
9273 mFreezeGcPending = now;
9274 }
9275 } else {
9276 mFreezeGcPending = now;
9277 }
Romain Guy06882f82009-06-10 13:36:04 -07009278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009279 mDisplayFrozen = true;
9280 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9281 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9282 mAppTransitionReady = true;
9283 }
Romain Guy06882f82009-06-10 13:36:04 -07009284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009285 if (PROFILE_ORIENTATION) {
9286 File file = new File("/data/system/frozen");
9287 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9288 }
9289 Surface.freezeDisplay(0);
9290 }
Romain Guy06882f82009-06-10 13:36:04 -07009291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009292 private void stopFreezingDisplayLocked() {
9293 if (!mDisplayFrozen) {
9294 return;
9295 }
Romain Guy06882f82009-06-10 13:36:04 -07009296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009297 mDisplayFrozen = false;
9298 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9299 if (PROFILE_ORIENTATION) {
9300 Debug.stopMethodTracing();
9301 }
9302 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009303
Chris Tate2ad63a92009-03-25 17:36:48 -07009304 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9305 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009306 synchronized (mKeyWaiter) {
9307 mKeyWaiter.mWasFrozen = true;
9308 mKeyWaiter.notifyAll();
9309 }
9310
9311 // A little kludge: a lot could have happened while the
9312 // display was frozen, so now that we are coming back we
9313 // do a gc so that any remote references the system
9314 // processes holds on others can be released if they are
9315 // no longer needed.
9316 mH.removeMessages(H.FORCE_GC);
9317 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9318 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009320 mScreenFrozenLock.release();
9321 }
Romain Guy06882f82009-06-10 13:36:04 -07009322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009323 @Override
9324 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9325 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9326 != PackageManager.PERMISSION_GRANTED) {
9327 pw.println("Permission Denial: can't dump WindowManager from from pid="
9328 + Binder.getCallingPid()
9329 + ", uid=" + Binder.getCallingUid());
9330 return;
9331 }
Romain Guy06882f82009-06-10 13:36:04 -07009332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009333 synchronized(mWindowMap) {
9334 pw.println("Current Window Manager state:");
9335 for (int i=mWindows.size()-1; i>=0; i--) {
9336 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009337 pw.print(" Window #"); pw.print(i); pw.print(' ');
9338 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339 w.dump(pw, " ");
9340 }
9341 if (mInputMethodDialogs.size() > 0) {
9342 pw.println(" ");
9343 pw.println(" Input method dialogs:");
9344 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9345 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009346 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009347 }
9348 }
9349 if (mPendingRemove.size() > 0) {
9350 pw.println(" ");
9351 pw.println(" Remove pending for:");
9352 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9353 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009354 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9355 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009356 w.dump(pw, " ");
9357 }
9358 }
9359 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9360 pw.println(" ");
9361 pw.println(" Windows force removing:");
9362 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9363 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009364 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9365 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009366 w.dump(pw, " ");
9367 }
9368 }
9369 if (mDestroySurface.size() > 0) {
9370 pw.println(" ");
9371 pw.println(" Windows waiting to destroy their surface:");
9372 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9373 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009374 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9375 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009376 w.dump(pw, " ");
9377 }
9378 }
9379 if (mLosingFocus.size() > 0) {
9380 pw.println(" ");
9381 pw.println(" Windows losing focus:");
9382 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9383 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009384 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9385 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009386 w.dump(pw, " ");
9387 }
9388 }
9389 if (mSessions.size() > 0) {
9390 pw.println(" ");
9391 pw.println(" All active sessions:");
9392 Iterator<Session> it = mSessions.iterator();
9393 while (it.hasNext()) {
9394 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009395 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009396 s.dump(pw, " ");
9397 }
9398 }
9399 if (mTokenMap.size() > 0) {
9400 pw.println(" ");
9401 pw.println(" All tokens:");
9402 Iterator<WindowToken> it = mTokenMap.values().iterator();
9403 while (it.hasNext()) {
9404 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009405 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009406 token.dump(pw, " ");
9407 }
9408 }
9409 if (mTokenList.size() > 0) {
9410 pw.println(" ");
9411 pw.println(" Window token list:");
9412 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009413 pw.print(" #"); pw.print(i); pw.print(": ");
9414 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 }
9416 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009417 if (mWallpaperTokens.size() > 0) {
9418 pw.println(" ");
9419 pw.println(" Wallpaper tokens:");
9420 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
9421 WindowToken token = mWallpaperTokens.get(i);
9422 pw.print(" Wallpaper #"); pw.print(i);
9423 pw.print(' '); pw.print(token); pw.println(':');
9424 token.dump(pw, " ");
9425 }
9426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 if (mAppTokens.size() > 0) {
9428 pw.println(" ");
9429 pw.println(" Application tokens in Z order:");
9430 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009431 pw.print(" App #"); pw.print(i); pw.print(": ");
9432 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009433 }
9434 }
9435 if (mFinishedStarting.size() > 0) {
9436 pw.println(" ");
9437 pw.println(" Finishing start of application tokens:");
9438 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9439 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009440 pw.print(" Finished Starting #"); pw.print(i);
9441 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009442 token.dump(pw, " ");
9443 }
9444 }
9445 if (mExitingTokens.size() > 0) {
9446 pw.println(" ");
9447 pw.println(" Exiting tokens:");
9448 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9449 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009450 pw.print(" Exiting #"); pw.print(i);
9451 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009452 token.dump(pw, " ");
9453 }
9454 }
9455 if (mExitingAppTokens.size() > 0) {
9456 pw.println(" ");
9457 pw.println(" Exiting application tokens:");
9458 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9459 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009460 pw.print(" Exiting App #"); pw.print(i);
9461 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009462 token.dump(pw, " ");
9463 }
9464 }
9465 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009466 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9467 pw.print(" mLastFocus="); pw.println(mLastFocus);
9468 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9469 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9470 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9471 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9472 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9473 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9474 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9475 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009476 if (mDimAnimator != null) {
9477 mDimAnimator.printTo(pw);
9478 } else {
9479 pw.print( " no DimAnimator ");
9480 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009481 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009482 pw.print(mInputMethodAnimLayerAdjustment);
9483 pw.print(" mWallpaperAnimLayerAdjustment=");
9484 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009485 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9486 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9487 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9488 pw.print(" mRotation="); pw.print(mRotation);
9489 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9490 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9491 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9492 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9493 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9494 pw.print(" mNextAppTransition=0x");
9495 pw.print(Integer.toHexString(mNextAppTransition));
9496 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9497 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9498 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9499 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9500 if (mOpeningApps.size() > 0) {
9501 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9502 }
9503 if (mClosingApps.size() > 0) {
9504 pw.print(" mClosingApps="); pw.println(mClosingApps);
9505 }
9506 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9507 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009508 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009509 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9510 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9511 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9512 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9513 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9514 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009515 }
9516 }
9517
9518 public void monitor() {
9519 synchronized (mWindowMap) { }
9520 synchronized (mKeyguardDisabled) { }
9521 synchronized (mKeyWaiter) { }
9522 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009523
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07009524 public void virtualKeyFeedback(KeyEvent event) {
9525 mPolicy.keyFeedbackFromInput(event);
9526 }
9527
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009528 /**
9529 * DimAnimator class that controls the dim animation. This holds the surface and
9530 * all state used for dim animation.
9531 */
9532 private static class DimAnimator {
9533 Surface mDimSurface;
9534 boolean mDimShown = false;
9535 float mDimCurrentAlpha;
9536 float mDimTargetAlpha;
9537 float mDimDeltaPerMs;
9538 long mLastDimAnimTime;
9539
9540 DimAnimator (SurfaceSession session) {
9541 if (mDimSurface == null) {
9542 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9543 + mDimSurface + ": CREATE");
9544 try {
9545 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9546 Surface.FX_SURFACE_DIM);
9547 } catch (Exception e) {
9548 Log.e(TAG, "Exception creating Dim surface", e);
9549 }
9550 }
9551 }
9552
9553 /**
9554 * Show the dim surface.
9555 */
9556 void show(int dw, int dh) {
9557 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9558 dw + "x" + dh + ")");
9559 mDimShown = true;
9560 try {
9561 mDimSurface.setPosition(0, 0);
9562 mDimSurface.setSize(dw, dh);
9563 mDimSurface.show();
9564 } catch (RuntimeException e) {
9565 Log.w(TAG, "Failure showing dim surface", e);
9566 }
9567 }
9568
9569 /**
9570 * Set's the dim surface's layer and update dim parameters that will be used in
9571 * {@link updateSurface} after all windows are examined.
9572 */
9573 void updateParameters(WindowState w, long currentTime) {
9574 mDimSurface.setLayer(w.mAnimLayer-1);
9575
9576 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9577 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9578 if (mDimTargetAlpha != target) {
9579 // If the desired dim level has changed, then
9580 // start an animation to it.
9581 mLastDimAnimTime = currentTime;
9582 long duration = (w.mAnimating && w.mAnimation != null)
9583 ? w.mAnimation.computeDurationHint()
9584 : DEFAULT_DIM_DURATION;
9585 if (target > mDimTargetAlpha) {
9586 // This is happening behind the activity UI,
9587 // so we can make it run a little longer to
9588 // give a stronger impression without disrupting
9589 // the user.
9590 duration *= DIM_DURATION_MULTIPLIER;
9591 }
9592 if (duration < 1) {
9593 // Don't divide by zero
9594 duration = 1;
9595 }
9596 mDimTargetAlpha = target;
9597 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9598 }
9599 }
9600
9601 /**
9602 * Updating the surface's alpha. Returns true if the animation continues, or returns
9603 * false when the animation is finished and the dim surface is hidden.
9604 */
9605 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9606 if (!dimming) {
9607 if (mDimTargetAlpha != 0) {
9608 mLastDimAnimTime = currentTime;
9609 mDimTargetAlpha = 0;
9610 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9611 }
9612 }
9613
9614 boolean animating = false;
9615 if (mLastDimAnimTime != 0) {
9616 mDimCurrentAlpha += mDimDeltaPerMs
9617 * (currentTime-mLastDimAnimTime);
9618 boolean more = true;
9619 if (displayFrozen) {
9620 // If the display is frozen, there is no reason to animate.
9621 more = false;
9622 } else if (mDimDeltaPerMs > 0) {
9623 if (mDimCurrentAlpha > mDimTargetAlpha) {
9624 more = false;
9625 }
9626 } else if (mDimDeltaPerMs < 0) {
9627 if (mDimCurrentAlpha < mDimTargetAlpha) {
9628 more = false;
9629 }
9630 } else {
9631 more = false;
9632 }
9633
9634 // Do we need to continue animating?
9635 if (more) {
9636 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9637 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9638 mLastDimAnimTime = currentTime;
9639 mDimSurface.setAlpha(mDimCurrentAlpha);
9640 animating = true;
9641 } else {
9642 mDimCurrentAlpha = mDimTargetAlpha;
9643 mLastDimAnimTime = 0;
9644 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9645 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9646 mDimSurface.setAlpha(mDimCurrentAlpha);
9647 if (!dimming) {
9648 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9649 + ": HIDE");
9650 try {
9651 mDimSurface.hide();
9652 } catch (RuntimeException e) {
9653 Log.w(TAG, "Illegal argument exception hiding dim surface");
9654 }
9655 mDimShown = false;
9656 }
9657 }
9658 }
9659 return animating;
9660 }
9661
9662 public void printTo(PrintWriter pw) {
9663 pw.print(" mDimShown="); pw.print(mDimShown);
9664 pw.print(" current="); pw.print(mDimCurrentAlpha);
9665 pw.print(" target="); pw.print(mDimTargetAlpha);
9666 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9667 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9668 }
9669 }
9670
9671 /**
9672 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9673 * This is used for opening/closing transition for apps in compatible mode.
9674 */
9675 private static class FadeInOutAnimation extends Animation {
9676 int mWidth;
9677 boolean mFadeIn;
9678
9679 public FadeInOutAnimation(boolean fadeIn) {
9680 setInterpolator(new AccelerateInterpolator());
9681 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9682 mFadeIn = fadeIn;
9683 }
9684
9685 @Override
9686 protected void applyTransformation(float interpolatedTime, Transformation t) {
9687 float x = interpolatedTime;
9688 if (!mFadeIn) {
9689 x = 1.0f - x; // reverse the interpolation for fade out
9690 }
9691 if (x < 0.5) {
9692 // move the window out of the screen.
9693 t.getMatrix().setTranslate(mWidth, 0);
9694 } else {
9695 t.getMatrix().setTranslate(0, 0);// show
9696 t.setAlpha((x - 0.5f) * 2);
9697 }
9698 }
9699
9700 @Override
9701 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9702 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9703 mWidth = width;
9704 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009705
9706 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07009707 public int getZAdjustment() {
9708 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009709 }
9710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009711}