blob: b0f049eceb637efe8c50213381a18a089b780a2d [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
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001240 if (visible) {
1241 updateWallpaperOffsetLocked(mWallpaperTarget, wallpaper, dw, dh);
1242 }
1243
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001244 // First, make sure the client has the current visibility
1245 // state.
1246 if (wallpaper.mWallpaperVisible != visible) {
1247 wallpaper.mWallpaperVisible = visible;
1248 try {
1249 if (DEBUG_VISIBILITY) Log.v(TAG,
1250 "Setting visibility of wallpaper " + wallpaper
1251 + ": " + visible);
1252 wallpaper.mClient.dispatchAppVisibility(visible);
1253 } catch (RemoteException e) {
1254 }
1255 }
1256
1257 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) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001309 boolean changed = false;
1310 if (target.mWallpaperX >= 0) {
1311 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1312 int offset = availw > 0 ? -(int)(availw*target.mWallpaperX+.5f) : 0;
1313 changed = wallpaperWin.mXOffset != offset;
1314 if (changed) {
1315 wallpaperWin.mXOffset = offset;
1316 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001317 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001318 if (target.mWallpaperY >= 0) {
1319 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1320 int offset = availh > 0 ? -(int)(availh*target.mWallpaperY+.5f) : 0;
1321 if (wallpaperWin.mYOffset != offset) {
1322 changed = true;
1323 wallpaperWin.mYOffset = offset;
1324 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001325 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001326
1327 if (wallpaperWin.mWallpaperX != target.mWallpaperX
1328 || wallpaperWin.mWallpaperY != target.mWallpaperY) {
1329 wallpaperWin.mWallpaperX = target.mWallpaperX;
1330 wallpaperWin.mWallpaperY = target.mWallpaperY;
1331 try {
1332 wallpaperWin.mClient.dispatchWallpaperOffsets(
1333 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY);
1334 } catch (RemoteException e) {
1335 }
1336 }
1337
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001338 return changed;
1339 }
1340
1341 boolean updateWallpaperOffsetLocked() {
1342 final int dw = mDisplay.getWidth();
1343 final int dh = mDisplay.getHeight();
1344
1345 boolean changed = false;
1346
1347 WindowState target = mWallpaperTarget;
1348 if (target != null) {
1349 int curTokenIndex = mWallpaperTokens.size();
1350 while (curTokenIndex > 0) {
1351 curTokenIndex--;
1352 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1353 int curWallpaperIndex = token.windows.size();
1354 while (curWallpaperIndex > 0) {
1355 curWallpaperIndex--;
1356 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1357 if (updateWallpaperOffsetLocked(target, wallpaper, dw, dh)) {
1358 wallpaper.computeShownFrameLocked();
1359 changed = true;
1360 }
1361 }
1362 }
1363 }
1364
1365 return changed;
1366 }
1367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 public int addWindow(Session session, IWindow client,
1369 WindowManager.LayoutParams attrs, int viewVisibility,
1370 Rect outContentInsets) {
1371 int res = mPolicy.checkAddPermission(attrs);
1372 if (res != WindowManagerImpl.ADD_OKAY) {
1373 return res;
1374 }
Romain Guy06882f82009-06-10 13:36:04 -07001375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 boolean reportNewConfig = false;
1377 WindowState attachedWindow = null;
1378 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 synchronized(mWindowMap) {
1381 // Instantiating a Display requires talking with the simulator,
1382 // so don't do it until we know the system is mostly up and
1383 // running.
1384 if (mDisplay == null) {
1385 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1386 mDisplay = wm.getDefaultDisplay();
1387 mQueue.setDisplay(mDisplay);
1388 reportNewConfig = true;
1389 }
Romain Guy06882f82009-06-10 13:36:04 -07001390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 if (mWindowMap.containsKey(client.asBinder())) {
1392 Log.w(TAG, "Window " + client + " is already added");
1393 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1394 }
1395
1396 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001397 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 if (attachedWindow == null) {
1399 Log.w(TAG, "Attempted to add window with token that is not a window: "
1400 + attrs.token + ". Aborting.");
1401 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1402 }
1403 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1404 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1405 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1406 + attrs.token + ". Aborting.");
1407 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1408 }
1409 }
1410
1411 boolean addToken = false;
1412 WindowToken token = mTokenMap.get(attrs.token);
1413 if (token == null) {
1414 if (attrs.type >= FIRST_APPLICATION_WINDOW
1415 && attrs.type <= LAST_APPLICATION_WINDOW) {
1416 Log.w(TAG, "Attempted to add application window with unknown token "
1417 + attrs.token + ". Aborting.");
1418 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1419 }
1420 if (attrs.type == TYPE_INPUT_METHOD) {
1421 Log.w(TAG, "Attempted to add input method window with unknown token "
1422 + attrs.token + ". Aborting.");
1423 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1424 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001425 if (attrs.type == TYPE_WALLPAPER) {
1426 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1427 + attrs.token + ". Aborting.");
1428 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 token = new WindowToken(attrs.token, -1, false);
1431 addToken = true;
1432 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1433 && attrs.type <= LAST_APPLICATION_WINDOW) {
1434 AppWindowToken atoken = token.appWindowToken;
1435 if (atoken == null) {
1436 Log.w(TAG, "Attempted to add window with non-application token "
1437 + token + ". Aborting.");
1438 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1439 } else if (atoken.removed) {
1440 Log.w(TAG, "Attempted to add window with exiting application token "
1441 + token + ". Aborting.");
1442 return WindowManagerImpl.ADD_APP_EXITING;
1443 }
1444 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1445 // No need for this guy!
1446 if (localLOGV) Log.v(
1447 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1448 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1449 }
1450 } else if (attrs.type == TYPE_INPUT_METHOD) {
1451 if (token.windowType != TYPE_INPUT_METHOD) {
1452 Log.w(TAG, "Attempted to add input method window with bad token "
1453 + attrs.token + ". Aborting.");
1454 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1455 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001456 } else if (attrs.type == TYPE_WALLPAPER) {
1457 if (token.windowType != TYPE_WALLPAPER) {
1458 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1459 + attrs.token + ". Aborting.");
1460 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 }
1463
1464 win = new WindowState(session, client, token,
1465 attachedWindow, attrs, viewVisibility);
1466 if (win.mDeathRecipient == null) {
1467 // Client has apparently died, so there is no reason to
1468 // continue.
1469 Log.w(TAG, "Adding window client " + client.asBinder()
1470 + " that is dead, aborting.");
1471 return WindowManagerImpl.ADD_APP_EXITING;
1472 }
1473
1474 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 res = mPolicy.prepareAddWindowLw(win, attrs);
1477 if (res != WindowManagerImpl.ADD_OKAY) {
1478 return res;
1479 }
1480
1481 // From now on, no exceptions or errors allowed!
1482
1483 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 if (addToken) {
1488 mTokenMap.put(attrs.token, token);
1489 mTokenList.add(token);
1490 }
1491 win.attach();
1492 mWindowMap.put(client.asBinder(), win);
1493
1494 if (attrs.type == TYPE_APPLICATION_STARTING &&
1495 token.appWindowToken != null) {
1496 token.appWindowToken.startingWindow = win;
1497 }
1498
1499 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 if (attrs.type == TYPE_INPUT_METHOD) {
1502 mInputMethodWindow = win;
1503 addInputMethodWindowToListLocked(win);
1504 imMayMove = false;
1505 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1506 mInputMethodDialogs.add(win);
1507 addWindowToListInOrderLocked(win, true);
1508 adjustInputMethodDialogsLocked();
1509 imMayMove = false;
1510 } else {
1511 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001512 if (attrs.type == TYPE_WALLPAPER ||
1513 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1514 adjustWallpaperWindowsLocked();
1515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 }
Romain Guy06882f82009-06-10 13:36:04 -07001517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 if (mInTouchMode) {
1523 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1524 }
1525 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1526 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1527 }
Romain Guy06882f82009-06-10 13:36:04 -07001528
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001529 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001531 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1532 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 imMayMove = false;
1534 }
1535 }
Romain Guy06882f82009-06-10 13:36:04 -07001536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001538 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 }
Romain Guy06882f82009-06-10 13:36:04 -07001540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 assignLayersLocked();
1542 // Don't do layout here, the window must call
1543 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 //dump();
1546
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001547 if (focusChanged) {
1548 if (mCurrentFocus != null) {
1549 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1550 }
1551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 if (localLOGV) Log.v(
1553 TAG, "New client " + client.asBinder()
1554 + ": window=" + win);
1555 }
1556
1557 // sendNewConfiguration() checks caller permissions so we must call it with
1558 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1559 // identity anyway, so it's safe to just clear & restore around this whole
1560 // block.
1561 final long origId = Binder.clearCallingIdentity();
1562 if (reportNewConfig) {
1563 sendNewConfiguration();
1564 } else {
1565 // Update Orientation after adding a window, only if the window needs to be
1566 // displayed right away
1567 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001568 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 sendNewConfiguration();
1570 }
1571 }
1572 }
1573 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 return res;
1576 }
Romain Guy06882f82009-06-10 13:36:04 -07001577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 public void removeWindow(Session session, IWindow client) {
1579 synchronized(mWindowMap) {
1580 WindowState win = windowForClientLocked(session, client);
1581 if (win == null) {
1582 return;
1583 }
1584 removeWindowLocked(session, win);
1585 }
1586 }
Romain Guy06882f82009-06-10 13:36:04 -07001587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 public void removeWindowLocked(Session session, WindowState win) {
1589
1590 if (localLOGV || DEBUG_FOCUS) Log.v(
1591 TAG, "Remove " + win + " client="
1592 + Integer.toHexString(System.identityHashCode(
1593 win.mClient.asBinder()))
1594 + ", surface=" + win.mSurface);
1595
1596 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 if (DEBUG_APP_TRANSITIONS) Log.v(
1599 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1600 + " mExiting=" + win.mExiting
1601 + " isAnimating=" + win.isAnimating()
1602 + " app-animation="
1603 + (win.mAppToken != null ? win.mAppToken.animation : null)
1604 + " inPendingTransaction="
1605 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1606 + " mDisplayFrozen=" + mDisplayFrozen);
1607 // Visibility of the removed window. Will be used later to update orientation later on.
1608 boolean wasVisible = false;
1609 // First, see if we need to run an animation. If we do, we have
1610 // to hold off on removing the window until the animation is done.
1611 // If the display is frozen, just remove immediately, since the
1612 // animation wouldn't be seen.
1613 if (win.mSurface != null && !mDisplayFrozen) {
1614 // If we are not currently running the exit animation, we
1615 // need to see about starting one.
1616 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1619 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1620 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1621 }
1622 // Try starting an animation.
1623 if (applyAnimationLocked(win, transit, false)) {
1624 win.mExiting = true;
1625 }
1626 }
1627 if (win.mExiting || win.isAnimating()) {
1628 // The exit animation is running... wait for it!
1629 //Log.i(TAG, "*** Running exit animation...");
1630 win.mExiting = true;
1631 win.mRemoveOnExit = true;
1632 mLayoutNeeded = true;
1633 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1634 performLayoutAndPlaceSurfacesLocked();
1635 if (win.mAppToken != null) {
1636 win.mAppToken.updateReportedVisibilityLocked();
1637 }
1638 //dump();
1639 Binder.restoreCallingIdentity(origId);
1640 return;
1641 }
1642 }
1643
1644 removeWindowInnerLocked(session, win);
1645 // Removing a visible window will effect the computed orientation
1646 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001647 if (wasVisible && computeForcedAppOrientationLocked()
1648 != mForcedAppOrientation) {
1649 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 }
1651 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1652 Binder.restoreCallingIdentity(origId);
1653 }
Romain Guy06882f82009-06-10 13:36:04 -07001654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 private void removeWindowInnerLocked(Session session, WindowState win) {
1656 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1657 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 if (mInputMethodTarget == win) {
1662 moveInputMethodWindowsIfNeededLocked(false);
1663 }
Romain Guy06882f82009-06-10 13:36:04 -07001664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 mPolicy.removeWindowLw(win);
1666 win.removeLocked();
1667
1668 mWindowMap.remove(win.mClient.asBinder());
1669 mWindows.remove(win);
1670
1671 if (mInputMethodWindow == win) {
1672 mInputMethodWindow = null;
1673 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1674 mInputMethodDialogs.remove(win);
1675 }
Romain Guy06882f82009-06-10 13:36:04 -07001676
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001677 if (win.mAttrs.type == TYPE_WALLPAPER ||
1678 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1679 adjustWallpaperWindowsLocked();
1680 }
1681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 final WindowToken token = win.mToken;
1683 final AppWindowToken atoken = win.mAppToken;
1684 token.windows.remove(win);
1685 if (atoken != null) {
1686 atoken.allAppWindows.remove(win);
1687 }
1688 if (localLOGV) Log.v(
1689 TAG, "**** Removing window " + win + ": count="
1690 + token.windows.size());
1691 if (token.windows.size() == 0) {
1692 if (!token.explicit) {
1693 mTokenMap.remove(token.token);
1694 mTokenList.remove(token);
1695 } else if (atoken != null) {
1696 atoken.firstWindowDrawn = false;
1697 }
1698 }
1699
1700 if (atoken != null) {
1701 if (atoken.startingWindow == win) {
1702 atoken.startingWindow = null;
1703 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1704 // If this is the last window and we had requested a starting
1705 // transition window, well there is no point now.
1706 atoken.startingData = null;
1707 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1708 // If this is the last window except for a starting transition
1709 // window, we need to get rid of the starting transition.
1710 if (DEBUG_STARTING_WINDOW) {
1711 Log.v(TAG, "Schedule remove starting " + token
1712 + ": no more real windows");
1713 }
1714 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1715 mH.sendMessage(m);
1716 }
1717 }
Romain Guy06882f82009-06-10 13:36:04 -07001718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 if (!mInLayout) {
1720 assignLayersLocked();
1721 mLayoutNeeded = true;
1722 performLayoutAndPlaceSurfacesLocked();
1723 if (win.mAppToken != null) {
1724 win.mAppToken.updateReportedVisibilityLocked();
1725 }
1726 }
1727 }
1728
1729 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1730 long origId = Binder.clearCallingIdentity();
1731 try {
1732 synchronized (mWindowMap) {
1733 WindowState w = windowForClientLocked(session, client);
1734 if ((w != null) && (w.mSurface != null)) {
1735 Surface.openTransaction();
1736 try {
1737 w.mSurface.setTransparentRegionHint(region);
1738 } finally {
1739 Surface.closeTransaction();
1740 }
1741 }
1742 }
1743 } finally {
1744 Binder.restoreCallingIdentity(origId);
1745 }
1746 }
1747
1748 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001749 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 Rect visibleInsets) {
1751 long origId = Binder.clearCallingIdentity();
1752 try {
1753 synchronized (mWindowMap) {
1754 WindowState w = windowForClientLocked(session, client);
1755 if (w != null) {
1756 w.mGivenInsetsPending = false;
1757 w.mGivenContentInsets.set(contentInsets);
1758 w.mGivenVisibleInsets.set(visibleInsets);
1759 w.mTouchableInsets = touchableInsets;
1760 mLayoutNeeded = true;
1761 performLayoutAndPlaceSurfacesLocked();
1762 }
1763 }
1764 } finally {
1765 Binder.restoreCallingIdentity(origId);
1766 }
1767 }
Romain Guy06882f82009-06-10 13:36:04 -07001768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 public void getWindowDisplayFrame(Session session, IWindow client,
1770 Rect outDisplayFrame) {
1771 synchronized(mWindowMap) {
1772 WindowState win = windowForClientLocked(session, client);
1773 if (win == null) {
1774 outDisplayFrame.setEmpty();
1775 return;
1776 }
1777 outDisplayFrame.set(win.mDisplayFrame);
1778 }
1779 }
1780
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001781 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
1782 if (window.mWallpaperX != x || window.mWallpaperY != y) {
1783 window.mWallpaperX = x;
1784 window.mWallpaperY = y;
1785
1786 if (mWallpaperTarget == window) {
1787 if (updateWallpaperOffsetLocked()) {
1788 performLayoutAndPlaceSurfacesLocked();
1789 }
1790 }
1791 }
1792 }
1793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 public int relayoutWindow(Session session, IWindow client,
1795 WindowManager.LayoutParams attrs, int requestedWidth,
1796 int requestedHeight, int viewVisibility, boolean insetsPending,
1797 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1798 Surface outSurface) {
1799 boolean displayed = false;
1800 boolean inTouchMode;
1801 Configuration newConfig = null;
1802 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 synchronized(mWindowMap) {
1805 WindowState win = windowForClientLocked(session, client);
1806 if (win == null) {
1807 return 0;
1808 }
1809 win.mRequestedWidth = requestedWidth;
1810 win.mRequestedHeight = requestedHeight;
1811
1812 if (attrs != null) {
1813 mPolicy.adjustWindowParamsLw(attrs);
1814 }
Romain Guy06882f82009-06-10 13:36:04 -07001815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 int attrChanges = 0;
1817 int flagChanges = 0;
1818 if (attrs != null) {
1819 flagChanges = win.mAttrs.flags ^= attrs.flags;
1820 attrChanges = win.mAttrs.copyFrom(attrs);
1821 }
1822
1823 if (localLOGV) Log.v(
1824 TAG, "Relayout given client " + client.asBinder()
1825 + " (" + win.mAttrs.getTitle() + ")");
1826
1827
1828 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1829 win.mAlpha = attrs.alpha;
1830 }
1831
1832 final boolean scaledWindow =
1833 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1834
1835 if (scaledWindow) {
1836 // requested{Width|Height} Surface's physical size
1837 // attrs.{width|height} Size on screen
1838 win.mHScale = (attrs.width != requestedWidth) ?
1839 (attrs.width / (float)requestedWidth) : 1.0f;
1840 win.mVScale = (attrs.height != requestedHeight) ?
1841 (attrs.height / (float)requestedHeight) : 1.0f;
1842 }
1843
1844 boolean imMayMove = (flagChanges&(
1845 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1846 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 boolean focusMayChange = win.mViewVisibility != viewVisibility
1849 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1850 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001851
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001852 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
1853 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
1854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 win.mRelayoutCalled = true;
1856 final int oldVisibility = win.mViewVisibility;
1857 win.mViewVisibility = viewVisibility;
1858 if (viewVisibility == View.VISIBLE &&
1859 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1860 displayed = !win.isVisibleLw();
1861 if (win.mExiting) {
1862 win.mExiting = false;
1863 win.mAnimation = null;
1864 }
1865 if (win.mDestroying) {
1866 win.mDestroying = false;
1867 mDestroySurface.remove(win);
1868 }
1869 if (oldVisibility == View.GONE) {
1870 win.mEnterAnimationPending = true;
1871 }
1872 if (displayed && win.mSurface != null && !win.mDrawPending
1873 && !win.mCommitDrawPending && !mDisplayFrozen) {
1874 applyEnterAnimationLocked(win);
1875 }
1876 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1877 // To change the format, we need to re-build the surface.
1878 win.destroySurfaceLocked();
1879 displayed = true;
1880 }
1881 try {
1882 Surface surface = win.createSurfaceLocked();
1883 if (surface != null) {
1884 outSurface.copyFrom(surface);
1885 } else {
1886 outSurface.clear();
1887 }
1888 } catch (Exception e) {
1889 Log.w(TAG, "Exception thrown when creating surface for client "
1890 + client + " (" + win.mAttrs.getTitle() + ")",
1891 e);
1892 Binder.restoreCallingIdentity(origId);
1893 return 0;
1894 }
1895 if (displayed) {
1896 focusMayChange = true;
1897 }
1898 if (win.mAttrs.type == TYPE_INPUT_METHOD
1899 && mInputMethodWindow == null) {
1900 mInputMethodWindow = win;
1901 imMayMove = true;
1902 }
1903 } else {
1904 win.mEnterAnimationPending = false;
1905 if (win.mSurface != null) {
1906 // If we are not currently running the exit animation, we
1907 // need to see about starting one.
1908 if (!win.mExiting) {
1909 // Try starting an animation; if there isn't one, we
1910 // can destroy the surface right away.
1911 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1912 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1913 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1914 }
1915 if (win.isWinVisibleLw() &&
1916 applyAnimationLocked(win, transit, false)) {
1917 win.mExiting = true;
1918 mKeyWaiter.finishedKey(session, client, true,
1919 KeyWaiter.RETURN_NOTHING);
1920 } else if (win.isAnimating()) {
1921 // Currently in a hide animation... turn this into
1922 // an exit.
1923 win.mExiting = true;
1924 } else {
1925 if (mInputMethodWindow == win) {
1926 mInputMethodWindow = null;
1927 }
1928 win.destroySurfaceLocked();
1929 }
1930 }
1931 }
1932 outSurface.clear();
1933 }
1934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 if (focusMayChange) {
1936 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1937 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 imMayMove = false;
1939 }
1940 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1941 }
Romain Guy06882f82009-06-10 13:36:04 -07001942
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001943 // updateFocusedWindowLocked() already assigned layers so we only need to
1944 // reassign them at this point if the IM window state gets shuffled
1945 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 if (imMayMove) {
1948 if (moveInputMethodWindowsIfNeededLocked(false)) {
1949 assignLayers = true;
1950 }
1951 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001952 if (wallpaperMayMove) {
1953 if (adjustWallpaperWindowsLocked()) {
1954 assignLayers = true;
1955 }
1956 }
Romain Guy06882f82009-06-10 13:36:04 -07001957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 mLayoutNeeded = true;
1959 win.mGivenInsetsPending = insetsPending;
1960 if (assignLayers) {
1961 assignLayersLocked();
1962 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001963 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 performLayoutAndPlaceSurfacesLocked();
1965 if (win.mAppToken != null) {
1966 win.mAppToken.updateReportedVisibilityLocked();
1967 }
1968 outFrame.set(win.mFrame);
1969 outContentInsets.set(win.mContentInsets);
1970 outVisibleInsets.set(win.mVisibleInsets);
1971 if (localLOGV) Log.v(
1972 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001973 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 + ", requestedHeight=" + requestedHeight
1975 + ", viewVisibility=" + viewVisibility
1976 + "\nRelayout returning frame=" + outFrame
1977 + ", surface=" + outSurface);
1978
1979 if (localLOGV || DEBUG_FOCUS) Log.v(
1980 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1981
1982 inTouchMode = mInTouchMode;
1983 }
1984
1985 if (newConfig != null) {
1986 sendNewConfiguration();
1987 }
Romain Guy06882f82009-06-10 13:36:04 -07001988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1992 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1993 }
1994
1995 public void finishDrawingWindow(Session session, IWindow client) {
1996 final long origId = Binder.clearCallingIdentity();
1997 synchronized(mWindowMap) {
1998 WindowState win = windowForClientLocked(session, client);
1999 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002000 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2001 adjustWallpaperWindowsLocked();
2002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 mLayoutNeeded = true;
2004 performLayoutAndPlaceSurfacesLocked();
2005 }
2006 }
2007 Binder.restoreCallingIdentity(origId);
2008 }
2009
2010 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2011 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2012 + (lp != null ? lp.packageName : null)
2013 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2014 if (lp != null && lp.windowAnimations != 0) {
2015 // If this is a system resource, don't try to load it from the
2016 // application resources. It is nice to avoid loading application
2017 // resources if we can.
2018 String packageName = lp.packageName != null ? lp.packageName : "android";
2019 int resId = lp.windowAnimations;
2020 if ((resId&0xFF000000) == 0x01000000) {
2021 packageName = "android";
2022 }
2023 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2024 + packageName);
2025 return AttributeCache.instance().get(packageName, resId,
2026 com.android.internal.R.styleable.WindowAnimation);
2027 }
2028 return null;
2029 }
Romain Guy06882f82009-06-10 13:36:04 -07002030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 private void applyEnterAnimationLocked(WindowState win) {
2032 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2033 if (win.mEnterAnimationPending) {
2034 win.mEnterAnimationPending = false;
2035 transit = WindowManagerPolicy.TRANSIT_ENTER;
2036 }
2037
2038 applyAnimationLocked(win, transit, true);
2039 }
2040
2041 private boolean applyAnimationLocked(WindowState win,
2042 int transit, boolean isEntrance) {
2043 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2044 // If we are trying to apply an animation, but already running
2045 // an animation of the same type, then just leave that one alone.
2046 return true;
2047 }
Romain Guy06882f82009-06-10 13:36:04 -07002048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 // Only apply an animation if the display isn't frozen. If it is
2050 // frozen, there is no reason to animate and it can cause strange
2051 // artifacts when we unfreeze the display if some different animation
2052 // is running.
2053 if (!mDisplayFrozen) {
2054 int anim = mPolicy.selectAnimationLw(win, transit);
2055 int attr = -1;
2056 Animation a = null;
2057 if (anim != 0) {
2058 a = AnimationUtils.loadAnimation(mContext, anim);
2059 } else {
2060 switch (transit) {
2061 case WindowManagerPolicy.TRANSIT_ENTER:
2062 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2063 break;
2064 case WindowManagerPolicy.TRANSIT_EXIT:
2065 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2066 break;
2067 case WindowManagerPolicy.TRANSIT_SHOW:
2068 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2069 break;
2070 case WindowManagerPolicy.TRANSIT_HIDE:
2071 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2072 break;
2073 }
2074 if (attr >= 0) {
2075 a = loadAnimation(win.mAttrs, attr);
2076 }
2077 }
2078 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2079 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2080 + " mAnimation=" + win.mAnimation
2081 + " isEntrance=" + isEntrance);
2082 if (a != null) {
2083 if (DEBUG_ANIM) {
2084 RuntimeException e = new RuntimeException();
2085 e.fillInStackTrace();
2086 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2087 }
2088 win.setAnimation(a);
2089 win.mAnimationIsEntrance = isEntrance;
2090 }
2091 } else {
2092 win.clearAnimation();
2093 }
2094
2095 return win.mAnimation != null;
2096 }
2097
2098 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2099 int anim = 0;
2100 Context context = mContext;
2101 if (animAttr >= 0) {
2102 AttributeCache.Entry ent = getCachedAnimations(lp);
2103 if (ent != null) {
2104 context = ent.context;
2105 anim = ent.array.getResourceId(animAttr, 0);
2106 }
2107 }
2108 if (anim != 0) {
2109 return AnimationUtils.loadAnimation(context, anim);
2110 }
2111 return null;
2112 }
Romain Guy06882f82009-06-10 13:36:04 -07002113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 private boolean applyAnimationLocked(AppWindowToken wtoken,
2115 WindowManager.LayoutParams lp, int transit, boolean enter) {
2116 // Only apply an animation if the display isn't frozen. If it is
2117 // frozen, there is no reason to animate and it can cause strange
2118 // artifacts when we unfreeze the display if some different animation
2119 // is running.
2120 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002121 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002122 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002123 a = new FadeInOutAnimation(enter);
2124 if (DEBUG_ANIM) Log.v(TAG,
2125 "applying FadeInOutAnimation for a window in compatibility mode");
2126 } else {
2127 int animAttr = 0;
2128 switch (transit) {
2129 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2130 animAttr = enter
2131 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2132 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2133 break;
2134 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2135 animAttr = enter
2136 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2137 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2138 break;
2139 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2140 animAttr = enter
2141 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2142 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2143 break;
2144 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2145 animAttr = enter
2146 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2147 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2148 break;
2149 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2150 animAttr = enter
2151 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2152 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2153 break;
2154 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2155 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002156 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002157 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2158 break;
2159 }
2160 a = loadAnimation(lp, animAttr);
2161 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2162 + " anim=" + a
2163 + " animAttr=0x" + Integer.toHexString(animAttr)
2164 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 if (a != null) {
2167 if (DEBUG_ANIM) {
2168 RuntimeException e = new RuntimeException();
2169 e.fillInStackTrace();
2170 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2171 }
2172 wtoken.setAnimation(a);
2173 }
2174 } else {
2175 wtoken.clearAnimation();
2176 }
2177
2178 return wtoken.animation != null;
2179 }
2180
2181 // -------------------------------------------------------------
2182 // Application Window Tokens
2183 // -------------------------------------------------------------
2184
2185 public void validateAppTokens(List tokens) {
2186 int v = tokens.size()-1;
2187 int m = mAppTokens.size()-1;
2188 while (v >= 0 && m >= 0) {
2189 AppWindowToken wtoken = mAppTokens.get(m);
2190 if (wtoken.removed) {
2191 m--;
2192 continue;
2193 }
2194 if (tokens.get(v) != wtoken.token) {
2195 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2196 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2197 }
2198 v--;
2199 m--;
2200 }
2201 while (v >= 0) {
2202 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2203 v--;
2204 }
2205 while (m >= 0) {
2206 AppWindowToken wtoken = mAppTokens.get(m);
2207 if (!wtoken.removed) {
2208 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2209 }
2210 m--;
2211 }
2212 }
2213
2214 boolean checkCallingPermission(String permission, String func) {
2215 // Quick check: if the calling permission is me, it's all okay.
2216 if (Binder.getCallingPid() == Process.myPid()) {
2217 return true;
2218 }
Romain Guy06882f82009-06-10 13:36:04 -07002219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 if (mContext.checkCallingPermission(permission)
2221 == PackageManager.PERMISSION_GRANTED) {
2222 return true;
2223 }
2224 String msg = "Permission Denial: " + func + " from pid="
2225 + Binder.getCallingPid()
2226 + ", uid=" + Binder.getCallingUid()
2227 + " requires " + permission;
2228 Log.w(TAG, msg);
2229 return false;
2230 }
Romain Guy06882f82009-06-10 13:36:04 -07002231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 AppWindowToken findAppWindowToken(IBinder token) {
2233 WindowToken wtoken = mTokenMap.get(token);
2234 if (wtoken == null) {
2235 return null;
2236 }
2237 return wtoken.appWindowToken;
2238 }
Romain Guy06882f82009-06-10 13:36:04 -07002239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 public void addWindowToken(IBinder token, int type) {
2241 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2242 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002243 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 }
Romain Guy06882f82009-06-10 13:36:04 -07002245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 synchronized(mWindowMap) {
2247 WindowToken wtoken = mTokenMap.get(token);
2248 if (wtoken != null) {
2249 Log.w(TAG, "Attempted to add existing input method token: " + token);
2250 return;
2251 }
2252 wtoken = new WindowToken(token, type, true);
2253 mTokenMap.put(token, wtoken);
2254 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002255 if (type == TYPE_WALLPAPER) {
2256 mWallpaperTokens.add(wtoken);
2257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 }
2259 }
Romain Guy06882f82009-06-10 13:36:04 -07002260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 public void removeWindowToken(IBinder token) {
2262 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2263 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002264 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 }
2266
2267 final long origId = Binder.clearCallingIdentity();
2268 synchronized(mWindowMap) {
2269 WindowToken wtoken = mTokenMap.remove(token);
2270 mTokenList.remove(wtoken);
2271 if (wtoken != null) {
2272 boolean delayed = false;
2273 if (!wtoken.hidden) {
2274 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 final int N = wtoken.windows.size();
2277 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 for (int i=0; i<N; i++) {
2280 WindowState win = wtoken.windows.get(i);
2281
2282 if (win.isAnimating()) {
2283 delayed = true;
2284 }
Romain Guy06882f82009-06-10 13:36:04 -07002285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 if (win.isVisibleNow()) {
2287 applyAnimationLocked(win,
2288 WindowManagerPolicy.TRANSIT_EXIT, false);
2289 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2290 KeyWaiter.RETURN_NOTHING);
2291 changed = true;
2292 }
2293 }
2294
2295 if (changed) {
2296 mLayoutNeeded = true;
2297 performLayoutAndPlaceSurfacesLocked();
2298 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2299 }
Romain Guy06882f82009-06-10 13:36:04 -07002300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 if (delayed) {
2302 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002303 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2304 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 }
2306 }
Romain Guy06882f82009-06-10 13:36:04 -07002307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 } else {
2309 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2310 }
2311 }
2312 Binder.restoreCallingIdentity(origId);
2313 }
2314
2315 public void addAppToken(int addPos, IApplicationToken token,
2316 int groupId, int requestedOrientation, boolean fullscreen) {
2317 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2318 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002319 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 }
Romain Guy06882f82009-06-10 13:36:04 -07002321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 synchronized(mWindowMap) {
2323 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2324 if (wtoken != null) {
2325 Log.w(TAG, "Attempted to add existing app token: " + token);
2326 return;
2327 }
2328 wtoken = new AppWindowToken(token);
2329 wtoken.groupId = groupId;
2330 wtoken.appFullscreen = fullscreen;
2331 wtoken.requestedOrientation = requestedOrientation;
2332 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002333 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 mTokenMap.put(token.asBinder(), wtoken);
2335 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 // Application tokens start out hidden.
2338 wtoken.hidden = true;
2339 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 //dump();
2342 }
2343 }
Romain Guy06882f82009-06-10 13:36:04 -07002344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 public void setAppGroupId(IBinder token, int groupId) {
2346 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2347 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002348 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 }
2350
2351 synchronized(mWindowMap) {
2352 AppWindowToken wtoken = findAppWindowToken(token);
2353 if (wtoken == null) {
2354 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2355 return;
2356 }
2357 wtoken.groupId = groupId;
2358 }
2359 }
Romain Guy06882f82009-06-10 13:36:04 -07002360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 public int getOrientationFromWindowsLocked() {
2362 int pos = mWindows.size() - 1;
2363 while (pos >= 0) {
2364 WindowState wtoken = (WindowState) mWindows.get(pos);
2365 pos--;
2366 if (wtoken.mAppToken != null) {
2367 // We hit an application window. so the orientation will be determined by the
2368 // app window. No point in continuing further.
2369 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2370 }
2371 if (!wtoken.isVisibleLw()) {
2372 continue;
2373 }
2374 int req = wtoken.mAttrs.screenOrientation;
2375 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2376 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2377 continue;
2378 } else {
2379 return req;
2380 }
2381 }
2382 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2383 }
Romain Guy06882f82009-06-10 13:36:04 -07002384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 public int getOrientationFromAppTokensLocked() {
2386 int pos = mAppTokens.size() - 1;
2387 int curGroup = 0;
2388 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002389 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002391 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 while (pos >= 0) {
2393 AppWindowToken wtoken = mAppTokens.get(pos);
2394 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002395 // if we're about to tear down this window and not seek for
2396 // the behind activity, don't use it for orientation
2397 if (!findingBehind
2398 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002399 continue;
2400 }
2401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 if (!haveGroup) {
2403 // We ignore any hidden applications on the top.
2404 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2405 continue;
2406 }
2407 haveGroup = true;
2408 curGroup = wtoken.groupId;
2409 lastOrientation = wtoken.requestedOrientation;
2410 } else if (curGroup != wtoken.groupId) {
2411 // If we have hit a new application group, and the bottom
2412 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002413 // the orientation behind it, and the last app was
2414 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002416 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2417 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 return lastOrientation;
2419 }
2420 }
2421 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002422 // If this application is fullscreen, and didn't explicitly say
2423 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002425 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002426 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002427 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 return or;
2429 }
2430 // If this application has requested an explicit orientation,
2431 // then use it.
2432 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2433 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2434 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2435 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2436 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2437 return or;
2438 }
Owen Lin3413b892009-05-01 17:12:32 -07002439 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 }
2441 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2442 }
Romain Guy06882f82009-06-10 13:36:04 -07002443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002445 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002446 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2447 "updateOrientationFromAppTokens()")) {
2448 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2449 }
2450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 Configuration config;
2452 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002453 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2454 freezeThisOneIfNeeded);
2455 Binder.restoreCallingIdentity(ident);
2456 return config;
2457 }
2458
2459 Configuration updateOrientationFromAppTokensUnchecked(
2460 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2461 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002463 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 }
2465 if (config != null) {
2466 mLayoutNeeded = true;
2467 performLayoutAndPlaceSurfacesLocked();
2468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 return config;
2470 }
Romain Guy06882f82009-06-10 13:36:04 -07002471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 /*
2473 * The orientation is computed from non-application windows first. If none of
2474 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002475 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2477 * android.os.IBinder)
2478 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002479 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002480 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 long ident = Binder.clearCallingIdentity();
2483 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002484 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 if (req != mForcedAppOrientation) {
2487 changed = true;
2488 mForcedAppOrientation = req;
2489 //send a message to Policy indicating orientation change to take
2490 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002491 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 }
Romain Guy06882f82009-06-10 13:36:04 -07002493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 if (changed) {
2495 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002496 WindowManagerPolicy.USE_LAST_ROTATION,
2497 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 if (changed) {
2499 if (freezeThisOneIfNeeded != null) {
2500 AppWindowToken wtoken = findAppWindowToken(
2501 freezeThisOneIfNeeded);
2502 if (wtoken != null) {
2503 startAppFreezingScreenLocked(wtoken,
2504 ActivityInfo.CONFIG_ORIENTATION);
2505 }
2506 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002507 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 }
2509 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002510
2511 // No obvious action we need to take, but if our current
2512 // state mismatches the activity maanager's, update it
2513 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002514 mTempConfiguration.setToDefaults();
2515 if (computeNewConfigurationLocked(mTempConfiguration)) {
2516 if (appConfig.diff(mTempConfiguration) != 0) {
2517 Log.i(TAG, "Config changed: " + mTempConfiguration);
2518 return new Configuration(mTempConfiguration);
2519 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002520 }
2521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 } finally {
2523 Binder.restoreCallingIdentity(ident);
2524 }
Romain Guy06882f82009-06-10 13:36:04 -07002525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 return null;
2527 }
Romain Guy06882f82009-06-10 13:36:04 -07002528
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002529 int computeForcedAppOrientationLocked() {
2530 int req = getOrientationFromWindowsLocked();
2531 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2532 req = getOrientationFromAppTokensLocked();
2533 }
2534 return req;
2535 }
Romain Guy06882f82009-06-10 13:36:04 -07002536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2538 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2539 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002540 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 }
Romain Guy06882f82009-06-10 13:36:04 -07002542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 synchronized(mWindowMap) {
2544 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2545 if (wtoken == null) {
2546 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2547 return;
2548 }
Romain Guy06882f82009-06-10 13:36:04 -07002549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 wtoken.requestedOrientation = requestedOrientation;
2551 }
2552 }
Romain Guy06882f82009-06-10 13:36:04 -07002553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 public int getAppOrientation(IApplicationToken token) {
2555 synchronized(mWindowMap) {
2556 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2557 if (wtoken == null) {
2558 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2559 }
Romain Guy06882f82009-06-10 13:36:04 -07002560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 return wtoken.requestedOrientation;
2562 }
2563 }
Romain Guy06882f82009-06-10 13:36:04 -07002564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2566 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2567 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002568 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 }
2570
2571 synchronized(mWindowMap) {
2572 boolean changed = false;
2573 if (token == null) {
2574 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2575 changed = mFocusedApp != null;
2576 mFocusedApp = null;
2577 mKeyWaiter.tickle();
2578 } else {
2579 AppWindowToken newFocus = findAppWindowToken(token);
2580 if (newFocus == null) {
2581 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2582 return;
2583 }
2584 changed = mFocusedApp != newFocus;
2585 mFocusedApp = newFocus;
2586 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2587 mKeyWaiter.tickle();
2588 }
2589
2590 if (moveFocusNow && changed) {
2591 final long origId = Binder.clearCallingIdentity();
2592 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2593 Binder.restoreCallingIdentity(origId);
2594 }
2595 }
2596 }
2597
2598 public void prepareAppTransition(int transit) {
2599 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2600 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002601 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 }
Romain Guy06882f82009-06-10 13:36:04 -07002603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 synchronized(mWindowMap) {
2605 if (DEBUG_APP_TRANSITIONS) Log.v(
2606 TAG, "Prepare app transition: transit=" + transit
2607 + " mNextAppTransition=" + mNextAppTransition);
2608 if (!mDisplayFrozen) {
2609 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2610 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002611 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2612 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2613 // Opening a new task always supersedes a close for the anim.
2614 mNextAppTransition = transit;
2615 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2616 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2617 // Opening a new activity always supersedes a close for the anim.
2618 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 }
2620 mAppTransitionReady = false;
2621 mAppTransitionTimeout = false;
2622 mStartingIconInTransition = false;
2623 mSkipAppTransitionAnimation = false;
2624 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2625 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2626 5000);
2627 }
2628 }
2629 }
2630
2631 public int getPendingAppTransition() {
2632 return mNextAppTransition;
2633 }
Romain Guy06882f82009-06-10 13:36:04 -07002634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 public void executeAppTransition() {
2636 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2637 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002638 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 }
Romain Guy06882f82009-06-10 13:36:04 -07002640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 synchronized(mWindowMap) {
2642 if (DEBUG_APP_TRANSITIONS) Log.v(
2643 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2644 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2645 mAppTransitionReady = true;
2646 final long origId = Binder.clearCallingIdentity();
2647 performLayoutAndPlaceSurfacesLocked();
2648 Binder.restoreCallingIdentity(origId);
2649 }
2650 }
2651 }
2652
2653 public void setAppStartingWindow(IBinder token, String pkg,
2654 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2655 IBinder transferFrom, boolean createIfNeeded) {
2656 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2657 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002658 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 }
2660
2661 synchronized(mWindowMap) {
2662 if (DEBUG_STARTING_WINDOW) Log.v(
2663 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2664 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 AppWindowToken wtoken = findAppWindowToken(token);
2667 if (wtoken == null) {
2668 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2669 return;
2670 }
2671
2672 // If the display is frozen, we won't do anything until the
2673 // actual window is displayed so there is no reason to put in
2674 // the starting window.
2675 if (mDisplayFrozen) {
2676 return;
2677 }
Romain Guy06882f82009-06-10 13:36:04 -07002678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 if (wtoken.startingData != null) {
2680 return;
2681 }
Romain Guy06882f82009-06-10 13:36:04 -07002682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 if (transferFrom != null) {
2684 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2685 if (ttoken != null) {
2686 WindowState startingWindow = ttoken.startingWindow;
2687 if (startingWindow != null) {
2688 if (mStartingIconInTransition) {
2689 // In this case, the starting icon has already
2690 // been displayed, so start letting windows get
2691 // shown immediately without any more transitions.
2692 mSkipAppTransitionAnimation = true;
2693 }
2694 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2695 "Moving existing starting from " + ttoken
2696 + " to " + wtoken);
2697 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 // Transfer the starting window over to the new
2700 // token.
2701 wtoken.startingData = ttoken.startingData;
2702 wtoken.startingView = ttoken.startingView;
2703 wtoken.startingWindow = startingWindow;
2704 ttoken.startingData = null;
2705 ttoken.startingView = null;
2706 ttoken.startingWindow = null;
2707 ttoken.startingMoved = true;
2708 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002709 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 startingWindow.mAppToken = wtoken;
2711 mWindows.remove(startingWindow);
2712 ttoken.windows.remove(startingWindow);
2713 ttoken.allAppWindows.remove(startingWindow);
2714 addWindowToListInOrderLocked(startingWindow, true);
2715 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 // Propagate other interesting state between the
2718 // tokens. If the old token is displayed, we should
2719 // immediately force the new one to be displayed. If
2720 // it is animating, we need to move that animation to
2721 // the new one.
2722 if (ttoken.allDrawn) {
2723 wtoken.allDrawn = true;
2724 }
2725 if (ttoken.firstWindowDrawn) {
2726 wtoken.firstWindowDrawn = true;
2727 }
2728 if (!ttoken.hidden) {
2729 wtoken.hidden = false;
2730 wtoken.hiddenRequested = false;
2731 wtoken.willBeHidden = false;
2732 }
2733 if (wtoken.clientHidden != ttoken.clientHidden) {
2734 wtoken.clientHidden = ttoken.clientHidden;
2735 wtoken.sendAppVisibilityToClients();
2736 }
2737 if (ttoken.animation != null) {
2738 wtoken.animation = ttoken.animation;
2739 wtoken.animating = ttoken.animating;
2740 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2741 ttoken.animation = null;
2742 ttoken.animLayerAdjustment = 0;
2743 wtoken.updateLayers();
2744 ttoken.updateLayers();
2745 }
Romain Guy06882f82009-06-10 13:36:04 -07002746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 mLayoutNeeded = true;
2749 performLayoutAndPlaceSurfacesLocked();
2750 Binder.restoreCallingIdentity(origId);
2751 return;
2752 } else if (ttoken.startingData != null) {
2753 // The previous app was getting ready to show a
2754 // starting window, but hasn't yet done so. Steal it!
2755 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2756 "Moving pending starting from " + ttoken
2757 + " to " + wtoken);
2758 wtoken.startingData = ttoken.startingData;
2759 ttoken.startingData = null;
2760 ttoken.startingMoved = true;
2761 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2762 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2763 // want to process the message ASAP, before any other queued
2764 // messages.
2765 mH.sendMessageAtFrontOfQueue(m);
2766 return;
2767 }
2768 }
2769 }
2770
2771 // There is no existing starting window, and the caller doesn't
2772 // want us to create one, so that's it!
2773 if (!createIfNeeded) {
2774 return;
2775 }
Romain Guy06882f82009-06-10 13:36:04 -07002776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 mStartingIconInTransition = true;
2778 wtoken.startingData = new StartingData(
2779 pkg, theme, nonLocalizedLabel,
2780 labelRes, icon);
2781 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2782 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2783 // want to process the message ASAP, before any other queued
2784 // messages.
2785 mH.sendMessageAtFrontOfQueue(m);
2786 }
2787 }
2788
2789 public void setAppWillBeHidden(IBinder token) {
2790 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2791 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002792 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 }
2794
2795 AppWindowToken wtoken;
2796
2797 synchronized(mWindowMap) {
2798 wtoken = findAppWindowToken(token);
2799 if (wtoken == null) {
2800 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2801 return;
2802 }
2803 wtoken.willBeHidden = true;
2804 }
2805 }
Romain Guy06882f82009-06-10 13:36:04 -07002806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2808 boolean visible, int transit, boolean performLayout) {
2809 boolean delayed = false;
2810
2811 if (wtoken.clientHidden == visible) {
2812 wtoken.clientHidden = !visible;
2813 wtoken.sendAppVisibilityToClients();
2814 }
Romain Guy06882f82009-06-10 13:36:04 -07002815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 wtoken.willBeHidden = false;
2817 if (wtoken.hidden == visible) {
2818 final int N = wtoken.allAppWindows.size();
2819 boolean changed = false;
2820 if (DEBUG_APP_TRANSITIONS) Log.v(
2821 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2822 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2827 if (wtoken.animation == sDummyAnimation) {
2828 wtoken.animation = null;
2829 }
2830 applyAnimationLocked(wtoken, lp, transit, visible);
2831 changed = true;
2832 if (wtoken.animation != null) {
2833 delayed = runningAppAnimation = true;
2834 }
2835 }
Romain Guy06882f82009-06-10 13:36:04 -07002836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 for (int i=0; i<N; i++) {
2838 WindowState win = wtoken.allAppWindows.get(i);
2839 if (win == wtoken.startingWindow) {
2840 continue;
2841 }
2842
2843 if (win.isAnimating()) {
2844 delayed = true;
2845 }
Romain Guy06882f82009-06-10 13:36:04 -07002846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2848 //win.dump(" ");
2849 if (visible) {
2850 if (!win.isVisibleNow()) {
2851 if (!runningAppAnimation) {
2852 applyAnimationLocked(win,
2853 WindowManagerPolicy.TRANSIT_ENTER, true);
2854 }
2855 changed = true;
2856 }
2857 } else if (win.isVisibleNow()) {
2858 if (!runningAppAnimation) {
2859 applyAnimationLocked(win,
2860 WindowManagerPolicy.TRANSIT_EXIT, false);
2861 }
2862 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2863 KeyWaiter.RETURN_NOTHING);
2864 changed = true;
2865 }
2866 }
2867
2868 wtoken.hidden = wtoken.hiddenRequested = !visible;
2869 if (!visible) {
2870 unsetAppFreezingScreenLocked(wtoken, true, true);
2871 } else {
2872 // If we are being set visible, and the starting window is
2873 // not yet displayed, then make sure it doesn't get displayed.
2874 WindowState swin = wtoken.startingWindow;
2875 if (swin != null && (swin.mDrawPending
2876 || swin.mCommitDrawPending)) {
2877 swin.mPolicyVisibility = false;
2878 swin.mPolicyVisibilityAfterAnim = false;
2879 }
2880 }
Romain Guy06882f82009-06-10 13:36:04 -07002881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2883 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2884 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 if (changed && performLayout) {
2887 mLayoutNeeded = true;
2888 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 performLayoutAndPlaceSurfacesLocked();
2890 }
2891 }
2892
2893 if (wtoken.animation != null) {
2894 delayed = true;
2895 }
Romain Guy06882f82009-06-10 13:36:04 -07002896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 return delayed;
2898 }
2899
2900 public void setAppVisibility(IBinder token, boolean visible) {
2901 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2902 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002903 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 }
2905
2906 AppWindowToken wtoken;
2907
2908 synchronized(mWindowMap) {
2909 wtoken = findAppWindowToken(token);
2910 if (wtoken == null) {
2911 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2912 return;
2913 }
2914
2915 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2916 RuntimeException e = new RuntimeException();
2917 e.fillInStackTrace();
2918 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2919 + "): mNextAppTransition=" + mNextAppTransition
2920 + " hidden=" + wtoken.hidden
2921 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2922 }
Romain Guy06882f82009-06-10 13:36:04 -07002923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 // If we are preparing an app transition, then delay changing
2925 // the visibility of this token until we execute that transition.
2926 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2927 // Already in requested state, don't do anything more.
2928 if (wtoken.hiddenRequested != visible) {
2929 return;
2930 }
2931 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 if (DEBUG_APP_TRANSITIONS) Log.v(
2934 TAG, "Setting dummy animation on: " + wtoken);
2935 wtoken.setDummyAnimation();
2936 mOpeningApps.remove(wtoken);
2937 mClosingApps.remove(wtoken);
2938 wtoken.inPendingTransaction = true;
2939 if (visible) {
2940 mOpeningApps.add(wtoken);
2941 wtoken.allDrawn = false;
2942 wtoken.startingDisplayed = false;
2943 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 if (wtoken.clientHidden) {
2946 // In the case where we are making an app visible
2947 // but holding off for a transition, we still need
2948 // to tell the client to make its windows visible so
2949 // they get drawn. Otherwise, we will wait on
2950 // performing the transition until all windows have
2951 // been drawn, they never will be, and we are sad.
2952 wtoken.clientHidden = false;
2953 wtoken.sendAppVisibilityToClients();
2954 }
2955 } else {
2956 mClosingApps.add(wtoken);
2957 }
2958 return;
2959 }
Romain Guy06882f82009-06-10 13:36:04 -07002960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 final long origId = Binder.clearCallingIdentity();
2962 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2963 wtoken.updateReportedVisibilityLocked();
2964 Binder.restoreCallingIdentity(origId);
2965 }
2966 }
2967
2968 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2969 boolean unfreezeSurfaceNow, boolean force) {
2970 if (wtoken.freezingScreen) {
2971 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2972 + " force=" + force);
2973 final int N = wtoken.allAppWindows.size();
2974 boolean unfrozeWindows = false;
2975 for (int i=0; i<N; i++) {
2976 WindowState w = wtoken.allAppWindows.get(i);
2977 if (w.mAppFreezing) {
2978 w.mAppFreezing = false;
2979 if (w.mSurface != null && !w.mOrientationChanging) {
2980 w.mOrientationChanging = true;
2981 }
2982 unfrozeWindows = true;
2983 }
2984 }
2985 if (force || unfrozeWindows) {
2986 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2987 wtoken.freezingScreen = false;
2988 mAppsFreezingScreen--;
2989 }
2990 if (unfreezeSurfaceNow) {
2991 if (unfrozeWindows) {
2992 mLayoutNeeded = true;
2993 performLayoutAndPlaceSurfacesLocked();
2994 }
2995 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2996 stopFreezingDisplayLocked();
2997 }
2998 }
2999 }
3000 }
Romain Guy06882f82009-06-10 13:36:04 -07003001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3003 int configChanges) {
3004 if (DEBUG_ORIENTATION) {
3005 RuntimeException e = new RuntimeException();
3006 e.fillInStackTrace();
3007 Log.i(TAG, "Set freezing of " + wtoken.appToken
3008 + ": hidden=" + wtoken.hidden + " freezing="
3009 + wtoken.freezingScreen, e);
3010 }
3011 if (!wtoken.hiddenRequested) {
3012 if (!wtoken.freezingScreen) {
3013 wtoken.freezingScreen = true;
3014 mAppsFreezingScreen++;
3015 if (mAppsFreezingScreen == 1) {
3016 startFreezingDisplayLocked();
3017 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3018 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3019 5000);
3020 }
3021 }
3022 final int N = wtoken.allAppWindows.size();
3023 for (int i=0; i<N; i++) {
3024 WindowState w = wtoken.allAppWindows.get(i);
3025 w.mAppFreezing = true;
3026 }
3027 }
3028 }
Romain Guy06882f82009-06-10 13:36:04 -07003029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 public void startAppFreezingScreen(IBinder token, int configChanges) {
3031 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3032 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003033 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 }
3035
3036 synchronized(mWindowMap) {
3037 if (configChanges == 0 && !mDisplayFrozen) {
3038 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3039 return;
3040 }
Romain Guy06882f82009-06-10 13:36:04 -07003041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 AppWindowToken wtoken = findAppWindowToken(token);
3043 if (wtoken == null || wtoken.appToken == null) {
3044 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3045 return;
3046 }
3047 final long origId = Binder.clearCallingIdentity();
3048 startAppFreezingScreenLocked(wtoken, configChanges);
3049 Binder.restoreCallingIdentity(origId);
3050 }
3051 }
Romain Guy06882f82009-06-10 13:36:04 -07003052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 public void stopAppFreezingScreen(IBinder token, boolean force) {
3054 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3055 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003056 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 }
3058
3059 synchronized(mWindowMap) {
3060 AppWindowToken wtoken = findAppWindowToken(token);
3061 if (wtoken == null || wtoken.appToken == null) {
3062 return;
3063 }
3064 final long origId = Binder.clearCallingIdentity();
3065 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3066 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3067 unsetAppFreezingScreenLocked(wtoken, true, force);
3068 Binder.restoreCallingIdentity(origId);
3069 }
3070 }
Romain Guy06882f82009-06-10 13:36:04 -07003071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 public void removeAppToken(IBinder token) {
3073 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3074 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003075 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 }
3077
3078 AppWindowToken wtoken = null;
3079 AppWindowToken startingToken = null;
3080 boolean delayed = false;
3081
3082 final long origId = Binder.clearCallingIdentity();
3083 synchronized(mWindowMap) {
3084 WindowToken basewtoken = mTokenMap.remove(token);
3085 mTokenList.remove(basewtoken);
3086 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3087 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3088 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3089 wtoken.inPendingTransaction = false;
3090 mOpeningApps.remove(wtoken);
3091 if (mClosingApps.contains(wtoken)) {
3092 delayed = true;
3093 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3094 mClosingApps.add(wtoken);
3095 delayed = true;
3096 }
3097 if (DEBUG_APP_TRANSITIONS) Log.v(
3098 TAG, "Removing app " + wtoken + " delayed=" + delayed
3099 + " animation=" + wtoken.animation
3100 + " animating=" + wtoken.animating);
3101 if (delayed) {
3102 // set the token aside because it has an active animation to be finished
3103 mExitingAppTokens.add(wtoken);
3104 }
3105 mAppTokens.remove(wtoken);
3106 wtoken.removed = true;
3107 if (wtoken.startingData != null) {
3108 startingToken = wtoken;
3109 }
3110 unsetAppFreezingScreenLocked(wtoken, true, true);
3111 if (mFocusedApp == wtoken) {
3112 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3113 mFocusedApp = null;
3114 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3115 mKeyWaiter.tickle();
3116 }
3117 } else {
3118 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3119 }
Romain Guy06882f82009-06-10 13:36:04 -07003120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 if (!delayed && wtoken != null) {
3122 wtoken.updateReportedVisibilityLocked();
3123 }
3124 }
3125 Binder.restoreCallingIdentity(origId);
3126
3127 if (startingToken != null) {
3128 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3129 + startingToken + ": app token removed");
3130 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3131 mH.sendMessage(m);
3132 }
3133 }
3134
3135 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3136 final int NW = token.windows.size();
3137 for (int i=0; i<NW; i++) {
3138 WindowState win = token.windows.get(i);
3139 mWindows.remove(win);
3140 int j = win.mChildWindows.size();
3141 while (j > 0) {
3142 j--;
3143 mWindows.remove(win.mChildWindows.get(j));
3144 }
3145 }
3146 return NW > 0;
3147 }
3148
3149 void dumpAppTokensLocked() {
3150 for (int i=mAppTokens.size()-1; i>=0; i--) {
3151 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3152 }
3153 }
Romain Guy06882f82009-06-10 13:36:04 -07003154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 void dumpWindowsLocked() {
3156 for (int i=mWindows.size()-1; i>=0; i--) {
3157 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3158 }
3159 }
Romain Guy06882f82009-06-10 13:36:04 -07003160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 private int findWindowOffsetLocked(int tokenPos) {
3162 final int NW = mWindows.size();
3163
3164 if (tokenPos >= mAppTokens.size()) {
3165 int i = NW;
3166 while (i > 0) {
3167 i--;
3168 WindowState win = (WindowState)mWindows.get(i);
3169 if (win.getAppToken() != null) {
3170 return i+1;
3171 }
3172 }
3173 }
3174
3175 while (tokenPos > 0) {
3176 // Find the first app token below the new position that has
3177 // a window displayed.
3178 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3179 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3180 + tokenPos + " -- " + wtoken.token);
3181 int i = wtoken.windows.size();
3182 while (i > 0) {
3183 i--;
3184 WindowState win = wtoken.windows.get(i);
3185 int j = win.mChildWindows.size();
3186 while (j > 0) {
3187 j--;
3188 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3189 if (cwin.mSubLayer >= 0 ) {
3190 for (int pos=NW-1; pos>=0; pos--) {
3191 if (mWindows.get(pos) == cwin) {
3192 if (DEBUG_REORDER) Log.v(TAG,
3193 "Found child win @" + (pos+1));
3194 return pos+1;
3195 }
3196 }
3197 }
3198 }
3199 for (int pos=NW-1; pos>=0; pos--) {
3200 if (mWindows.get(pos) == win) {
3201 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3202 return pos+1;
3203 }
3204 }
3205 }
3206 tokenPos--;
3207 }
3208
3209 return 0;
3210 }
3211
3212 private final int reAddWindowLocked(int index, WindowState win) {
3213 final int NCW = win.mChildWindows.size();
3214 boolean added = false;
3215 for (int j=0; j<NCW; j++) {
3216 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3217 if (!added && cwin.mSubLayer >= 0) {
3218 mWindows.add(index, win);
3219 index++;
3220 added = true;
3221 }
3222 mWindows.add(index, cwin);
3223 index++;
3224 }
3225 if (!added) {
3226 mWindows.add(index, win);
3227 index++;
3228 }
3229 return index;
3230 }
Romain Guy06882f82009-06-10 13:36:04 -07003231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3233 final int NW = token.windows.size();
3234 for (int i=0; i<NW; i++) {
3235 index = reAddWindowLocked(index, token.windows.get(i));
3236 }
3237 return index;
3238 }
3239
3240 public void moveAppToken(int index, IBinder token) {
3241 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3242 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003243 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 }
3245
3246 synchronized(mWindowMap) {
3247 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3248 if (DEBUG_REORDER) dumpAppTokensLocked();
3249 final AppWindowToken wtoken = findAppWindowToken(token);
3250 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3251 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3252 + token + " (" + wtoken + ")");
3253 return;
3254 }
3255 mAppTokens.add(index, wtoken);
3256 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3257 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 final long origId = Binder.clearCallingIdentity();
3260 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3261 if (DEBUG_REORDER) dumpWindowsLocked();
3262 if (tmpRemoveAppWindowsLocked(wtoken)) {
3263 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3264 if (DEBUG_REORDER) dumpWindowsLocked();
3265 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3266 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3267 if (DEBUG_REORDER) dumpWindowsLocked();
3268 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 mLayoutNeeded = true;
3270 performLayoutAndPlaceSurfacesLocked();
3271 }
3272 Binder.restoreCallingIdentity(origId);
3273 }
3274 }
3275
3276 private void removeAppTokensLocked(List<IBinder> tokens) {
3277 // XXX This should be done more efficiently!
3278 // (take advantage of the fact that both lists should be
3279 // ordered in the same way.)
3280 int N = tokens.size();
3281 for (int i=0; i<N; i++) {
3282 IBinder token = tokens.get(i);
3283 final AppWindowToken wtoken = findAppWindowToken(token);
3284 if (!mAppTokens.remove(wtoken)) {
3285 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3286 + token + " (" + wtoken + ")");
3287 i--;
3288 N--;
3289 }
3290 }
3291 }
3292
3293 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3294 // First remove all of the windows from the list.
3295 final int N = tokens.size();
3296 int i;
3297 for (i=0; i<N; i++) {
3298 WindowToken token = mTokenMap.get(tokens.get(i));
3299 if (token != null) {
3300 tmpRemoveAppWindowsLocked(token);
3301 }
3302 }
3303
3304 // Where to start adding?
3305 int pos = findWindowOffsetLocked(tokenPos);
3306
3307 // And now add them back at the correct place.
3308 for (i=0; i<N; i++) {
3309 WindowToken token = mTokenMap.get(tokens.get(i));
3310 if (token != null) {
3311 pos = reAddAppWindowsLocked(pos, token);
3312 }
3313 }
3314
3315 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 mLayoutNeeded = true;
3317 performLayoutAndPlaceSurfacesLocked();
3318
3319 //dump();
3320 }
3321
3322 public void moveAppTokensToTop(List<IBinder> tokens) {
3323 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3324 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003325 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 }
3327
3328 final long origId = Binder.clearCallingIdentity();
3329 synchronized(mWindowMap) {
3330 removeAppTokensLocked(tokens);
3331 final int N = tokens.size();
3332 for (int i=0; i<N; i++) {
3333 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3334 if (wt != null) {
3335 mAppTokens.add(wt);
3336 }
3337 }
3338 moveAppWindowsLocked(tokens, mAppTokens.size());
3339 }
3340 Binder.restoreCallingIdentity(origId);
3341 }
3342
3343 public void moveAppTokensToBottom(List<IBinder> tokens) {
3344 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3345 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003346 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 }
3348
3349 final long origId = Binder.clearCallingIdentity();
3350 synchronized(mWindowMap) {
3351 removeAppTokensLocked(tokens);
3352 final int N = tokens.size();
3353 int pos = 0;
3354 for (int i=0; i<N; i++) {
3355 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3356 if (wt != null) {
3357 mAppTokens.add(pos, wt);
3358 pos++;
3359 }
3360 }
3361 moveAppWindowsLocked(tokens, 0);
3362 }
3363 Binder.restoreCallingIdentity(origId);
3364 }
3365
3366 // -------------------------------------------------------------
3367 // Misc IWindowSession methods
3368 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 public void disableKeyguard(IBinder token, String tag) {
3371 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3372 != PackageManager.PERMISSION_GRANTED) {
3373 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3374 }
3375 mKeyguardDisabled.acquire(token, tag);
3376 }
3377
3378 public void reenableKeyguard(IBinder token) {
3379 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3380 != PackageManager.PERMISSION_GRANTED) {
3381 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3382 }
3383 synchronized (mKeyguardDisabled) {
3384 mKeyguardDisabled.release(token);
3385
3386 if (!mKeyguardDisabled.isAcquired()) {
3387 // if we are the last one to reenable the keyguard wait until
3388 // we have actaully finished reenabling until returning
3389 mWaitingUntilKeyguardReenabled = true;
3390 while (mWaitingUntilKeyguardReenabled) {
3391 try {
3392 mKeyguardDisabled.wait();
3393 } catch (InterruptedException e) {
3394 Thread.currentThread().interrupt();
3395 }
3396 }
3397 }
3398 }
3399 }
3400
3401 /**
3402 * @see android.app.KeyguardManager#exitKeyguardSecurely
3403 */
3404 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3405 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3406 != PackageManager.PERMISSION_GRANTED) {
3407 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3408 }
3409 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3410 public void onKeyguardExitResult(boolean success) {
3411 try {
3412 callback.onKeyguardExitResult(success);
3413 } catch (RemoteException e) {
3414 // Client has died, we don't care.
3415 }
3416 }
3417 });
3418 }
3419
3420 public boolean inKeyguardRestrictedInputMode() {
3421 return mPolicy.inKeyguardRestrictedKeyInputMode();
3422 }
Romain Guy06882f82009-06-10 13:36:04 -07003423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 static float fixScale(float scale) {
3425 if (scale < 0) scale = 0;
3426 else if (scale > 20) scale = 20;
3427 return Math.abs(scale);
3428 }
Romain Guy06882f82009-06-10 13:36:04 -07003429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 public void setAnimationScale(int which, float scale) {
3431 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3432 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003433 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 }
3435
3436 if (scale < 0) scale = 0;
3437 else if (scale > 20) scale = 20;
3438 scale = Math.abs(scale);
3439 switch (which) {
3440 case 0: mWindowAnimationScale = fixScale(scale); break;
3441 case 1: mTransitionAnimationScale = fixScale(scale); break;
3442 }
Romain Guy06882f82009-06-10 13:36:04 -07003443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 // Persist setting
3445 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3446 }
Romain Guy06882f82009-06-10 13:36:04 -07003447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 public void setAnimationScales(float[] scales) {
3449 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3450 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003451 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 }
3453
3454 if (scales != null) {
3455 if (scales.length >= 1) {
3456 mWindowAnimationScale = fixScale(scales[0]);
3457 }
3458 if (scales.length >= 2) {
3459 mTransitionAnimationScale = fixScale(scales[1]);
3460 }
3461 }
Romain Guy06882f82009-06-10 13:36:04 -07003462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 // Persist setting
3464 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3465 }
Romain Guy06882f82009-06-10 13:36:04 -07003466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 public float getAnimationScale(int which) {
3468 switch (which) {
3469 case 0: return mWindowAnimationScale;
3470 case 1: return mTransitionAnimationScale;
3471 }
3472 return 0;
3473 }
Romain Guy06882f82009-06-10 13:36:04 -07003474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 public float[] getAnimationScales() {
3476 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3477 }
Romain Guy06882f82009-06-10 13:36:04 -07003478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 public int getSwitchState(int sw) {
3480 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3481 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003482 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 }
3484 return KeyInputQueue.getSwitchState(sw);
3485 }
Romain Guy06882f82009-06-10 13:36:04 -07003486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 public int getSwitchStateForDevice(int devid, int sw) {
3488 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3489 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003490 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 }
3492 return KeyInputQueue.getSwitchState(devid, sw);
3493 }
Romain Guy06882f82009-06-10 13:36:04 -07003494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 public int getScancodeState(int sw) {
3496 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3497 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003498 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 }
3500 return KeyInputQueue.getScancodeState(sw);
3501 }
Romain Guy06882f82009-06-10 13:36:04 -07003502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 public int getScancodeStateForDevice(int devid, int sw) {
3504 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3505 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003506 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 }
3508 return KeyInputQueue.getScancodeState(devid, sw);
3509 }
Romain Guy06882f82009-06-10 13:36:04 -07003510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 public int getKeycodeState(int sw) {
3512 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3513 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003514 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 }
3516 return KeyInputQueue.getKeycodeState(sw);
3517 }
Romain Guy06882f82009-06-10 13:36:04 -07003518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 public int getKeycodeStateForDevice(int devid, int sw) {
3520 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3521 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003522 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 }
3524 return KeyInputQueue.getKeycodeState(devid, sw);
3525 }
Romain Guy06882f82009-06-10 13:36:04 -07003526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3528 return KeyInputQueue.hasKeys(keycodes, keyExists);
3529 }
Romain Guy06882f82009-06-10 13:36:04 -07003530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 public void enableScreenAfterBoot() {
3532 synchronized(mWindowMap) {
3533 if (mSystemBooted) {
3534 return;
3535 }
3536 mSystemBooted = true;
3537 }
Romain Guy06882f82009-06-10 13:36:04 -07003538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 performEnableScreen();
3540 }
Romain Guy06882f82009-06-10 13:36:04 -07003541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 public void enableScreenIfNeededLocked() {
3543 if (mDisplayEnabled) {
3544 return;
3545 }
3546 if (!mSystemBooted) {
3547 return;
3548 }
3549 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3550 }
Romain Guy06882f82009-06-10 13:36:04 -07003551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 public void performEnableScreen() {
3553 synchronized(mWindowMap) {
3554 if (mDisplayEnabled) {
3555 return;
3556 }
3557 if (!mSystemBooted) {
3558 return;
3559 }
Romain Guy06882f82009-06-10 13:36:04 -07003560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 // Don't enable the screen until all existing windows
3562 // have been drawn.
3563 final int N = mWindows.size();
3564 for (int i=0; i<N; i++) {
3565 WindowState w = (WindowState)mWindows.get(i);
3566 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3567 return;
3568 }
3569 }
Romain Guy06882f82009-06-10 13:36:04 -07003570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 mDisplayEnabled = true;
3572 if (false) {
3573 Log.i(TAG, "ENABLING SCREEN!");
3574 StringWriter sw = new StringWriter();
3575 PrintWriter pw = new PrintWriter(sw);
3576 this.dump(null, pw, null);
3577 Log.i(TAG, sw.toString());
3578 }
3579 try {
3580 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3581 if (surfaceFlinger != null) {
3582 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3583 Parcel data = Parcel.obtain();
3584 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3585 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3586 data, null, 0);
3587 data.recycle();
3588 }
3589 } catch (RemoteException ex) {
3590 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3591 }
3592 }
Romain Guy06882f82009-06-10 13:36:04 -07003593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003597 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3598 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 }
Romain Guy06882f82009-06-10 13:36:04 -07003600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 public void setInTouchMode(boolean mode) {
3602 synchronized(mWindowMap) {
3603 mInTouchMode = mode;
3604 }
3605 }
3606
Romain Guy06882f82009-06-10 13:36:04 -07003607 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003608 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003610 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003611 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 }
3613
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003614 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 }
Romain Guy06882f82009-06-10 13:36:04 -07003616
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003617 public void setRotationUnchecked(int rotation,
3618 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 if(DEBUG_ORIENTATION) Log.v(TAG,
3620 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 long origId = Binder.clearCallingIdentity();
3623 boolean changed;
3624 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003625 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 }
Romain Guy06882f82009-06-10 13:36:04 -07003627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 if (changed) {
3629 sendNewConfiguration();
3630 synchronized(mWindowMap) {
3631 mLayoutNeeded = true;
3632 performLayoutAndPlaceSurfacesLocked();
3633 }
3634 } else if (alwaysSendConfiguration) {
3635 //update configuration ignoring orientation change
3636 sendNewConfiguration();
3637 }
Romain Guy06882f82009-06-10 13:36:04 -07003638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 Binder.restoreCallingIdentity(origId);
3640 }
Romain Guy06882f82009-06-10 13:36:04 -07003641
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003642 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003643 boolean changed;
3644 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3645 rotation = mRequestedRotation;
3646 } else {
3647 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003648 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 }
3650 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003651 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 mRotation, mDisplayEnabled);
3653 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3654 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003657 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 "Rotation changed to " + rotation
3659 + " from " + mRotation
3660 + " (forceApp=" + mForcedAppOrientation
3661 + ", req=" + mRequestedRotation + ")");
3662 mRotation = rotation;
3663 mWindowsFreezingScreen = true;
3664 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3665 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3666 2000);
3667 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003668 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 mQueue.setOrientation(rotation);
3670 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003671 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 }
3673 for (int i=mWindows.size()-1; i>=0; i--) {
3674 WindowState w = (WindowState)mWindows.get(i);
3675 if (w.mSurface != null) {
3676 w.mOrientationChanging = true;
3677 }
3678 }
3679 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3680 try {
3681 mRotationWatchers.get(i).onRotationChanged(rotation);
3682 } catch (RemoteException e) {
3683 }
3684 }
3685 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 return changed;
3688 }
Romain Guy06882f82009-06-10 13:36:04 -07003689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 public int getRotation() {
3691 return mRotation;
3692 }
3693
3694 public int watchRotation(IRotationWatcher watcher) {
3695 final IBinder watcherBinder = watcher.asBinder();
3696 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3697 public void binderDied() {
3698 synchronized (mWindowMap) {
3699 for (int i=0; i<mRotationWatchers.size(); i++) {
3700 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003701 IRotationWatcher removed = mRotationWatchers.remove(i);
3702 if (removed != null) {
3703 removed.asBinder().unlinkToDeath(this, 0);
3704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 i--;
3706 }
3707 }
3708 }
3709 }
3710 };
Romain Guy06882f82009-06-10 13:36:04 -07003711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 synchronized (mWindowMap) {
3713 try {
3714 watcher.asBinder().linkToDeath(dr, 0);
3715 mRotationWatchers.add(watcher);
3716 } catch (RemoteException e) {
3717 // Client died, no cleanup needed.
3718 }
Romain Guy06882f82009-06-10 13:36:04 -07003719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 return mRotation;
3721 }
3722 }
3723
3724 /**
3725 * Starts the view server on the specified port.
3726 *
3727 * @param port The port to listener to.
3728 *
3729 * @return True if the server was successfully started, false otherwise.
3730 *
3731 * @see com.android.server.ViewServer
3732 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3733 */
3734 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003735 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 return false;
3737 }
3738
3739 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3740 return false;
3741 }
3742
3743 if (port < 1024) {
3744 return false;
3745 }
3746
3747 if (mViewServer != null) {
3748 if (!mViewServer.isRunning()) {
3749 try {
3750 return mViewServer.start();
3751 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003752 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 }
3754 }
3755 return false;
3756 }
3757
3758 try {
3759 mViewServer = new ViewServer(this, port);
3760 return mViewServer.start();
3761 } catch (IOException e) {
3762 Log.w(TAG, "View server did not start");
3763 }
3764 return false;
3765 }
3766
Romain Guy06882f82009-06-10 13:36:04 -07003767 private boolean isSystemSecure() {
3768 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3769 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3770 }
3771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003772 /**
3773 * Stops the view server if it exists.
3774 *
3775 * @return True if the server stopped, false if it wasn't started or
3776 * couldn't be stopped.
3777 *
3778 * @see com.android.server.ViewServer
3779 */
3780 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003781 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 return false;
3783 }
3784
3785 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3786 return false;
3787 }
3788
3789 if (mViewServer != null) {
3790 return mViewServer.stop();
3791 }
3792 return false;
3793 }
3794
3795 /**
3796 * Indicates whether the view server is running.
3797 *
3798 * @return True if the server is running, false otherwise.
3799 *
3800 * @see com.android.server.ViewServer
3801 */
3802 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003803 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 return false;
3805 }
3806
3807 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3808 return false;
3809 }
3810
3811 return mViewServer != null && mViewServer.isRunning();
3812 }
3813
3814 /**
3815 * Lists all availble windows in the system. The listing is written in the
3816 * specified Socket's output stream with the following syntax:
3817 * windowHashCodeInHexadecimal windowName
3818 * Each line of the ouput represents a different window.
3819 *
3820 * @param client The remote client to send the listing to.
3821 * @return False if an error occured, true otherwise.
3822 */
3823 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003824 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 return false;
3826 }
3827
3828 boolean result = true;
3829
3830 Object[] windows;
3831 synchronized (mWindowMap) {
3832 windows = new Object[mWindows.size()];
3833 //noinspection unchecked
3834 windows = mWindows.toArray(windows);
3835 }
3836
3837 BufferedWriter out = null;
3838
3839 // Any uncaught exception will crash the system process
3840 try {
3841 OutputStream clientStream = client.getOutputStream();
3842 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3843
3844 final int count = windows.length;
3845 for (int i = 0; i < count; i++) {
3846 final WindowState w = (WindowState) windows[i];
3847 out.write(Integer.toHexString(System.identityHashCode(w)));
3848 out.write(' ');
3849 out.append(w.mAttrs.getTitle());
3850 out.write('\n');
3851 }
3852
3853 out.write("DONE.\n");
3854 out.flush();
3855 } catch (Exception e) {
3856 result = false;
3857 } finally {
3858 if (out != null) {
3859 try {
3860 out.close();
3861 } catch (IOException e) {
3862 result = false;
3863 }
3864 }
3865 }
3866
3867 return result;
3868 }
3869
3870 /**
3871 * Sends a command to a target window. The result of the command, if any, will be
3872 * written in the output stream of the specified socket.
3873 *
3874 * The parameters must follow this syntax:
3875 * windowHashcode extra
3876 *
3877 * Where XX is the length in characeters of the windowTitle.
3878 *
3879 * The first parameter is the target window. The window with the specified hashcode
3880 * will be the target. If no target can be found, nothing happens. The extra parameters
3881 * will be delivered to the target window and as parameters to the command itself.
3882 *
3883 * @param client The remote client to sent the result, if any, to.
3884 * @param command The command to execute.
3885 * @param parameters The command parameters.
3886 *
3887 * @return True if the command was successfully delivered, false otherwise. This does
3888 * not indicate whether the command itself was successful.
3889 */
3890 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003891 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 return false;
3893 }
3894
3895 boolean success = true;
3896 Parcel data = null;
3897 Parcel reply = null;
3898
3899 // Any uncaught exception will crash the system process
3900 try {
3901 // Find the hashcode of the window
3902 int index = parameters.indexOf(' ');
3903 if (index == -1) {
3904 index = parameters.length();
3905 }
3906 final String code = parameters.substring(0, index);
3907 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3908
3909 // Extract the command's parameter after the window description
3910 if (index < parameters.length()) {
3911 parameters = parameters.substring(index + 1);
3912 } else {
3913 parameters = "";
3914 }
3915
3916 final WindowManagerService.WindowState window = findWindow(hashCode);
3917 if (window == null) {
3918 return false;
3919 }
3920
3921 data = Parcel.obtain();
3922 data.writeInterfaceToken("android.view.IWindow");
3923 data.writeString(command);
3924 data.writeString(parameters);
3925 data.writeInt(1);
3926 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3927
3928 reply = Parcel.obtain();
3929
3930 final IBinder binder = window.mClient.asBinder();
3931 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3932 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3933
3934 reply.readException();
3935
3936 } catch (Exception e) {
3937 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3938 success = false;
3939 } finally {
3940 if (data != null) {
3941 data.recycle();
3942 }
3943 if (reply != null) {
3944 reply.recycle();
3945 }
3946 }
3947
3948 return success;
3949 }
3950
3951 private WindowState findWindow(int hashCode) {
3952 if (hashCode == -1) {
3953 return getFocusedWindow();
3954 }
3955
3956 synchronized (mWindowMap) {
3957 final ArrayList windows = mWindows;
3958 final int count = windows.size();
3959
3960 for (int i = 0; i < count; i++) {
3961 WindowState w = (WindowState) windows.get(i);
3962 if (System.identityHashCode(w) == hashCode) {
3963 return w;
3964 }
3965 }
3966 }
3967
3968 return null;
3969 }
3970
3971 /*
3972 * Instruct the Activity Manager to fetch the current configuration and broadcast
3973 * that to config-changed listeners if appropriate.
3974 */
3975 void sendNewConfiguration() {
3976 try {
3977 mActivityManager.updateConfiguration(null);
3978 } catch (RemoteException e) {
3979 }
3980 }
Romain Guy06882f82009-06-10 13:36:04 -07003981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 public Configuration computeNewConfiguration() {
3983 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003984 return computeNewConfigurationLocked();
3985 }
3986 }
Romain Guy06882f82009-06-10 13:36:04 -07003987
Dianne Hackbornc485a602009-03-24 22:39:49 -07003988 Configuration computeNewConfigurationLocked() {
3989 Configuration config = new Configuration();
3990 if (!computeNewConfigurationLocked(config)) {
3991 return null;
3992 }
3993 Log.i(TAG, "Config changed: " + config);
3994 long now = SystemClock.uptimeMillis();
3995 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3996 if (mFreezeGcPending != 0) {
3997 if (now > (mFreezeGcPending+1000)) {
3998 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3999 mH.removeMessages(H.FORCE_GC);
4000 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 mFreezeGcPending = now;
4002 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004003 } else {
4004 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004006 return config;
4007 }
Romain Guy06882f82009-06-10 13:36:04 -07004008
Dianne Hackbornc485a602009-03-24 22:39:49 -07004009 boolean computeNewConfigurationLocked(Configuration config) {
4010 if (mDisplay == null) {
4011 return false;
4012 }
4013 mQueue.getInputConfiguration(config);
4014 final int dw = mDisplay.getWidth();
4015 final int dh = mDisplay.getHeight();
4016 int orientation = Configuration.ORIENTATION_SQUARE;
4017 if (dw < dh) {
4018 orientation = Configuration.ORIENTATION_PORTRAIT;
4019 } else if (dw > dh) {
4020 orientation = Configuration.ORIENTATION_LANDSCAPE;
4021 }
4022 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004023
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004024 DisplayMetrics dm = new DisplayMetrics();
4025 mDisplay.getMetrics(dm);
4026 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4027
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004028 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004029 // Note we only do this once because at this point we don't
4030 // expect the screen to change in this way at runtime, and want
4031 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004032 int longSize = dw;
4033 int shortSize = dh;
4034 if (longSize < shortSize) {
4035 int tmp = longSize;
4036 longSize = shortSize;
4037 shortSize = tmp;
4038 }
4039 longSize = (int)(longSize/dm.density);
4040 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004041
Dianne Hackborn723738c2009-06-25 19:48:04 -07004042 // These semi-magic numbers define our compatibility modes for
4043 // applications with different screens. Don't change unless you
4044 // make sure to test lots and lots of apps!
4045 if (longSize < 470) {
4046 // This is shorter than an HVGA normal density screen (which
4047 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004048 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4049 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004050 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004051 // Is this a large screen?
4052 if (longSize > 640 && shortSize >= 480) {
4053 // VGA or larger screens at medium density are the point
4054 // at which we consider it to be a large screen.
4055 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4056 } else {
4057 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4058
4059 // If this screen is wider than normal HVGA, or taller
4060 // than FWVGA, then for old apps we want to run in size
4061 // compatibility mode.
4062 if (shortSize > 321 || longSize > 570) {
4063 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4064 }
4065 }
4066
4067 // Is this a long screen?
4068 if (((longSize*3)/5) >= (shortSize-1)) {
4069 // Anything wider than WVGA (5:3) is considering to be long.
4070 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4071 } else {
4072 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4073 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004074 }
4075 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004076 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004077
Dianne Hackbornc485a602009-03-24 22:39:49 -07004078 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4079 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4080 mPolicy.adjustConfigurationLw(config);
4081 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 }
Romain Guy06882f82009-06-10 13:36:04 -07004083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 // -------------------------------------------------------------
4085 // Input Events and Focus Management
4086 // -------------------------------------------------------------
4087
4088 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004089 long curTime = SystemClock.uptimeMillis();
4090
Michael Chane10de972009-05-18 11:24:50 -07004091 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004092 if (mLastTouchEventType == eventType &&
4093 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4094 return;
4095 }
4096 mLastUserActivityCallTime = curTime;
4097 mLastTouchEventType = eventType;
4098 }
4099
4100 if (targetWin == null
4101 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4102 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 }
4104 }
4105
4106 // tells if it's a cheek event or not -- this function is stateful
4107 private static final int EVENT_NONE = 0;
4108 private static final int EVENT_UNKNOWN = 0;
4109 private static final int EVENT_CHEEK = 0;
4110 private static final int EVENT_IGNORE_DURATION = 300; // ms
4111 private static final float CHEEK_THRESHOLD = 0.6f;
4112 private int mEventState = EVENT_NONE;
4113 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 private int eventType(MotionEvent ev) {
4116 float size = ev.getSize();
4117 switch (ev.getAction()) {
4118 case MotionEvent.ACTION_DOWN:
4119 mEventSize = size;
4120 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4121 case MotionEvent.ACTION_UP:
4122 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004123 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 case MotionEvent.ACTION_MOVE:
4125 final int N = ev.getHistorySize();
4126 if (size > mEventSize) mEventSize = size;
4127 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4128 for (int i=0; i<N; i++) {
4129 size = ev.getHistoricalSize(i);
4130 if (size > mEventSize) mEventSize = size;
4131 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4132 }
4133 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4134 return TOUCH_EVENT;
4135 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004136 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 }
4138 default:
4139 // not good
4140 return OTHER_EVENT;
4141 }
4142 }
4143
4144 /**
4145 * @return Returns true if event was dispatched, false if it was dropped for any reason
4146 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004147 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4149 "dispatchPointer " + ev);
4150
Michael Chan53071d62009-05-13 17:29:48 -07004151 if (MEASURE_LATENCY) {
4152 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4153 }
4154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004156 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157
Michael Chan53071d62009-05-13 17:29:48 -07004158 if (MEASURE_LATENCY) {
4159 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4160 }
4161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 if (action == MotionEvent.ACTION_UP) {
4165 // let go of our target
4166 mKeyWaiter.mMotionTarget = null;
4167 mPowerManager.logPointerUpEvent();
4168 } else if (action == MotionEvent.ACTION_DOWN) {
4169 mPowerManager.logPointerDownEvent();
4170 }
4171
4172 if (targetObj == null) {
4173 // In this case we are either dropping the event, or have received
4174 // a move or up without a down. It is common to receive move
4175 // events in such a way, since this means the user is moving the
4176 // pointer without actually pressing down. All other cases should
4177 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004178 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4180 }
4181 if (qev != null) {
4182 mQueue.recycleEvent(qev);
4183 }
4184 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004185 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 }
4187 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4188 if (qev != null) {
4189 mQueue.recycleEvent(qev);
4190 }
4191 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004192 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 }
Romain Guy06882f82009-06-10 13:36:04 -07004194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004198 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199
4200 //Log.i(TAG, "Sending " + ev + " to " + target);
4201
4202 if (uid != 0 && uid != target.mSession.mUid) {
4203 if (mContext.checkPermission(
4204 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4205 != PackageManager.PERMISSION_GRANTED) {
4206 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4207 + pid + " uid " + uid + " to window " + target
4208 + " owned by uid " + target.mSession.mUid);
4209 if (qev != null) {
4210 mQueue.recycleEvent(qev);
4211 }
4212 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004213 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 }
4215 }
4216
Michael Chan53071d62009-05-13 17:29:48 -07004217 if (MEASURE_LATENCY) {
4218 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4219 }
4220
Romain Guy06882f82009-06-10 13:36:04 -07004221 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4223 //target wants to ignore fat touch events
4224 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4225 //explicit flag to return without processing event further
4226 boolean returnFlag = false;
4227 if((action == MotionEvent.ACTION_DOWN)) {
4228 mFatTouch = false;
4229 if(cheekPress) {
4230 mFatTouch = true;
4231 returnFlag = true;
4232 }
4233 } else {
4234 if(action == MotionEvent.ACTION_UP) {
4235 if(mFatTouch) {
4236 //earlier even was invalid doesnt matter if current up is cheekpress or not
4237 mFatTouch = false;
4238 returnFlag = true;
4239 } else if(cheekPress) {
4240 //cancel the earlier event
4241 ev.setAction(MotionEvent.ACTION_CANCEL);
4242 action = MotionEvent.ACTION_CANCEL;
4243 }
4244 } else if(action == MotionEvent.ACTION_MOVE) {
4245 if(mFatTouch) {
4246 //two cases here
4247 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004248 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 returnFlag = true;
4250 } else if(cheekPress) {
4251 //valid down followed by invalid moves
4252 //an invalid move have to cancel earlier action
4253 ev.setAction(MotionEvent.ACTION_CANCEL);
4254 action = MotionEvent.ACTION_CANCEL;
4255 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4256 //note that the subsequent invalid moves will not get here
4257 mFatTouch = true;
4258 }
4259 }
4260 } //else if action
4261 if(returnFlag) {
4262 //recycle que, ev
4263 if (qev != null) {
4264 mQueue.recycleEvent(qev);
4265 }
4266 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004267 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004268 }
4269 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004270
Michael Chan9f028e62009-08-04 17:37:46 -07004271 // Enable this for testing the "right" value
4272 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004273 int max_events_per_sec = 35;
4274 try {
4275 max_events_per_sec = Integer.parseInt(SystemProperties
4276 .get("windowsmgr.max_events_per_sec"));
4277 if (max_events_per_sec < 1) {
4278 max_events_per_sec = 35;
4279 }
4280 } catch (NumberFormatException e) {
4281 }
4282 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4283 }
4284
4285 /*
4286 * Throttle events to minimize CPU usage when there's a flood of events
4287 * e.g. constant contact with the screen
4288 */
4289 if (action == MotionEvent.ACTION_MOVE) {
4290 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4291 long now = SystemClock.uptimeMillis();
4292 if (now < nextEventTime) {
4293 try {
4294 Thread.sleep(nextEventTime - now);
4295 } catch (InterruptedException e) {
4296 }
4297 mLastTouchEventTime = nextEventTime;
4298 } else {
4299 mLastTouchEventTime = now;
4300 }
4301 }
4302
Michael Chan53071d62009-05-13 17:29:48 -07004303 if (MEASURE_LATENCY) {
4304 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4305 }
4306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 synchronized(mWindowMap) {
4308 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4309 mKeyWaiter.bindTargetWindowLocked(target,
4310 KeyWaiter.RETURN_PENDING_POINTER, qev);
4311 ev = null;
4312 } else {
4313 if (action == MotionEvent.ACTION_DOWN) {
4314 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4315 if (out != null) {
4316 MotionEvent oev = MotionEvent.obtain(ev);
4317 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4318 do {
4319 final Rect frame = out.mFrame;
4320 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4321 try {
4322 out.mClient.dispatchPointer(oev, eventTime);
4323 } catch (android.os.RemoteException e) {
4324 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4325 }
4326 oev.offsetLocation((float)frame.left, (float)frame.top);
4327 out = out.mNextOutsideTouch;
4328 } while (out != null);
4329 mKeyWaiter.mOutsideTouchTargets = null;
4330 }
4331 }
4332 final Rect frame = target.mFrame;
4333 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4334 mKeyWaiter.bindTargetWindowLocked(target);
4335 }
4336 }
Romain Guy06882f82009-06-10 13:36:04 -07004337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 // finally offset the event to the target's coordinate system and
4339 // dispatch the event.
4340 try {
4341 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4342 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4343 }
Michael Chan53071d62009-05-13 17:29:48 -07004344
4345 if (MEASURE_LATENCY) {
4346 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4347 }
4348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 target.mClient.dispatchPointer(ev, eventTime);
Michael Chan53071d62009-05-13 17:29:48 -07004350
4351 if (MEASURE_LATENCY) {
4352 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4353 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004354 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 } catch (android.os.RemoteException e) {
4356 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4357 mKeyWaiter.mMotionTarget = null;
4358 try {
4359 removeWindow(target.mSession, target.mClient);
4360 } catch (java.util.NoSuchElementException ex) {
4361 // This will happen if the window has already been
4362 // removed.
4363 }
4364 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004365 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 }
Romain Guy06882f82009-06-10 13:36:04 -07004367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 /**
4369 * @return Returns true if event was dispatched, false if it was dropped for any reason
4370 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004371 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 if (DEBUG_INPUT) Log.v(
4373 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004376 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 if (focusObj == null) {
4378 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4379 if (qev != null) {
4380 mQueue.recycleEvent(qev);
4381 }
4382 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004383 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 }
4385 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4386 if (qev != null) {
4387 mQueue.recycleEvent(qev);
4388 }
4389 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004390 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 }
Romain Guy06882f82009-06-10 13:36:04 -07004392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 if (uid != 0 && uid != focus.mSession.mUid) {
4396 if (mContext.checkPermission(
4397 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4398 != PackageManager.PERMISSION_GRANTED) {
4399 Log.w(TAG, "Permission denied: injecting key event from pid "
4400 + pid + " uid " + uid + " to window " + focus
4401 + " owned by uid " + focus.mSession.mUid);
4402 if (qev != null) {
4403 mQueue.recycleEvent(qev);
4404 }
4405 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004406 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 }
4408 }
Romain Guy06882f82009-06-10 13:36:04 -07004409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 synchronized(mWindowMap) {
4413 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4414 mKeyWaiter.bindTargetWindowLocked(focus,
4415 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4416 // We don't deliver movement events to the client, we hold
4417 // them and wait for them to call back.
4418 ev = null;
4419 } else {
4420 mKeyWaiter.bindTargetWindowLocked(focus);
4421 }
4422 }
Romain Guy06882f82009-06-10 13:36:04 -07004423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 try {
4425 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004426 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 } catch (android.os.RemoteException e) {
4428 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4429 try {
4430 removeWindow(focus.mSession, focus.mClient);
4431 } catch (java.util.NoSuchElementException ex) {
4432 // This will happen if the window has already been
4433 // removed.
4434 }
4435 }
Romain Guy06882f82009-06-10 13:36:04 -07004436
Dianne Hackborncfaef692009-06-15 14:24:44 -07004437 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 }
Romain Guy06882f82009-06-10 13:36:04 -07004439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 /**
4441 * @return Returns true if event was dispatched, false if it was dropped for any reason
4442 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004443 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4445
4446 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004447 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 if (focusObj == null) {
4449 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004450 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 }
4452 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004453 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 }
Romain Guy06882f82009-06-10 13:36:04 -07004455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 if (DEBUG_INPUT) Log.v(
4459 TAG, "Dispatching to " + focus + ": " + event);
4460
4461 if (uid != 0 && uid != focus.mSession.mUid) {
4462 if (mContext.checkPermission(
4463 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4464 != PackageManager.PERMISSION_GRANTED) {
4465 Log.w(TAG, "Permission denied: injecting key event from pid "
4466 + pid + " uid " + uid + " to window " + focus
4467 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004468 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 }
4470 }
Romain Guy06882f82009-06-10 13:36:04 -07004471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 synchronized(mWindowMap) {
4473 mKeyWaiter.bindTargetWindowLocked(focus);
4474 }
4475
4476 // NOSHIP extra state logging
4477 mKeyWaiter.recordDispatchState(event, focus);
4478 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004480 try {
4481 if (DEBUG_INPUT || DEBUG_FOCUS) {
4482 Log.v(TAG, "Delivering key " + event.getKeyCode()
4483 + " to " + focus);
4484 }
4485 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004486 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 } catch (android.os.RemoteException e) {
4488 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4489 try {
4490 removeWindow(focus.mSession, focus.mClient);
4491 } catch (java.util.NoSuchElementException ex) {
4492 // This will happen if the window has already been
4493 // removed.
4494 }
4495 }
Romain Guy06882f82009-06-10 13:36:04 -07004496
Dianne Hackborncfaef692009-06-15 14:24:44 -07004497 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 }
Romain Guy06882f82009-06-10 13:36:04 -07004499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004500 public void pauseKeyDispatching(IBinder _token) {
4501 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4502 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004503 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 }
4505
4506 synchronized (mWindowMap) {
4507 WindowToken token = mTokenMap.get(_token);
4508 if (token != null) {
4509 mKeyWaiter.pauseDispatchingLocked(token);
4510 }
4511 }
4512 }
4513
4514 public void resumeKeyDispatching(IBinder _token) {
4515 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4516 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004517 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 }
4519
4520 synchronized (mWindowMap) {
4521 WindowToken token = mTokenMap.get(_token);
4522 if (token != null) {
4523 mKeyWaiter.resumeDispatchingLocked(token);
4524 }
4525 }
4526 }
4527
4528 public void setEventDispatching(boolean enabled) {
4529 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4530 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004531 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 }
4533
4534 synchronized (mWindowMap) {
4535 mKeyWaiter.setEventDispatchingLocked(enabled);
4536 }
4537 }
Romain Guy06882f82009-06-10 13:36:04 -07004538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539 /**
4540 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004541 *
4542 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 * {@link SystemClock#uptimeMillis()} as the timebase.)
4544 * @param sync If true, wait for the event to be completed before returning to the caller.
4545 * @return Returns true if event was dispatched, false if it was dropped for any reason
4546 */
4547 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4548 long downTime = ev.getDownTime();
4549 long eventTime = ev.getEventTime();
4550
4551 int action = ev.getAction();
4552 int code = ev.getKeyCode();
4553 int repeatCount = ev.getRepeatCount();
4554 int metaState = ev.getMetaState();
4555 int deviceId = ev.getDeviceId();
4556 int scancode = ev.getScanCode();
4557
4558 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4559 if (downTime == 0) downTime = eventTime;
4560
4561 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004562 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004564 final int pid = Binder.getCallingPid();
4565 final int uid = Binder.getCallingUid();
4566 final long ident = Binder.clearCallingIdentity();
4567 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004569 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004571 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004572 switch (result) {
4573 case INJECT_NO_PERMISSION:
4574 throw new SecurityException(
4575 "Injecting to another application requires INJECT_EVENT permission");
4576 case INJECT_SUCCEEDED:
4577 return true;
4578 }
4579 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 }
4581
4582 /**
4583 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004584 *
4585 * @param ev A motion event describing the pointer (touch) action. (As noted in
4586 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004587 * {@link SystemClock#uptimeMillis()} as the timebase.)
4588 * @param sync If true, wait for the event to be completed before returning to the caller.
4589 * @return Returns true if event was dispatched, false if it was dropped for any reason
4590 */
4591 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004592 final int pid = Binder.getCallingPid();
4593 final int uid = Binder.getCallingUid();
4594 final long ident = Binder.clearCallingIdentity();
4595 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004597 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004599 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004600 switch (result) {
4601 case INJECT_NO_PERMISSION:
4602 throw new SecurityException(
4603 "Injecting to another application requires INJECT_EVENT permission");
4604 case INJECT_SUCCEEDED:
4605 return true;
4606 }
4607 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 }
Romain Guy06882f82009-06-10 13:36:04 -07004609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004610 /**
4611 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004612 *
4613 * @param ev A motion event describing the trackball action. (As noted in
4614 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004615 * {@link SystemClock#uptimeMillis()} as the timebase.)
4616 * @param sync If true, wait for the event to be completed before returning to the caller.
4617 * @return Returns true if event was dispatched, false if it was dropped for any reason
4618 */
4619 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004620 final int pid = Binder.getCallingPid();
4621 final int uid = Binder.getCallingUid();
4622 final long ident = Binder.clearCallingIdentity();
4623 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004625 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004626 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004627 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004628 switch (result) {
4629 case INJECT_NO_PERMISSION:
4630 throw new SecurityException(
4631 "Injecting to another application requires INJECT_EVENT permission");
4632 case INJECT_SUCCEEDED:
4633 return true;
4634 }
4635 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 }
Romain Guy06882f82009-06-10 13:36:04 -07004637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 private WindowState getFocusedWindow() {
4639 synchronized (mWindowMap) {
4640 return getFocusedWindowLocked();
4641 }
4642 }
4643
4644 private WindowState getFocusedWindowLocked() {
4645 return mCurrentFocus;
4646 }
Romain Guy06882f82009-06-10 13:36:04 -07004647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 /**
4649 * This class holds the state for dispatching key events. This state
4650 * is protected by the KeyWaiter instance, NOT by the window lock. You
4651 * can be holding the main window lock while acquire the KeyWaiter lock,
4652 * but not the other way around.
4653 */
4654 final class KeyWaiter {
4655 // NOSHIP debugging
4656 public class DispatchState {
4657 private KeyEvent event;
4658 private WindowState focus;
4659 private long time;
4660 private WindowState lastWin;
4661 private IBinder lastBinder;
4662 private boolean finished;
4663 private boolean gotFirstWindow;
4664 private boolean eventDispatching;
4665 private long timeToSwitch;
4666 private boolean wasFrozen;
4667 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004668 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4671 focus = theFocus;
4672 event = theEvent;
4673 time = System.currentTimeMillis();
4674 // snapshot KeyWaiter state
4675 lastWin = mLastWin;
4676 lastBinder = mLastBinder;
4677 finished = mFinished;
4678 gotFirstWindow = mGotFirstWindow;
4679 eventDispatching = mEventDispatching;
4680 timeToSwitch = mTimeToSwitch;
4681 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004682 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 // cache the paused state at ctor time as well
4684 if (theFocus == null || theFocus.mToken == null) {
4685 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4686 focusPaused = false;
4687 } else {
4688 focusPaused = theFocus.mToken.paused;
4689 }
4690 }
Romain Guy06882f82009-06-10 13:36:04 -07004691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 public String toString() {
4693 return "{{" + event + " to " + focus + " @ " + time
4694 + " lw=" + lastWin + " lb=" + lastBinder
4695 + " fin=" + finished + " gfw=" + gotFirstWindow
4696 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004697 + " wf=" + wasFrozen + " fp=" + focusPaused
4698 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 }
4700 };
4701 private DispatchState mDispatchState = null;
4702 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4703 mDispatchState = new DispatchState(theEvent, theFocus);
4704 }
4705 // END NOSHIP
4706
4707 public static final int RETURN_NOTHING = 0;
4708 public static final int RETURN_PENDING_POINTER = 1;
4709 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 final Object SKIP_TARGET_TOKEN = new Object();
4712 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004714 private WindowState mLastWin = null;
4715 private IBinder mLastBinder = null;
4716 private boolean mFinished = true;
4717 private boolean mGotFirstWindow = false;
4718 private boolean mEventDispatching = true;
4719 private long mTimeToSwitch = 0;
4720 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 // Target of Motion events
4723 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 // Windows above the target who would like to receive an "outside"
4726 // touch event for any down events outside of them.
4727 WindowState mOutsideTouchTargets;
4728
4729 /**
4730 * Wait for the last event dispatch to complete, then find the next
4731 * target that should receive the given event and wait for that one
4732 * to be ready to receive it.
4733 */
4734 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4735 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004736 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004737 long startTime = SystemClock.uptimeMillis();
4738 long keyDispatchingTimeout = 5 * 1000;
4739 long waitedFor = 0;
4740
4741 while (true) {
4742 // Figure out which window we care about. It is either the
4743 // last window we are waiting to have process the event or,
4744 // if none, then the next window we think the event should go
4745 // to. Note: we retrieve mLastWin outside of the lock, so
4746 // it may change before we lock. Thus we must check it again.
4747 WindowState targetWin = mLastWin;
4748 boolean targetIsNew = targetWin == null;
4749 if (DEBUG_INPUT) Log.v(
4750 TAG, "waitForLastKey: mFinished=" + mFinished +
4751 ", mLastWin=" + mLastWin);
4752 if (targetIsNew) {
4753 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004754 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755 if (target == SKIP_TARGET_TOKEN) {
4756 // The user has pressed a special key, and we are
4757 // dropping all pending events before it.
4758 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4759 + " " + nextMotion);
4760 return null;
4761 }
4762 if (target == CONSUMED_EVENT_TOKEN) {
4763 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4764 + " " + nextMotion);
4765 return target;
4766 }
4767 targetWin = (WindowState)target;
4768 }
Romain Guy06882f82009-06-10 13:36:04 -07004769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004772 // Now: is it okay to send the next event to this window?
4773 synchronized (this) {
4774 // First: did we come here based on the last window not
4775 // being null, but it changed by the time we got here?
4776 // If so, try again.
4777 if (!targetIsNew && mLastWin == null) {
4778 continue;
4779 }
Romain Guy06882f82009-06-10 13:36:04 -07004780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 // We never dispatch events if not finished with the
4782 // last one, or the display is frozen.
4783 if (mFinished && !mDisplayFrozen) {
4784 // If event dispatching is disabled, then we
4785 // just consume the events.
4786 if (!mEventDispatching) {
4787 if (DEBUG_INPUT) Log.v(TAG,
4788 "Skipping event; dispatching disabled: "
4789 + nextKey + " " + nextMotion);
4790 return null;
4791 }
4792 if (targetWin != null) {
4793 // If this is a new target, and that target is not
4794 // paused or unresponsive, then all looks good to
4795 // handle the event.
4796 if (targetIsNew && !targetWin.mToken.paused) {
4797 return targetWin;
4798 }
Romain Guy06882f82009-06-10 13:36:04 -07004799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004800 // If we didn't find a target window, and there is no
4801 // focused app window, then just eat the events.
4802 } else if (mFocusedApp == null) {
4803 if (DEBUG_INPUT) Log.v(TAG,
4804 "Skipping event; no focused app: "
4805 + nextKey + " " + nextMotion);
4806 return null;
4807 }
4808 }
Romain Guy06882f82009-06-10 13:36:04 -07004809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004810 if (DEBUG_INPUT) Log.v(
4811 TAG, "Waiting for last key in " + mLastBinder
4812 + " target=" + targetWin
4813 + " mFinished=" + mFinished
4814 + " mDisplayFrozen=" + mDisplayFrozen
4815 + " targetIsNew=" + targetIsNew
4816 + " paused="
4817 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004818 + " mFocusedApp=" + mFocusedApp
4819 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 targetApp = targetWin != null
4822 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824 long curTimeout = keyDispatchingTimeout;
4825 if (mTimeToSwitch != 0) {
4826 long now = SystemClock.uptimeMillis();
4827 if (mTimeToSwitch <= now) {
4828 // If an app switch key has been pressed, and we have
4829 // waited too long for the current app to finish
4830 // processing keys, then wait no more!
4831 doFinishedKeyLocked(true);
4832 continue;
4833 }
4834 long switchTimeout = mTimeToSwitch - now;
4835 if (curTimeout > switchTimeout) {
4836 curTimeout = switchTimeout;
4837 }
4838 }
Romain Guy06882f82009-06-10 13:36:04 -07004839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 try {
4841 // after that continue
4842 // processing keys, so we don't get stuck.
4843 if (DEBUG_INPUT) Log.v(
4844 TAG, "Waiting for key dispatch: " + curTimeout);
4845 wait(curTimeout);
4846 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4847 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004848 + startTime + " switchTime=" + mTimeToSwitch
4849 + " target=" + targetWin + " mLW=" + mLastWin
4850 + " mLB=" + mLastBinder + " fin=" + mFinished
4851 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004852 } catch (InterruptedException e) {
4853 }
4854 }
4855
4856 // If we were frozen during configuration change, restart the
4857 // timeout checks from now; otherwise look at whether we timed
4858 // out before awakening.
4859 if (mWasFrozen) {
4860 waitedFor = 0;
4861 mWasFrozen = false;
4862 } else {
4863 waitedFor = SystemClock.uptimeMillis() - startTime;
4864 }
4865
4866 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4867 IApplicationToken at = null;
4868 synchronized (this) {
4869 Log.w(TAG, "Key dispatching timed out sending to " +
4870 (targetWin != null ? targetWin.mAttrs.getTitle()
4871 : "<null>"));
4872 // NOSHIP debugging
4873 Log.w(TAG, "Dispatch state: " + mDispatchState);
4874 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4875 // END NOSHIP
4876 //dump();
4877 if (targetWin != null) {
4878 at = targetWin.getAppToken();
4879 } else if (targetApp != null) {
4880 at = targetApp.appToken;
4881 }
4882 }
4883
4884 boolean abort = true;
4885 if (at != null) {
4886 try {
4887 long timeout = at.getKeyDispatchingTimeout();
4888 if (timeout > waitedFor) {
4889 // we did not wait the proper amount of time for this application.
4890 // set the timeout to be the real timeout and wait again.
4891 keyDispatchingTimeout = timeout - waitedFor;
4892 continue;
4893 } else {
4894 abort = at.keyDispatchingTimedOut();
4895 }
4896 } catch (RemoteException ex) {
4897 }
4898 }
4899
4900 synchronized (this) {
4901 if (abort && (mLastWin == targetWin || targetWin == null)) {
4902 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004903 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004904 if (DEBUG_INPUT) Log.v(TAG,
4905 "Window " + mLastWin +
4906 " timed out on key input");
4907 if (mLastWin.mToken.paused) {
4908 Log.w(TAG, "Un-pausing dispatching to this window");
4909 mLastWin.mToken.paused = false;
4910 }
4911 }
4912 if (mMotionTarget == targetWin) {
4913 mMotionTarget = null;
4914 }
4915 mLastWin = null;
4916 mLastBinder = null;
4917 if (failIfTimeout || targetWin == null) {
4918 return null;
4919 }
4920 } else {
4921 Log.w(TAG, "Continuing to wait for key to be dispatched");
4922 startTime = SystemClock.uptimeMillis();
4923 }
4924 }
4925 }
4926 }
4927 }
Romain Guy06882f82009-06-10 13:36:04 -07004928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004930 MotionEvent nextMotion, boolean isPointerEvent,
4931 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 if (nextKey != null) {
4935 // Find the target window for a normal key event.
4936 final int keycode = nextKey.getKeyCode();
4937 final int repeatCount = nextKey.getRepeatCount();
4938 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4939 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004942 if (callingUid == 0 ||
4943 mContext.checkPermission(
4944 android.Manifest.permission.INJECT_EVENTS,
4945 callingPid, callingUid)
4946 == PackageManager.PERMISSION_GRANTED) {
4947 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07004948 nextKey.getMetaState(), down, repeatCount,
4949 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 Log.w(TAG, "Event timeout during app switch: dropping "
4952 + nextKey);
4953 return SKIP_TARGET_TOKEN;
4954 }
Romain Guy06882f82009-06-10 13:36:04 -07004955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004956 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 WindowState focus = null;
4959 synchronized(mWindowMap) {
4960 focus = getFocusedWindowLocked();
4961 }
Romain Guy06882f82009-06-10 13:36:04 -07004962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004964
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004965 if (callingUid == 0 ||
4966 (focus != null && callingUid == focus.mSession.mUid) ||
4967 mContext.checkPermission(
4968 android.Manifest.permission.INJECT_EVENTS,
4969 callingPid, callingUid)
4970 == PackageManager.PERMISSION_GRANTED) {
4971 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07004972 keycode, nextKey.getMetaState(), down, repeatCount,
4973 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004974 return CONSUMED_EVENT_TOKEN;
4975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 }
Romain Guy06882f82009-06-10 13:36:04 -07004977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980 } else if (!isPointerEvent) {
4981 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4982 if (!dispatch) {
4983 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4984 + nextMotion);
4985 return SKIP_TARGET_TOKEN;
4986 }
Romain Guy06882f82009-06-10 13:36:04 -07004987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 WindowState focus = null;
4989 synchronized(mWindowMap) {
4990 focus = getFocusedWindowLocked();
4991 }
Romain Guy06882f82009-06-10 13:36:04 -07004992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004993 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4994 return focus;
4995 }
Romain Guy06882f82009-06-10 13:36:04 -07004996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997 if (nextMotion == null) {
4998 return SKIP_TARGET_TOKEN;
4999 }
Romain Guy06882f82009-06-10 13:36:04 -07005000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5002 KeyEvent.KEYCODE_UNKNOWN);
5003 if (!dispatch) {
5004 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5005 + nextMotion);
5006 return SKIP_TARGET_TOKEN;
5007 }
Romain Guy06882f82009-06-10 13:36:04 -07005008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 // Find the target window for a pointer event.
5010 int action = nextMotion.getAction();
5011 final float xf = nextMotion.getX();
5012 final float yf = nextMotion.getY();
5013 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 final boolean screenWasOff = qev != null
5016 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 synchronized(mWindowMap) {
5021 synchronized (this) {
5022 if (action == MotionEvent.ACTION_DOWN) {
5023 if (mMotionTarget != null) {
5024 // this is weird, we got a pen down, but we thought it was
5025 // already down!
5026 // XXX: We should probably send an ACTION_UP to the current
5027 // target.
5028 Log.w(TAG, "Pointer down received while already down in: "
5029 + mMotionTarget);
5030 mMotionTarget = null;
5031 }
Romain Guy06882f82009-06-10 13:36:04 -07005032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 // ACTION_DOWN is special, because we need to lock next events to
5034 // the window we'll land onto.
5035 final int x = (int)xf;
5036 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 final ArrayList windows = mWindows;
5039 final int N = windows.size();
5040 WindowState topErrWindow = null;
5041 final Rect tmpRect = mTempRect;
5042 for (int i=N-1; i>=0; i--) {
5043 WindowState child = (WindowState)windows.get(i);
5044 //Log.i(TAG, "Checking dispatch to: " + child);
5045 final int flags = child.mAttrs.flags;
5046 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5047 if (topErrWindow == null) {
5048 topErrWindow = child;
5049 }
5050 }
5051 if (!child.isVisibleLw()) {
5052 //Log.i(TAG, "Not visible!");
5053 continue;
5054 }
5055 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5056 //Log.i(TAG, "Not touchable!");
5057 if ((flags & WindowManager.LayoutParams
5058 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5059 child.mNextOutsideTouch = mOutsideTouchTargets;
5060 mOutsideTouchTargets = child;
5061 }
5062 continue;
5063 }
5064 tmpRect.set(child.mFrame);
5065 if (child.mTouchableInsets == ViewTreeObserver
5066 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5067 // The touch is inside of the window if it is
5068 // inside the frame, AND the content part of that
5069 // frame that was given by the application.
5070 tmpRect.left += child.mGivenContentInsets.left;
5071 tmpRect.top += child.mGivenContentInsets.top;
5072 tmpRect.right -= child.mGivenContentInsets.right;
5073 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5074 } else if (child.mTouchableInsets == ViewTreeObserver
5075 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5076 // The touch is inside of the window if it is
5077 // inside the frame, AND the visible part of that
5078 // frame that was given by the application.
5079 tmpRect.left += child.mGivenVisibleInsets.left;
5080 tmpRect.top += child.mGivenVisibleInsets.top;
5081 tmpRect.right -= child.mGivenVisibleInsets.right;
5082 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5083 }
5084 final int touchFlags = flags &
5085 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5086 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5087 if (tmpRect.contains(x, y) || touchFlags == 0) {
5088 //Log.i(TAG, "Using this target!");
5089 if (!screenWasOff || (flags &
5090 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5091 mMotionTarget = child;
5092 } else {
5093 //Log.i(TAG, "Waking, skip!");
5094 mMotionTarget = null;
5095 }
5096 break;
5097 }
Romain Guy06882f82009-06-10 13:36:04 -07005098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099 if ((flags & WindowManager.LayoutParams
5100 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5101 child.mNextOutsideTouch = mOutsideTouchTargets;
5102 mOutsideTouchTargets = child;
5103 //Log.i(TAG, "Adding to outside target list: " + child);
5104 }
5105 }
5106
5107 // if there's an error window but it's not accepting
5108 // focus (typically because it is not yet visible) just
5109 // wait for it -- any other focused window may in fact
5110 // be in ANR state.
5111 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5112 mMotionTarget = null;
5113 }
5114 }
Romain Guy06882f82009-06-10 13:36:04 -07005115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 target = mMotionTarget;
5117 }
5118 }
Romain Guy06882f82009-06-10 13:36:04 -07005119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005122 // Pointer events are a little different -- if there isn't a
5123 // target found for any event, then just drop it.
5124 return target != null ? target : SKIP_TARGET_TOKEN;
5125 }
Romain Guy06882f82009-06-10 13:36:04 -07005126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 boolean checkShouldDispatchKey(int keycode) {
5128 synchronized (this) {
5129 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5130 mTimeToSwitch = 0;
5131 return true;
5132 }
5133 if (mTimeToSwitch != 0
5134 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5135 return false;
5136 }
5137 return true;
5138 }
5139 }
Romain Guy06882f82009-06-10 13:36:04 -07005140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005141 void bindTargetWindowLocked(WindowState win,
5142 int pendingWhat, QueuedEvent pendingMotion) {
5143 synchronized (this) {
5144 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5145 }
5146 }
Romain Guy06882f82009-06-10 13:36:04 -07005147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 void bindTargetWindowLocked(WindowState win) {
5149 synchronized (this) {
5150 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5151 }
5152 }
5153
5154 void bindTargetWindowLockedLocked(WindowState win,
5155 int pendingWhat, QueuedEvent pendingMotion) {
5156 mLastWin = win;
5157 mLastBinder = win.mClient.asBinder();
5158 mFinished = false;
5159 if (pendingMotion != null) {
5160 final Session s = win.mSession;
5161 if (pendingWhat == RETURN_PENDING_POINTER) {
5162 releasePendingPointerLocked(s);
5163 s.mPendingPointerMove = pendingMotion;
5164 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005165 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 "bindTargetToWindow " + s.mPendingPointerMove);
5167 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5168 releasePendingTrackballLocked(s);
5169 s.mPendingTrackballMove = pendingMotion;
5170 s.mPendingTrackballWindow = win;
5171 }
5172 }
5173 }
Romain Guy06882f82009-06-10 13:36:04 -07005174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 void releasePendingPointerLocked(Session s) {
5176 if (DEBUG_INPUT) Log.v(TAG,
5177 "releasePendingPointer " + s.mPendingPointerMove);
5178 if (s.mPendingPointerMove != null) {
5179 mQueue.recycleEvent(s.mPendingPointerMove);
5180 s.mPendingPointerMove = null;
5181 }
5182 }
Romain Guy06882f82009-06-10 13:36:04 -07005183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 void releasePendingTrackballLocked(Session s) {
5185 if (s.mPendingTrackballMove != null) {
5186 mQueue.recycleEvent(s.mPendingTrackballMove);
5187 s.mPendingTrackballMove = null;
5188 }
5189 }
Romain Guy06882f82009-06-10 13:36:04 -07005190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005191 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5192 int returnWhat) {
5193 if (DEBUG_INPUT) Log.v(
5194 TAG, "finishedKey: client=" + client + ", force=" + force);
5195
5196 if (client == null) {
5197 return null;
5198 }
5199
5200 synchronized (this) {
5201 if (DEBUG_INPUT) Log.v(
5202 TAG, "finishedKey: client=" + client.asBinder()
5203 + ", force=" + force + ", last=" + mLastBinder
5204 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5205
5206 QueuedEvent qev = null;
5207 WindowState win = null;
5208 if (returnWhat == RETURN_PENDING_POINTER) {
5209 qev = session.mPendingPointerMove;
5210 win = session.mPendingPointerWindow;
5211 session.mPendingPointerMove = null;
5212 session.mPendingPointerWindow = null;
5213 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5214 qev = session.mPendingTrackballMove;
5215 win = session.mPendingTrackballWindow;
5216 session.mPendingTrackballMove = null;
5217 session.mPendingTrackballWindow = null;
5218 }
Romain Guy06882f82009-06-10 13:36:04 -07005219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 if (mLastBinder == client.asBinder()) {
5221 if (DEBUG_INPUT) Log.v(
5222 TAG, "finishedKey: last paused="
5223 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5224 if (mLastWin != null && (!mLastWin.mToken.paused || force
5225 || !mEventDispatching)) {
5226 doFinishedKeyLocked(false);
5227 } else {
5228 // Make sure to wake up anyone currently waiting to
5229 // dispatch a key, so they can re-evaluate their
5230 // current situation.
5231 mFinished = true;
5232 notifyAll();
5233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 }
Romain Guy06882f82009-06-10 13:36:04 -07005235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 if (qev != null) {
5237 MotionEvent res = (MotionEvent)qev.event;
5238 if (DEBUG_INPUT) Log.v(TAG,
5239 "Returning pending motion: " + res);
5240 mQueue.recycleEvent(qev);
5241 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5242 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5243 }
5244 return res;
5245 }
5246 return null;
5247 }
5248 }
5249
5250 void tickle() {
5251 synchronized (this) {
5252 notifyAll();
5253 }
5254 }
Romain Guy06882f82009-06-10 13:36:04 -07005255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 void handleNewWindowLocked(WindowState newWindow) {
5257 if (!newWindow.canReceiveKeys()) {
5258 return;
5259 }
5260 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005261 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 TAG, "New key dispatch window: win="
5263 + newWindow.mClient.asBinder()
5264 + ", last=" + mLastBinder
5265 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5266 + "), finished=" + mFinished + ", paused="
5267 + newWindow.mToken.paused);
5268
5269 // Displaying a window implicitly causes dispatching to
5270 // be unpaused. (This is to protect against bugs if someone
5271 // pauses dispatching but forgets to resume.)
5272 newWindow.mToken.paused = false;
5273
5274 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275
5276 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5277 if (DEBUG_INPUT) Log.v(TAG,
5278 "New SYSTEM_ERROR window; resetting state");
5279 mLastWin = null;
5280 mLastBinder = null;
5281 mMotionTarget = null;
5282 mFinished = true;
5283 } else if (mLastWin != null) {
5284 // If the new window is above the window we are
5285 // waiting on, then stop waiting and let key dispatching
5286 // start on the new guy.
5287 if (DEBUG_INPUT) Log.v(
5288 TAG, "Last win layer=" + mLastWin.mLayer
5289 + ", new win layer=" + newWindow.mLayer);
5290 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005291 // The new window is above the old; finish pending input to the last
5292 // window and start directing it to the new one.
5293 mLastWin.mToken.paused = false;
5294 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005296 // Either the new window is lower, so there is no need to wake key waiters,
5297 // or we just finished key input to the previous window, which implicitly
5298 // notified the key waiters. In both cases, we don't need to issue the
5299 // notification here.
5300 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 }
5302
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005303 // Now that we've put a new window state in place, make the event waiter
5304 // take notice and retarget its attentions.
5305 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 }
5307 }
5308
5309 void pauseDispatchingLocked(WindowToken token) {
5310 synchronized (this)
5311 {
5312 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5313 token.paused = true;
5314
5315 /*
5316 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5317 mPaused = true;
5318 } else {
5319 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005320 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005322 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005324 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 }
5326 }
5327 */
5328 }
5329 }
5330
5331 void resumeDispatchingLocked(WindowToken token) {
5332 synchronized (this) {
5333 if (token.paused) {
5334 if (DEBUG_INPUT) Log.v(
5335 TAG, "Resuming WindowToken " + token
5336 + ", last=" + mLastBinder
5337 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5338 + "), finished=" + mFinished + ", paused="
5339 + token.paused);
5340 token.paused = false;
5341 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5342 doFinishedKeyLocked(true);
5343 } else {
5344 notifyAll();
5345 }
5346 }
5347 }
5348 }
5349
5350 void setEventDispatchingLocked(boolean enabled) {
5351 synchronized (this) {
5352 mEventDispatching = enabled;
5353 notifyAll();
5354 }
5355 }
Romain Guy06882f82009-06-10 13:36:04 -07005356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005357 void appSwitchComing() {
5358 synchronized (this) {
5359 // Don't wait for more than .5 seconds for app to finish
5360 // processing the pending events.
5361 long now = SystemClock.uptimeMillis() + 500;
5362 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5363 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5364 mTimeToSwitch = now;
5365 }
5366 notifyAll();
5367 }
5368 }
Romain Guy06882f82009-06-10 13:36:04 -07005369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 private final void doFinishedKeyLocked(boolean doRecycle) {
5371 if (mLastWin != null) {
5372 releasePendingPointerLocked(mLastWin.mSession);
5373 releasePendingTrackballLocked(mLastWin.mSession);
5374 }
Romain Guy06882f82009-06-10 13:36:04 -07005375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 if (mLastWin == null || !mLastWin.mToken.paused
5377 || !mLastWin.isVisibleLw()) {
5378 // If the current window has been paused, we aren't -really-
5379 // finished... so let the waiters still wait.
5380 mLastWin = null;
5381 mLastBinder = null;
5382 }
5383 mFinished = true;
5384 notifyAll();
5385 }
5386 }
5387
5388 private class KeyQ extends KeyInputQueue
5389 implements KeyInputQueue.FilterCallback {
5390 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005393 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5395 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5396 "KEEP_SCREEN_ON_FLAG");
5397 mHoldingScreen.setReferenceCounted(false);
5398 }
5399
5400 @Override
5401 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5402 if (mPolicy.preprocessInputEventTq(event)) {
5403 return true;
5404 }
Romain Guy06882f82009-06-10 13:36:04 -07005405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 switch (event.type) {
5407 case RawInputEvent.EV_KEY: {
5408 // XXX begin hack
5409 if (DEBUG) {
5410 if (event.keycode == KeyEvent.KEYCODE_G) {
5411 if (event.value != 0) {
5412 // G down
5413 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5414 }
5415 return false;
5416 }
5417 if (event.keycode == KeyEvent.KEYCODE_D) {
5418 if (event.value != 0) {
5419 //dump();
5420 }
5421 return false;
5422 }
5423 }
5424 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 boolean screenIsOff = !mPowerManager.screenIsOn();
5427 boolean screenIsDim = !mPowerManager.screenIsBright();
5428 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5431 mPowerManager.goToSleep(event.when);
5432 }
5433
5434 if (screenIsOff) {
5435 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5436 }
5437 if (screenIsDim) {
5438 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5439 }
5440 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5441 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005442 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005443 }
Romain Guy06882f82009-06-10 13:36:04 -07005444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005445 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5446 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5447 filterQueue(this);
5448 mKeyWaiter.appSwitchComing();
5449 }
5450 return true;
5451 } else {
5452 return false;
5453 }
5454 }
Romain Guy06882f82009-06-10 13:36:04 -07005455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 case RawInputEvent.EV_REL: {
5457 boolean screenIsOff = !mPowerManager.screenIsOn();
5458 boolean screenIsDim = !mPowerManager.screenIsBright();
5459 if (screenIsOff) {
5460 if (!mPolicy.isWakeRelMovementTq(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 case RawInputEvent.EV_ABS: {
5474 boolean screenIsOff = !mPowerManager.screenIsOn();
5475 boolean screenIsDim = !mPowerManager.screenIsBright();
5476 if (screenIsOff) {
5477 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5478 device.classes, event)) {
5479 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5480 return false;
5481 }
5482 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5483 }
5484 if (screenIsDim) {
5485 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5486 }
5487 return true;
5488 }
Romain Guy06882f82009-06-10 13:36:04 -07005489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 default:
5491 return true;
5492 }
5493 }
5494
5495 public int filterEvent(QueuedEvent ev) {
5496 switch (ev.classType) {
5497 case RawInputEvent.CLASS_KEYBOARD:
5498 KeyEvent ke = (KeyEvent)ev.event;
5499 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5500 Log.w(TAG, "Dropping movement key during app switch: "
5501 + ke.getKeyCode() + ", action=" + ke.getAction());
5502 return FILTER_REMOVE;
5503 }
5504 return FILTER_ABORT;
5505 default:
5506 return FILTER_KEEP;
5507 }
5508 }
Romain Guy06882f82009-06-10 13:36:04 -07005509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 /**
5511 * Must be called with the main window manager lock held.
5512 */
5513 void setHoldScreenLocked(boolean holding) {
5514 boolean state = mHoldingScreen.isHeld();
5515 if (holding != state) {
5516 if (holding) {
5517 mHoldingScreen.acquire();
5518 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005519 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 mHoldingScreen.release();
5521 }
5522 }
5523 }
Michael Chan53071d62009-05-13 17:29:48 -07005524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005525
5526 public boolean detectSafeMode() {
5527 mSafeMode = mPolicy.detectSafeMode();
5528 return mSafeMode;
5529 }
Romain Guy06882f82009-06-10 13:36:04 -07005530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 public void systemReady() {
5532 mPolicy.systemReady();
5533 }
Romain Guy06882f82009-06-10 13:36:04 -07005534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 private final class InputDispatcherThread extends Thread {
5536 // Time to wait when there is nothing to do: 9999 seconds.
5537 static final int LONG_WAIT=9999*1000;
5538
5539 public InputDispatcherThread() {
5540 super("InputDispatcher");
5541 }
Romain Guy06882f82009-06-10 13:36:04 -07005542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 @Override
5544 public void run() {
5545 while (true) {
5546 try {
5547 process();
5548 } catch (Exception e) {
5549 Log.e(TAG, "Exception in input dispatcher", e);
5550 }
5551 }
5552 }
Romain Guy06882f82009-06-10 13:36:04 -07005553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005554 private void process() {
5555 android.os.Process.setThreadPriority(
5556 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 // The last key event we saw
5559 KeyEvent lastKey = null;
5560
5561 // Last keydown time for auto-repeating keys
5562 long lastKeyTime = SystemClock.uptimeMillis();
5563 long nextKeyTime = lastKeyTime+LONG_WAIT;
5564
Romain Guy06882f82009-06-10 13:36:04 -07005565 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 int keyRepeatCount = 0;
5567
5568 // Need to report that configuration has changed?
5569 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 while (true) {
5572 long curTime = SystemClock.uptimeMillis();
5573
5574 if (DEBUG_INPUT) Log.v(
5575 TAG, "Waiting for next key: now=" + curTime
5576 + ", repeat @ " + nextKeyTime);
5577
5578 // Retrieve next event, waiting only as long as the next
5579 // repeat timeout. If the configuration has changed, then
5580 // don't wait at all -- we'll report the change as soon as
5581 // we have processed all events.
5582 QueuedEvent ev = mQueue.getEvent(
5583 (int)((!configChanged && curTime < nextKeyTime)
5584 ? (nextKeyTime-curTime) : 0));
5585
5586 if (DEBUG_INPUT && ev != null) Log.v(
5587 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5588
Michael Chan53071d62009-05-13 17:29:48 -07005589 if (MEASURE_LATENCY) {
5590 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5591 }
5592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005593 try {
5594 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005595 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 int eventType;
5597 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5598 eventType = eventType((MotionEvent)ev.event);
5599 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5600 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5601 eventType = LocalPowerManager.BUTTON_EVENT;
5602 } else {
5603 eventType = LocalPowerManager.OTHER_EVENT;
5604 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005605 try {
Michael Chan53071d62009-05-13 17:29:48 -07005606 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005607 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005608 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005609 mBatteryStats.noteInputEvent();
5610 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005611 } catch (RemoteException e) {
5612 // Ignore
5613 }
Michael Chane10de972009-05-18 11:24:50 -07005614
5615 if (eventType != TOUCH_EVENT
5616 && eventType != LONG_TOUCH_EVENT
5617 && eventType != CHEEK_EVENT) {
5618 mPowerManager.userActivity(curTime, false,
5619 eventType, false);
5620 } else if (mLastTouchEventType != eventType
5621 || (curTime - mLastUserActivityCallTime)
5622 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5623 mLastUserActivityCallTime = curTime;
5624 mLastTouchEventType = eventType;
5625 mPowerManager.userActivity(curTime, false,
5626 eventType, false);
5627 }
5628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 switch (ev.classType) {
5630 case RawInputEvent.CLASS_KEYBOARD:
5631 KeyEvent ke = (KeyEvent)ev.event;
5632 if (ke.isDown()) {
5633 lastKey = ke;
5634 keyRepeatCount = 0;
5635 lastKeyTime = curTime;
5636 nextKeyTime = lastKeyTime
5637 + KEY_REPEAT_FIRST_DELAY;
5638 if (DEBUG_INPUT) Log.v(
5639 TAG, "Received key down: first repeat @ "
5640 + nextKeyTime);
5641 } else {
5642 lastKey = null;
5643 // Arbitrary long timeout.
5644 lastKeyTime = curTime;
5645 nextKeyTime = curTime + LONG_WAIT;
5646 if (DEBUG_INPUT) Log.v(
5647 TAG, "Received key up: ignore repeat @ "
5648 + nextKeyTime);
5649 }
5650 dispatchKey((KeyEvent)ev.event, 0, 0);
5651 mQueue.recycleEvent(ev);
5652 break;
5653 case RawInputEvent.CLASS_TOUCHSCREEN:
5654 //Log.i(TAG, "Read next event " + ev);
5655 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5656 break;
5657 case RawInputEvent.CLASS_TRACKBALL:
5658 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5659 break;
5660 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5661 configChanged = true;
5662 break;
5663 default:
5664 mQueue.recycleEvent(ev);
5665 break;
5666 }
Romain Guy06882f82009-06-10 13:36:04 -07005667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668 } else if (configChanged) {
5669 configChanged = false;
5670 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672 } else if (lastKey != null) {
5673 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675 // Timeout occurred while key was down. If it is at or
5676 // past the key repeat time, dispatch the repeat.
5677 if (DEBUG_INPUT) Log.v(
5678 TAG, "Key timeout: repeat=" + nextKeyTime
5679 + ", now=" + curTime);
5680 if (curTime < nextKeyTime) {
5681 continue;
5682 }
Romain Guy06882f82009-06-10 13:36:04 -07005683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 lastKeyTime = nextKeyTime;
5685 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5686 keyRepeatCount++;
5687 if (DEBUG_INPUT) Log.v(
5688 TAG, "Key repeat: count=" + keyRepeatCount
5689 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005690 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005692 } else {
5693 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 lastKeyTime = curTime;
5696 nextKeyTime = curTime + LONG_WAIT;
5697 }
Romain Guy06882f82009-06-10 13:36:04 -07005698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 } catch (Exception e) {
5700 Log.e(TAG,
5701 "Input thread received uncaught exception: " + e, e);
5702 }
5703 }
5704 }
5705 }
5706
5707 // -------------------------------------------------------------
5708 // Client Session State
5709 // -------------------------------------------------------------
5710
5711 private final class Session extends IWindowSession.Stub
5712 implements IBinder.DeathRecipient {
5713 final IInputMethodClient mClient;
5714 final IInputContext mInputContext;
5715 final int mUid;
5716 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005717 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 SurfaceSession mSurfaceSession;
5719 int mNumWindow = 0;
5720 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 /**
5723 * Current pointer move event being dispatched to client window... must
5724 * hold key lock to access.
5725 */
5726 QueuedEvent mPendingPointerMove;
5727 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 /**
5730 * Current trackball move event being dispatched to client window... must
5731 * hold key lock to access.
5732 */
5733 QueuedEvent mPendingTrackballMove;
5734 WindowState mPendingTrackballWindow;
5735
5736 public Session(IInputMethodClient client, IInputContext inputContext) {
5737 mClient = client;
5738 mInputContext = inputContext;
5739 mUid = Binder.getCallingUid();
5740 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005741 StringBuilder sb = new StringBuilder();
5742 sb.append("Session{");
5743 sb.append(Integer.toHexString(System.identityHashCode(this)));
5744 sb.append(" uid ");
5745 sb.append(mUid);
5746 sb.append("}");
5747 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005749 synchronized (mWindowMap) {
5750 if (mInputMethodManager == null && mHaveInputMethods) {
5751 IBinder b = ServiceManager.getService(
5752 Context.INPUT_METHOD_SERVICE);
5753 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5754 }
5755 }
5756 long ident = Binder.clearCallingIdentity();
5757 try {
5758 // Note: it is safe to call in to the input method manager
5759 // here because we are not holding our lock.
5760 if (mInputMethodManager != null) {
5761 mInputMethodManager.addClient(client, inputContext,
5762 mUid, mPid);
5763 } else {
5764 client.setUsingInputMethod(false);
5765 }
5766 client.asBinder().linkToDeath(this, 0);
5767 } catch (RemoteException e) {
5768 // The caller has died, so we can just forget about this.
5769 try {
5770 if (mInputMethodManager != null) {
5771 mInputMethodManager.removeClient(client);
5772 }
5773 } catch (RemoteException ee) {
5774 }
5775 } finally {
5776 Binder.restoreCallingIdentity(ident);
5777 }
5778 }
Romain Guy06882f82009-06-10 13:36:04 -07005779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 @Override
5781 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5782 throws RemoteException {
5783 try {
5784 return super.onTransact(code, data, reply, flags);
5785 } catch (RuntimeException e) {
5786 // Log all 'real' exceptions thrown to the caller
5787 if (!(e instanceof SecurityException)) {
5788 Log.e(TAG, "Window Session Crash", e);
5789 }
5790 throw e;
5791 }
5792 }
5793
5794 public void binderDied() {
5795 // Note: it is safe to call in to the input method manager
5796 // here because we are not holding our lock.
5797 try {
5798 if (mInputMethodManager != null) {
5799 mInputMethodManager.removeClient(mClient);
5800 }
5801 } catch (RemoteException e) {
5802 }
5803 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005804 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 mClientDead = true;
5806 killSessionLocked();
5807 }
5808 }
5809
5810 public int add(IWindow window, WindowManager.LayoutParams attrs,
5811 int viewVisibility, Rect outContentInsets) {
5812 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5813 }
Romain Guy06882f82009-06-10 13:36:04 -07005814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005815 public void remove(IWindow window) {
5816 removeWindow(this, window);
5817 }
Romain Guy06882f82009-06-10 13:36:04 -07005818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005819 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5820 int requestedWidth, int requestedHeight, int viewFlags,
5821 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5822 Rect outVisibleInsets, Surface outSurface) {
5823 return relayoutWindow(this, window, attrs,
5824 requestedWidth, requestedHeight, viewFlags, insetsPending,
5825 outFrame, outContentInsets, outVisibleInsets, outSurface);
5826 }
Romain Guy06882f82009-06-10 13:36:04 -07005827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 public void setTransparentRegion(IWindow window, Region region) {
5829 setTransparentRegionWindow(this, window, region);
5830 }
Romain Guy06882f82009-06-10 13:36:04 -07005831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005832 public void setInsets(IWindow window, int touchableInsets,
5833 Rect contentInsets, Rect visibleInsets) {
5834 setInsetsWindow(this, window, touchableInsets, contentInsets,
5835 visibleInsets);
5836 }
Romain Guy06882f82009-06-10 13:36:04 -07005837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5839 getWindowDisplayFrame(this, window, outDisplayFrame);
5840 }
Romain Guy06882f82009-06-10 13:36:04 -07005841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 public void finishDrawing(IWindow window) {
5843 if (localLOGV) Log.v(
5844 TAG, "IWindow finishDrawing called for " + window);
5845 finishDrawingWindow(this, window);
5846 }
5847
5848 public void finishKey(IWindow window) {
5849 if (localLOGV) Log.v(
5850 TAG, "IWindow finishKey called for " + window);
5851 mKeyWaiter.finishedKey(this, window, false,
5852 KeyWaiter.RETURN_NOTHING);
5853 }
5854
5855 public MotionEvent getPendingPointerMove(IWindow window) {
5856 if (localLOGV) Log.v(
5857 TAG, "IWindow getPendingMotionEvent called for " + window);
5858 return mKeyWaiter.finishedKey(this, window, false,
5859 KeyWaiter.RETURN_PENDING_POINTER);
5860 }
Romain Guy06882f82009-06-10 13:36:04 -07005861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 public MotionEvent getPendingTrackballMove(IWindow window) {
5863 if (localLOGV) Log.v(
5864 TAG, "IWindow getPendingMotionEvent called for " + window);
5865 return mKeyWaiter.finishedKey(this, window, false,
5866 KeyWaiter.RETURN_PENDING_TRACKBALL);
5867 }
5868
5869 public void setInTouchMode(boolean mode) {
5870 synchronized(mWindowMap) {
5871 mInTouchMode = mode;
5872 }
5873 }
5874
5875 public boolean getInTouchMode() {
5876 synchronized(mWindowMap) {
5877 return mInTouchMode;
5878 }
5879 }
5880
5881 public boolean performHapticFeedback(IWindow window, int effectId,
5882 boolean always) {
5883 synchronized(mWindowMap) {
5884 long ident = Binder.clearCallingIdentity();
5885 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005886 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 windowForClientLocked(this, window), effectId, always);
5888 } finally {
5889 Binder.restoreCallingIdentity(ident);
5890 }
5891 }
5892 }
Romain Guy06882f82009-06-10 13:36:04 -07005893
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07005894 public void setWallpaperPosition(IBinder window, float x, float y) {
5895 synchronized(mWindowMap) {
5896 long ident = Binder.clearCallingIdentity();
5897 try {
5898 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
5899 x, y);
5900 } finally {
5901 Binder.restoreCallingIdentity(ident);
5902 }
5903 }
5904 }
5905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005906 void windowAddedLocked() {
5907 if (mSurfaceSession == null) {
5908 if (localLOGV) Log.v(
5909 TAG, "First window added to " + this + ", creating SurfaceSession");
5910 mSurfaceSession = new SurfaceSession();
5911 mSessions.add(this);
5912 }
5913 mNumWindow++;
5914 }
5915
5916 void windowRemovedLocked() {
5917 mNumWindow--;
5918 killSessionLocked();
5919 }
Romain Guy06882f82009-06-10 13:36:04 -07005920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005921 void killSessionLocked() {
5922 if (mNumWindow <= 0 && mClientDead) {
5923 mSessions.remove(this);
5924 if (mSurfaceSession != null) {
5925 if (localLOGV) Log.v(
5926 TAG, "Last window removed from " + this
5927 + ", destroying " + mSurfaceSession);
5928 try {
5929 mSurfaceSession.kill();
5930 } catch (Exception e) {
5931 Log.w(TAG, "Exception thrown when killing surface session "
5932 + mSurfaceSession + " in session " + this
5933 + ": " + e.toString());
5934 }
5935 mSurfaceSession = null;
5936 }
5937 }
5938 }
Romain Guy06882f82009-06-10 13:36:04 -07005939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005941 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5942 pw.print(" mClientDead="); pw.print(mClientDead);
5943 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5944 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5945 pw.print(prefix);
5946 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5947 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5948 }
5949 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5950 pw.print(prefix);
5951 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5952 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 }
5955
5956 @Override
5957 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005958 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 }
5960 }
5961
5962 // -------------------------------------------------------------
5963 // Client Window State
5964 // -------------------------------------------------------------
5965
5966 private final class WindowState implements WindowManagerPolicy.WindowState {
5967 final Session mSession;
5968 final IWindow mClient;
5969 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005970 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 AppWindowToken mAppToken;
5972 AppWindowToken mTargetAppToken;
5973 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5974 final DeathRecipient mDeathRecipient;
5975 final WindowState mAttachedWindow;
5976 final ArrayList mChildWindows = new ArrayList();
5977 final int mBaseLayer;
5978 final int mSubLayer;
5979 final boolean mLayoutAttached;
5980 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07005981 final boolean mIsWallpaper;
5982 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 int mViewVisibility;
5984 boolean mPolicyVisibility = true;
5985 boolean mPolicyVisibilityAfterAnim = true;
5986 boolean mAppFreezing;
5987 Surface mSurface;
5988 boolean mAttachedHidden; // is our parent window hidden?
5989 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07005990 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 int mRequestedWidth;
5992 int mRequestedHeight;
5993 int mLastRequestedWidth;
5994 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 int mLayer;
5996 int mAnimLayer;
5997 int mLastLayer;
5998 boolean mHaveFrame;
5999
6000 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 // Actual frame shown on-screen (may be modified by animation)
6003 final Rect mShownFrame = new Rect();
6004 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 /**
6007 * Insets that determine the actually visible area
6008 */
6009 final Rect mVisibleInsets = new Rect();
6010 final Rect mLastVisibleInsets = new Rect();
6011 boolean mVisibleInsetsChanged;
6012
6013 /**
6014 * Insets that are covered by system windows
6015 */
6016 final Rect mContentInsets = new Rect();
6017 final Rect mLastContentInsets = new Rect();
6018 boolean mContentInsetsChanged;
6019
6020 /**
6021 * Set to true if we are waiting for this window to receive its
6022 * given internal insets before laying out other windows based on it.
6023 */
6024 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006026 /**
6027 * These are the content insets that were given during layout for
6028 * this window, to be applied to windows behind it.
6029 */
6030 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032 /**
6033 * These are the visible insets that were given during layout for
6034 * this window, to be applied to windows behind it.
6035 */
6036 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006038 /**
6039 * Flag indicating whether the touchable region should be adjusted by
6040 * the visible insets; if false the area outside the visible insets is
6041 * NOT touchable, so we must use those to adjust the frame during hit
6042 * tests.
6043 */
6044 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006046 // Current transformation being applied.
6047 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6048 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6049 float mHScale=1, mVScale=1;
6050 float mLastHScale=1, mLastVScale=1;
6051 final Matrix mTmpMatrix = new Matrix();
6052
6053 // "Real" frame that the application sees.
6054 final Rect mFrame = new Rect();
6055 final Rect mLastFrame = new Rect();
6056
6057 final Rect mContainingFrame = new Rect();
6058 final Rect mDisplayFrame = new Rect();
6059 final Rect mContentFrame = new Rect();
6060 final Rect mVisibleFrame = new Rect();
6061
6062 float mShownAlpha = 1;
6063 float mAlpha = 1;
6064 float mLastAlpha = 1;
6065
6066 // Set to true if, when the window gets displayed, it should perform
6067 // an enter animation.
6068 boolean mEnterAnimationPending;
6069
6070 // Currently running animation.
6071 boolean mAnimating;
6072 boolean mLocalAnimating;
6073 Animation mAnimation;
6074 boolean mAnimationIsEntrance;
6075 boolean mHasTransformation;
6076 boolean mHasLocalTransformation;
6077 final Transformation mTransformation = new Transformation();
6078
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006079 // If a window showing a wallpaper: the requested offset for the
6080 // wallpaper; if a wallpaper window: the currently applied offset.
6081 float mWallpaperX = -1;
6082 float mWallpaperY = -1;
6083
6084 // Wallpaper windows: pixels offset based on above variables.
6085 int mXOffset;
6086 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006088 // This is set after IWindowSession.relayout() has been called at
6089 // least once for the window. It allows us to detect the situation
6090 // where we don't yet have a surface, but should have one soon, so
6091 // we can give the window focus before waiting for the relayout.
6092 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 // This is set after the Surface has been created but before the
6095 // window has been drawn. During this time the surface is hidden.
6096 boolean mDrawPending;
6097
6098 // This is set after the window has finished drawing for the first
6099 // time but before its surface is shown. The surface will be
6100 // displayed when the next layout is run.
6101 boolean mCommitDrawPending;
6102
6103 // This is set during the time after the window's drawing has been
6104 // committed, and before its surface is actually shown. It is used
6105 // to delay showing the surface until all windows in a token are ready
6106 // to be shown.
6107 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 // Set when the window has been shown in the screen the first time.
6110 boolean mHasDrawn;
6111
6112 // Currently running an exit animation?
6113 boolean mExiting;
6114
6115 // Currently on the mDestroySurface list?
6116 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 // Completely remove from window manager after exit animation?
6119 boolean mRemoveOnExit;
6120
6121 // Set when the orientation is changing and this window has not yet
6122 // been updated for the new orientation.
6123 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006125 // Is this window now (or just being) removed?
6126 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006128 WindowState(Session s, IWindow c, WindowToken token,
6129 WindowState attachedWindow, WindowManager.LayoutParams a,
6130 int viewVisibility) {
6131 mSession = s;
6132 mClient = c;
6133 mToken = token;
6134 mAttrs.copyFrom(a);
6135 mViewVisibility = viewVisibility;
6136 DeathRecipient deathRecipient = new DeathRecipient();
6137 mAlpha = a.alpha;
6138 if (localLOGV) Log.v(
6139 TAG, "Window " + this + " client=" + c.asBinder()
6140 + " token=" + token + " (" + mAttrs.token + ")");
6141 try {
6142 c.asBinder().linkToDeath(deathRecipient, 0);
6143 } catch (RemoteException e) {
6144 mDeathRecipient = null;
6145 mAttachedWindow = null;
6146 mLayoutAttached = false;
6147 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006148 mIsWallpaper = false;
6149 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006150 mBaseLayer = 0;
6151 mSubLayer = 0;
6152 return;
6153 }
6154 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6157 mAttrs.type <= LAST_SUB_WINDOW)) {
6158 // The multiplier here is to reserve space for multiple
6159 // windows in the same type layer.
6160 mBaseLayer = mPolicy.windowTypeToLayerLw(
6161 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6162 + TYPE_LAYER_OFFSET;
6163 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6164 mAttachedWindow = attachedWindow;
6165 mAttachedWindow.mChildWindows.add(this);
6166 mLayoutAttached = mAttrs.type !=
6167 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6168 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6169 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006170 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6171 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 } else {
6173 // The multiplier here is to reserve space for multiple
6174 // windows in the same type layer.
6175 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6176 * TYPE_LAYER_MULTIPLIER
6177 + TYPE_LAYER_OFFSET;
6178 mSubLayer = 0;
6179 mAttachedWindow = null;
6180 mLayoutAttached = false;
6181 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6182 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006183 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6184 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 }
6186
6187 WindowState appWin = this;
6188 while (appWin.mAttachedWindow != null) {
6189 appWin = mAttachedWindow;
6190 }
6191 WindowToken appToken = appWin.mToken;
6192 while (appToken.appWindowToken == null) {
6193 WindowToken parent = mTokenMap.get(appToken.token);
6194 if (parent == null || appToken == parent) {
6195 break;
6196 }
6197 appToken = parent;
6198 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006199 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 mAppToken = appToken.appWindowToken;
6201
6202 mSurface = null;
6203 mRequestedWidth = 0;
6204 mRequestedHeight = 0;
6205 mLastRequestedWidth = 0;
6206 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006207 mXOffset = 0;
6208 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 mLayer = 0;
6210 mAnimLayer = 0;
6211 mLastLayer = 0;
6212 }
6213
6214 void attach() {
6215 if (localLOGV) Log.v(
6216 TAG, "Attaching " + this + " token=" + mToken
6217 + ", list=" + mToken.windows);
6218 mSession.windowAddedLocked();
6219 }
6220
6221 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6222 mHaveFrame = true;
6223
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006224 final Rect container = mContainingFrame;
6225 container.set(pf);
6226
6227 final Rect display = mDisplayFrame;
6228 display.set(df);
6229
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006230 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006231 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006232 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6233 display.intersect(mCompatibleScreenFrame);
6234 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006235 }
6236
6237 final int pw = container.right - container.left;
6238 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239
6240 int w,h;
6241 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6242 w = mAttrs.width < 0 ? pw : mAttrs.width;
6243 h = mAttrs.height< 0 ? ph : mAttrs.height;
6244 } else {
6245 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6246 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6247 }
Romain Guy06882f82009-06-10 13:36:04 -07006248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006249 final Rect content = mContentFrame;
6250 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252 final Rect visible = mVisibleFrame;
6253 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07006256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6258 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6259
6260 Gravity.apply(mAttrs.gravity, w, h, container,
6261 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6262 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6263
6264 //System.out.println("Out: " + mFrame);
6265
6266 // Now make sure the window fits in the overall display.
6267 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 // Make sure the content and visible frames are inside of the
6270 // final window frame.
6271 if (content.left < frame.left) content.left = frame.left;
6272 if (content.top < frame.top) content.top = frame.top;
6273 if (content.right > frame.right) content.right = frame.right;
6274 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6275 if (visible.left < frame.left) visible.left = frame.left;
6276 if (visible.top < frame.top) visible.top = frame.top;
6277 if (visible.right > frame.right) visible.right = frame.right;
6278 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006280 final Rect contentInsets = mContentInsets;
6281 contentInsets.left = content.left-frame.left;
6282 contentInsets.top = content.top-frame.top;
6283 contentInsets.right = frame.right-content.right;
6284 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 final Rect visibleInsets = mVisibleInsets;
6287 visibleInsets.left = visible.left-frame.left;
6288 visibleInsets.top = visible.top-frame.top;
6289 visibleInsets.right = frame.right-visible.right;
6290 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 if (localLOGV) {
6293 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6294 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6295 Log.v(TAG, "Resolving (mRequestedWidth="
6296 + mRequestedWidth + ", mRequestedheight="
6297 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6298 + "): frame=" + mFrame.toShortString()
6299 + " ci=" + contentInsets.toShortString()
6300 + " vi=" + visibleInsets.toShortString());
6301 //}
6302 }
6303 }
Romain Guy06882f82009-06-10 13:36:04 -07006304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006305 public Rect getFrameLw() {
6306 return mFrame;
6307 }
6308
6309 public Rect getShownFrameLw() {
6310 return mShownFrame;
6311 }
6312
6313 public Rect getDisplayFrameLw() {
6314 return mDisplayFrame;
6315 }
6316
6317 public Rect getContentFrameLw() {
6318 return mContentFrame;
6319 }
6320
6321 public Rect getVisibleFrameLw() {
6322 return mVisibleFrame;
6323 }
6324
6325 public boolean getGivenInsetsPendingLw() {
6326 return mGivenInsetsPending;
6327 }
6328
6329 public Rect getGivenContentInsetsLw() {
6330 return mGivenContentInsets;
6331 }
Romain Guy06882f82009-06-10 13:36:04 -07006332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 public Rect getGivenVisibleInsetsLw() {
6334 return mGivenVisibleInsets;
6335 }
Romain Guy06882f82009-06-10 13:36:04 -07006336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 public WindowManager.LayoutParams getAttrs() {
6338 return mAttrs;
6339 }
6340
6341 public int getSurfaceLayer() {
6342 return mLayer;
6343 }
Romain Guy06882f82009-06-10 13:36:04 -07006344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345 public IApplicationToken getAppToken() {
6346 return mAppToken != null ? mAppToken.appToken : null;
6347 }
6348
6349 public boolean hasAppShownWindows() {
6350 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6351 }
6352
6353 public boolean hasAppStartingIcon() {
6354 return mAppToken != null ? (mAppToken.startingData != null) : false;
6355 }
6356
6357 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6358 return mAppToken != null ? mAppToken.startingWindow : null;
6359 }
6360
6361 public void setAnimation(Animation anim) {
6362 if (localLOGV) Log.v(
6363 TAG, "Setting animation in " + this + ": " + anim);
6364 mAnimating = false;
6365 mLocalAnimating = false;
6366 mAnimation = anim;
6367 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6368 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6369 }
6370
6371 public void clearAnimation() {
6372 if (mAnimation != null) {
6373 mAnimating = true;
6374 mLocalAnimating = false;
6375 mAnimation = null;
6376 }
6377 }
Romain Guy06882f82009-06-10 13:36:04 -07006378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006379 Surface createSurfaceLocked() {
6380 if (mSurface == null) {
6381 mDrawPending = true;
6382 mCommitDrawPending = false;
6383 mReadyToShow = false;
6384 if (mAppToken != null) {
6385 mAppToken.allDrawn = false;
6386 }
6387
6388 int flags = 0;
6389 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
6390 flags |= Surface.HARDWARE;
6391 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6392 flags |= Surface.GPU;
6393 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6394 flags |= Surface.PUSH_BUFFERS;
6395 }
6396
6397 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6398 flags |= Surface.SECURE;
6399 }
6400 if (DEBUG_VISIBILITY) Log.v(
6401 TAG, "Creating surface in session "
6402 + mSession.mSurfaceSession + " window " + this
6403 + " w=" + mFrame.width()
6404 + " h=" + mFrame.height() + " format="
6405 + mAttrs.format + " flags=" + flags);
6406
6407 int w = mFrame.width();
6408 int h = mFrame.height();
6409 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6410 // for a scaled surface, we always want the requested
6411 // size.
6412 w = mRequestedWidth;
6413 h = mRequestedHeight;
6414 }
6415
6416 try {
6417 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006418 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419 0, w, h, mAttrs.format, flags);
6420 } catch (Surface.OutOfResourcesException e) {
6421 Log.w(TAG, "OutOfResourcesException creating surface");
6422 reclaimSomeSurfaceMemoryLocked(this, "create");
6423 return null;
6424 } catch (Exception e) {
6425 Log.e(TAG, "Exception creating surface", e);
6426 return null;
6427 }
Romain Guy06882f82009-06-10 13:36:04 -07006428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006429 if (localLOGV) Log.v(
6430 TAG, "Got surface: " + mSurface
6431 + ", set left=" + mFrame.left + " top=" + mFrame.top
6432 + ", animLayer=" + mAnimLayer);
6433 if (SHOW_TRANSACTIONS) {
6434 Log.i(TAG, ">>> OPEN TRANSACTION");
6435 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6436 + mAttrs.getTitle() + ") pos=(" +
6437 mFrame.left + "," + mFrame.top + ") (" +
6438 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6439 mAnimLayer + " HIDE");
6440 }
6441 Surface.openTransaction();
6442 try {
6443 try {
6444 mSurface.setPosition(mFrame.left, mFrame.top);
6445 mSurface.setLayer(mAnimLayer);
6446 mSurface.hide();
6447 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6448 mSurface.setFlags(Surface.SURFACE_DITHER,
6449 Surface.SURFACE_DITHER);
6450 }
6451 } catch (RuntimeException e) {
6452 Log.w(TAG, "Error creating surface in " + w, e);
6453 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6454 }
6455 mLastHidden = true;
6456 } finally {
6457 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6458 Surface.closeTransaction();
6459 }
6460 if (localLOGV) Log.v(
6461 TAG, "Created surface " + this);
6462 }
6463 return mSurface;
6464 }
Romain Guy06882f82009-06-10 13:36:04 -07006465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006466 void destroySurfaceLocked() {
6467 // Window is no longer on-screen, so can no longer receive
6468 // key events... if we were waiting for it to finish
6469 // handling a key event, the wait is over!
6470 mKeyWaiter.finishedKey(mSession, mClient, true,
6471 KeyWaiter.RETURN_NOTHING);
6472 mKeyWaiter.releasePendingPointerLocked(mSession);
6473 mKeyWaiter.releasePendingTrackballLocked(mSession);
6474
6475 if (mAppToken != null && this == mAppToken.startingWindow) {
6476 mAppToken.startingDisplayed = false;
6477 }
Romain Guy06882f82009-06-10 13:36:04 -07006478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 if (localLOGV) Log.v(
6480 TAG, "Window " + this
6481 + " destroying surface " + mSurface + ", session " + mSession);
6482 if (mSurface != null) {
6483 try {
6484 if (SHOW_TRANSACTIONS) {
6485 RuntimeException ex = new RuntimeException();
6486 ex.fillInStackTrace();
6487 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6488 + mAttrs.getTitle() + ")", ex);
6489 }
6490 mSurface.clear();
6491 } catch (RuntimeException e) {
6492 Log.w(TAG, "Exception thrown when destroying Window " + this
6493 + " surface " + mSurface + " session " + mSession
6494 + ": " + e.toString());
6495 }
6496 mSurface = null;
6497 mDrawPending = false;
6498 mCommitDrawPending = false;
6499 mReadyToShow = false;
6500
6501 int i = mChildWindows.size();
6502 while (i > 0) {
6503 i--;
6504 WindowState c = (WindowState)mChildWindows.get(i);
6505 c.mAttachedHidden = true;
6506 }
6507 }
6508 }
6509
6510 boolean finishDrawingLocked() {
6511 if (mDrawPending) {
6512 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6513 TAG, "finishDrawingLocked: " + mSurface);
6514 mCommitDrawPending = true;
6515 mDrawPending = false;
6516 return true;
6517 }
6518 return false;
6519 }
6520
6521 // This must be called while inside a transaction.
6522 void commitFinishDrawingLocked(long currentTime) {
6523 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6524 if (!mCommitDrawPending) {
6525 return;
6526 }
6527 mCommitDrawPending = false;
6528 mReadyToShow = true;
6529 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6530 final AppWindowToken atoken = mAppToken;
6531 if (atoken == null || atoken.allDrawn || starting) {
6532 performShowLocked();
6533 }
6534 }
6535
6536 // This must be called while inside a transaction.
6537 boolean performShowLocked() {
6538 if (DEBUG_VISIBILITY) {
6539 RuntimeException e = new RuntimeException();
6540 e.fillInStackTrace();
6541 Log.v(TAG, "performShow on " + this
6542 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6543 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6544 }
6545 if (mReadyToShow && isReadyForDisplay()) {
6546 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6547 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6548 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6549 + " during animation: policyVis=" + mPolicyVisibility
6550 + " attHidden=" + mAttachedHidden
6551 + " tok.hiddenRequested="
6552 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6553 + " tok.idden="
6554 + (mAppToken != null ? mAppToken.hidden : false)
6555 + " animating=" + mAnimating
6556 + " tok animating="
6557 + (mAppToken != null ? mAppToken.animating : false));
6558 if (!showSurfaceRobustlyLocked(this)) {
6559 return false;
6560 }
6561 mLastAlpha = -1;
6562 mHasDrawn = true;
6563 mLastHidden = false;
6564 mReadyToShow = false;
6565 enableScreenIfNeededLocked();
6566
6567 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006569 int i = mChildWindows.size();
6570 while (i > 0) {
6571 i--;
6572 WindowState c = (WindowState)mChildWindows.get(i);
6573 if (c.mSurface != null && c.mAttachedHidden) {
6574 c.mAttachedHidden = false;
6575 c.performShowLocked();
6576 }
6577 }
Romain Guy06882f82009-06-10 13:36:04 -07006578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 if (mAttrs.type != TYPE_APPLICATION_STARTING
6580 && mAppToken != null) {
6581 mAppToken.firstWindowDrawn = true;
6582 if (mAnimation == null && mAppToken.startingData != null) {
6583 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6584 + mToken
6585 + ": first real window is shown, no animation");
6586 mFinishedStarting.add(mAppToken);
6587 mH.sendEmptyMessage(H.FINISHED_STARTING);
6588 }
6589 mAppToken.updateReportedVisibilityLocked();
6590 }
6591 }
6592 return true;
6593 }
Romain Guy06882f82009-06-10 13:36:04 -07006594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006595 // This must be called while inside a transaction. Returns true if
6596 // there is more animation to run.
6597 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6598 if (!mDisplayFrozen) {
6599 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006601 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6602 mHasTransformation = true;
6603 mHasLocalTransformation = true;
6604 if (!mLocalAnimating) {
6605 if (DEBUG_ANIM) Log.v(
6606 TAG, "Starting animation in " + this +
6607 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6608 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6609 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6610 mAnimation.setStartTime(currentTime);
6611 mLocalAnimating = true;
6612 mAnimating = true;
6613 }
6614 mTransformation.clear();
6615 final boolean more = mAnimation.getTransformation(
6616 currentTime, mTransformation);
6617 if (DEBUG_ANIM) Log.v(
6618 TAG, "Stepped animation in " + this +
6619 ": more=" + more + ", xform=" + mTransformation);
6620 if (more) {
6621 // we're not done!
6622 return true;
6623 }
6624 if (DEBUG_ANIM) Log.v(
6625 TAG, "Finished animation in " + this +
6626 " @ " + currentTime);
6627 mAnimation = null;
6628 //WindowManagerService.this.dump();
6629 }
6630 mHasLocalTransformation = false;
6631 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6632 && mAppToken.hasTransformation) {
6633 // When our app token is animating, we kind-of pretend like
6634 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6635 // part of this check means that we will only do this if
6636 // our window is not currently exiting, or it is not
6637 // locally animating itself. The idea being that one that
6638 // is exiting and doing a local animation should be removed
6639 // once that animation is done.
6640 mAnimating = true;
6641 mHasTransformation = true;
6642 mTransformation.clear();
6643 return false;
6644 } else if (mHasTransformation) {
6645 // Little trick to get through the path below to act like
6646 // we have finished an animation.
6647 mAnimating = true;
6648 } else if (isAnimating()) {
6649 mAnimating = true;
6650 }
6651 } else if (mAnimation != null) {
6652 // If the display is frozen, and there is a pending animation,
6653 // clear it and make sure we run the cleanup code.
6654 mAnimating = true;
6655 mLocalAnimating = true;
6656 mAnimation = null;
6657 }
Romain Guy06882f82009-06-10 13:36:04 -07006658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006659 if (!mAnimating && !mLocalAnimating) {
6660 return false;
6661 }
6662
6663 if (DEBUG_ANIM) Log.v(
6664 TAG, "Animation done in " + this + ": exiting=" + mExiting
6665 + ", reportedVisible="
6666 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006668 mAnimating = false;
6669 mLocalAnimating = false;
6670 mAnimation = null;
6671 mAnimLayer = mLayer;
6672 if (mIsImWindow) {
6673 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006674 } else if (mIsWallpaper) {
6675 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 }
6677 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6678 + " anim layer: " + mAnimLayer);
6679 mHasTransformation = false;
6680 mHasLocalTransformation = false;
6681 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6682 mTransformation.clear();
6683 if (mHasDrawn
6684 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6685 && mAppToken != null
6686 && mAppToken.firstWindowDrawn
6687 && mAppToken.startingData != null) {
6688 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6689 + mToken + ": first real window done animating");
6690 mFinishedStarting.add(mAppToken);
6691 mH.sendEmptyMessage(H.FINISHED_STARTING);
6692 }
Romain Guy06882f82009-06-10 13:36:04 -07006693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006694 finishExit();
6695
6696 if (mAppToken != null) {
6697 mAppToken.updateReportedVisibilityLocked();
6698 }
6699
6700 return false;
6701 }
6702
6703 void finishExit() {
6704 if (DEBUG_ANIM) Log.v(
6705 TAG, "finishExit in " + this
6706 + ": exiting=" + mExiting
6707 + " remove=" + mRemoveOnExit
6708 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006710 final int N = mChildWindows.size();
6711 for (int i=0; i<N; i++) {
6712 ((WindowState)mChildWindows.get(i)).finishExit();
6713 }
Romain Guy06882f82009-06-10 13:36:04 -07006714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 if (!mExiting) {
6716 return;
6717 }
Romain Guy06882f82009-06-10 13:36:04 -07006718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006719 if (isWindowAnimating()) {
6720 return;
6721 }
6722
6723 if (localLOGV) Log.v(
6724 TAG, "Exit animation finished in " + this
6725 + ": remove=" + mRemoveOnExit);
6726 if (mSurface != null) {
6727 mDestroySurface.add(this);
6728 mDestroying = true;
6729 if (SHOW_TRANSACTIONS) Log.i(
6730 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6731 try {
6732 mSurface.hide();
6733 } catch (RuntimeException e) {
6734 Log.w(TAG, "Error hiding surface in " + this, e);
6735 }
6736 mLastHidden = true;
6737 mKeyWaiter.releasePendingPointerLocked(mSession);
6738 }
6739 mExiting = false;
6740 if (mRemoveOnExit) {
6741 mPendingRemove.add(this);
6742 mRemoveOnExit = false;
6743 }
6744 }
Romain Guy06882f82009-06-10 13:36:04 -07006745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6747 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6748 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6749 if (dtdx < -.000001f || dtdx > .000001f) return false;
6750 if (dsdy < -.000001f || dsdy > .000001f) return false;
6751 return true;
6752 }
Romain Guy06882f82009-06-10 13:36:04 -07006753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 void computeShownFrameLocked() {
6755 final boolean selfTransformation = mHasLocalTransformation;
6756 Transformation attachedTransformation =
6757 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6758 ? mAttachedWindow.mTransformation : null;
6759 Transformation appTransformation =
6760 (mAppToken != null && mAppToken.hasTransformation)
6761 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006762
6763 // Wallpapers are animated based on the "real" window they
6764 // are currently targeting.
6765 if (mAttrs.type == TYPE_WALLPAPER && mWallpaperTarget != null) {
6766 if (mWallpaperTarget.mHasLocalTransformation) {
6767 attachedTransformation = mWallpaperTarget.mTransformation;
6768 }
6769 if (mWallpaperTarget.mAppToken != null &&
6770 mWallpaperTarget.mAppToken.hasTransformation) {
6771 appTransformation = mWallpaperTarget.mAppToken.transformation;
6772 }
6773 }
6774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 if (selfTransformation || attachedTransformation != null
6776 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006777 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006778 final Rect frame = mFrame;
6779 final float tmpFloats[] = mTmpFloats;
6780 final Matrix tmpMatrix = mTmpMatrix;
6781
6782 // Compute the desired transformation.
6783 tmpMatrix.setTranslate(frame.left, frame.top);
6784 if (selfTransformation) {
6785 tmpMatrix.preConcat(mTransformation.getMatrix());
6786 }
6787 if (attachedTransformation != null) {
6788 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6789 }
6790 if (appTransformation != null) {
6791 tmpMatrix.preConcat(appTransformation.getMatrix());
6792 }
6793
6794 // "convert" it into SurfaceFlinger's format
6795 // (a 2x2 matrix + an offset)
6796 // Here we must not transform the position of the surface
6797 // since it is already included in the transformation.
6798 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006800 tmpMatrix.getValues(tmpFloats);
6801 mDsDx = tmpFloats[Matrix.MSCALE_X];
6802 mDtDx = tmpFloats[Matrix.MSKEW_X];
6803 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6804 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006805 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
6806 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006807 int w = frame.width();
6808 int h = frame.height();
6809 mShownFrame.set(x, y, x+w, y+h);
6810
6811 // Now set the alpha... but because our current hardware
6812 // can't do alpha transformation on a non-opaque surface,
6813 // turn it off if we are running an animation that is also
6814 // transforming since it is more important to have that
6815 // animation be smooth.
6816 mShownAlpha = mAlpha;
6817 if (!mLimitedAlphaCompositing
6818 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6819 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6820 && x == frame.left && y == frame.top))) {
6821 //Log.i(TAG, "Applying alpha transform");
6822 if (selfTransformation) {
6823 mShownAlpha *= mTransformation.getAlpha();
6824 }
6825 if (attachedTransformation != null) {
6826 mShownAlpha *= attachedTransformation.getAlpha();
6827 }
6828 if (appTransformation != null) {
6829 mShownAlpha *= appTransformation.getAlpha();
6830 }
6831 } else {
6832 //Log.i(TAG, "Not applying alpha transform");
6833 }
Romain Guy06882f82009-06-10 13:36:04 -07006834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006835 if (localLOGV) Log.v(
6836 TAG, "Continuing animation in " + this +
6837 ": " + mShownFrame +
6838 ", alpha=" + mTransformation.getAlpha());
6839 return;
6840 }
Romain Guy06882f82009-06-10 13:36:04 -07006841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006843 if (mXOffset != 0 || mYOffset != 0) {
6844 mShownFrame.offset(mXOffset, mYOffset);
6845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846 mShownAlpha = mAlpha;
6847 mDsDx = 1;
6848 mDtDx = 0;
6849 mDsDy = 0;
6850 mDtDy = 1;
6851 }
Romain Guy06882f82009-06-10 13:36:04 -07006852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853 /**
6854 * Is this window visible? It is not visible if there is no
6855 * surface, or we are in the process of running an exit animation
6856 * that will remove the surface, or its app token has been hidden.
6857 */
6858 public boolean isVisibleLw() {
6859 final AppWindowToken atoken = mAppToken;
6860 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6861 && (atoken == null || !atoken.hiddenRequested)
6862 && !mExiting && !mDestroying;
6863 }
6864
6865 /**
6866 * Is this window visible, ignoring its app token? It is not visible
6867 * if there is no surface, or we are in the process of running an exit animation
6868 * that will remove the surface.
6869 */
6870 public boolean isWinVisibleLw() {
6871 final AppWindowToken atoken = mAppToken;
6872 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6873 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6874 && !mExiting && !mDestroying;
6875 }
6876
6877 /**
6878 * The same as isVisible(), but follows the current hidden state of
6879 * the associated app token, not the pending requested hidden state.
6880 */
6881 boolean isVisibleNow() {
6882 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006883 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006884 }
6885
6886 /**
6887 * Same as isVisible(), but we also count it as visible between the
6888 * call to IWindowSession.add() and the first relayout().
6889 */
6890 boolean isVisibleOrAdding() {
6891 final AppWindowToken atoken = mAppToken;
6892 return (mSurface != null
6893 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6894 && mPolicyVisibility && !mAttachedHidden
6895 && (atoken == null || !atoken.hiddenRequested)
6896 && !mExiting && !mDestroying;
6897 }
6898
6899 /**
6900 * Is this window currently on-screen? It is on-screen either if it
6901 * is visible or it is currently running an animation before no longer
6902 * being visible.
6903 */
6904 boolean isOnScreen() {
6905 final AppWindowToken atoken = mAppToken;
6906 if (atoken != null) {
6907 return mSurface != null && mPolicyVisibility && !mDestroying
6908 && ((!mAttachedHidden && !atoken.hiddenRequested)
6909 || mAnimating || atoken.animating);
6910 } else {
6911 return mSurface != null && mPolicyVisibility && !mDestroying
6912 && (!mAttachedHidden || mAnimating);
6913 }
6914 }
Romain Guy06882f82009-06-10 13:36:04 -07006915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006916 /**
6917 * Like isOnScreen(), but we don't return true if the window is part
6918 * of a transition that has not yet been started.
6919 */
6920 boolean isReadyForDisplay() {
6921 final AppWindowToken atoken = mAppToken;
6922 final boolean animating = atoken != null ? atoken.animating : false;
6923 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006924 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 || mAnimating || animating);
6926 }
6927
6928 /** Is the window or its container currently animating? */
6929 boolean isAnimating() {
6930 final WindowState attached = mAttachedWindow;
6931 final AppWindowToken atoken = mAppToken;
6932 return mAnimation != null
6933 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006934 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 (atoken.animation != null
6936 || atoken.inPendingTransaction));
6937 }
6938
6939 /** Is this window currently animating? */
6940 boolean isWindowAnimating() {
6941 return mAnimation != null;
6942 }
6943
6944 /**
6945 * Like isOnScreen, but returns false if the surface hasn't yet
6946 * been drawn.
6947 */
6948 public boolean isDisplayedLw() {
6949 final AppWindowToken atoken = mAppToken;
6950 return mSurface != null && mPolicyVisibility && !mDestroying
6951 && !mDrawPending && !mCommitDrawPending
6952 && ((!mAttachedHidden &&
6953 (atoken == null || !atoken.hiddenRequested))
6954 || mAnimating);
6955 }
6956
6957 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6958 boolean shownFrame, boolean onlyOpaque) {
6959 if (mSurface == null) {
6960 return false;
6961 }
6962 if (mAppToken != null && !mAppToken.appFullscreen) {
6963 return false;
6964 }
6965 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6966 return false;
6967 }
6968 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006969
6970 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
6971 return frame.left <= mCompatibleScreenFrame.left &&
6972 frame.top <= mCompatibleScreenFrame.top &&
6973 frame.right >= mCompatibleScreenFrame.right &&
6974 frame.bottom >= mCompatibleScreenFrame.bottom;
6975 } else {
6976 return frame.left <= 0 && frame.top <= 0
6977 && frame.right >= screenWidth
6978 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006980 }
Romain Guy06882f82009-06-10 13:36:04 -07006981
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006982 /**
6983 * Return true if the window is opaque and fully drawn.
6984 */
6985 boolean isOpaqueDrawn() {
6986 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
6987 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
6988 }
6989
6990 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
6991 return
6992 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006993 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
6994 // only if it's visible
6995 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006996 // and only if the application fills the compatible screen
6997 mFrame.left <= mCompatibleScreenFrame.left &&
6998 mFrame.top <= mCompatibleScreenFrame.top &&
6999 mFrame.right >= mCompatibleScreenFrame.right &&
7000 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007001 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007002 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007003 }
7004
7005 boolean isFullscreen(int screenWidth, int screenHeight) {
7006 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007007 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 }
7009
7010 void removeLocked() {
7011 if (mAttachedWindow != null) {
7012 mAttachedWindow.mChildWindows.remove(this);
7013 }
7014 destroySurfaceLocked();
7015 mSession.windowRemovedLocked();
7016 try {
7017 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7018 } catch (RuntimeException e) {
7019 // Ignore if it has already been removed (usually because
7020 // we are doing this as part of processing a death note.)
7021 }
7022 }
7023
7024 private class DeathRecipient implements IBinder.DeathRecipient {
7025 public void binderDied() {
7026 try {
7027 synchronized(mWindowMap) {
7028 WindowState win = windowForClientLocked(mSession, mClient);
7029 Log.i(TAG, "WIN DEATH: " + win);
7030 if (win != null) {
7031 removeWindowLocked(mSession, win);
7032 }
7033 }
7034 } catch (IllegalArgumentException ex) {
7035 // This will happen if the window has already been
7036 // removed.
7037 }
7038 }
7039 }
7040
7041 /** Returns true if this window desires key events. */
7042 public final boolean canReceiveKeys() {
7043 return isVisibleOrAdding()
7044 && (mViewVisibility == View.VISIBLE)
7045 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7046 }
7047
7048 public boolean hasDrawnLw() {
7049 return mHasDrawn;
7050 }
7051
7052 public boolean showLw(boolean doAnimation) {
7053 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7054 mPolicyVisibility = true;
7055 mPolicyVisibilityAfterAnim = true;
7056 if (doAnimation) {
7057 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7058 }
7059 requestAnimationLocked(0);
7060 return true;
7061 }
7062 return false;
7063 }
7064
7065 public boolean hideLw(boolean doAnimation) {
7066 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7067 : mPolicyVisibility;
7068 if (current) {
7069 if (doAnimation) {
7070 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7071 if (mAnimation == null) {
7072 doAnimation = false;
7073 }
7074 }
7075 if (doAnimation) {
7076 mPolicyVisibilityAfterAnim = false;
7077 } else {
7078 mPolicyVisibilityAfterAnim = false;
7079 mPolicyVisibility = false;
7080 }
7081 requestAnimationLocked(0);
7082 return true;
7083 }
7084 return false;
7085 }
7086
7087 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007088 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007089
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007090 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7091 pw.print(" mClient="); pw.println(mClient.asBinder());
7092 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7093 if (mAttachedWindow != null || mLayoutAttached) {
7094 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7095 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7096 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007097 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7098 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7099 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007100 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7101 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007102 }
7103 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7104 pw.print(" mSubLayer="); pw.print(mSubLayer);
7105 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7106 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7107 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7108 pw.print("="); pw.print(mAnimLayer);
7109 pw.print(" mLastLayer="); pw.println(mLastLayer);
7110 if (mSurface != null) {
7111 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7112 }
7113 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7114 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7115 if (mAppToken != null) {
7116 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7117 }
7118 if (mTargetAppToken != null) {
7119 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7120 }
7121 pw.print(prefix); pw.print("mViewVisibility=0x");
7122 pw.print(Integer.toHexString(mViewVisibility));
7123 pw.print(" mLastHidden="); pw.print(mLastHidden);
7124 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
7125 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7126 pw.print(prefix); pw.print("mPolicyVisibility=");
7127 pw.print(mPolicyVisibility);
7128 pw.print(" mPolicyVisibilityAfterAnim=");
7129 pw.print(mPolicyVisibilityAfterAnim);
7130 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7131 }
7132 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007133 pw.print(" h="); pw.println(mRequestedHeight);
7134 if (mXOffset != 0 || mYOffset != 0) {
7135 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7136 pw.print(" y="); pw.println(mYOffset);
7137 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007138 pw.print(prefix); pw.print("mGivenContentInsets=");
7139 mGivenContentInsets.printShortString(pw);
7140 pw.print(" mGivenVisibleInsets=");
7141 mGivenVisibleInsets.printShortString(pw);
7142 pw.println();
7143 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7144 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7145 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7146 }
7147 pw.print(prefix); pw.print("mShownFrame=");
7148 mShownFrame.printShortString(pw);
7149 pw.print(" last="); mLastShownFrame.printShortString(pw);
7150 pw.println();
7151 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7152 pw.print(" last="); mLastFrame.printShortString(pw);
7153 pw.println();
7154 pw.print(prefix); pw.print("mContainingFrame=");
7155 mContainingFrame.printShortString(pw);
7156 pw.print(" mDisplayFrame=");
7157 mDisplayFrame.printShortString(pw);
7158 pw.println();
7159 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7160 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7161 pw.println();
7162 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7163 pw.print(" last="); mLastContentInsets.printShortString(pw);
7164 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7165 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7166 pw.println();
7167 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7168 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7169 pw.print(" mAlpha="); pw.print(mAlpha);
7170 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7171 }
7172 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7173 || mAnimation != null) {
7174 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7175 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7176 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7177 pw.print(" mAnimation="); pw.println(mAnimation);
7178 }
7179 if (mHasTransformation || mHasLocalTransformation) {
7180 pw.print(prefix); pw.print("XForm: has=");
7181 pw.print(mHasTransformation);
7182 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7183 pw.print(" "); mTransformation.printShortString(pw);
7184 pw.println();
7185 }
7186 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7187 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7188 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7189 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7190 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7191 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7192 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7193 pw.print(" mDestroying="); pw.print(mDestroying);
7194 pw.print(" mRemoved="); pw.println(mRemoved);
7195 }
7196 if (mOrientationChanging || mAppFreezing) {
7197 pw.print(prefix); pw.print("mOrientationChanging=");
7198 pw.print(mOrientationChanging);
7199 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7200 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007201 if (mHScale != 1 || mVScale != 1) {
7202 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7203 pw.print(" mVScale="); pw.println(mVScale);
7204 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007205 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007206 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7207 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 }
7210
7211 @Override
7212 public String toString() {
7213 return "Window{"
7214 + Integer.toHexString(System.identityHashCode(this))
7215 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7216 }
7217 }
Romain Guy06882f82009-06-10 13:36:04 -07007218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 // -------------------------------------------------------------
7220 // Window Token State
7221 // -------------------------------------------------------------
7222
7223 class WindowToken {
7224 // The actual token.
7225 final IBinder token;
7226
7227 // The type of window this token is for, as per WindowManager.LayoutParams.
7228 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007230 // Set if this token was explicitly added by a client, so should
7231 // not be removed when all windows are removed.
7232 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007233
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007234 // For printing.
7235 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007237 // If this is an AppWindowToken, this is non-null.
7238 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007240 // All of the windows associated with this token.
7241 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7242
7243 // Is key dispatching paused for this token?
7244 boolean paused = false;
7245
7246 // Should this token's windows be hidden?
7247 boolean hidden;
7248
7249 // Temporary for finding which tokens no longer have visible windows.
7250 boolean hasVisible;
7251
7252 WindowToken(IBinder _token, int type, boolean _explicit) {
7253 token = _token;
7254 windowType = type;
7255 explicit = _explicit;
7256 }
7257
7258 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007259 pw.print(prefix); pw.print("token="); pw.println(token);
7260 pw.print(prefix); pw.print("windows="); pw.println(windows);
7261 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7262 pw.print(" hidden="); pw.print(hidden);
7263 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 }
7265
7266 @Override
7267 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007268 if (stringName == null) {
7269 StringBuilder sb = new StringBuilder();
7270 sb.append("WindowToken{");
7271 sb.append(Integer.toHexString(System.identityHashCode(this)));
7272 sb.append(" token="); sb.append(token); sb.append('}');
7273 stringName = sb.toString();
7274 }
7275 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 }
7277 };
7278
7279 class AppWindowToken extends WindowToken {
7280 // Non-null only for application tokens.
7281 final IApplicationToken appToken;
7282
7283 // All of the windows and child windows that are included in this
7284 // application token. Note this list is NOT sorted!
7285 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7286
7287 int groupId = -1;
7288 boolean appFullscreen;
7289 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007291 // These are used for determining when all windows associated with
7292 // an activity have been drawn, so they can be made visible together
7293 // at the same time.
7294 int lastTransactionSequence = mTransactionSequence-1;
7295 int numInterestingWindows;
7296 int numDrawnWindows;
7297 boolean inPendingTransaction;
7298 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007300 // Is this token going to be hidden in a little while? If so, it
7301 // won't be taken into account for setting the screen orientation.
7302 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007304 // Is this window's surface needed? This is almost like hidden, except
7305 // it will sometimes be true a little earlier: when the token has
7306 // been shown, but is still waiting for its app transition to execute
7307 // before making its windows shown.
7308 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007310 // Have we told the window clients to hide themselves?
7311 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007313 // Last visibility state we reported to the app token.
7314 boolean reportedVisible;
7315
7316 // Set to true when the token has been removed from the window mgr.
7317 boolean removed;
7318
7319 // Have we been asked to have this token keep the screen frozen?
7320 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007322 boolean animating;
7323 Animation animation;
7324 boolean hasTransformation;
7325 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 // Offset to the window of all layers in the token, for use by
7328 // AppWindowToken animations.
7329 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007331 // Information about an application starting window if displayed.
7332 StartingData startingData;
7333 WindowState startingWindow;
7334 View startingView;
7335 boolean startingDisplayed;
7336 boolean startingMoved;
7337 boolean firstWindowDrawn;
7338
7339 AppWindowToken(IApplicationToken _token) {
7340 super(_token.asBinder(),
7341 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7342 appWindowToken = this;
7343 appToken = _token;
7344 }
Romain Guy06882f82009-06-10 13:36:04 -07007345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007346 public void setAnimation(Animation anim) {
7347 if (localLOGV) Log.v(
7348 TAG, "Setting animation in " + this + ": " + anim);
7349 animation = anim;
7350 animating = false;
7351 anim.restrictDuration(MAX_ANIMATION_DURATION);
7352 anim.scaleCurrentDuration(mTransitionAnimationScale);
7353 int zorder = anim.getZAdjustment();
7354 int adj = 0;
7355 if (zorder == Animation.ZORDER_TOP) {
7356 adj = TYPE_LAYER_OFFSET;
7357 } else if (zorder == Animation.ZORDER_BOTTOM) {
7358 adj = -TYPE_LAYER_OFFSET;
7359 }
Romain Guy06882f82009-06-10 13:36:04 -07007360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007361 if (animLayerAdjustment != adj) {
7362 animLayerAdjustment = adj;
7363 updateLayers();
7364 }
7365 }
Romain Guy06882f82009-06-10 13:36:04 -07007366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007367 public void setDummyAnimation() {
7368 if (animation == null) {
7369 if (localLOGV) Log.v(
7370 TAG, "Setting dummy animation in " + this);
7371 animation = sDummyAnimation;
7372 }
7373 }
7374
7375 public void clearAnimation() {
7376 if (animation != null) {
7377 animation = null;
7378 animating = true;
7379 }
7380 }
Romain Guy06882f82009-06-10 13:36:04 -07007381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007382 void updateLayers() {
7383 final int N = allAppWindows.size();
7384 final int adj = animLayerAdjustment;
7385 for (int i=0; i<N; i++) {
7386 WindowState w = allAppWindows.get(i);
7387 w.mAnimLayer = w.mLayer + adj;
7388 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7389 + w.mAnimLayer);
7390 if (w == mInputMethodTarget) {
7391 setInputMethodAnimLayerAdjustment(adj);
7392 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007393 if (w == mWallpaperTarget) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007394 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007396 }
7397 }
Romain Guy06882f82009-06-10 13:36:04 -07007398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007399 void sendAppVisibilityToClients() {
7400 final int N = allAppWindows.size();
7401 for (int i=0; i<N; i++) {
7402 WindowState win = allAppWindows.get(i);
7403 if (win == startingWindow && clientHidden) {
7404 // Don't hide the starting window.
7405 continue;
7406 }
7407 try {
7408 if (DEBUG_VISIBILITY) Log.v(TAG,
7409 "Setting visibility of " + win + ": " + (!clientHidden));
7410 win.mClient.dispatchAppVisibility(!clientHidden);
7411 } catch (RemoteException e) {
7412 }
7413 }
7414 }
Romain Guy06882f82009-06-10 13:36:04 -07007415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007416 void showAllWindowsLocked() {
7417 final int NW = allAppWindows.size();
7418 for (int i=0; i<NW; i++) {
7419 WindowState w = allAppWindows.get(i);
7420 if (DEBUG_VISIBILITY) Log.v(TAG,
7421 "performing show on: " + w);
7422 w.performShowLocked();
7423 }
7424 }
Romain Guy06882f82009-06-10 13:36:04 -07007425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007426 // This must be called while inside a transaction.
7427 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7428 if (!mDisplayFrozen) {
7429 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007431 if (animation == sDummyAnimation) {
7432 // This guy is going to animate, but not yet. For now count
7433 // it is not animating for purposes of scheduling transactions;
7434 // when it is really time to animate, this will be set to
7435 // a real animation and the next call will execute normally.
7436 return false;
7437 }
Romain Guy06882f82009-06-10 13:36:04 -07007438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007439 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7440 if (!animating) {
7441 if (DEBUG_ANIM) Log.v(
7442 TAG, "Starting animation in " + this +
7443 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7444 + " scale=" + mTransitionAnimationScale
7445 + " allDrawn=" + allDrawn + " animating=" + animating);
7446 animation.initialize(dw, dh, dw, dh);
7447 animation.setStartTime(currentTime);
7448 animating = true;
7449 }
7450 transformation.clear();
7451 final boolean more = animation.getTransformation(
7452 currentTime, transformation);
7453 if (DEBUG_ANIM) Log.v(
7454 TAG, "Stepped animation in " + this +
7455 ": more=" + more + ", xform=" + transformation);
7456 if (more) {
7457 // we're done!
7458 hasTransformation = true;
7459 return true;
7460 }
7461 if (DEBUG_ANIM) Log.v(
7462 TAG, "Finished animation in " + this +
7463 " @ " + currentTime);
7464 animation = null;
7465 }
7466 } else if (animation != null) {
7467 // If the display is frozen, and there is a pending animation,
7468 // clear it and make sure we run the cleanup code.
7469 animating = true;
7470 animation = null;
7471 }
7472
7473 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007475 if (!animating) {
7476 return false;
7477 }
7478
7479 clearAnimation();
7480 animating = false;
7481 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7482 moveInputMethodWindowsIfNeededLocked(true);
7483 }
Romain Guy06882f82009-06-10 13:36:04 -07007484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007485 if (DEBUG_ANIM) Log.v(
7486 TAG, "Animation done in " + this
7487 + ": reportedVisible=" + reportedVisible);
7488
7489 transformation.clear();
7490 if (animLayerAdjustment != 0) {
7491 animLayerAdjustment = 0;
7492 updateLayers();
7493 }
Romain Guy06882f82009-06-10 13:36:04 -07007494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 final int N = windows.size();
7496 for (int i=0; i<N; i++) {
7497 ((WindowState)windows.get(i)).finishExit();
7498 }
7499 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007501 return false;
7502 }
7503
7504 void updateReportedVisibilityLocked() {
7505 if (appToken == null) {
7506 return;
7507 }
Romain Guy06882f82009-06-10 13:36:04 -07007508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 int numInteresting = 0;
7510 int numVisible = 0;
7511 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7514 final int N = allAppWindows.size();
7515 for (int i=0; i<N; i++) {
7516 WindowState win = allAppWindows.get(i);
7517 if (win == startingWindow || win.mAppFreezing) {
7518 continue;
7519 }
7520 if (DEBUG_VISIBILITY) {
7521 Log.v(TAG, "Win " + win + ": isDisplayed="
7522 + win.isDisplayedLw()
7523 + ", isAnimating=" + win.isAnimating());
7524 if (!win.isDisplayedLw()) {
7525 Log.v(TAG, "Not displayed: s=" + win.mSurface
7526 + " pv=" + win.mPolicyVisibility
7527 + " dp=" + win.mDrawPending
7528 + " cdp=" + win.mCommitDrawPending
7529 + " ah=" + win.mAttachedHidden
7530 + " th="
7531 + (win.mAppToken != null
7532 ? win.mAppToken.hiddenRequested : false)
7533 + " a=" + win.mAnimating);
7534 }
7535 }
7536 numInteresting++;
7537 if (win.isDisplayedLw()) {
7538 if (!win.isAnimating()) {
7539 numVisible++;
7540 }
7541 nowGone = false;
7542 } else if (win.isAnimating()) {
7543 nowGone = false;
7544 }
7545 }
Romain Guy06882f82009-06-10 13:36:04 -07007546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007547 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7548 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7549 + numInteresting + " visible=" + numVisible);
7550 if (nowVisible != reportedVisible) {
7551 if (DEBUG_VISIBILITY) Log.v(
7552 TAG, "Visibility changed in " + this
7553 + ": vis=" + nowVisible);
7554 reportedVisible = nowVisible;
7555 Message m = mH.obtainMessage(
7556 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7557 nowVisible ? 1 : 0,
7558 nowGone ? 1 : 0,
7559 this);
7560 mH.sendMessage(m);
7561 }
7562 }
Romain Guy06882f82009-06-10 13:36:04 -07007563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007564 void dump(PrintWriter pw, String prefix) {
7565 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007566 if (appToken != null) {
7567 pw.print(prefix); pw.println("app=true");
7568 }
7569 if (allAppWindows.size() > 0) {
7570 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7571 }
7572 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7573 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7574 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7575 pw.print(" clientHidden="); pw.print(clientHidden);
7576 pw.print(" willBeHidden="); pw.print(willBeHidden);
7577 pw.print(" reportedVisible="); pw.println(reportedVisible);
7578 if (paused || freezingScreen) {
7579 pw.print(prefix); pw.print("paused="); pw.print(paused);
7580 pw.print(" freezingScreen="); pw.println(freezingScreen);
7581 }
7582 if (numInterestingWindows != 0 || numDrawnWindows != 0
7583 || inPendingTransaction || allDrawn) {
7584 pw.print(prefix); pw.print("numInterestingWindows=");
7585 pw.print(numInterestingWindows);
7586 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7587 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7588 pw.print(" allDrawn="); pw.println(allDrawn);
7589 }
7590 if (animating || animation != null) {
7591 pw.print(prefix); pw.print("animating="); pw.print(animating);
7592 pw.print(" animation="); pw.println(animation);
7593 }
7594 if (animLayerAdjustment != 0) {
7595 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7596 }
7597 if (hasTransformation) {
7598 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7599 pw.print(" transformation="); transformation.printShortString(pw);
7600 pw.println();
7601 }
7602 if (startingData != null || removed || firstWindowDrawn) {
7603 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7604 pw.print(" removed="); pw.print(removed);
7605 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7606 }
7607 if (startingWindow != null || startingView != null
7608 || startingDisplayed || startingMoved) {
7609 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7610 pw.print(" startingView="); pw.print(startingView);
7611 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7612 pw.print(" startingMoved"); pw.println(startingMoved);
7613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007614 }
7615
7616 @Override
7617 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007618 if (stringName == null) {
7619 StringBuilder sb = new StringBuilder();
7620 sb.append("AppWindowToken{");
7621 sb.append(Integer.toHexString(System.identityHashCode(this)));
7622 sb.append(" token="); sb.append(token); sb.append('}');
7623 stringName = sb.toString();
7624 }
7625 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 }
7627 }
Romain Guy06882f82009-06-10 13:36:04 -07007628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007629 public static WindowManager.LayoutParams findAnimations(
7630 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007631 ArrayList<AppWindowToken> openingTokenList1,
7632 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007633 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007634
7635 // First, check if there is a compatible window in opening/closing
7636 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007637 WindowManager.LayoutParams animParams = null;
7638 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007639 animParams = findCompatibleWindowParams(openingTokenList1);
7640 if (animParams == null) {
7641 animParams = findCompatibleWindowParams(closingTokenList2);
7642 }
7643 if (animParams != null) {
7644 return animParams;
7645 }
7646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007647 //Log.i(TAG, "Looking for animations...");
7648 for (int i=order.size()-1; i>=0; i--) {
7649 AppWindowToken wtoken = order.get(i);
7650 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007651 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007652 int j = wtoken.windows.size();
7653 while (j > 0) {
7654 j--;
7655 WindowState win = wtoken.windows.get(j);
7656 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7657 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7658 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7659 //Log.i(TAG, "Found base or application window, done!");
7660 if (wtoken.appFullscreen) {
7661 return win.mAttrs;
7662 }
7663 if (animSrc < 2) {
7664 animParams = win.mAttrs;
7665 animSrc = 2;
7666 }
7667 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7668 //Log.i(TAG, "Found normal window, we may use this...");
7669 animParams = win.mAttrs;
7670 animSrc = 1;
7671 }
7672 }
7673 }
7674 }
Romain Guy06882f82009-06-10 13:36:04 -07007675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 return animParams;
7677 }
Romain Guy06882f82009-06-10 13:36:04 -07007678
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007679 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7680 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7681 AppWindowToken wtoken = tokenList.get(appCount);
7682 // Just checking one window is sufficient as all windows have the compatible flag
7683 // if the application is in compatibility mode.
7684 if (wtoken.windows.size() > 0) {
7685 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7686 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7687 return params;
7688 }
7689 }
7690 }
7691 return null;
7692 }
7693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 // -------------------------------------------------------------
7695 // DummyAnimation
7696 // -------------------------------------------------------------
7697
7698 // This is an animation that does nothing: it just immediately finishes
7699 // itself every time it is called. It is used as a stub animation in cases
7700 // where we want to synchronize multiple things that may be animating.
7701 static final class DummyAnimation extends Animation {
7702 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7703 return false;
7704 }
7705 }
7706 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007708 // -------------------------------------------------------------
7709 // Async Handler
7710 // -------------------------------------------------------------
7711
7712 static final class StartingData {
7713 final String pkg;
7714 final int theme;
7715 final CharSequence nonLocalizedLabel;
7716 final int labelRes;
7717 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7720 int _labelRes, int _icon) {
7721 pkg = _pkg;
7722 theme = _theme;
7723 nonLocalizedLabel = _nonLocalizedLabel;
7724 labelRes = _labelRes;
7725 icon = _icon;
7726 }
7727 }
7728
7729 private final class H extends Handler {
7730 public static final int REPORT_FOCUS_CHANGE = 2;
7731 public static final int REPORT_LOSING_FOCUS = 3;
7732 public static final int ANIMATE = 4;
7733 public static final int ADD_STARTING = 5;
7734 public static final int REMOVE_STARTING = 6;
7735 public static final int FINISHED_STARTING = 7;
7736 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7738 public static final int HOLD_SCREEN_CHANGED = 12;
7739 public static final int APP_TRANSITION_TIMEOUT = 13;
7740 public static final int PERSIST_ANIMATION_SCALE = 14;
7741 public static final int FORCE_GC = 15;
7742 public static final int ENABLE_SCREEN = 16;
7743 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007744 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007746 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007748 public H() {
7749 }
Romain Guy06882f82009-06-10 13:36:04 -07007750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007751 @Override
7752 public void handleMessage(Message msg) {
7753 switch (msg.what) {
7754 case REPORT_FOCUS_CHANGE: {
7755 WindowState lastFocus;
7756 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 synchronized(mWindowMap) {
7759 lastFocus = mLastFocus;
7760 newFocus = mCurrentFocus;
7761 if (lastFocus == newFocus) {
7762 // Focus is not changing, so nothing to do.
7763 return;
7764 }
7765 mLastFocus = newFocus;
7766 //Log.i(TAG, "Focus moving from " + lastFocus
7767 // + " to " + newFocus);
7768 if (newFocus != null && lastFocus != null
7769 && !newFocus.isDisplayedLw()) {
7770 //Log.i(TAG, "Delaying loss of focus...");
7771 mLosingFocus.add(lastFocus);
7772 lastFocus = null;
7773 }
7774 }
7775
7776 if (lastFocus != newFocus) {
7777 //System.out.println("Changing focus from " + lastFocus
7778 // + " to " + newFocus);
7779 if (newFocus != null) {
7780 try {
7781 //Log.i(TAG, "Gaining focus: " + newFocus);
7782 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7783 } catch (RemoteException e) {
7784 // Ignore if process has died.
7785 }
7786 }
7787
7788 if (lastFocus != null) {
7789 try {
7790 //Log.i(TAG, "Losing focus: " + lastFocus);
7791 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7792 } catch (RemoteException e) {
7793 // Ignore if process has died.
7794 }
7795 }
7796 }
7797 } break;
7798
7799 case REPORT_LOSING_FOCUS: {
7800 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 synchronized(mWindowMap) {
7803 losers = mLosingFocus;
7804 mLosingFocus = new ArrayList<WindowState>();
7805 }
7806
7807 final int N = losers.size();
7808 for (int i=0; i<N; i++) {
7809 try {
7810 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7811 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7812 } catch (RemoteException e) {
7813 // Ignore if process has died.
7814 }
7815 }
7816 } break;
7817
7818 case ANIMATE: {
7819 synchronized(mWindowMap) {
7820 mAnimationPending = false;
7821 performLayoutAndPlaceSurfacesLocked();
7822 }
7823 } break;
7824
7825 case ADD_STARTING: {
7826 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7827 final StartingData sd = wtoken.startingData;
7828
7829 if (sd == null) {
7830 // Animation has been canceled... do nothing.
7831 return;
7832 }
Romain Guy06882f82009-06-10 13:36:04 -07007833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7835 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 View view = null;
7838 try {
7839 view = mPolicy.addStartingWindow(
7840 wtoken.token, sd.pkg,
7841 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7842 sd.icon);
7843 } catch (Exception e) {
7844 Log.w(TAG, "Exception when adding starting window", e);
7845 }
7846
7847 if (view != null) {
7848 boolean abort = false;
7849
7850 synchronized(mWindowMap) {
7851 if (wtoken.removed || wtoken.startingData == null) {
7852 // If the window was successfully added, then
7853 // we need to remove it.
7854 if (wtoken.startingWindow != null) {
7855 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7856 "Aborted starting " + wtoken
7857 + ": removed=" + wtoken.removed
7858 + " startingData=" + wtoken.startingData);
7859 wtoken.startingWindow = null;
7860 wtoken.startingData = null;
7861 abort = true;
7862 }
7863 } else {
7864 wtoken.startingView = view;
7865 }
7866 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7867 "Added starting " + wtoken
7868 + ": startingWindow="
7869 + wtoken.startingWindow + " startingView="
7870 + wtoken.startingView);
7871 }
7872
7873 if (abort) {
7874 try {
7875 mPolicy.removeStartingWindow(wtoken.token, view);
7876 } catch (Exception e) {
7877 Log.w(TAG, "Exception when removing starting window", e);
7878 }
7879 }
7880 }
7881 } break;
7882
7883 case REMOVE_STARTING: {
7884 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7885 IBinder token = null;
7886 View view = null;
7887 synchronized (mWindowMap) {
7888 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7889 + wtoken + ": startingWindow="
7890 + wtoken.startingWindow + " startingView="
7891 + wtoken.startingView);
7892 if (wtoken.startingWindow != null) {
7893 view = wtoken.startingView;
7894 token = wtoken.token;
7895 wtoken.startingData = null;
7896 wtoken.startingView = null;
7897 wtoken.startingWindow = null;
7898 }
7899 }
7900 if (view != null) {
7901 try {
7902 mPolicy.removeStartingWindow(token, view);
7903 } catch (Exception e) {
7904 Log.w(TAG, "Exception when removing starting window", e);
7905 }
7906 }
7907 } break;
7908
7909 case FINISHED_STARTING: {
7910 IBinder token = null;
7911 View view = null;
7912 while (true) {
7913 synchronized (mWindowMap) {
7914 final int N = mFinishedStarting.size();
7915 if (N <= 0) {
7916 break;
7917 }
7918 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7919
7920 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7921 "Finished starting " + wtoken
7922 + ": startingWindow=" + wtoken.startingWindow
7923 + " startingView=" + wtoken.startingView);
7924
7925 if (wtoken.startingWindow == null) {
7926 continue;
7927 }
7928
7929 view = wtoken.startingView;
7930 token = wtoken.token;
7931 wtoken.startingData = null;
7932 wtoken.startingView = null;
7933 wtoken.startingWindow = null;
7934 }
7935
7936 try {
7937 mPolicy.removeStartingWindow(token, view);
7938 } catch (Exception e) {
7939 Log.w(TAG, "Exception when removing starting window", e);
7940 }
7941 }
7942 } break;
7943
7944 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7945 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7946
7947 boolean nowVisible = msg.arg1 != 0;
7948 boolean nowGone = msg.arg2 != 0;
7949
7950 try {
7951 if (DEBUG_VISIBILITY) Log.v(
7952 TAG, "Reporting visible in " + wtoken
7953 + " visible=" + nowVisible
7954 + " gone=" + nowGone);
7955 if (nowVisible) {
7956 wtoken.appToken.windowsVisible();
7957 } else {
7958 wtoken.appToken.windowsGone();
7959 }
7960 } catch (RemoteException ex) {
7961 }
7962 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007964 case WINDOW_FREEZE_TIMEOUT: {
7965 synchronized (mWindowMap) {
7966 Log.w(TAG, "Window freeze timeout expired.");
7967 int i = mWindows.size();
7968 while (i > 0) {
7969 i--;
7970 WindowState w = (WindowState)mWindows.get(i);
7971 if (w.mOrientationChanging) {
7972 w.mOrientationChanging = false;
7973 Log.w(TAG, "Force clearing orientation change: " + w);
7974 }
7975 }
7976 performLayoutAndPlaceSurfacesLocked();
7977 }
7978 break;
7979 }
Romain Guy06882f82009-06-10 13:36:04 -07007980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007981 case HOLD_SCREEN_CHANGED: {
7982 Session oldHold;
7983 Session newHold;
7984 synchronized (mWindowMap) {
7985 oldHold = mLastReportedHold;
7986 newHold = (Session)msg.obj;
7987 mLastReportedHold = newHold;
7988 }
Romain Guy06882f82009-06-10 13:36:04 -07007989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007990 if (oldHold != newHold) {
7991 try {
7992 if (oldHold != null) {
7993 mBatteryStats.noteStopWakelock(oldHold.mUid,
7994 "window",
7995 BatteryStats.WAKE_TYPE_WINDOW);
7996 }
7997 if (newHold != null) {
7998 mBatteryStats.noteStartWakelock(newHold.mUid,
7999 "window",
8000 BatteryStats.WAKE_TYPE_WINDOW);
8001 }
8002 } catch (RemoteException e) {
8003 }
8004 }
8005 break;
8006 }
Romain Guy06882f82009-06-10 13:36:04 -07008007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 case APP_TRANSITION_TIMEOUT: {
8009 synchronized (mWindowMap) {
8010 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8011 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8012 "*** APP TRANSITION TIMEOUT");
8013 mAppTransitionReady = true;
8014 mAppTransitionTimeout = true;
8015 performLayoutAndPlaceSurfacesLocked();
8016 }
8017 }
8018 break;
8019 }
Romain Guy06882f82009-06-10 13:36:04 -07008020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 case PERSIST_ANIMATION_SCALE: {
8022 Settings.System.putFloat(mContext.getContentResolver(),
8023 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8024 Settings.System.putFloat(mContext.getContentResolver(),
8025 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8026 break;
8027 }
Romain Guy06882f82009-06-10 13:36:04 -07008028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 case FORCE_GC: {
8030 synchronized(mWindowMap) {
8031 if (mAnimationPending) {
8032 // If we are animating, don't do the gc now but
8033 // delay a bit so we don't interrupt the animation.
8034 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8035 2000);
8036 return;
8037 }
8038 // If we are currently rotating the display, it will
8039 // schedule a new message when done.
8040 if (mDisplayFrozen) {
8041 return;
8042 }
8043 mFreezeGcPending = 0;
8044 }
8045 Runtime.getRuntime().gc();
8046 break;
8047 }
Romain Guy06882f82009-06-10 13:36:04 -07008048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008049 case ENABLE_SCREEN: {
8050 performEnableScreen();
8051 break;
8052 }
Romain Guy06882f82009-06-10 13:36:04 -07008053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054 case APP_FREEZE_TIMEOUT: {
8055 synchronized (mWindowMap) {
8056 Log.w(TAG, "App freeze timeout expired.");
8057 int i = mAppTokens.size();
8058 while (i > 0) {
8059 i--;
8060 AppWindowToken tok = mAppTokens.get(i);
8061 if (tok.freezingScreen) {
8062 Log.w(TAG, "Force clearing freeze: " + tok);
8063 unsetAppFreezingScreenLocked(tok, true, true);
8064 }
8065 }
8066 }
8067 break;
8068 }
Romain Guy06882f82009-06-10 13:36:04 -07008069
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008070 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008071 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008072 sendNewConfiguration();
8073 }
8074 break;
8075 }
Romain Guy06882f82009-06-10 13:36:04 -07008076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008077 }
8078 }
8079 }
8080
8081 // -------------------------------------------------------------
8082 // IWindowManager API
8083 // -------------------------------------------------------------
8084
8085 public IWindowSession openSession(IInputMethodClient client,
8086 IInputContext inputContext) {
8087 if (client == null) throw new IllegalArgumentException("null client");
8088 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8089 return new Session(client, inputContext);
8090 }
8091
8092 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8093 synchronized (mWindowMap) {
8094 // The focus for the client is the window immediately below
8095 // where we would place the input method window.
8096 int idx = findDesiredInputMethodWindowIndexLocked(false);
8097 WindowState imFocus;
8098 if (idx > 0) {
8099 imFocus = (WindowState)mWindows.get(idx-1);
8100 if (imFocus != null) {
8101 if (imFocus.mSession.mClient != null &&
8102 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8103 return true;
8104 }
8105 }
8106 }
8107 }
8108 return false;
8109 }
Romain Guy06882f82009-06-10 13:36:04 -07008110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 // -------------------------------------------------------------
8112 // Internals
8113 // -------------------------------------------------------------
8114
8115 final WindowState windowForClientLocked(Session session, IWindow client) {
8116 return windowForClientLocked(session, client.asBinder());
8117 }
Romain Guy06882f82009-06-10 13:36:04 -07008118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008119 final WindowState windowForClientLocked(Session session, IBinder client) {
8120 WindowState win = mWindowMap.get(client);
8121 if (localLOGV) Log.v(
8122 TAG, "Looking up client " + client + ": " + win);
8123 if (win == null) {
8124 RuntimeException ex = new RuntimeException();
8125 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8126 return null;
8127 }
8128 if (session != null && win.mSession != session) {
8129 RuntimeException ex = new RuntimeException();
8130 Log.w(TAG, "Requested window " + client + " is in session " +
8131 win.mSession + ", not " + session, ex);
8132 return null;
8133 }
8134
8135 return win;
8136 }
8137
8138 private final void assignLayersLocked() {
8139 int N = mWindows.size();
8140 int curBaseLayer = 0;
8141 int curLayer = 0;
8142 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 for (i=0; i<N; i++) {
8145 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008146 if (w.mBaseLayer == curBaseLayer || w.mIsFloatingLayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008147 curLayer += WINDOW_LAYER_MULTIPLIER;
8148 w.mLayer = curLayer;
8149 } else {
8150 curBaseLayer = curLayer = w.mBaseLayer;
8151 w.mLayer = curLayer;
8152 }
8153 if (w.mTargetAppToken != null) {
8154 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8155 } else if (w.mAppToken != null) {
8156 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8157 } else {
8158 w.mAnimLayer = w.mLayer;
8159 }
8160 if (w.mIsImWindow) {
8161 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008162 } else if (w.mIsWallpaper) {
8163 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008164 }
8165 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8166 + w.mAnimLayer);
8167 //System.out.println(
8168 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8169 }
8170 }
8171
8172 private boolean mInLayout = false;
8173 private final void performLayoutAndPlaceSurfacesLocked() {
8174 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008175 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008176 throw new RuntimeException("Recursive call!");
8177 }
8178 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8179 return;
8180 }
8181
8182 boolean recoveringMemory = false;
8183 if (mForceRemoves != null) {
8184 recoveringMemory = true;
8185 // Wait a little it for things to settle down, and off we go.
8186 for (int i=0; i<mForceRemoves.size(); i++) {
8187 WindowState ws = mForceRemoves.get(i);
8188 Log.i(TAG, "Force removing: " + ws);
8189 removeWindowInnerLocked(ws.mSession, ws);
8190 }
8191 mForceRemoves = null;
8192 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8193 Object tmp = new Object();
8194 synchronized (tmp) {
8195 try {
8196 tmp.wait(250);
8197 } catch (InterruptedException e) {
8198 }
8199 }
8200 }
Romain Guy06882f82009-06-10 13:36:04 -07008201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008202 mInLayout = true;
8203 try {
8204 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008206 int i = mPendingRemove.size()-1;
8207 if (i >= 0) {
8208 while (i >= 0) {
8209 WindowState w = mPendingRemove.get(i);
8210 removeWindowInnerLocked(w.mSession, w);
8211 i--;
8212 }
8213 mPendingRemove.clear();
8214
8215 mInLayout = false;
8216 assignLayersLocked();
8217 mLayoutNeeded = true;
8218 performLayoutAndPlaceSurfacesLocked();
8219
8220 } else {
8221 mInLayout = false;
8222 if (mLayoutNeeded) {
8223 requestAnimationLocked(0);
8224 }
8225 }
8226 } catch (RuntimeException e) {
8227 mInLayout = false;
8228 Log.e(TAG, "Unhandled exception while layout out windows", e);
8229 }
8230 }
8231
8232 private final void performLayoutLockedInner() {
8233 final int dw = mDisplay.getWidth();
8234 final int dh = mDisplay.getHeight();
8235
8236 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008237 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 int i;
8239
8240 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008241
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008242 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008243 mPolicy.beginLayoutLw(dw, dh);
8244
8245 // First perform layout of any root windows (not attached
8246 // to another window).
8247 int topAttached = -1;
8248 for (i = N-1; i >= 0; i--) {
8249 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008250
8251 // Don't do layout of a window if it is not visible, or
8252 // soon won't be visible, to avoid wasting time and funky
8253 // changes while a window is animating away.
8254 final AppWindowToken atoken = win.mAppToken;
8255 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008257 || win.mRootToken.hidden
8258 || (atoken != null && atoken.hiddenRequested)
8259 || !win.mPolicyVisibility
8260 || win.mAttachedHidden
8261 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008262
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 (!gone || !win.mHaveFrame) {
8269 if (!win.mLayoutAttached) {
8270 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8271 } else {
8272 if (topAttached < 0) topAttached = i;
8273 }
8274 }
8275 }
Romain Guy06882f82009-06-10 13:36:04 -07008276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008277 // Now perform layout of attached windows, which usually
8278 // depend on the position of the window they are attached to.
8279 // XXX does not deal with windows that are attached to windows
8280 // that are themselves attached.
8281 for (i = topAttached; i >= 0; i--) {
8282 WindowState win = (WindowState) mWindows.get(i);
8283
8284 // If this view is GONE, then skip it -- keep the current
8285 // frame, and let the caller know so they can ignore it
8286 // if they want. (We do the normal layout for INVISIBLE
8287 // windows, since that means "perform layout as normal,
8288 // just don't display").
8289 if (win.mLayoutAttached) {
8290 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8291 || !win.mHaveFrame) {
8292 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8293 }
8294 }
8295 }
8296
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008297 if (!mPolicy.finishLayoutLw()) {
8298 mLayoutNeeded = false;
8299 } else if (repeats > 2) {
8300 Log.w(TAG, "Layout repeat aborted after too many iterations");
8301 mLayoutNeeded = false;
8302 } else {
8303 repeats++;
8304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008305 }
8306 }
Romain Guy06882f82009-06-10 13:36:04 -07008307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008308 private final void performLayoutAndPlaceSurfacesLockedInner(
8309 boolean recoveringMemory) {
8310 final long currentTime = SystemClock.uptimeMillis();
8311 final int dw = mDisplay.getWidth();
8312 final int dh = mDisplay.getHeight();
8313
8314 final int N = mWindows.size();
8315 int i;
8316
8317 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008318 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008320 if (mFxSession == null) {
8321 mFxSession = new SurfaceSession();
8322 }
Romain Guy06882f82009-06-10 13:36:04 -07008323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008324 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8325
8326 // Initialize state of exiting tokens.
8327 for (i=mExitingTokens.size()-1; i>=0; i--) {
8328 mExitingTokens.get(i).hasVisible = false;
8329 }
8330
8331 // Initialize state of exiting applications.
8332 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8333 mExitingAppTokens.get(i).hasVisible = false;
8334 }
8335
8336 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 boolean orientationChangeComplete = true;
8338 Session holdScreen = null;
8339 float screenBrightness = -1;
8340 boolean focusDisplayed = false;
8341 boolean animating = false;
8342
8343 Surface.openTransaction();
8344 try {
8345 boolean restart;
8346
8347 do {
8348 final int transactionSequence = ++mTransactionSequence;
8349
8350 // Update animations of all applications, including those
8351 // associated with exiting/removed apps
8352 boolean tokensAnimating = false;
8353 final int NAT = mAppTokens.size();
8354 for (i=0; i<NAT; i++) {
8355 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8356 tokensAnimating = true;
8357 }
8358 }
8359 final int NEAT = mExitingAppTokens.size();
8360 for (i=0; i<NEAT; i++) {
8361 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8362 tokensAnimating = true;
8363 }
8364 }
8365
8366 animating = tokensAnimating;
8367 restart = false;
8368
8369 boolean tokenMayBeDrawn = false;
8370
8371 mPolicy.beginAnimationLw(dw, dh);
8372
8373 for (i=N-1; i>=0; i--) {
8374 WindowState w = (WindowState)mWindows.get(i);
8375
8376 final WindowManager.LayoutParams attrs = w.mAttrs;
8377
8378 if (w.mSurface != null) {
8379 // Execute animation.
8380 w.commitFinishDrawingLocked(currentTime);
8381 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8382 animating = true;
8383 //w.dump(" ");
8384 }
8385
8386 mPolicy.animatingWindowLw(w, attrs);
8387 }
8388
8389 final AppWindowToken atoken = w.mAppToken;
8390 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8391 if (atoken.lastTransactionSequence != transactionSequence) {
8392 atoken.lastTransactionSequence = transactionSequence;
8393 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8394 atoken.startingDisplayed = false;
8395 }
8396 if ((w.isOnScreen() || w.mAttrs.type
8397 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8398 && !w.mExiting && !w.mDestroying) {
8399 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8400 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8401 + w.isDisplayedLw()
8402 + ", isAnimating=" + w.isAnimating());
8403 if (!w.isDisplayedLw()) {
8404 Log.v(TAG, "Not displayed: s=" + w.mSurface
8405 + " pv=" + w.mPolicyVisibility
8406 + " dp=" + w.mDrawPending
8407 + " cdp=" + w.mCommitDrawPending
8408 + " ah=" + w.mAttachedHidden
8409 + " th=" + atoken.hiddenRequested
8410 + " a=" + w.mAnimating);
8411 }
8412 }
8413 if (w != atoken.startingWindow) {
8414 if (!atoken.freezingScreen || !w.mAppFreezing) {
8415 atoken.numInterestingWindows++;
8416 if (w.isDisplayedLw()) {
8417 atoken.numDrawnWindows++;
8418 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8419 "tokenMayBeDrawn: " + atoken
8420 + " freezingScreen=" + atoken.freezingScreen
8421 + " mAppFreezing=" + w.mAppFreezing);
8422 tokenMayBeDrawn = true;
8423 }
8424 }
8425 } else if (w.isDisplayedLw()) {
8426 atoken.startingDisplayed = true;
8427 }
8428 }
8429 } else if (w.mReadyToShow) {
8430 w.performShowLocked();
8431 }
8432 }
8433
8434 if (mPolicy.finishAnimationLw()) {
8435 restart = true;
8436 }
8437
8438 if (tokenMayBeDrawn) {
8439 // See if any windows have been drawn, so they (and others
8440 // associated with them) can now be shown.
8441 final int NT = mTokenList.size();
8442 for (i=0; i<NT; i++) {
8443 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8444 if (wtoken == null) {
8445 continue;
8446 }
8447 if (wtoken.freezingScreen) {
8448 int numInteresting = wtoken.numInterestingWindows;
8449 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8450 if (DEBUG_VISIBILITY) Log.v(TAG,
8451 "allDrawn: " + wtoken
8452 + " interesting=" + numInteresting
8453 + " drawn=" + wtoken.numDrawnWindows);
8454 wtoken.showAllWindowsLocked();
8455 unsetAppFreezingScreenLocked(wtoken, false, true);
8456 orientationChangeComplete = true;
8457 }
8458 } else if (!wtoken.allDrawn) {
8459 int numInteresting = wtoken.numInterestingWindows;
8460 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8461 if (DEBUG_VISIBILITY) Log.v(TAG,
8462 "allDrawn: " + wtoken
8463 + " interesting=" + numInteresting
8464 + " drawn=" + wtoken.numDrawnWindows);
8465 wtoken.allDrawn = true;
8466 restart = true;
8467
8468 // We can now show all of the drawn windows!
8469 if (!mOpeningApps.contains(wtoken)) {
8470 wtoken.showAllWindowsLocked();
8471 }
8472 }
8473 }
8474 }
8475 }
8476
8477 // If we are ready to perform an app transition, check through
8478 // all of the app tokens to be shown and see if they are ready
8479 // to go.
8480 if (mAppTransitionReady) {
8481 int NN = mOpeningApps.size();
8482 boolean goodToGo = true;
8483 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8484 "Checking " + NN + " opening apps (frozen="
8485 + mDisplayFrozen + " timeout="
8486 + mAppTransitionTimeout + ")...");
8487 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8488 // If the display isn't frozen, wait to do anything until
8489 // all of the apps are ready. Otherwise just go because
8490 // we'll unfreeze the display when everyone is ready.
8491 for (i=0; i<NN && goodToGo; i++) {
8492 AppWindowToken wtoken = mOpeningApps.get(i);
8493 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8494 "Check opening app" + wtoken + ": allDrawn="
8495 + wtoken.allDrawn + " startingDisplayed="
8496 + wtoken.startingDisplayed);
8497 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8498 && !wtoken.startingMoved) {
8499 goodToGo = false;
8500 }
8501 }
8502 }
8503 if (goodToGo) {
8504 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8505 int transit = mNextAppTransition;
8506 if (mSkipAppTransitionAnimation) {
8507 transit = WindowManagerPolicy.TRANSIT_NONE;
8508 }
8509 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8510 mAppTransitionReady = false;
8511 mAppTransitionTimeout = false;
8512 mStartingIconInTransition = false;
8513 mSkipAppTransitionAnimation = false;
8514
8515 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8516
8517 // We need to figure out which animation to use...
8518 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8519 mOpeningApps, mClosingApps);
8520
8521 NN = mOpeningApps.size();
8522 for (i=0; i<NN; i++) {
8523 AppWindowToken wtoken = mOpeningApps.get(i);
8524 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8525 "Now opening app" + wtoken);
8526 wtoken.reportedVisible = false;
8527 wtoken.inPendingTransaction = false;
8528 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8529 wtoken.updateReportedVisibilityLocked();
8530 wtoken.showAllWindowsLocked();
8531 }
8532 NN = mClosingApps.size();
8533 for (i=0; i<NN; i++) {
8534 AppWindowToken wtoken = mClosingApps.get(i);
8535 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8536 "Now closing app" + wtoken);
8537 wtoken.inPendingTransaction = false;
8538 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8539 wtoken.updateReportedVisibilityLocked();
8540 // Force the allDrawn flag, because we want to start
8541 // this guy's animations regardless of whether it's
8542 // gotten drawn.
8543 wtoken.allDrawn = true;
8544 }
8545
8546 mOpeningApps.clear();
8547 mClosingApps.clear();
8548
8549 // This has changed the visibility of windows, so perform
8550 // a new layout to get them all up-to-date.
8551 mLayoutNeeded = true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008552 adjustWallpaperWindowsLocked();
Dianne Hackborn20583ff2009-07-27 21:51:05 -07008553 if (!moveInputMethodWindowsIfNeededLocked(true)) {
8554 assignLayersLocked();
8555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008556 performLayoutLockedInner();
8557 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8558
8559 restart = true;
8560 }
8561 }
8562 } while (restart);
8563
8564 // THIRD LOOP: Update the surfaces of all windows.
8565
8566 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8567
8568 boolean obscured = false;
8569 boolean blurring = false;
8570 boolean dimming = false;
8571 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008572 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008573 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008574
8575 for (i=N-1; i>=0; i--) {
8576 WindowState w = (WindowState)mWindows.get(i);
8577
8578 boolean displayed = false;
8579 final WindowManager.LayoutParams attrs = w.mAttrs;
8580 final int attrFlags = attrs.flags;
8581
8582 if (w.mSurface != null) {
8583 w.computeShownFrameLocked();
8584 if (localLOGV) Log.v(
8585 TAG, "Placing surface #" + i + " " + w.mSurface
8586 + ": new=" + w.mShownFrame + ", old="
8587 + w.mLastShownFrame);
8588
8589 boolean resize;
8590 int width, height;
8591 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8592 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8593 w.mLastRequestedHeight != w.mRequestedHeight;
8594 // for a scaled surface, we just want to use
8595 // the requested size.
8596 width = w.mRequestedWidth;
8597 height = w.mRequestedHeight;
8598 w.mLastRequestedWidth = width;
8599 w.mLastRequestedHeight = height;
8600 w.mLastShownFrame.set(w.mShownFrame);
8601 try {
8602 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8603 } catch (RuntimeException e) {
8604 Log.w(TAG, "Error positioning surface in " + w, e);
8605 if (!recoveringMemory) {
8606 reclaimSomeSurfaceMemoryLocked(w, "position");
8607 }
8608 }
8609 } else {
8610 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8611 width = w.mShownFrame.width();
8612 height = w.mShownFrame.height();
8613 w.mLastShownFrame.set(w.mShownFrame);
8614 if (resize) {
8615 if (SHOW_TRANSACTIONS) Log.i(
8616 TAG, " SURFACE " + w.mSurface + ": ("
8617 + w.mShownFrame.left + ","
8618 + w.mShownFrame.top + ") ("
8619 + w.mShownFrame.width() + "x"
8620 + w.mShownFrame.height() + ")");
8621 }
8622 }
8623
8624 if (resize) {
8625 if (width < 1) width = 1;
8626 if (height < 1) height = 1;
8627 if (w.mSurface != null) {
8628 try {
8629 w.mSurface.setSize(width, height);
8630 w.mSurface.setPosition(w.mShownFrame.left,
8631 w.mShownFrame.top);
8632 } catch (RuntimeException e) {
8633 // If something goes wrong with the surface (such
8634 // as running out of memory), don't take down the
8635 // entire system.
8636 Log.e(TAG, "Failure updating surface of " + w
8637 + "size=(" + width + "x" + height
8638 + "), pos=(" + w.mShownFrame.left
8639 + "," + w.mShownFrame.top + ")", e);
8640 if (!recoveringMemory) {
8641 reclaimSomeSurfaceMemoryLocked(w, "size");
8642 }
8643 }
8644 }
8645 }
8646 if (!w.mAppFreezing) {
8647 w.mContentInsetsChanged =
8648 !w.mLastContentInsets.equals(w.mContentInsets);
8649 w.mVisibleInsetsChanged =
8650 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008651 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 || w.mContentInsetsChanged
8653 || w.mVisibleInsetsChanged) {
8654 w.mLastFrame.set(w.mFrame);
8655 w.mLastContentInsets.set(w.mContentInsets);
8656 w.mLastVisibleInsets.set(w.mVisibleInsets);
8657 // If the orientation is changing, then we need to
8658 // hold off on unfreezing the display until this
8659 // window has been redrawn; to do that, we need
8660 // to go through the process of getting informed
8661 // by the application when it has finished drawing.
8662 if (w.mOrientationChanging) {
8663 if (DEBUG_ORIENTATION) Log.v(TAG,
8664 "Orientation start waiting for draw in "
8665 + w + ", surface " + w.mSurface);
8666 w.mDrawPending = true;
8667 w.mCommitDrawPending = false;
8668 w.mReadyToShow = false;
8669 if (w.mAppToken != null) {
8670 w.mAppToken.allDrawn = false;
8671 }
8672 }
Romain Guy06882f82009-06-10 13:36:04 -07008673 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 "Resizing window " + w + " to " + w.mFrame);
8675 mResizingWindows.add(w);
8676 } else if (w.mOrientationChanging) {
8677 if (!w.mDrawPending && !w.mCommitDrawPending) {
8678 if (DEBUG_ORIENTATION) Log.v(TAG,
8679 "Orientation not waiting for draw in "
8680 + w + ", surface " + w.mSurface);
8681 w.mOrientationChanging = false;
8682 }
8683 }
8684 }
8685
8686 if (w.mAttachedHidden) {
8687 if (!w.mLastHidden) {
8688 //dump();
8689 w.mLastHidden = true;
8690 if (SHOW_TRANSACTIONS) Log.i(
8691 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8692 if (w.mSurface != null) {
8693 try {
8694 w.mSurface.hide();
8695 } catch (RuntimeException e) {
8696 Log.w(TAG, "Exception hiding surface in " + w);
8697 }
8698 }
8699 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8700 }
8701 // If we are waiting for this window to handle an
8702 // orientation change, well, it is hidden, so
8703 // doesn't really matter. Note that this does
8704 // introduce a potential glitch if the window
8705 // becomes unhidden before it has drawn for the
8706 // new orientation.
8707 if (w.mOrientationChanging) {
8708 w.mOrientationChanging = false;
8709 if (DEBUG_ORIENTATION) Log.v(TAG,
8710 "Orientation change skips hidden " + w);
8711 }
8712 } else if (!w.isReadyForDisplay()) {
8713 if (!w.mLastHidden) {
8714 //dump();
8715 w.mLastHidden = true;
8716 if (SHOW_TRANSACTIONS) Log.i(
8717 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8718 if (w.mSurface != null) {
8719 try {
8720 w.mSurface.hide();
8721 } catch (RuntimeException e) {
8722 Log.w(TAG, "Exception exception hiding surface in " + w);
8723 }
8724 }
8725 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8726 }
8727 // If we are waiting for this window to handle an
8728 // orientation change, well, it is hidden, so
8729 // doesn't really matter. Note that this does
8730 // introduce a potential glitch if the window
8731 // becomes unhidden before it has drawn for the
8732 // new orientation.
8733 if (w.mOrientationChanging) {
8734 w.mOrientationChanging = false;
8735 if (DEBUG_ORIENTATION) Log.v(TAG,
8736 "Orientation change skips hidden " + w);
8737 }
8738 } else if (w.mLastLayer != w.mAnimLayer
8739 || w.mLastAlpha != w.mShownAlpha
8740 || w.mLastDsDx != w.mDsDx
8741 || w.mLastDtDx != w.mDtDx
8742 || w.mLastDsDy != w.mDsDy
8743 || w.mLastDtDy != w.mDtDy
8744 || w.mLastHScale != w.mHScale
8745 || w.mLastVScale != w.mVScale
8746 || w.mLastHidden) {
8747 displayed = true;
8748 w.mLastAlpha = w.mShownAlpha;
8749 w.mLastLayer = w.mAnimLayer;
8750 w.mLastDsDx = w.mDsDx;
8751 w.mLastDtDx = w.mDtDx;
8752 w.mLastDsDy = w.mDsDy;
8753 w.mLastDtDy = w.mDtDy;
8754 w.mLastHScale = w.mHScale;
8755 w.mLastVScale = w.mVScale;
8756 if (SHOW_TRANSACTIONS) Log.i(
8757 TAG, " SURFACE " + w.mSurface + ": alpha="
8758 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8759 if (w.mSurface != null) {
8760 try {
8761 w.mSurface.setAlpha(w.mShownAlpha);
8762 w.mSurface.setLayer(w.mAnimLayer);
8763 w.mSurface.setMatrix(
8764 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8765 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8766 } catch (RuntimeException e) {
8767 Log.w(TAG, "Error updating surface in " + w, e);
8768 if (!recoveringMemory) {
8769 reclaimSomeSurfaceMemoryLocked(w, "update");
8770 }
8771 }
8772 }
8773
8774 if (w.mLastHidden && !w.mDrawPending
8775 && !w.mCommitDrawPending
8776 && !w.mReadyToShow) {
8777 if (SHOW_TRANSACTIONS) Log.i(
8778 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8779 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8780 + " during relayout");
8781 if (showSurfaceRobustlyLocked(w)) {
8782 w.mHasDrawn = true;
8783 w.mLastHidden = false;
8784 } else {
8785 w.mOrientationChanging = false;
8786 }
8787 }
8788 if (w.mSurface != null) {
8789 w.mToken.hasVisible = true;
8790 }
8791 } else {
8792 displayed = true;
8793 }
8794
8795 if (displayed) {
8796 if (!covered) {
8797 if (attrs.width == LayoutParams.FILL_PARENT
8798 && attrs.height == LayoutParams.FILL_PARENT) {
8799 covered = true;
8800 }
8801 }
8802 if (w.mOrientationChanging) {
8803 if (w.mDrawPending || w.mCommitDrawPending) {
8804 orientationChangeComplete = false;
8805 if (DEBUG_ORIENTATION) Log.v(TAG,
8806 "Orientation continue waiting for draw in " + w);
8807 } else {
8808 w.mOrientationChanging = false;
8809 if (DEBUG_ORIENTATION) Log.v(TAG,
8810 "Orientation change complete in " + w);
8811 }
8812 }
8813 w.mToken.hasVisible = true;
8814 }
8815 } else if (w.mOrientationChanging) {
8816 if (DEBUG_ORIENTATION) Log.v(TAG,
8817 "Orientation change skips hidden " + w);
8818 w.mOrientationChanging = false;
8819 }
8820
8821 final boolean canBeSeen = w.isDisplayedLw();
8822
8823 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8824 focusDisplayed = true;
8825 }
8826
8827 // Update effect.
8828 if (!obscured) {
8829 if (w.mSurface != null) {
8830 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8831 holdScreen = w.mSession;
8832 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008833 if (!syswin && w.mAttrs.screenBrightness >= 0
8834 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008835 screenBrightness = w.mAttrs.screenBrightness;
8836 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008837 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8838 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8839 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8840 syswin = true;
8841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008842 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008843
8844 boolean opaqueDrawn = w.isOpaqueDrawn();
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008845 if ((opaqueDrawn && w.isFullscreen(dw, dh))
8846 || attrs.type == TYPE_WALLPAPER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008847 // This window completely covers everything behind it,
8848 // so we want to leave all of them as unblurred (for
8849 // performance reasons).
8850 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008851 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8852 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008853 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008854 obscured = true;
8855 if (mBackgroundFillerSurface == null) {
8856 try {
8857 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8858 0, dw, dh,
8859 PixelFormat.OPAQUE,
8860 Surface.FX_SURFACE_NORMAL);
8861 } catch (Exception e) {
8862 Log.e(TAG, "Exception creating filler surface", e);
8863 }
8864 }
8865 try {
8866 mBackgroundFillerSurface.setPosition(0, 0);
8867 mBackgroundFillerSurface.setSize(dw, dh);
8868 // Using the same layer as Dim because they will never be shown at the
8869 // same time.
8870 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8871 mBackgroundFillerSurface.show();
8872 } catch (RuntimeException e) {
8873 Log.e(TAG, "Exception showing filler surface");
8874 }
8875 backgroundFillerShown = true;
8876 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008877 } else if (canBeSeen && !obscured &&
8878 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8879 if (localLOGV) Log.v(TAG, "Win " + w
8880 + ": blurring=" + blurring
8881 + " obscured=" + obscured
8882 + " displayed=" + displayed);
8883 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8884 if (!dimming) {
8885 //Log.i(TAG, "DIM BEHIND: " + w);
8886 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008887 if (mDimAnimator == null) {
8888 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008890 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008891 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008892 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008893 }
8894 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8895 if (!blurring) {
8896 //Log.i(TAG, "BLUR BEHIND: " + w);
8897 blurring = true;
8898 mBlurShown = true;
8899 if (mBlurSurface == null) {
8900 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8901 + mBlurSurface + ": CREATE");
8902 try {
Romain Guy06882f82009-06-10 13:36:04 -07008903 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 -1, 16, 16,
8905 PixelFormat.OPAQUE,
8906 Surface.FX_SURFACE_BLUR);
8907 } catch (Exception e) {
8908 Log.e(TAG, "Exception creating Blur surface", e);
8909 }
8910 }
8911 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8912 + mBlurSurface + ": SHOW pos=(0,0) (" +
8913 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8914 if (mBlurSurface != null) {
8915 mBlurSurface.setPosition(0, 0);
8916 mBlurSurface.setSize(dw, dh);
8917 try {
8918 mBlurSurface.show();
8919 } catch (RuntimeException e) {
8920 Log.w(TAG, "Failure showing blur surface", e);
8921 }
8922 }
8923 }
8924 mBlurSurface.setLayer(w.mAnimLayer-2);
8925 }
8926 }
8927 }
8928 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008929
8930 if (backgroundFillerShown == false && mBackgroundFillerShown) {
8931 mBackgroundFillerShown = false;
8932 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
8933 try {
8934 mBackgroundFillerSurface.hide();
8935 } catch (RuntimeException e) {
8936 Log.e(TAG, "Exception hiding filler surface", e);
8937 }
8938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008939
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008940 if (mDimAnimator != null && mDimAnimator.mDimShown) {
8941 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008942 }
Romain Guy06882f82009-06-10 13:36:04 -07008943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008944 if (!blurring && mBlurShown) {
8945 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8946 + ": HIDE");
8947 try {
8948 mBlurSurface.hide();
8949 } catch (IllegalArgumentException e) {
8950 Log.w(TAG, "Illegal argument exception hiding blur surface");
8951 }
8952 mBlurShown = false;
8953 }
8954
8955 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8956 } catch (RuntimeException e) {
8957 Log.e(TAG, "Unhandled exception in Window Manager", e);
8958 }
8959
8960 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008962 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8963 "With display frozen, orientationChangeComplete="
8964 + orientationChangeComplete);
8965 if (orientationChangeComplete) {
8966 if (mWindowsFreezingScreen) {
8967 mWindowsFreezingScreen = false;
8968 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8969 }
8970 if (mAppsFreezingScreen == 0) {
8971 stopFreezingDisplayLocked();
8972 }
8973 }
Romain Guy06882f82009-06-10 13:36:04 -07008974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008975 i = mResizingWindows.size();
8976 if (i > 0) {
8977 do {
8978 i--;
8979 WindowState win = mResizingWindows.get(i);
8980 try {
8981 win.mClient.resized(win.mFrame.width(),
8982 win.mFrame.height(), win.mLastContentInsets,
8983 win.mLastVisibleInsets, win.mDrawPending);
8984 win.mContentInsetsChanged = false;
8985 win.mVisibleInsetsChanged = false;
8986 } catch (RemoteException e) {
8987 win.mOrientationChanging = false;
8988 }
8989 } while (i > 0);
8990 mResizingWindows.clear();
8991 }
Romain Guy06882f82009-06-10 13:36:04 -07008992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008993 // Destroy the surface of any windows that are no longer visible.
8994 i = mDestroySurface.size();
8995 if (i > 0) {
8996 do {
8997 i--;
8998 WindowState win = mDestroySurface.get(i);
8999 win.mDestroying = false;
9000 if (mInputMethodWindow == win) {
9001 mInputMethodWindow = null;
9002 }
9003 win.destroySurfaceLocked();
9004 } while (i > 0);
9005 mDestroySurface.clear();
9006 }
9007
9008 // Time to remove any exiting tokens?
9009 for (i=mExitingTokens.size()-1; i>=0; i--) {
9010 WindowToken token = mExitingTokens.get(i);
9011 if (!token.hasVisible) {
9012 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009013 if (token.windowType == TYPE_WALLPAPER) {
9014 mWallpaperTokens.remove(token);
9015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009016 }
9017 }
9018
9019 // Time to remove any exiting applications?
9020 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9021 AppWindowToken token = mExitingAppTokens.get(i);
9022 if (!token.hasVisible && !mClosingApps.contains(token)) {
9023 mAppTokens.remove(token);
9024 mExitingAppTokens.remove(i);
9025 }
9026 }
9027
9028 if (focusDisplayed) {
9029 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9030 }
9031 if (animating) {
9032 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9033 }
9034 mQueue.setHoldScreenLocked(holdScreen != null);
9035 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9036 mPowerManager.setScreenBrightnessOverride(-1);
9037 } else {
9038 mPowerManager.setScreenBrightnessOverride((int)
9039 (screenBrightness * Power.BRIGHTNESS_ON));
9040 }
9041 if (holdScreen != mHoldingScreenOn) {
9042 mHoldingScreenOn = holdScreen;
9043 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9044 mH.sendMessage(m);
9045 }
9046 }
9047
9048 void requestAnimationLocked(long delay) {
9049 if (!mAnimationPending) {
9050 mAnimationPending = true;
9051 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9052 }
9053 }
Romain Guy06882f82009-06-10 13:36:04 -07009054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009055 /**
9056 * Have the surface flinger show a surface, robustly dealing with
9057 * error conditions. In particular, if there is not enough memory
9058 * to show the surface, then we will try to get rid of other surfaces
9059 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009060 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009061 * @return Returns true if the surface was successfully shown.
9062 */
9063 boolean showSurfaceRobustlyLocked(WindowState win) {
9064 try {
9065 if (win.mSurface != null) {
9066 win.mSurface.show();
9067 }
9068 return true;
9069 } catch (RuntimeException e) {
9070 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9071 }
Romain Guy06882f82009-06-10 13:36:04 -07009072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009073 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009075 return false;
9076 }
Romain Guy06882f82009-06-10 13:36:04 -07009077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009078 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9079 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009081 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9082 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009084 if (mForceRemoves == null) {
9085 mForceRemoves = new ArrayList<WindowState>();
9086 }
Romain Guy06882f82009-06-10 13:36:04 -07009087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009088 long callingIdentity = Binder.clearCallingIdentity();
9089 try {
9090 // There was some problem... first, do a sanity check of the
9091 // window list to make sure we haven't left any dangling surfaces
9092 // around.
9093 int N = mWindows.size();
9094 boolean leakedSurface = false;
9095 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9096 for (int i=0; i<N; i++) {
9097 WindowState ws = (WindowState)mWindows.get(i);
9098 if (ws.mSurface != null) {
9099 if (!mSessions.contains(ws.mSession)) {
9100 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9101 + ws + " surface=" + ws.mSurface
9102 + " token=" + win.mToken
9103 + " pid=" + ws.mSession.mPid
9104 + " uid=" + ws.mSession.mUid);
9105 ws.mSurface.clear();
9106 ws.mSurface = null;
9107 mForceRemoves.add(ws);
9108 i--;
9109 N--;
9110 leakedSurface = true;
9111 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9112 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9113 + ws + " surface=" + ws.mSurface
9114 + " token=" + win.mAppToken);
9115 ws.mSurface.clear();
9116 ws.mSurface = null;
9117 leakedSurface = true;
9118 }
9119 }
9120 }
Romain Guy06882f82009-06-10 13:36:04 -07009121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009122 boolean killedApps = false;
9123 if (!leakedSurface) {
9124 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9125 SparseIntArray pidCandidates = new SparseIntArray();
9126 for (int i=0; i<N; i++) {
9127 WindowState ws = (WindowState)mWindows.get(i);
9128 if (ws.mSurface != null) {
9129 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9130 }
9131 }
9132 if (pidCandidates.size() > 0) {
9133 int[] pids = new int[pidCandidates.size()];
9134 for (int i=0; i<pids.length; i++) {
9135 pids[i] = pidCandidates.keyAt(i);
9136 }
9137 try {
9138 if (mActivityManager.killPidsForMemory(pids)) {
9139 killedApps = true;
9140 }
9141 } catch (RemoteException e) {
9142 }
9143 }
9144 }
Romain Guy06882f82009-06-10 13:36:04 -07009145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009146 if (leakedSurface || killedApps) {
9147 // We managed to reclaim some memory, so get rid of the trouble
9148 // surface and ask the app to request another one.
9149 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9150 if (surface != null) {
9151 surface.clear();
9152 win.mSurface = null;
9153 }
Romain Guy06882f82009-06-10 13:36:04 -07009154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009155 try {
9156 win.mClient.dispatchGetNewSurface();
9157 } catch (RemoteException e) {
9158 }
9159 }
9160 } finally {
9161 Binder.restoreCallingIdentity(callingIdentity);
9162 }
9163 }
Romain Guy06882f82009-06-10 13:36:04 -07009164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009165 private boolean updateFocusedWindowLocked(int mode) {
9166 WindowState newFocus = computeFocusedWindowLocked();
9167 if (mCurrentFocus != newFocus) {
9168 // This check makes sure that we don't already have the focus
9169 // change message pending.
9170 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9171 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9172 if (localLOGV) Log.v(
9173 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9174 final WindowState oldFocus = mCurrentFocus;
9175 mCurrentFocus = newFocus;
9176 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009178 final WindowState imWindow = mInputMethodWindow;
9179 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009180 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009181 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009182 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9183 mLayoutNeeded = true;
9184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009185 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9186 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009187 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9188 // Client will do the layout, but we need to assign layers
9189 // for handleNewWindowLocked() below.
9190 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009191 }
9192 }
Romain Guy06882f82009-06-10 13:36:04 -07009193
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009194 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9195 mKeyWaiter.handleNewWindowLocked(newFocus);
9196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009197 return true;
9198 }
9199 return false;
9200 }
9201
9202 private WindowState computeFocusedWindowLocked() {
9203 WindowState result = null;
9204 WindowState win;
9205
9206 int i = mWindows.size() - 1;
9207 int nextAppIndex = mAppTokens.size()-1;
9208 WindowToken nextApp = nextAppIndex >= 0
9209 ? mAppTokens.get(nextAppIndex) : null;
9210
9211 while (i >= 0) {
9212 win = (WindowState)mWindows.get(i);
9213
9214 if (localLOGV || DEBUG_FOCUS) Log.v(
9215 TAG, "Looking for focus: " + i
9216 + " = " + win
9217 + ", flags=" + win.mAttrs.flags
9218 + ", canReceive=" + win.canReceiveKeys());
9219
9220 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009222 // If this window's application has been removed, just skip it.
9223 if (thisApp != null && thisApp.removed) {
9224 i--;
9225 continue;
9226 }
Romain Guy06882f82009-06-10 13:36:04 -07009227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 // If there is a focused app, don't allow focus to go to any
9229 // windows below it. If this is an application window, step
9230 // through the app tokens until we find its app.
9231 if (thisApp != null && nextApp != null && thisApp != nextApp
9232 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9233 int origAppIndex = nextAppIndex;
9234 while (nextAppIndex > 0) {
9235 if (nextApp == mFocusedApp) {
9236 // Whoops, we are below the focused app... no focus
9237 // for you!
9238 if (localLOGV || DEBUG_FOCUS) Log.v(
9239 TAG, "Reached focused app: " + mFocusedApp);
9240 return null;
9241 }
9242 nextAppIndex--;
9243 nextApp = mAppTokens.get(nextAppIndex);
9244 if (nextApp == thisApp) {
9245 break;
9246 }
9247 }
9248 if (thisApp != nextApp) {
9249 // Uh oh, the app token doesn't exist! This shouldn't
9250 // happen, but if it does we can get totally hosed...
9251 // so restart at the original app.
9252 nextAppIndex = origAppIndex;
9253 nextApp = mAppTokens.get(nextAppIndex);
9254 }
9255 }
9256
9257 // Dispatch to this window if it is wants key events.
9258 if (win.canReceiveKeys()) {
9259 if (DEBUG_FOCUS) Log.v(
9260 TAG, "Found focus @ " + i + " = " + win);
9261 result = win;
9262 break;
9263 }
9264
9265 i--;
9266 }
9267
9268 return result;
9269 }
9270
9271 private void startFreezingDisplayLocked() {
9272 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009273 // Freezing the display also suspends key event delivery, to
9274 // keep events from going astray while the display is reconfigured.
9275 // If someone has changed orientation again while the screen is
9276 // still frozen, the events will continue to be blocked while the
9277 // successive orientation change is processed. To prevent spurious
9278 // ANRs, we reset the event dispatch timeout in this case.
9279 synchronized (mKeyWaiter) {
9280 mKeyWaiter.mWasFrozen = true;
9281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009282 return;
9283 }
Romain Guy06882f82009-06-10 13:36:04 -07009284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009285 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009287 long now = SystemClock.uptimeMillis();
9288 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9289 if (mFreezeGcPending != 0) {
9290 if (now > (mFreezeGcPending+1000)) {
9291 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9292 mH.removeMessages(H.FORCE_GC);
9293 Runtime.getRuntime().gc();
9294 mFreezeGcPending = now;
9295 }
9296 } else {
9297 mFreezeGcPending = now;
9298 }
Romain Guy06882f82009-06-10 13:36:04 -07009299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300 mDisplayFrozen = true;
9301 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9302 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9303 mAppTransitionReady = true;
9304 }
Romain Guy06882f82009-06-10 13:36:04 -07009305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009306 if (PROFILE_ORIENTATION) {
9307 File file = new File("/data/system/frozen");
9308 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9309 }
9310 Surface.freezeDisplay(0);
9311 }
Romain Guy06882f82009-06-10 13:36:04 -07009312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009313 private void stopFreezingDisplayLocked() {
9314 if (!mDisplayFrozen) {
9315 return;
9316 }
Romain Guy06882f82009-06-10 13:36:04 -07009317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009318 mDisplayFrozen = false;
9319 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9320 if (PROFILE_ORIENTATION) {
9321 Debug.stopMethodTracing();
9322 }
9323 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009324
Chris Tate2ad63a92009-03-25 17:36:48 -07009325 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9326 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 synchronized (mKeyWaiter) {
9328 mKeyWaiter.mWasFrozen = true;
9329 mKeyWaiter.notifyAll();
9330 }
9331
9332 // A little kludge: a lot could have happened while the
9333 // display was frozen, so now that we are coming back we
9334 // do a gc so that any remote references the system
9335 // processes holds on others can be released if they are
9336 // no longer needed.
9337 mH.removeMessages(H.FORCE_GC);
9338 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9339 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009341 mScreenFrozenLock.release();
9342 }
Romain Guy06882f82009-06-10 13:36:04 -07009343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009344 @Override
9345 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9346 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9347 != PackageManager.PERMISSION_GRANTED) {
9348 pw.println("Permission Denial: can't dump WindowManager from from pid="
9349 + Binder.getCallingPid()
9350 + ", uid=" + Binder.getCallingUid());
9351 return;
9352 }
Romain Guy06882f82009-06-10 13:36:04 -07009353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009354 synchronized(mWindowMap) {
9355 pw.println("Current Window Manager state:");
9356 for (int i=mWindows.size()-1; i>=0; i--) {
9357 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009358 pw.print(" Window #"); pw.print(i); pw.print(' ');
9359 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009360 w.dump(pw, " ");
9361 }
9362 if (mInputMethodDialogs.size() > 0) {
9363 pw.println(" ");
9364 pw.println(" Input method dialogs:");
9365 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9366 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009367 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009368 }
9369 }
9370 if (mPendingRemove.size() > 0) {
9371 pw.println(" ");
9372 pw.println(" Remove pending for:");
9373 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9374 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009375 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9376 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009377 w.dump(pw, " ");
9378 }
9379 }
9380 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9381 pw.println(" ");
9382 pw.println(" Windows force removing:");
9383 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9384 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009385 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9386 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009387 w.dump(pw, " ");
9388 }
9389 }
9390 if (mDestroySurface.size() > 0) {
9391 pw.println(" ");
9392 pw.println(" Windows waiting to destroy their surface:");
9393 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9394 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009395 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9396 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397 w.dump(pw, " ");
9398 }
9399 }
9400 if (mLosingFocus.size() > 0) {
9401 pw.println(" ");
9402 pw.println(" Windows losing focus:");
9403 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9404 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009405 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9406 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009407 w.dump(pw, " ");
9408 }
9409 }
9410 if (mSessions.size() > 0) {
9411 pw.println(" ");
9412 pw.println(" All active sessions:");
9413 Iterator<Session> it = mSessions.iterator();
9414 while (it.hasNext()) {
9415 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009416 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009417 s.dump(pw, " ");
9418 }
9419 }
9420 if (mTokenMap.size() > 0) {
9421 pw.println(" ");
9422 pw.println(" All tokens:");
9423 Iterator<WindowToken> it = mTokenMap.values().iterator();
9424 while (it.hasNext()) {
9425 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009426 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 token.dump(pw, " ");
9428 }
9429 }
9430 if (mTokenList.size() > 0) {
9431 pw.println(" ");
9432 pw.println(" Window token list:");
9433 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009434 pw.print(" #"); pw.print(i); pw.print(": ");
9435 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009436 }
9437 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009438 if (mWallpaperTokens.size() > 0) {
9439 pw.println(" ");
9440 pw.println(" Wallpaper tokens:");
9441 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
9442 WindowToken token = mWallpaperTokens.get(i);
9443 pw.print(" Wallpaper #"); pw.print(i);
9444 pw.print(' '); pw.print(token); pw.println(':');
9445 token.dump(pw, " ");
9446 }
9447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009448 if (mAppTokens.size() > 0) {
9449 pw.println(" ");
9450 pw.println(" Application tokens in Z order:");
9451 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009452 pw.print(" App #"); pw.print(i); pw.print(": ");
9453 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009454 }
9455 }
9456 if (mFinishedStarting.size() > 0) {
9457 pw.println(" ");
9458 pw.println(" Finishing start of application tokens:");
9459 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9460 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009461 pw.print(" Finished Starting #"); pw.print(i);
9462 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009463 token.dump(pw, " ");
9464 }
9465 }
9466 if (mExitingTokens.size() > 0) {
9467 pw.println(" ");
9468 pw.println(" Exiting tokens:");
9469 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9470 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009471 pw.print(" Exiting #"); pw.print(i);
9472 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009473 token.dump(pw, " ");
9474 }
9475 }
9476 if (mExitingAppTokens.size() > 0) {
9477 pw.println(" ");
9478 pw.println(" Exiting application tokens:");
9479 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9480 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009481 pw.print(" Exiting App #"); pw.print(i);
9482 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009483 token.dump(pw, " ");
9484 }
9485 }
9486 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009487 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9488 pw.print(" mLastFocus="); pw.println(mLastFocus);
9489 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9490 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9491 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9492 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9493 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9494 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9495 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9496 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009497 if (mDimAnimator != null) {
9498 mDimAnimator.printTo(pw);
9499 } else {
9500 pw.print( " no DimAnimator ");
9501 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009502 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009503 pw.print(mInputMethodAnimLayerAdjustment);
9504 pw.print(" mWallpaperAnimLayerAdjustment=");
9505 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009506 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9507 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9508 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9509 pw.print(" mRotation="); pw.print(mRotation);
9510 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9511 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9512 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9513 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9514 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9515 pw.print(" mNextAppTransition=0x");
9516 pw.print(Integer.toHexString(mNextAppTransition));
9517 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9518 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9519 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9520 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9521 if (mOpeningApps.size() > 0) {
9522 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9523 }
9524 if (mClosingApps.size() > 0) {
9525 pw.print(" mClosingApps="); pw.println(mClosingApps);
9526 }
9527 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9528 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009529 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009530 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9531 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9532 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9533 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9534 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9535 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009536 }
9537 }
9538
9539 public void monitor() {
9540 synchronized (mWindowMap) { }
9541 synchronized (mKeyguardDisabled) { }
9542 synchronized (mKeyWaiter) { }
9543 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009544
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07009545 public void virtualKeyFeedback(KeyEvent event) {
9546 mPolicy.keyFeedbackFromInput(event);
9547 }
9548
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009549 /**
9550 * DimAnimator class that controls the dim animation. This holds the surface and
9551 * all state used for dim animation.
9552 */
9553 private static class DimAnimator {
9554 Surface mDimSurface;
9555 boolean mDimShown = false;
9556 float mDimCurrentAlpha;
9557 float mDimTargetAlpha;
9558 float mDimDeltaPerMs;
9559 long mLastDimAnimTime;
9560
9561 DimAnimator (SurfaceSession session) {
9562 if (mDimSurface == null) {
9563 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9564 + mDimSurface + ": CREATE");
9565 try {
9566 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9567 Surface.FX_SURFACE_DIM);
9568 } catch (Exception e) {
9569 Log.e(TAG, "Exception creating Dim surface", e);
9570 }
9571 }
9572 }
9573
9574 /**
9575 * Show the dim surface.
9576 */
9577 void show(int dw, int dh) {
9578 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9579 dw + "x" + dh + ")");
9580 mDimShown = true;
9581 try {
9582 mDimSurface.setPosition(0, 0);
9583 mDimSurface.setSize(dw, dh);
9584 mDimSurface.show();
9585 } catch (RuntimeException e) {
9586 Log.w(TAG, "Failure showing dim surface", e);
9587 }
9588 }
9589
9590 /**
9591 * Set's the dim surface's layer and update dim parameters that will be used in
9592 * {@link updateSurface} after all windows are examined.
9593 */
9594 void updateParameters(WindowState w, long currentTime) {
9595 mDimSurface.setLayer(w.mAnimLayer-1);
9596
9597 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9598 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9599 if (mDimTargetAlpha != target) {
9600 // If the desired dim level has changed, then
9601 // start an animation to it.
9602 mLastDimAnimTime = currentTime;
9603 long duration = (w.mAnimating && w.mAnimation != null)
9604 ? w.mAnimation.computeDurationHint()
9605 : DEFAULT_DIM_DURATION;
9606 if (target > mDimTargetAlpha) {
9607 // This is happening behind the activity UI,
9608 // so we can make it run a little longer to
9609 // give a stronger impression without disrupting
9610 // the user.
9611 duration *= DIM_DURATION_MULTIPLIER;
9612 }
9613 if (duration < 1) {
9614 // Don't divide by zero
9615 duration = 1;
9616 }
9617 mDimTargetAlpha = target;
9618 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9619 }
9620 }
9621
9622 /**
9623 * Updating the surface's alpha. Returns true if the animation continues, or returns
9624 * false when the animation is finished and the dim surface is hidden.
9625 */
9626 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9627 if (!dimming) {
9628 if (mDimTargetAlpha != 0) {
9629 mLastDimAnimTime = currentTime;
9630 mDimTargetAlpha = 0;
9631 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9632 }
9633 }
9634
9635 boolean animating = false;
9636 if (mLastDimAnimTime != 0) {
9637 mDimCurrentAlpha += mDimDeltaPerMs
9638 * (currentTime-mLastDimAnimTime);
9639 boolean more = true;
9640 if (displayFrozen) {
9641 // If the display is frozen, there is no reason to animate.
9642 more = false;
9643 } else if (mDimDeltaPerMs > 0) {
9644 if (mDimCurrentAlpha > mDimTargetAlpha) {
9645 more = false;
9646 }
9647 } else if (mDimDeltaPerMs < 0) {
9648 if (mDimCurrentAlpha < mDimTargetAlpha) {
9649 more = false;
9650 }
9651 } else {
9652 more = false;
9653 }
9654
9655 // Do we need to continue animating?
9656 if (more) {
9657 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9658 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9659 mLastDimAnimTime = currentTime;
9660 mDimSurface.setAlpha(mDimCurrentAlpha);
9661 animating = true;
9662 } else {
9663 mDimCurrentAlpha = mDimTargetAlpha;
9664 mLastDimAnimTime = 0;
9665 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9666 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9667 mDimSurface.setAlpha(mDimCurrentAlpha);
9668 if (!dimming) {
9669 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9670 + ": HIDE");
9671 try {
9672 mDimSurface.hide();
9673 } catch (RuntimeException e) {
9674 Log.w(TAG, "Illegal argument exception hiding dim surface");
9675 }
9676 mDimShown = false;
9677 }
9678 }
9679 }
9680 return animating;
9681 }
9682
9683 public void printTo(PrintWriter pw) {
9684 pw.print(" mDimShown="); pw.print(mDimShown);
9685 pw.print(" current="); pw.print(mDimCurrentAlpha);
9686 pw.print(" target="); pw.print(mDimTargetAlpha);
9687 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9688 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9689 }
9690 }
9691
9692 /**
9693 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9694 * This is used for opening/closing transition for apps in compatible mode.
9695 */
9696 private static class FadeInOutAnimation extends Animation {
9697 int mWidth;
9698 boolean mFadeIn;
9699
9700 public FadeInOutAnimation(boolean fadeIn) {
9701 setInterpolator(new AccelerateInterpolator());
9702 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9703 mFadeIn = fadeIn;
9704 }
9705
9706 @Override
9707 protected void applyTransformation(float interpolatedTime, Transformation t) {
9708 float x = interpolatedTime;
9709 if (!mFadeIn) {
9710 x = 1.0f - x; // reverse the interpolation for fade out
9711 }
9712 if (x < 0.5) {
9713 // move the window out of the screen.
9714 t.getMatrix().setTranslate(mWidth, 0);
9715 } else {
9716 t.getMatrix().setTranslate(0, 0);// show
9717 t.setAlpha((x - 0.5f) * 2);
9718 }
9719 }
9720
9721 @Override
9722 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9723 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9724 mWidth = width;
9725 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009726
9727 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07009728 public int getZAdjustment() {
9729 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009730 }
9731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732}