blob: e41524e4aebd261102f630b558412b3bf8c70c8a [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;
30import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
31import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
32import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
33import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
34import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
35import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_GPU;
36import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_HARDWARE;
37import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
38import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
39import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
40import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
41import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
42
43import com.android.internal.app.IBatteryStats;
44import com.android.internal.policy.PolicyManager;
45import com.android.internal.view.IInputContext;
46import com.android.internal.view.IInputMethodClient;
47import com.android.internal.view.IInputMethodManager;
48import com.android.server.KeyInputQueue.QueuedEvent;
49import com.android.server.am.BatteryStatsService;
50
51import android.Manifest;
52import android.app.ActivityManagerNative;
53import android.app.IActivityManager;
54import android.content.Context;
55import android.content.pm.ActivityInfo;
56import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070057import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.res.Configuration;
59import android.graphics.Matrix;
60import android.graphics.PixelFormat;
61import android.graphics.Rect;
62import android.graphics.Region;
63import android.os.BatteryStats;
64import android.os.Binder;
65import android.os.Debug;
66import android.os.Handler;
67import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070068import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.LocalPowerManager;
70import android.os.Looper;
71import android.os.Message;
72import android.os.Parcel;
73import android.os.ParcelFileDescriptor;
74import android.os.Power;
75import android.os.PowerManager;
76import android.os.Process;
77import android.os.RemoteException;
78import android.os.ServiceManager;
79import android.os.SystemClock;
80import android.os.SystemProperties;
81import android.os.TokenWatcher;
82import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070083import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.EventLog;
85import android.util.Log;
86import android.util.SparseIntArray;
87import android.view.Display;
88import android.view.Gravity;
89import android.view.IApplicationToken;
90import android.view.IOnKeyguardExitResult;
91import android.view.IRotationWatcher;
92import android.view.IWindow;
93import android.view.IWindowManager;
94import android.view.IWindowSession;
95import android.view.KeyEvent;
96import android.view.MotionEvent;
97import android.view.RawInputEvent;
98import android.view.Surface;
99import android.view.SurfaceSession;
100import android.view.View;
101import android.view.ViewTreeObserver;
102import android.view.WindowManager;
103import android.view.WindowManagerImpl;
104import android.view.WindowManagerPolicy;
105import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700106import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.view.animation.Animation;
108import android.view.animation.AnimationUtils;
109import android.view.animation.Transformation;
110
111import java.io.BufferedWriter;
112import java.io.File;
113import java.io.FileDescriptor;
114import java.io.IOException;
115import java.io.OutputStream;
116import java.io.OutputStreamWriter;
117import java.io.PrintWriter;
118import java.io.StringWriter;
119import java.net.Socket;
120import java.util.ArrayList;
121import java.util.HashMap;
122import java.util.HashSet;
123import java.util.Iterator;
124import java.util.List;
125
126/** {@hide} */
127public class WindowManagerService extends IWindowManager.Stub implements Watchdog.Monitor {
128 static final String TAG = "WindowManager";
129 static final boolean DEBUG = false;
130 static final boolean DEBUG_FOCUS = false;
131 static final boolean DEBUG_ANIM = false;
132 static final boolean DEBUG_LAYERS = false;
133 static final boolean DEBUG_INPUT = false;
134 static final boolean DEBUG_INPUT_METHOD = false;
135 static final boolean DEBUG_VISIBILITY = false;
136 static final boolean DEBUG_ORIENTATION = false;
137 static final boolean DEBUG_APP_TRANSITIONS = false;
138 static final boolean DEBUG_STARTING_WINDOW = false;
139 static final boolean DEBUG_REORDER = false;
140 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700141 static final boolean MEASURE_LATENCY = false;
142 static private LatencyTimer lt;
143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 static final boolean PROFILE_ORIENTATION = false;
145 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700146 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 /** How long to wait for first key repeat, in milliseconds */
151 static final int KEY_REPEAT_FIRST_DELAY = 750;
Romain Guy06882f82009-06-10 13:36:04 -0700152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /** How long to wait for subsequent key repeats, in milliseconds */
154 static final int KEY_REPEAT_DELAY = 50;
155
156 /** How much to multiply the policy's type layer, to reserve room
157 * for multiple windows of the same type and Z-ordering adjustment
158 * with TYPE_LAYER_OFFSET. */
159 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
162 * or below others in the same layer. */
163 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /** How much to increment the layer for each window, to reserve room
166 * for effect surfaces between them.
167 */
168 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 /** The maximum length we will accept for a loaded animation duration:
171 * this is 10 seconds.
172 */
173 static final int MAX_ANIMATION_DURATION = 10*1000;
174
175 /** Amount of time (in milliseconds) to animate the dim surface from one
176 * value to another, when no window animation is driving it.
177 */
178 static final int DEFAULT_DIM_DURATION = 200;
179
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700180 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
181 * compatible windows.
182 */
183 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 /** Adjustment to time to perform a dim, to make it more dramatic.
186 */
187 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700188
Dianne Hackborncfaef692009-06-15 14:24:44 -0700189 static final int INJECT_FAILED = 0;
190 static final int INJECT_SUCCEEDED = 1;
191 static final int INJECT_NO_PERMISSION = -1;
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 static final int UPDATE_FOCUS_NORMAL = 0;
194 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
195 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
196 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700197
Michael Chane96440f2009-05-06 10:27:36 -0700198 /** The minimum time between dispatching touch events. */
199 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
200
201 // Last touch event time
202 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700203
Michael Chane96440f2009-05-06 10:27:36 -0700204 // Last touch event type
205 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700206
Michael Chane96440f2009-05-06 10:27:36 -0700207 // Time to wait before calling useractivity again. This saves CPU usage
208 // when we get a flood of touch events.
209 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
210
211 // Last time we call user activity
212 long mLastUserActivityCallTime = 0;
213
Romain Guy06882f82009-06-10 13:36:04 -0700214 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700215 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700218 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
220 /**
221 * Condition waited on by {@link #reenableKeyguard} to know the call to
222 * the window policy has finished.
223 */
224 private boolean mWaitingUntilKeyguardReenabled = false;
225
226
227 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
228 new Handler(), "WindowManagerService.mKeyguardDisabled") {
229 public void acquired() {
230 mPolicy.enableKeyguard(false);
231 }
232 public void released() {
233 synchronized (mKeyguardDisabled) {
234 mPolicy.enableKeyguard(true);
235 mWaitingUntilKeyguardReenabled = false;
236 mKeyguardDisabled.notifyAll();
237 }
238 }
239 };
240
241 final Context mContext;
242
243 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
248
249 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 /**
254 * All currently active sessions with clients.
255 */
256 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 /**
259 * Mapping from an IWindow IBinder to the server's Window object.
260 * This is also used as the lock for all of our state.
261 */
262 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
263
264 /**
265 * Mapping from a token IBinder to a WindowToken object.
266 */
267 final HashMap<IBinder, WindowToken> mTokenMap =
268 new HashMap<IBinder, WindowToken>();
269
270 /**
271 * The same tokens as mTokenMap, stored in a list for efficient iteration
272 * over them.
273 */
274 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
277 * Window tokens that are in the process of exiting, but still
278 * on screen for animations.
279 */
280 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
281
282 /**
283 * Z-ordered (bottom-most first) list of all application tokens, for
284 * controlling the ordering of windows in different applications. This
285 * contains WindowToken objects.
286 */
287 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
288
289 /**
290 * Application tokens that are in the process of exiting, but still
291 * on screen for animations.
292 */
293 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
294
295 /**
296 * List of window tokens that have finished starting their application,
297 * and now need to have the policy remove their windows.
298 */
299 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
300
301 /**
302 * Z-ordered (bottom-most first) list of all Window objects.
303 */
304 final ArrayList mWindows = new ArrayList();
305
306 /**
307 * Windows that are being resized. Used so we can tell the client about
308 * the resize after closing the transaction in which we resized the
309 * underlying surface.
310 */
311 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
312
313 /**
314 * Windows whose animations have ended and now must be removed.
315 */
316 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
317
318 /**
319 * Windows whose surface should be destroyed.
320 */
321 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
322
323 /**
324 * Windows that have lost input focus and are waiting for the new
325 * focus window to be displayed before they are told about this.
326 */
327 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
328
329 /**
330 * This is set when we have run out of memory, and will either be an empty
331 * list or contain windows that need to be force removed.
332 */
333 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700338 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 Surface mBlurSurface;
340 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 final float[] mTmpFloats = new float[9];
345
346 boolean mSafeMode;
347 boolean mDisplayEnabled = false;
348 boolean mSystemBooted = false;
349 int mRotation = 0;
350 int mRequestedRotation = 0;
351 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700352 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 ArrayList<IRotationWatcher> mRotationWatchers
354 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 boolean mLayoutNeeded = true;
357 boolean mAnimationPending = false;
358 boolean mDisplayFrozen = false;
359 boolean mWindowsFreezingScreen = false;
360 long mFreezeGcPending = 0;
361 int mAppsFreezingScreen = 0;
362
363 // This is held as long as we have the screen frozen, to give us time to
364 // perform a rotation animation when turning off shows the lock screen which
365 // changes the orientation.
366 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 // State management of app transitions. When we are preparing for a
369 // transition, mNextAppTransition will be the kind of transition to
370 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
371 // mOpeningApps and mClosingApps are the lists of tokens that will be
372 // made visible or hidden at the next transition.
373 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
374 boolean mAppTransitionReady = false;
375 boolean mAppTransitionTimeout = false;
376 boolean mStartingIconInTransition = false;
377 boolean mSkipAppTransitionAnimation = false;
378 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
379 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 //flag to detect fat touch events
382 boolean mFatTouch = false;
383 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 H mH = new H();
386
387 WindowState mCurrentFocus = null;
388 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 // This just indicates the window the input method is on top of, not
391 // necessarily the window its input is going to.
392 WindowState mInputMethodTarget = null;
393 WindowState mUpcomingInputMethodTarget = null;
394 boolean mInputMethodTargetWaitingAnim;
395 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 WindowState mInputMethodWindow = null;
398 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
399
400 AppWindowToken mFocusedApp = null;
401
402 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 float mWindowAnimationScale = 1.0f;
405 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 final KeyWaiter mKeyWaiter = new KeyWaiter();
408 final KeyQ mQueue;
409 final InputDispatcherThread mInputThread;
410
411 // Who is holding the screen on.
412 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
415 * Whether the UI is currently running in touch mode (not showing
416 * navigational focus because the user is directly pressing the screen).
417 */
418 boolean mInTouchMode = false;
419
420 private ViewServer mViewServer;
421
422 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700423
Dianne Hackbornc485a602009-03-24 22:39:49 -0700424 final Configuration mTempConfiguration = new Configuration();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700425 int screenLayout = Configuration.SCREENLAYOUT_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700426
427 // The frame use to limit the size of the app running in compatibility mode.
428 Rect mCompatibleScreenFrame = new Rect();
429 // The surface used to fill the outer rim of the app running in compatibility mode.
430 Surface mBackgroundFillerSurface = null;
431 boolean mBackgroundFillerShown = false;
432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 public static WindowManagerService main(Context context,
434 PowerManagerService pm, boolean haveInputMethods) {
435 WMThread thr = new WMThread(context, pm, haveInputMethods);
436 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 synchronized (thr) {
439 while (thr.mService == null) {
440 try {
441 thr.wait();
442 } catch (InterruptedException e) {
443 }
444 }
445 }
Romain Guy06882f82009-06-10 13:36:04 -0700446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 return thr.mService;
448 }
Romain Guy06882f82009-06-10 13:36:04 -0700449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 static class WMThread extends Thread {
451 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 private final Context mContext;
454 private final PowerManagerService mPM;
455 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 public WMThread(Context context, PowerManagerService pm,
458 boolean haveInputMethods) {
459 super("WindowManager");
460 mContext = context;
461 mPM = pm;
462 mHaveInputMethods = haveInputMethods;
463 }
Romain Guy06882f82009-06-10 13:36:04 -0700464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 public void run() {
466 Looper.prepare();
467 WindowManagerService s = new WindowManagerService(mContext, mPM,
468 mHaveInputMethods);
469 android.os.Process.setThreadPriority(
470 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 synchronized (this) {
473 mService = s;
474 notifyAll();
475 }
Romain Guy06882f82009-06-10 13:36:04 -0700476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 Looper.loop();
478 }
479 }
480
481 static class PolicyThread extends Thread {
482 private final WindowManagerPolicy mPolicy;
483 private final WindowManagerService mService;
484 private final Context mContext;
485 private final PowerManagerService mPM;
486 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 public PolicyThread(WindowManagerPolicy policy,
489 WindowManagerService service, Context context,
490 PowerManagerService pm) {
491 super("WindowManagerPolicy");
492 mPolicy = policy;
493 mService = service;
494 mContext = context;
495 mPM = pm;
496 }
Romain Guy06882f82009-06-10 13:36:04 -0700497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 public void run() {
499 Looper.prepare();
500 //Looper.myLooper().setMessageLogging(new LogPrinter(
501 // Log.VERBOSE, "WindowManagerPolicy"));
502 android.os.Process.setThreadPriority(
503 android.os.Process.THREAD_PRIORITY_FOREGROUND);
504 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 synchronized (this) {
507 mRunning = true;
508 notifyAll();
509 }
Romain Guy06882f82009-06-10 13:36:04 -0700510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 Looper.loop();
512 }
513 }
514
515 private WindowManagerService(Context context, PowerManagerService pm,
516 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700517 if (MEASURE_LATENCY) {
518 lt = new LatencyTimer(100, 1000);
519 }
520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 mContext = context;
522 mHaveInputMethods = haveInputMethods;
523 mLimitedAlphaCompositing = context.getResources().getBoolean(
524 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 mPowerManager = pm;
527 mPowerManager.setPolicy(mPolicy);
528 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
529 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
530 "SCREEN_FROZEN");
531 mScreenFrozenLock.setReferenceCounted(false);
532
533 mActivityManager = ActivityManagerNative.getDefault();
534 mBatteryStats = BatteryStatsService.getService();
535
536 // Get persisted window scale setting
537 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
538 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
539 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
540 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 mQueue = new KeyQ();
543
544 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
547 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 synchronized (thr) {
550 while (!thr.mRunning) {
551 try {
552 thr.wait();
553 } catch (InterruptedException e) {
554 }
555 }
556 }
Romain Guy06882f82009-06-10 13:36:04 -0700557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 // Add ourself to the Watchdog monitors.
561 Watchdog.getInstance().addMonitor(this);
562 }
563
564 @Override
565 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
566 throws RemoteException {
567 try {
568 return super.onTransact(code, data, reply, flags);
569 } catch (RuntimeException e) {
570 // The window manager only throws security exceptions, so let's
571 // log all others.
572 if (!(e instanceof SecurityException)) {
573 Log.e(TAG, "Window Manager Crash", e);
574 }
575 throw e;
576 }
577 }
578
579 private void placeWindowAfter(Object pos, WindowState window) {
580 final int i = mWindows.indexOf(pos);
581 if (localLOGV || DEBUG_FOCUS) Log.v(
582 TAG, "Adding window " + window + " at "
583 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
584 mWindows.add(i+1, window);
585 }
586
587 private void placeWindowBefore(Object pos, WindowState window) {
588 final int i = mWindows.indexOf(pos);
589 if (localLOGV || DEBUG_FOCUS) Log.v(
590 TAG, "Adding window " + window + " at "
591 + i + " of " + mWindows.size() + " (before " + pos + ")");
592 mWindows.add(i, window);
593 }
594
595 //This method finds out the index of a window that has the same app token as
596 //win. used for z ordering the windows in mWindows
597 private int findIdxBasedOnAppTokens(WindowState win) {
598 //use a local variable to cache mWindows
599 ArrayList localmWindows = mWindows;
600 int jmax = localmWindows.size();
601 if(jmax == 0) {
602 return -1;
603 }
604 for(int j = (jmax-1); j >= 0; j--) {
605 WindowState wentry = (WindowState)localmWindows.get(j);
606 if(wentry.mAppToken == win.mAppToken) {
607 return j;
608 }
609 }
610 return -1;
611 }
Romain Guy06882f82009-06-10 13:36:04 -0700612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
614 final IWindow client = win.mClient;
615 final WindowToken token = win.mToken;
616 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 final int N = localmWindows.size();
619 final WindowState attached = win.mAttachedWindow;
620 int i;
621 if (attached == null) {
622 int tokenWindowsPos = token.windows.size();
623 if (token.appWindowToken != null) {
624 int index = tokenWindowsPos-1;
625 if (index >= 0) {
626 // If this application has existing windows, we
627 // simply place the new window on top of them... but
628 // keep the starting window on top.
629 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
630 // Base windows go behind everything else.
631 placeWindowBefore(token.windows.get(0), win);
632 tokenWindowsPos = 0;
633 } else {
634 AppWindowToken atoken = win.mAppToken;
635 if (atoken != null &&
636 token.windows.get(index) == atoken.startingWindow) {
637 placeWindowBefore(token.windows.get(index), win);
638 tokenWindowsPos--;
639 } else {
640 int newIdx = findIdxBasedOnAppTokens(win);
641 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700642 //there is a window above this one associated with the same
643 //apptoken note that the window could be a floating window
644 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 //windows associated with this token.
646 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 }
649 }
650 } else {
651 if (localLOGV) Log.v(
652 TAG, "Figuring out where to add app window "
653 + client.asBinder() + " (token=" + token + ")");
654 // Figure out where the window should go, based on the
655 // order of applications.
656 final int NA = mAppTokens.size();
657 Object pos = null;
658 for (i=NA-1; i>=0; i--) {
659 AppWindowToken t = mAppTokens.get(i);
660 if (t == token) {
661 i--;
662 break;
663 }
664 if (t.windows.size() > 0) {
665 pos = t.windows.get(0);
666 }
667 }
668 // We now know the index into the apps. If we found
669 // an app window above, that gives us the position; else
670 // we need to look some more.
671 if (pos != null) {
672 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700673 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 mTokenMap.get(((WindowState)pos).mClient.asBinder());
675 if (atoken != null) {
676 final int NC = atoken.windows.size();
677 if (NC > 0) {
678 WindowState bottom = atoken.windows.get(0);
679 if (bottom.mSubLayer < 0) {
680 pos = bottom;
681 }
682 }
683 }
684 placeWindowBefore(pos, win);
685 } else {
686 while (i >= 0) {
687 AppWindowToken t = mAppTokens.get(i);
688 final int NW = t.windows.size();
689 if (NW > 0) {
690 pos = t.windows.get(NW-1);
691 break;
692 }
693 i--;
694 }
695 if (pos != null) {
696 // Move in front of any windows attached to this
697 // one.
698 WindowToken atoken =
699 mTokenMap.get(((WindowState)pos).mClient.asBinder());
700 if (atoken != null) {
701 final int NC = atoken.windows.size();
702 if (NC > 0) {
703 WindowState top = atoken.windows.get(NC-1);
704 if (top.mSubLayer >= 0) {
705 pos = top;
706 }
707 }
708 }
709 placeWindowAfter(pos, win);
710 } else {
711 // Just search for the start of this layer.
712 final int myLayer = win.mBaseLayer;
713 for (i=0; i<N; i++) {
714 WindowState w = (WindowState)localmWindows.get(i);
715 if (w.mBaseLayer > myLayer) {
716 break;
717 }
718 }
719 if (localLOGV || DEBUG_FOCUS) Log.v(
720 TAG, "Adding window " + win + " at "
721 + i + " of " + N);
722 localmWindows.add(i, win);
723 }
724 }
725 }
726 } else {
727 // Figure out where window should go, based on layer.
728 final int myLayer = win.mBaseLayer;
729 for (i=N-1; i>=0; i--) {
730 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
731 i++;
732 break;
733 }
734 }
735 if (i < 0) i = 0;
736 if (localLOGV || DEBUG_FOCUS) Log.v(
737 TAG, "Adding window " + win + " at "
738 + i + " of " + N);
739 localmWindows.add(i, win);
740 }
741 if (addToToken) {
742 token.windows.add(tokenWindowsPos, win);
743 }
744
745 } else {
746 // Figure out this window's ordering relative to the window
747 // it is attached to.
748 final int NA = token.windows.size();
749 final int sublayer = win.mSubLayer;
750 int largestSublayer = Integer.MIN_VALUE;
751 WindowState windowWithLargestSublayer = null;
752 for (i=0; i<NA; i++) {
753 WindowState w = token.windows.get(i);
754 final int wSublayer = w.mSubLayer;
755 if (wSublayer >= largestSublayer) {
756 largestSublayer = wSublayer;
757 windowWithLargestSublayer = w;
758 }
759 if (sublayer < 0) {
760 // For negative sublayers, we go below all windows
761 // in the same sublayer.
762 if (wSublayer >= sublayer) {
763 if (addToToken) {
764 token.windows.add(i, win);
765 }
766 placeWindowBefore(
767 wSublayer >= 0 ? attached : w, win);
768 break;
769 }
770 } else {
771 // For positive sublayers, we go above all windows
772 // in the same sublayer.
773 if (wSublayer > sublayer) {
774 if (addToToken) {
775 token.windows.add(i, win);
776 }
777 placeWindowBefore(w, win);
778 break;
779 }
780 }
781 }
782 if (i >= NA) {
783 if (addToToken) {
784 token.windows.add(win);
785 }
786 if (sublayer < 0) {
787 placeWindowBefore(attached, win);
788 } else {
789 placeWindowAfter(largestSublayer >= 0
790 ? windowWithLargestSublayer
791 : attached,
792 win);
793 }
794 }
795 }
Romain Guy06882f82009-06-10 13:36:04 -0700796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 if (win.mAppToken != null && addToToken) {
798 win.mAppToken.allAppWindows.add(win);
799 }
800 }
Romain Guy06882f82009-06-10 13:36:04 -0700801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 static boolean canBeImeTarget(WindowState w) {
803 final int fl = w.mAttrs.flags
804 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
805 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
806 return w.isVisibleOrAdding();
807 }
808 return false;
809 }
Romain Guy06882f82009-06-10 13:36:04 -0700810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
812 final ArrayList localmWindows = mWindows;
813 final int N = localmWindows.size();
814 WindowState w = null;
815 int i = N;
816 while (i > 0) {
817 i--;
818 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
821 // + Integer.toHexString(w.mAttrs.flags));
822 if (canBeImeTarget(w)) {
823 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 // Yet more tricksyness! If this window is a "starting"
826 // window, we do actually want to be on top of it, but
827 // it is not -really- where input will go. So if the caller
828 // is not actually looking to move the IME, look down below
829 // for a real window to target...
830 if (!willMove
831 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
832 && i > 0) {
833 WindowState wb = (WindowState)localmWindows.get(i-1);
834 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
835 i--;
836 w = wb;
837 }
838 }
839 break;
840 }
841 }
Romain Guy06882f82009-06-10 13:36:04 -0700842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
846 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 if (willMove && w != null) {
849 final WindowState curTarget = mInputMethodTarget;
850 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 // Now some fun for dealing with window animations that
853 // modify the Z order. We need to look at all windows below
854 // the current target that are in this app, finding the highest
855 // visible one in layering.
856 AppWindowToken token = curTarget.mAppToken;
857 WindowState highestTarget = null;
858 int highestPos = 0;
859 if (token.animating || token.animation != null) {
860 int pos = 0;
861 pos = localmWindows.indexOf(curTarget);
862 while (pos >= 0) {
863 WindowState win = (WindowState)localmWindows.get(pos);
864 if (win.mAppToken != token) {
865 break;
866 }
867 if (!win.mRemoved) {
868 if (highestTarget == null || win.mAnimLayer >
869 highestTarget.mAnimLayer) {
870 highestTarget = win;
871 highestPos = pos;
872 }
873 }
874 pos--;
875 }
876 }
Romain Guy06882f82009-06-10 13:36:04 -0700877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700879 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 + mNextAppTransition + " " + highestTarget
881 + " animating=" + highestTarget.isAnimating()
882 + " layer=" + highestTarget.mAnimLayer
883 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
886 // If we are currently setting up for an animation,
887 // hold everything until we can find out what will happen.
888 mInputMethodTargetWaitingAnim = true;
889 mInputMethodTarget = highestTarget;
890 return highestPos + 1;
891 } else if (highestTarget.isAnimating() &&
892 highestTarget.mAnimLayer > w.mAnimLayer) {
893 // If the window we are currently targeting is involved
894 // with an animation, and it is on top of the next target
895 // we will be over, then hold off on moving until
896 // that is done.
897 mInputMethodTarget = highestTarget;
898 return highestPos + 1;
899 }
900 }
901 }
902 }
Romain Guy06882f82009-06-10 13:36:04 -0700903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 //Log.i(TAG, "Placing input method @" + (i+1));
905 if (w != null) {
906 if (willMove) {
907 RuntimeException e = new RuntimeException();
908 e.fillInStackTrace();
909 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
910 + mInputMethodTarget + " to " + w, e);
911 mInputMethodTarget = w;
912 if (w.mAppToken != null) {
913 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
914 } else {
915 setInputMethodAnimLayerAdjustment(0);
916 }
917 }
918 return i+1;
919 }
920 if (willMove) {
921 RuntimeException e = new RuntimeException();
922 e.fillInStackTrace();
923 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
924 + mInputMethodTarget + " to null", e);
925 mInputMethodTarget = null;
926 setInputMethodAnimLayerAdjustment(0);
927 }
928 return -1;
929 }
Romain Guy06882f82009-06-10 13:36:04 -0700930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 void addInputMethodWindowToListLocked(WindowState win) {
932 int pos = findDesiredInputMethodWindowIndexLocked(true);
933 if (pos >= 0) {
934 win.mTargetAppToken = mInputMethodTarget.mAppToken;
935 mWindows.add(pos, win);
936 moveInputMethodDialogsLocked(pos+1);
937 return;
938 }
939 win.mTargetAppToken = null;
940 addWindowToListInOrderLocked(win, true);
941 moveInputMethodDialogsLocked(pos);
942 }
Romain Guy06882f82009-06-10 13:36:04 -0700943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 void setInputMethodAnimLayerAdjustment(int adj) {
945 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
946 mInputMethodAnimLayerAdjustment = adj;
947 WindowState imw = mInputMethodWindow;
948 if (imw != null) {
949 imw.mAnimLayer = imw.mLayer + adj;
950 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
951 + " anim layer: " + imw.mAnimLayer);
952 int wi = imw.mChildWindows.size();
953 while (wi > 0) {
954 wi--;
955 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
956 cw.mAnimLayer = cw.mLayer + adj;
957 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
958 + " anim layer: " + cw.mAnimLayer);
959 }
960 }
961 int di = mInputMethodDialogs.size();
962 while (di > 0) {
963 di --;
964 imw = mInputMethodDialogs.get(di);
965 imw.mAnimLayer = imw.mLayer + adj;
966 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
967 + " anim layer: " + imw.mAnimLayer);
968 }
969 }
Romain Guy06882f82009-06-10 13:36:04 -0700970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
972 int wpos = mWindows.indexOf(win);
973 if (wpos >= 0) {
974 if (wpos < interestingPos) interestingPos--;
975 mWindows.remove(wpos);
976 int NC = win.mChildWindows.size();
977 while (NC > 0) {
978 NC--;
979 WindowState cw = (WindowState)win.mChildWindows.get(NC);
980 int cpos = mWindows.indexOf(cw);
981 if (cpos >= 0) {
982 if (cpos < interestingPos) interestingPos--;
983 mWindows.remove(cpos);
984 }
985 }
986 }
987 return interestingPos;
988 }
Romain Guy06882f82009-06-10 13:36:04 -0700989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 private void reAddWindowToListInOrderLocked(WindowState win) {
991 addWindowToListInOrderLocked(win, false);
992 // This is a hack to get all of the child windows added as well
993 // at the right position. Child windows should be rare and
994 // this case should be rare, so it shouldn't be that big a deal.
995 int wpos = mWindows.indexOf(win);
996 if (wpos >= 0) {
997 mWindows.remove(wpos);
998 reAddWindowLocked(wpos, win);
999 }
1000 }
Romain Guy06882f82009-06-10 13:36:04 -07001001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 void logWindowList(String prefix) {
1003 int N = mWindows.size();
1004 while (N > 0) {
1005 N--;
1006 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1007 }
1008 }
Romain Guy06882f82009-06-10 13:36:04 -07001009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 void moveInputMethodDialogsLocked(int pos) {
1011 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 final int N = dialogs.size();
1014 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1015 for (int i=0; i<N; i++) {
1016 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1017 }
1018 if (DEBUG_INPUT_METHOD) {
1019 Log.v(TAG, "Window list w/pos=" + pos);
1020 logWindowList(" ");
1021 }
Romain Guy06882f82009-06-10 13:36:04 -07001022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 if (pos >= 0) {
1024 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1025 if (pos < mWindows.size()) {
1026 WindowState wp = (WindowState)mWindows.get(pos);
1027 if (wp == mInputMethodWindow) {
1028 pos++;
1029 }
1030 }
1031 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1032 for (int i=0; i<N; i++) {
1033 WindowState win = dialogs.get(i);
1034 win.mTargetAppToken = targetAppToken;
1035 pos = reAddWindowLocked(pos, win);
1036 }
1037 if (DEBUG_INPUT_METHOD) {
1038 Log.v(TAG, "Final window list:");
1039 logWindowList(" ");
1040 }
1041 return;
1042 }
1043 for (int i=0; i<N; i++) {
1044 WindowState win = dialogs.get(i);
1045 win.mTargetAppToken = null;
1046 reAddWindowToListInOrderLocked(win);
1047 if (DEBUG_INPUT_METHOD) {
1048 Log.v(TAG, "No IM target, final list:");
1049 logWindowList(" ");
1050 }
1051 }
1052 }
Romain Guy06882f82009-06-10 13:36:04 -07001053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1055 final WindowState imWin = mInputMethodWindow;
1056 final int DN = mInputMethodDialogs.size();
1057 if (imWin == null && DN == 0) {
1058 return false;
1059 }
Romain Guy06882f82009-06-10 13:36:04 -07001060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1062 if (imPos >= 0) {
1063 // In this case, the input method windows are to be placed
1064 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 // First check to see if the input method windows are already
1067 // located here, and contiguous.
1068 final int N = mWindows.size();
1069 WindowState firstImWin = imPos < N
1070 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 // Figure out the actual input method window that should be
1073 // at the bottom of their stack.
1074 WindowState baseImWin = imWin != null
1075 ? imWin : mInputMethodDialogs.get(0);
1076 if (baseImWin.mChildWindows.size() > 0) {
1077 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1078 if (cw.mSubLayer < 0) baseImWin = cw;
1079 }
Romain Guy06882f82009-06-10 13:36:04 -07001080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 if (firstImWin == baseImWin) {
1082 // The windows haven't moved... but are they still contiguous?
1083 // First find the top IM window.
1084 int pos = imPos+1;
1085 while (pos < N) {
1086 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1087 break;
1088 }
1089 pos++;
1090 }
1091 pos++;
1092 // Now there should be no more input method windows above.
1093 while (pos < N) {
1094 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1095 break;
1096 }
1097 pos++;
1098 }
1099 if (pos >= N) {
1100 // All is good!
1101 return false;
1102 }
1103 }
Romain Guy06882f82009-06-10 13:36:04 -07001104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 if (imWin != null) {
1106 if (DEBUG_INPUT_METHOD) {
1107 Log.v(TAG, "Moving IM from " + imPos);
1108 logWindowList(" ");
1109 }
1110 imPos = tmpRemoveWindowLocked(imPos, imWin);
1111 if (DEBUG_INPUT_METHOD) {
1112 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1113 logWindowList(" ");
1114 }
1115 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1116 reAddWindowLocked(imPos, imWin);
1117 if (DEBUG_INPUT_METHOD) {
1118 Log.v(TAG, "List after moving IM to " + imPos + ":");
1119 logWindowList(" ");
1120 }
1121 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1122 } else {
1123 moveInputMethodDialogsLocked(imPos);
1124 }
Romain Guy06882f82009-06-10 13:36:04 -07001125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 } else {
1127 // In this case, the input method windows go in a fixed layer,
1128 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 if (imWin != null) {
1131 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1132 tmpRemoveWindowLocked(0, imWin);
1133 imWin.mTargetAppToken = null;
1134 reAddWindowToListInOrderLocked(imWin);
1135 if (DEBUG_INPUT_METHOD) {
1136 Log.v(TAG, "List with no IM target:");
1137 logWindowList(" ");
1138 }
1139 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1140 } else {
1141 moveInputMethodDialogsLocked(-1);;
1142 }
Romain Guy06882f82009-06-10 13:36:04 -07001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
Romain Guy06882f82009-06-10 13:36:04 -07001145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 if (needAssignLayers) {
1147 assignLayersLocked();
1148 }
Romain Guy06882f82009-06-10 13:36:04 -07001149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 return true;
1151 }
Romain Guy06882f82009-06-10 13:36:04 -07001152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 void adjustInputMethodDialogsLocked() {
1154 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1155 }
Romain Guy06882f82009-06-10 13:36:04 -07001156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 public int addWindow(Session session, IWindow client,
1158 WindowManager.LayoutParams attrs, int viewVisibility,
1159 Rect outContentInsets) {
1160 int res = mPolicy.checkAddPermission(attrs);
1161 if (res != WindowManagerImpl.ADD_OKAY) {
1162 return res;
1163 }
Romain Guy06882f82009-06-10 13:36:04 -07001164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 boolean reportNewConfig = false;
1166 WindowState attachedWindow = null;
1167 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 synchronized(mWindowMap) {
1170 // Instantiating a Display requires talking with the simulator,
1171 // so don't do it until we know the system is mostly up and
1172 // running.
1173 if (mDisplay == null) {
1174 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1175 mDisplay = wm.getDefaultDisplay();
1176 mQueue.setDisplay(mDisplay);
1177 reportNewConfig = true;
1178 }
Romain Guy06882f82009-06-10 13:36:04 -07001179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 if (mWindowMap.containsKey(client.asBinder())) {
1181 Log.w(TAG, "Window " + client + " is already added");
1182 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1183 }
1184
1185 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001186 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 if (attachedWindow == null) {
1188 Log.w(TAG, "Attempted to add window with token that is not a window: "
1189 + attrs.token + ". Aborting.");
1190 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1191 }
1192 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1193 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1194 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1195 + attrs.token + ". Aborting.");
1196 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1197 }
1198 }
1199
1200 boolean addToken = false;
1201 WindowToken token = mTokenMap.get(attrs.token);
1202 if (token == null) {
1203 if (attrs.type >= FIRST_APPLICATION_WINDOW
1204 && attrs.type <= LAST_APPLICATION_WINDOW) {
1205 Log.w(TAG, "Attempted to add application window with unknown token "
1206 + attrs.token + ". Aborting.");
1207 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1208 }
1209 if (attrs.type == TYPE_INPUT_METHOD) {
1210 Log.w(TAG, "Attempted to add input method window with unknown token "
1211 + attrs.token + ". Aborting.");
1212 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1213 }
1214 token = new WindowToken(attrs.token, -1, false);
1215 addToken = true;
1216 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1217 && attrs.type <= LAST_APPLICATION_WINDOW) {
1218 AppWindowToken atoken = token.appWindowToken;
1219 if (atoken == null) {
1220 Log.w(TAG, "Attempted to add window with non-application token "
1221 + token + ". Aborting.");
1222 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1223 } else if (atoken.removed) {
1224 Log.w(TAG, "Attempted to add window with exiting application token "
1225 + token + ". Aborting.");
1226 return WindowManagerImpl.ADD_APP_EXITING;
1227 }
1228 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1229 // No need for this guy!
1230 if (localLOGV) Log.v(
1231 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1232 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1233 }
1234 } else if (attrs.type == TYPE_INPUT_METHOD) {
1235 if (token.windowType != TYPE_INPUT_METHOD) {
1236 Log.w(TAG, "Attempted to add input method window with bad token "
1237 + attrs.token + ". Aborting.");
1238 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1239 }
1240 }
1241
1242 win = new WindowState(session, client, token,
1243 attachedWindow, attrs, viewVisibility);
1244 if (win.mDeathRecipient == null) {
1245 // Client has apparently died, so there is no reason to
1246 // continue.
1247 Log.w(TAG, "Adding window client " + client.asBinder()
1248 + " that is dead, aborting.");
1249 return WindowManagerImpl.ADD_APP_EXITING;
1250 }
1251
1252 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 res = mPolicy.prepareAddWindowLw(win, attrs);
1255 if (res != WindowManagerImpl.ADD_OKAY) {
1256 return res;
1257 }
1258
1259 // From now on, no exceptions or errors allowed!
1260
1261 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 if (addToken) {
1266 mTokenMap.put(attrs.token, token);
1267 mTokenList.add(token);
1268 }
1269 win.attach();
1270 mWindowMap.put(client.asBinder(), win);
1271
1272 if (attrs.type == TYPE_APPLICATION_STARTING &&
1273 token.appWindowToken != null) {
1274 token.appWindowToken.startingWindow = win;
1275 }
1276
1277 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 if (attrs.type == TYPE_INPUT_METHOD) {
1280 mInputMethodWindow = win;
1281 addInputMethodWindowToListLocked(win);
1282 imMayMove = false;
1283 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1284 mInputMethodDialogs.add(win);
1285 addWindowToListInOrderLocked(win, true);
1286 adjustInputMethodDialogsLocked();
1287 imMayMove = false;
1288 } else {
1289 addWindowToListInOrderLocked(win, true);
1290 }
Romain Guy06882f82009-06-10 13:36:04 -07001291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 if (mInTouchMode) {
1297 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1298 }
1299 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1300 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1301 }
Romain Guy06882f82009-06-10 13:36:04 -07001302
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001303 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001305 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1306 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 imMayMove = false;
1308 }
1309 }
Romain Guy06882f82009-06-10 13:36:04 -07001310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001312 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 }
Romain Guy06882f82009-06-10 13:36:04 -07001314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 assignLayersLocked();
1316 // Don't do layout here, the window must call
1317 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 //dump();
1320
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001321 if (focusChanged) {
1322 if (mCurrentFocus != null) {
1323 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1324 }
1325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 if (localLOGV) Log.v(
1327 TAG, "New client " + client.asBinder()
1328 + ": window=" + win);
1329 }
1330
1331 // sendNewConfiguration() checks caller permissions so we must call it with
1332 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1333 // identity anyway, so it's safe to just clear & restore around this whole
1334 // block.
1335 final long origId = Binder.clearCallingIdentity();
1336 if (reportNewConfig) {
1337 sendNewConfiguration();
1338 } else {
1339 // Update Orientation after adding a window, only if the window needs to be
1340 // displayed right away
1341 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001342 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 sendNewConfiguration();
1344 }
1345 }
1346 }
1347 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 return res;
1350 }
Romain Guy06882f82009-06-10 13:36:04 -07001351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 public void removeWindow(Session session, IWindow client) {
1353 synchronized(mWindowMap) {
1354 WindowState win = windowForClientLocked(session, client);
1355 if (win == null) {
1356 return;
1357 }
1358 removeWindowLocked(session, win);
1359 }
1360 }
Romain Guy06882f82009-06-10 13:36:04 -07001361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 public void removeWindowLocked(Session session, WindowState win) {
1363
1364 if (localLOGV || DEBUG_FOCUS) Log.v(
1365 TAG, "Remove " + win + " client="
1366 + Integer.toHexString(System.identityHashCode(
1367 win.mClient.asBinder()))
1368 + ", surface=" + win.mSurface);
1369
1370 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 if (DEBUG_APP_TRANSITIONS) Log.v(
1373 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1374 + " mExiting=" + win.mExiting
1375 + " isAnimating=" + win.isAnimating()
1376 + " app-animation="
1377 + (win.mAppToken != null ? win.mAppToken.animation : null)
1378 + " inPendingTransaction="
1379 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1380 + " mDisplayFrozen=" + mDisplayFrozen);
1381 // Visibility of the removed window. Will be used later to update orientation later on.
1382 boolean wasVisible = false;
1383 // First, see if we need to run an animation. If we do, we have
1384 // to hold off on removing the window until the animation is done.
1385 // If the display is frozen, just remove immediately, since the
1386 // animation wouldn't be seen.
1387 if (win.mSurface != null && !mDisplayFrozen) {
1388 // If we are not currently running the exit animation, we
1389 // need to see about starting one.
1390 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1393 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1394 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1395 }
1396 // Try starting an animation.
1397 if (applyAnimationLocked(win, transit, false)) {
1398 win.mExiting = true;
1399 }
1400 }
1401 if (win.mExiting || win.isAnimating()) {
1402 // The exit animation is running... wait for it!
1403 //Log.i(TAG, "*** Running exit animation...");
1404 win.mExiting = true;
1405 win.mRemoveOnExit = true;
1406 mLayoutNeeded = true;
1407 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1408 performLayoutAndPlaceSurfacesLocked();
1409 if (win.mAppToken != null) {
1410 win.mAppToken.updateReportedVisibilityLocked();
1411 }
1412 //dump();
1413 Binder.restoreCallingIdentity(origId);
1414 return;
1415 }
1416 }
1417
1418 removeWindowInnerLocked(session, win);
1419 // Removing a visible window will effect the computed orientation
1420 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001421 if (wasVisible && computeForcedAppOrientationLocked()
1422 != mForcedAppOrientation) {
1423 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 }
1425 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1426 Binder.restoreCallingIdentity(origId);
1427 }
Romain Guy06882f82009-06-10 13:36:04 -07001428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 private void removeWindowInnerLocked(Session session, WindowState win) {
1430 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1431 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 if (mInputMethodTarget == win) {
1436 moveInputMethodWindowsIfNeededLocked(false);
1437 }
Romain Guy06882f82009-06-10 13:36:04 -07001438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 mPolicy.removeWindowLw(win);
1440 win.removeLocked();
1441
1442 mWindowMap.remove(win.mClient.asBinder());
1443 mWindows.remove(win);
1444
1445 if (mInputMethodWindow == win) {
1446 mInputMethodWindow = null;
1447 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1448 mInputMethodDialogs.remove(win);
1449 }
Romain Guy06882f82009-06-10 13:36:04 -07001450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 final WindowToken token = win.mToken;
1452 final AppWindowToken atoken = win.mAppToken;
1453 token.windows.remove(win);
1454 if (atoken != null) {
1455 atoken.allAppWindows.remove(win);
1456 }
1457 if (localLOGV) Log.v(
1458 TAG, "**** Removing window " + win + ": count="
1459 + token.windows.size());
1460 if (token.windows.size() == 0) {
1461 if (!token.explicit) {
1462 mTokenMap.remove(token.token);
1463 mTokenList.remove(token);
1464 } else if (atoken != null) {
1465 atoken.firstWindowDrawn = false;
1466 }
1467 }
1468
1469 if (atoken != null) {
1470 if (atoken.startingWindow == win) {
1471 atoken.startingWindow = null;
1472 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1473 // If this is the last window and we had requested a starting
1474 // transition window, well there is no point now.
1475 atoken.startingData = null;
1476 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1477 // If this is the last window except for a starting transition
1478 // window, we need to get rid of the starting transition.
1479 if (DEBUG_STARTING_WINDOW) {
1480 Log.v(TAG, "Schedule remove starting " + token
1481 + ": no more real windows");
1482 }
1483 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1484 mH.sendMessage(m);
1485 }
1486 }
Romain Guy06882f82009-06-10 13:36:04 -07001487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 if (!mInLayout) {
1489 assignLayersLocked();
1490 mLayoutNeeded = true;
1491 performLayoutAndPlaceSurfacesLocked();
1492 if (win.mAppToken != null) {
1493 win.mAppToken.updateReportedVisibilityLocked();
1494 }
1495 }
1496 }
1497
1498 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1499 long origId = Binder.clearCallingIdentity();
1500 try {
1501 synchronized (mWindowMap) {
1502 WindowState w = windowForClientLocked(session, client);
1503 if ((w != null) && (w.mSurface != null)) {
1504 Surface.openTransaction();
1505 try {
1506 w.mSurface.setTransparentRegionHint(region);
1507 } finally {
1508 Surface.closeTransaction();
1509 }
1510 }
1511 }
1512 } finally {
1513 Binder.restoreCallingIdentity(origId);
1514 }
1515 }
1516
1517 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001518 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 Rect visibleInsets) {
1520 long origId = Binder.clearCallingIdentity();
1521 try {
1522 synchronized (mWindowMap) {
1523 WindowState w = windowForClientLocked(session, client);
1524 if (w != null) {
1525 w.mGivenInsetsPending = false;
1526 w.mGivenContentInsets.set(contentInsets);
1527 w.mGivenVisibleInsets.set(visibleInsets);
1528 w.mTouchableInsets = touchableInsets;
1529 mLayoutNeeded = true;
1530 performLayoutAndPlaceSurfacesLocked();
1531 }
1532 }
1533 } finally {
1534 Binder.restoreCallingIdentity(origId);
1535 }
1536 }
Romain Guy06882f82009-06-10 13:36:04 -07001537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 public void getWindowDisplayFrame(Session session, IWindow client,
1539 Rect outDisplayFrame) {
1540 synchronized(mWindowMap) {
1541 WindowState win = windowForClientLocked(session, client);
1542 if (win == null) {
1543 outDisplayFrame.setEmpty();
1544 return;
1545 }
1546 outDisplayFrame.set(win.mDisplayFrame);
1547 }
1548 }
1549
1550 public int relayoutWindow(Session session, IWindow client,
1551 WindowManager.LayoutParams attrs, int requestedWidth,
1552 int requestedHeight, int viewVisibility, boolean insetsPending,
1553 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1554 Surface outSurface) {
1555 boolean displayed = false;
1556 boolean inTouchMode;
1557 Configuration newConfig = null;
1558 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 synchronized(mWindowMap) {
1561 WindowState win = windowForClientLocked(session, client);
1562 if (win == null) {
1563 return 0;
1564 }
1565 win.mRequestedWidth = requestedWidth;
1566 win.mRequestedHeight = requestedHeight;
1567
1568 if (attrs != null) {
1569 mPolicy.adjustWindowParamsLw(attrs);
1570 }
Romain Guy06882f82009-06-10 13:36:04 -07001571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 int attrChanges = 0;
1573 int flagChanges = 0;
1574 if (attrs != null) {
1575 flagChanges = win.mAttrs.flags ^= attrs.flags;
1576 attrChanges = win.mAttrs.copyFrom(attrs);
1577 }
1578
1579 if (localLOGV) Log.v(
1580 TAG, "Relayout given client " + client.asBinder()
1581 + " (" + win.mAttrs.getTitle() + ")");
1582
1583
1584 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1585 win.mAlpha = attrs.alpha;
1586 }
1587
1588 final boolean scaledWindow =
1589 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1590
1591 if (scaledWindow) {
1592 // requested{Width|Height} Surface's physical size
1593 // attrs.{width|height} Size on screen
1594 win.mHScale = (attrs.width != requestedWidth) ?
1595 (attrs.width / (float)requestedWidth) : 1.0f;
1596 win.mVScale = (attrs.height != requestedHeight) ?
1597 (attrs.height / (float)requestedHeight) : 1.0f;
1598 }
1599
1600 boolean imMayMove = (flagChanges&(
1601 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1602 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 boolean focusMayChange = win.mViewVisibility != viewVisibility
1605 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1606 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 win.mRelayoutCalled = true;
1609 final int oldVisibility = win.mViewVisibility;
1610 win.mViewVisibility = viewVisibility;
1611 if (viewVisibility == View.VISIBLE &&
1612 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1613 displayed = !win.isVisibleLw();
1614 if (win.mExiting) {
1615 win.mExiting = false;
1616 win.mAnimation = null;
1617 }
1618 if (win.mDestroying) {
1619 win.mDestroying = false;
1620 mDestroySurface.remove(win);
1621 }
1622 if (oldVisibility == View.GONE) {
1623 win.mEnterAnimationPending = true;
1624 }
1625 if (displayed && win.mSurface != null && !win.mDrawPending
1626 && !win.mCommitDrawPending && !mDisplayFrozen) {
1627 applyEnterAnimationLocked(win);
1628 }
1629 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1630 // To change the format, we need to re-build the surface.
1631 win.destroySurfaceLocked();
1632 displayed = true;
1633 }
1634 try {
1635 Surface surface = win.createSurfaceLocked();
1636 if (surface != null) {
1637 outSurface.copyFrom(surface);
1638 } else {
1639 outSurface.clear();
1640 }
1641 } catch (Exception e) {
1642 Log.w(TAG, "Exception thrown when creating surface for client "
1643 + client + " (" + win.mAttrs.getTitle() + ")",
1644 e);
1645 Binder.restoreCallingIdentity(origId);
1646 return 0;
1647 }
1648 if (displayed) {
1649 focusMayChange = true;
1650 }
1651 if (win.mAttrs.type == TYPE_INPUT_METHOD
1652 && mInputMethodWindow == null) {
1653 mInputMethodWindow = win;
1654 imMayMove = true;
1655 }
1656 } else {
1657 win.mEnterAnimationPending = false;
1658 if (win.mSurface != null) {
1659 // If we are not currently running the exit animation, we
1660 // need to see about starting one.
1661 if (!win.mExiting) {
1662 // Try starting an animation; if there isn't one, we
1663 // can destroy the surface right away.
1664 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1665 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1666 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1667 }
1668 if (win.isWinVisibleLw() &&
1669 applyAnimationLocked(win, transit, false)) {
1670 win.mExiting = true;
1671 mKeyWaiter.finishedKey(session, client, true,
1672 KeyWaiter.RETURN_NOTHING);
1673 } else if (win.isAnimating()) {
1674 // Currently in a hide animation... turn this into
1675 // an exit.
1676 win.mExiting = true;
1677 } else {
1678 if (mInputMethodWindow == win) {
1679 mInputMethodWindow = null;
1680 }
1681 win.destroySurfaceLocked();
1682 }
1683 }
1684 }
1685 outSurface.clear();
1686 }
1687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 if (focusMayChange) {
1689 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1690 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 imMayMove = false;
1692 }
1693 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1694 }
Romain Guy06882f82009-06-10 13:36:04 -07001695
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001696 // updateFocusedWindowLocked() already assigned layers so we only need to
1697 // reassign them at this point if the IM window state gets shuffled
1698 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 if (imMayMove) {
1701 if (moveInputMethodWindowsIfNeededLocked(false)) {
1702 assignLayers = true;
1703 }
1704 }
Romain Guy06882f82009-06-10 13:36:04 -07001705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 mLayoutNeeded = true;
1707 win.mGivenInsetsPending = insetsPending;
1708 if (assignLayers) {
1709 assignLayersLocked();
1710 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001711 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 performLayoutAndPlaceSurfacesLocked();
1713 if (win.mAppToken != null) {
1714 win.mAppToken.updateReportedVisibilityLocked();
1715 }
1716 outFrame.set(win.mFrame);
1717 outContentInsets.set(win.mContentInsets);
1718 outVisibleInsets.set(win.mVisibleInsets);
1719 if (localLOGV) Log.v(
1720 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001721 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 + ", requestedHeight=" + requestedHeight
1723 + ", viewVisibility=" + viewVisibility
1724 + "\nRelayout returning frame=" + outFrame
1725 + ", surface=" + outSurface);
1726
1727 if (localLOGV || DEBUG_FOCUS) Log.v(
1728 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1729
1730 inTouchMode = mInTouchMode;
1731 }
1732
1733 if (newConfig != null) {
1734 sendNewConfiguration();
1735 }
Romain Guy06882f82009-06-10 13:36:04 -07001736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1740 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1741 }
1742
1743 public void finishDrawingWindow(Session session, IWindow client) {
1744 final long origId = Binder.clearCallingIdentity();
1745 synchronized(mWindowMap) {
1746 WindowState win = windowForClientLocked(session, client);
1747 if (win != null && win.finishDrawingLocked()) {
1748 mLayoutNeeded = true;
1749 performLayoutAndPlaceSurfacesLocked();
1750 }
1751 }
1752 Binder.restoreCallingIdentity(origId);
1753 }
1754
1755 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1756 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1757 + (lp != null ? lp.packageName : null)
1758 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1759 if (lp != null && lp.windowAnimations != 0) {
1760 // If this is a system resource, don't try to load it from the
1761 // application resources. It is nice to avoid loading application
1762 // resources if we can.
1763 String packageName = lp.packageName != null ? lp.packageName : "android";
1764 int resId = lp.windowAnimations;
1765 if ((resId&0xFF000000) == 0x01000000) {
1766 packageName = "android";
1767 }
1768 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
1769 + packageName);
1770 return AttributeCache.instance().get(packageName, resId,
1771 com.android.internal.R.styleable.WindowAnimation);
1772 }
1773 return null;
1774 }
Romain Guy06882f82009-06-10 13:36:04 -07001775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 private void applyEnterAnimationLocked(WindowState win) {
1777 int transit = WindowManagerPolicy.TRANSIT_SHOW;
1778 if (win.mEnterAnimationPending) {
1779 win.mEnterAnimationPending = false;
1780 transit = WindowManagerPolicy.TRANSIT_ENTER;
1781 }
1782
1783 applyAnimationLocked(win, transit, true);
1784 }
1785
1786 private boolean applyAnimationLocked(WindowState win,
1787 int transit, boolean isEntrance) {
1788 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
1789 // If we are trying to apply an animation, but already running
1790 // an animation of the same type, then just leave that one alone.
1791 return true;
1792 }
Romain Guy06882f82009-06-10 13:36:04 -07001793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 // Only apply an animation if the display isn't frozen. If it is
1795 // frozen, there is no reason to animate and it can cause strange
1796 // artifacts when we unfreeze the display if some different animation
1797 // is running.
1798 if (!mDisplayFrozen) {
1799 int anim = mPolicy.selectAnimationLw(win, transit);
1800 int attr = -1;
1801 Animation a = null;
1802 if (anim != 0) {
1803 a = AnimationUtils.loadAnimation(mContext, anim);
1804 } else {
1805 switch (transit) {
1806 case WindowManagerPolicy.TRANSIT_ENTER:
1807 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1808 break;
1809 case WindowManagerPolicy.TRANSIT_EXIT:
1810 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1811 break;
1812 case WindowManagerPolicy.TRANSIT_SHOW:
1813 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1814 break;
1815 case WindowManagerPolicy.TRANSIT_HIDE:
1816 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1817 break;
1818 }
1819 if (attr >= 0) {
1820 a = loadAnimation(win.mAttrs, attr);
1821 }
1822 }
1823 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
1824 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1825 + " mAnimation=" + win.mAnimation
1826 + " isEntrance=" + isEntrance);
1827 if (a != null) {
1828 if (DEBUG_ANIM) {
1829 RuntimeException e = new RuntimeException();
1830 e.fillInStackTrace();
1831 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
1832 }
1833 win.setAnimation(a);
1834 win.mAnimationIsEntrance = isEntrance;
1835 }
1836 } else {
1837 win.clearAnimation();
1838 }
1839
1840 return win.mAnimation != null;
1841 }
1842
1843 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
1844 int anim = 0;
1845 Context context = mContext;
1846 if (animAttr >= 0) {
1847 AttributeCache.Entry ent = getCachedAnimations(lp);
1848 if (ent != null) {
1849 context = ent.context;
1850 anim = ent.array.getResourceId(animAttr, 0);
1851 }
1852 }
1853 if (anim != 0) {
1854 return AnimationUtils.loadAnimation(context, anim);
1855 }
1856 return null;
1857 }
Romain Guy06882f82009-06-10 13:36:04 -07001858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 private boolean applyAnimationLocked(AppWindowToken wtoken,
1860 WindowManager.LayoutParams lp, int transit, boolean enter) {
1861 // Only apply an animation if the display isn't frozen. If it is
1862 // frozen, there is no reason to animate and it can cause strange
1863 // artifacts when we unfreeze the display if some different animation
1864 // is running.
1865 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001866 Animation a;
1867 if ((lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
1868 a = new FadeInOutAnimation(enter);
1869 if (DEBUG_ANIM) Log.v(TAG,
1870 "applying FadeInOutAnimation for a window in compatibility mode");
1871 } else {
1872 int animAttr = 0;
1873 switch (transit) {
1874 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
1875 animAttr = enter
1876 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
1877 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
1878 break;
1879 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
1880 animAttr = enter
1881 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
1882 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
1883 break;
1884 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
1885 animAttr = enter
1886 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
1887 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
1888 break;
1889 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
1890 animAttr = enter
1891 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
1892 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
1893 break;
1894 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
1895 animAttr = enter
1896 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
1897 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
1898 break;
1899 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
1900 animAttr = enter
1901 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
1902 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
1903 break;
1904 }
1905 a = loadAnimation(lp, animAttr);
1906 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
1907 + " anim=" + a
1908 + " animAttr=0x" + Integer.toHexString(animAttr)
1909 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 if (a != null) {
1912 if (DEBUG_ANIM) {
1913 RuntimeException e = new RuntimeException();
1914 e.fillInStackTrace();
1915 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
1916 }
1917 wtoken.setAnimation(a);
1918 }
1919 } else {
1920 wtoken.clearAnimation();
1921 }
1922
1923 return wtoken.animation != null;
1924 }
1925
1926 // -------------------------------------------------------------
1927 // Application Window Tokens
1928 // -------------------------------------------------------------
1929
1930 public void validateAppTokens(List tokens) {
1931 int v = tokens.size()-1;
1932 int m = mAppTokens.size()-1;
1933 while (v >= 0 && m >= 0) {
1934 AppWindowToken wtoken = mAppTokens.get(m);
1935 if (wtoken.removed) {
1936 m--;
1937 continue;
1938 }
1939 if (tokens.get(v) != wtoken.token) {
1940 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
1941 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
1942 }
1943 v--;
1944 m--;
1945 }
1946 while (v >= 0) {
1947 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
1948 v--;
1949 }
1950 while (m >= 0) {
1951 AppWindowToken wtoken = mAppTokens.get(m);
1952 if (!wtoken.removed) {
1953 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
1954 }
1955 m--;
1956 }
1957 }
1958
1959 boolean checkCallingPermission(String permission, String func) {
1960 // Quick check: if the calling permission is me, it's all okay.
1961 if (Binder.getCallingPid() == Process.myPid()) {
1962 return true;
1963 }
Romain Guy06882f82009-06-10 13:36:04 -07001964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 if (mContext.checkCallingPermission(permission)
1966 == PackageManager.PERMISSION_GRANTED) {
1967 return true;
1968 }
1969 String msg = "Permission Denial: " + func + " from pid="
1970 + Binder.getCallingPid()
1971 + ", uid=" + Binder.getCallingUid()
1972 + " requires " + permission;
1973 Log.w(TAG, msg);
1974 return false;
1975 }
Romain Guy06882f82009-06-10 13:36:04 -07001976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 AppWindowToken findAppWindowToken(IBinder token) {
1978 WindowToken wtoken = mTokenMap.get(token);
1979 if (wtoken == null) {
1980 return null;
1981 }
1982 return wtoken.appWindowToken;
1983 }
Romain Guy06882f82009-06-10 13:36:04 -07001984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 public void addWindowToken(IBinder token, int type) {
1986 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1987 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001988 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
Romain Guy06882f82009-06-10 13:36:04 -07001990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 synchronized(mWindowMap) {
1992 WindowToken wtoken = mTokenMap.get(token);
1993 if (wtoken != null) {
1994 Log.w(TAG, "Attempted to add existing input method token: " + token);
1995 return;
1996 }
1997 wtoken = new WindowToken(token, type, true);
1998 mTokenMap.put(token, wtoken);
1999 mTokenList.add(wtoken);
2000 }
2001 }
Romain Guy06882f82009-06-10 13:36:04 -07002002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 public void removeWindowToken(IBinder token) {
2004 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2005 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002006 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
2008
2009 final long origId = Binder.clearCallingIdentity();
2010 synchronized(mWindowMap) {
2011 WindowToken wtoken = mTokenMap.remove(token);
2012 mTokenList.remove(wtoken);
2013 if (wtoken != null) {
2014 boolean delayed = false;
2015 if (!wtoken.hidden) {
2016 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 final int N = wtoken.windows.size();
2019 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 for (int i=0; i<N; i++) {
2022 WindowState win = wtoken.windows.get(i);
2023
2024 if (win.isAnimating()) {
2025 delayed = true;
2026 }
Romain Guy06882f82009-06-10 13:36:04 -07002027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 if (win.isVisibleNow()) {
2029 applyAnimationLocked(win,
2030 WindowManagerPolicy.TRANSIT_EXIT, false);
2031 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2032 KeyWaiter.RETURN_NOTHING);
2033 changed = true;
2034 }
2035 }
2036
2037 if (changed) {
2038 mLayoutNeeded = true;
2039 performLayoutAndPlaceSurfacesLocked();
2040 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2041 }
Romain Guy06882f82009-06-10 13:36:04 -07002042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 if (delayed) {
2044 mExitingTokens.add(wtoken);
2045 }
2046 }
Romain Guy06882f82009-06-10 13:36:04 -07002047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 } else {
2049 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2050 }
2051 }
2052 Binder.restoreCallingIdentity(origId);
2053 }
2054
2055 public void addAppToken(int addPos, IApplicationToken token,
2056 int groupId, int requestedOrientation, boolean fullscreen) {
2057 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2058 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002059 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
Romain Guy06882f82009-06-10 13:36:04 -07002061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 synchronized(mWindowMap) {
2063 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2064 if (wtoken != null) {
2065 Log.w(TAG, "Attempted to add existing app token: " + token);
2066 return;
2067 }
2068 wtoken = new AppWindowToken(token);
2069 wtoken.groupId = groupId;
2070 wtoken.appFullscreen = fullscreen;
2071 wtoken.requestedOrientation = requestedOrientation;
2072 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002073 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 mTokenMap.put(token.asBinder(), wtoken);
2075 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 // Application tokens start out hidden.
2078 wtoken.hidden = true;
2079 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 //dump();
2082 }
2083 }
Romain Guy06882f82009-06-10 13:36:04 -07002084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 public void setAppGroupId(IBinder token, int groupId) {
2086 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2087 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002088 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 }
2090
2091 synchronized(mWindowMap) {
2092 AppWindowToken wtoken = findAppWindowToken(token);
2093 if (wtoken == null) {
2094 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2095 return;
2096 }
2097 wtoken.groupId = groupId;
2098 }
2099 }
Romain Guy06882f82009-06-10 13:36:04 -07002100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 public int getOrientationFromWindowsLocked() {
2102 int pos = mWindows.size() - 1;
2103 while (pos >= 0) {
2104 WindowState wtoken = (WindowState) mWindows.get(pos);
2105 pos--;
2106 if (wtoken.mAppToken != null) {
2107 // We hit an application window. so the orientation will be determined by the
2108 // app window. No point in continuing further.
2109 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2110 }
2111 if (!wtoken.isVisibleLw()) {
2112 continue;
2113 }
2114 int req = wtoken.mAttrs.screenOrientation;
2115 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2116 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2117 continue;
2118 } else {
2119 return req;
2120 }
2121 }
2122 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2123 }
Romain Guy06882f82009-06-10 13:36:04 -07002124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 public int getOrientationFromAppTokensLocked() {
2126 int pos = mAppTokens.size() - 1;
2127 int curGroup = 0;
2128 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002129 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002131 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 while (pos >= 0) {
2133 AppWindowToken wtoken = mAppTokens.get(pos);
2134 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002135 // if we're about to tear down this window and not seek for
2136 // the behind activity, don't use it for orientation
2137 if (!findingBehind
2138 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002139 continue;
2140 }
2141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 if (!haveGroup) {
2143 // We ignore any hidden applications on the top.
2144 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2145 continue;
2146 }
2147 haveGroup = true;
2148 curGroup = wtoken.groupId;
2149 lastOrientation = wtoken.requestedOrientation;
2150 } else if (curGroup != wtoken.groupId) {
2151 // If we have hit a new application group, and the bottom
2152 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002153 // the orientation behind it, and the last app was
2154 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002156 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2157 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 return lastOrientation;
2159 }
2160 }
2161 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002162 // If this application is fullscreen, and didn't explicitly say
2163 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002165 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002166 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002167 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 return or;
2169 }
2170 // If this application has requested an explicit orientation,
2171 // then use it.
2172 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2173 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2174 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2175 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2176 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2177 return or;
2178 }
Owen Lin3413b892009-05-01 17:12:32 -07002179 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 }
2181 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2182 }
Romain Guy06882f82009-06-10 13:36:04 -07002183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002185 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002186 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2187 "updateOrientationFromAppTokens()")) {
2188 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2189 }
2190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 Configuration config;
2192 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002193 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2194 freezeThisOneIfNeeded);
2195 Binder.restoreCallingIdentity(ident);
2196 return config;
2197 }
2198
2199 Configuration updateOrientationFromAppTokensUnchecked(
2200 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2201 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002203 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 }
2205 if (config != null) {
2206 mLayoutNeeded = true;
2207 performLayoutAndPlaceSurfacesLocked();
2208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 return config;
2210 }
Romain Guy06882f82009-06-10 13:36:04 -07002211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 /*
2213 * The orientation is computed from non-application windows first. If none of
2214 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002215 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2217 * android.os.IBinder)
2218 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002219 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002220 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 long ident = Binder.clearCallingIdentity();
2223 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002224 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 if (req != mForcedAppOrientation) {
2227 changed = true;
2228 mForcedAppOrientation = req;
2229 //send a message to Policy indicating orientation change to take
2230 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002231 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 }
Romain Guy06882f82009-06-10 13:36:04 -07002233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 if (changed) {
2235 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002236 WindowManagerPolicy.USE_LAST_ROTATION,
2237 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 if (changed) {
2239 if (freezeThisOneIfNeeded != null) {
2240 AppWindowToken wtoken = findAppWindowToken(
2241 freezeThisOneIfNeeded);
2242 if (wtoken != null) {
2243 startAppFreezingScreenLocked(wtoken,
2244 ActivityInfo.CONFIG_ORIENTATION);
2245 }
2246 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002247 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 }
2249 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002250
2251 // No obvious action we need to take, but if our current
2252 // state mismatches the activity maanager's, update it
2253 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002254 mTempConfiguration.setToDefaults();
2255 if (computeNewConfigurationLocked(mTempConfiguration)) {
2256 if (appConfig.diff(mTempConfiguration) != 0) {
2257 Log.i(TAG, "Config changed: " + mTempConfiguration);
2258 return new Configuration(mTempConfiguration);
2259 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002260 }
2261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 } finally {
2263 Binder.restoreCallingIdentity(ident);
2264 }
Romain Guy06882f82009-06-10 13:36:04 -07002265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 return null;
2267 }
Romain Guy06882f82009-06-10 13:36:04 -07002268
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002269 int computeForcedAppOrientationLocked() {
2270 int req = getOrientationFromWindowsLocked();
2271 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2272 req = getOrientationFromAppTokensLocked();
2273 }
2274 return req;
2275 }
Romain Guy06882f82009-06-10 13:36:04 -07002276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2278 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2279 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002280 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 }
Romain Guy06882f82009-06-10 13:36:04 -07002282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 synchronized(mWindowMap) {
2284 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2285 if (wtoken == null) {
2286 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2287 return;
2288 }
Romain Guy06882f82009-06-10 13:36:04 -07002289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 wtoken.requestedOrientation = requestedOrientation;
2291 }
2292 }
Romain Guy06882f82009-06-10 13:36:04 -07002293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 public int getAppOrientation(IApplicationToken token) {
2295 synchronized(mWindowMap) {
2296 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2297 if (wtoken == null) {
2298 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2299 }
Romain Guy06882f82009-06-10 13:36:04 -07002300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 return wtoken.requestedOrientation;
2302 }
2303 }
Romain Guy06882f82009-06-10 13:36:04 -07002304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2306 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2307 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002308 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 }
2310
2311 synchronized(mWindowMap) {
2312 boolean changed = false;
2313 if (token == null) {
2314 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2315 changed = mFocusedApp != null;
2316 mFocusedApp = null;
2317 mKeyWaiter.tickle();
2318 } else {
2319 AppWindowToken newFocus = findAppWindowToken(token);
2320 if (newFocus == null) {
2321 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2322 return;
2323 }
2324 changed = mFocusedApp != newFocus;
2325 mFocusedApp = newFocus;
2326 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2327 mKeyWaiter.tickle();
2328 }
2329
2330 if (moveFocusNow && changed) {
2331 final long origId = Binder.clearCallingIdentity();
2332 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2333 Binder.restoreCallingIdentity(origId);
2334 }
2335 }
2336 }
2337
2338 public void prepareAppTransition(int transit) {
2339 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2340 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002341 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 }
Romain Guy06882f82009-06-10 13:36:04 -07002343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 synchronized(mWindowMap) {
2345 if (DEBUG_APP_TRANSITIONS) Log.v(
2346 TAG, "Prepare app transition: transit=" + transit
2347 + " mNextAppTransition=" + mNextAppTransition);
2348 if (!mDisplayFrozen) {
2349 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2350 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002351 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2352 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2353 // Opening a new task always supersedes a close for the anim.
2354 mNextAppTransition = transit;
2355 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2356 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2357 // Opening a new activity always supersedes a close for the anim.
2358 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
2360 mAppTransitionReady = false;
2361 mAppTransitionTimeout = false;
2362 mStartingIconInTransition = false;
2363 mSkipAppTransitionAnimation = false;
2364 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2365 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2366 5000);
2367 }
2368 }
2369 }
2370
2371 public int getPendingAppTransition() {
2372 return mNextAppTransition;
2373 }
Romain Guy06882f82009-06-10 13:36:04 -07002374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 public void executeAppTransition() {
2376 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2377 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002378 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 }
Romain Guy06882f82009-06-10 13:36:04 -07002380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 synchronized(mWindowMap) {
2382 if (DEBUG_APP_TRANSITIONS) Log.v(
2383 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2384 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2385 mAppTransitionReady = true;
2386 final long origId = Binder.clearCallingIdentity();
2387 performLayoutAndPlaceSurfacesLocked();
2388 Binder.restoreCallingIdentity(origId);
2389 }
2390 }
2391 }
2392
2393 public void setAppStartingWindow(IBinder token, String pkg,
2394 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2395 IBinder transferFrom, boolean createIfNeeded) {
2396 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2397 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002398 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 }
2400
2401 synchronized(mWindowMap) {
2402 if (DEBUG_STARTING_WINDOW) Log.v(
2403 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2404 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 AppWindowToken wtoken = findAppWindowToken(token);
2407 if (wtoken == null) {
2408 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2409 return;
2410 }
2411
2412 // If the display is frozen, we won't do anything until the
2413 // actual window is displayed so there is no reason to put in
2414 // the starting window.
2415 if (mDisplayFrozen) {
2416 return;
2417 }
Romain Guy06882f82009-06-10 13:36:04 -07002418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 if (wtoken.startingData != null) {
2420 return;
2421 }
Romain Guy06882f82009-06-10 13:36:04 -07002422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 if (transferFrom != null) {
2424 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2425 if (ttoken != null) {
2426 WindowState startingWindow = ttoken.startingWindow;
2427 if (startingWindow != null) {
2428 if (mStartingIconInTransition) {
2429 // In this case, the starting icon has already
2430 // been displayed, so start letting windows get
2431 // shown immediately without any more transitions.
2432 mSkipAppTransitionAnimation = true;
2433 }
2434 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2435 "Moving existing starting from " + ttoken
2436 + " to " + wtoken);
2437 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 // Transfer the starting window over to the new
2440 // token.
2441 wtoken.startingData = ttoken.startingData;
2442 wtoken.startingView = ttoken.startingView;
2443 wtoken.startingWindow = startingWindow;
2444 ttoken.startingData = null;
2445 ttoken.startingView = null;
2446 ttoken.startingWindow = null;
2447 ttoken.startingMoved = true;
2448 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002449 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 startingWindow.mAppToken = wtoken;
2451 mWindows.remove(startingWindow);
2452 ttoken.windows.remove(startingWindow);
2453 ttoken.allAppWindows.remove(startingWindow);
2454 addWindowToListInOrderLocked(startingWindow, true);
2455 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 // Propagate other interesting state between the
2458 // tokens. If the old token is displayed, we should
2459 // immediately force the new one to be displayed. If
2460 // it is animating, we need to move that animation to
2461 // the new one.
2462 if (ttoken.allDrawn) {
2463 wtoken.allDrawn = true;
2464 }
2465 if (ttoken.firstWindowDrawn) {
2466 wtoken.firstWindowDrawn = true;
2467 }
2468 if (!ttoken.hidden) {
2469 wtoken.hidden = false;
2470 wtoken.hiddenRequested = false;
2471 wtoken.willBeHidden = false;
2472 }
2473 if (wtoken.clientHidden != ttoken.clientHidden) {
2474 wtoken.clientHidden = ttoken.clientHidden;
2475 wtoken.sendAppVisibilityToClients();
2476 }
2477 if (ttoken.animation != null) {
2478 wtoken.animation = ttoken.animation;
2479 wtoken.animating = ttoken.animating;
2480 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2481 ttoken.animation = null;
2482 ttoken.animLayerAdjustment = 0;
2483 wtoken.updateLayers();
2484 ttoken.updateLayers();
2485 }
Romain Guy06882f82009-06-10 13:36:04 -07002486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 mLayoutNeeded = true;
2489 performLayoutAndPlaceSurfacesLocked();
2490 Binder.restoreCallingIdentity(origId);
2491 return;
2492 } else if (ttoken.startingData != null) {
2493 // The previous app was getting ready to show a
2494 // starting window, but hasn't yet done so. Steal it!
2495 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2496 "Moving pending starting from " + ttoken
2497 + " to " + wtoken);
2498 wtoken.startingData = ttoken.startingData;
2499 ttoken.startingData = null;
2500 ttoken.startingMoved = true;
2501 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2502 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2503 // want to process the message ASAP, before any other queued
2504 // messages.
2505 mH.sendMessageAtFrontOfQueue(m);
2506 return;
2507 }
2508 }
2509 }
2510
2511 // There is no existing starting window, and the caller doesn't
2512 // want us to create one, so that's it!
2513 if (!createIfNeeded) {
2514 return;
2515 }
Romain Guy06882f82009-06-10 13:36:04 -07002516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 mStartingIconInTransition = true;
2518 wtoken.startingData = new StartingData(
2519 pkg, theme, nonLocalizedLabel,
2520 labelRes, icon);
2521 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2522 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2523 // want to process the message ASAP, before any other queued
2524 // messages.
2525 mH.sendMessageAtFrontOfQueue(m);
2526 }
2527 }
2528
2529 public void setAppWillBeHidden(IBinder token) {
2530 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2531 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002532 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 }
2534
2535 AppWindowToken wtoken;
2536
2537 synchronized(mWindowMap) {
2538 wtoken = findAppWindowToken(token);
2539 if (wtoken == null) {
2540 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2541 return;
2542 }
2543 wtoken.willBeHidden = true;
2544 }
2545 }
Romain Guy06882f82009-06-10 13:36:04 -07002546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2548 boolean visible, int transit, boolean performLayout) {
2549 boolean delayed = false;
2550
2551 if (wtoken.clientHidden == visible) {
2552 wtoken.clientHidden = !visible;
2553 wtoken.sendAppVisibilityToClients();
2554 }
Romain Guy06882f82009-06-10 13:36:04 -07002555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 wtoken.willBeHidden = false;
2557 if (wtoken.hidden == visible) {
2558 final int N = wtoken.allAppWindows.size();
2559 boolean changed = false;
2560 if (DEBUG_APP_TRANSITIONS) Log.v(
2561 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2562 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2567 if (wtoken.animation == sDummyAnimation) {
2568 wtoken.animation = null;
2569 }
2570 applyAnimationLocked(wtoken, lp, transit, visible);
2571 changed = true;
2572 if (wtoken.animation != null) {
2573 delayed = runningAppAnimation = true;
2574 }
2575 }
Romain Guy06882f82009-06-10 13:36:04 -07002576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 for (int i=0; i<N; i++) {
2578 WindowState win = wtoken.allAppWindows.get(i);
2579 if (win == wtoken.startingWindow) {
2580 continue;
2581 }
2582
2583 if (win.isAnimating()) {
2584 delayed = true;
2585 }
Romain Guy06882f82009-06-10 13:36:04 -07002586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2588 //win.dump(" ");
2589 if (visible) {
2590 if (!win.isVisibleNow()) {
2591 if (!runningAppAnimation) {
2592 applyAnimationLocked(win,
2593 WindowManagerPolicy.TRANSIT_ENTER, true);
2594 }
2595 changed = true;
2596 }
2597 } else if (win.isVisibleNow()) {
2598 if (!runningAppAnimation) {
2599 applyAnimationLocked(win,
2600 WindowManagerPolicy.TRANSIT_EXIT, false);
2601 }
2602 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2603 KeyWaiter.RETURN_NOTHING);
2604 changed = true;
2605 }
2606 }
2607
2608 wtoken.hidden = wtoken.hiddenRequested = !visible;
2609 if (!visible) {
2610 unsetAppFreezingScreenLocked(wtoken, true, true);
2611 } else {
2612 // If we are being set visible, and the starting window is
2613 // not yet displayed, then make sure it doesn't get displayed.
2614 WindowState swin = wtoken.startingWindow;
2615 if (swin != null && (swin.mDrawPending
2616 || swin.mCommitDrawPending)) {
2617 swin.mPolicyVisibility = false;
2618 swin.mPolicyVisibilityAfterAnim = false;
2619 }
2620 }
Romain Guy06882f82009-06-10 13:36:04 -07002621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2623 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2624 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 if (changed && performLayout) {
2627 mLayoutNeeded = true;
2628 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 performLayoutAndPlaceSurfacesLocked();
2630 }
2631 }
2632
2633 if (wtoken.animation != null) {
2634 delayed = true;
2635 }
Romain Guy06882f82009-06-10 13:36:04 -07002636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 return delayed;
2638 }
2639
2640 public void setAppVisibility(IBinder token, boolean visible) {
2641 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2642 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002643 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 }
2645
2646 AppWindowToken wtoken;
2647
2648 synchronized(mWindowMap) {
2649 wtoken = findAppWindowToken(token);
2650 if (wtoken == null) {
2651 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2652 return;
2653 }
2654
2655 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2656 RuntimeException e = new RuntimeException();
2657 e.fillInStackTrace();
2658 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2659 + "): mNextAppTransition=" + mNextAppTransition
2660 + " hidden=" + wtoken.hidden
2661 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2662 }
Romain Guy06882f82009-06-10 13:36:04 -07002663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 // If we are preparing an app transition, then delay changing
2665 // the visibility of this token until we execute that transition.
2666 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2667 // Already in requested state, don't do anything more.
2668 if (wtoken.hiddenRequested != visible) {
2669 return;
2670 }
2671 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 if (DEBUG_APP_TRANSITIONS) Log.v(
2674 TAG, "Setting dummy animation on: " + wtoken);
2675 wtoken.setDummyAnimation();
2676 mOpeningApps.remove(wtoken);
2677 mClosingApps.remove(wtoken);
2678 wtoken.inPendingTransaction = true;
2679 if (visible) {
2680 mOpeningApps.add(wtoken);
2681 wtoken.allDrawn = false;
2682 wtoken.startingDisplayed = false;
2683 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 if (wtoken.clientHidden) {
2686 // In the case where we are making an app visible
2687 // but holding off for a transition, we still need
2688 // to tell the client to make its windows visible so
2689 // they get drawn. Otherwise, we will wait on
2690 // performing the transition until all windows have
2691 // been drawn, they never will be, and we are sad.
2692 wtoken.clientHidden = false;
2693 wtoken.sendAppVisibilityToClients();
2694 }
2695 } else {
2696 mClosingApps.add(wtoken);
2697 }
2698 return;
2699 }
Romain Guy06882f82009-06-10 13:36:04 -07002700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 final long origId = Binder.clearCallingIdentity();
2702 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2703 wtoken.updateReportedVisibilityLocked();
2704 Binder.restoreCallingIdentity(origId);
2705 }
2706 }
2707
2708 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2709 boolean unfreezeSurfaceNow, boolean force) {
2710 if (wtoken.freezingScreen) {
2711 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2712 + " force=" + force);
2713 final int N = wtoken.allAppWindows.size();
2714 boolean unfrozeWindows = false;
2715 for (int i=0; i<N; i++) {
2716 WindowState w = wtoken.allAppWindows.get(i);
2717 if (w.mAppFreezing) {
2718 w.mAppFreezing = false;
2719 if (w.mSurface != null && !w.mOrientationChanging) {
2720 w.mOrientationChanging = true;
2721 }
2722 unfrozeWindows = true;
2723 }
2724 }
2725 if (force || unfrozeWindows) {
2726 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2727 wtoken.freezingScreen = false;
2728 mAppsFreezingScreen--;
2729 }
2730 if (unfreezeSurfaceNow) {
2731 if (unfrozeWindows) {
2732 mLayoutNeeded = true;
2733 performLayoutAndPlaceSurfacesLocked();
2734 }
2735 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2736 stopFreezingDisplayLocked();
2737 }
2738 }
2739 }
2740 }
Romain Guy06882f82009-06-10 13:36:04 -07002741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2743 int configChanges) {
2744 if (DEBUG_ORIENTATION) {
2745 RuntimeException e = new RuntimeException();
2746 e.fillInStackTrace();
2747 Log.i(TAG, "Set freezing of " + wtoken.appToken
2748 + ": hidden=" + wtoken.hidden + " freezing="
2749 + wtoken.freezingScreen, e);
2750 }
2751 if (!wtoken.hiddenRequested) {
2752 if (!wtoken.freezingScreen) {
2753 wtoken.freezingScreen = true;
2754 mAppsFreezingScreen++;
2755 if (mAppsFreezingScreen == 1) {
2756 startFreezingDisplayLocked();
2757 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
2758 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
2759 5000);
2760 }
2761 }
2762 final int N = wtoken.allAppWindows.size();
2763 for (int i=0; i<N; i++) {
2764 WindowState w = wtoken.allAppWindows.get(i);
2765 w.mAppFreezing = true;
2766 }
2767 }
2768 }
Romain Guy06882f82009-06-10 13:36:04 -07002769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 public void startAppFreezingScreen(IBinder token, int configChanges) {
2771 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2772 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002773 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 }
2775
2776 synchronized(mWindowMap) {
2777 if (configChanges == 0 && !mDisplayFrozen) {
2778 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
2779 return;
2780 }
Romain Guy06882f82009-06-10 13:36:04 -07002781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 AppWindowToken wtoken = findAppWindowToken(token);
2783 if (wtoken == null || wtoken.appToken == null) {
2784 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
2785 return;
2786 }
2787 final long origId = Binder.clearCallingIdentity();
2788 startAppFreezingScreenLocked(wtoken, configChanges);
2789 Binder.restoreCallingIdentity(origId);
2790 }
2791 }
Romain Guy06882f82009-06-10 13:36:04 -07002792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 public void stopAppFreezingScreen(IBinder token, boolean force) {
2794 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2795 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002796 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 }
2798
2799 synchronized(mWindowMap) {
2800 AppWindowToken wtoken = findAppWindowToken(token);
2801 if (wtoken == null || wtoken.appToken == null) {
2802 return;
2803 }
2804 final long origId = Binder.clearCallingIdentity();
2805 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
2806 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
2807 unsetAppFreezingScreenLocked(wtoken, true, force);
2808 Binder.restoreCallingIdentity(origId);
2809 }
2810 }
Romain Guy06882f82009-06-10 13:36:04 -07002811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 public void removeAppToken(IBinder token) {
2813 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2814 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002815 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 }
2817
2818 AppWindowToken wtoken = null;
2819 AppWindowToken startingToken = null;
2820 boolean delayed = false;
2821
2822 final long origId = Binder.clearCallingIdentity();
2823 synchronized(mWindowMap) {
2824 WindowToken basewtoken = mTokenMap.remove(token);
2825 mTokenList.remove(basewtoken);
2826 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
2827 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
2828 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
2829 wtoken.inPendingTransaction = false;
2830 mOpeningApps.remove(wtoken);
2831 if (mClosingApps.contains(wtoken)) {
2832 delayed = true;
2833 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2834 mClosingApps.add(wtoken);
2835 delayed = true;
2836 }
2837 if (DEBUG_APP_TRANSITIONS) Log.v(
2838 TAG, "Removing app " + wtoken + " delayed=" + delayed
2839 + " animation=" + wtoken.animation
2840 + " animating=" + wtoken.animating);
2841 if (delayed) {
2842 // set the token aside because it has an active animation to be finished
2843 mExitingAppTokens.add(wtoken);
2844 }
2845 mAppTokens.remove(wtoken);
2846 wtoken.removed = true;
2847 if (wtoken.startingData != null) {
2848 startingToken = wtoken;
2849 }
2850 unsetAppFreezingScreenLocked(wtoken, true, true);
2851 if (mFocusedApp == wtoken) {
2852 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
2853 mFocusedApp = null;
2854 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2855 mKeyWaiter.tickle();
2856 }
2857 } else {
2858 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
2859 }
Romain Guy06882f82009-06-10 13:36:04 -07002860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 if (!delayed && wtoken != null) {
2862 wtoken.updateReportedVisibilityLocked();
2863 }
2864 }
2865 Binder.restoreCallingIdentity(origId);
2866
2867 if (startingToken != null) {
2868 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
2869 + startingToken + ": app token removed");
2870 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
2871 mH.sendMessage(m);
2872 }
2873 }
2874
2875 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
2876 final int NW = token.windows.size();
2877 for (int i=0; i<NW; i++) {
2878 WindowState win = token.windows.get(i);
2879 mWindows.remove(win);
2880 int j = win.mChildWindows.size();
2881 while (j > 0) {
2882 j--;
2883 mWindows.remove(win.mChildWindows.get(j));
2884 }
2885 }
2886 return NW > 0;
2887 }
2888
2889 void dumpAppTokensLocked() {
2890 for (int i=mAppTokens.size()-1; i>=0; i--) {
2891 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
2892 }
2893 }
Romain Guy06882f82009-06-10 13:36:04 -07002894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 void dumpWindowsLocked() {
2896 for (int i=mWindows.size()-1; i>=0; i--) {
2897 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
2898 }
2899 }
Romain Guy06882f82009-06-10 13:36:04 -07002900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 private int findWindowOffsetLocked(int tokenPos) {
2902 final int NW = mWindows.size();
2903
2904 if (tokenPos >= mAppTokens.size()) {
2905 int i = NW;
2906 while (i > 0) {
2907 i--;
2908 WindowState win = (WindowState)mWindows.get(i);
2909 if (win.getAppToken() != null) {
2910 return i+1;
2911 }
2912 }
2913 }
2914
2915 while (tokenPos > 0) {
2916 // Find the first app token below the new position that has
2917 // a window displayed.
2918 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
2919 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
2920 + tokenPos + " -- " + wtoken.token);
2921 int i = wtoken.windows.size();
2922 while (i > 0) {
2923 i--;
2924 WindowState win = wtoken.windows.get(i);
2925 int j = win.mChildWindows.size();
2926 while (j > 0) {
2927 j--;
2928 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2929 if (cwin.mSubLayer >= 0 ) {
2930 for (int pos=NW-1; pos>=0; pos--) {
2931 if (mWindows.get(pos) == cwin) {
2932 if (DEBUG_REORDER) Log.v(TAG,
2933 "Found child win @" + (pos+1));
2934 return pos+1;
2935 }
2936 }
2937 }
2938 }
2939 for (int pos=NW-1; pos>=0; pos--) {
2940 if (mWindows.get(pos) == win) {
2941 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
2942 return pos+1;
2943 }
2944 }
2945 }
2946 tokenPos--;
2947 }
2948
2949 return 0;
2950 }
2951
2952 private final int reAddWindowLocked(int index, WindowState win) {
2953 final int NCW = win.mChildWindows.size();
2954 boolean added = false;
2955 for (int j=0; j<NCW; j++) {
2956 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2957 if (!added && cwin.mSubLayer >= 0) {
2958 mWindows.add(index, win);
2959 index++;
2960 added = true;
2961 }
2962 mWindows.add(index, cwin);
2963 index++;
2964 }
2965 if (!added) {
2966 mWindows.add(index, win);
2967 index++;
2968 }
2969 return index;
2970 }
Romain Guy06882f82009-06-10 13:36:04 -07002971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 private final int reAddAppWindowsLocked(int index, WindowToken token) {
2973 final int NW = token.windows.size();
2974 for (int i=0; i<NW; i++) {
2975 index = reAddWindowLocked(index, token.windows.get(i));
2976 }
2977 return index;
2978 }
2979
2980 public void moveAppToken(int index, IBinder token) {
2981 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2982 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002983 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 }
2985
2986 synchronized(mWindowMap) {
2987 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
2988 if (DEBUG_REORDER) dumpAppTokensLocked();
2989 final AppWindowToken wtoken = findAppWindowToken(token);
2990 if (wtoken == null || !mAppTokens.remove(wtoken)) {
2991 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
2992 + token + " (" + wtoken + ")");
2993 return;
2994 }
2995 mAppTokens.add(index, wtoken);
2996 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
2997 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 final long origId = Binder.clearCallingIdentity();
3000 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3001 if (DEBUG_REORDER) dumpWindowsLocked();
3002 if (tmpRemoveAppWindowsLocked(wtoken)) {
3003 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3004 if (DEBUG_REORDER) dumpWindowsLocked();
3005 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3006 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3007 if (DEBUG_REORDER) dumpWindowsLocked();
3008 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 mLayoutNeeded = true;
3010 performLayoutAndPlaceSurfacesLocked();
3011 }
3012 Binder.restoreCallingIdentity(origId);
3013 }
3014 }
3015
3016 private void removeAppTokensLocked(List<IBinder> tokens) {
3017 // XXX This should be done more efficiently!
3018 // (take advantage of the fact that both lists should be
3019 // ordered in the same way.)
3020 int N = tokens.size();
3021 for (int i=0; i<N; i++) {
3022 IBinder token = tokens.get(i);
3023 final AppWindowToken wtoken = findAppWindowToken(token);
3024 if (!mAppTokens.remove(wtoken)) {
3025 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3026 + token + " (" + wtoken + ")");
3027 i--;
3028 N--;
3029 }
3030 }
3031 }
3032
3033 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3034 // First remove all of the windows from the list.
3035 final int N = tokens.size();
3036 int i;
3037 for (i=0; i<N; i++) {
3038 WindowToken token = mTokenMap.get(tokens.get(i));
3039 if (token != null) {
3040 tmpRemoveAppWindowsLocked(token);
3041 }
3042 }
3043
3044 // Where to start adding?
3045 int pos = findWindowOffsetLocked(tokenPos);
3046
3047 // And now add them back at the correct place.
3048 for (i=0; i<N; i++) {
3049 WindowToken token = mTokenMap.get(tokens.get(i));
3050 if (token != null) {
3051 pos = reAddAppWindowsLocked(pos, token);
3052 }
3053 }
3054
3055 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 mLayoutNeeded = true;
3057 performLayoutAndPlaceSurfacesLocked();
3058
3059 //dump();
3060 }
3061
3062 public void moveAppTokensToTop(List<IBinder> tokens) {
3063 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3064 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003065 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 }
3067
3068 final long origId = Binder.clearCallingIdentity();
3069 synchronized(mWindowMap) {
3070 removeAppTokensLocked(tokens);
3071 final int N = tokens.size();
3072 for (int i=0; i<N; i++) {
3073 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3074 if (wt != null) {
3075 mAppTokens.add(wt);
3076 }
3077 }
3078 moveAppWindowsLocked(tokens, mAppTokens.size());
3079 }
3080 Binder.restoreCallingIdentity(origId);
3081 }
3082
3083 public void moveAppTokensToBottom(List<IBinder> tokens) {
3084 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3085 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003086 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
3088
3089 final long origId = Binder.clearCallingIdentity();
3090 synchronized(mWindowMap) {
3091 removeAppTokensLocked(tokens);
3092 final int N = tokens.size();
3093 int pos = 0;
3094 for (int i=0; i<N; i++) {
3095 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3096 if (wt != null) {
3097 mAppTokens.add(pos, wt);
3098 pos++;
3099 }
3100 }
3101 moveAppWindowsLocked(tokens, 0);
3102 }
3103 Binder.restoreCallingIdentity(origId);
3104 }
3105
3106 // -------------------------------------------------------------
3107 // Misc IWindowSession methods
3108 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 public void disableKeyguard(IBinder token, String tag) {
3111 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3112 != PackageManager.PERMISSION_GRANTED) {
3113 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3114 }
3115 mKeyguardDisabled.acquire(token, tag);
3116 }
3117
3118 public void reenableKeyguard(IBinder token) {
3119 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3120 != PackageManager.PERMISSION_GRANTED) {
3121 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3122 }
3123 synchronized (mKeyguardDisabled) {
3124 mKeyguardDisabled.release(token);
3125
3126 if (!mKeyguardDisabled.isAcquired()) {
3127 // if we are the last one to reenable the keyguard wait until
3128 // we have actaully finished reenabling until returning
3129 mWaitingUntilKeyguardReenabled = true;
3130 while (mWaitingUntilKeyguardReenabled) {
3131 try {
3132 mKeyguardDisabled.wait();
3133 } catch (InterruptedException e) {
3134 Thread.currentThread().interrupt();
3135 }
3136 }
3137 }
3138 }
3139 }
3140
3141 /**
3142 * @see android.app.KeyguardManager#exitKeyguardSecurely
3143 */
3144 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3145 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3146 != PackageManager.PERMISSION_GRANTED) {
3147 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3148 }
3149 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3150 public void onKeyguardExitResult(boolean success) {
3151 try {
3152 callback.onKeyguardExitResult(success);
3153 } catch (RemoteException e) {
3154 // Client has died, we don't care.
3155 }
3156 }
3157 });
3158 }
3159
3160 public boolean inKeyguardRestrictedInputMode() {
3161 return mPolicy.inKeyguardRestrictedKeyInputMode();
3162 }
Romain Guy06882f82009-06-10 13:36:04 -07003163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 static float fixScale(float scale) {
3165 if (scale < 0) scale = 0;
3166 else if (scale > 20) scale = 20;
3167 return Math.abs(scale);
3168 }
Romain Guy06882f82009-06-10 13:36:04 -07003169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 public void setAnimationScale(int which, float scale) {
3171 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3172 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003173 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 }
3175
3176 if (scale < 0) scale = 0;
3177 else if (scale > 20) scale = 20;
3178 scale = Math.abs(scale);
3179 switch (which) {
3180 case 0: mWindowAnimationScale = fixScale(scale); break;
3181 case 1: mTransitionAnimationScale = fixScale(scale); break;
3182 }
Romain Guy06882f82009-06-10 13:36:04 -07003183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 // Persist setting
3185 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3186 }
Romain Guy06882f82009-06-10 13:36:04 -07003187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 public void setAnimationScales(float[] scales) {
3189 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3190 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003191 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 }
3193
3194 if (scales != null) {
3195 if (scales.length >= 1) {
3196 mWindowAnimationScale = fixScale(scales[0]);
3197 }
3198 if (scales.length >= 2) {
3199 mTransitionAnimationScale = fixScale(scales[1]);
3200 }
3201 }
Romain Guy06882f82009-06-10 13:36:04 -07003202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 // Persist setting
3204 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3205 }
Romain Guy06882f82009-06-10 13:36:04 -07003206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 public float getAnimationScale(int which) {
3208 switch (which) {
3209 case 0: return mWindowAnimationScale;
3210 case 1: return mTransitionAnimationScale;
3211 }
3212 return 0;
3213 }
Romain Guy06882f82009-06-10 13:36:04 -07003214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 public float[] getAnimationScales() {
3216 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3217 }
Romain Guy06882f82009-06-10 13:36:04 -07003218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 public int getSwitchState(int sw) {
3220 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3221 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003222 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 }
3224 return KeyInputQueue.getSwitchState(sw);
3225 }
Romain Guy06882f82009-06-10 13:36:04 -07003226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 public int getSwitchStateForDevice(int devid, int sw) {
3228 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3229 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003230 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 }
3232 return KeyInputQueue.getSwitchState(devid, sw);
3233 }
Romain Guy06882f82009-06-10 13:36:04 -07003234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 public int getScancodeState(int sw) {
3236 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3237 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003238 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 }
3240 return KeyInputQueue.getScancodeState(sw);
3241 }
Romain Guy06882f82009-06-10 13:36:04 -07003242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 public int getScancodeStateForDevice(int devid, int sw) {
3244 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3245 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003246 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 }
3248 return KeyInputQueue.getScancodeState(devid, sw);
3249 }
Romain Guy06882f82009-06-10 13:36:04 -07003250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 public int getKeycodeState(int sw) {
3252 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3253 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003254 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 }
3256 return KeyInputQueue.getKeycodeState(sw);
3257 }
Romain Guy06882f82009-06-10 13:36:04 -07003258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 public int getKeycodeStateForDevice(int devid, int sw) {
3260 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3261 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003262 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 }
3264 return KeyInputQueue.getKeycodeState(devid, sw);
3265 }
Romain Guy06882f82009-06-10 13:36:04 -07003266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3268 return KeyInputQueue.hasKeys(keycodes, keyExists);
3269 }
Romain Guy06882f82009-06-10 13:36:04 -07003270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 public void enableScreenAfterBoot() {
3272 synchronized(mWindowMap) {
3273 if (mSystemBooted) {
3274 return;
3275 }
3276 mSystemBooted = true;
3277 }
Romain Guy06882f82009-06-10 13:36:04 -07003278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 performEnableScreen();
3280 }
Romain Guy06882f82009-06-10 13:36:04 -07003281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 public void enableScreenIfNeededLocked() {
3283 if (mDisplayEnabled) {
3284 return;
3285 }
3286 if (!mSystemBooted) {
3287 return;
3288 }
3289 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3290 }
Romain Guy06882f82009-06-10 13:36:04 -07003291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 public void performEnableScreen() {
3293 synchronized(mWindowMap) {
3294 if (mDisplayEnabled) {
3295 return;
3296 }
3297 if (!mSystemBooted) {
3298 return;
3299 }
Romain Guy06882f82009-06-10 13:36:04 -07003300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 // Don't enable the screen until all existing windows
3302 // have been drawn.
3303 final int N = mWindows.size();
3304 for (int i=0; i<N; i++) {
3305 WindowState w = (WindowState)mWindows.get(i);
3306 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3307 return;
3308 }
3309 }
Romain Guy06882f82009-06-10 13:36:04 -07003310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 mDisplayEnabled = true;
3312 if (false) {
3313 Log.i(TAG, "ENABLING SCREEN!");
3314 StringWriter sw = new StringWriter();
3315 PrintWriter pw = new PrintWriter(sw);
3316 this.dump(null, pw, null);
3317 Log.i(TAG, sw.toString());
3318 }
3319 try {
3320 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3321 if (surfaceFlinger != null) {
3322 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3323 Parcel data = Parcel.obtain();
3324 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3325 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3326 data, null, 0);
3327 data.recycle();
3328 }
3329 } catch (RemoteException ex) {
3330 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3331 }
3332 }
Romain Guy06882f82009-06-10 13:36:04 -07003333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003337 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3338 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 }
Romain Guy06882f82009-06-10 13:36:04 -07003340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 public void setInTouchMode(boolean mode) {
3342 synchronized(mWindowMap) {
3343 mInTouchMode = mode;
3344 }
3345 }
3346
Romain Guy06882f82009-06-10 13:36:04 -07003347 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003348 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003350 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003351 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 }
3353
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003354 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 }
Romain Guy06882f82009-06-10 13:36:04 -07003356
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003357 public void setRotationUnchecked(int rotation,
3358 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 if(DEBUG_ORIENTATION) Log.v(TAG,
3360 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 long origId = Binder.clearCallingIdentity();
3363 boolean changed;
3364 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003365 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 }
Romain Guy06882f82009-06-10 13:36:04 -07003367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 if (changed) {
3369 sendNewConfiguration();
3370 synchronized(mWindowMap) {
3371 mLayoutNeeded = true;
3372 performLayoutAndPlaceSurfacesLocked();
3373 }
3374 } else if (alwaysSendConfiguration) {
3375 //update configuration ignoring orientation change
3376 sendNewConfiguration();
3377 }
Romain Guy06882f82009-06-10 13:36:04 -07003378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 Binder.restoreCallingIdentity(origId);
3380 }
Romain Guy06882f82009-06-10 13:36:04 -07003381
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003382 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 boolean changed;
3384 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3385 rotation = mRequestedRotation;
3386 } else {
3387 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003388 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 }
3390 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003391 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 mRotation, mDisplayEnabled);
3393 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3394 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003397 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 "Rotation changed to " + rotation
3399 + " from " + mRotation
3400 + " (forceApp=" + mForcedAppOrientation
3401 + ", req=" + mRequestedRotation + ")");
3402 mRotation = rotation;
3403 mWindowsFreezingScreen = true;
3404 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3405 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3406 2000);
3407 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003408 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 mQueue.setOrientation(rotation);
3410 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003411 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 }
3413 for (int i=mWindows.size()-1; i>=0; i--) {
3414 WindowState w = (WindowState)mWindows.get(i);
3415 if (w.mSurface != null) {
3416 w.mOrientationChanging = true;
3417 }
3418 }
3419 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3420 try {
3421 mRotationWatchers.get(i).onRotationChanged(rotation);
3422 } catch (RemoteException e) {
3423 }
3424 }
3425 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 return changed;
3428 }
Romain Guy06882f82009-06-10 13:36:04 -07003429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 public int getRotation() {
3431 return mRotation;
3432 }
3433
3434 public int watchRotation(IRotationWatcher watcher) {
3435 final IBinder watcherBinder = watcher.asBinder();
3436 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3437 public void binderDied() {
3438 synchronized (mWindowMap) {
3439 for (int i=0; i<mRotationWatchers.size(); i++) {
3440 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003441 IRotationWatcher removed = mRotationWatchers.remove(i);
3442 if (removed != null) {
3443 removed.asBinder().unlinkToDeath(this, 0);
3444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 i--;
3446 }
3447 }
3448 }
3449 }
3450 };
Romain Guy06882f82009-06-10 13:36:04 -07003451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 synchronized (mWindowMap) {
3453 try {
3454 watcher.asBinder().linkToDeath(dr, 0);
3455 mRotationWatchers.add(watcher);
3456 } catch (RemoteException e) {
3457 // Client died, no cleanup needed.
3458 }
Romain Guy06882f82009-06-10 13:36:04 -07003459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 return mRotation;
3461 }
3462 }
3463
3464 /**
3465 * Starts the view server on the specified port.
3466 *
3467 * @param port The port to listener to.
3468 *
3469 * @return True if the server was successfully started, false otherwise.
3470 *
3471 * @see com.android.server.ViewServer
3472 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3473 */
3474 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003475 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 return false;
3477 }
3478
3479 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3480 return false;
3481 }
3482
3483 if (port < 1024) {
3484 return false;
3485 }
3486
3487 if (mViewServer != null) {
3488 if (!mViewServer.isRunning()) {
3489 try {
3490 return mViewServer.start();
3491 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003492 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 }
3494 }
3495 return false;
3496 }
3497
3498 try {
3499 mViewServer = new ViewServer(this, port);
3500 return mViewServer.start();
3501 } catch (IOException e) {
3502 Log.w(TAG, "View server did not start");
3503 }
3504 return false;
3505 }
3506
Romain Guy06882f82009-06-10 13:36:04 -07003507 private boolean isSystemSecure() {
3508 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3509 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3510 }
3511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 /**
3513 * Stops the view server if it exists.
3514 *
3515 * @return True if the server stopped, false if it wasn't started or
3516 * couldn't be stopped.
3517 *
3518 * @see com.android.server.ViewServer
3519 */
3520 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003521 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 return false;
3523 }
3524
3525 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3526 return false;
3527 }
3528
3529 if (mViewServer != null) {
3530 return mViewServer.stop();
3531 }
3532 return false;
3533 }
3534
3535 /**
3536 * Indicates whether the view server is running.
3537 *
3538 * @return True if the server is running, false otherwise.
3539 *
3540 * @see com.android.server.ViewServer
3541 */
3542 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003543 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 return false;
3545 }
3546
3547 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3548 return false;
3549 }
3550
3551 return mViewServer != null && mViewServer.isRunning();
3552 }
3553
3554 /**
3555 * Lists all availble windows in the system. The listing is written in the
3556 * specified Socket's output stream with the following syntax:
3557 * windowHashCodeInHexadecimal windowName
3558 * Each line of the ouput represents a different window.
3559 *
3560 * @param client The remote client to send the listing to.
3561 * @return False if an error occured, true otherwise.
3562 */
3563 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003564 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 return false;
3566 }
3567
3568 boolean result = true;
3569
3570 Object[] windows;
3571 synchronized (mWindowMap) {
3572 windows = new Object[mWindows.size()];
3573 //noinspection unchecked
3574 windows = mWindows.toArray(windows);
3575 }
3576
3577 BufferedWriter out = null;
3578
3579 // Any uncaught exception will crash the system process
3580 try {
3581 OutputStream clientStream = client.getOutputStream();
3582 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3583
3584 final int count = windows.length;
3585 for (int i = 0; i < count; i++) {
3586 final WindowState w = (WindowState) windows[i];
3587 out.write(Integer.toHexString(System.identityHashCode(w)));
3588 out.write(' ');
3589 out.append(w.mAttrs.getTitle());
3590 out.write('\n');
3591 }
3592
3593 out.write("DONE.\n");
3594 out.flush();
3595 } catch (Exception e) {
3596 result = false;
3597 } finally {
3598 if (out != null) {
3599 try {
3600 out.close();
3601 } catch (IOException e) {
3602 result = false;
3603 }
3604 }
3605 }
3606
3607 return result;
3608 }
3609
3610 /**
3611 * Sends a command to a target window. The result of the command, if any, will be
3612 * written in the output stream of the specified socket.
3613 *
3614 * The parameters must follow this syntax:
3615 * windowHashcode extra
3616 *
3617 * Where XX is the length in characeters of the windowTitle.
3618 *
3619 * The first parameter is the target window. The window with the specified hashcode
3620 * will be the target. If no target can be found, nothing happens. The extra parameters
3621 * will be delivered to the target window and as parameters to the command itself.
3622 *
3623 * @param client The remote client to sent the result, if any, to.
3624 * @param command The command to execute.
3625 * @param parameters The command parameters.
3626 *
3627 * @return True if the command was successfully delivered, false otherwise. This does
3628 * not indicate whether the command itself was successful.
3629 */
3630 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003631 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 return false;
3633 }
3634
3635 boolean success = true;
3636 Parcel data = null;
3637 Parcel reply = null;
3638
3639 // Any uncaught exception will crash the system process
3640 try {
3641 // Find the hashcode of the window
3642 int index = parameters.indexOf(' ');
3643 if (index == -1) {
3644 index = parameters.length();
3645 }
3646 final String code = parameters.substring(0, index);
3647 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3648
3649 // Extract the command's parameter after the window description
3650 if (index < parameters.length()) {
3651 parameters = parameters.substring(index + 1);
3652 } else {
3653 parameters = "";
3654 }
3655
3656 final WindowManagerService.WindowState window = findWindow(hashCode);
3657 if (window == null) {
3658 return false;
3659 }
3660
3661 data = Parcel.obtain();
3662 data.writeInterfaceToken("android.view.IWindow");
3663 data.writeString(command);
3664 data.writeString(parameters);
3665 data.writeInt(1);
3666 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3667
3668 reply = Parcel.obtain();
3669
3670 final IBinder binder = window.mClient.asBinder();
3671 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3672 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3673
3674 reply.readException();
3675
3676 } catch (Exception e) {
3677 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3678 success = false;
3679 } finally {
3680 if (data != null) {
3681 data.recycle();
3682 }
3683 if (reply != null) {
3684 reply.recycle();
3685 }
3686 }
3687
3688 return success;
3689 }
3690
3691 private WindowState findWindow(int hashCode) {
3692 if (hashCode == -1) {
3693 return getFocusedWindow();
3694 }
3695
3696 synchronized (mWindowMap) {
3697 final ArrayList windows = mWindows;
3698 final int count = windows.size();
3699
3700 for (int i = 0; i < count; i++) {
3701 WindowState w = (WindowState) windows.get(i);
3702 if (System.identityHashCode(w) == hashCode) {
3703 return w;
3704 }
3705 }
3706 }
3707
3708 return null;
3709 }
3710
3711 /*
3712 * Instruct the Activity Manager to fetch the current configuration and broadcast
3713 * that to config-changed listeners if appropriate.
3714 */
3715 void sendNewConfiguration() {
3716 try {
3717 mActivityManager.updateConfiguration(null);
3718 } catch (RemoteException e) {
3719 }
3720 }
Romain Guy06882f82009-06-10 13:36:04 -07003721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 public Configuration computeNewConfiguration() {
3723 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003724 return computeNewConfigurationLocked();
3725 }
3726 }
Romain Guy06882f82009-06-10 13:36:04 -07003727
Dianne Hackbornc485a602009-03-24 22:39:49 -07003728 Configuration computeNewConfigurationLocked() {
3729 Configuration config = new Configuration();
3730 if (!computeNewConfigurationLocked(config)) {
3731 return null;
3732 }
3733 Log.i(TAG, "Config changed: " + config);
3734 long now = SystemClock.uptimeMillis();
3735 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3736 if (mFreezeGcPending != 0) {
3737 if (now > (mFreezeGcPending+1000)) {
3738 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3739 mH.removeMessages(H.FORCE_GC);
3740 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 mFreezeGcPending = now;
3742 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003743 } else {
3744 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003746 return config;
3747 }
Romain Guy06882f82009-06-10 13:36:04 -07003748
Dianne Hackbornc485a602009-03-24 22:39:49 -07003749 boolean computeNewConfigurationLocked(Configuration config) {
3750 if (mDisplay == null) {
3751 return false;
3752 }
3753 mQueue.getInputConfiguration(config);
3754 final int dw = mDisplay.getWidth();
3755 final int dh = mDisplay.getHeight();
3756 int orientation = Configuration.ORIENTATION_SQUARE;
3757 if (dw < dh) {
3758 orientation = Configuration.ORIENTATION_PORTRAIT;
3759 } else if (dw > dh) {
3760 orientation = Configuration.ORIENTATION_LANDSCAPE;
3761 }
3762 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003763
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003764 DisplayMetrics dm = new DisplayMetrics();
3765 mDisplay.getMetrics(dm);
3766 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
3767
Dianne Hackborn723738c2009-06-25 19:48:04 -07003768 if (screenLayout == Configuration.SCREENLAYOUT_UNDEFINED) {
3769 // Note we only do this once because at this point we don't
3770 // expect the screen to change in this way at runtime, and want
3771 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07003772 int longSize = dw;
3773 int shortSize = dh;
3774 if (longSize < shortSize) {
3775 int tmp = longSize;
3776 longSize = shortSize;
3777 shortSize = tmp;
3778 }
3779 longSize = (int)(longSize/dm.density);
3780 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003781
Dianne Hackborn723738c2009-06-25 19:48:04 -07003782 // These semi-magic numbers define our compatibility modes for
3783 // applications with different screens. Don't change unless you
3784 // make sure to test lots and lots of apps!
3785 if (longSize < 470) {
3786 // This is shorter than an HVGA normal density screen (which
3787 // is 480 pixels on its long side).
3788 screenLayout = Configuration.SCREENLAYOUT_SMALL;
3789 } else if (longSize > 490 && shortSize > 330) {
3790 // This is larger than an HVGA normal density screen (which
3791 // is 480x320 pixels).
3792 screenLayout = Configuration.SCREENLAYOUT_LARGE;
3793 } else {
3794 screenLayout = Configuration.SCREENLAYOUT_NORMAL;
3795 }
3796 }
3797 config.screenLayout = screenLayout;
3798
Dianne Hackbornc485a602009-03-24 22:39:49 -07003799 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3800 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3801 mPolicy.adjustConfigurationLw(config);
3802 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 }
Romain Guy06882f82009-06-10 13:36:04 -07003804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 // -------------------------------------------------------------
3806 // Input Events and Focus Management
3807 // -------------------------------------------------------------
3808
3809 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07003810 long curTime = SystemClock.uptimeMillis();
3811
Michael Chane10de972009-05-18 11:24:50 -07003812 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07003813 if (mLastTouchEventType == eventType &&
3814 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
3815 return;
3816 }
3817 mLastUserActivityCallTime = curTime;
3818 mLastTouchEventType = eventType;
3819 }
3820
3821 if (targetWin == null
3822 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
3823 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 }
3825 }
3826
3827 // tells if it's a cheek event or not -- this function is stateful
3828 private static final int EVENT_NONE = 0;
3829 private static final int EVENT_UNKNOWN = 0;
3830 private static final int EVENT_CHEEK = 0;
3831 private static final int EVENT_IGNORE_DURATION = 300; // ms
3832 private static final float CHEEK_THRESHOLD = 0.6f;
3833 private int mEventState = EVENT_NONE;
3834 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 private int eventType(MotionEvent ev) {
3837 float size = ev.getSize();
3838 switch (ev.getAction()) {
3839 case MotionEvent.ACTION_DOWN:
3840 mEventSize = size;
3841 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
3842 case MotionEvent.ACTION_UP:
3843 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003844 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003845 case MotionEvent.ACTION_MOVE:
3846 final int N = ev.getHistorySize();
3847 if (size > mEventSize) mEventSize = size;
3848 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3849 for (int i=0; i<N; i++) {
3850 size = ev.getHistoricalSize(i);
3851 if (size > mEventSize) mEventSize = size;
3852 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3853 }
3854 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
3855 return TOUCH_EVENT;
3856 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003857 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 }
3859 default:
3860 // not good
3861 return OTHER_EVENT;
3862 }
3863 }
3864
3865 /**
3866 * @return Returns true if event was dispatched, false if it was dropped for any reason
3867 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07003868 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
3870 "dispatchPointer " + ev);
3871
Michael Chan53071d62009-05-13 17:29:48 -07003872 if (MEASURE_LATENCY) {
3873 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
3874 }
3875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07003877 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878
Michael Chan53071d62009-05-13 17:29:48 -07003879 if (MEASURE_LATENCY) {
3880 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
3881 }
3882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07003884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 if (action == MotionEvent.ACTION_UP) {
3886 // let go of our target
3887 mKeyWaiter.mMotionTarget = null;
3888 mPowerManager.logPointerUpEvent();
3889 } else if (action == MotionEvent.ACTION_DOWN) {
3890 mPowerManager.logPointerDownEvent();
3891 }
3892
3893 if (targetObj == null) {
3894 // In this case we are either dropping the event, or have received
3895 // a move or up without a down. It is common to receive move
3896 // events in such a way, since this means the user is moving the
3897 // pointer without actually pressing down. All other cases should
3898 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07003899 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
3901 }
3902 if (qev != null) {
3903 mQueue.recycleEvent(qev);
3904 }
3905 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003906 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 }
3908 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
3909 if (qev != null) {
3910 mQueue.recycleEvent(qev);
3911 }
3912 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003913 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 }
Romain Guy06882f82009-06-10 13:36:04 -07003915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07003917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003918 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07003919 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920
3921 //Log.i(TAG, "Sending " + ev + " to " + target);
3922
3923 if (uid != 0 && uid != target.mSession.mUid) {
3924 if (mContext.checkPermission(
3925 android.Manifest.permission.INJECT_EVENTS, pid, uid)
3926 != PackageManager.PERMISSION_GRANTED) {
3927 Log.w(TAG, "Permission denied: injecting pointer event from pid "
3928 + pid + " uid " + uid + " to window " + target
3929 + " owned by uid " + target.mSession.mUid);
3930 if (qev != null) {
3931 mQueue.recycleEvent(qev);
3932 }
3933 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003934 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 }
3936 }
3937
Michael Chan53071d62009-05-13 17:29:48 -07003938 if (MEASURE_LATENCY) {
3939 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
3940 }
3941
Romain Guy06882f82009-06-10 13:36:04 -07003942 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
3944 //target wants to ignore fat touch events
3945 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
3946 //explicit flag to return without processing event further
3947 boolean returnFlag = false;
3948 if((action == MotionEvent.ACTION_DOWN)) {
3949 mFatTouch = false;
3950 if(cheekPress) {
3951 mFatTouch = true;
3952 returnFlag = true;
3953 }
3954 } else {
3955 if(action == MotionEvent.ACTION_UP) {
3956 if(mFatTouch) {
3957 //earlier even was invalid doesnt matter if current up is cheekpress or not
3958 mFatTouch = false;
3959 returnFlag = true;
3960 } else if(cheekPress) {
3961 //cancel the earlier event
3962 ev.setAction(MotionEvent.ACTION_CANCEL);
3963 action = MotionEvent.ACTION_CANCEL;
3964 }
3965 } else if(action == MotionEvent.ACTION_MOVE) {
3966 if(mFatTouch) {
3967 //two cases here
3968 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07003969 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 returnFlag = true;
3971 } else if(cheekPress) {
3972 //valid down followed by invalid moves
3973 //an invalid move have to cancel earlier action
3974 ev.setAction(MotionEvent.ACTION_CANCEL);
3975 action = MotionEvent.ACTION_CANCEL;
3976 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
3977 //note that the subsequent invalid moves will not get here
3978 mFatTouch = true;
3979 }
3980 }
3981 } //else if action
3982 if(returnFlag) {
3983 //recycle que, ev
3984 if (qev != null) {
3985 mQueue.recycleEvent(qev);
3986 }
3987 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003988 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 }
3990 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07003991
3992 // TODO remove once we settle on a value or make it app specific
3993 if (action == MotionEvent.ACTION_DOWN) {
3994 int max_events_per_sec = 35;
3995 try {
3996 max_events_per_sec = Integer.parseInt(SystemProperties
3997 .get("windowsmgr.max_events_per_sec"));
3998 if (max_events_per_sec < 1) {
3999 max_events_per_sec = 35;
4000 }
4001 } catch (NumberFormatException e) {
4002 }
4003 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4004 }
4005
4006 /*
4007 * Throttle events to minimize CPU usage when there's a flood of events
4008 * e.g. constant contact with the screen
4009 */
4010 if (action == MotionEvent.ACTION_MOVE) {
4011 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4012 long now = SystemClock.uptimeMillis();
4013 if (now < nextEventTime) {
4014 try {
4015 Thread.sleep(nextEventTime - now);
4016 } catch (InterruptedException e) {
4017 }
4018 mLastTouchEventTime = nextEventTime;
4019 } else {
4020 mLastTouchEventTime = now;
4021 }
4022 }
4023
Michael Chan53071d62009-05-13 17:29:48 -07004024 if (MEASURE_LATENCY) {
4025 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4026 }
4027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 synchronized(mWindowMap) {
4029 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4030 mKeyWaiter.bindTargetWindowLocked(target,
4031 KeyWaiter.RETURN_PENDING_POINTER, qev);
4032 ev = null;
4033 } else {
4034 if (action == MotionEvent.ACTION_DOWN) {
4035 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4036 if (out != null) {
4037 MotionEvent oev = MotionEvent.obtain(ev);
4038 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4039 do {
4040 final Rect frame = out.mFrame;
4041 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4042 try {
4043 out.mClient.dispatchPointer(oev, eventTime);
4044 } catch (android.os.RemoteException e) {
4045 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4046 }
4047 oev.offsetLocation((float)frame.left, (float)frame.top);
4048 out = out.mNextOutsideTouch;
4049 } while (out != null);
4050 mKeyWaiter.mOutsideTouchTargets = null;
4051 }
4052 }
4053 final Rect frame = target.mFrame;
4054 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4055 mKeyWaiter.bindTargetWindowLocked(target);
4056 }
4057 }
Romain Guy06882f82009-06-10 13:36:04 -07004058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 // finally offset the event to the target's coordinate system and
4060 // dispatch the event.
4061 try {
4062 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4063 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4064 }
Michael Chan53071d62009-05-13 17:29:48 -07004065
4066 if (MEASURE_LATENCY) {
4067 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4068 }
4069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 target.mClient.dispatchPointer(ev, eventTime);
Michael Chan53071d62009-05-13 17:29:48 -07004071
4072 if (MEASURE_LATENCY) {
4073 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4074 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004075 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076 } catch (android.os.RemoteException e) {
4077 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4078 mKeyWaiter.mMotionTarget = null;
4079 try {
4080 removeWindow(target.mSession, target.mClient);
4081 } catch (java.util.NoSuchElementException ex) {
4082 // This will happen if the window has already been
4083 // removed.
4084 }
4085 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004086 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 }
Romain Guy06882f82009-06-10 13:36:04 -07004088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 /**
4090 * @return Returns true if event was dispatched, false if it was dropped for any reason
4091 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004092 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 if (DEBUG_INPUT) Log.v(
4094 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004097 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 if (focusObj == null) {
4099 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4100 if (qev != null) {
4101 mQueue.recycleEvent(qev);
4102 }
4103 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004104 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 }
4106 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4107 if (qev != null) {
4108 mQueue.recycleEvent(qev);
4109 }
4110 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004111 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 }
Romain Guy06882f82009-06-10 13:36:04 -07004113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 if (uid != 0 && uid != focus.mSession.mUid) {
4117 if (mContext.checkPermission(
4118 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4119 != PackageManager.PERMISSION_GRANTED) {
4120 Log.w(TAG, "Permission denied: injecting key event from pid "
4121 + pid + " uid " + uid + " to window " + focus
4122 + " owned by uid " + focus.mSession.mUid);
4123 if (qev != null) {
4124 mQueue.recycleEvent(qev);
4125 }
4126 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004127 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 }
4129 }
Romain Guy06882f82009-06-10 13:36:04 -07004130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 synchronized(mWindowMap) {
4134 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4135 mKeyWaiter.bindTargetWindowLocked(focus,
4136 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4137 // We don't deliver movement events to the client, we hold
4138 // them and wait for them to call back.
4139 ev = null;
4140 } else {
4141 mKeyWaiter.bindTargetWindowLocked(focus);
4142 }
4143 }
Romain Guy06882f82009-06-10 13:36:04 -07004144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 try {
4146 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004147 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 } catch (android.os.RemoteException e) {
4149 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4150 try {
4151 removeWindow(focus.mSession, focus.mClient);
4152 } catch (java.util.NoSuchElementException ex) {
4153 // This will happen if the window has already been
4154 // removed.
4155 }
4156 }
Romain Guy06882f82009-06-10 13:36:04 -07004157
Dianne Hackborncfaef692009-06-15 14:24:44 -07004158 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 }
Romain Guy06882f82009-06-10 13:36:04 -07004160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 /**
4162 * @return Returns true if event was dispatched, false if it was dropped for any reason
4163 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004164 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4166
4167 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004168 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 if (focusObj == null) {
4170 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004171 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 }
4173 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004174 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 }
Romain Guy06882f82009-06-10 13:36:04 -07004176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 if (DEBUG_INPUT) Log.v(
4180 TAG, "Dispatching to " + focus + ": " + event);
4181
4182 if (uid != 0 && uid != focus.mSession.mUid) {
4183 if (mContext.checkPermission(
4184 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4185 != PackageManager.PERMISSION_GRANTED) {
4186 Log.w(TAG, "Permission denied: injecting key event from pid "
4187 + pid + " uid " + uid + " to window " + focus
4188 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004189 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 }
4191 }
Romain Guy06882f82009-06-10 13:36:04 -07004192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 synchronized(mWindowMap) {
4194 mKeyWaiter.bindTargetWindowLocked(focus);
4195 }
4196
4197 // NOSHIP extra state logging
4198 mKeyWaiter.recordDispatchState(event, focus);
4199 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 try {
4202 if (DEBUG_INPUT || DEBUG_FOCUS) {
4203 Log.v(TAG, "Delivering key " + event.getKeyCode()
4204 + " to " + focus);
4205 }
4206 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004207 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 } catch (android.os.RemoteException e) {
4209 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4210 try {
4211 removeWindow(focus.mSession, focus.mClient);
4212 } catch (java.util.NoSuchElementException ex) {
4213 // This will happen if the window has already been
4214 // removed.
4215 }
4216 }
Romain Guy06882f82009-06-10 13:36:04 -07004217
Dianne Hackborncfaef692009-06-15 14:24:44 -07004218 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 }
Romain Guy06882f82009-06-10 13:36:04 -07004220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 public void pauseKeyDispatching(IBinder _token) {
4222 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4223 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004224 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 }
4226
4227 synchronized (mWindowMap) {
4228 WindowToken token = mTokenMap.get(_token);
4229 if (token != null) {
4230 mKeyWaiter.pauseDispatchingLocked(token);
4231 }
4232 }
4233 }
4234
4235 public void resumeKeyDispatching(IBinder _token) {
4236 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4237 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004238 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 }
4240
4241 synchronized (mWindowMap) {
4242 WindowToken token = mTokenMap.get(_token);
4243 if (token != null) {
4244 mKeyWaiter.resumeDispatchingLocked(token);
4245 }
4246 }
4247 }
4248
4249 public void setEventDispatching(boolean enabled) {
4250 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4251 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004252 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 }
4254
4255 synchronized (mWindowMap) {
4256 mKeyWaiter.setEventDispatchingLocked(enabled);
4257 }
4258 }
Romain Guy06882f82009-06-10 13:36:04 -07004259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 /**
4261 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004262 *
4263 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004264 * {@link SystemClock#uptimeMillis()} as the timebase.)
4265 * @param sync If true, wait for the event to be completed before returning to the caller.
4266 * @return Returns true if event was dispatched, false if it was dropped for any reason
4267 */
4268 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4269 long downTime = ev.getDownTime();
4270 long eventTime = ev.getEventTime();
4271
4272 int action = ev.getAction();
4273 int code = ev.getKeyCode();
4274 int repeatCount = ev.getRepeatCount();
4275 int metaState = ev.getMetaState();
4276 int deviceId = ev.getDeviceId();
4277 int scancode = ev.getScanCode();
4278
4279 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4280 if (downTime == 0) downTime = eventTime;
4281
4282 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004283 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004284
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004285 final int pid = Binder.getCallingPid();
4286 final int uid = Binder.getCallingUid();
4287 final long ident = Binder.clearCallingIdentity();
4288 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004290 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004292 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004293 switch (result) {
4294 case INJECT_NO_PERMISSION:
4295 throw new SecurityException(
4296 "Injecting to another application requires INJECT_EVENT permission");
4297 case INJECT_SUCCEEDED:
4298 return true;
4299 }
4300 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 }
4302
4303 /**
4304 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004305 *
4306 * @param ev A motion event describing the pointer (touch) action. (As noted in
4307 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004308 * {@link SystemClock#uptimeMillis()} as the timebase.)
4309 * @param sync If true, wait for the event to be completed before returning to the caller.
4310 * @return Returns true if event was dispatched, false if it was dropped for any reason
4311 */
4312 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004313 final int pid = Binder.getCallingPid();
4314 final int uid = Binder.getCallingUid();
4315 final long ident = Binder.clearCallingIdentity();
4316 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004318 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004320 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004321 switch (result) {
4322 case INJECT_NO_PERMISSION:
4323 throw new SecurityException(
4324 "Injecting to another application requires INJECT_EVENT permission");
4325 case INJECT_SUCCEEDED:
4326 return true;
4327 }
4328 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 }
Romain Guy06882f82009-06-10 13:36:04 -07004330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 /**
4332 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004333 *
4334 * @param ev A motion event describing the trackball action. (As noted in
4335 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 * {@link SystemClock#uptimeMillis()} as the timebase.)
4337 * @param sync If true, wait for the event to be completed before returning to the caller.
4338 * @return Returns true if event was dispatched, false if it was dropped for any reason
4339 */
4340 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004341 final int pid = Binder.getCallingPid();
4342 final int uid = Binder.getCallingUid();
4343 final long ident = Binder.clearCallingIdentity();
4344 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004346 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004348 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004349 switch (result) {
4350 case INJECT_NO_PERMISSION:
4351 throw new SecurityException(
4352 "Injecting to another application requires INJECT_EVENT permission");
4353 case INJECT_SUCCEEDED:
4354 return true;
4355 }
4356 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 }
Romain Guy06882f82009-06-10 13:36:04 -07004358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 private WindowState getFocusedWindow() {
4360 synchronized (mWindowMap) {
4361 return getFocusedWindowLocked();
4362 }
4363 }
4364
4365 private WindowState getFocusedWindowLocked() {
4366 return mCurrentFocus;
4367 }
Romain Guy06882f82009-06-10 13:36:04 -07004368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 /**
4370 * This class holds the state for dispatching key events. This state
4371 * is protected by the KeyWaiter instance, NOT by the window lock. You
4372 * can be holding the main window lock while acquire the KeyWaiter lock,
4373 * but not the other way around.
4374 */
4375 final class KeyWaiter {
4376 // NOSHIP debugging
4377 public class DispatchState {
4378 private KeyEvent event;
4379 private WindowState focus;
4380 private long time;
4381 private WindowState lastWin;
4382 private IBinder lastBinder;
4383 private boolean finished;
4384 private boolean gotFirstWindow;
4385 private boolean eventDispatching;
4386 private long timeToSwitch;
4387 private boolean wasFrozen;
4388 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004389 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4392 focus = theFocus;
4393 event = theEvent;
4394 time = System.currentTimeMillis();
4395 // snapshot KeyWaiter state
4396 lastWin = mLastWin;
4397 lastBinder = mLastBinder;
4398 finished = mFinished;
4399 gotFirstWindow = mGotFirstWindow;
4400 eventDispatching = mEventDispatching;
4401 timeToSwitch = mTimeToSwitch;
4402 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004403 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 // cache the paused state at ctor time as well
4405 if (theFocus == null || theFocus.mToken == null) {
4406 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4407 focusPaused = false;
4408 } else {
4409 focusPaused = theFocus.mToken.paused;
4410 }
4411 }
Romain Guy06882f82009-06-10 13:36:04 -07004412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 public String toString() {
4414 return "{{" + event + " to " + focus + " @ " + time
4415 + " lw=" + lastWin + " lb=" + lastBinder
4416 + " fin=" + finished + " gfw=" + gotFirstWindow
4417 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004418 + " wf=" + wasFrozen + " fp=" + focusPaused
4419 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 }
4421 };
4422 private DispatchState mDispatchState = null;
4423 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4424 mDispatchState = new DispatchState(theEvent, theFocus);
4425 }
4426 // END NOSHIP
4427
4428 public static final int RETURN_NOTHING = 0;
4429 public static final int RETURN_PENDING_POINTER = 1;
4430 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 final Object SKIP_TARGET_TOKEN = new Object();
4433 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 private WindowState mLastWin = null;
4436 private IBinder mLastBinder = null;
4437 private boolean mFinished = true;
4438 private boolean mGotFirstWindow = false;
4439 private boolean mEventDispatching = true;
4440 private long mTimeToSwitch = 0;
4441 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 // Target of Motion events
4444 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004446 // Windows above the target who would like to receive an "outside"
4447 // touch event for any down events outside of them.
4448 WindowState mOutsideTouchTargets;
4449
4450 /**
4451 * Wait for the last event dispatch to complete, then find the next
4452 * target that should receive the given event and wait for that one
4453 * to be ready to receive it.
4454 */
4455 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4456 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004457 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 long startTime = SystemClock.uptimeMillis();
4459 long keyDispatchingTimeout = 5 * 1000;
4460 long waitedFor = 0;
4461
4462 while (true) {
4463 // Figure out which window we care about. It is either the
4464 // last window we are waiting to have process the event or,
4465 // if none, then the next window we think the event should go
4466 // to. Note: we retrieve mLastWin outside of the lock, so
4467 // it may change before we lock. Thus we must check it again.
4468 WindowState targetWin = mLastWin;
4469 boolean targetIsNew = targetWin == null;
4470 if (DEBUG_INPUT) Log.v(
4471 TAG, "waitForLastKey: mFinished=" + mFinished +
4472 ", mLastWin=" + mLastWin);
4473 if (targetIsNew) {
4474 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004475 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 if (target == SKIP_TARGET_TOKEN) {
4477 // The user has pressed a special key, and we are
4478 // dropping all pending events before it.
4479 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4480 + " " + nextMotion);
4481 return null;
4482 }
4483 if (target == CONSUMED_EVENT_TOKEN) {
4484 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4485 + " " + nextMotion);
4486 return target;
4487 }
4488 targetWin = (WindowState)target;
4489 }
Romain Guy06882f82009-06-10 13:36:04 -07004490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004491 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 // Now: is it okay to send the next event to this window?
4494 synchronized (this) {
4495 // First: did we come here based on the last window not
4496 // being null, but it changed by the time we got here?
4497 // If so, try again.
4498 if (!targetIsNew && mLastWin == null) {
4499 continue;
4500 }
Romain Guy06882f82009-06-10 13:36:04 -07004501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004502 // We never dispatch events if not finished with the
4503 // last one, or the display is frozen.
4504 if (mFinished && !mDisplayFrozen) {
4505 // If event dispatching is disabled, then we
4506 // just consume the events.
4507 if (!mEventDispatching) {
4508 if (DEBUG_INPUT) Log.v(TAG,
4509 "Skipping event; dispatching disabled: "
4510 + nextKey + " " + nextMotion);
4511 return null;
4512 }
4513 if (targetWin != null) {
4514 // If this is a new target, and that target is not
4515 // paused or unresponsive, then all looks good to
4516 // handle the event.
4517 if (targetIsNew && !targetWin.mToken.paused) {
4518 return targetWin;
4519 }
Romain Guy06882f82009-06-10 13:36:04 -07004520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 // If we didn't find a target window, and there is no
4522 // focused app window, then just eat the events.
4523 } else if (mFocusedApp == null) {
4524 if (DEBUG_INPUT) Log.v(TAG,
4525 "Skipping event; no focused app: "
4526 + nextKey + " " + nextMotion);
4527 return null;
4528 }
4529 }
Romain Guy06882f82009-06-10 13:36:04 -07004530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531 if (DEBUG_INPUT) Log.v(
4532 TAG, "Waiting for last key in " + mLastBinder
4533 + " target=" + targetWin
4534 + " mFinished=" + mFinished
4535 + " mDisplayFrozen=" + mDisplayFrozen
4536 + " targetIsNew=" + targetIsNew
4537 + " paused="
4538 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004539 + " mFocusedApp=" + mFocusedApp
4540 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 targetApp = targetWin != null
4543 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004545 long curTimeout = keyDispatchingTimeout;
4546 if (mTimeToSwitch != 0) {
4547 long now = SystemClock.uptimeMillis();
4548 if (mTimeToSwitch <= now) {
4549 // If an app switch key has been pressed, and we have
4550 // waited too long for the current app to finish
4551 // processing keys, then wait no more!
4552 doFinishedKeyLocked(true);
4553 continue;
4554 }
4555 long switchTimeout = mTimeToSwitch - now;
4556 if (curTimeout > switchTimeout) {
4557 curTimeout = switchTimeout;
4558 }
4559 }
Romain Guy06882f82009-06-10 13:36:04 -07004560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004561 try {
4562 // after that continue
4563 // processing keys, so we don't get stuck.
4564 if (DEBUG_INPUT) Log.v(
4565 TAG, "Waiting for key dispatch: " + curTimeout);
4566 wait(curTimeout);
4567 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4568 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004569 + startTime + " switchTime=" + mTimeToSwitch
4570 + " target=" + targetWin + " mLW=" + mLastWin
4571 + " mLB=" + mLastBinder + " fin=" + mFinished
4572 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004573 } catch (InterruptedException e) {
4574 }
4575 }
4576
4577 // If we were frozen during configuration change, restart the
4578 // timeout checks from now; otherwise look at whether we timed
4579 // out before awakening.
4580 if (mWasFrozen) {
4581 waitedFor = 0;
4582 mWasFrozen = false;
4583 } else {
4584 waitedFor = SystemClock.uptimeMillis() - startTime;
4585 }
4586
4587 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4588 IApplicationToken at = null;
4589 synchronized (this) {
4590 Log.w(TAG, "Key dispatching timed out sending to " +
4591 (targetWin != null ? targetWin.mAttrs.getTitle()
4592 : "<null>"));
4593 // NOSHIP debugging
4594 Log.w(TAG, "Dispatch state: " + mDispatchState);
4595 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4596 // END NOSHIP
4597 //dump();
4598 if (targetWin != null) {
4599 at = targetWin.getAppToken();
4600 } else if (targetApp != null) {
4601 at = targetApp.appToken;
4602 }
4603 }
4604
4605 boolean abort = true;
4606 if (at != null) {
4607 try {
4608 long timeout = at.getKeyDispatchingTimeout();
4609 if (timeout > waitedFor) {
4610 // we did not wait the proper amount of time for this application.
4611 // set the timeout to be the real timeout and wait again.
4612 keyDispatchingTimeout = timeout - waitedFor;
4613 continue;
4614 } else {
4615 abort = at.keyDispatchingTimedOut();
4616 }
4617 } catch (RemoteException ex) {
4618 }
4619 }
4620
4621 synchronized (this) {
4622 if (abort && (mLastWin == targetWin || targetWin == null)) {
4623 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004624 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 if (DEBUG_INPUT) Log.v(TAG,
4626 "Window " + mLastWin +
4627 " timed out on key input");
4628 if (mLastWin.mToken.paused) {
4629 Log.w(TAG, "Un-pausing dispatching to this window");
4630 mLastWin.mToken.paused = false;
4631 }
4632 }
4633 if (mMotionTarget == targetWin) {
4634 mMotionTarget = null;
4635 }
4636 mLastWin = null;
4637 mLastBinder = null;
4638 if (failIfTimeout || targetWin == null) {
4639 return null;
4640 }
4641 } else {
4642 Log.w(TAG, "Continuing to wait for key to be dispatched");
4643 startTime = SystemClock.uptimeMillis();
4644 }
4645 }
4646 }
4647 }
4648 }
Romain Guy06882f82009-06-10 13:36:04 -07004649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004650 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004651 MotionEvent nextMotion, boolean isPointerEvent,
4652 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655 if (nextKey != null) {
4656 // Find the target window for a normal key event.
4657 final int keycode = nextKey.getKeyCode();
4658 final int repeatCount = nextKey.getRepeatCount();
4659 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4660 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004662 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004663 if (callingUid == 0 ||
4664 mContext.checkPermission(
4665 android.Manifest.permission.INJECT_EVENTS,
4666 callingPid, callingUid)
4667 == PackageManager.PERMISSION_GRANTED) {
4668 mPolicy.interceptKeyTi(null, keycode,
4669 nextKey.getMetaState(), down, repeatCount);
4670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004671 Log.w(TAG, "Event timeout during app switch: dropping "
4672 + nextKey);
4673 return SKIP_TARGET_TOKEN;
4674 }
Romain Guy06882f82009-06-10 13:36:04 -07004675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004676 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 WindowState focus = null;
4679 synchronized(mWindowMap) {
4680 focus = getFocusedWindowLocked();
4681 }
Romain Guy06882f82009-06-10 13:36:04 -07004682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004684
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004685 if (callingUid == 0 ||
4686 (focus != null && callingUid == focus.mSession.mUid) ||
4687 mContext.checkPermission(
4688 android.Manifest.permission.INJECT_EVENTS,
4689 callingPid, callingUid)
4690 == PackageManager.PERMISSION_GRANTED) {
4691 if (mPolicy.interceptKeyTi(focus,
4692 keycode, nextKey.getMetaState(), down, repeatCount)) {
4693 return CONSUMED_EVENT_TOKEN;
4694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 }
Romain Guy06882f82009-06-10 13:36:04 -07004696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 } else if (!isPointerEvent) {
4700 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4701 if (!dispatch) {
4702 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4703 + nextMotion);
4704 return SKIP_TARGET_TOKEN;
4705 }
Romain Guy06882f82009-06-10 13:36:04 -07004706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 WindowState focus = null;
4708 synchronized(mWindowMap) {
4709 focus = getFocusedWindowLocked();
4710 }
Romain Guy06882f82009-06-10 13:36:04 -07004711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004712 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4713 return focus;
4714 }
Romain Guy06882f82009-06-10 13:36:04 -07004715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 if (nextMotion == null) {
4717 return SKIP_TARGET_TOKEN;
4718 }
Romain Guy06882f82009-06-10 13:36:04 -07004719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4721 KeyEvent.KEYCODE_UNKNOWN);
4722 if (!dispatch) {
4723 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4724 + nextMotion);
4725 return SKIP_TARGET_TOKEN;
4726 }
Romain Guy06882f82009-06-10 13:36:04 -07004727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004728 // Find the target window for a pointer event.
4729 int action = nextMotion.getAction();
4730 final float xf = nextMotion.getX();
4731 final float yf = nextMotion.getY();
4732 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 final boolean screenWasOff = qev != null
4735 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07004736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004737 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07004738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 synchronized(mWindowMap) {
4740 synchronized (this) {
4741 if (action == MotionEvent.ACTION_DOWN) {
4742 if (mMotionTarget != null) {
4743 // this is weird, we got a pen down, but we thought it was
4744 // already down!
4745 // XXX: We should probably send an ACTION_UP to the current
4746 // target.
4747 Log.w(TAG, "Pointer down received while already down in: "
4748 + mMotionTarget);
4749 mMotionTarget = null;
4750 }
Romain Guy06882f82009-06-10 13:36:04 -07004751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004752 // ACTION_DOWN is special, because we need to lock next events to
4753 // the window we'll land onto.
4754 final int x = (int)xf;
4755 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07004756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 final ArrayList windows = mWindows;
4758 final int N = windows.size();
4759 WindowState topErrWindow = null;
4760 final Rect tmpRect = mTempRect;
4761 for (int i=N-1; i>=0; i--) {
4762 WindowState child = (WindowState)windows.get(i);
4763 //Log.i(TAG, "Checking dispatch to: " + child);
4764 final int flags = child.mAttrs.flags;
4765 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4766 if (topErrWindow == null) {
4767 topErrWindow = child;
4768 }
4769 }
4770 if (!child.isVisibleLw()) {
4771 //Log.i(TAG, "Not visible!");
4772 continue;
4773 }
4774 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4775 //Log.i(TAG, "Not touchable!");
4776 if ((flags & WindowManager.LayoutParams
4777 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4778 child.mNextOutsideTouch = mOutsideTouchTargets;
4779 mOutsideTouchTargets = child;
4780 }
4781 continue;
4782 }
4783 tmpRect.set(child.mFrame);
4784 if (child.mTouchableInsets == ViewTreeObserver
4785 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4786 // The touch is inside of the window if it is
4787 // inside the frame, AND the content part of that
4788 // frame that was given by the application.
4789 tmpRect.left += child.mGivenContentInsets.left;
4790 tmpRect.top += child.mGivenContentInsets.top;
4791 tmpRect.right -= child.mGivenContentInsets.right;
4792 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4793 } else if (child.mTouchableInsets == ViewTreeObserver
4794 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4795 // The touch is inside of the window if it is
4796 // inside the frame, AND the visible part of that
4797 // frame that was given by the application.
4798 tmpRect.left += child.mGivenVisibleInsets.left;
4799 tmpRect.top += child.mGivenVisibleInsets.top;
4800 tmpRect.right -= child.mGivenVisibleInsets.right;
4801 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
4802 }
4803 final int touchFlags = flags &
4804 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
4805 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
4806 if (tmpRect.contains(x, y) || touchFlags == 0) {
4807 //Log.i(TAG, "Using this target!");
4808 if (!screenWasOff || (flags &
4809 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
4810 mMotionTarget = child;
4811 } else {
4812 //Log.i(TAG, "Waking, skip!");
4813 mMotionTarget = null;
4814 }
4815 break;
4816 }
Romain Guy06882f82009-06-10 13:36:04 -07004817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 if ((flags & WindowManager.LayoutParams
4819 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4820 child.mNextOutsideTouch = mOutsideTouchTargets;
4821 mOutsideTouchTargets = child;
4822 //Log.i(TAG, "Adding to outside target list: " + child);
4823 }
4824 }
4825
4826 // if there's an error window but it's not accepting
4827 // focus (typically because it is not yet visible) just
4828 // wait for it -- any other focused window may in fact
4829 // be in ANR state.
4830 if (topErrWindow != null && mMotionTarget != topErrWindow) {
4831 mMotionTarget = null;
4832 }
4833 }
Romain Guy06882f82009-06-10 13:36:04 -07004834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004835 target = mMotionTarget;
4836 }
4837 }
Romain Guy06882f82009-06-10 13:36:04 -07004838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004839 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07004840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 // Pointer events are a little different -- if there isn't a
4842 // target found for any event, then just drop it.
4843 return target != null ? target : SKIP_TARGET_TOKEN;
4844 }
Romain Guy06882f82009-06-10 13:36:04 -07004845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 boolean checkShouldDispatchKey(int keycode) {
4847 synchronized (this) {
4848 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
4849 mTimeToSwitch = 0;
4850 return true;
4851 }
4852 if (mTimeToSwitch != 0
4853 && mTimeToSwitch < SystemClock.uptimeMillis()) {
4854 return false;
4855 }
4856 return true;
4857 }
4858 }
Romain Guy06882f82009-06-10 13:36:04 -07004859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860 void bindTargetWindowLocked(WindowState win,
4861 int pendingWhat, QueuedEvent pendingMotion) {
4862 synchronized (this) {
4863 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
4864 }
4865 }
Romain Guy06882f82009-06-10 13:36:04 -07004866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867 void bindTargetWindowLocked(WindowState win) {
4868 synchronized (this) {
4869 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
4870 }
4871 }
4872
4873 void bindTargetWindowLockedLocked(WindowState win,
4874 int pendingWhat, QueuedEvent pendingMotion) {
4875 mLastWin = win;
4876 mLastBinder = win.mClient.asBinder();
4877 mFinished = false;
4878 if (pendingMotion != null) {
4879 final Session s = win.mSession;
4880 if (pendingWhat == RETURN_PENDING_POINTER) {
4881 releasePendingPointerLocked(s);
4882 s.mPendingPointerMove = pendingMotion;
4883 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07004884 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 "bindTargetToWindow " + s.mPendingPointerMove);
4886 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
4887 releasePendingTrackballLocked(s);
4888 s.mPendingTrackballMove = pendingMotion;
4889 s.mPendingTrackballWindow = win;
4890 }
4891 }
4892 }
Romain Guy06882f82009-06-10 13:36:04 -07004893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 void releasePendingPointerLocked(Session s) {
4895 if (DEBUG_INPUT) Log.v(TAG,
4896 "releasePendingPointer " + s.mPendingPointerMove);
4897 if (s.mPendingPointerMove != null) {
4898 mQueue.recycleEvent(s.mPendingPointerMove);
4899 s.mPendingPointerMove = null;
4900 }
4901 }
Romain Guy06882f82009-06-10 13:36:04 -07004902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903 void releasePendingTrackballLocked(Session s) {
4904 if (s.mPendingTrackballMove != null) {
4905 mQueue.recycleEvent(s.mPendingTrackballMove);
4906 s.mPendingTrackballMove = null;
4907 }
4908 }
Romain Guy06882f82009-06-10 13:36:04 -07004909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004910 MotionEvent finishedKey(Session session, IWindow client, boolean force,
4911 int returnWhat) {
4912 if (DEBUG_INPUT) Log.v(
4913 TAG, "finishedKey: client=" + client + ", force=" + force);
4914
4915 if (client == null) {
4916 return null;
4917 }
4918
4919 synchronized (this) {
4920 if (DEBUG_INPUT) Log.v(
4921 TAG, "finishedKey: client=" + client.asBinder()
4922 + ", force=" + force + ", last=" + mLastBinder
4923 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
4924
4925 QueuedEvent qev = null;
4926 WindowState win = null;
4927 if (returnWhat == RETURN_PENDING_POINTER) {
4928 qev = session.mPendingPointerMove;
4929 win = session.mPendingPointerWindow;
4930 session.mPendingPointerMove = null;
4931 session.mPendingPointerWindow = null;
4932 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
4933 qev = session.mPendingTrackballMove;
4934 win = session.mPendingTrackballWindow;
4935 session.mPendingTrackballMove = null;
4936 session.mPendingTrackballWindow = null;
4937 }
Romain Guy06882f82009-06-10 13:36:04 -07004938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 if (mLastBinder == client.asBinder()) {
4940 if (DEBUG_INPUT) Log.v(
4941 TAG, "finishedKey: last paused="
4942 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
4943 if (mLastWin != null && (!mLastWin.mToken.paused || force
4944 || !mEventDispatching)) {
4945 doFinishedKeyLocked(false);
4946 } else {
4947 // Make sure to wake up anyone currently waiting to
4948 // dispatch a key, so they can re-evaluate their
4949 // current situation.
4950 mFinished = true;
4951 notifyAll();
4952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953 }
Romain Guy06882f82009-06-10 13:36:04 -07004954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 if (qev != null) {
4956 MotionEvent res = (MotionEvent)qev.event;
4957 if (DEBUG_INPUT) Log.v(TAG,
4958 "Returning pending motion: " + res);
4959 mQueue.recycleEvent(qev);
4960 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
4961 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
4962 }
4963 return res;
4964 }
4965 return null;
4966 }
4967 }
4968
4969 void tickle() {
4970 synchronized (this) {
4971 notifyAll();
4972 }
4973 }
Romain Guy06882f82009-06-10 13:36:04 -07004974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004975 void handleNewWindowLocked(WindowState newWindow) {
4976 if (!newWindow.canReceiveKeys()) {
4977 return;
4978 }
4979 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004980 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004981 TAG, "New key dispatch window: win="
4982 + newWindow.mClient.asBinder()
4983 + ", last=" + mLastBinder
4984 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
4985 + "), finished=" + mFinished + ", paused="
4986 + newWindow.mToken.paused);
4987
4988 // Displaying a window implicitly causes dispatching to
4989 // be unpaused. (This is to protect against bugs if someone
4990 // pauses dispatching but forgets to resume.)
4991 newWindow.mToken.paused = false;
4992
4993 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994
4995 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
4996 if (DEBUG_INPUT) Log.v(TAG,
4997 "New SYSTEM_ERROR window; resetting state");
4998 mLastWin = null;
4999 mLastBinder = null;
5000 mMotionTarget = null;
5001 mFinished = true;
5002 } else if (mLastWin != null) {
5003 // If the new window is above the window we are
5004 // waiting on, then stop waiting and let key dispatching
5005 // start on the new guy.
5006 if (DEBUG_INPUT) Log.v(
5007 TAG, "Last win layer=" + mLastWin.mLayer
5008 + ", new win layer=" + newWindow.mLayer);
5009 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005010 // The new window is above the old; finish pending input to the last
5011 // window and start directing it to the new one.
5012 mLastWin.mToken.paused = false;
5013 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005015 // Either the new window is lower, so there is no need to wake key waiters,
5016 // or we just finished key input to the previous window, which implicitly
5017 // notified the key waiters. In both cases, we don't need to issue the
5018 // notification here.
5019 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 }
5021
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005022 // Now that we've put a new window state in place, make the event waiter
5023 // take notice and retarget its attentions.
5024 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005025 }
5026 }
5027
5028 void pauseDispatchingLocked(WindowToken token) {
5029 synchronized (this)
5030 {
5031 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5032 token.paused = true;
5033
5034 /*
5035 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5036 mPaused = true;
5037 } else {
5038 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005039 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005041 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005043 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 }
5045 }
5046 */
5047 }
5048 }
5049
5050 void resumeDispatchingLocked(WindowToken token) {
5051 synchronized (this) {
5052 if (token.paused) {
5053 if (DEBUG_INPUT) Log.v(
5054 TAG, "Resuming WindowToken " + token
5055 + ", last=" + mLastBinder
5056 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5057 + "), finished=" + mFinished + ", paused="
5058 + token.paused);
5059 token.paused = false;
5060 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5061 doFinishedKeyLocked(true);
5062 } else {
5063 notifyAll();
5064 }
5065 }
5066 }
5067 }
5068
5069 void setEventDispatchingLocked(boolean enabled) {
5070 synchronized (this) {
5071 mEventDispatching = enabled;
5072 notifyAll();
5073 }
5074 }
Romain Guy06882f82009-06-10 13:36:04 -07005075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 void appSwitchComing() {
5077 synchronized (this) {
5078 // Don't wait for more than .5 seconds for app to finish
5079 // processing the pending events.
5080 long now = SystemClock.uptimeMillis() + 500;
5081 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5082 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5083 mTimeToSwitch = now;
5084 }
5085 notifyAll();
5086 }
5087 }
Romain Guy06882f82009-06-10 13:36:04 -07005088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 private final void doFinishedKeyLocked(boolean doRecycle) {
5090 if (mLastWin != null) {
5091 releasePendingPointerLocked(mLastWin.mSession);
5092 releasePendingTrackballLocked(mLastWin.mSession);
5093 }
Romain Guy06882f82009-06-10 13:36:04 -07005094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 if (mLastWin == null || !mLastWin.mToken.paused
5096 || !mLastWin.isVisibleLw()) {
5097 // If the current window has been paused, we aren't -really-
5098 // finished... so let the waiters still wait.
5099 mLastWin = null;
5100 mLastBinder = null;
5101 }
5102 mFinished = true;
5103 notifyAll();
5104 }
5105 }
5106
5107 private class KeyQ extends KeyInputQueue
5108 implements KeyInputQueue.FilterCallback {
5109 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111 KeyQ() {
5112 super(mContext);
5113 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5114 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5115 "KEEP_SCREEN_ON_FLAG");
5116 mHoldingScreen.setReferenceCounted(false);
5117 }
5118
5119 @Override
5120 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5121 if (mPolicy.preprocessInputEventTq(event)) {
5122 return true;
5123 }
Romain Guy06882f82009-06-10 13:36:04 -07005124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 switch (event.type) {
5126 case RawInputEvent.EV_KEY: {
5127 // XXX begin hack
5128 if (DEBUG) {
5129 if (event.keycode == KeyEvent.KEYCODE_G) {
5130 if (event.value != 0) {
5131 // G down
5132 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5133 }
5134 return false;
5135 }
5136 if (event.keycode == KeyEvent.KEYCODE_D) {
5137 if (event.value != 0) {
5138 //dump();
5139 }
5140 return false;
5141 }
5142 }
5143 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 boolean screenIsOff = !mPowerManager.screenIsOn();
5146 boolean screenIsDim = !mPowerManager.screenIsBright();
5147 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5150 mPowerManager.goToSleep(event.when);
5151 }
5152
5153 if (screenIsOff) {
5154 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5155 }
5156 if (screenIsDim) {
5157 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5158 }
5159 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5160 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005161 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 }
Romain Guy06882f82009-06-10 13:36:04 -07005163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5165 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5166 filterQueue(this);
5167 mKeyWaiter.appSwitchComing();
5168 }
5169 return true;
5170 } else {
5171 return false;
5172 }
5173 }
Romain Guy06882f82009-06-10 13:36:04 -07005174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 case RawInputEvent.EV_REL: {
5176 boolean screenIsOff = !mPowerManager.screenIsOn();
5177 boolean screenIsDim = !mPowerManager.screenIsBright();
5178 if (screenIsOff) {
5179 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5180 device.classes, event)) {
5181 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5182 return false;
5183 }
5184 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5185 }
5186 if (screenIsDim) {
5187 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5188 }
5189 return true;
5190 }
Romain Guy06882f82009-06-10 13:36:04 -07005191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 case RawInputEvent.EV_ABS: {
5193 boolean screenIsOff = !mPowerManager.screenIsOn();
5194 boolean screenIsDim = !mPowerManager.screenIsBright();
5195 if (screenIsOff) {
5196 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5197 device.classes, event)) {
5198 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5199 return false;
5200 }
5201 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5202 }
5203 if (screenIsDim) {
5204 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5205 }
5206 return true;
5207 }
Romain Guy06882f82009-06-10 13:36:04 -07005208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 default:
5210 return true;
5211 }
5212 }
5213
5214 public int filterEvent(QueuedEvent ev) {
5215 switch (ev.classType) {
5216 case RawInputEvent.CLASS_KEYBOARD:
5217 KeyEvent ke = (KeyEvent)ev.event;
5218 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5219 Log.w(TAG, "Dropping movement key during app switch: "
5220 + ke.getKeyCode() + ", action=" + ke.getAction());
5221 return FILTER_REMOVE;
5222 }
5223 return FILTER_ABORT;
5224 default:
5225 return FILTER_KEEP;
5226 }
5227 }
Romain Guy06882f82009-06-10 13:36:04 -07005228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 /**
5230 * Must be called with the main window manager lock held.
5231 */
5232 void setHoldScreenLocked(boolean holding) {
5233 boolean state = mHoldingScreen.isHeld();
5234 if (holding != state) {
5235 if (holding) {
5236 mHoldingScreen.acquire();
5237 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005238 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 mHoldingScreen.release();
5240 }
5241 }
5242 }
Michael Chan53071d62009-05-13 17:29:48 -07005243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244
5245 public boolean detectSafeMode() {
5246 mSafeMode = mPolicy.detectSafeMode();
5247 return mSafeMode;
5248 }
Romain Guy06882f82009-06-10 13:36:04 -07005249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005250 public void systemReady() {
5251 mPolicy.systemReady();
5252 }
Romain Guy06882f82009-06-10 13:36:04 -07005253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005254 private final class InputDispatcherThread extends Thread {
5255 // Time to wait when there is nothing to do: 9999 seconds.
5256 static final int LONG_WAIT=9999*1000;
5257
5258 public InputDispatcherThread() {
5259 super("InputDispatcher");
5260 }
Romain Guy06882f82009-06-10 13:36:04 -07005261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 @Override
5263 public void run() {
5264 while (true) {
5265 try {
5266 process();
5267 } catch (Exception e) {
5268 Log.e(TAG, "Exception in input dispatcher", e);
5269 }
5270 }
5271 }
Romain Guy06882f82009-06-10 13:36:04 -07005272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005273 private void process() {
5274 android.os.Process.setThreadPriority(
5275 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 // The last key event we saw
5278 KeyEvent lastKey = null;
5279
5280 // Last keydown time for auto-repeating keys
5281 long lastKeyTime = SystemClock.uptimeMillis();
5282 long nextKeyTime = lastKeyTime+LONG_WAIT;
5283
Romain Guy06882f82009-06-10 13:36:04 -07005284 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 int keyRepeatCount = 0;
5286
5287 // Need to report that configuration has changed?
5288 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005290 while (true) {
5291 long curTime = SystemClock.uptimeMillis();
5292
5293 if (DEBUG_INPUT) Log.v(
5294 TAG, "Waiting for next key: now=" + curTime
5295 + ", repeat @ " + nextKeyTime);
5296
5297 // Retrieve next event, waiting only as long as the next
5298 // repeat timeout. If the configuration has changed, then
5299 // don't wait at all -- we'll report the change as soon as
5300 // we have processed all events.
5301 QueuedEvent ev = mQueue.getEvent(
5302 (int)((!configChanged && curTime < nextKeyTime)
5303 ? (nextKeyTime-curTime) : 0));
5304
5305 if (DEBUG_INPUT && ev != null) Log.v(
5306 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5307
Michael Chan53071d62009-05-13 17:29:48 -07005308 if (MEASURE_LATENCY) {
5309 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5310 }
5311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312 try {
5313 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005314 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005315 int eventType;
5316 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5317 eventType = eventType((MotionEvent)ev.event);
5318 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5319 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5320 eventType = LocalPowerManager.BUTTON_EVENT;
5321 } else {
5322 eventType = LocalPowerManager.OTHER_EVENT;
5323 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005324 try {
Michael Chan53071d62009-05-13 17:29:48 -07005325 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005326 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005327 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005328 mBatteryStats.noteInputEvent();
5329 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005330 } catch (RemoteException e) {
5331 // Ignore
5332 }
Michael Chane10de972009-05-18 11:24:50 -07005333
5334 if (eventType != TOUCH_EVENT
5335 && eventType != LONG_TOUCH_EVENT
5336 && eventType != CHEEK_EVENT) {
5337 mPowerManager.userActivity(curTime, false,
5338 eventType, false);
5339 } else if (mLastTouchEventType != eventType
5340 || (curTime - mLastUserActivityCallTime)
5341 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5342 mLastUserActivityCallTime = curTime;
5343 mLastTouchEventType = eventType;
5344 mPowerManager.userActivity(curTime, false,
5345 eventType, false);
5346 }
5347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005348 switch (ev.classType) {
5349 case RawInputEvent.CLASS_KEYBOARD:
5350 KeyEvent ke = (KeyEvent)ev.event;
5351 if (ke.isDown()) {
5352 lastKey = ke;
5353 keyRepeatCount = 0;
5354 lastKeyTime = curTime;
5355 nextKeyTime = lastKeyTime
5356 + KEY_REPEAT_FIRST_DELAY;
5357 if (DEBUG_INPUT) Log.v(
5358 TAG, "Received key down: first repeat @ "
5359 + nextKeyTime);
5360 } else {
5361 lastKey = null;
5362 // Arbitrary long timeout.
5363 lastKeyTime = curTime;
5364 nextKeyTime = curTime + LONG_WAIT;
5365 if (DEBUG_INPUT) Log.v(
5366 TAG, "Received key up: ignore repeat @ "
5367 + nextKeyTime);
5368 }
5369 dispatchKey((KeyEvent)ev.event, 0, 0);
5370 mQueue.recycleEvent(ev);
5371 break;
5372 case RawInputEvent.CLASS_TOUCHSCREEN:
5373 //Log.i(TAG, "Read next event " + ev);
5374 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5375 break;
5376 case RawInputEvent.CLASS_TRACKBALL:
5377 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5378 break;
5379 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5380 configChanged = true;
5381 break;
5382 default:
5383 mQueue.recycleEvent(ev);
5384 break;
5385 }
Romain Guy06882f82009-06-10 13:36:04 -07005386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005387 } else if (configChanged) {
5388 configChanged = false;
5389 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 } else if (lastKey != null) {
5392 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 // Timeout occurred while key was down. If it is at or
5395 // past the key repeat time, dispatch the repeat.
5396 if (DEBUG_INPUT) Log.v(
5397 TAG, "Key timeout: repeat=" + nextKeyTime
5398 + ", now=" + curTime);
5399 if (curTime < nextKeyTime) {
5400 continue;
5401 }
Romain Guy06882f82009-06-10 13:36:04 -07005402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 lastKeyTime = nextKeyTime;
5404 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5405 keyRepeatCount++;
5406 if (DEBUG_INPUT) Log.v(
5407 TAG, "Key repeat: count=" + keyRepeatCount
5408 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005409 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005411 } else {
5412 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 lastKeyTime = curTime;
5415 nextKeyTime = curTime + LONG_WAIT;
5416 }
Romain Guy06882f82009-06-10 13:36:04 -07005417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005418 } catch (Exception e) {
5419 Log.e(TAG,
5420 "Input thread received uncaught exception: " + e, e);
5421 }
5422 }
5423 }
5424 }
5425
5426 // -------------------------------------------------------------
5427 // Client Session State
5428 // -------------------------------------------------------------
5429
5430 private final class Session extends IWindowSession.Stub
5431 implements IBinder.DeathRecipient {
5432 final IInputMethodClient mClient;
5433 final IInputContext mInputContext;
5434 final int mUid;
5435 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005436 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 SurfaceSession mSurfaceSession;
5438 int mNumWindow = 0;
5439 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005441 /**
5442 * Current pointer move event being dispatched to client window... must
5443 * hold key lock to access.
5444 */
5445 QueuedEvent mPendingPointerMove;
5446 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 /**
5449 * Current trackball move event being dispatched to client window... must
5450 * hold key lock to access.
5451 */
5452 QueuedEvent mPendingTrackballMove;
5453 WindowState mPendingTrackballWindow;
5454
5455 public Session(IInputMethodClient client, IInputContext inputContext) {
5456 mClient = client;
5457 mInputContext = inputContext;
5458 mUid = Binder.getCallingUid();
5459 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005460 StringBuilder sb = new StringBuilder();
5461 sb.append("Session{");
5462 sb.append(Integer.toHexString(System.identityHashCode(this)));
5463 sb.append(" uid ");
5464 sb.append(mUid);
5465 sb.append("}");
5466 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 synchronized (mWindowMap) {
5469 if (mInputMethodManager == null && mHaveInputMethods) {
5470 IBinder b = ServiceManager.getService(
5471 Context.INPUT_METHOD_SERVICE);
5472 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5473 }
5474 }
5475 long ident = Binder.clearCallingIdentity();
5476 try {
5477 // Note: it is safe to call in to the input method manager
5478 // here because we are not holding our lock.
5479 if (mInputMethodManager != null) {
5480 mInputMethodManager.addClient(client, inputContext,
5481 mUid, mPid);
5482 } else {
5483 client.setUsingInputMethod(false);
5484 }
5485 client.asBinder().linkToDeath(this, 0);
5486 } catch (RemoteException e) {
5487 // The caller has died, so we can just forget about this.
5488 try {
5489 if (mInputMethodManager != null) {
5490 mInputMethodManager.removeClient(client);
5491 }
5492 } catch (RemoteException ee) {
5493 }
5494 } finally {
5495 Binder.restoreCallingIdentity(ident);
5496 }
5497 }
Romain Guy06882f82009-06-10 13:36:04 -07005498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 @Override
5500 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5501 throws RemoteException {
5502 try {
5503 return super.onTransact(code, data, reply, flags);
5504 } catch (RuntimeException e) {
5505 // Log all 'real' exceptions thrown to the caller
5506 if (!(e instanceof SecurityException)) {
5507 Log.e(TAG, "Window Session Crash", e);
5508 }
5509 throw e;
5510 }
5511 }
5512
5513 public void binderDied() {
5514 // Note: it is safe to call in to the input method manager
5515 // here because we are not holding our lock.
5516 try {
5517 if (mInputMethodManager != null) {
5518 mInputMethodManager.removeClient(mClient);
5519 }
5520 } catch (RemoteException e) {
5521 }
5522 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005523 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 mClientDead = true;
5525 killSessionLocked();
5526 }
5527 }
5528
5529 public int add(IWindow window, WindowManager.LayoutParams attrs,
5530 int viewVisibility, Rect outContentInsets) {
5531 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5532 }
Romain Guy06882f82009-06-10 13:36:04 -07005533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 public void remove(IWindow window) {
5535 removeWindow(this, window);
5536 }
Romain Guy06882f82009-06-10 13:36:04 -07005537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5539 int requestedWidth, int requestedHeight, int viewFlags,
5540 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5541 Rect outVisibleInsets, Surface outSurface) {
5542 return relayoutWindow(this, window, attrs,
5543 requestedWidth, requestedHeight, viewFlags, insetsPending,
5544 outFrame, outContentInsets, outVisibleInsets, outSurface);
5545 }
Romain Guy06882f82009-06-10 13:36:04 -07005546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 public void setTransparentRegion(IWindow window, Region region) {
5548 setTransparentRegionWindow(this, window, region);
5549 }
Romain Guy06882f82009-06-10 13:36:04 -07005550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 public void setInsets(IWindow window, int touchableInsets,
5552 Rect contentInsets, Rect visibleInsets) {
5553 setInsetsWindow(this, window, touchableInsets, contentInsets,
5554 visibleInsets);
5555 }
Romain Guy06882f82009-06-10 13:36:04 -07005556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005557 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5558 getWindowDisplayFrame(this, window, outDisplayFrame);
5559 }
Romain Guy06882f82009-06-10 13:36:04 -07005560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005561 public void finishDrawing(IWindow window) {
5562 if (localLOGV) Log.v(
5563 TAG, "IWindow finishDrawing called for " + window);
5564 finishDrawingWindow(this, window);
5565 }
5566
5567 public void finishKey(IWindow window) {
5568 if (localLOGV) Log.v(
5569 TAG, "IWindow finishKey called for " + window);
5570 mKeyWaiter.finishedKey(this, window, false,
5571 KeyWaiter.RETURN_NOTHING);
5572 }
5573
5574 public MotionEvent getPendingPointerMove(IWindow window) {
5575 if (localLOGV) Log.v(
5576 TAG, "IWindow getPendingMotionEvent called for " + window);
5577 return mKeyWaiter.finishedKey(this, window, false,
5578 KeyWaiter.RETURN_PENDING_POINTER);
5579 }
Romain Guy06882f82009-06-10 13:36:04 -07005580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 public MotionEvent getPendingTrackballMove(IWindow window) {
5582 if (localLOGV) Log.v(
5583 TAG, "IWindow getPendingMotionEvent called for " + window);
5584 return mKeyWaiter.finishedKey(this, window, false,
5585 KeyWaiter.RETURN_PENDING_TRACKBALL);
5586 }
5587
5588 public void setInTouchMode(boolean mode) {
5589 synchronized(mWindowMap) {
5590 mInTouchMode = mode;
5591 }
5592 }
5593
5594 public boolean getInTouchMode() {
5595 synchronized(mWindowMap) {
5596 return mInTouchMode;
5597 }
5598 }
5599
5600 public boolean performHapticFeedback(IWindow window, int effectId,
5601 boolean always) {
5602 synchronized(mWindowMap) {
5603 long ident = Binder.clearCallingIdentity();
5604 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005605 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 windowForClientLocked(this, window), effectId, always);
5607 } finally {
5608 Binder.restoreCallingIdentity(ident);
5609 }
5610 }
5611 }
Romain Guy06882f82009-06-10 13:36:04 -07005612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005613 void windowAddedLocked() {
5614 if (mSurfaceSession == null) {
5615 if (localLOGV) Log.v(
5616 TAG, "First window added to " + this + ", creating SurfaceSession");
5617 mSurfaceSession = new SurfaceSession();
5618 mSessions.add(this);
5619 }
5620 mNumWindow++;
5621 }
5622
5623 void windowRemovedLocked() {
5624 mNumWindow--;
5625 killSessionLocked();
5626 }
Romain Guy06882f82009-06-10 13:36:04 -07005627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005628 void killSessionLocked() {
5629 if (mNumWindow <= 0 && mClientDead) {
5630 mSessions.remove(this);
5631 if (mSurfaceSession != null) {
5632 if (localLOGV) Log.v(
5633 TAG, "Last window removed from " + this
5634 + ", destroying " + mSurfaceSession);
5635 try {
5636 mSurfaceSession.kill();
5637 } catch (Exception e) {
5638 Log.w(TAG, "Exception thrown when killing surface session "
5639 + mSurfaceSession + " in session " + this
5640 + ": " + e.toString());
5641 }
5642 mSurfaceSession = null;
5643 }
5644 }
5645 }
Romain Guy06882f82009-06-10 13:36:04 -07005646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005648 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5649 pw.print(" mClientDead="); pw.print(mClientDead);
5650 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5651 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5652 pw.print(prefix);
5653 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5654 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5655 }
5656 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5657 pw.print(prefix);
5658 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5659 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005661 }
5662
5663 @Override
5664 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005665 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 }
5667 }
5668
5669 // -------------------------------------------------------------
5670 // Client Window State
5671 // -------------------------------------------------------------
5672
5673 private final class WindowState implements WindowManagerPolicy.WindowState {
5674 final Session mSession;
5675 final IWindow mClient;
5676 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005677 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005678 AppWindowToken mAppToken;
5679 AppWindowToken mTargetAppToken;
5680 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5681 final DeathRecipient mDeathRecipient;
5682 final WindowState mAttachedWindow;
5683 final ArrayList mChildWindows = new ArrayList();
5684 final int mBaseLayer;
5685 final int mSubLayer;
5686 final boolean mLayoutAttached;
5687 final boolean mIsImWindow;
5688 int mViewVisibility;
5689 boolean mPolicyVisibility = true;
5690 boolean mPolicyVisibilityAfterAnim = true;
5691 boolean mAppFreezing;
5692 Surface mSurface;
5693 boolean mAttachedHidden; // is our parent window hidden?
5694 boolean mLastHidden; // was this window last hidden?
5695 int mRequestedWidth;
5696 int mRequestedHeight;
5697 int mLastRequestedWidth;
5698 int mLastRequestedHeight;
5699 int mReqXPos;
5700 int mReqYPos;
5701 int mLayer;
5702 int mAnimLayer;
5703 int mLastLayer;
5704 boolean mHaveFrame;
5705
5706 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 // Actual frame shown on-screen (may be modified by animation)
5709 final Rect mShownFrame = new Rect();
5710 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 /**
5713 * Insets that determine the actually visible area
5714 */
5715 final Rect mVisibleInsets = new Rect();
5716 final Rect mLastVisibleInsets = new Rect();
5717 boolean mVisibleInsetsChanged;
5718
5719 /**
5720 * Insets that are covered by system windows
5721 */
5722 final Rect mContentInsets = new Rect();
5723 final Rect mLastContentInsets = new Rect();
5724 boolean mContentInsetsChanged;
5725
5726 /**
5727 * Set to true if we are waiting for this window to receive its
5728 * given internal insets before laying out other windows based on it.
5729 */
5730 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07005731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 /**
5733 * These are the content insets that were given during layout for
5734 * this window, to be applied to windows behind it.
5735 */
5736 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 /**
5739 * These are the visible insets that were given during layout for
5740 * this window, to be applied to windows behind it.
5741 */
5742 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 /**
5745 * Flag indicating whether the touchable region should be adjusted by
5746 * the visible insets; if false the area outside the visible insets is
5747 * NOT touchable, so we must use those to adjust the frame during hit
5748 * tests.
5749 */
5750 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07005751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005752 // Current transformation being applied.
5753 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5754 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5755 float mHScale=1, mVScale=1;
5756 float mLastHScale=1, mLastVScale=1;
5757 final Matrix mTmpMatrix = new Matrix();
5758
5759 // "Real" frame that the application sees.
5760 final Rect mFrame = new Rect();
5761 final Rect mLastFrame = new Rect();
5762
5763 final Rect mContainingFrame = new Rect();
5764 final Rect mDisplayFrame = new Rect();
5765 final Rect mContentFrame = new Rect();
5766 final Rect mVisibleFrame = new Rect();
5767
5768 float mShownAlpha = 1;
5769 float mAlpha = 1;
5770 float mLastAlpha = 1;
5771
5772 // Set to true if, when the window gets displayed, it should perform
5773 // an enter animation.
5774 boolean mEnterAnimationPending;
5775
5776 // Currently running animation.
5777 boolean mAnimating;
5778 boolean mLocalAnimating;
5779 Animation mAnimation;
5780 boolean mAnimationIsEntrance;
5781 boolean mHasTransformation;
5782 boolean mHasLocalTransformation;
5783 final Transformation mTransformation = new Transformation();
5784
5785 // This is set after IWindowSession.relayout() has been called at
5786 // least once for the window. It allows us to detect the situation
5787 // where we don't yet have a surface, but should have one soon, so
5788 // we can give the window focus before waiting for the relayout.
5789 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07005790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005791 // This is set after the Surface has been created but before the
5792 // window has been drawn. During this time the surface is hidden.
5793 boolean mDrawPending;
5794
5795 // This is set after the window has finished drawing for the first
5796 // time but before its surface is shown. The surface will be
5797 // displayed when the next layout is run.
5798 boolean mCommitDrawPending;
5799
5800 // This is set during the time after the window's drawing has been
5801 // committed, and before its surface is actually shown. It is used
5802 // to delay showing the surface until all windows in a token are ready
5803 // to be shown.
5804 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07005805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 // Set when the window has been shown in the screen the first time.
5807 boolean mHasDrawn;
5808
5809 // Currently running an exit animation?
5810 boolean mExiting;
5811
5812 // Currently on the mDestroySurface list?
5813 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07005814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005815 // Completely remove from window manager after exit animation?
5816 boolean mRemoveOnExit;
5817
5818 // Set when the orientation is changing and this window has not yet
5819 // been updated for the new orientation.
5820 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07005821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 // Is this window now (or just being) removed?
5823 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07005824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005825 WindowState(Session s, IWindow c, WindowToken token,
5826 WindowState attachedWindow, WindowManager.LayoutParams a,
5827 int viewVisibility) {
5828 mSession = s;
5829 mClient = c;
5830 mToken = token;
5831 mAttrs.copyFrom(a);
5832 mViewVisibility = viewVisibility;
5833 DeathRecipient deathRecipient = new DeathRecipient();
5834 mAlpha = a.alpha;
5835 if (localLOGV) Log.v(
5836 TAG, "Window " + this + " client=" + c.asBinder()
5837 + " token=" + token + " (" + mAttrs.token + ")");
5838 try {
5839 c.asBinder().linkToDeath(deathRecipient, 0);
5840 } catch (RemoteException e) {
5841 mDeathRecipient = null;
5842 mAttachedWindow = null;
5843 mLayoutAttached = false;
5844 mIsImWindow = false;
5845 mBaseLayer = 0;
5846 mSubLayer = 0;
5847 return;
5848 }
5849 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07005850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005851 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
5852 mAttrs.type <= LAST_SUB_WINDOW)) {
5853 // The multiplier here is to reserve space for multiple
5854 // windows in the same type layer.
5855 mBaseLayer = mPolicy.windowTypeToLayerLw(
5856 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
5857 + TYPE_LAYER_OFFSET;
5858 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
5859 mAttachedWindow = attachedWindow;
5860 mAttachedWindow.mChildWindows.add(this);
5861 mLayoutAttached = mAttrs.type !=
5862 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
5863 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
5864 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5865 } else {
5866 // The multiplier here is to reserve space for multiple
5867 // windows in the same type layer.
5868 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
5869 * TYPE_LAYER_MULTIPLIER
5870 + TYPE_LAYER_OFFSET;
5871 mSubLayer = 0;
5872 mAttachedWindow = null;
5873 mLayoutAttached = false;
5874 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
5875 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5876 }
5877
5878 WindowState appWin = this;
5879 while (appWin.mAttachedWindow != null) {
5880 appWin = mAttachedWindow;
5881 }
5882 WindowToken appToken = appWin.mToken;
5883 while (appToken.appWindowToken == null) {
5884 WindowToken parent = mTokenMap.get(appToken.token);
5885 if (parent == null || appToken == parent) {
5886 break;
5887 }
5888 appToken = parent;
5889 }
The Android Open Source Project10592532009-03-18 17:39:46 -07005890 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 mAppToken = appToken.appWindowToken;
5892
5893 mSurface = null;
5894 mRequestedWidth = 0;
5895 mRequestedHeight = 0;
5896 mLastRequestedWidth = 0;
5897 mLastRequestedHeight = 0;
5898 mReqXPos = 0;
5899 mReqYPos = 0;
5900 mLayer = 0;
5901 mAnimLayer = 0;
5902 mLastLayer = 0;
5903 }
5904
5905 void attach() {
5906 if (localLOGV) Log.v(
5907 TAG, "Attaching " + this + " token=" + mToken
5908 + ", list=" + mToken.windows);
5909 mSession.windowAddedLocked();
5910 }
5911
5912 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
5913 mHaveFrame = true;
5914
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005915 final Rect container = mContainingFrame;
5916 container.set(pf);
5917
5918 final Rect display = mDisplayFrame;
5919 display.set(df);
5920
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07005921 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005922 container.intersect(mCompatibleScreenFrame);
5923 display.intersect(mCompatibleScreenFrame);
5924 }
5925
5926 final int pw = container.right - container.left;
5927 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005928
5929 int w,h;
5930 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
5931 w = mAttrs.width < 0 ? pw : mAttrs.width;
5932 h = mAttrs.height< 0 ? ph : mAttrs.height;
5933 } else {
5934 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
5935 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
5936 }
Romain Guy06882f82009-06-10 13:36:04 -07005937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005938 final Rect content = mContentFrame;
5939 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07005940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941 final Rect visible = mVisibleFrame;
5942 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07005943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07005945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946 //System.out.println("In: w=" + w + " h=" + h + " container=" +
5947 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
5948
5949 Gravity.apply(mAttrs.gravity, w, h, container,
5950 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
5951 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
5952
5953 //System.out.println("Out: " + mFrame);
5954
5955 // Now make sure the window fits in the overall display.
5956 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 // Make sure the content and visible frames are inside of the
5959 // final window frame.
5960 if (content.left < frame.left) content.left = frame.left;
5961 if (content.top < frame.top) content.top = frame.top;
5962 if (content.right > frame.right) content.right = frame.right;
5963 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
5964 if (visible.left < frame.left) visible.left = frame.left;
5965 if (visible.top < frame.top) visible.top = frame.top;
5966 if (visible.right > frame.right) visible.right = frame.right;
5967 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005969 final Rect contentInsets = mContentInsets;
5970 contentInsets.left = content.left-frame.left;
5971 contentInsets.top = content.top-frame.top;
5972 contentInsets.right = frame.right-content.right;
5973 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 final Rect visibleInsets = mVisibleInsets;
5976 visibleInsets.left = visible.left-frame.left;
5977 visibleInsets.top = visible.top-frame.top;
5978 visibleInsets.right = frame.right-visible.right;
5979 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005981 if (localLOGV) {
5982 //if ("com.google.android.youtube".equals(mAttrs.packageName)
5983 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
5984 Log.v(TAG, "Resolving (mRequestedWidth="
5985 + mRequestedWidth + ", mRequestedheight="
5986 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
5987 + "): frame=" + mFrame.toShortString()
5988 + " ci=" + contentInsets.toShortString()
5989 + " vi=" + visibleInsets.toShortString());
5990 //}
5991 }
5992 }
Romain Guy06882f82009-06-10 13:36:04 -07005993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 public Rect getFrameLw() {
5995 return mFrame;
5996 }
5997
5998 public Rect getShownFrameLw() {
5999 return mShownFrame;
6000 }
6001
6002 public Rect getDisplayFrameLw() {
6003 return mDisplayFrame;
6004 }
6005
6006 public Rect getContentFrameLw() {
6007 return mContentFrame;
6008 }
6009
6010 public Rect getVisibleFrameLw() {
6011 return mVisibleFrame;
6012 }
6013
6014 public boolean getGivenInsetsPendingLw() {
6015 return mGivenInsetsPending;
6016 }
6017
6018 public Rect getGivenContentInsetsLw() {
6019 return mGivenContentInsets;
6020 }
Romain Guy06882f82009-06-10 13:36:04 -07006021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 public Rect getGivenVisibleInsetsLw() {
6023 return mGivenVisibleInsets;
6024 }
Romain Guy06882f82009-06-10 13:36:04 -07006025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006026 public WindowManager.LayoutParams getAttrs() {
6027 return mAttrs;
6028 }
6029
6030 public int getSurfaceLayer() {
6031 return mLayer;
6032 }
Romain Guy06882f82009-06-10 13:36:04 -07006033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034 public IApplicationToken getAppToken() {
6035 return mAppToken != null ? mAppToken.appToken : null;
6036 }
6037
6038 public boolean hasAppShownWindows() {
6039 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6040 }
6041
6042 public boolean hasAppStartingIcon() {
6043 return mAppToken != null ? (mAppToken.startingData != null) : false;
6044 }
6045
6046 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6047 return mAppToken != null ? mAppToken.startingWindow : null;
6048 }
6049
6050 public void setAnimation(Animation anim) {
6051 if (localLOGV) Log.v(
6052 TAG, "Setting animation in " + this + ": " + anim);
6053 mAnimating = false;
6054 mLocalAnimating = false;
6055 mAnimation = anim;
6056 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6057 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6058 }
6059
6060 public void clearAnimation() {
6061 if (mAnimation != null) {
6062 mAnimating = true;
6063 mLocalAnimating = false;
6064 mAnimation = null;
6065 }
6066 }
Romain Guy06882f82009-06-10 13:36:04 -07006067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068 Surface createSurfaceLocked() {
6069 if (mSurface == null) {
6070 mDrawPending = true;
6071 mCommitDrawPending = false;
6072 mReadyToShow = false;
6073 if (mAppToken != null) {
6074 mAppToken.allDrawn = false;
6075 }
6076
6077 int flags = 0;
6078 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
6079 flags |= Surface.HARDWARE;
6080 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6081 flags |= Surface.GPU;
6082 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6083 flags |= Surface.PUSH_BUFFERS;
6084 }
6085
6086 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6087 flags |= Surface.SECURE;
6088 }
6089 if (DEBUG_VISIBILITY) Log.v(
6090 TAG, "Creating surface in session "
6091 + mSession.mSurfaceSession + " window " + this
6092 + " w=" + mFrame.width()
6093 + " h=" + mFrame.height() + " format="
6094 + mAttrs.format + " flags=" + flags);
6095
6096 int w = mFrame.width();
6097 int h = mFrame.height();
6098 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6099 // for a scaled surface, we always want the requested
6100 // size.
6101 w = mRequestedWidth;
6102 h = mRequestedHeight;
6103 }
6104
6105 try {
6106 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006107 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 0, w, h, mAttrs.format, flags);
6109 } catch (Surface.OutOfResourcesException e) {
6110 Log.w(TAG, "OutOfResourcesException creating surface");
6111 reclaimSomeSurfaceMemoryLocked(this, "create");
6112 return null;
6113 } catch (Exception e) {
6114 Log.e(TAG, "Exception creating surface", e);
6115 return null;
6116 }
Romain Guy06882f82009-06-10 13:36:04 -07006117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 if (localLOGV) Log.v(
6119 TAG, "Got surface: " + mSurface
6120 + ", set left=" + mFrame.left + " top=" + mFrame.top
6121 + ", animLayer=" + mAnimLayer);
6122 if (SHOW_TRANSACTIONS) {
6123 Log.i(TAG, ">>> OPEN TRANSACTION");
6124 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6125 + mAttrs.getTitle() + ") pos=(" +
6126 mFrame.left + "," + mFrame.top + ") (" +
6127 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6128 mAnimLayer + " HIDE");
6129 }
6130 Surface.openTransaction();
6131 try {
6132 try {
6133 mSurface.setPosition(mFrame.left, mFrame.top);
6134 mSurface.setLayer(mAnimLayer);
6135 mSurface.hide();
6136 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6137 mSurface.setFlags(Surface.SURFACE_DITHER,
6138 Surface.SURFACE_DITHER);
6139 }
6140 } catch (RuntimeException e) {
6141 Log.w(TAG, "Error creating surface in " + w, e);
6142 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6143 }
6144 mLastHidden = true;
6145 } finally {
6146 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6147 Surface.closeTransaction();
6148 }
6149 if (localLOGV) Log.v(
6150 TAG, "Created surface " + this);
6151 }
6152 return mSurface;
6153 }
Romain Guy06882f82009-06-10 13:36:04 -07006154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 void destroySurfaceLocked() {
6156 // Window is no longer on-screen, so can no longer receive
6157 // key events... if we were waiting for it to finish
6158 // handling a key event, the wait is over!
6159 mKeyWaiter.finishedKey(mSession, mClient, true,
6160 KeyWaiter.RETURN_NOTHING);
6161 mKeyWaiter.releasePendingPointerLocked(mSession);
6162 mKeyWaiter.releasePendingTrackballLocked(mSession);
6163
6164 if (mAppToken != null && this == mAppToken.startingWindow) {
6165 mAppToken.startingDisplayed = false;
6166 }
Romain Guy06882f82009-06-10 13:36:04 -07006167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006168 if (localLOGV) Log.v(
6169 TAG, "Window " + this
6170 + " destroying surface " + mSurface + ", session " + mSession);
6171 if (mSurface != null) {
6172 try {
6173 if (SHOW_TRANSACTIONS) {
6174 RuntimeException ex = new RuntimeException();
6175 ex.fillInStackTrace();
6176 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6177 + mAttrs.getTitle() + ")", ex);
6178 }
6179 mSurface.clear();
6180 } catch (RuntimeException e) {
6181 Log.w(TAG, "Exception thrown when destroying Window " + this
6182 + " surface " + mSurface + " session " + mSession
6183 + ": " + e.toString());
6184 }
6185 mSurface = null;
6186 mDrawPending = false;
6187 mCommitDrawPending = false;
6188 mReadyToShow = false;
6189
6190 int i = mChildWindows.size();
6191 while (i > 0) {
6192 i--;
6193 WindowState c = (WindowState)mChildWindows.get(i);
6194 c.mAttachedHidden = true;
6195 }
6196 }
6197 }
6198
6199 boolean finishDrawingLocked() {
6200 if (mDrawPending) {
6201 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6202 TAG, "finishDrawingLocked: " + mSurface);
6203 mCommitDrawPending = true;
6204 mDrawPending = false;
6205 return true;
6206 }
6207 return false;
6208 }
6209
6210 // This must be called while inside a transaction.
6211 void commitFinishDrawingLocked(long currentTime) {
6212 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6213 if (!mCommitDrawPending) {
6214 return;
6215 }
6216 mCommitDrawPending = false;
6217 mReadyToShow = true;
6218 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6219 final AppWindowToken atoken = mAppToken;
6220 if (atoken == null || atoken.allDrawn || starting) {
6221 performShowLocked();
6222 }
6223 }
6224
6225 // This must be called while inside a transaction.
6226 boolean performShowLocked() {
6227 if (DEBUG_VISIBILITY) {
6228 RuntimeException e = new RuntimeException();
6229 e.fillInStackTrace();
6230 Log.v(TAG, "performShow on " + this
6231 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6232 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6233 }
6234 if (mReadyToShow && isReadyForDisplay()) {
6235 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6236 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6237 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6238 + " during animation: policyVis=" + mPolicyVisibility
6239 + " attHidden=" + mAttachedHidden
6240 + " tok.hiddenRequested="
6241 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6242 + " tok.idden="
6243 + (mAppToken != null ? mAppToken.hidden : false)
6244 + " animating=" + mAnimating
6245 + " tok animating="
6246 + (mAppToken != null ? mAppToken.animating : false));
6247 if (!showSurfaceRobustlyLocked(this)) {
6248 return false;
6249 }
6250 mLastAlpha = -1;
6251 mHasDrawn = true;
6252 mLastHidden = false;
6253 mReadyToShow = false;
6254 enableScreenIfNeededLocked();
6255
6256 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 int i = mChildWindows.size();
6259 while (i > 0) {
6260 i--;
6261 WindowState c = (WindowState)mChildWindows.get(i);
6262 if (c.mSurface != null && c.mAttachedHidden) {
6263 c.mAttachedHidden = false;
6264 c.performShowLocked();
6265 }
6266 }
Romain Guy06882f82009-06-10 13:36:04 -07006267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 if (mAttrs.type != TYPE_APPLICATION_STARTING
6269 && mAppToken != null) {
6270 mAppToken.firstWindowDrawn = true;
6271 if (mAnimation == null && mAppToken.startingData != null) {
6272 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6273 + mToken
6274 + ": first real window is shown, no animation");
6275 mFinishedStarting.add(mAppToken);
6276 mH.sendEmptyMessage(H.FINISHED_STARTING);
6277 }
6278 mAppToken.updateReportedVisibilityLocked();
6279 }
6280 }
6281 return true;
6282 }
Romain Guy06882f82009-06-10 13:36:04 -07006283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006284 // This must be called while inside a transaction. Returns true if
6285 // there is more animation to run.
6286 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6287 if (!mDisplayFrozen) {
6288 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6291 mHasTransformation = true;
6292 mHasLocalTransformation = true;
6293 if (!mLocalAnimating) {
6294 if (DEBUG_ANIM) Log.v(
6295 TAG, "Starting animation in " + this +
6296 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6297 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6298 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6299 mAnimation.setStartTime(currentTime);
6300 mLocalAnimating = true;
6301 mAnimating = true;
6302 }
6303 mTransformation.clear();
6304 final boolean more = mAnimation.getTransformation(
6305 currentTime, mTransformation);
6306 if (DEBUG_ANIM) Log.v(
6307 TAG, "Stepped animation in " + this +
6308 ": more=" + more + ", xform=" + mTransformation);
6309 if (more) {
6310 // we're not done!
6311 return true;
6312 }
6313 if (DEBUG_ANIM) Log.v(
6314 TAG, "Finished animation in " + this +
6315 " @ " + currentTime);
6316 mAnimation = null;
6317 //WindowManagerService.this.dump();
6318 }
6319 mHasLocalTransformation = false;
6320 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6321 && mAppToken.hasTransformation) {
6322 // When our app token is animating, we kind-of pretend like
6323 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6324 // part of this check means that we will only do this if
6325 // our window is not currently exiting, or it is not
6326 // locally animating itself. The idea being that one that
6327 // is exiting and doing a local animation should be removed
6328 // once that animation is done.
6329 mAnimating = true;
6330 mHasTransformation = true;
6331 mTransformation.clear();
6332 return false;
6333 } else if (mHasTransformation) {
6334 // Little trick to get through the path below to act like
6335 // we have finished an animation.
6336 mAnimating = true;
6337 } else if (isAnimating()) {
6338 mAnimating = true;
6339 }
6340 } else if (mAnimation != null) {
6341 // If the display is frozen, and there is a pending animation,
6342 // clear it and make sure we run the cleanup code.
6343 mAnimating = true;
6344 mLocalAnimating = true;
6345 mAnimation = null;
6346 }
Romain Guy06882f82009-06-10 13:36:04 -07006347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006348 if (!mAnimating && !mLocalAnimating) {
6349 return false;
6350 }
6351
6352 if (DEBUG_ANIM) Log.v(
6353 TAG, "Animation done in " + this + ": exiting=" + mExiting
6354 + ", reportedVisible="
6355 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 mAnimating = false;
6358 mLocalAnimating = false;
6359 mAnimation = null;
6360 mAnimLayer = mLayer;
6361 if (mIsImWindow) {
6362 mAnimLayer += mInputMethodAnimLayerAdjustment;
6363 }
6364 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6365 + " anim layer: " + mAnimLayer);
6366 mHasTransformation = false;
6367 mHasLocalTransformation = false;
6368 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6369 mTransformation.clear();
6370 if (mHasDrawn
6371 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6372 && mAppToken != null
6373 && mAppToken.firstWindowDrawn
6374 && mAppToken.startingData != null) {
6375 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6376 + mToken + ": first real window done animating");
6377 mFinishedStarting.add(mAppToken);
6378 mH.sendEmptyMessage(H.FINISHED_STARTING);
6379 }
Romain Guy06882f82009-06-10 13:36:04 -07006380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 finishExit();
6382
6383 if (mAppToken != null) {
6384 mAppToken.updateReportedVisibilityLocked();
6385 }
6386
6387 return false;
6388 }
6389
6390 void finishExit() {
6391 if (DEBUG_ANIM) Log.v(
6392 TAG, "finishExit in " + this
6393 + ": exiting=" + mExiting
6394 + " remove=" + mRemoveOnExit
6395 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006397 final int N = mChildWindows.size();
6398 for (int i=0; i<N; i++) {
6399 ((WindowState)mChildWindows.get(i)).finishExit();
6400 }
Romain Guy06882f82009-06-10 13:36:04 -07006401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 if (!mExiting) {
6403 return;
6404 }
Romain Guy06882f82009-06-10 13:36:04 -07006405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 if (isWindowAnimating()) {
6407 return;
6408 }
6409
6410 if (localLOGV) Log.v(
6411 TAG, "Exit animation finished in " + this
6412 + ": remove=" + mRemoveOnExit);
6413 if (mSurface != null) {
6414 mDestroySurface.add(this);
6415 mDestroying = true;
6416 if (SHOW_TRANSACTIONS) Log.i(
6417 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6418 try {
6419 mSurface.hide();
6420 } catch (RuntimeException e) {
6421 Log.w(TAG, "Error hiding surface in " + this, e);
6422 }
6423 mLastHidden = true;
6424 mKeyWaiter.releasePendingPointerLocked(mSession);
6425 }
6426 mExiting = false;
6427 if (mRemoveOnExit) {
6428 mPendingRemove.add(this);
6429 mRemoveOnExit = false;
6430 }
6431 }
Romain Guy06882f82009-06-10 13:36:04 -07006432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6434 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6435 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6436 if (dtdx < -.000001f || dtdx > .000001f) return false;
6437 if (dsdy < -.000001f || dsdy > .000001f) return false;
6438 return true;
6439 }
Romain Guy06882f82009-06-10 13:36:04 -07006440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006441 void computeShownFrameLocked() {
6442 final boolean selfTransformation = mHasLocalTransformation;
6443 Transformation attachedTransformation =
6444 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6445 ? mAttachedWindow.mTransformation : null;
6446 Transformation appTransformation =
6447 (mAppToken != null && mAppToken.hasTransformation)
6448 ? mAppToken.transformation : null;
6449 if (selfTransformation || attachedTransformation != null
6450 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006451 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006452 final Rect frame = mFrame;
6453 final float tmpFloats[] = mTmpFloats;
6454 final Matrix tmpMatrix = mTmpMatrix;
6455
6456 // Compute the desired transformation.
6457 tmpMatrix.setTranslate(frame.left, frame.top);
6458 if (selfTransformation) {
6459 tmpMatrix.preConcat(mTransformation.getMatrix());
6460 }
6461 if (attachedTransformation != null) {
6462 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6463 }
6464 if (appTransformation != null) {
6465 tmpMatrix.preConcat(appTransformation.getMatrix());
6466 }
6467
6468 // "convert" it into SurfaceFlinger's format
6469 // (a 2x2 matrix + an offset)
6470 // Here we must not transform the position of the surface
6471 // since it is already included in the transformation.
6472 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006474 tmpMatrix.getValues(tmpFloats);
6475 mDsDx = tmpFloats[Matrix.MSCALE_X];
6476 mDtDx = tmpFloats[Matrix.MSKEW_X];
6477 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6478 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6479 int x = (int)tmpFloats[Matrix.MTRANS_X];
6480 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6481 int w = frame.width();
6482 int h = frame.height();
6483 mShownFrame.set(x, y, x+w, y+h);
6484
6485 // Now set the alpha... but because our current hardware
6486 // can't do alpha transformation on a non-opaque surface,
6487 // turn it off if we are running an animation that is also
6488 // transforming since it is more important to have that
6489 // animation be smooth.
6490 mShownAlpha = mAlpha;
6491 if (!mLimitedAlphaCompositing
6492 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6493 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6494 && x == frame.left && y == frame.top))) {
6495 //Log.i(TAG, "Applying alpha transform");
6496 if (selfTransformation) {
6497 mShownAlpha *= mTransformation.getAlpha();
6498 }
6499 if (attachedTransformation != null) {
6500 mShownAlpha *= attachedTransformation.getAlpha();
6501 }
6502 if (appTransformation != null) {
6503 mShownAlpha *= appTransformation.getAlpha();
6504 }
6505 } else {
6506 //Log.i(TAG, "Not applying alpha transform");
6507 }
Romain Guy06882f82009-06-10 13:36:04 -07006508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 if (localLOGV) Log.v(
6510 TAG, "Continuing animation in " + this +
6511 ": " + mShownFrame +
6512 ", alpha=" + mTransformation.getAlpha());
6513 return;
6514 }
Romain Guy06882f82009-06-10 13:36:04 -07006515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006516 mShownFrame.set(mFrame);
6517 mShownAlpha = mAlpha;
6518 mDsDx = 1;
6519 mDtDx = 0;
6520 mDsDy = 0;
6521 mDtDy = 1;
6522 }
Romain Guy06882f82009-06-10 13:36:04 -07006523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 /**
6525 * Is this window visible? It is not visible if there is no
6526 * surface, or we are in the process of running an exit animation
6527 * that will remove the surface, or its app token has been hidden.
6528 */
6529 public boolean isVisibleLw() {
6530 final AppWindowToken atoken = mAppToken;
6531 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6532 && (atoken == null || !atoken.hiddenRequested)
6533 && !mExiting && !mDestroying;
6534 }
6535
6536 /**
6537 * Is this window visible, ignoring its app token? It is not visible
6538 * if there is no surface, or we are in the process of running an exit animation
6539 * that will remove the surface.
6540 */
6541 public boolean isWinVisibleLw() {
6542 final AppWindowToken atoken = mAppToken;
6543 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6544 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6545 && !mExiting && !mDestroying;
6546 }
6547
6548 /**
6549 * The same as isVisible(), but follows the current hidden state of
6550 * the associated app token, not the pending requested hidden state.
6551 */
6552 boolean isVisibleNow() {
6553 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006554 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 }
6556
6557 /**
6558 * Same as isVisible(), but we also count it as visible between the
6559 * call to IWindowSession.add() and the first relayout().
6560 */
6561 boolean isVisibleOrAdding() {
6562 final AppWindowToken atoken = mAppToken;
6563 return (mSurface != null
6564 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6565 && mPolicyVisibility && !mAttachedHidden
6566 && (atoken == null || !atoken.hiddenRequested)
6567 && !mExiting && !mDestroying;
6568 }
6569
6570 /**
6571 * Is this window currently on-screen? It is on-screen either if it
6572 * is visible or it is currently running an animation before no longer
6573 * being visible.
6574 */
6575 boolean isOnScreen() {
6576 final AppWindowToken atoken = mAppToken;
6577 if (atoken != null) {
6578 return mSurface != null && mPolicyVisibility && !mDestroying
6579 && ((!mAttachedHidden && !atoken.hiddenRequested)
6580 || mAnimating || atoken.animating);
6581 } else {
6582 return mSurface != null && mPolicyVisibility && !mDestroying
6583 && (!mAttachedHidden || mAnimating);
6584 }
6585 }
Romain Guy06882f82009-06-10 13:36:04 -07006586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006587 /**
6588 * Like isOnScreen(), but we don't return true if the window is part
6589 * of a transition that has not yet been started.
6590 */
6591 boolean isReadyForDisplay() {
6592 final AppWindowToken atoken = mAppToken;
6593 final boolean animating = atoken != null ? atoken.animating : false;
6594 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006595 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006596 || mAnimating || animating);
6597 }
6598
6599 /** Is the window or its container currently animating? */
6600 boolean isAnimating() {
6601 final WindowState attached = mAttachedWindow;
6602 final AppWindowToken atoken = mAppToken;
6603 return mAnimation != null
6604 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006605 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 (atoken.animation != null
6607 || atoken.inPendingTransaction));
6608 }
6609
6610 /** Is this window currently animating? */
6611 boolean isWindowAnimating() {
6612 return mAnimation != null;
6613 }
6614
6615 /**
6616 * Like isOnScreen, but returns false if the surface hasn't yet
6617 * been drawn.
6618 */
6619 public boolean isDisplayedLw() {
6620 final AppWindowToken atoken = mAppToken;
6621 return mSurface != null && mPolicyVisibility && !mDestroying
6622 && !mDrawPending && !mCommitDrawPending
6623 && ((!mAttachedHidden &&
6624 (atoken == null || !atoken.hiddenRequested))
6625 || mAnimating);
6626 }
6627
6628 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6629 boolean shownFrame, boolean onlyOpaque) {
6630 if (mSurface == null) {
6631 return false;
6632 }
6633 if (mAppToken != null && !mAppToken.appFullscreen) {
6634 return false;
6635 }
6636 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6637 return false;
6638 }
6639 final Rect frame = shownFrame ? mShownFrame : mFrame;
6640 if (frame.left <= 0 && frame.top <= 0
6641 && frame.right >= screenWidth
6642 && frame.bottom >= screenHeight) {
6643 return true;
6644 }
6645 return false;
6646 }
Romain Guy06882f82009-06-10 13:36:04 -07006647
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006648 /**
6649 * Return true if the window is opaque and fully drawn.
6650 */
6651 boolean isOpaqueDrawn() {
6652 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
6653 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
6654 }
6655
6656 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
6657 return
6658 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006659 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
6660 // only if it's visible
6661 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006662 // and only if the application wanted to fill the screen
6663 mAttrs.width == mAttrs.FILL_PARENT &&
6664 mAttrs.height == mAttrs.FILL_PARENT &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006665 // and only if the window is not hidden
6666 mFrame.left == mCompatibleScreenFrame.left &&
6667 // and starting window do not need background filler
6668 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING &&
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006669 // and only if the screen is bigger
6670 ((mFrame.right - mFrame.right) < screenWidth ||
6671 (mFrame.bottom - mFrame.top) < screenHeight);
6672 }
6673
6674 boolean isFullscreen(int screenWidth, int screenHeight) {
6675 return mFrame.left <= 0 && mFrame.top <= 0 &&
6676 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006677 }
6678
6679 void removeLocked() {
6680 if (mAttachedWindow != null) {
6681 mAttachedWindow.mChildWindows.remove(this);
6682 }
6683 destroySurfaceLocked();
6684 mSession.windowRemovedLocked();
6685 try {
6686 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6687 } catch (RuntimeException e) {
6688 // Ignore if it has already been removed (usually because
6689 // we are doing this as part of processing a death note.)
6690 }
6691 }
6692
6693 private class DeathRecipient implements IBinder.DeathRecipient {
6694 public void binderDied() {
6695 try {
6696 synchronized(mWindowMap) {
6697 WindowState win = windowForClientLocked(mSession, mClient);
6698 Log.i(TAG, "WIN DEATH: " + win);
6699 if (win != null) {
6700 removeWindowLocked(mSession, win);
6701 }
6702 }
6703 } catch (IllegalArgumentException ex) {
6704 // This will happen if the window has already been
6705 // removed.
6706 }
6707 }
6708 }
6709
6710 /** Returns true if this window desires key events. */
6711 public final boolean canReceiveKeys() {
6712 return isVisibleOrAdding()
6713 && (mViewVisibility == View.VISIBLE)
6714 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6715 }
6716
6717 public boolean hasDrawnLw() {
6718 return mHasDrawn;
6719 }
6720
6721 public boolean showLw(boolean doAnimation) {
6722 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6723 mPolicyVisibility = true;
6724 mPolicyVisibilityAfterAnim = true;
6725 if (doAnimation) {
6726 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6727 }
6728 requestAnimationLocked(0);
6729 return true;
6730 }
6731 return false;
6732 }
6733
6734 public boolean hideLw(boolean doAnimation) {
6735 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6736 : mPolicyVisibility;
6737 if (current) {
6738 if (doAnimation) {
6739 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6740 if (mAnimation == null) {
6741 doAnimation = false;
6742 }
6743 }
6744 if (doAnimation) {
6745 mPolicyVisibilityAfterAnim = false;
6746 } else {
6747 mPolicyVisibilityAfterAnim = false;
6748 mPolicyVisibility = false;
6749 }
6750 requestAnimationLocked(0);
6751 return true;
6752 }
6753 return false;
6754 }
6755
6756 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006757 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07006758
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006759 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6760 pw.print(" mClient="); pw.println(mClient.asBinder());
6761 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6762 if (mAttachedWindow != null || mLayoutAttached) {
6763 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6764 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6765 }
6766 if (mIsImWindow) {
6767 pw.print(prefix); pw.print("mIsImWindow="); pw.println(mIsImWindow);
6768 }
6769 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
6770 pw.print(" mSubLayer="); pw.print(mSubLayer);
6771 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
6772 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
6773 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
6774 pw.print("="); pw.print(mAnimLayer);
6775 pw.print(" mLastLayer="); pw.println(mLastLayer);
6776 if (mSurface != null) {
6777 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
6778 }
6779 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
6780 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
6781 if (mAppToken != null) {
6782 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
6783 }
6784 if (mTargetAppToken != null) {
6785 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
6786 }
6787 pw.print(prefix); pw.print("mViewVisibility=0x");
6788 pw.print(Integer.toHexString(mViewVisibility));
6789 pw.print(" mLastHidden="); pw.print(mLastHidden);
6790 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
6791 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
6792 pw.print(prefix); pw.print("mPolicyVisibility=");
6793 pw.print(mPolicyVisibility);
6794 pw.print(" mPolicyVisibilityAfterAnim=");
6795 pw.print(mPolicyVisibilityAfterAnim);
6796 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
6797 }
6798 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
6799 pw.print(" h="); pw.print(mRequestedHeight);
6800 pw.print(" x="); pw.print(mReqXPos);
6801 pw.print(" y="); pw.println(mReqYPos);
6802 pw.print(prefix); pw.print("mGivenContentInsets=");
6803 mGivenContentInsets.printShortString(pw);
6804 pw.print(" mGivenVisibleInsets=");
6805 mGivenVisibleInsets.printShortString(pw);
6806 pw.println();
6807 if (mTouchableInsets != 0 || mGivenInsetsPending) {
6808 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
6809 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
6810 }
6811 pw.print(prefix); pw.print("mShownFrame=");
6812 mShownFrame.printShortString(pw);
6813 pw.print(" last="); mLastShownFrame.printShortString(pw);
6814 pw.println();
6815 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
6816 pw.print(" last="); mLastFrame.printShortString(pw);
6817 pw.println();
6818 pw.print(prefix); pw.print("mContainingFrame=");
6819 mContainingFrame.printShortString(pw);
6820 pw.print(" mDisplayFrame=");
6821 mDisplayFrame.printShortString(pw);
6822 pw.println();
6823 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
6824 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
6825 pw.println();
6826 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
6827 pw.print(" last="); mLastContentInsets.printShortString(pw);
6828 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
6829 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
6830 pw.println();
6831 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
6832 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
6833 pw.print(" mAlpha="); pw.print(mAlpha);
6834 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
6835 }
6836 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
6837 || mAnimation != null) {
6838 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
6839 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
6840 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
6841 pw.print(" mAnimation="); pw.println(mAnimation);
6842 }
6843 if (mHasTransformation || mHasLocalTransformation) {
6844 pw.print(prefix); pw.print("XForm: has=");
6845 pw.print(mHasTransformation);
6846 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
6847 pw.print(" "); mTransformation.printShortString(pw);
6848 pw.println();
6849 }
6850 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
6851 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
6852 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
6853 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
6854 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
6855 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
6856 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
6857 pw.print(" mDestroying="); pw.print(mDestroying);
6858 pw.print(" mRemoved="); pw.println(mRemoved);
6859 }
6860 if (mOrientationChanging || mAppFreezing) {
6861 pw.print(prefix); pw.print("mOrientationChanging=");
6862 pw.print(mOrientationChanging);
6863 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
6864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865 }
6866
6867 @Override
6868 public String toString() {
6869 return "Window{"
6870 + Integer.toHexString(System.identityHashCode(this))
6871 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
6872 }
6873 }
Romain Guy06882f82009-06-10 13:36:04 -07006874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 // -------------------------------------------------------------
6876 // Window Token State
6877 // -------------------------------------------------------------
6878
6879 class WindowToken {
6880 // The actual token.
6881 final IBinder token;
6882
6883 // The type of window this token is for, as per WindowManager.LayoutParams.
6884 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07006885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006886 // Set if this token was explicitly added by a client, so should
6887 // not be removed when all windows are removed.
6888 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07006889
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006890 // For printing.
6891 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07006892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 // If this is an AppWindowToken, this is non-null.
6894 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07006895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896 // All of the windows associated with this token.
6897 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
6898
6899 // Is key dispatching paused for this token?
6900 boolean paused = false;
6901
6902 // Should this token's windows be hidden?
6903 boolean hidden;
6904
6905 // Temporary for finding which tokens no longer have visible windows.
6906 boolean hasVisible;
6907
6908 WindowToken(IBinder _token, int type, boolean _explicit) {
6909 token = _token;
6910 windowType = type;
6911 explicit = _explicit;
6912 }
6913
6914 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006915 pw.print(prefix); pw.print("token="); pw.println(token);
6916 pw.print(prefix); pw.print("windows="); pw.println(windows);
6917 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
6918 pw.print(" hidden="); pw.print(hidden);
6919 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006920 }
6921
6922 @Override
6923 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006924 if (stringName == null) {
6925 StringBuilder sb = new StringBuilder();
6926 sb.append("WindowToken{");
6927 sb.append(Integer.toHexString(System.identityHashCode(this)));
6928 sb.append(" token="); sb.append(token); sb.append('}');
6929 stringName = sb.toString();
6930 }
6931 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 }
6933 };
6934
6935 class AppWindowToken extends WindowToken {
6936 // Non-null only for application tokens.
6937 final IApplicationToken appToken;
6938
6939 // All of the windows and child windows that are included in this
6940 // application token. Note this list is NOT sorted!
6941 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
6942
6943 int groupId = -1;
6944 boolean appFullscreen;
6945 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07006946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006947 // These are used for determining when all windows associated with
6948 // an activity have been drawn, so they can be made visible together
6949 // at the same time.
6950 int lastTransactionSequence = mTransactionSequence-1;
6951 int numInterestingWindows;
6952 int numDrawnWindows;
6953 boolean inPendingTransaction;
6954 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07006955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 // Is this token going to be hidden in a little while? If so, it
6957 // won't be taken into account for setting the screen orientation.
6958 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006960 // Is this window's surface needed? This is almost like hidden, except
6961 // it will sometimes be true a little earlier: when the token has
6962 // been shown, but is still waiting for its app transition to execute
6963 // before making its windows shown.
6964 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07006965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006966 // Have we told the window clients to hide themselves?
6967 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006969 // Last visibility state we reported to the app token.
6970 boolean reportedVisible;
6971
6972 // Set to true when the token has been removed from the window mgr.
6973 boolean removed;
6974
6975 // Have we been asked to have this token keep the screen frozen?
6976 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006978 boolean animating;
6979 Animation animation;
6980 boolean hasTransformation;
6981 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07006982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 // Offset to the window of all layers in the token, for use by
6984 // AppWindowToken animations.
6985 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07006986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006987 // Information about an application starting window if displayed.
6988 StartingData startingData;
6989 WindowState startingWindow;
6990 View startingView;
6991 boolean startingDisplayed;
6992 boolean startingMoved;
6993 boolean firstWindowDrawn;
6994
6995 AppWindowToken(IApplicationToken _token) {
6996 super(_token.asBinder(),
6997 WindowManager.LayoutParams.TYPE_APPLICATION, true);
6998 appWindowToken = this;
6999 appToken = _token;
7000 }
Romain Guy06882f82009-06-10 13:36:04 -07007001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 public void setAnimation(Animation anim) {
7003 if (localLOGV) Log.v(
7004 TAG, "Setting animation in " + this + ": " + anim);
7005 animation = anim;
7006 animating = false;
7007 anim.restrictDuration(MAX_ANIMATION_DURATION);
7008 anim.scaleCurrentDuration(mTransitionAnimationScale);
7009 int zorder = anim.getZAdjustment();
7010 int adj = 0;
7011 if (zorder == Animation.ZORDER_TOP) {
7012 adj = TYPE_LAYER_OFFSET;
7013 } else if (zorder == Animation.ZORDER_BOTTOM) {
7014 adj = -TYPE_LAYER_OFFSET;
7015 }
Romain Guy06882f82009-06-10 13:36:04 -07007016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007017 if (animLayerAdjustment != adj) {
7018 animLayerAdjustment = adj;
7019 updateLayers();
7020 }
7021 }
Romain Guy06882f82009-06-10 13:36:04 -07007022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007023 public void setDummyAnimation() {
7024 if (animation == null) {
7025 if (localLOGV) Log.v(
7026 TAG, "Setting dummy animation in " + this);
7027 animation = sDummyAnimation;
7028 }
7029 }
7030
7031 public void clearAnimation() {
7032 if (animation != null) {
7033 animation = null;
7034 animating = true;
7035 }
7036 }
Romain Guy06882f82009-06-10 13:36:04 -07007037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007038 void updateLayers() {
7039 final int N = allAppWindows.size();
7040 final int adj = animLayerAdjustment;
7041 for (int i=0; i<N; i++) {
7042 WindowState w = allAppWindows.get(i);
7043 w.mAnimLayer = w.mLayer + adj;
7044 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7045 + w.mAnimLayer);
7046 if (w == mInputMethodTarget) {
7047 setInputMethodAnimLayerAdjustment(adj);
7048 }
7049 }
7050 }
Romain Guy06882f82009-06-10 13:36:04 -07007051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007052 void sendAppVisibilityToClients() {
7053 final int N = allAppWindows.size();
7054 for (int i=0; i<N; i++) {
7055 WindowState win = allAppWindows.get(i);
7056 if (win == startingWindow && clientHidden) {
7057 // Don't hide the starting window.
7058 continue;
7059 }
7060 try {
7061 if (DEBUG_VISIBILITY) Log.v(TAG,
7062 "Setting visibility of " + win + ": " + (!clientHidden));
7063 win.mClient.dispatchAppVisibility(!clientHidden);
7064 } catch (RemoteException e) {
7065 }
7066 }
7067 }
Romain Guy06882f82009-06-10 13:36:04 -07007068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 void showAllWindowsLocked() {
7070 final int NW = allAppWindows.size();
7071 for (int i=0; i<NW; i++) {
7072 WindowState w = allAppWindows.get(i);
7073 if (DEBUG_VISIBILITY) Log.v(TAG,
7074 "performing show on: " + w);
7075 w.performShowLocked();
7076 }
7077 }
Romain Guy06882f82009-06-10 13:36:04 -07007078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007079 // This must be called while inside a transaction.
7080 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7081 if (!mDisplayFrozen) {
7082 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007084 if (animation == sDummyAnimation) {
7085 // This guy is going to animate, but not yet. For now count
7086 // it is not animating for purposes of scheduling transactions;
7087 // when it is really time to animate, this will be set to
7088 // a real animation and the next call will execute normally.
7089 return false;
7090 }
Romain Guy06882f82009-06-10 13:36:04 -07007091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007092 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7093 if (!animating) {
7094 if (DEBUG_ANIM) Log.v(
7095 TAG, "Starting animation in " + this +
7096 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7097 + " scale=" + mTransitionAnimationScale
7098 + " allDrawn=" + allDrawn + " animating=" + animating);
7099 animation.initialize(dw, dh, dw, dh);
7100 animation.setStartTime(currentTime);
7101 animating = true;
7102 }
7103 transformation.clear();
7104 final boolean more = animation.getTransformation(
7105 currentTime, transformation);
7106 if (DEBUG_ANIM) Log.v(
7107 TAG, "Stepped animation in " + this +
7108 ": more=" + more + ", xform=" + transformation);
7109 if (more) {
7110 // we're done!
7111 hasTransformation = true;
7112 return true;
7113 }
7114 if (DEBUG_ANIM) Log.v(
7115 TAG, "Finished animation in " + this +
7116 " @ " + currentTime);
7117 animation = null;
7118 }
7119 } else if (animation != null) {
7120 // If the display is frozen, and there is a pending animation,
7121 // clear it and make sure we run the cleanup code.
7122 animating = true;
7123 animation = null;
7124 }
7125
7126 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 if (!animating) {
7129 return false;
7130 }
7131
7132 clearAnimation();
7133 animating = false;
7134 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7135 moveInputMethodWindowsIfNeededLocked(true);
7136 }
Romain Guy06882f82009-06-10 13:36:04 -07007137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007138 if (DEBUG_ANIM) Log.v(
7139 TAG, "Animation done in " + this
7140 + ": reportedVisible=" + reportedVisible);
7141
7142 transformation.clear();
7143 if (animLayerAdjustment != 0) {
7144 animLayerAdjustment = 0;
7145 updateLayers();
7146 }
Romain Guy06882f82009-06-10 13:36:04 -07007147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007148 final int N = windows.size();
7149 for (int i=0; i<N; i++) {
7150 ((WindowState)windows.get(i)).finishExit();
7151 }
7152 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007154 return false;
7155 }
7156
7157 void updateReportedVisibilityLocked() {
7158 if (appToken == null) {
7159 return;
7160 }
Romain Guy06882f82009-06-10 13:36:04 -07007161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007162 int numInteresting = 0;
7163 int numVisible = 0;
7164 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007166 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7167 final int N = allAppWindows.size();
7168 for (int i=0; i<N; i++) {
7169 WindowState win = allAppWindows.get(i);
7170 if (win == startingWindow || win.mAppFreezing) {
7171 continue;
7172 }
7173 if (DEBUG_VISIBILITY) {
7174 Log.v(TAG, "Win " + win + ": isDisplayed="
7175 + win.isDisplayedLw()
7176 + ", isAnimating=" + win.isAnimating());
7177 if (!win.isDisplayedLw()) {
7178 Log.v(TAG, "Not displayed: s=" + win.mSurface
7179 + " pv=" + win.mPolicyVisibility
7180 + " dp=" + win.mDrawPending
7181 + " cdp=" + win.mCommitDrawPending
7182 + " ah=" + win.mAttachedHidden
7183 + " th="
7184 + (win.mAppToken != null
7185 ? win.mAppToken.hiddenRequested : false)
7186 + " a=" + win.mAnimating);
7187 }
7188 }
7189 numInteresting++;
7190 if (win.isDisplayedLw()) {
7191 if (!win.isAnimating()) {
7192 numVisible++;
7193 }
7194 nowGone = false;
7195 } else if (win.isAnimating()) {
7196 nowGone = false;
7197 }
7198 }
Romain Guy06882f82009-06-10 13:36:04 -07007199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007200 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7201 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7202 + numInteresting + " visible=" + numVisible);
7203 if (nowVisible != reportedVisible) {
7204 if (DEBUG_VISIBILITY) Log.v(
7205 TAG, "Visibility changed in " + this
7206 + ": vis=" + nowVisible);
7207 reportedVisible = nowVisible;
7208 Message m = mH.obtainMessage(
7209 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7210 nowVisible ? 1 : 0,
7211 nowGone ? 1 : 0,
7212 this);
7213 mH.sendMessage(m);
7214 }
7215 }
Romain Guy06882f82009-06-10 13:36:04 -07007216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007217 void dump(PrintWriter pw, String prefix) {
7218 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007219 if (appToken != null) {
7220 pw.print(prefix); pw.println("app=true");
7221 }
7222 if (allAppWindows.size() > 0) {
7223 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7224 }
7225 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7226 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7227 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7228 pw.print(" clientHidden="); pw.print(clientHidden);
7229 pw.print(" willBeHidden="); pw.print(willBeHidden);
7230 pw.print(" reportedVisible="); pw.println(reportedVisible);
7231 if (paused || freezingScreen) {
7232 pw.print(prefix); pw.print("paused="); pw.print(paused);
7233 pw.print(" freezingScreen="); pw.println(freezingScreen);
7234 }
7235 if (numInterestingWindows != 0 || numDrawnWindows != 0
7236 || inPendingTransaction || allDrawn) {
7237 pw.print(prefix); pw.print("numInterestingWindows=");
7238 pw.print(numInterestingWindows);
7239 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7240 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7241 pw.print(" allDrawn="); pw.println(allDrawn);
7242 }
7243 if (animating || animation != null) {
7244 pw.print(prefix); pw.print("animating="); pw.print(animating);
7245 pw.print(" animation="); pw.println(animation);
7246 }
7247 if (animLayerAdjustment != 0) {
7248 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7249 }
7250 if (hasTransformation) {
7251 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7252 pw.print(" transformation="); transformation.printShortString(pw);
7253 pw.println();
7254 }
7255 if (startingData != null || removed || firstWindowDrawn) {
7256 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7257 pw.print(" removed="); pw.print(removed);
7258 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7259 }
7260 if (startingWindow != null || startingView != null
7261 || startingDisplayed || startingMoved) {
7262 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7263 pw.print(" startingView="); pw.print(startingView);
7264 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7265 pw.print(" startingMoved"); pw.println(startingMoved);
7266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007267 }
7268
7269 @Override
7270 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007271 if (stringName == null) {
7272 StringBuilder sb = new StringBuilder();
7273 sb.append("AppWindowToken{");
7274 sb.append(Integer.toHexString(System.identityHashCode(this)));
7275 sb.append(" token="); sb.append(token); sb.append('}');
7276 stringName = sb.toString();
7277 }
7278 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 }
7280 }
Romain Guy06882f82009-06-10 13:36:04 -07007281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007282 public static WindowManager.LayoutParams findAnimations(
7283 ArrayList<AppWindowToken> order,
7284 ArrayList<AppWindowToken> tokenList1,
7285 ArrayList<AppWindowToken> tokenList2) {
7286 // We need to figure out which animation to use...
7287 WindowManager.LayoutParams animParams = null;
7288 int animSrc = 0;
Romain Guy06882f82009-06-10 13:36:04 -07007289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 //Log.i(TAG, "Looking for animations...");
7291 for (int i=order.size()-1; i>=0; i--) {
7292 AppWindowToken wtoken = order.get(i);
7293 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
7294 if (tokenList1.contains(wtoken) || tokenList2.contains(wtoken)) {
7295 int j = wtoken.windows.size();
7296 while (j > 0) {
7297 j--;
7298 WindowState win = wtoken.windows.get(j);
7299 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7300 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7301 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7302 //Log.i(TAG, "Found base or application window, done!");
7303 if (wtoken.appFullscreen) {
7304 return win.mAttrs;
7305 }
7306 if (animSrc < 2) {
7307 animParams = win.mAttrs;
7308 animSrc = 2;
7309 }
7310 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7311 //Log.i(TAG, "Found normal window, we may use this...");
7312 animParams = win.mAttrs;
7313 animSrc = 1;
7314 }
7315 }
7316 }
7317 }
Romain Guy06882f82009-06-10 13:36:04 -07007318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 return animParams;
7320 }
Romain Guy06882f82009-06-10 13:36:04 -07007321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007322 // -------------------------------------------------------------
7323 // DummyAnimation
7324 // -------------------------------------------------------------
7325
7326 // This is an animation that does nothing: it just immediately finishes
7327 // itself every time it is called. It is used as a stub animation in cases
7328 // where we want to synchronize multiple things that may be animating.
7329 static final class DummyAnimation extends Animation {
7330 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7331 return false;
7332 }
7333 }
7334 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007336 // -------------------------------------------------------------
7337 // Async Handler
7338 // -------------------------------------------------------------
7339
7340 static final class StartingData {
7341 final String pkg;
7342 final int theme;
7343 final CharSequence nonLocalizedLabel;
7344 final int labelRes;
7345 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007347 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7348 int _labelRes, int _icon) {
7349 pkg = _pkg;
7350 theme = _theme;
7351 nonLocalizedLabel = _nonLocalizedLabel;
7352 labelRes = _labelRes;
7353 icon = _icon;
7354 }
7355 }
7356
7357 private final class H extends Handler {
7358 public static final int REPORT_FOCUS_CHANGE = 2;
7359 public static final int REPORT_LOSING_FOCUS = 3;
7360 public static final int ANIMATE = 4;
7361 public static final int ADD_STARTING = 5;
7362 public static final int REMOVE_STARTING = 6;
7363 public static final int FINISHED_STARTING = 7;
7364 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7366 public static final int HOLD_SCREEN_CHANGED = 12;
7367 public static final int APP_TRANSITION_TIMEOUT = 13;
7368 public static final int PERSIST_ANIMATION_SCALE = 14;
7369 public static final int FORCE_GC = 15;
7370 public static final int ENABLE_SCREEN = 16;
7371 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007372 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 public H() {
7377 }
Romain Guy06882f82009-06-10 13:36:04 -07007378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007379 @Override
7380 public void handleMessage(Message msg) {
7381 switch (msg.what) {
7382 case REPORT_FOCUS_CHANGE: {
7383 WindowState lastFocus;
7384 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 synchronized(mWindowMap) {
7387 lastFocus = mLastFocus;
7388 newFocus = mCurrentFocus;
7389 if (lastFocus == newFocus) {
7390 // Focus is not changing, so nothing to do.
7391 return;
7392 }
7393 mLastFocus = newFocus;
7394 //Log.i(TAG, "Focus moving from " + lastFocus
7395 // + " to " + newFocus);
7396 if (newFocus != null && lastFocus != null
7397 && !newFocus.isDisplayedLw()) {
7398 //Log.i(TAG, "Delaying loss of focus...");
7399 mLosingFocus.add(lastFocus);
7400 lastFocus = null;
7401 }
7402 }
7403
7404 if (lastFocus != newFocus) {
7405 //System.out.println("Changing focus from " + lastFocus
7406 // + " to " + newFocus);
7407 if (newFocus != null) {
7408 try {
7409 //Log.i(TAG, "Gaining focus: " + newFocus);
7410 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7411 } catch (RemoteException e) {
7412 // Ignore if process has died.
7413 }
7414 }
7415
7416 if (lastFocus != null) {
7417 try {
7418 //Log.i(TAG, "Losing focus: " + lastFocus);
7419 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7420 } catch (RemoteException e) {
7421 // Ignore if process has died.
7422 }
7423 }
7424 }
7425 } break;
7426
7427 case REPORT_LOSING_FOCUS: {
7428 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007430 synchronized(mWindowMap) {
7431 losers = mLosingFocus;
7432 mLosingFocus = new ArrayList<WindowState>();
7433 }
7434
7435 final int N = losers.size();
7436 for (int i=0; i<N; i++) {
7437 try {
7438 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7439 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7440 } catch (RemoteException e) {
7441 // Ignore if process has died.
7442 }
7443 }
7444 } break;
7445
7446 case ANIMATE: {
7447 synchronized(mWindowMap) {
7448 mAnimationPending = false;
7449 performLayoutAndPlaceSurfacesLocked();
7450 }
7451 } break;
7452
7453 case ADD_STARTING: {
7454 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7455 final StartingData sd = wtoken.startingData;
7456
7457 if (sd == null) {
7458 // Animation has been canceled... do nothing.
7459 return;
7460 }
Romain Guy06882f82009-06-10 13:36:04 -07007461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7463 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 View view = null;
7466 try {
7467 view = mPolicy.addStartingWindow(
7468 wtoken.token, sd.pkg,
7469 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7470 sd.icon);
7471 } catch (Exception e) {
7472 Log.w(TAG, "Exception when adding starting window", e);
7473 }
7474
7475 if (view != null) {
7476 boolean abort = false;
7477
7478 synchronized(mWindowMap) {
7479 if (wtoken.removed || wtoken.startingData == null) {
7480 // If the window was successfully added, then
7481 // we need to remove it.
7482 if (wtoken.startingWindow != null) {
7483 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7484 "Aborted starting " + wtoken
7485 + ": removed=" + wtoken.removed
7486 + " startingData=" + wtoken.startingData);
7487 wtoken.startingWindow = null;
7488 wtoken.startingData = null;
7489 abort = true;
7490 }
7491 } else {
7492 wtoken.startingView = view;
7493 }
7494 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7495 "Added starting " + wtoken
7496 + ": startingWindow="
7497 + wtoken.startingWindow + " startingView="
7498 + wtoken.startingView);
7499 }
7500
7501 if (abort) {
7502 try {
7503 mPolicy.removeStartingWindow(wtoken.token, view);
7504 } catch (Exception e) {
7505 Log.w(TAG, "Exception when removing starting window", e);
7506 }
7507 }
7508 }
7509 } break;
7510
7511 case REMOVE_STARTING: {
7512 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7513 IBinder token = null;
7514 View view = null;
7515 synchronized (mWindowMap) {
7516 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7517 + wtoken + ": startingWindow="
7518 + wtoken.startingWindow + " startingView="
7519 + wtoken.startingView);
7520 if (wtoken.startingWindow != null) {
7521 view = wtoken.startingView;
7522 token = wtoken.token;
7523 wtoken.startingData = null;
7524 wtoken.startingView = null;
7525 wtoken.startingWindow = null;
7526 }
7527 }
7528 if (view != null) {
7529 try {
7530 mPolicy.removeStartingWindow(token, view);
7531 } catch (Exception e) {
7532 Log.w(TAG, "Exception when removing starting window", e);
7533 }
7534 }
7535 } break;
7536
7537 case FINISHED_STARTING: {
7538 IBinder token = null;
7539 View view = null;
7540 while (true) {
7541 synchronized (mWindowMap) {
7542 final int N = mFinishedStarting.size();
7543 if (N <= 0) {
7544 break;
7545 }
7546 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7547
7548 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7549 "Finished starting " + wtoken
7550 + ": startingWindow=" + wtoken.startingWindow
7551 + " startingView=" + wtoken.startingView);
7552
7553 if (wtoken.startingWindow == null) {
7554 continue;
7555 }
7556
7557 view = wtoken.startingView;
7558 token = wtoken.token;
7559 wtoken.startingData = null;
7560 wtoken.startingView = null;
7561 wtoken.startingWindow = null;
7562 }
7563
7564 try {
7565 mPolicy.removeStartingWindow(token, view);
7566 } catch (Exception e) {
7567 Log.w(TAG, "Exception when removing starting window", e);
7568 }
7569 }
7570 } break;
7571
7572 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7573 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7574
7575 boolean nowVisible = msg.arg1 != 0;
7576 boolean nowGone = msg.arg2 != 0;
7577
7578 try {
7579 if (DEBUG_VISIBILITY) Log.v(
7580 TAG, "Reporting visible in " + wtoken
7581 + " visible=" + nowVisible
7582 + " gone=" + nowGone);
7583 if (nowVisible) {
7584 wtoken.appToken.windowsVisible();
7585 } else {
7586 wtoken.appToken.windowsGone();
7587 }
7588 } catch (RemoteException ex) {
7589 }
7590 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007592 case WINDOW_FREEZE_TIMEOUT: {
7593 synchronized (mWindowMap) {
7594 Log.w(TAG, "Window freeze timeout expired.");
7595 int i = mWindows.size();
7596 while (i > 0) {
7597 i--;
7598 WindowState w = (WindowState)mWindows.get(i);
7599 if (w.mOrientationChanging) {
7600 w.mOrientationChanging = false;
7601 Log.w(TAG, "Force clearing orientation change: " + w);
7602 }
7603 }
7604 performLayoutAndPlaceSurfacesLocked();
7605 }
7606 break;
7607 }
Romain Guy06882f82009-06-10 13:36:04 -07007608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609 case HOLD_SCREEN_CHANGED: {
7610 Session oldHold;
7611 Session newHold;
7612 synchronized (mWindowMap) {
7613 oldHold = mLastReportedHold;
7614 newHold = (Session)msg.obj;
7615 mLastReportedHold = newHold;
7616 }
Romain Guy06882f82009-06-10 13:36:04 -07007617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 if (oldHold != newHold) {
7619 try {
7620 if (oldHold != null) {
7621 mBatteryStats.noteStopWakelock(oldHold.mUid,
7622 "window",
7623 BatteryStats.WAKE_TYPE_WINDOW);
7624 }
7625 if (newHold != null) {
7626 mBatteryStats.noteStartWakelock(newHold.mUid,
7627 "window",
7628 BatteryStats.WAKE_TYPE_WINDOW);
7629 }
7630 } catch (RemoteException e) {
7631 }
7632 }
7633 break;
7634 }
Romain Guy06882f82009-06-10 13:36:04 -07007635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 case APP_TRANSITION_TIMEOUT: {
7637 synchronized (mWindowMap) {
7638 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7639 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7640 "*** APP TRANSITION TIMEOUT");
7641 mAppTransitionReady = true;
7642 mAppTransitionTimeout = true;
7643 performLayoutAndPlaceSurfacesLocked();
7644 }
7645 }
7646 break;
7647 }
Romain Guy06882f82009-06-10 13:36:04 -07007648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 case PERSIST_ANIMATION_SCALE: {
7650 Settings.System.putFloat(mContext.getContentResolver(),
7651 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7652 Settings.System.putFloat(mContext.getContentResolver(),
7653 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7654 break;
7655 }
Romain Guy06882f82009-06-10 13:36:04 -07007656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 case FORCE_GC: {
7658 synchronized(mWindowMap) {
7659 if (mAnimationPending) {
7660 // If we are animating, don't do the gc now but
7661 // delay a bit so we don't interrupt the animation.
7662 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7663 2000);
7664 return;
7665 }
7666 // If we are currently rotating the display, it will
7667 // schedule a new message when done.
7668 if (mDisplayFrozen) {
7669 return;
7670 }
7671 mFreezeGcPending = 0;
7672 }
7673 Runtime.getRuntime().gc();
7674 break;
7675 }
Romain Guy06882f82009-06-10 13:36:04 -07007676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 case ENABLE_SCREEN: {
7678 performEnableScreen();
7679 break;
7680 }
Romain Guy06882f82009-06-10 13:36:04 -07007681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007682 case APP_FREEZE_TIMEOUT: {
7683 synchronized (mWindowMap) {
7684 Log.w(TAG, "App freeze timeout expired.");
7685 int i = mAppTokens.size();
7686 while (i > 0) {
7687 i--;
7688 AppWindowToken tok = mAppTokens.get(i);
7689 if (tok.freezingScreen) {
7690 Log.w(TAG, "Force clearing freeze: " + tok);
7691 unsetAppFreezingScreenLocked(tok, true, true);
7692 }
7693 }
7694 }
7695 break;
7696 }
Romain Guy06882f82009-06-10 13:36:04 -07007697
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007698 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07007699 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007700 sendNewConfiguration();
7701 }
7702 break;
7703 }
Romain Guy06882f82009-06-10 13:36:04 -07007704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007705 }
7706 }
7707 }
7708
7709 // -------------------------------------------------------------
7710 // IWindowManager API
7711 // -------------------------------------------------------------
7712
7713 public IWindowSession openSession(IInputMethodClient client,
7714 IInputContext inputContext) {
7715 if (client == null) throw new IllegalArgumentException("null client");
7716 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7717 return new Session(client, inputContext);
7718 }
7719
7720 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7721 synchronized (mWindowMap) {
7722 // The focus for the client is the window immediately below
7723 // where we would place the input method window.
7724 int idx = findDesiredInputMethodWindowIndexLocked(false);
7725 WindowState imFocus;
7726 if (idx > 0) {
7727 imFocus = (WindowState)mWindows.get(idx-1);
7728 if (imFocus != null) {
7729 if (imFocus.mSession.mClient != null &&
7730 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7731 return true;
7732 }
7733 }
7734 }
7735 }
7736 return false;
7737 }
Romain Guy06882f82009-06-10 13:36:04 -07007738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007739 // -------------------------------------------------------------
7740 // Internals
7741 // -------------------------------------------------------------
7742
7743 final WindowState windowForClientLocked(Session session, IWindow client) {
7744 return windowForClientLocked(session, client.asBinder());
7745 }
Romain Guy06882f82009-06-10 13:36:04 -07007746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007747 final WindowState windowForClientLocked(Session session, IBinder client) {
7748 WindowState win = mWindowMap.get(client);
7749 if (localLOGV) Log.v(
7750 TAG, "Looking up client " + client + ": " + win);
7751 if (win == null) {
7752 RuntimeException ex = new RuntimeException();
7753 Log.w(TAG, "Requested window " + client + " does not exist", ex);
7754 return null;
7755 }
7756 if (session != null && win.mSession != session) {
7757 RuntimeException ex = new RuntimeException();
7758 Log.w(TAG, "Requested window " + client + " is in session " +
7759 win.mSession + ", not " + session, ex);
7760 return null;
7761 }
7762
7763 return win;
7764 }
7765
7766 private final void assignLayersLocked() {
7767 int N = mWindows.size();
7768 int curBaseLayer = 0;
7769 int curLayer = 0;
7770 int i;
Romain Guy06882f82009-06-10 13:36:04 -07007771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007772 for (i=0; i<N; i++) {
7773 WindowState w = (WindowState)mWindows.get(i);
7774 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow) {
7775 curLayer += WINDOW_LAYER_MULTIPLIER;
7776 w.mLayer = curLayer;
7777 } else {
7778 curBaseLayer = curLayer = w.mBaseLayer;
7779 w.mLayer = curLayer;
7780 }
7781 if (w.mTargetAppToken != null) {
7782 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
7783 } else if (w.mAppToken != null) {
7784 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
7785 } else {
7786 w.mAnimLayer = w.mLayer;
7787 }
7788 if (w.mIsImWindow) {
7789 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
7790 }
7791 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
7792 + w.mAnimLayer);
7793 //System.out.println(
7794 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
7795 }
7796 }
7797
7798 private boolean mInLayout = false;
7799 private final void performLayoutAndPlaceSurfacesLocked() {
7800 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07007801 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 throw new RuntimeException("Recursive call!");
7803 }
7804 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
7805 return;
7806 }
7807
7808 boolean recoveringMemory = false;
7809 if (mForceRemoves != null) {
7810 recoveringMemory = true;
7811 // Wait a little it for things to settle down, and off we go.
7812 for (int i=0; i<mForceRemoves.size(); i++) {
7813 WindowState ws = mForceRemoves.get(i);
7814 Log.i(TAG, "Force removing: " + ws);
7815 removeWindowInnerLocked(ws.mSession, ws);
7816 }
7817 mForceRemoves = null;
7818 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
7819 Object tmp = new Object();
7820 synchronized (tmp) {
7821 try {
7822 tmp.wait(250);
7823 } catch (InterruptedException e) {
7824 }
7825 }
7826 }
Romain Guy06882f82009-06-10 13:36:04 -07007827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 mInLayout = true;
7829 try {
7830 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07007831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 int i = mPendingRemove.size()-1;
7833 if (i >= 0) {
7834 while (i >= 0) {
7835 WindowState w = mPendingRemove.get(i);
7836 removeWindowInnerLocked(w.mSession, w);
7837 i--;
7838 }
7839 mPendingRemove.clear();
7840
7841 mInLayout = false;
7842 assignLayersLocked();
7843 mLayoutNeeded = true;
7844 performLayoutAndPlaceSurfacesLocked();
7845
7846 } else {
7847 mInLayout = false;
7848 if (mLayoutNeeded) {
7849 requestAnimationLocked(0);
7850 }
7851 }
7852 } catch (RuntimeException e) {
7853 mInLayout = false;
7854 Log.e(TAG, "Unhandled exception while layout out windows", e);
7855 }
7856 }
7857
7858 private final void performLayoutLockedInner() {
7859 final int dw = mDisplay.getWidth();
7860 final int dh = mDisplay.getHeight();
7861
7862 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007863 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 int i;
7865
7866 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07007867
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007868 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 mPolicy.beginLayoutLw(dw, dh);
7870
7871 // First perform layout of any root windows (not attached
7872 // to another window).
7873 int topAttached = -1;
7874 for (i = N-1; i >= 0; i--) {
7875 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007876
7877 // Don't do layout of a window if it is not visible, or
7878 // soon won't be visible, to avoid wasting time and funky
7879 // changes while a window is animating away.
7880 final AppWindowToken atoken = win.mAppToken;
7881 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007882 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007883 || win.mRootToken.hidden
7884 || (atoken != null && atoken.hiddenRequested)
7885 || !win.mPolicyVisibility
7886 || win.mAttachedHidden
7887 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007888
7889 // If this view is GONE, then skip it -- keep the current
7890 // frame, and let the caller know so they can ignore it
7891 // if they want. (We do the normal layout for INVISIBLE
7892 // windows, since that means "perform layout as normal,
7893 // just don't display").
7894 if (!gone || !win.mHaveFrame) {
7895 if (!win.mLayoutAttached) {
7896 mPolicy.layoutWindowLw(win, win.mAttrs, null);
7897 } else {
7898 if (topAttached < 0) topAttached = i;
7899 }
7900 }
7901 }
Romain Guy06882f82009-06-10 13:36:04 -07007902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 // Now perform layout of attached windows, which usually
7904 // depend on the position of the window they are attached to.
7905 // XXX does not deal with windows that are attached to windows
7906 // that are themselves attached.
7907 for (i = topAttached; i >= 0; i--) {
7908 WindowState win = (WindowState) mWindows.get(i);
7909
7910 // If this view is GONE, then skip it -- keep the current
7911 // frame, and let the caller know so they can ignore it
7912 // if they want. (We do the normal layout for INVISIBLE
7913 // windows, since that means "perform layout as normal,
7914 // just don't display").
7915 if (win.mLayoutAttached) {
7916 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
7917 || !win.mHaveFrame) {
7918 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
7919 }
7920 }
7921 }
7922
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007923 if (!mPolicy.finishLayoutLw()) {
7924 mLayoutNeeded = false;
7925 } else if (repeats > 2) {
7926 Log.w(TAG, "Layout repeat aborted after too many iterations");
7927 mLayoutNeeded = false;
7928 } else {
7929 repeats++;
7930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931 }
7932 }
Romain Guy06882f82009-06-10 13:36:04 -07007933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007934 private final void performLayoutAndPlaceSurfacesLockedInner(
7935 boolean recoveringMemory) {
7936 final long currentTime = SystemClock.uptimeMillis();
7937 final int dw = mDisplay.getWidth();
7938 final int dh = mDisplay.getHeight();
7939
7940 final int N = mWindows.size();
7941 int i;
7942
7943 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007944 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07007945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946 if (mFxSession == null) {
7947 mFxSession = new SurfaceSession();
7948 }
Romain Guy06882f82009-06-10 13:36:04 -07007949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007950 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
7951
7952 // Initialize state of exiting tokens.
7953 for (i=mExitingTokens.size()-1; i>=0; i--) {
7954 mExitingTokens.get(i).hasVisible = false;
7955 }
7956
7957 // Initialize state of exiting applications.
7958 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
7959 mExitingAppTokens.get(i).hasVisible = false;
7960 }
7961
7962 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007963 boolean orientationChangeComplete = true;
7964 Session holdScreen = null;
7965 float screenBrightness = -1;
7966 boolean focusDisplayed = false;
7967 boolean animating = false;
7968
7969 Surface.openTransaction();
7970 try {
7971 boolean restart;
7972
7973 do {
7974 final int transactionSequence = ++mTransactionSequence;
7975
7976 // Update animations of all applications, including those
7977 // associated with exiting/removed apps
7978 boolean tokensAnimating = false;
7979 final int NAT = mAppTokens.size();
7980 for (i=0; i<NAT; i++) {
7981 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7982 tokensAnimating = true;
7983 }
7984 }
7985 final int NEAT = mExitingAppTokens.size();
7986 for (i=0; i<NEAT; i++) {
7987 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7988 tokensAnimating = true;
7989 }
7990 }
7991
7992 animating = tokensAnimating;
7993 restart = false;
7994
7995 boolean tokenMayBeDrawn = false;
7996
7997 mPolicy.beginAnimationLw(dw, dh);
7998
7999 for (i=N-1; i>=0; i--) {
8000 WindowState w = (WindowState)mWindows.get(i);
8001
8002 final WindowManager.LayoutParams attrs = w.mAttrs;
8003
8004 if (w.mSurface != null) {
8005 // Execute animation.
8006 w.commitFinishDrawingLocked(currentTime);
8007 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8008 animating = true;
8009 //w.dump(" ");
8010 }
8011
8012 mPolicy.animatingWindowLw(w, attrs);
8013 }
8014
8015 final AppWindowToken atoken = w.mAppToken;
8016 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8017 if (atoken.lastTransactionSequence != transactionSequence) {
8018 atoken.lastTransactionSequence = transactionSequence;
8019 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8020 atoken.startingDisplayed = false;
8021 }
8022 if ((w.isOnScreen() || w.mAttrs.type
8023 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8024 && !w.mExiting && !w.mDestroying) {
8025 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8026 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8027 + w.isDisplayedLw()
8028 + ", isAnimating=" + w.isAnimating());
8029 if (!w.isDisplayedLw()) {
8030 Log.v(TAG, "Not displayed: s=" + w.mSurface
8031 + " pv=" + w.mPolicyVisibility
8032 + " dp=" + w.mDrawPending
8033 + " cdp=" + w.mCommitDrawPending
8034 + " ah=" + w.mAttachedHidden
8035 + " th=" + atoken.hiddenRequested
8036 + " a=" + w.mAnimating);
8037 }
8038 }
8039 if (w != atoken.startingWindow) {
8040 if (!atoken.freezingScreen || !w.mAppFreezing) {
8041 atoken.numInterestingWindows++;
8042 if (w.isDisplayedLw()) {
8043 atoken.numDrawnWindows++;
8044 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8045 "tokenMayBeDrawn: " + atoken
8046 + " freezingScreen=" + atoken.freezingScreen
8047 + " mAppFreezing=" + w.mAppFreezing);
8048 tokenMayBeDrawn = true;
8049 }
8050 }
8051 } else if (w.isDisplayedLw()) {
8052 atoken.startingDisplayed = true;
8053 }
8054 }
8055 } else if (w.mReadyToShow) {
8056 w.performShowLocked();
8057 }
8058 }
8059
8060 if (mPolicy.finishAnimationLw()) {
8061 restart = true;
8062 }
8063
8064 if (tokenMayBeDrawn) {
8065 // See if any windows have been drawn, so they (and others
8066 // associated with them) can now be shown.
8067 final int NT = mTokenList.size();
8068 for (i=0; i<NT; i++) {
8069 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8070 if (wtoken == null) {
8071 continue;
8072 }
8073 if (wtoken.freezingScreen) {
8074 int numInteresting = wtoken.numInterestingWindows;
8075 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8076 if (DEBUG_VISIBILITY) Log.v(TAG,
8077 "allDrawn: " + wtoken
8078 + " interesting=" + numInteresting
8079 + " drawn=" + wtoken.numDrawnWindows);
8080 wtoken.showAllWindowsLocked();
8081 unsetAppFreezingScreenLocked(wtoken, false, true);
8082 orientationChangeComplete = true;
8083 }
8084 } else if (!wtoken.allDrawn) {
8085 int numInteresting = wtoken.numInterestingWindows;
8086 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8087 if (DEBUG_VISIBILITY) Log.v(TAG,
8088 "allDrawn: " + wtoken
8089 + " interesting=" + numInteresting
8090 + " drawn=" + wtoken.numDrawnWindows);
8091 wtoken.allDrawn = true;
8092 restart = true;
8093
8094 // We can now show all of the drawn windows!
8095 if (!mOpeningApps.contains(wtoken)) {
8096 wtoken.showAllWindowsLocked();
8097 }
8098 }
8099 }
8100 }
8101 }
8102
8103 // If we are ready to perform an app transition, check through
8104 // all of the app tokens to be shown and see if they are ready
8105 // to go.
8106 if (mAppTransitionReady) {
8107 int NN = mOpeningApps.size();
8108 boolean goodToGo = true;
8109 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8110 "Checking " + NN + " opening apps (frozen="
8111 + mDisplayFrozen + " timeout="
8112 + mAppTransitionTimeout + ")...");
8113 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8114 // If the display isn't frozen, wait to do anything until
8115 // all of the apps are ready. Otherwise just go because
8116 // we'll unfreeze the display when everyone is ready.
8117 for (i=0; i<NN && goodToGo; i++) {
8118 AppWindowToken wtoken = mOpeningApps.get(i);
8119 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8120 "Check opening app" + wtoken + ": allDrawn="
8121 + wtoken.allDrawn + " startingDisplayed="
8122 + wtoken.startingDisplayed);
8123 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8124 && !wtoken.startingMoved) {
8125 goodToGo = false;
8126 }
8127 }
8128 }
8129 if (goodToGo) {
8130 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8131 int transit = mNextAppTransition;
8132 if (mSkipAppTransitionAnimation) {
8133 transit = WindowManagerPolicy.TRANSIT_NONE;
8134 }
8135 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8136 mAppTransitionReady = false;
8137 mAppTransitionTimeout = false;
8138 mStartingIconInTransition = false;
8139 mSkipAppTransitionAnimation = false;
8140
8141 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8142
8143 // We need to figure out which animation to use...
8144 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8145 mOpeningApps, mClosingApps);
8146
8147 NN = mOpeningApps.size();
8148 for (i=0; i<NN; i++) {
8149 AppWindowToken wtoken = mOpeningApps.get(i);
8150 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8151 "Now opening app" + wtoken);
8152 wtoken.reportedVisible = false;
8153 wtoken.inPendingTransaction = false;
8154 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8155 wtoken.updateReportedVisibilityLocked();
8156 wtoken.showAllWindowsLocked();
8157 }
8158 NN = mClosingApps.size();
8159 for (i=0; i<NN; i++) {
8160 AppWindowToken wtoken = mClosingApps.get(i);
8161 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8162 "Now closing app" + wtoken);
8163 wtoken.inPendingTransaction = false;
8164 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8165 wtoken.updateReportedVisibilityLocked();
8166 // Force the allDrawn flag, because we want to start
8167 // this guy's animations regardless of whether it's
8168 // gotten drawn.
8169 wtoken.allDrawn = true;
8170 }
8171
8172 mOpeningApps.clear();
8173 mClosingApps.clear();
8174
8175 // This has changed the visibility of windows, so perform
8176 // a new layout to get them all up-to-date.
8177 mLayoutNeeded = true;
8178 moveInputMethodWindowsIfNeededLocked(true);
8179 performLayoutLockedInner();
8180 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8181
8182 restart = true;
8183 }
8184 }
8185 } while (restart);
8186
8187 // THIRD LOOP: Update the surfaces of all windows.
8188
8189 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8190
8191 boolean obscured = false;
8192 boolean blurring = false;
8193 boolean dimming = false;
8194 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008195 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008196 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008197
8198 for (i=N-1; i>=0; i--) {
8199 WindowState w = (WindowState)mWindows.get(i);
8200
8201 boolean displayed = false;
8202 final WindowManager.LayoutParams attrs = w.mAttrs;
8203 final int attrFlags = attrs.flags;
8204
8205 if (w.mSurface != null) {
8206 w.computeShownFrameLocked();
8207 if (localLOGV) Log.v(
8208 TAG, "Placing surface #" + i + " " + w.mSurface
8209 + ": new=" + w.mShownFrame + ", old="
8210 + w.mLastShownFrame);
8211
8212 boolean resize;
8213 int width, height;
8214 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8215 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8216 w.mLastRequestedHeight != w.mRequestedHeight;
8217 // for a scaled surface, we just want to use
8218 // the requested size.
8219 width = w.mRequestedWidth;
8220 height = w.mRequestedHeight;
8221 w.mLastRequestedWidth = width;
8222 w.mLastRequestedHeight = height;
8223 w.mLastShownFrame.set(w.mShownFrame);
8224 try {
8225 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8226 } catch (RuntimeException e) {
8227 Log.w(TAG, "Error positioning surface in " + w, e);
8228 if (!recoveringMemory) {
8229 reclaimSomeSurfaceMemoryLocked(w, "position");
8230 }
8231 }
8232 } else {
8233 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8234 width = w.mShownFrame.width();
8235 height = w.mShownFrame.height();
8236 w.mLastShownFrame.set(w.mShownFrame);
8237 if (resize) {
8238 if (SHOW_TRANSACTIONS) Log.i(
8239 TAG, " SURFACE " + w.mSurface + ": ("
8240 + w.mShownFrame.left + ","
8241 + w.mShownFrame.top + ") ("
8242 + w.mShownFrame.width() + "x"
8243 + w.mShownFrame.height() + ")");
8244 }
8245 }
8246
8247 if (resize) {
8248 if (width < 1) width = 1;
8249 if (height < 1) height = 1;
8250 if (w.mSurface != null) {
8251 try {
8252 w.mSurface.setSize(width, height);
8253 w.mSurface.setPosition(w.mShownFrame.left,
8254 w.mShownFrame.top);
8255 } catch (RuntimeException e) {
8256 // If something goes wrong with the surface (such
8257 // as running out of memory), don't take down the
8258 // entire system.
8259 Log.e(TAG, "Failure updating surface of " + w
8260 + "size=(" + width + "x" + height
8261 + "), pos=(" + w.mShownFrame.left
8262 + "," + w.mShownFrame.top + ")", e);
8263 if (!recoveringMemory) {
8264 reclaimSomeSurfaceMemoryLocked(w, "size");
8265 }
8266 }
8267 }
8268 }
8269 if (!w.mAppFreezing) {
8270 w.mContentInsetsChanged =
8271 !w.mLastContentInsets.equals(w.mContentInsets);
8272 w.mVisibleInsetsChanged =
8273 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008274 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 || w.mContentInsetsChanged
8276 || w.mVisibleInsetsChanged) {
8277 w.mLastFrame.set(w.mFrame);
8278 w.mLastContentInsets.set(w.mContentInsets);
8279 w.mLastVisibleInsets.set(w.mVisibleInsets);
8280 // If the orientation is changing, then we need to
8281 // hold off on unfreezing the display until this
8282 // window has been redrawn; to do that, we need
8283 // to go through the process of getting informed
8284 // by the application when it has finished drawing.
8285 if (w.mOrientationChanging) {
8286 if (DEBUG_ORIENTATION) Log.v(TAG,
8287 "Orientation start waiting for draw in "
8288 + w + ", surface " + w.mSurface);
8289 w.mDrawPending = true;
8290 w.mCommitDrawPending = false;
8291 w.mReadyToShow = false;
8292 if (w.mAppToken != null) {
8293 w.mAppToken.allDrawn = false;
8294 }
8295 }
Romain Guy06882f82009-06-10 13:36:04 -07008296 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 "Resizing window " + w + " to " + w.mFrame);
8298 mResizingWindows.add(w);
8299 } else if (w.mOrientationChanging) {
8300 if (!w.mDrawPending && !w.mCommitDrawPending) {
8301 if (DEBUG_ORIENTATION) Log.v(TAG,
8302 "Orientation not waiting for draw in "
8303 + w + ", surface " + w.mSurface);
8304 w.mOrientationChanging = false;
8305 }
8306 }
8307 }
8308
8309 if (w.mAttachedHidden) {
8310 if (!w.mLastHidden) {
8311 //dump();
8312 w.mLastHidden = true;
8313 if (SHOW_TRANSACTIONS) Log.i(
8314 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8315 if (w.mSurface != null) {
8316 try {
8317 w.mSurface.hide();
8318 } catch (RuntimeException e) {
8319 Log.w(TAG, "Exception hiding surface in " + w);
8320 }
8321 }
8322 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8323 }
8324 // If we are waiting for this window to handle an
8325 // orientation change, well, it is hidden, so
8326 // doesn't really matter. Note that this does
8327 // introduce a potential glitch if the window
8328 // becomes unhidden before it has drawn for the
8329 // new orientation.
8330 if (w.mOrientationChanging) {
8331 w.mOrientationChanging = false;
8332 if (DEBUG_ORIENTATION) Log.v(TAG,
8333 "Orientation change skips hidden " + w);
8334 }
8335 } else if (!w.isReadyForDisplay()) {
8336 if (!w.mLastHidden) {
8337 //dump();
8338 w.mLastHidden = true;
8339 if (SHOW_TRANSACTIONS) Log.i(
8340 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8341 if (w.mSurface != null) {
8342 try {
8343 w.mSurface.hide();
8344 } catch (RuntimeException e) {
8345 Log.w(TAG, "Exception exception hiding surface in " + w);
8346 }
8347 }
8348 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8349 }
8350 // If we are waiting for this window to handle an
8351 // orientation change, well, it is hidden, so
8352 // doesn't really matter. Note that this does
8353 // introduce a potential glitch if the window
8354 // becomes unhidden before it has drawn for the
8355 // new orientation.
8356 if (w.mOrientationChanging) {
8357 w.mOrientationChanging = false;
8358 if (DEBUG_ORIENTATION) Log.v(TAG,
8359 "Orientation change skips hidden " + w);
8360 }
8361 } else if (w.mLastLayer != w.mAnimLayer
8362 || w.mLastAlpha != w.mShownAlpha
8363 || w.mLastDsDx != w.mDsDx
8364 || w.mLastDtDx != w.mDtDx
8365 || w.mLastDsDy != w.mDsDy
8366 || w.mLastDtDy != w.mDtDy
8367 || w.mLastHScale != w.mHScale
8368 || w.mLastVScale != w.mVScale
8369 || w.mLastHidden) {
8370 displayed = true;
8371 w.mLastAlpha = w.mShownAlpha;
8372 w.mLastLayer = w.mAnimLayer;
8373 w.mLastDsDx = w.mDsDx;
8374 w.mLastDtDx = w.mDtDx;
8375 w.mLastDsDy = w.mDsDy;
8376 w.mLastDtDy = w.mDtDy;
8377 w.mLastHScale = w.mHScale;
8378 w.mLastVScale = w.mVScale;
8379 if (SHOW_TRANSACTIONS) Log.i(
8380 TAG, " SURFACE " + w.mSurface + ": alpha="
8381 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8382 if (w.mSurface != null) {
8383 try {
8384 w.mSurface.setAlpha(w.mShownAlpha);
8385 w.mSurface.setLayer(w.mAnimLayer);
8386 w.mSurface.setMatrix(
8387 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8388 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8389 } catch (RuntimeException e) {
8390 Log.w(TAG, "Error updating surface in " + w, e);
8391 if (!recoveringMemory) {
8392 reclaimSomeSurfaceMemoryLocked(w, "update");
8393 }
8394 }
8395 }
8396
8397 if (w.mLastHidden && !w.mDrawPending
8398 && !w.mCommitDrawPending
8399 && !w.mReadyToShow) {
8400 if (SHOW_TRANSACTIONS) Log.i(
8401 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8402 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8403 + " during relayout");
8404 if (showSurfaceRobustlyLocked(w)) {
8405 w.mHasDrawn = true;
8406 w.mLastHidden = false;
8407 } else {
8408 w.mOrientationChanging = false;
8409 }
8410 }
8411 if (w.mSurface != null) {
8412 w.mToken.hasVisible = true;
8413 }
8414 } else {
8415 displayed = true;
8416 }
8417
8418 if (displayed) {
8419 if (!covered) {
8420 if (attrs.width == LayoutParams.FILL_PARENT
8421 && attrs.height == LayoutParams.FILL_PARENT) {
8422 covered = true;
8423 }
8424 }
8425 if (w.mOrientationChanging) {
8426 if (w.mDrawPending || w.mCommitDrawPending) {
8427 orientationChangeComplete = false;
8428 if (DEBUG_ORIENTATION) Log.v(TAG,
8429 "Orientation continue waiting for draw in " + w);
8430 } else {
8431 w.mOrientationChanging = false;
8432 if (DEBUG_ORIENTATION) Log.v(TAG,
8433 "Orientation change complete in " + w);
8434 }
8435 }
8436 w.mToken.hasVisible = true;
8437 }
8438 } else if (w.mOrientationChanging) {
8439 if (DEBUG_ORIENTATION) Log.v(TAG,
8440 "Orientation change skips hidden " + w);
8441 w.mOrientationChanging = false;
8442 }
8443
8444 final boolean canBeSeen = w.isDisplayedLw();
8445
8446 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8447 focusDisplayed = true;
8448 }
8449
8450 // Update effect.
8451 if (!obscured) {
8452 if (w.mSurface != null) {
8453 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8454 holdScreen = w.mSession;
8455 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008456 if (!syswin && w.mAttrs.screenBrightness >= 0
8457 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008458 screenBrightness = w.mAttrs.screenBrightness;
8459 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008460 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8461 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8462 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8463 syswin = true;
8464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008465 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008466
8467 boolean opaqueDrawn = w.isOpaqueDrawn();
8468 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008469 // This window completely covers everything behind it,
8470 // so we want to leave all of them as unblurred (for
8471 // performance reasons).
8472 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008473 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8474 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008475 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008476 obscured = true;
8477 if (mBackgroundFillerSurface == null) {
8478 try {
8479 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8480 0, dw, dh,
8481 PixelFormat.OPAQUE,
8482 Surface.FX_SURFACE_NORMAL);
8483 } catch (Exception e) {
8484 Log.e(TAG, "Exception creating filler surface", e);
8485 }
8486 }
8487 try {
8488 mBackgroundFillerSurface.setPosition(0, 0);
8489 mBackgroundFillerSurface.setSize(dw, dh);
8490 // Using the same layer as Dim because they will never be shown at the
8491 // same time.
8492 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8493 mBackgroundFillerSurface.show();
8494 } catch (RuntimeException e) {
8495 Log.e(TAG, "Exception showing filler surface");
8496 }
8497 backgroundFillerShown = true;
8498 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 } else if (canBeSeen && !obscured &&
8500 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8501 if (localLOGV) Log.v(TAG, "Win " + w
8502 + ": blurring=" + blurring
8503 + " obscured=" + obscured
8504 + " displayed=" + displayed);
8505 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8506 if (!dimming) {
8507 //Log.i(TAG, "DIM BEHIND: " + w);
8508 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008509 if (mDimAnimator == null) {
8510 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008511 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008512 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008513 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008514 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008515 }
8516 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8517 if (!blurring) {
8518 //Log.i(TAG, "BLUR BEHIND: " + w);
8519 blurring = true;
8520 mBlurShown = true;
8521 if (mBlurSurface == null) {
8522 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8523 + mBlurSurface + ": CREATE");
8524 try {
Romain Guy06882f82009-06-10 13:36:04 -07008525 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008526 -1, 16, 16,
8527 PixelFormat.OPAQUE,
8528 Surface.FX_SURFACE_BLUR);
8529 } catch (Exception e) {
8530 Log.e(TAG, "Exception creating Blur surface", e);
8531 }
8532 }
8533 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8534 + mBlurSurface + ": SHOW pos=(0,0) (" +
8535 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8536 if (mBlurSurface != null) {
8537 mBlurSurface.setPosition(0, 0);
8538 mBlurSurface.setSize(dw, dh);
8539 try {
8540 mBlurSurface.show();
8541 } catch (RuntimeException e) {
8542 Log.w(TAG, "Failure showing blur surface", e);
8543 }
8544 }
8545 }
8546 mBlurSurface.setLayer(w.mAnimLayer-2);
8547 }
8548 }
8549 }
8550 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008551
8552 if (backgroundFillerShown == false && mBackgroundFillerShown) {
8553 mBackgroundFillerShown = false;
8554 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
8555 try {
8556 mBackgroundFillerSurface.hide();
8557 } catch (RuntimeException e) {
8558 Log.e(TAG, "Exception hiding filler surface", e);
8559 }
8560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008561
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008562 if (mDimAnimator != null && mDimAnimator.mDimShown) {
8563 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008564 }
Romain Guy06882f82009-06-10 13:36:04 -07008565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 if (!blurring && mBlurShown) {
8567 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8568 + ": HIDE");
8569 try {
8570 mBlurSurface.hide();
8571 } catch (IllegalArgumentException e) {
8572 Log.w(TAG, "Illegal argument exception hiding blur surface");
8573 }
8574 mBlurShown = false;
8575 }
8576
8577 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8578 } catch (RuntimeException e) {
8579 Log.e(TAG, "Unhandled exception in Window Manager", e);
8580 }
8581
8582 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008584 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8585 "With display frozen, orientationChangeComplete="
8586 + orientationChangeComplete);
8587 if (orientationChangeComplete) {
8588 if (mWindowsFreezingScreen) {
8589 mWindowsFreezingScreen = false;
8590 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8591 }
8592 if (mAppsFreezingScreen == 0) {
8593 stopFreezingDisplayLocked();
8594 }
8595 }
Romain Guy06882f82009-06-10 13:36:04 -07008596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 i = mResizingWindows.size();
8598 if (i > 0) {
8599 do {
8600 i--;
8601 WindowState win = mResizingWindows.get(i);
8602 try {
8603 win.mClient.resized(win.mFrame.width(),
8604 win.mFrame.height(), win.mLastContentInsets,
8605 win.mLastVisibleInsets, win.mDrawPending);
8606 win.mContentInsetsChanged = false;
8607 win.mVisibleInsetsChanged = false;
8608 } catch (RemoteException e) {
8609 win.mOrientationChanging = false;
8610 }
8611 } while (i > 0);
8612 mResizingWindows.clear();
8613 }
Romain Guy06882f82009-06-10 13:36:04 -07008614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008615 // Destroy the surface of any windows that are no longer visible.
8616 i = mDestroySurface.size();
8617 if (i > 0) {
8618 do {
8619 i--;
8620 WindowState win = mDestroySurface.get(i);
8621 win.mDestroying = false;
8622 if (mInputMethodWindow == win) {
8623 mInputMethodWindow = null;
8624 }
8625 win.destroySurfaceLocked();
8626 } while (i > 0);
8627 mDestroySurface.clear();
8628 }
8629
8630 // Time to remove any exiting tokens?
8631 for (i=mExitingTokens.size()-1; i>=0; i--) {
8632 WindowToken token = mExitingTokens.get(i);
8633 if (!token.hasVisible) {
8634 mExitingTokens.remove(i);
8635 }
8636 }
8637
8638 // Time to remove any exiting applications?
8639 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8640 AppWindowToken token = mExitingAppTokens.get(i);
8641 if (!token.hasVisible && !mClosingApps.contains(token)) {
8642 mAppTokens.remove(token);
8643 mExitingAppTokens.remove(i);
8644 }
8645 }
8646
8647 if (focusDisplayed) {
8648 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8649 }
8650 if (animating) {
8651 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8652 }
8653 mQueue.setHoldScreenLocked(holdScreen != null);
8654 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8655 mPowerManager.setScreenBrightnessOverride(-1);
8656 } else {
8657 mPowerManager.setScreenBrightnessOverride((int)
8658 (screenBrightness * Power.BRIGHTNESS_ON));
8659 }
8660 if (holdScreen != mHoldingScreenOn) {
8661 mHoldingScreenOn = holdScreen;
8662 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8663 mH.sendMessage(m);
8664 }
8665 }
8666
8667 void requestAnimationLocked(long delay) {
8668 if (!mAnimationPending) {
8669 mAnimationPending = true;
8670 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8671 }
8672 }
Romain Guy06882f82009-06-10 13:36:04 -07008673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 /**
8675 * Have the surface flinger show a surface, robustly dealing with
8676 * error conditions. In particular, if there is not enough memory
8677 * to show the surface, then we will try to get rid of other surfaces
8678 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07008679 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008680 * @return Returns true if the surface was successfully shown.
8681 */
8682 boolean showSurfaceRobustlyLocked(WindowState win) {
8683 try {
8684 if (win.mSurface != null) {
8685 win.mSurface.show();
8686 }
8687 return true;
8688 } catch (RuntimeException e) {
8689 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8690 }
Romain Guy06882f82009-06-10 13:36:04 -07008691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008692 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07008693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008694 return false;
8695 }
Romain Guy06882f82009-06-10 13:36:04 -07008696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8698 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07008699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008700 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8701 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07008702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008703 if (mForceRemoves == null) {
8704 mForceRemoves = new ArrayList<WindowState>();
8705 }
Romain Guy06882f82009-06-10 13:36:04 -07008706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707 long callingIdentity = Binder.clearCallingIdentity();
8708 try {
8709 // There was some problem... first, do a sanity check of the
8710 // window list to make sure we haven't left any dangling surfaces
8711 // around.
8712 int N = mWindows.size();
8713 boolean leakedSurface = false;
8714 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8715 for (int i=0; i<N; i++) {
8716 WindowState ws = (WindowState)mWindows.get(i);
8717 if (ws.mSurface != null) {
8718 if (!mSessions.contains(ws.mSession)) {
8719 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8720 + ws + " surface=" + ws.mSurface
8721 + " token=" + win.mToken
8722 + " pid=" + ws.mSession.mPid
8723 + " uid=" + ws.mSession.mUid);
8724 ws.mSurface.clear();
8725 ws.mSurface = null;
8726 mForceRemoves.add(ws);
8727 i--;
8728 N--;
8729 leakedSurface = true;
8730 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
8731 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
8732 + ws + " surface=" + ws.mSurface
8733 + " token=" + win.mAppToken);
8734 ws.mSurface.clear();
8735 ws.mSurface = null;
8736 leakedSurface = true;
8737 }
8738 }
8739 }
Romain Guy06882f82009-06-10 13:36:04 -07008740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008741 boolean killedApps = false;
8742 if (!leakedSurface) {
8743 Log.w(TAG, "No leaked surfaces; killing applicatons!");
8744 SparseIntArray pidCandidates = new SparseIntArray();
8745 for (int i=0; i<N; i++) {
8746 WindowState ws = (WindowState)mWindows.get(i);
8747 if (ws.mSurface != null) {
8748 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
8749 }
8750 }
8751 if (pidCandidates.size() > 0) {
8752 int[] pids = new int[pidCandidates.size()];
8753 for (int i=0; i<pids.length; i++) {
8754 pids[i] = pidCandidates.keyAt(i);
8755 }
8756 try {
8757 if (mActivityManager.killPidsForMemory(pids)) {
8758 killedApps = true;
8759 }
8760 } catch (RemoteException e) {
8761 }
8762 }
8763 }
Romain Guy06882f82009-06-10 13:36:04 -07008764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008765 if (leakedSurface || killedApps) {
8766 // We managed to reclaim some memory, so get rid of the trouble
8767 // surface and ask the app to request another one.
8768 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
8769 if (surface != null) {
8770 surface.clear();
8771 win.mSurface = null;
8772 }
Romain Guy06882f82009-06-10 13:36:04 -07008773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008774 try {
8775 win.mClient.dispatchGetNewSurface();
8776 } catch (RemoteException e) {
8777 }
8778 }
8779 } finally {
8780 Binder.restoreCallingIdentity(callingIdentity);
8781 }
8782 }
Romain Guy06882f82009-06-10 13:36:04 -07008783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008784 private boolean updateFocusedWindowLocked(int mode) {
8785 WindowState newFocus = computeFocusedWindowLocked();
8786 if (mCurrentFocus != newFocus) {
8787 // This check makes sure that we don't already have the focus
8788 // change message pending.
8789 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
8790 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
8791 if (localLOGV) Log.v(
8792 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
8793 final WindowState oldFocus = mCurrentFocus;
8794 mCurrentFocus = newFocus;
8795 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07008796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 final WindowState imWindow = mInputMethodWindow;
8798 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008799 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008800 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008801 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
8802 mLayoutNeeded = true;
8803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008804 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
8805 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008806 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
8807 // Client will do the layout, but we need to assign layers
8808 // for handleNewWindowLocked() below.
8809 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008810 }
8811 }
Romain Guy06882f82009-06-10 13:36:04 -07008812
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008813 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
8814 mKeyWaiter.handleNewWindowLocked(newFocus);
8815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008816 return true;
8817 }
8818 return false;
8819 }
8820
8821 private WindowState computeFocusedWindowLocked() {
8822 WindowState result = null;
8823 WindowState win;
8824
8825 int i = mWindows.size() - 1;
8826 int nextAppIndex = mAppTokens.size()-1;
8827 WindowToken nextApp = nextAppIndex >= 0
8828 ? mAppTokens.get(nextAppIndex) : null;
8829
8830 while (i >= 0) {
8831 win = (WindowState)mWindows.get(i);
8832
8833 if (localLOGV || DEBUG_FOCUS) Log.v(
8834 TAG, "Looking for focus: " + i
8835 + " = " + win
8836 + ", flags=" + win.mAttrs.flags
8837 + ", canReceive=" + win.canReceiveKeys());
8838
8839 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07008840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008841 // If this window's application has been removed, just skip it.
8842 if (thisApp != null && thisApp.removed) {
8843 i--;
8844 continue;
8845 }
Romain Guy06882f82009-06-10 13:36:04 -07008846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008847 // If there is a focused app, don't allow focus to go to any
8848 // windows below it. If this is an application window, step
8849 // through the app tokens until we find its app.
8850 if (thisApp != null && nextApp != null && thisApp != nextApp
8851 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
8852 int origAppIndex = nextAppIndex;
8853 while (nextAppIndex > 0) {
8854 if (nextApp == mFocusedApp) {
8855 // Whoops, we are below the focused app... no focus
8856 // for you!
8857 if (localLOGV || DEBUG_FOCUS) Log.v(
8858 TAG, "Reached focused app: " + mFocusedApp);
8859 return null;
8860 }
8861 nextAppIndex--;
8862 nextApp = mAppTokens.get(nextAppIndex);
8863 if (nextApp == thisApp) {
8864 break;
8865 }
8866 }
8867 if (thisApp != nextApp) {
8868 // Uh oh, the app token doesn't exist! This shouldn't
8869 // happen, but if it does we can get totally hosed...
8870 // so restart at the original app.
8871 nextAppIndex = origAppIndex;
8872 nextApp = mAppTokens.get(nextAppIndex);
8873 }
8874 }
8875
8876 // Dispatch to this window if it is wants key events.
8877 if (win.canReceiveKeys()) {
8878 if (DEBUG_FOCUS) Log.v(
8879 TAG, "Found focus @ " + i + " = " + win);
8880 result = win;
8881 break;
8882 }
8883
8884 i--;
8885 }
8886
8887 return result;
8888 }
8889
8890 private void startFreezingDisplayLocked() {
8891 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07008892 // Freezing the display also suspends key event delivery, to
8893 // keep events from going astray while the display is reconfigured.
8894 // If someone has changed orientation again while the screen is
8895 // still frozen, the events will continue to be blocked while the
8896 // successive orientation change is processed. To prevent spurious
8897 // ANRs, we reset the event dispatch timeout in this case.
8898 synchronized (mKeyWaiter) {
8899 mKeyWaiter.mWasFrozen = true;
8900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008901 return;
8902 }
Romain Guy06882f82009-06-10 13:36:04 -07008903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07008905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008906 long now = SystemClock.uptimeMillis();
8907 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
8908 if (mFreezeGcPending != 0) {
8909 if (now > (mFreezeGcPending+1000)) {
8910 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
8911 mH.removeMessages(H.FORCE_GC);
8912 Runtime.getRuntime().gc();
8913 mFreezeGcPending = now;
8914 }
8915 } else {
8916 mFreezeGcPending = now;
8917 }
Romain Guy06882f82009-06-10 13:36:04 -07008918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008919 mDisplayFrozen = true;
8920 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8921 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8922 mAppTransitionReady = true;
8923 }
Romain Guy06882f82009-06-10 13:36:04 -07008924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008925 if (PROFILE_ORIENTATION) {
8926 File file = new File("/data/system/frozen");
8927 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
8928 }
8929 Surface.freezeDisplay(0);
8930 }
Romain Guy06882f82009-06-10 13:36:04 -07008931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008932 private void stopFreezingDisplayLocked() {
8933 if (!mDisplayFrozen) {
8934 return;
8935 }
Romain Guy06882f82009-06-10 13:36:04 -07008936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937 mDisplayFrozen = false;
8938 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
8939 if (PROFILE_ORIENTATION) {
8940 Debug.stopMethodTracing();
8941 }
8942 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07008943
Chris Tate2ad63a92009-03-25 17:36:48 -07008944 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
8945 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008946 synchronized (mKeyWaiter) {
8947 mKeyWaiter.mWasFrozen = true;
8948 mKeyWaiter.notifyAll();
8949 }
8950
8951 // A little kludge: a lot could have happened while the
8952 // display was frozen, so now that we are coming back we
8953 // do a gc so that any remote references the system
8954 // processes holds on others can be released if they are
8955 // no longer needed.
8956 mH.removeMessages(H.FORCE_GC);
8957 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8958 2000);
Romain Guy06882f82009-06-10 13:36:04 -07008959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008960 mScreenFrozenLock.release();
8961 }
Romain Guy06882f82009-06-10 13:36:04 -07008962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008963 @Override
8964 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
8965 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
8966 != PackageManager.PERMISSION_GRANTED) {
8967 pw.println("Permission Denial: can't dump WindowManager from from pid="
8968 + Binder.getCallingPid()
8969 + ", uid=" + Binder.getCallingUid());
8970 return;
8971 }
Romain Guy06882f82009-06-10 13:36:04 -07008972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008973 synchronized(mWindowMap) {
8974 pw.println("Current Window Manager state:");
8975 for (int i=mWindows.size()-1; i>=0; i--) {
8976 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008977 pw.print(" Window #"); pw.print(i); pw.print(' ');
8978 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008979 w.dump(pw, " ");
8980 }
8981 if (mInputMethodDialogs.size() > 0) {
8982 pw.println(" ");
8983 pw.println(" Input method dialogs:");
8984 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
8985 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008986 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008987 }
8988 }
8989 if (mPendingRemove.size() > 0) {
8990 pw.println(" ");
8991 pw.println(" Remove pending for:");
8992 for (int i=mPendingRemove.size()-1; i>=0; i--) {
8993 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008994 pw.print(" Remove #"); pw.print(i); pw.print(' ');
8995 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008996 w.dump(pw, " ");
8997 }
8998 }
8999 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9000 pw.println(" ");
9001 pw.println(" Windows force removing:");
9002 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9003 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009004 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9005 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009006 w.dump(pw, " ");
9007 }
9008 }
9009 if (mDestroySurface.size() > 0) {
9010 pw.println(" ");
9011 pw.println(" Windows waiting to destroy their surface:");
9012 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9013 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009014 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9015 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009016 w.dump(pw, " ");
9017 }
9018 }
9019 if (mLosingFocus.size() > 0) {
9020 pw.println(" ");
9021 pw.println(" Windows losing focus:");
9022 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9023 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009024 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9025 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009026 w.dump(pw, " ");
9027 }
9028 }
9029 if (mSessions.size() > 0) {
9030 pw.println(" ");
9031 pw.println(" All active sessions:");
9032 Iterator<Session> it = mSessions.iterator();
9033 while (it.hasNext()) {
9034 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009035 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009036 s.dump(pw, " ");
9037 }
9038 }
9039 if (mTokenMap.size() > 0) {
9040 pw.println(" ");
9041 pw.println(" All tokens:");
9042 Iterator<WindowToken> it = mTokenMap.values().iterator();
9043 while (it.hasNext()) {
9044 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009045 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009046 token.dump(pw, " ");
9047 }
9048 }
9049 if (mTokenList.size() > 0) {
9050 pw.println(" ");
9051 pw.println(" Window token list:");
9052 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009053 pw.print(" #"); pw.print(i); pw.print(": ");
9054 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009055 }
9056 }
9057 if (mAppTokens.size() > 0) {
9058 pw.println(" ");
9059 pw.println(" Application tokens in Z order:");
9060 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009061 pw.print(" App #"); pw.print(i); pw.print(": ");
9062 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009063 }
9064 }
9065 if (mFinishedStarting.size() > 0) {
9066 pw.println(" ");
9067 pw.println(" Finishing start of application tokens:");
9068 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9069 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009070 pw.print(" Finished Starting #"); pw.print(i);
9071 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009072 token.dump(pw, " ");
9073 }
9074 }
9075 if (mExitingTokens.size() > 0) {
9076 pw.println(" ");
9077 pw.println(" Exiting tokens:");
9078 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9079 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009080 pw.print(" Exiting #"); pw.print(i);
9081 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082 token.dump(pw, " ");
9083 }
9084 }
9085 if (mExitingAppTokens.size() > 0) {
9086 pw.println(" ");
9087 pw.println(" Exiting application tokens:");
9088 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9089 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009090 pw.print(" Exiting App #"); pw.print(i);
9091 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009092 token.dump(pw, " ");
9093 }
9094 }
9095 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009096 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9097 pw.print(" mLastFocus="); pw.println(mLastFocus);
9098 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9099 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9100 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9101 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9102 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9103 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9104 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9105 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009106 if (mDimAnimator != null) {
9107 mDimAnimator.printTo(pw);
9108 } else {
9109 pw.print( " no DimAnimator ");
9110 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009111 pw.print(" mInputMethodAnimLayerAdjustment=");
9112 pw.println(mInputMethodAnimLayerAdjustment);
9113 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9114 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9115 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9116 pw.print(" mRotation="); pw.print(mRotation);
9117 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9118 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9119 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9120 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9121 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9122 pw.print(" mNextAppTransition=0x");
9123 pw.print(Integer.toHexString(mNextAppTransition));
9124 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9125 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9126 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9127 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9128 if (mOpeningApps.size() > 0) {
9129 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9130 }
9131 if (mClosingApps.size() > 0) {
9132 pw.print(" mClosingApps="); pw.println(mClosingApps);
9133 }
9134 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9135 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009136 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009137 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9138 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9139 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9140 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9141 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9142 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009143 }
9144 }
9145
9146 public void monitor() {
9147 synchronized (mWindowMap) { }
9148 synchronized (mKeyguardDisabled) { }
9149 synchronized (mKeyWaiter) { }
9150 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009151
9152 /**
9153 * DimAnimator class that controls the dim animation. This holds the surface and
9154 * all state used for dim animation.
9155 */
9156 private static class DimAnimator {
9157 Surface mDimSurface;
9158 boolean mDimShown = false;
9159 float mDimCurrentAlpha;
9160 float mDimTargetAlpha;
9161 float mDimDeltaPerMs;
9162 long mLastDimAnimTime;
9163
9164 DimAnimator (SurfaceSession session) {
9165 if (mDimSurface == null) {
9166 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9167 + mDimSurface + ": CREATE");
9168 try {
9169 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9170 Surface.FX_SURFACE_DIM);
9171 } catch (Exception e) {
9172 Log.e(TAG, "Exception creating Dim surface", e);
9173 }
9174 }
9175 }
9176
9177 /**
9178 * Show the dim surface.
9179 */
9180 void show(int dw, int dh) {
9181 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9182 dw + "x" + dh + ")");
9183 mDimShown = true;
9184 try {
9185 mDimSurface.setPosition(0, 0);
9186 mDimSurface.setSize(dw, dh);
9187 mDimSurface.show();
9188 } catch (RuntimeException e) {
9189 Log.w(TAG, "Failure showing dim surface", e);
9190 }
9191 }
9192
9193 /**
9194 * Set's the dim surface's layer and update dim parameters that will be used in
9195 * {@link updateSurface} after all windows are examined.
9196 */
9197 void updateParameters(WindowState w, long currentTime) {
9198 mDimSurface.setLayer(w.mAnimLayer-1);
9199
9200 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9201 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9202 if (mDimTargetAlpha != target) {
9203 // If the desired dim level has changed, then
9204 // start an animation to it.
9205 mLastDimAnimTime = currentTime;
9206 long duration = (w.mAnimating && w.mAnimation != null)
9207 ? w.mAnimation.computeDurationHint()
9208 : DEFAULT_DIM_DURATION;
9209 if (target > mDimTargetAlpha) {
9210 // This is happening behind the activity UI,
9211 // so we can make it run a little longer to
9212 // give a stronger impression without disrupting
9213 // the user.
9214 duration *= DIM_DURATION_MULTIPLIER;
9215 }
9216 if (duration < 1) {
9217 // Don't divide by zero
9218 duration = 1;
9219 }
9220 mDimTargetAlpha = target;
9221 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9222 }
9223 }
9224
9225 /**
9226 * Updating the surface's alpha. Returns true if the animation continues, or returns
9227 * false when the animation is finished and the dim surface is hidden.
9228 */
9229 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9230 if (!dimming) {
9231 if (mDimTargetAlpha != 0) {
9232 mLastDimAnimTime = currentTime;
9233 mDimTargetAlpha = 0;
9234 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9235 }
9236 }
9237
9238 boolean animating = false;
9239 if (mLastDimAnimTime != 0) {
9240 mDimCurrentAlpha += mDimDeltaPerMs
9241 * (currentTime-mLastDimAnimTime);
9242 boolean more = true;
9243 if (displayFrozen) {
9244 // If the display is frozen, there is no reason to animate.
9245 more = false;
9246 } else if (mDimDeltaPerMs > 0) {
9247 if (mDimCurrentAlpha > mDimTargetAlpha) {
9248 more = false;
9249 }
9250 } else if (mDimDeltaPerMs < 0) {
9251 if (mDimCurrentAlpha < mDimTargetAlpha) {
9252 more = false;
9253 }
9254 } else {
9255 more = false;
9256 }
9257
9258 // Do we need to continue animating?
9259 if (more) {
9260 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9261 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9262 mLastDimAnimTime = currentTime;
9263 mDimSurface.setAlpha(mDimCurrentAlpha);
9264 animating = true;
9265 } else {
9266 mDimCurrentAlpha = mDimTargetAlpha;
9267 mLastDimAnimTime = 0;
9268 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9269 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9270 mDimSurface.setAlpha(mDimCurrentAlpha);
9271 if (!dimming) {
9272 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9273 + ": HIDE");
9274 try {
9275 mDimSurface.hide();
9276 } catch (RuntimeException e) {
9277 Log.w(TAG, "Illegal argument exception hiding dim surface");
9278 }
9279 mDimShown = false;
9280 }
9281 }
9282 }
9283 return animating;
9284 }
9285
9286 public void printTo(PrintWriter pw) {
9287 pw.print(" mDimShown="); pw.print(mDimShown);
9288 pw.print(" current="); pw.print(mDimCurrentAlpha);
9289 pw.print(" target="); pw.print(mDimTargetAlpha);
9290 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9291 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9292 }
9293 }
9294
9295 /**
9296 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9297 * This is used for opening/closing transition for apps in compatible mode.
9298 */
9299 private static class FadeInOutAnimation extends Animation {
9300 int mWidth;
9301 boolean mFadeIn;
9302
9303 public FadeInOutAnimation(boolean fadeIn) {
9304 setInterpolator(new AccelerateInterpolator());
9305 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9306 mFadeIn = fadeIn;
9307 }
9308
9309 @Override
9310 protected void applyTransformation(float interpolatedTime, Transformation t) {
9311 float x = interpolatedTime;
9312 if (!mFadeIn) {
9313 x = 1.0f - x; // reverse the interpolation for fade out
9314 }
9315 if (x < 0.5) {
9316 // move the window out of the screen.
9317 t.getMatrix().setTranslate(mWidth, 0);
9318 } else {
9319 t.getMatrix().setTranslate(0, 0);// show
9320 t.setAlpha((x - 0.5f) * 2);
9321 }
9322 }
9323
9324 @Override
9325 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9326 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9327 mWidth = width;
9328 }
9329
9330 @Override
9331 public boolean willChangeTransformationMatrix() {
9332 return true;
9333 }
9334
9335 @Override
9336 public boolean willChangeBounds() {
9337 return true;
9338 }
9339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340}
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009341