blob: 25aff5c60603d2468748d8d2ab5c210be54f377d [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;
27import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
28import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
29import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
30import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
31import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
32import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
33import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
34import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_GPU;
35import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_HARDWARE;
36import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
37import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
38import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
39import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
40import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
41
42import com.android.internal.app.IBatteryStats;
43import com.android.internal.policy.PolicyManager;
44import com.android.internal.view.IInputContext;
45import com.android.internal.view.IInputMethodClient;
46import com.android.internal.view.IInputMethodManager;
47import com.android.server.KeyInputQueue.QueuedEvent;
48import com.android.server.am.BatteryStatsService;
49
50import android.Manifest;
51import android.app.ActivityManagerNative;
52import android.app.IActivityManager;
53import android.content.Context;
54import android.content.pm.ActivityInfo;
55import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070056import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.res.Configuration;
58import android.graphics.Matrix;
59import android.graphics.PixelFormat;
60import android.graphics.Rect;
61import android.graphics.Region;
62import android.os.BatteryStats;
63import android.os.Binder;
64import android.os.Debug;
65import android.os.Handler;
66import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070067import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.LocalPowerManager;
69import android.os.Looper;
70import android.os.Message;
71import android.os.Parcel;
72import android.os.ParcelFileDescriptor;
73import android.os.Power;
74import android.os.PowerManager;
75import android.os.Process;
76import android.os.RemoteException;
77import android.os.ServiceManager;
78import android.os.SystemClock;
79import android.os.SystemProperties;
80import android.os.TokenWatcher;
81import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070082import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.util.EventLog;
84import android.util.Log;
85import android.util.SparseIntArray;
86import android.view.Display;
87import android.view.Gravity;
88import android.view.IApplicationToken;
89import android.view.IOnKeyguardExitResult;
90import android.view.IRotationWatcher;
91import android.view.IWindow;
92import android.view.IWindowManager;
93import android.view.IWindowSession;
94import android.view.KeyEvent;
95import android.view.MotionEvent;
96import android.view.RawInputEvent;
97import android.view.Surface;
98import android.view.SurfaceSession;
99import android.view.View;
100import android.view.ViewTreeObserver;
101import android.view.WindowManager;
102import android.view.WindowManagerImpl;
103import android.view.WindowManagerPolicy;
104import android.view.WindowManager.LayoutParams;
105import android.view.animation.Animation;
106import android.view.animation.AnimationUtils;
107import android.view.animation.Transformation;
108
109import java.io.BufferedWriter;
110import java.io.File;
111import java.io.FileDescriptor;
112import java.io.IOException;
113import java.io.OutputStream;
114import java.io.OutputStreamWriter;
115import java.io.PrintWriter;
116import java.io.StringWriter;
117import java.net.Socket;
118import java.util.ArrayList;
119import java.util.HashMap;
120import java.util.HashSet;
121import java.util.Iterator;
122import java.util.List;
123
124/** {@hide} */
125public class WindowManagerService extends IWindowManager.Stub implements Watchdog.Monitor {
126 static final String TAG = "WindowManager";
127 static final boolean DEBUG = false;
128 static final boolean DEBUG_FOCUS = false;
129 static final boolean DEBUG_ANIM = false;
130 static final boolean DEBUG_LAYERS = false;
131 static final boolean DEBUG_INPUT = false;
132 static final boolean DEBUG_INPUT_METHOD = false;
133 static final boolean DEBUG_VISIBILITY = false;
134 static final boolean DEBUG_ORIENTATION = false;
135 static final boolean DEBUG_APP_TRANSITIONS = false;
136 static final boolean DEBUG_STARTING_WINDOW = false;
137 static final boolean DEBUG_REORDER = false;
138 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700139 static final boolean MEASURE_LATENCY = false;
140 static private LatencyTimer lt;
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 static final boolean PROFILE_ORIENTATION = false;
143 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700144 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 /** How long to wait for first key repeat, in milliseconds */
149 static final int KEY_REPEAT_FIRST_DELAY = 750;
Romain Guy06882f82009-06-10 13:36:04 -0700150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /** How long to wait for subsequent key repeats, in milliseconds */
152 static final int KEY_REPEAT_DELAY = 50;
153
154 /** How much to multiply the policy's type layer, to reserve room
155 * for multiple windows of the same type and Z-ordering adjustment
156 * with TYPE_LAYER_OFFSET. */
157 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
160 * or below others in the same layer. */
161 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 /** How much to increment the layer for each window, to reserve room
164 * for effect surfaces between them.
165 */
166 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /** The maximum length we will accept for a loaded animation duration:
169 * this is 10 seconds.
170 */
171 static final int MAX_ANIMATION_DURATION = 10*1000;
172
173 /** Amount of time (in milliseconds) to animate the dim surface from one
174 * value to another, when no window animation is driving it.
175 */
176 static final int DEFAULT_DIM_DURATION = 200;
177
178 /** Adjustment to time to perform a dim, to make it more dramatic.
179 */
180 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700181
Dianne Hackborncfaef692009-06-15 14:24:44 -0700182 static final int INJECT_FAILED = 0;
183 static final int INJECT_SUCCEEDED = 1;
184 static final int INJECT_NO_PERMISSION = -1;
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 static final int UPDATE_FOCUS_NORMAL = 0;
187 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
188 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
189 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700190
Michael Chane96440f2009-05-06 10:27:36 -0700191 /** The minimum time between dispatching touch events. */
192 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
193
194 // Last touch event time
195 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700196
Michael Chane96440f2009-05-06 10:27:36 -0700197 // Last touch event type
198 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700199
Michael Chane96440f2009-05-06 10:27:36 -0700200 // Time to wait before calling useractivity again. This saves CPU usage
201 // when we get a flood of touch events.
202 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
203
204 // Last time we call user activity
205 long mLastUserActivityCallTime = 0;
206
Romain Guy06882f82009-06-10 13:36:04 -0700207 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700208 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700211 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
213 /**
214 * Condition waited on by {@link #reenableKeyguard} to know the call to
215 * the window policy has finished.
216 */
217 private boolean mWaitingUntilKeyguardReenabled = false;
218
219
220 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
221 new Handler(), "WindowManagerService.mKeyguardDisabled") {
222 public void acquired() {
223 mPolicy.enableKeyguard(false);
224 }
225 public void released() {
226 synchronized (mKeyguardDisabled) {
227 mPolicy.enableKeyguard(true);
228 mWaitingUntilKeyguardReenabled = false;
229 mKeyguardDisabled.notifyAll();
230 }
231 }
232 };
233
234 final Context mContext;
235
236 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
241
242 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 /**
247 * All currently active sessions with clients.
248 */
249 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 /**
252 * Mapping from an IWindow IBinder to the server's Window object.
253 * This is also used as the lock for all of our state.
254 */
255 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
256
257 /**
258 * Mapping from a token IBinder to a WindowToken object.
259 */
260 final HashMap<IBinder, WindowToken> mTokenMap =
261 new HashMap<IBinder, WindowToken>();
262
263 /**
264 * The same tokens as mTokenMap, stored in a list for efficient iteration
265 * over them.
266 */
267 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 /**
270 * Window tokens that are in the process of exiting, but still
271 * on screen for animations.
272 */
273 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
274
275 /**
276 * Z-ordered (bottom-most first) list of all application tokens, for
277 * controlling the ordering of windows in different applications. This
278 * contains WindowToken objects.
279 */
280 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
281
282 /**
283 * Application tokens that are in the process of exiting, but still
284 * on screen for animations.
285 */
286 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
287
288 /**
289 * List of window tokens that have finished starting their application,
290 * and now need to have the policy remove their windows.
291 */
292 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
293
294 /**
295 * Z-ordered (bottom-most first) list of all Window objects.
296 */
297 final ArrayList mWindows = new ArrayList();
298
299 /**
300 * Windows that are being resized. Used so we can tell the client about
301 * the resize after closing the transaction in which we resized the
302 * underlying surface.
303 */
304 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
305
306 /**
307 * Windows whose animations have ended and now must be removed.
308 */
309 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
310
311 /**
312 * Windows whose surface should be destroyed.
313 */
314 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
315
316 /**
317 * Windows that have lost input focus and are waiting for the new
318 * focus window to be displayed before they are told about this.
319 */
320 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
321
322 /**
323 * This is set when we have run out of memory, and will either be an empty
324 * list or contain windows that need to be force removed.
325 */
326 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 SurfaceSession mFxSession;
331 Surface mDimSurface;
332 boolean mDimShown;
333 float mDimCurrentAlpha;
334 float mDimTargetAlpha;
335 float mDimDeltaPerMs;
336 long mLastDimAnimTime;
337 Surface mBlurSurface;
338 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 final float[] mTmpFloats = new float[9];
343
344 boolean mSafeMode;
345 boolean mDisplayEnabled = false;
346 boolean mSystemBooted = false;
347 int mRotation = 0;
348 int mRequestedRotation = 0;
349 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700350 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 ArrayList<IRotationWatcher> mRotationWatchers
352 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 boolean mLayoutNeeded = true;
355 boolean mAnimationPending = false;
356 boolean mDisplayFrozen = false;
357 boolean mWindowsFreezingScreen = false;
358 long mFreezeGcPending = 0;
359 int mAppsFreezingScreen = 0;
360
361 // This is held as long as we have the screen frozen, to give us time to
362 // perform a rotation animation when turning off shows the lock screen which
363 // changes the orientation.
364 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 // State management of app transitions. When we are preparing for a
367 // transition, mNextAppTransition will be the kind of transition to
368 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
369 // mOpeningApps and mClosingApps are the lists of tokens that will be
370 // made visible or hidden at the next transition.
371 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
372 boolean mAppTransitionReady = false;
373 boolean mAppTransitionTimeout = false;
374 boolean mStartingIconInTransition = false;
375 boolean mSkipAppTransitionAnimation = false;
376 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
377 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 //flag to detect fat touch events
380 boolean mFatTouch = false;
381 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 H mH = new H();
384
385 WindowState mCurrentFocus = null;
386 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 // This just indicates the window the input method is on top of, not
389 // necessarily the window its input is going to.
390 WindowState mInputMethodTarget = null;
391 WindowState mUpcomingInputMethodTarget = null;
392 boolean mInputMethodTargetWaitingAnim;
393 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 WindowState mInputMethodWindow = null;
396 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
397
398 AppWindowToken mFocusedApp = null;
399
400 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 float mWindowAnimationScale = 1.0f;
403 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 final KeyWaiter mKeyWaiter = new KeyWaiter();
406 final KeyQ mQueue;
407 final InputDispatcherThread mInputThread;
408
409 // Who is holding the screen on.
410 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 /**
413 * Whether the UI is currently running in touch mode (not showing
414 * navigational focus because the user is directly pressing the screen).
415 */
416 boolean mInTouchMode = false;
417
418 private ViewServer mViewServer;
419
420 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700421
Dianne Hackbornc485a602009-03-24 22:39:49 -0700422 final Configuration mTempConfiguration = new Configuration();
Dianne Hackborn723738c2009-06-25 19:48:04 -0700423 int screenLayout = Configuration.SCREENLAYOUT_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700424
425 // The frame use to limit the size of the app running in compatibility mode.
426 Rect mCompatibleScreenFrame = new Rect();
427 // The surface used to fill the outer rim of the app running in compatibility mode.
428 Surface mBackgroundFillerSurface = null;
429 boolean mBackgroundFillerShown = false;
430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 public static WindowManagerService main(Context context,
432 PowerManagerService pm, boolean haveInputMethods) {
433 WMThread thr = new WMThread(context, pm, haveInputMethods);
434 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 synchronized (thr) {
437 while (thr.mService == null) {
438 try {
439 thr.wait();
440 } catch (InterruptedException e) {
441 }
442 }
443 }
Romain Guy06882f82009-06-10 13:36:04 -0700444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 return thr.mService;
446 }
Romain Guy06882f82009-06-10 13:36:04 -0700447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 static class WMThread extends Thread {
449 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 private final Context mContext;
452 private final PowerManagerService mPM;
453 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 public WMThread(Context context, PowerManagerService pm,
456 boolean haveInputMethods) {
457 super("WindowManager");
458 mContext = context;
459 mPM = pm;
460 mHaveInputMethods = haveInputMethods;
461 }
Romain Guy06882f82009-06-10 13:36:04 -0700462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 public void run() {
464 Looper.prepare();
465 WindowManagerService s = new WindowManagerService(mContext, mPM,
466 mHaveInputMethods);
467 android.os.Process.setThreadPriority(
468 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 synchronized (this) {
471 mService = s;
472 notifyAll();
473 }
Romain Guy06882f82009-06-10 13:36:04 -0700474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 Looper.loop();
476 }
477 }
478
479 static class PolicyThread extends Thread {
480 private final WindowManagerPolicy mPolicy;
481 private final WindowManagerService mService;
482 private final Context mContext;
483 private final PowerManagerService mPM;
484 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 public PolicyThread(WindowManagerPolicy policy,
487 WindowManagerService service, Context context,
488 PowerManagerService pm) {
489 super("WindowManagerPolicy");
490 mPolicy = policy;
491 mService = service;
492 mContext = context;
493 mPM = pm;
494 }
Romain Guy06882f82009-06-10 13:36:04 -0700495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 public void run() {
497 Looper.prepare();
498 //Looper.myLooper().setMessageLogging(new LogPrinter(
499 // Log.VERBOSE, "WindowManagerPolicy"));
500 android.os.Process.setThreadPriority(
501 android.os.Process.THREAD_PRIORITY_FOREGROUND);
502 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 synchronized (this) {
505 mRunning = true;
506 notifyAll();
507 }
Romain Guy06882f82009-06-10 13:36:04 -0700508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 Looper.loop();
510 }
511 }
512
513 private WindowManagerService(Context context, PowerManagerService pm,
514 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700515 if (MEASURE_LATENCY) {
516 lt = new LatencyTimer(100, 1000);
517 }
518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 mContext = context;
520 mHaveInputMethods = haveInputMethods;
521 mLimitedAlphaCompositing = context.getResources().getBoolean(
522 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 mPowerManager = pm;
525 mPowerManager.setPolicy(mPolicy);
526 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
527 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
528 "SCREEN_FROZEN");
529 mScreenFrozenLock.setReferenceCounted(false);
530
531 mActivityManager = ActivityManagerNative.getDefault();
532 mBatteryStats = BatteryStatsService.getService();
533
534 // Get persisted window scale setting
535 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
536 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
537 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
538 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 mQueue = new KeyQ();
541
542 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
545 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 synchronized (thr) {
548 while (!thr.mRunning) {
549 try {
550 thr.wait();
551 } catch (InterruptedException e) {
552 }
553 }
554 }
Romain Guy06882f82009-06-10 13:36:04 -0700555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 // Add ourself to the Watchdog monitors.
559 Watchdog.getInstance().addMonitor(this);
560 }
561
562 @Override
563 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
564 throws RemoteException {
565 try {
566 return super.onTransact(code, data, reply, flags);
567 } catch (RuntimeException e) {
568 // The window manager only throws security exceptions, so let's
569 // log all others.
570 if (!(e instanceof SecurityException)) {
571 Log.e(TAG, "Window Manager Crash", e);
572 }
573 throw e;
574 }
575 }
576
577 private void placeWindowAfter(Object pos, WindowState window) {
578 final int i = mWindows.indexOf(pos);
579 if (localLOGV || DEBUG_FOCUS) Log.v(
580 TAG, "Adding window " + window + " at "
581 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
582 mWindows.add(i+1, window);
583 }
584
585 private void placeWindowBefore(Object pos, WindowState window) {
586 final int i = mWindows.indexOf(pos);
587 if (localLOGV || DEBUG_FOCUS) Log.v(
588 TAG, "Adding window " + window + " at "
589 + i + " of " + mWindows.size() + " (before " + pos + ")");
590 mWindows.add(i, window);
591 }
592
593 //This method finds out the index of a window that has the same app token as
594 //win. used for z ordering the windows in mWindows
595 private int findIdxBasedOnAppTokens(WindowState win) {
596 //use a local variable to cache mWindows
597 ArrayList localmWindows = mWindows;
598 int jmax = localmWindows.size();
599 if(jmax == 0) {
600 return -1;
601 }
602 for(int j = (jmax-1); j >= 0; j--) {
603 WindowState wentry = (WindowState)localmWindows.get(j);
604 if(wentry.mAppToken == win.mAppToken) {
605 return j;
606 }
607 }
608 return -1;
609 }
Romain Guy06882f82009-06-10 13:36:04 -0700610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
612 final IWindow client = win.mClient;
613 final WindowToken token = win.mToken;
614 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 final int N = localmWindows.size();
617 final WindowState attached = win.mAttachedWindow;
618 int i;
619 if (attached == null) {
620 int tokenWindowsPos = token.windows.size();
621 if (token.appWindowToken != null) {
622 int index = tokenWindowsPos-1;
623 if (index >= 0) {
624 // If this application has existing windows, we
625 // simply place the new window on top of them... but
626 // keep the starting window on top.
627 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
628 // Base windows go behind everything else.
629 placeWindowBefore(token.windows.get(0), win);
630 tokenWindowsPos = 0;
631 } else {
632 AppWindowToken atoken = win.mAppToken;
633 if (atoken != null &&
634 token.windows.get(index) == atoken.startingWindow) {
635 placeWindowBefore(token.windows.get(index), win);
636 tokenWindowsPos--;
637 } else {
638 int newIdx = findIdxBasedOnAppTokens(win);
639 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700640 //there is a window above this one associated with the same
641 //apptoken note that the window could be a floating window
642 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 //windows associated with this token.
644 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 }
647 }
648 } else {
649 if (localLOGV) Log.v(
650 TAG, "Figuring out where to add app window "
651 + client.asBinder() + " (token=" + token + ")");
652 // Figure out where the window should go, based on the
653 // order of applications.
654 final int NA = mAppTokens.size();
655 Object pos = null;
656 for (i=NA-1; i>=0; i--) {
657 AppWindowToken t = mAppTokens.get(i);
658 if (t == token) {
659 i--;
660 break;
661 }
662 if (t.windows.size() > 0) {
663 pos = t.windows.get(0);
664 }
665 }
666 // We now know the index into the apps. If we found
667 // an app window above, that gives us the position; else
668 // we need to look some more.
669 if (pos != null) {
670 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700671 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 mTokenMap.get(((WindowState)pos).mClient.asBinder());
673 if (atoken != null) {
674 final int NC = atoken.windows.size();
675 if (NC > 0) {
676 WindowState bottom = atoken.windows.get(0);
677 if (bottom.mSubLayer < 0) {
678 pos = bottom;
679 }
680 }
681 }
682 placeWindowBefore(pos, win);
683 } else {
684 while (i >= 0) {
685 AppWindowToken t = mAppTokens.get(i);
686 final int NW = t.windows.size();
687 if (NW > 0) {
688 pos = t.windows.get(NW-1);
689 break;
690 }
691 i--;
692 }
693 if (pos != null) {
694 // Move in front of any windows attached to this
695 // one.
696 WindowToken atoken =
697 mTokenMap.get(((WindowState)pos).mClient.asBinder());
698 if (atoken != null) {
699 final int NC = atoken.windows.size();
700 if (NC > 0) {
701 WindowState top = atoken.windows.get(NC-1);
702 if (top.mSubLayer >= 0) {
703 pos = top;
704 }
705 }
706 }
707 placeWindowAfter(pos, win);
708 } else {
709 // Just search for the start of this layer.
710 final int myLayer = win.mBaseLayer;
711 for (i=0; i<N; i++) {
712 WindowState w = (WindowState)localmWindows.get(i);
713 if (w.mBaseLayer > myLayer) {
714 break;
715 }
716 }
717 if (localLOGV || DEBUG_FOCUS) Log.v(
718 TAG, "Adding window " + win + " at "
719 + i + " of " + N);
720 localmWindows.add(i, win);
721 }
722 }
723 }
724 } else {
725 // Figure out where window should go, based on layer.
726 final int myLayer = win.mBaseLayer;
727 for (i=N-1; i>=0; i--) {
728 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
729 i++;
730 break;
731 }
732 }
733 if (i < 0) i = 0;
734 if (localLOGV || DEBUG_FOCUS) Log.v(
735 TAG, "Adding window " + win + " at "
736 + i + " of " + N);
737 localmWindows.add(i, win);
738 }
739 if (addToToken) {
740 token.windows.add(tokenWindowsPos, win);
741 }
742
743 } else {
744 // Figure out this window's ordering relative to the window
745 // it is attached to.
746 final int NA = token.windows.size();
747 final int sublayer = win.mSubLayer;
748 int largestSublayer = Integer.MIN_VALUE;
749 WindowState windowWithLargestSublayer = null;
750 for (i=0; i<NA; i++) {
751 WindowState w = token.windows.get(i);
752 final int wSublayer = w.mSubLayer;
753 if (wSublayer >= largestSublayer) {
754 largestSublayer = wSublayer;
755 windowWithLargestSublayer = w;
756 }
757 if (sublayer < 0) {
758 // For negative sublayers, we go below all windows
759 // in the same sublayer.
760 if (wSublayer >= sublayer) {
761 if (addToToken) {
762 token.windows.add(i, win);
763 }
764 placeWindowBefore(
765 wSublayer >= 0 ? attached : w, win);
766 break;
767 }
768 } else {
769 // For positive sublayers, we go above all windows
770 // in the same sublayer.
771 if (wSublayer > sublayer) {
772 if (addToToken) {
773 token.windows.add(i, win);
774 }
775 placeWindowBefore(w, win);
776 break;
777 }
778 }
779 }
780 if (i >= NA) {
781 if (addToToken) {
782 token.windows.add(win);
783 }
784 if (sublayer < 0) {
785 placeWindowBefore(attached, win);
786 } else {
787 placeWindowAfter(largestSublayer >= 0
788 ? windowWithLargestSublayer
789 : attached,
790 win);
791 }
792 }
793 }
Romain Guy06882f82009-06-10 13:36:04 -0700794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 if (win.mAppToken != null && addToToken) {
796 win.mAppToken.allAppWindows.add(win);
797 }
798 }
Romain Guy06882f82009-06-10 13:36:04 -0700799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 static boolean canBeImeTarget(WindowState w) {
801 final int fl = w.mAttrs.flags
802 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
803 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
804 return w.isVisibleOrAdding();
805 }
806 return false;
807 }
Romain Guy06882f82009-06-10 13:36:04 -0700808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
810 final ArrayList localmWindows = mWindows;
811 final int N = localmWindows.size();
812 WindowState w = null;
813 int i = N;
814 while (i > 0) {
815 i--;
816 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
819 // + Integer.toHexString(w.mAttrs.flags));
820 if (canBeImeTarget(w)) {
821 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 // Yet more tricksyness! If this window is a "starting"
824 // window, we do actually want to be on top of it, but
825 // it is not -really- where input will go. So if the caller
826 // is not actually looking to move the IME, look down below
827 // for a real window to target...
828 if (!willMove
829 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
830 && i > 0) {
831 WindowState wb = (WindowState)localmWindows.get(i-1);
832 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
833 i--;
834 w = wb;
835 }
836 }
837 break;
838 }
839 }
Romain Guy06882f82009-06-10 13:36:04 -0700840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
844 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 if (willMove && w != null) {
847 final WindowState curTarget = mInputMethodTarget;
848 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 // Now some fun for dealing with window animations that
851 // modify the Z order. We need to look at all windows below
852 // the current target that are in this app, finding the highest
853 // visible one in layering.
854 AppWindowToken token = curTarget.mAppToken;
855 WindowState highestTarget = null;
856 int highestPos = 0;
857 if (token.animating || token.animation != null) {
858 int pos = 0;
859 pos = localmWindows.indexOf(curTarget);
860 while (pos >= 0) {
861 WindowState win = (WindowState)localmWindows.get(pos);
862 if (win.mAppToken != token) {
863 break;
864 }
865 if (!win.mRemoved) {
866 if (highestTarget == null || win.mAnimLayer >
867 highestTarget.mAnimLayer) {
868 highestTarget = win;
869 highestPos = pos;
870 }
871 }
872 pos--;
873 }
874 }
Romain Guy06882f82009-06-10 13:36:04 -0700875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700877 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 + mNextAppTransition + " " + highestTarget
879 + " animating=" + highestTarget.isAnimating()
880 + " layer=" + highestTarget.mAnimLayer
881 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
884 // If we are currently setting up for an animation,
885 // hold everything until we can find out what will happen.
886 mInputMethodTargetWaitingAnim = true;
887 mInputMethodTarget = highestTarget;
888 return highestPos + 1;
889 } else if (highestTarget.isAnimating() &&
890 highestTarget.mAnimLayer > w.mAnimLayer) {
891 // If the window we are currently targeting is involved
892 // with an animation, and it is on top of the next target
893 // we will be over, then hold off on moving until
894 // that is done.
895 mInputMethodTarget = highestTarget;
896 return highestPos + 1;
897 }
898 }
899 }
900 }
Romain Guy06882f82009-06-10 13:36:04 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 //Log.i(TAG, "Placing input method @" + (i+1));
903 if (w != null) {
904 if (willMove) {
905 RuntimeException e = new RuntimeException();
906 e.fillInStackTrace();
907 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
908 + mInputMethodTarget + " to " + w, e);
909 mInputMethodTarget = w;
910 if (w.mAppToken != null) {
911 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
912 } else {
913 setInputMethodAnimLayerAdjustment(0);
914 }
915 }
916 return i+1;
917 }
918 if (willMove) {
919 RuntimeException e = new RuntimeException();
920 e.fillInStackTrace();
921 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
922 + mInputMethodTarget + " to null", e);
923 mInputMethodTarget = null;
924 setInputMethodAnimLayerAdjustment(0);
925 }
926 return -1;
927 }
Romain Guy06882f82009-06-10 13:36:04 -0700928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 void addInputMethodWindowToListLocked(WindowState win) {
930 int pos = findDesiredInputMethodWindowIndexLocked(true);
931 if (pos >= 0) {
932 win.mTargetAppToken = mInputMethodTarget.mAppToken;
933 mWindows.add(pos, win);
934 moveInputMethodDialogsLocked(pos+1);
935 return;
936 }
937 win.mTargetAppToken = null;
938 addWindowToListInOrderLocked(win, true);
939 moveInputMethodDialogsLocked(pos);
940 }
Romain Guy06882f82009-06-10 13:36:04 -0700941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 void setInputMethodAnimLayerAdjustment(int adj) {
943 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
944 mInputMethodAnimLayerAdjustment = adj;
945 WindowState imw = mInputMethodWindow;
946 if (imw != null) {
947 imw.mAnimLayer = imw.mLayer + adj;
948 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
949 + " anim layer: " + imw.mAnimLayer);
950 int wi = imw.mChildWindows.size();
951 while (wi > 0) {
952 wi--;
953 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
954 cw.mAnimLayer = cw.mLayer + adj;
955 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
956 + " anim layer: " + cw.mAnimLayer);
957 }
958 }
959 int di = mInputMethodDialogs.size();
960 while (di > 0) {
961 di --;
962 imw = mInputMethodDialogs.get(di);
963 imw.mAnimLayer = imw.mLayer + adj;
964 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
965 + " anim layer: " + imw.mAnimLayer);
966 }
967 }
Romain Guy06882f82009-06-10 13:36:04 -0700968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
970 int wpos = mWindows.indexOf(win);
971 if (wpos >= 0) {
972 if (wpos < interestingPos) interestingPos--;
973 mWindows.remove(wpos);
974 int NC = win.mChildWindows.size();
975 while (NC > 0) {
976 NC--;
977 WindowState cw = (WindowState)win.mChildWindows.get(NC);
978 int cpos = mWindows.indexOf(cw);
979 if (cpos >= 0) {
980 if (cpos < interestingPos) interestingPos--;
981 mWindows.remove(cpos);
982 }
983 }
984 }
985 return interestingPos;
986 }
Romain Guy06882f82009-06-10 13:36:04 -0700987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 private void reAddWindowToListInOrderLocked(WindowState win) {
989 addWindowToListInOrderLocked(win, false);
990 // This is a hack to get all of the child windows added as well
991 // at the right position. Child windows should be rare and
992 // this case should be rare, so it shouldn't be that big a deal.
993 int wpos = mWindows.indexOf(win);
994 if (wpos >= 0) {
995 mWindows.remove(wpos);
996 reAddWindowLocked(wpos, win);
997 }
998 }
Romain Guy06882f82009-06-10 13:36:04 -0700999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 void logWindowList(String prefix) {
1001 int N = mWindows.size();
1002 while (N > 0) {
1003 N--;
1004 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1005 }
1006 }
Romain Guy06882f82009-06-10 13:36:04 -07001007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 void moveInputMethodDialogsLocked(int pos) {
1009 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 final int N = dialogs.size();
1012 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1013 for (int i=0; i<N; i++) {
1014 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1015 }
1016 if (DEBUG_INPUT_METHOD) {
1017 Log.v(TAG, "Window list w/pos=" + pos);
1018 logWindowList(" ");
1019 }
Romain Guy06882f82009-06-10 13:36:04 -07001020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 if (pos >= 0) {
1022 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1023 if (pos < mWindows.size()) {
1024 WindowState wp = (WindowState)mWindows.get(pos);
1025 if (wp == mInputMethodWindow) {
1026 pos++;
1027 }
1028 }
1029 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1030 for (int i=0; i<N; i++) {
1031 WindowState win = dialogs.get(i);
1032 win.mTargetAppToken = targetAppToken;
1033 pos = reAddWindowLocked(pos, win);
1034 }
1035 if (DEBUG_INPUT_METHOD) {
1036 Log.v(TAG, "Final window list:");
1037 logWindowList(" ");
1038 }
1039 return;
1040 }
1041 for (int i=0; i<N; i++) {
1042 WindowState win = dialogs.get(i);
1043 win.mTargetAppToken = null;
1044 reAddWindowToListInOrderLocked(win);
1045 if (DEBUG_INPUT_METHOD) {
1046 Log.v(TAG, "No IM target, final list:");
1047 logWindowList(" ");
1048 }
1049 }
1050 }
Romain Guy06882f82009-06-10 13:36:04 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1053 final WindowState imWin = mInputMethodWindow;
1054 final int DN = mInputMethodDialogs.size();
1055 if (imWin == null && DN == 0) {
1056 return false;
1057 }
Romain Guy06882f82009-06-10 13:36:04 -07001058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1060 if (imPos >= 0) {
1061 // In this case, the input method windows are to be placed
1062 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 // First check to see if the input method windows are already
1065 // located here, and contiguous.
1066 final int N = mWindows.size();
1067 WindowState firstImWin = imPos < N
1068 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 // Figure out the actual input method window that should be
1071 // at the bottom of their stack.
1072 WindowState baseImWin = imWin != null
1073 ? imWin : mInputMethodDialogs.get(0);
1074 if (baseImWin.mChildWindows.size() > 0) {
1075 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1076 if (cw.mSubLayer < 0) baseImWin = cw;
1077 }
Romain Guy06882f82009-06-10 13:36:04 -07001078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 if (firstImWin == baseImWin) {
1080 // The windows haven't moved... but are they still contiguous?
1081 // First find the top IM window.
1082 int pos = imPos+1;
1083 while (pos < N) {
1084 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1085 break;
1086 }
1087 pos++;
1088 }
1089 pos++;
1090 // Now there should be no more input method windows above.
1091 while (pos < N) {
1092 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1093 break;
1094 }
1095 pos++;
1096 }
1097 if (pos >= N) {
1098 // All is good!
1099 return false;
1100 }
1101 }
Romain Guy06882f82009-06-10 13:36:04 -07001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 if (imWin != null) {
1104 if (DEBUG_INPUT_METHOD) {
1105 Log.v(TAG, "Moving IM from " + imPos);
1106 logWindowList(" ");
1107 }
1108 imPos = tmpRemoveWindowLocked(imPos, imWin);
1109 if (DEBUG_INPUT_METHOD) {
1110 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1111 logWindowList(" ");
1112 }
1113 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1114 reAddWindowLocked(imPos, imWin);
1115 if (DEBUG_INPUT_METHOD) {
1116 Log.v(TAG, "List after moving IM to " + imPos + ":");
1117 logWindowList(" ");
1118 }
1119 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1120 } else {
1121 moveInputMethodDialogsLocked(imPos);
1122 }
Romain Guy06882f82009-06-10 13:36:04 -07001123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 } else {
1125 // In this case, the input method windows go in a fixed layer,
1126 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 if (imWin != null) {
1129 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1130 tmpRemoveWindowLocked(0, imWin);
1131 imWin.mTargetAppToken = null;
1132 reAddWindowToListInOrderLocked(imWin);
1133 if (DEBUG_INPUT_METHOD) {
1134 Log.v(TAG, "List with no IM target:");
1135 logWindowList(" ");
1136 }
1137 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1138 } else {
1139 moveInputMethodDialogsLocked(-1);;
1140 }
Romain Guy06882f82009-06-10 13:36:04 -07001141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 }
Romain Guy06882f82009-06-10 13:36:04 -07001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 if (needAssignLayers) {
1145 assignLayersLocked();
1146 }
Romain Guy06882f82009-06-10 13:36:04 -07001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 return true;
1149 }
Romain Guy06882f82009-06-10 13:36:04 -07001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 void adjustInputMethodDialogsLocked() {
1152 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1153 }
Romain Guy06882f82009-06-10 13:36:04 -07001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 public int addWindow(Session session, IWindow client,
1156 WindowManager.LayoutParams attrs, int viewVisibility,
1157 Rect outContentInsets) {
1158 int res = mPolicy.checkAddPermission(attrs);
1159 if (res != WindowManagerImpl.ADD_OKAY) {
1160 return res;
1161 }
Romain Guy06882f82009-06-10 13:36:04 -07001162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 boolean reportNewConfig = false;
1164 WindowState attachedWindow = null;
1165 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 synchronized(mWindowMap) {
1168 // Instantiating a Display requires talking with the simulator,
1169 // so don't do it until we know the system is mostly up and
1170 // running.
1171 if (mDisplay == null) {
1172 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1173 mDisplay = wm.getDefaultDisplay();
1174 mQueue.setDisplay(mDisplay);
1175 reportNewConfig = true;
1176 }
Romain Guy06882f82009-06-10 13:36:04 -07001177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 if (mWindowMap.containsKey(client.asBinder())) {
1179 Log.w(TAG, "Window " + client + " is already added");
1180 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1181 }
1182
1183 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001184 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 if (attachedWindow == null) {
1186 Log.w(TAG, "Attempted to add window with token that is not a window: "
1187 + attrs.token + ". Aborting.");
1188 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1189 }
1190 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1191 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1192 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1193 + attrs.token + ". Aborting.");
1194 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1195 }
1196 }
1197
1198 boolean addToken = false;
1199 WindowToken token = mTokenMap.get(attrs.token);
1200 if (token == null) {
1201 if (attrs.type >= FIRST_APPLICATION_WINDOW
1202 && attrs.type <= LAST_APPLICATION_WINDOW) {
1203 Log.w(TAG, "Attempted to add application window with unknown token "
1204 + attrs.token + ". Aborting.");
1205 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1206 }
1207 if (attrs.type == TYPE_INPUT_METHOD) {
1208 Log.w(TAG, "Attempted to add input method window with unknown token "
1209 + attrs.token + ". Aborting.");
1210 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1211 }
1212 token = new WindowToken(attrs.token, -1, false);
1213 addToken = true;
1214 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1215 && attrs.type <= LAST_APPLICATION_WINDOW) {
1216 AppWindowToken atoken = token.appWindowToken;
1217 if (atoken == null) {
1218 Log.w(TAG, "Attempted to add window with non-application token "
1219 + token + ". Aborting.");
1220 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1221 } else if (atoken.removed) {
1222 Log.w(TAG, "Attempted to add window with exiting application token "
1223 + token + ". Aborting.");
1224 return WindowManagerImpl.ADD_APP_EXITING;
1225 }
1226 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1227 // No need for this guy!
1228 if (localLOGV) Log.v(
1229 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1230 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1231 }
1232 } else if (attrs.type == TYPE_INPUT_METHOD) {
1233 if (token.windowType != TYPE_INPUT_METHOD) {
1234 Log.w(TAG, "Attempted to add input method window with bad token "
1235 + attrs.token + ". Aborting.");
1236 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1237 }
1238 }
1239
1240 win = new WindowState(session, client, token,
1241 attachedWindow, attrs, viewVisibility);
1242 if (win.mDeathRecipient == null) {
1243 // Client has apparently died, so there is no reason to
1244 // continue.
1245 Log.w(TAG, "Adding window client " + client.asBinder()
1246 + " that is dead, aborting.");
1247 return WindowManagerImpl.ADD_APP_EXITING;
1248 }
1249
1250 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 res = mPolicy.prepareAddWindowLw(win, attrs);
1253 if (res != WindowManagerImpl.ADD_OKAY) {
1254 return res;
1255 }
1256
1257 // From now on, no exceptions or errors allowed!
1258
1259 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 if (addToken) {
1264 mTokenMap.put(attrs.token, token);
1265 mTokenList.add(token);
1266 }
1267 win.attach();
1268 mWindowMap.put(client.asBinder(), win);
1269
1270 if (attrs.type == TYPE_APPLICATION_STARTING &&
1271 token.appWindowToken != null) {
1272 token.appWindowToken.startingWindow = win;
1273 }
1274
1275 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 if (attrs.type == TYPE_INPUT_METHOD) {
1278 mInputMethodWindow = win;
1279 addInputMethodWindowToListLocked(win);
1280 imMayMove = false;
1281 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1282 mInputMethodDialogs.add(win);
1283 addWindowToListInOrderLocked(win, true);
1284 adjustInputMethodDialogsLocked();
1285 imMayMove = false;
1286 } else {
1287 addWindowToListInOrderLocked(win, true);
1288 }
Romain Guy06882f82009-06-10 13:36:04 -07001289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 if (mInTouchMode) {
1295 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1296 }
1297 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1298 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1299 }
Romain Guy06882f82009-06-10 13:36:04 -07001300
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001301 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001303 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1304 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 imMayMove = false;
1306 }
1307 }
Romain Guy06882f82009-06-10 13:36:04 -07001308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001310 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
Romain Guy06882f82009-06-10 13:36:04 -07001312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 assignLayersLocked();
1314 // Don't do layout here, the window must call
1315 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 //dump();
1318
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001319 if (focusChanged) {
1320 if (mCurrentFocus != null) {
1321 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1322 }
1323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 if (localLOGV) Log.v(
1325 TAG, "New client " + client.asBinder()
1326 + ": window=" + win);
1327 }
1328
1329 // sendNewConfiguration() checks caller permissions so we must call it with
1330 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1331 // identity anyway, so it's safe to just clear & restore around this whole
1332 // block.
1333 final long origId = Binder.clearCallingIdentity();
1334 if (reportNewConfig) {
1335 sendNewConfiguration();
1336 } else {
1337 // Update Orientation after adding a window, only if the window needs to be
1338 // displayed right away
1339 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001340 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 sendNewConfiguration();
1342 }
1343 }
1344 }
1345 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 return res;
1348 }
Romain Guy06882f82009-06-10 13:36:04 -07001349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 public void removeWindow(Session session, IWindow client) {
1351 synchronized(mWindowMap) {
1352 WindowState win = windowForClientLocked(session, client);
1353 if (win == null) {
1354 return;
1355 }
1356 removeWindowLocked(session, win);
1357 }
1358 }
Romain Guy06882f82009-06-10 13:36:04 -07001359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 public void removeWindowLocked(Session session, WindowState win) {
1361
1362 if (localLOGV || DEBUG_FOCUS) Log.v(
1363 TAG, "Remove " + win + " client="
1364 + Integer.toHexString(System.identityHashCode(
1365 win.mClient.asBinder()))
1366 + ", surface=" + win.mSurface);
1367
1368 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 if (DEBUG_APP_TRANSITIONS) Log.v(
1371 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1372 + " mExiting=" + win.mExiting
1373 + " isAnimating=" + win.isAnimating()
1374 + " app-animation="
1375 + (win.mAppToken != null ? win.mAppToken.animation : null)
1376 + " inPendingTransaction="
1377 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1378 + " mDisplayFrozen=" + mDisplayFrozen);
1379 // Visibility of the removed window. Will be used later to update orientation later on.
1380 boolean wasVisible = false;
1381 // First, see if we need to run an animation. If we do, we have
1382 // to hold off on removing the window until the animation is done.
1383 // If the display is frozen, just remove immediately, since the
1384 // animation wouldn't be seen.
1385 if (win.mSurface != null && !mDisplayFrozen) {
1386 // If we are not currently running the exit animation, we
1387 // need to see about starting one.
1388 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1391 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1392 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1393 }
1394 // Try starting an animation.
1395 if (applyAnimationLocked(win, transit, false)) {
1396 win.mExiting = true;
1397 }
1398 }
1399 if (win.mExiting || win.isAnimating()) {
1400 // The exit animation is running... wait for it!
1401 //Log.i(TAG, "*** Running exit animation...");
1402 win.mExiting = true;
1403 win.mRemoveOnExit = true;
1404 mLayoutNeeded = true;
1405 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1406 performLayoutAndPlaceSurfacesLocked();
1407 if (win.mAppToken != null) {
1408 win.mAppToken.updateReportedVisibilityLocked();
1409 }
1410 //dump();
1411 Binder.restoreCallingIdentity(origId);
1412 return;
1413 }
1414 }
1415
1416 removeWindowInnerLocked(session, win);
1417 // Removing a visible window will effect the computed orientation
1418 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001419 if (wasVisible && computeForcedAppOrientationLocked()
1420 != mForcedAppOrientation) {
1421 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
1423 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1424 Binder.restoreCallingIdentity(origId);
1425 }
Romain Guy06882f82009-06-10 13:36:04 -07001426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 private void removeWindowInnerLocked(Session session, WindowState win) {
1428 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1429 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 if (mInputMethodTarget == win) {
1434 moveInputMethodWindowsIfNeededLocked(false);
1435 }
Romain Guy06882f82009-06-10 13:36:04 -07001436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 mPolicy.removeWindowLw(win);
1438 win.removeLocked();
1439
1440 mWindowMap.remove(win.mClient.asBinder());
1441 mWindows.remove(win);
1442
1443 if (mInputMethodWindow == win) {
1444 mInputMethodWindow = null;
1445 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1446 mInputMethodDialogs.remove(win);
1447 }
Romain Guy06882f82009-06-10 13:36:04 -07001448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 final WindowToken token = win.mToken;
1450 final AppWindowToken atoken = win.mAppToken;
1451 token.windows.remove(win);
1452 if (atoken != null) {
1453 atoken.allAppWindows.remove(win);
1454 }
1455 if (localLOGV) Log.v(
1456 TAG, "**** Removing window " + win + ": count="
1457 + token.windows.size());
1458 if (token.windows.size() == 0) {
1459 if (!token.explicit) {
1460 mTokenMap.remove(token.token);
1461 mTokenList.remove(token);
1462 } else if (atoken != null) {
1463 atoken.firstWindowDrawn = false;
1464 }
1465 }
1466
1467 if (atoken != null) {
1468 if (atoken.startingWindow == win) {
1469 atoken.startingWindow = null;
1470 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1471 // If this is the last window and we had requested a starting
1472 // transition window, well there is no point now.
1473 atoken.startingData = null;
1474 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1475 // If this is the last window except for a starting transition
1476 // window, we need to get rid of the starting transition.
1477 if (DEBUG_STARTING_WINDOW) {
1478 Log.v(TAG, "Schedule remove starting " + token
1479 + ": no more real windows");
1480 }
1481 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1482 mH.sendMessage(m);
1483 }
1484 }
Romain Guy06882f82009-06-10 13:36:04 -07001485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 if (!mInLayout) {
1487 assignLayersLocked();
1488 mLayoutNeeded = true;
1489 performLayoutAndPlaceSurfacesLocked();
1490 if (win.mAppToken != null) {
1491 win.mAppToken.updateReportedVisibilityLocked();
1492 }
1493 }
1494 }
1495
1496 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1497 long origId = Binder.clearCallingIdentity();
1498 try {
1499 synchronized (mWindowMap) {
1500 WindowState w = windowForClientLocked(session, client);
1501 if ((w != null) && (w.mSurface != null)) {
1502 Surface.openTransaction();
1503 try {
1504 w.mSurface.setTransparentRegionHint(region);
1505 } finally {
1506 Surface.closeTransaction();
1507 }
1508 }
1509 }
1510 } finally {
1511 Binder.restoreCallingIdentity(origId);
1512 }
1513 }
1514
1515 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001516 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 Rect visibleInsets) {
1518 long origId = Binder.clearCallingIdentity();
1519 try {
1520 synchronized (mWindowMap) {
1521 WindowState w = windowForClientLocked(session, client);
1522 if (w != null) {
1523 w.mGivenInsetsPending = false;
1524 w.mGivenContentInsets.set(contentInsets);
1525 w.mGivenVisibleInsets.set(visibleInsets);
1526 w.mTouchableInsets = touchableInsets;
1527 mLayoutNeeded = true;
1528 performLayoutAndPlaceSurfacesLocked();
1529 }
1530 }
1531 } finally {
1532 Binder.restoreCallingIdentity(origId);
1533 }
1534 }
Romain Guy06882f82009-06-10 13:36:04 -07001535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 public void getWindowDisplayFrame(Session session, IWindow client,
1537 Rect outDisplayFrame) {
1538 synchronized(mWindowMap) {
1539 WindowState win = windowForClientLocked(session, client);
1540 if (win == null) {
1541 outDisplayFrame.setEmpty();
1542 return;
1543 }
1544 outDisplayFrame.set(win.mDisplayFrame);
1545 }
1546 }
1547
1548 public int relayoutWindow(Session session, IWindow client,
1549 WindowManager.LayoutParams attrs, int requestedWidth,
1550 int requestedHeight, int viewVisibility, boolean insetsPending,
1551 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1552 Surface outSurface) {
1553 boolean displayed = false;
1554 boolean inTouchMode;
1555 Configuration newConfig = null;
1556 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 synchronized(mWindowMap) {
1559 WindowState win = windowForClientLocked(session, client);
1560 if (win == null) {
1561 return 0;
1562 }
1563 win.mRequestedWidth = requestedWidth;
1564 win.mRequestedHeight = requestedHeight;
1565
1566 if (attrs != null) {
1567 mPolicy.adjustWindowParamsLw(attrs);
1568 }
Romain Guy06882f82009-06-10 13:36:04 -07001569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 int attrChanges = 0;
1571 int flagChanges = 0;
1572 if (attrs != null) {
1573 flagChanges = win.mAttrs.flags ^= attrs.flags;
1574 attrChanges = win.mAttrs.copyFrom(attrs);
1575 }
1576
1577 if (localLOGV) Log.v(
1578 TAG, "Relayout given client " + client.asBinder()
1579 + " (" + win.mAttrs.getTitle() + ")");
1580
1581
1582 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1583 win.mAlpha = attrs.alpha;
1584 }
1585
1586 final boolean scaledWindow =
1587 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1588
1589 if (scaledWindow) {
1590 // requested{Width|Height} Surface's physical size
1591 // attrs.{width|height} Size on screen
1592 win.mHScale = (attrs.width != requestedWidth) ?
1593 (attrs.width / (float)requestedWidth) : 1.0f;
1594 win.mVScale = (attrs.height != requestedHeight) ?
1595 (attrs.height / (float)requestedHeight) : 1.0f;
1596 }
1597
1598 boolean imMayMove = (flagChanges&(
1599 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1600 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 boolean focusMayChange = win.mViewVisibility != viewVisibility
1603 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1604 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 win.mRelayoutCalled = true;
1607 final int oldVisibility = win.mViewVisibility;
1608 win.mViewVisibility = viewVisibility;
1609 if (viewVisibility == View.VISIBLE &&
1610 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1611 displayed = !win.isVisibleLw();
1612 if (win.mExiting) {
1613 win.mExiting = false;
1614 win.mAnimation = null;
1615 }
1616 if (win.mDestroying) {
1617 win.mDestroying = false;
1618 mDestroySurface.remove(win);
1619 }
1620 if (oldVisibility == View.GONE) {
1621 win.mEnterAnimationPending = true;
1622 }
1623 if (displayed && win.mSurface != null && !win.mDrawPending
1624 && !win.mCommitDrawPending && !mDisplayFrozen) {
1625 applyEnterAnimationLocked(win);
1626 }
1627 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1628 // To change the format, we need to re-build the surface.
1629 win.destroySurfaceLocked();
1630 displayed = true;
1631 }
1632 try {
1633 Surface surface = win.createSurfaceLocked();
1634 if (surface != null) {
1635 outSurface.copyFrom(surface);
1636 } else {
1637 outSurface.clear();
1638 }
1639 } catch (Exception e) {
1640 Log.w(TAG, "Exception thrown when creating surface for client "
1641 + client + " (" + win.mAttrs.getTitle() + ")",
1642 e);
1643 Binder.restoreCallingIdentity(origId);
1644 return 0;
1645 }
1646 if (displayed) {
1647 focusMayChange = true;
1648 }
1649 if (win.mAttrs.type == TYPE_INPUT_METHOD
1650 && mInputMethodWindow == null) {
1651 mInputMethodWindow = win;
1652 imMayMove = true;
1653 }
1654 } else {
1655 win.mEnterAnimationPending = false;
1656 if (win.mSurface != null) {
1657 // If we are not currently running the exit animation, we
1658 // need to see about starting one.
1659 if (!win.mExiting) {
1660 // Try starting an animation; if there isn't one, we
1661 // can destroy the surface right away.
1662 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1663 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1664 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1665 }
1666 if (win.isWinVisibleLw() &&
1667 applyAnimationLocked(win, transit, false)) {
1668 win.mExiting = true;
1669 mKeyWaiter.finishedKey(session, client, true,
1670 KeyWaiter.RETURN_NOTHING);
1671 } else if (win.isAnimating()) {
1672 // Currently in a hide animation... turn this into
1673 // an exit.
1674 win.mExiting = true;
1675 } else {
1676 if (mInputMethodWindow == win) {
1677 mInputMethodWindow = null;
1678 }
1679 win.destroySurfaceLocked();
1680 }
1681 }
1682 }
1683 outSurface.clear();
1684 }
1685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 if (focusMayChange) {
1687 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1688 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 imMayMove = false;
1690 }
1691 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1692 }
Romain Guy06882f82009-06-10 13:36:04 -07001693
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001694 // updateFocusedWindowLocked() already assigned layers so we only need to
1695 // reassign them at this point if the IM window state gets shuffled
1696 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 if (imMayMove) {
1699 if (moveInputMethodWindowsIfNeededLocked(false)) {
1700 assignLayers = true;
1701 }
1702 }
Romain Guy06882f82009-06-10 13:36:04 -07001703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 mLayoutNeeded = true;
1705 win.mGivenInsetsPending = insetsPending;
1706 if (assignLayers) {
1707 assignLayersLocked();
1708 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001709 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 performLayoutAndPlaceSurfacesLocked();
1711 if (win.mAppToken != null) {
1712 win.mAppToken.updateReportedVisibilityLocked();
1713 }
1714 outFrame.set(win.mFrame);
1715 outContentInsets.set(win.mContentInsets);
1716 outVisibleInsets.set(win.mVisibleInsets);
1717 if (localLOGV) Log.v(
1718 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001719 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 + ", requestedHeight=" + requestedHeight
1721 + ", viewVisibility=" + viewVisibility
1722 + "\nRelayout returning frame=" + outFrame
1723 + ", surface=" + outSurface);
1724
1725 if (localLOGV || DEBUG_FOCUS) Log.v(
1726 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1727
1728 inTouchMode = mInTouchMode;
1729 }
1730
1731 if (newConfig != null) {
1732 sendNewConfiguration();
1733 }
Romain Guy06882f82009-06-10 13:36:04 -07001734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1738 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1739 }
1740
1741 public void finishDrawingWindow(Session session, IWindow client) {
1742 final long origId = Binder.clearCallingIdentity();
1743 synchronized(mWindowMap) {
1744 WindowState win = windowForClientLocked(session, client);
1745 if (win != null && win.finishDrawingLocked()) {
1746 mLayoutNeeded = true;
1747 performLayoutAndPlaceSurfacesLocked();
1748 }
1749 }
1750 Binder.restoreCallingIdentity(origId);
1751 }
1752
1753 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1754 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1755 + (lp != null ? lp.packageName : null)
1756 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1757 if (lp != null && lp.windowAnimations != 0) {
1758 // If this is a system resource, don't try to load it from the
1759 // application resources. It is nice to avoid loading application
1760 // resources if we can.
1761 String packageName = lp.packageName != null ? lp.packageName : "android";
1762 int resId = lp.windowAnimations;
1763 if ((resId&0xFF000000) == 0x01000000) {
1764 packageName = "android";
1765 }
1766 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
1767 + packageName);
1768 return AttributeCache.instance().get(packageName, resId,
1769 com.android.internal.R.styleable.WindowAnimation);
1770 }
1771 return null;
1772 }
Romain Guy06882f82009-06-10 13:36:04 -07001773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 private void applyEnterAnimationLocked(WindowState win) {
1775 int transit = WindowManagerPolicy.TRANSIT_SHOW;
1776 if (win.mEnterAnimationPending) {
1777 win.mEnterAnimationPending = false;
1778 transit = WindowManagerPolicy.TRANSIT_ENTER;
1779 }
1780
1781 applyAnimationLocked(win, transit, true);
1782 }
1783
1784 private boolean applyAnimationLocked(WindowState win,
1785 int transit, boolean isEntrance) {
1786 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
1787 // If we are trying to apply an animation, but already running
1788 // an animation of the same type, then just leave that one alone.
1789 return true;
1790 }
Romain Guy06882f82009-06-10 13:36:04 -07001791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 // Only apply an animation if the display isn't frozen. If it is
1793 // frozen, there is no reason to animate and it can cause strange
1794 // artifacts when we unfreeze the display if some different animation
1795 // is running.
1796 if (!mDisplayFrozen) {
1797 int anim = mPolicy.selectAnimationLw(win, transit);
1798 int attr = -1;
1799 Animation a = null;
1800 if (anim != 0) {
1801 a = AnimationUtils.loadAnimation(mContext, anim);
1802 } else {
1803 switch (transit) {
1804 case WindowManagerPolicy.TRANSIT_ENTER:
1805 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1806 break;
1807 case WindowManagerPolicy.TRANSIT_EXIT:
1808 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1809 break;
1810 case WindowManagerPolicy.TRANSIT_SHOW:
1811 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1812 break;
1813 case WindowManagerPolicy.TRANSIT_HIDE:
1814 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1815 break;
1816 }
1817 if (attr >= 0) {
1818 a = loadAnimation(win.mAttrs, attr);
1819 }
1820 }
1821 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
1822 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1823 + " mAnimation=" + win.mAnimation
1824 + " isEntrance=" + isEntrance);
1825 if (a != null) {
1826 if (DEBUG_ANIM) {
1827 RuntimeException e = new RuntimeException();
1828 e.fillInStackTrace();
1829 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
1830 }
1831 win.setAnimation(a);
1832 win.mAnimationIsEntrance = isEntrance;
1833 }
1834 } else {
1835 win.clearAnimation();
1836 }
1837
1838 return win.mAnimation != null;
1839 }
1840
1841 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
1842 int anim = 0;
1843 Context context = mContext;
1844 if (animAttr >= 0) {
1845 AttributeCache.Entry ent = getCachedAnimations(lp);
1846 if (ent != null) {
1847 context = ent.context;
1848 anim = ent.array.getResourceId(animAttr, 0);
1849 }
1850 }
1851 if (anim != 0) {
1852 return AnimationUtils.loadAnimation(context, anim);
1853 }
1854 return null;
1855 }
Romain Guy06882f82009-06-10 13:36:04 -07001856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 private boolean applyAnimationLocked(AppWindowToken wtoken,
1858 WindowManager.LayoutParams lp, int transit, boolean enter) {
1859 // Only apply an animation if the display isn't frozen. If it is
1860 // frozen, there is no reason to animate and it can cause strange
1861 // artifacts when we unfreeze the display if some different animation
1862 // is running.
1863 if (!mDisplayFrozen) {
1864 int animAttr = 0;
1865 switch (transit) {
1866 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
1867 animAttr = enter
1868 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
1869 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
1870 break;
1871 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
1872 animAttr = enter
1873 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
1874 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
1875 break;
1876 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
1877 animAttr = enter
1878 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
1879 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
1880 break;
1881 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
1882 animAttr = enter
1883 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
1884 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
1885 break;
1886 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
1887 animAttr = enter
1888 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
1889 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
1890 break;
1891 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
1892 animAttr = enter
1893 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
1894 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
1895 break;
1896 }
1897 Animation a = loadAnimation(lp, animAttr);
1898 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
1899 + " anim=" + a
1900 + " animAttr=0x" + Integer.toHexString(animAttr)
1901 + " transit=" + transit);
1902 if (a != null) {
1903 if (DEBUG_ANIM) {
1904 RuntimeException e = new RuntimeException();
1905 e.fillInStackTrace();
1906 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
1907 }
1908 wtoken.setAnimation(a);
1909 }
1910 } else {
1911 wtoken.clearAnimation();
1912 }
1913
1914 return wtoken.animation != null;
1915 }
1916
1917 // -------------------------------------------------------------
1918 // Application Window Tokens
1919 // -------------------------------------------------------------
1920
1921 public void validateAppTokens(List tokens) {
1922 int v = tokens.size()-1;
1923 int m = mAppTokens.size()-1;
1924 while (v >= 0 && m >= 0) {
1925 AppWindowToken wtoken = mAppTokens.get(m);
1926 if (wtoken.removed) {
1927 m--;
1928 continue;
1929 }
1930 if (tokens.get(v) != wtoken.token) {
1931 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
1932 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
1933 }
1934 v--;
1935 m--;
1936 }
1937 while (v >= 0) {
1938 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
1939 v--;
1940 }
1941 while (m >= 0) {
1942 AppWindowToken wtoken = mAppTokens.get(m);
1943 if (!wtoken.removed) {
1944 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
1945 }
1946 m--;
1947 }
1948 }
1949
1950 boolean checkCallingPermission(String permission, String func) {
1951 // Quick check: if the calling permission is me, it's all okay.
1952 if (Binder.getCallingPid() == Process.myPid()) {
1953 return true;
1954 }
Romain Guy06882f82009-06-10 13:36:04 -07001955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 if (mContext.checkCallingPermission(permission)
1957 == PackageManager.PERMISSION_GRANTED) {
1958 return true;
1959 }
1960 String msg = "Permission Denial: " + func + " from pid="
1961 + Binder.getCallingPid()
1962 + ", uid=" + Binder.getCallingUid()
1963 + " requires " + permission;
1964 Log.w(TAG, msg);
1965 return false;
1966 }
Romain Guy06882f82009-06-10 13:36:04 -07001967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 AppWindowToken findAppWindowToken(IBinder token) {
1969 WindowToken wtoken = mTokenMap.get(token);
1970 if (wtoken == null) {
1971 return null;
1972 }
1973 return wtoken.appWindowToken;
1974 }
Romain Guy06882f82009-06-10 13:36:04 -07001975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 public void addWindowToken(IBinder token, int type) {
1977 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1978 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001979 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 }
Romain Guy06882f82009-06-10 13:36:04 -07001981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 synchronized(mWindowMap) {
1983 WindowToken wtoken = mTokenMap.get(token);
1984 if (wtoken != null) {
1985 Log.w(TAG, "Attempted to add existing input method token: " + token);
1986 return;
1987 }
1988 wtoken = new WindowToken(token, type, true);
1989 mTokenMap.put(token, wtoken);
1990 mTokenList.add(wtoken);
1991 }
1992 }
Romain Guy06882f82009-06-10 13:36:04 -07001993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 public void removeWindowToken(IBinder token) {
1995 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1996 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001997 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 }
1999
2000 final long origId = Binder.clearCallingIdentity();
2001 synchronized(mWindowMap) {
2002 WindowToken wtoken = mTokenMap.remove(token);
2003 mTokenList.remove(wtoken);
2004 if (wtoken != null) {
2005 boolean delayed = false;
2006 if (!wtoken.hidden) {
2007 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 final int N = wtoken.windows.size();
2010 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 for (int i=0; i<N; i++) {
2013 WindowState win = wtoken.windows.get(i);
2014
2015 if (win.isAnimating()) {
2016 delayed = true;
2017 }
Romain Guy06882f82009-06-10 13:36:04 -07002018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 if (win.isVisibleNow()) {
2020 applyAnimationLocked(win,
2021 WindowManagerPolicy.TRANSIT_EXIT, false);
2022 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2023 KeyWaiter.RETURN_NOTHING);
2024 changed = true;
2025 }
2026 }
2027
2028 if (changed) {
2029 mLayoutNeeded = true;
2030 performLayoutAndPlaceSurfacesLocked();
2031 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2032 }
Romain Guy06882f82009-06-10 13:36:04 -07002033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 if (delayed) {
2035 mExitingTokens.add(wtoken);
2036 }
2037 }
Romain Guy06882f82009-06-10 13:36:04 -07002038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 } else {
2040 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2041 }
2042 }
2043 Binder.restoreCallingIdentity(origId);
2044 }
2045
2046 public void addAppToken(int addPos, IApplicationToken token,
2047 int groupId, int requestedOrientation, boolean fullscreen) {
2048 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2049 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002050 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 }
Romain Guy06882f82009-06-10 13:36:04 -07002052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 synchronized(mWindowMap) {
2054 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2055 if (wtoken != null) {
2056 Log.w(TAG, "Attempted to add existing app token: " + token);
2057 return;
2058 }
2059 wtoken = new AppWindowToken(token);
2060 wtoken.groupId = groupId;
2061 wtoken.appFullscreen = fullscreen;
2062 wtoken.requestedOrientation = requestedOrientation;
2063 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002064 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 mTokenMap.put(token.asBinder(), wtoken);
2066 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 // Application tokens start out hidden.
2069 wtoken.hidden = true;
2070 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 //dump();
2073 }
2074 }
Romain Guy06882f82009-06-10 13:36:04 -07002075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 public void setAppGroupId(IBinder token, int groupId) {
2077 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2078 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002079 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 }
2081
2082 synchronized(mWindowMap) {
2083 AppWindowToken wtoken = findAppWindowToken(token);
2084 if (wtoken == null) {
2085 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2086 return;
2087 }
2088 wtoken.groupId = groupId;
2089 }
2090 }
Romain Guy06882f82009-06-10 13:36:04 -07002091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 public int getOrientationFromWindowsLocked() {
2093 int pos = mWindows.size() - 1;
2094 while (pos >= 0) {
2095 WindowState wtoken = (WindowState) mWindows.get(pos);
2096 pos--;
2097 if (wtoken.mAppToken != null) {
2098 // We hit an application window. so the orientation will be determined by the
2099 // app window. No point in continuing further.
2100 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2101 }
2102 if (!wtoken.isVisibleLw()) {
2103 continue;
2104 }
2105 int req = wtoken.mAttrs.screenOrientation;
2106 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2107 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2108 continue;
2109 } else {
2110 return req;
2111 }
2112 }
2113 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2114 }
Romain Guy06882f82009-06-10 13:36:04 -07002115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 public int getOrientationFromAppTokensLocked() {
2117 int pos = mAppTokens.size() - 1;
2118 int curGroup = 0;
2119 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002120 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002122 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 while (pos >= 0) {
2124 AppWindowToken wtoken = mAppTokens.get(pos);
2125 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002126 // if we're about to tear down this window and not seek for
2127 // the behind activity, don't use it for orientation
2128 if (!findingBehind
2129 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002130 continue;
2131 }
2132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 if (!haveGroup) {
2134 // We ignore any hidden applications on the top.
2135 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2136 continue;
2137 }
2138 haveGroup = true;
2139 curGroup = wtoken.groupId;
2140 lastOrientation = wtoken.requestedOrientation;
2141 } else if (curGroup != wtoken.groupId) {
2142 // If we have hit a new application group, and the bottom
2143 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002144 // the orientation behind it, and the last app was
2145 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002147 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2148 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 return lastOrientation;
2150 }
2151 }
2152 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002153 // If this application is fullscreen, and didn't explicitly say
2154 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002156 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002157 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002158 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 return or;
2160 }
2161 // If this application has requested an explicit orientation,
2162 // then use it.
2163 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2164 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2165 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2166 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2167 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2168 return or;
2169 }
Owen Lin3413b892009-05-01 17:12:32 -07002170 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 }
2172 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2173 }
Romain Guy06882f82009-06-10 13:36:04 -07002174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002176 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002177 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2178 "updateOrientationFromAppTokens()")) {
2179 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2180 }
2181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 Configuration config;
2183 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002184 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2185 freezeThisOneIfNeeded);
2186 Binder.restoreCallingIdentity(ident);
2187 return config;
2188 }
2189
2190 Configuration updateOrientationFromAppTokensUnchecked(
2191 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2192 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002194 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 }
2196 if (config != null) {
2197 mLayoutNeeded = true;
2198 performLayoutAndPlaceSurfacesLocked();
2199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 return config;
2201 }
Romain Guy06882f82009-06-10 13:36:04 -07002202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 /*
2204 * The orientation is computed from non-application windows first. If none of
2205 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002206 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2208 * android.os.IBinder)
2209 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002210 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002211 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 long ident = Binder.clearCallingIdentity();
2214 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002215 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 if (req != mForcedAppOrientation) {
2218 changed = true;
2219 mForcedAppOrientation = req;
2220 //send a message to Policy indicating orientation change to take
2221 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002222 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 }
Romain Guy06882f82009-06-10 13:36:04 -07002224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 if (changed) {
2226 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002227 WindowManagerPolicy.USE_LAST_ROTATION,
2228 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 if (changed) {
2230 if (freezeThisOneIfNeeded != null) {
2231 AppWindowToken wtoken = findAppWindowToken(
2232 freezeThisOneIfNeeded);
2233 if (wtoken != null) {
2234 startAppFreezingScreenLocked(wtoken,
2235 ActivityInfo.CONFIG_ORIENTATION);
2236 }
2237 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002238 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 }
2240 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002241
2242 // No obvious action we need to take, but if our current
2243 // state mismatches the activity maanager's, update it
2244 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002245 mTempConfiguration.setToDefaults();
2246 if (computeNewConfigurationLocked(mTempConfiguration)) {
2247 if (appConfig.diff(mTempConfiguration) != 0) {
2248 Log.i(TAG, "Config changed: " + mTempConfiguration);
2249 return new Configuration(mTempConfiguration);
2250 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002251 }
2252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 } finally {
2254 Binder.restoreCallingIdentity(ident);
2255 }
Romain Guy06882f82009-06-10 13:36:04 -07002256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 return null;
2258 }
Romain Guy06882f82009-06-10 13:36:04 -07002259
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002260 int computeForcedAppOrientationLocked() {
2261 int req = getOrientationFromWindowsLocked();
2262 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2263 req = getOrientationFromAppTokensLocked();
2264 }
2265 return req;
2266 }
Romain Guy06882f82009-06-10 13:36:04 -07002267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2269 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2270 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002271 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 }
Romain Guy06882f82009-06-10 13:36:04 -07002273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 synchronized(mWindowMap) {
2275 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2276 if (wtoken == null) {
2277 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2278 return;
2279 }
Romain Guy06882f82009-06-10 13:36:04 -07002280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 wtoken.requestedOrientation = requestedOrientation;
2282 }
2283 }
Romain Guy06882f82009-06-10 13:36:04 -07002284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 public int getAppOrientation(IApplicationToken token) {
2286 synchronized(mWindowMap) {
2287 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2288 if (wtoken == null) {
2289 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2290 }
Romain Guy06882f82009-06-10 13:36:04 -07002291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 return wtoken.requestedOrientation;
2293 }
2294 }
Romain Guy06882f82009-06-10 13:36:04 -07002295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2297 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2298 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002299 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 }
2301
2302 synchronized(mWindowMap) {
2303 boolean changed = false;
2304 if (token == null) {
2305 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2306 changed = mFocusedApp != null;
2307 mFocusedApp = null;
2308 mKeyWaiter.tickle();
2309 } else {
2310 AppWindowToken newFocus = findAppWindowToken(token);
2311 if (newFocus == null) {
2312 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2313 return;
2314 }
2315 changed = mFocusedApp != newFocus;
2316 mFocusedApp = newFocus;
2317 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2318 mKeyWaiter.tickle();
2319 }
2320
2321 if (moveFocusNow && changed) {
2322 final long origId = Binder.clearCallingIdentity();
2323 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2324 Binder.restoreCallingIdentity(origId);
2325 }
2326 }
2327 }
2328
2329 public void prepareAppTransition(int transit) {
2330 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2331 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002332 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 }
Romain Guy06882f82009-06-10 13:36:04 -07002334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 synchronized(mWindowMap) {
2336 if (DEBUG_APP_TRANSITIONS) Log.v(
2337 TAG, "Prepare app transition: transit=" + transit
2338 + " mNextAppTransition=" + mNextAppTransition);
2339 if (!mDisplayFrozen) {
2340 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2341 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002342 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2343 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2344 // Opening a new task always supersedes a close for the anim.
2345 mNextAppTransition = transit;
2346 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2347 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2348 // Opening a new activity always supersedes a close for the anim.
2349 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 }
2351 mAppTransitionReady = false;
2352 mAppTransitionTimeout = false;
2353 mStartingIconInTransition = false;
2354 mSkipAppTransitionAnimation = false;
2355 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2356 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2357 5000);
2358 }
2359 }
2360 }
2361
2362 public int getPendingAppTransition() {
2363 return mNextAppTransition;
2364 }
Romain Guy06882f82009-06-10 13:36:04 -07002365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 public void executeAppTransition() {
2367 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2368 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002369 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 }
Romain Guy06882f82009-06-10 13:36:04 -07002371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 synchronized(mWindowMap) {
2373 if (DEBUG_APP_TRANSITIONS) Log.v(
2374 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2375 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2376 mAppTransitionReady = true;
2377 final long origId = Binder.clearCallingIdentity();
2378 performLayoutAndPlaceSurfacesLocked();
2379 Binder.restoreCallingIdentity(origId);
2380 }
2381 }
2382 }
2383
2384 public void setAppStartingWindow(IBinder token, String pkg,
2385 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2386 IBinder transferFrom, boolean createIfNeeded) {
2387 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2388 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002389 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 }
2391
2392 synchronized(mWindowMap) {
2393 if (DEBUG_STARTING_WINDOW) Log.v(
2394 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2395 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 AppWindowToken wtoken = findAppWindowToken(token);
2398 if (wtoken == null) {
2399 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2400 return;
2401 }
2402
2403 // If the display is frozen, we won't do anything until the
2404 // actual window is displayed so there is no reason to put in
2405 // the starting window.
2406 if (mDisplayFrozen) {
2407 return;
2408 }
Romain Guy06882f82009-06-10 13:36:04 -07002409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 if (wtoken.startingData != null) {
2411 return;
2412 }
Romain Guy06882f82009-06-10 13:36:04 -07002413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 if (transferFrom != null) {
2415 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2416 if (ttoken != null) {
2417 WindowState startingWindow = ttoken.startingWindow;
2418 if (startingWindow != null) {
2419 if (mStartingIconInTransition) {
2420 // In this case, the starting icon has already
2421 // been displayed, so start letting windows get
2422 // shown immediately without any more transitions.
2423 mSkipAppTransitionAnimation = true;
2424 }
2425 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2426 "Moving existing starting from " + ttoken
2427 + " to " + wtoken);
2428 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 // Transfer the starting window over to the new
2431 // token.
2432 wtoken.startingData = ttoken.startingData;
2433 wtoken.startingView = ttoken.startingView;
2434 wtoken.startingWindow = startingWindow;
2435 ttoken.startingData = null;
2436 ttoken.startingView = null;
2437 ttoken.startingWindow = null;
2438 ttoken.startingMoved = true;
2439 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002440 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 startingWindow.mAppToken = wtoken;
2442 mWindows.remove(startingWindow);
2443 ttoken.windows.remove(startingWindow);
2444 ttoken.allAppWindows.remove(startingWindow);
2445 addWindowToListInOrderLocked(startingWindow, true);
2446 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 // Propagate other interesting state between the
2449 // tokens. If the old token is displayed, we should
2450 // immediately force the new one to be displayed. If
2451 // it is animating, we need to move that animation to
2452 // the new one.
2453 if (ttoken.allDrawn) {
2454 wtoken.allDrawn = true;
2455 }
2456 if (ttoken.firstWindowDrawn) {
2457 wtoken.firstWindowDrawn = true;
2458 }
2459 if (!ttoken.hidden) {
2460 wtoken.hidden = false;
2461 wtoken.hiddenRequested = false;
2462 wtoken.willBeHidden = false;
2463 }
2464 if (wtoken.clientHidden != ttoken.clientHidden) {
2465 wtoken.clientHidden = ttoken.clientHidden;
2466 wtoken.sendAppVisibilityToClients();
2467 }
2468 if (ttoken.animation != null) {
2469 wtoken.animation = ttoken.animation;
2470 wtoken.animating = ttoken.animating;
2471 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2472 ttoken.animation = null;
2473 ttoken.animLayerAdjustment = 0;
2474 wtoken.updateLayers();
2475 ttoken.updateLayers();
2476 }
Romain Guy06882f82009-06-10 13:36:04 -07002477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 mLayoutNeeded = true;
2480 performLayoutAndPlaceSurfacesLocked();
2481 Binder.restoreCallingIdentity(origId);
2482 return;
2483 } else if (ttoken.startingData != null) {
2484 // The previous app was getting ready to show a
2485 // starting window, but hasn't yet done so. Steal it!
2486 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2487 "Moving pending starting from " + ttoken
2488 + " to " + wtoken);
2489 wtoken.startingData = ttoken.startingData;
2490 ttoken.startingData = null;
2491 ttoken.startingMoved = true;
2492 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2493 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2494 // want to process the message ASAP, before any other queued
2495 // messages.
2496 mH.sendMessageAtFrontOfQueue(m);
2497 return;
2498 }
2499 }
2500 }
2501
2502 // There is no existing starting window, and the caller doesn't
2503 // want us to create one, so that's it!
2504 if (!createIfNeeded) {
2505 return;
2506 }
Romain Guy06882f82009-06-10 13:36:04 -07002507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 mStartingIconInTransition = true;
2509 wtoken.startingData = new StartingData(
2510 pkg, theme, nonLocalizedLabel,
2511 labelRes, icon);
2512 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2513 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2514 // want to process the message ASAP, before any other queued
2515 // messages.
2516 mH.sendMessageAtFrontOfQueue(m);
2517 }
2518 }
2519
2520 public void setAppWillBeHidden(IBinder token) {
2521 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2522 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002523 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 }
2525
2526 AppWindowToken wtoken;
2527
2528 synchronized(mWindowMap) {
2529 wtoken = findAppWindowToken(token);
2530 if (wtoken == null) {
2531 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2532 return;
2533 }
2534 wtoken.willBeHidden = true;
2535 }
2536 }
Romain Guy06882f82009-06-10 13:36:04 -07002537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2539 boolean visible, int transit, boolean performLayout) {
2540 boolean delayed = false;
2541
2542 if (wtoken.clientHidden == visible) {
2543 wtoken.clientHidden = !visible;
2544 wtoken.sendAppVisibilityToClients();
2545 }
Romain Guy06882f82009-06-10 13:36:04 -07002546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 wtoken.willBeHidden = false;
2548 if (wtoken.hidden == visible) {
2549 final int N = wtoken.allAppWindows.size();
2550 boolean changed = false;
2551 if (DEBUG_APP_TRANSITIONS) Log.v(
2552 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2553 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2558 if (wtoken.animation == sDummyAnimation) {
2559 wtoken.animation = null;
2560 }
2561 applyAnimationLocked(wtoken, lp, transit, visible);
2562 changed = true;
2563 if (wtoken.animation != null) {
2564 delayed = runningAppAnimation = true;
2565 }
2566 }
Romain Guy06882f82009-06-10 13:36:04 -07002567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 for (int i=0; i<N; i++) {
2569 WindowState win = wtoken.allAppWindows.get(i);
2570 if (win == wtoken.startingWindow) {
2571 continue;
2572 }
2573
2574 if (win.isAnimating()) {
2575 delayed = true;
2576 }
Romain Guy06882f82009-06-10 13:36:04 -07002577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2579 //win.dump(" ");
2580 if (visible) {
2581 if (!win.isVisibleNow()) {
2582 if (!runningAppAnimation) {
2583 applyAnimationLocked(win,
2584 WindowManagerPolicy.TRANSIT_ENTER, true);
2585 }
2586 changed = true;
2587 }
2588 } else if (win.isVisibleNow()) {
2589 if (!runningAppAnimation) {
2590 applyAnimationLocked(win,
2591 WindowManagerPolicy.TRANSIT_EXIT, false);
2592 }
2593 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2594 KeyWaiter.RETURN_NOTHING);
2595 changed = true;
2596 }
2597 }
2598
2599 wtoken.hidden = wtoken.hiddenRequested = !visible;
2600 if (!visible) {
2601 unsetAppFreezingScreenLocked(wtoken, true, true);
2602 } else {
2603 // If we are being set visible, and the starting window is
2604 // not yet displayed, then make sure it doesn't get displayed.
2605 WindowState swin = wtoken.startingWindow;
2606 if (swin != null && (swin.mDrawPending
2607 || swin.mCommitDrawPending)) {
2608 swin.mPolicyVisibility = false;
2609 swin.mPolicyVisibilityAfterAnim = false;
2610 }
2611 }
Romain Guy06882f82009-06-10 13:36:04 -07002612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2614 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2615 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 if (changed && performLayout) {
2618 mLayoutNeeded = true;
2619 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 performLayoutAndPlaceSurfacesLocked();
2621 }
2622 }
2623
2624 if (wtoken.animation != null) {
2625 delayed = true;
2626 }
Romain Guy06882f82009-06-10 13:36:04 -07002627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 return delayed;
2629 }
2630
2631 public void setAppVisibility(IBinder token, boolean visible) {
2632 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2633 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002634 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 }
2636
2637 AppWindowToken wtoken;
2638
2639 synchronized(mWindowMap) {
2640 wtoken = findAppWindowToken(token);
2641 if (wtoken == null) {
2642 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2643 return;
2644 }
2645
2646 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2647 RuntimeException e = new RuntimeException();
2648 e.fillInStackTrace();
2649 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2650 + "): mNextAppTransition=" + mNextAppTransition
2651 + " hidden=" + wtoken.hidden
2652 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2653 }
Romain Guy06882f82009-06-10 13:36:04 -07002654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 // If we are preparing an app transition, then delay changing
2656 // the visibility of this token until we execute that transition.
2657 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2658 // Already in requested state, don't do anything more.
2659 if (wtoken.hiddenRequested != visible) {
2660 return;
2661 }
2662 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 if (DEBUG_APP_TRANSITIONS) Log.v(
2665 TAG, "Setting dummy animation on: " + wtoken);
2666 wtoken.setDummyAnimation();
2667 mOpeningApps.remove(wtoken);
2668 mClosingApps.remove(wtoken);
2669 wtoken.inPendingTransaction = true;
2670 if (visible) {
2671 mOpeningApps.add(wtoken);
2672 wtoken.allDrawn = false;
2673 wtoken.startingDisplayed = false;
2674 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 if (wtoken.clientHidden) {
2677 // In the case where we are making an app visible
2678 // but holding off for a transition, we still need
2679 // to tell the client to make its windows visible so
2680 // they get drawn. Otherwise, we will wait on
2681 // performing the transition until all windows have
2682 // been drawn, they never will be, and we are sad.
2683 wtoken.clientHidden = false;
2684 wtoken.sendAppVisibilityToClients();
2685 }
2686 } else {
2687 mClosingApps.add(wtoken);
2688 }
2689 return;
2690 }
Romain Guy06882f82009-06-10 13:36:04 -07002691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 final long origId = Binder.clearCallingIdentity();
2693 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2694 wtoken.updateReportedVisibilityLocked();
2695 Binder.restoreCallingIdentity(origId);
2696 }
2697 }
2698
2699 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2700 boolean unfreezeSurfaceNow, boolean force) {
2701 if (wtoken.freezingScreen) {
2702 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2703 + " force=" + force);
2704 final int N = wtoken.allAppWindows.size();
2705 boolean unfrozeWindows = false;
2706 for (int i=0; i<N; i++) {
2707 WindowState w = wtoken.allAppWindows.get(i);
2708 if (w.mAppFreezing) {
2709 w.mAppFreezing = false;
2710 if (w.mSurface != null && !w.mOrientationChanging) {
2711 w.mOrientationChanging = true;
2712 }
2713 unfrozeWindows = true;
2714 }
2715 }
2716 if (force || unfrozeWindows) {
2717 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2718 wtoken.freezingScreen = false;
2719 mAppsFreezingScreen--;
2720 }
2721 if (unfreezeSurfaceNow) {
2722 if (unfrozeWindows) {
2723 mLayoutNeeded = true;
2724 performLayoutAndPlaceSurfacesLocked();
2725 }
2726 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2727 stopFreezingDisplayLocked();
2728 }
2729 }
2730 }
2731 }
Romain Guy06882f82009-06-10 13:36:04 -07002732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2734 int configChanges) {
2735 if (DEBUG_ORIENTATION) {
2736 RuntimeException e = new RuntimeException();
2737 e.fillInStackTrace();
2738 Log.i(TAG, "Set freezing of " + wtoken.appToken
2739 + ": hidden=" + wtoken.hidden + " freezing="
2740 + wtoken.freezingScreen, e);
2741 }
2742 if (!wtoken.hiddenRequested) {
2743 if (!wtoken.freezingScreen) {
2744 wtoken.freezingScreen = true;
2745 mAppsFreezingScreen++;
2746 if (mAppsFreezingScreen == 1) {
2747 startFreezingDisplayLocked();
2748 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
2749 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
2750 5000);
2751 }
2752 }
2753 final int N = wtoken.allAppWindows.size();
2754 for (int i=0; i<N; i++) {
2755 WindowState w = wtoken.allAppWindows.get(i);
2756 w.mAppFreezing = true;
2757 }
2758 }
2759 }
Romain Guy06882f82009-06-10 13:36:04 -07002760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 public void startAppFreezingScreen(IBinder token, int configChanges) {
2762 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2763 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002764 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 }
2766
2767 synchronized(mWindowMap) {
2768 if (configChanges == 0 && !mDisplayFrozen) {
2769 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
2770 return;
2771 }
Romain Guy06882f82009-06-10 13:36:04 -07002772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 AppWindowToken wtoken = findAppWindowToken(token);
2774 if (wtoken == null || wtoken.appToken == null) {
2775 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
2776 return;
2777 }
2778 final long origId = Binder.clearCallingIdentity();
2779 startAppFreezingScreenLocked(wtoken, configChanges);
2780 Binder.restoreCallingIdentity(origId);
2781 }
2782 }
Romain Guy06882f82009-06-10 13:36:04 -07002783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 public void stopAppFreezingScreen(IBinder token, boolean force) {
2785 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2786 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002787 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 }
2789
2790 synchronized(mWindowMap) {
2791 AppWindowToken wtoken = findAppWindowToken(token);
2792 if (wtoken == null || wtoken.appToken == null) {
2793 return;
2794 }
2795 final long origId = Binder.clearCallingIdentity();
2796 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
2797 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
2798 unsetAppFreezingScreenLocked(wtoken, true, force);
2799 Binder.restoreCallingIdentity(origId);
2800 }
2801 }
Romain Guy06882f82009-06-10 13:36:04 -07002802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 public void removeAppToken(IBinder token) {
2804 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2805 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002806 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 }
2808
2809 AppWindowToken wtoken = null;
2810 AppWindowToken startingToken = null;
2811 boolean delayed = false;
2812
2813 final long origId = Binder.clearCallingIdentity();
2814 synchronized(mWindowMap) {
2815 WindowToken basewtoken = mTokenMap.remove(token);
2816 mTokenList.remove(basewtoken);
2817 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
2818 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
2819 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
2820 wtoken.inPendingTransaction = false;
2821 mOpeningApps.remove(wtoken);
2822 if (mClosingApps.contains(wtoken)) {
2823 delayed = true;
2824 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2825 mClosingApps.add(wtoken);
2826 delayed = true;
2827 }
2828 if (DEBUG_APP_TRANSITIONS) Log.v(
2829 TAG, "Removing app " + wtoken + " delayed=" + delayed
2830 + " animation=" + wtoken.animation
2831 + " animating=" + wtoken.animating);
2832 if (delayed) {
2833 // set the token aside because it has an active animation to be finished
2834 mExitingAppTokens.add(wtoken);
2835 }
2836 mAppTokens.remove(wtoken);
2837 wtoken.removed = true;
2838 if (wtoken.startingData != null) {
2839 startingToken = wtoken;
2840 }
2841 unsetAppFreezingScreenLocked(wtoken, true, true);
2842 if (mFocusedApp == wtoken) {
2843 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
2844 mFocusedApp = null;
2845 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2846 mKeyWaiter.tickle();
2847 }
2848 } else {
2849 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
2850 }
Romain Guy06882f82009-06-10 13:36:04 -07002851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 if (!delayed && wtoken != null) {
2853 wtoken.updateReportedVisibilityLocked();
2854 }
2855 }
2856 Binder.restoreCallingIdentity(origId);
2857
2858 if (startingToken != null) {
2859 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
2860 + startingToken + ": app token removed");
2861 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
2862 mH.sendMessage(m);
2863 }
2864 }
2865
2866 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
2867 final int NW = token.windows.size();
2868 for (int i=0; i<NW; i++) {
2869 WindowState win = token.windows.get(i);
2870 mWindows.remove(win);
2871 int j = win.mChildWindows.size();
2872 while (j > 0) {
2873 j--;
2874 mWindows.remove(win.mChildWindows.get(j));
2875 }
2876 }
2877 return NW > 0;
2878 }
2879
2880 void dumpAppTokensLocked() {
2881 for (int i=mAppTokens.size()-1; i>=0; i--) {
2882 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
2883 }
2884 }
Romain Guy06882f82009-06-10 13:36:04 -07002885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 void dumpWindowsLocked() {
2887 for (int i=mWindows.size()-1; i>=0; i--) {
2888 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
2889 }
2890 }
Romain Guy06882f82009-06-10 13:36:04 -07002891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 private int findWindowOffsetLocked(int tokenPos) {
2893 final int NW = mWindows.size();
2894
2895 if (tokenPos >= mAppTokens.size()) {
2896 int i = NW;
2897 while (i > 0) {
2898 i--;
2899 WindowState win = (WindowState)mWindows.get(i);
2900 if (win.getAppToken() != null) {
2901 return i+1;
2902 }
2903 }
2904 }
2905
2906 while (tokenPos > 0) {
2907 // Find the first app token below the new position that has
2908 // a window displayed.
2909 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
2910 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
2911 + tokenPos + " -- " + wtoken.token);
2912 int i = wtoken.windows.size();
2913 while (i > 0) {
2914 i--;
2915 WindowState win = wtoken.windows.get(i);
2916 int j = win.mChildWindows.size();
2917 while (j > 0) {
2918 j--;
2919 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2920 if (cwin.mSubLayer >= 0 ) {
2921 for (int pos=NW-1; pos>=0; pos--) {
2922 if (mWindows.get(pos) == cwin) {
2923 if (DEBUG_REORDER) Log.v(TAG,
2924 "Found child win @" + (pos+1));
2925 return pos+1;
2926 }
2927 }
2928 }
2929 }
2930 for (int pos=NW-1; pos>=0; pos--) {
2931 if (mWindows.get(pos) == win) {
2932 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
2933 return pos+1;
2934 }
2935 }
2936 }
2937 tokenPos--;
2938 }
2939
2940 return 0;
2941 }
2942
2943 private final int reAddWindowLocked(int index, WindowState win) {
2944 final int NCW = win.mChildWindows.size();
2945 boolean added = false;
2946 for (int j=0; j<NCW; j++) {
2947 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2948 if (!added && cwin.mSubLayer >= 0) {
2949 mWindows.add(index, win);
2950 index++;
2951 added = true;
2952 }
2953 mWindows.add(index, cwin);
2954 index++;
2955 }
2956 if (!added) {
2957 mWindows.add(index, win);
2958 index++;
2959 }
2960 return index;
2961 }
Romain Guy06882f82009-06-10 13:36:04 -07002962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 private final int reAddAppWindowsLocked(int index, WindowToken token) {
2964 final int NW = token.windows.size();
2965 for (int i=0; i<NW; i++) {
2966 index = reAddWindowLocked(index, token.windows.get(i));
2967 }
2968 return index;
2969 }
2970
2971 public void moveAppToken(int index, IBinder token) {
2972 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2973 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002974 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 }
2976
2977 synchronized(mWindowMap) {
2978 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
2979 if (DEBUG_REORDER) dumpAppTokensLocked();
2980 final AppWindowToken wtoken = findAppWindowToken(token);
2981 if (wtoken == null || !mAppTokens.remove(wtoken)) {
2982 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
2983 + token + " (" + wtoken + ")");
2984 return;
2985 }
2986 mAppTokens.add(index, wtoken);
2987 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
2988 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 final long origId = Binder.clearCallingIdentity();
2991 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
2992 if (DEBUG_REORDER) dumpWindowsLocked();
2993 if (tmpRemoveAppWindowsLocked(wtoken)) {
2994 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
2995 if (DEBUG_REORDER) dumpWindowsLocked();
2996 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
2997 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
2998 if (DEBUG_REORDER) dumpWindowsLocked();
2999 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 mLayoutNeeded = true;
3001 performLayoutAndPlaceSurfacesLocked();
3002 }
3003 Binder.restoreCallingIdentity(origId);
3004 }
3005 }
3006
3007 private void removeAppTokensLocked(List<IBinder> tokens) {
3008 // XXX This should be done more efficiently!
3009 // (take advantage of the fact that both lists should be
3010 // ordered in the same way.)
3011 int N = tokens.size();
3012 for (int i=0; i<N; i++) {
3013 IBinder token = tokens.get(i);
3014 final AppWindowToken wtoken = findAppWindowToken(token);
3015 if (!mAppTokens.remove(wtoken)) {
3016 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3017 + token + " (" + wtoken + ")");
3018 i--;
3019 N--;
3020 }
3021 }
3022 }
3023
3024 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3025 // First remove all of the windows from the list.
3026 final int N = tokens.size();
3027 int i;
3028 for (i=0; i<N; i++) {
3029 WindowToken token = mTokenMap.get(tokens.get(i));
3030 if (token != null) {
3031 tmpRemoveAppWindowsLocked(token);
3032 }
3033 }
3034
3035 // Where to start adding?
3036 int pos = findWindowOffsetLocked(tokenPos);
3037
3038 // And now add them back at the correct place.
3039 for (i=0; i<N; i++) {
3040 WindowToken token = mTokenMap.get(tokens.get(i));
3041 if (token != null) {
3042 pos = reAddAppWindowsLocked(pos, token);
3043 }
3044 }
3045
3046 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 mLayoutNeeded = true;
3048 performLayoutAndPlaceSurfacesLocked();
3049
3050 //dump();
3051 }
3052
3053 public void moveAppTokensToTop(List<IBinder> tokens) {
3054 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3055 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003056 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 }
3058
3059 final long origId = Binder.clearCallingIdentity();
3060 synchronized(mWindowMap) {
3061 removeAppTokensLocked(tokens);
3062 final int N = tokens.size();
3063 for (int i=0; i<N; i++) {
3064 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3065 if (wt != null) {
3066 mAppTokens.add(wt);
3067 }
3068 }
3069 moveAppWindowsLocked(tokens, mAppTokens.size());
3070 }
3071 Binder.restoreCallingIdentity(origId);
3072 }
3073
3074 public void moveAppTokensToBottom(List<IBinder> tokens) {
3075 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3076 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003077 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 }
3079
3080 final long origId = Binder.clearCallingIdentity();
3081 synchronized(mWindowMap) {
3082 removeAppTokensLocked(tokens);
3083 final int N = tokens.size();
3084 int pos = 0;
3085 for (int i=0; i<N; i++) {
3086 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3087 if (wt != null) {
3088 mAppTokens.add(pos, wt);
3089 pos++;
3090 }
3091 }
3092 moveAppWindowsLocked(tokens, 0);
3093 }
3094 Binder.restoreCallingIdentity(origId);
3095 }
3096
3097 // -------------------------------------------------------------
3098 // Misc IWindowSession methods
3099 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 public void disableKeyguard(IBinder token, String tag) {
3102 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3103 != PackageManager.PERMISSION_GRANTED) {
3104 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3105 }
3106 mKeyguardDisabled.acquire(token, tag);
3107 }
3108
3109 public void reenableKeyguard(IBinder token) {
3110 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3111 != PackageManager.PERMISSION_GRANTED) {
3112 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3113 }
3114 synchronized (mKeyguardDisabled) {
3115 mKeyguardDisabled.release(token);
3116
3117 if (!mKeyguardDisabled.isAcquired()) {
3118 // if we are the last one to reenable the keyguard wait until
3119 // we have actaully finished reenabling until returning
3120 mWaitingUntilKeyguardReenabled = true;
3121 while (mWaitingUntilKeyguardReenabled) {
3122 try {
3123 mKeyguardDisabled.wait();
3124 } catch (InterruptedException e) {
3125 Thread.currentThread().interrupt();
3126 }
3127 }
3128 }
3129 }
3130 }
3131
3132 /**
3133 * @see android.app.KeyguardManager#exitKeyguardSecurely
3134 */
3135 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3136 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3137 != PackageManager.PERMISSION_GRANTED) {
3138 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3139 }
3140 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3141 public void onKeyguardExitResult(boolean success) {
3142 try {
3143 callback.onKeyguardExitResult(success);
3144 } catch (RemoteException e) {
3145 // Client has died, we don't care.
3146 }
3147 }
3148 });
3149 }
3150
3151 public boolean inKeyguardRestrictedInputMode() {
3152 return mPolicy.inKeyguardRestrictedKeyInputMode();
3153 }
Romain Guy06882f82009-06-10 13:36:04 -07003154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 static float fixScale(float scale) {
3156 if (scale < 0) scale = 0;
3157 else if (scale > 20) scale = 20;
3158 return Math.abs(scale);
3159 }
Romain Guy06882f82009-06-10 13:36:04 -07003160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 public void setAnimationScale(int which, float scale) {
3162 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3163 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003164 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 }
3166
3167 if (scale < 0) scale = 0;
3168 else if (scale > 20) scale = 20;
3169 scale = Math.abs(scale);
3170 switch (which) {
3171 case 0: mWindowAnimationScale = fixScale(scale); break;
3172 case 1: mTransitionAnimationScale = fixScale(scale); break;
3173 }
Romain Guy06882f82009-06-10 13:36:04 -07003174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 // Persist setting
3176 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3177 }
Romain Guy06882f82009-06-10 13:36:04 -07003178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 public void setAnimationScales(float[] scales) {
3180 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3181 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003182 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 }
3184
3185 if (scales != null) {
3186 if (scales.length >= 1) {
3187 mWindowAnimationScale = fixScale(scales[0]);
3188 }
3189 if (scales.length >= 2) {
3190 mTransitionAnimationScale = fixScale(scales[1]);
3191 }
3192 }
Romain Guy06882f82009-06-10 13:36:04 -07003193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 // Persist setting
3195 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3196 }
Romain Guy06882f82009-06-10 13:36:04 -07003197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 public float getAnimationScale(int which) {
3199 switch (which) {
3200 case 0: return mWindowAnimationScale;
3201 case 1: return mTransitionAnimationScale;
3202 }
3203 return 0;
3204 }
Romain Guy06882f82009-06-10 13:36:04 -07003205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 public float[] getAnimationScales() {
3207 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3208 }
Romain Guy06882f82009-06-10 13:36:04 -07003209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 public int getSwitchState(int sw) {
3211 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3212 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003213 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 }
3215 return KeyInputQueue.getSwitchState(sw);
3216 }
Romain Guy06882f82009-06-10 13:36:04 -07003217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 public int getSwitchStateForDevice(int devid, int sw) {
3219 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3220 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003221 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 }
3223 return KeyInputQueue.getSwitchState(devid, sw);
3224 }
Romain Guy06882f82009-06-10 13:36:04 -07003225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 public int getScancodeState(int sw) {
3227 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3228 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003229 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 }
3231 return KeyInputQueue.getScancodeState(sw);
3232 }
Romain Guy06882f82009-06-10 13:36:04 -07003233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 public int getScancodeStateForDevice(int devid, int sw) {
3235 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3236 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003237 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
3239 return KeyInputQueue.getScancodeState(devid, sw);
3240 }
Romain Guy06882f82009-06-10 13:36:04 -07003241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 public int getKeycodeState(int sw) {
3243 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3244 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003245 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 }
3247 return KeyInputQueue.getKeycodeState(sw);
3248 }
Romain Guy06882f82009-06-10 13:36:04 -07003249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 public int getKeycodeStateForDevice(int devid, int sw) {
3251 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3252 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003253 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 }
3255 return KeyInputQueue.getKeycodeState(devid, sw);
3256 }
Romain Guy06882f82009-06-10 13:36:04 -07003257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3259 return KeyInputQueue.hasKeys(keycodes, keyExists);
3260 }
Romain Guy06882f82009-06-10 13:36:04 -07003261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 public void enableScreenAfterBoot() {
3263 synchronized(mWindowMap) {
3264 if (mSystemBooted) {
3265 return;
3266 }
3267 mSystemBooted = true;
3268 }
Romain Guy06882f82009-06-10 13:36:04 -07003269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 performEnableScreen();
3271 }
Romain Guy06882f82009-06-10 13:36:04 -07003272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 public void enableScreenIfNeededLocked() {
3274 if (mDisplayEnabled) {
3275 return;
3276 }
3277 if (!mSystemBooted) {
3278 return;
3279 }
3280 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3281 }
Romain Guy06882f82009-06-10 13:36:04 -07003282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 public void performEnableScreen() {
3284 synchronized(mWindowMap) {
3285 if (mDisplayEnabled) {
3286 return;
3287 }
3288 if (!mSystemBooted) {
3289 return;
3290 }
Romain Guy06882f82009-06-10 13:36:04 -07003291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 // Don't enable the screen until all existing windows
3293 // have been drawn.
3294 final int N = mWindows.size();
3295 for (int i=0; i<N; i++) {
3296 WindowState w = (WindowState)mWindows.get(i);
3297 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3298 return;
3299 }
3300 }
Romain Guy06882f82009-06-10 13:36:04 -07003301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 mDisplayEnabled = true;
3303 if (false) {
3304 Log.i(TAG, "ENABLING SCREEN!");
3305 StringWriter sw = new StringWriter();
3306 PrintWriter pw = new PrintWriter(sw);
3307 this.dump(null, pw, null);
3308 Log.i(TAG, sw.toString());
3309 }
3310 try {
3311 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3312 if (surfaceFlinger != null) {
3313 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3314 Parcel data = Parcel.obtain();
3315 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3316 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3317 data, null, 0);
3318 data.recycle();
3319 }
3320 } catch (RemoteException ex) {
3321 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3322 }
3323 }
Romain Guy06882f82009-06-10 13:36:04 -07003324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003328 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3329 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 }
Romain Guy06882f82009-06-10 13:36:04 -07003331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 public void setInTouchMode(boolean mode) {
3333 synchronized(mWindowMap) {
3334 mInTouchMode = mode;
3335 }
3336 }
3337
Romain Guy06882f82009-06-10 13:36:04 -07003338 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003339 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003341 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003342 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 }
3344
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003345 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 }
Romain Guy06882f82009-06-10 13:36:04 -07003347
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003348 public void setRotationUnchecked(int rotation,
3349 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 if(DEBUG_ORIENTATION) Log.v(TAG,
3351 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 long origId = Binder.clearCallingIdentity();
3354 boolean changed;
3355 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003356 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 }
Romain Guy06882f82009-06-10 13:36:04 -07003358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 if (changed) {
3360 sendNewConfiguration();
3361 synchronized(mWindowMap) {
3362 mLayoutNeeded = true;
3363 performLayoutAndPlaceSurfacesLocked();
3364 }
3365 } else if (alwaysSendConfiguration) {
3366 //update configuration ignoring orientation change
3367 sendNewConfiguration();
3368 }
Romain Guy06882f82009-06-10 13:36:04 -07003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 Binder.restoreCallingIdentity(origId);
3371 }
Romain Guy06882f82009-06-10 13:36:04 -07003372
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003373 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 boolean changed;
3375 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3376 rotation = mRequestedRotation;
3377 } else {
3378 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003379 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 }
3381 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003382 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 mRotation, mDisplayEnabled);
3384 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3385 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003388 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 "Rotation changed to " + rotation
3390 + " from " + mRotation
3391 + " (forceApp=" + mForcedAppOrientation
3392 + ", req=" + mRequestedRotation + ")");
3393 mRotation = rotation;
3394 mWindowsFreezingScreen = true;
3395 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3396 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3397 2000);
3398 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003399 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 mQueue.setOrientation(rotation);
3401 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003402 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 }
3404 for (int i=mWindows.size()-1; i>=0; i--) {
3405 WindowState w = (WindowState)mWindows.get(i);
3406 if (w.mSurface != null) {
3407 w.mOrientationChanging = true;
3408 }
3409 }
3410 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3411 try {
3412 mRotationWatchers.get(i).onRotationChanged(rotation);
3413 } catch (RemoteException e) {
3414 }
3415 }
3416 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 return changed;
3419 }
Romain Guy06882f82009-06-10 13:36:04 -07003420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 public int getRotation() {
3422 return mRotation;
3423 }
3424
3425 public int watchRotation(IRotationWatcher watcher) {
3426 final IBinder watcherBinder = watcher.asBinder();
3427 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3428 public void binderDied() {
3429 synchronized (mWindowMap) {
3430 for (int i=0; i<mRotationWatchers.size(); i++) {
3431 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003432 IRotationWatcher removed = mRotationWatchers.remove(i);
3433 if (removed != null) {
3434 removed.asBinder().unlinkToDeath(this, 0);
3435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 i--;
3437 }
3438 }
3439 }
3440 }
3441 };
Romain Guy06882f82009-06-10 13:36:04 -07003442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 synchronized (mWindowMap) {
3444 try {
3445 watcher.asBinder().linkToDeath(dr, 0);
3446 mRotationWatchers.add(watcher);
3447 } catch (RemoteException e) {
3448 // Client died, no cleanup needed.
3449 }
Romain Guy06882f82009-06-10 13:36:04 -07003450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 return mRotation;
3452 }
3453 }
3454
3455 /**
3456 * Starts the view server on the specified port.
3457 *
3458 * @param port The port to listener to.
3459 *
3460 * @return True if the server was successfully started, false otherwise.
3461 *
3462 * @see com.android.server.ViewServer
3463 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3464 */
3465 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003466 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 return false;
3468 }
3469
3470 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3471 return false;
3472 }
3473
3474 if (port < 1024) {
3475 return false;
3476 }
3477
3478 if (mViewServer != null) {
3479 if (!mViewServer.isRunning()) {
3480 try {
3481 return mViewServer.start();
3482 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003483 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 }
3485 }
3486 return false;
3487 }
3488
3489 try {
3490 mViewServer = new ViewServer(this, port);
3491 return mViewServer.start();
3492 } catch (IOException e) {
3493 Log.w(TAG, "View server did not start");
3494 }
3495 return false;
3496 }
3497
Romain Guy06882f82009-06-10 13:36:04 -07003498 private boolean isSystemSecure() {
3499 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3500 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3501 }
3502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 /**
3504 * Stops the view server if it exists.
3505 *
3506 * @return True if the server stopped, false if it wasn't started or
3507 * couldn't be stopped.
3508 *
3509 * @see com.android.server.ViewServer
3510 */
3511 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003512 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 return false;
3514 }
3515
3516 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3517 return false;
3518 }
3519
3520 if (mViewServer != null) {
3521 return mViewServer.stop();
3522 }
3523 return false;
3524 }
3525
3526 /**
3527 * Indicates whether the view server is running.
3528 *
3529 * @return True if the server is running, false otherwise.
3530 *
3531 * @see com.android.server.ViewServer
3532 */
3533 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003534 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 return false;
3536 }
3537
3538 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3539 return false;
3540 }
3541
3542 return mViewServer != null && mViewServer.isRunning();
3543 }
3544
3545 /**
3546 * Lists all availble windows in the system. The listing is written in the
3547 * specified Socket's output stream with the following syntax:
3548 * windowHashCodeInHexadecimal windowName
3549 * Each line of the ouput represents a different window.
3550 *
3551 * @param client The remote client to send the listing to.
3552 * @return False if an error occured, true otherwise.
3553 */
3554 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003555 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 return false;
3557 }
3558
3559 boolean result = true;
3560
3561 Object[] windows;
3562 synchronized (mWindowMap) {
3563 windows = new Object[mWindows.size()];
3564 //noinspection unchecked
3565 windows = mWindows.toArray(windows);
3566 }
3567
3568 BufferedWriter out = null;
3569
3570 // Any uncaught exception will crash the system process
3571 try {
3572 OutputStream clientStream = client.getOutputStream();
3573 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3574
3575 final int count = windows.length;
3576 for (int i = 0; i < count; i++) {
3577 final WindowState w = (WindowState) windows[i];
3578 out.write(Integer.toHexString(System.identityHashCode(w)));
3579 out.write(' ');
3580 out.append(w.mAttrs.getTitle());
3581 out.write('\n');
3582 }
3583
3584 out.write("DONE.\n");
3585 out.flush();
3586 } catch (Exception e) {
3587 result = false;
3588 } finally {
3589 if (out != null) {
3590 try {
3591 out.close();
3592 } catch (IOException e) {
3593 result = false;
3594 }
3595 }
3596 }
3597
3598 return result;
3599 }
3600
3601 /**
3602 * Sends a command to a target window. The result of the command, if any, will be
3603 * written in the output stream of the specified socket.
3604 *
3605 * The parameters must follow this syntax:
3606 * windowHashcode extra
3607 *
3608 * Where XX is the length in characeters of the windowTitle.
3609 *
3610 * The first parameter is the target window. The window with the specified hashcode
3611 * will be the target. If no target can be found, nothing happens. The extra parameters
3612 * will be delivered to the target window and as parameters to the command itself.
3613 *
3614 * @param client The remote client to sent the result, if any, to.
3615 * @param command The command to execute.
3616 * @param parameters The command parameters.
3617 *
3618 * @return True if the command was successfully delivered, false otherwise. This does
3619 * not indicate whether the command itself was successful.
3620 */
3621 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003622 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 return false;
3624 }
3625
3626 boolean success = true;
3627 Parcel data = null;
3628 Parcel reply = null;
3629
3630 // Any uncaught exception will crash the system process
3631 try {
3632 // Find the hashcode of the window
3633 int index = parameters.indexOf(' ');
3634 if (index == -1) {
3635 index = parameters.length();
3636 }
3637 final String code = parameters.substring(0, index);
3638 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3639
3640 // Extract the command's parameter after the window description
3641 if (index < parameters.length()) {
3642 parameters = parameters.substring(index + 1);
3643 } else {
3644 parameters = "";
3645 }
3646
3647 final WindowManagerService.WindowState window = findWindow(hashCode);
3648 if (window == null) {
3649 return false;
3650 }
3651
3652 data = Parcel.obtain();
3653 data.writeInterfaceToken("android.view.IWindow");
3654 data.writeString(command);
3655 data.writeString(parameters);
3656 data.writeInt(1);
3657 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3658
3659 reply = Parcel.obtain();
3660
3661 final IBinder binder = window.mClient.asBinder();
3662 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3663 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3664
3665 reply.readException();
3666
3667 } catch (Exception e) {
3668 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3669 success = false;
3670 } finally {
3671 if (data != null) {
3672 data.recycle();
3673 }
3674 if (reply != null) {
3675 reply.recycle();
3676 }
3677 }
3678
3679 return success;
3680 }
3681
3682 private WindowState findWindow(int hashCode) {
3683 if (hashCode == -1) {
3684 return getFocusedWindow();
3685 }
3686
3687 synchronized (mWindowMap) {
3688 final ArrayList windows = mWindows;
3689 final int count = windows.size();
3690
3691 for (int i = 0; i < count; i++) {
3692 WindowState w = (WindowState) windows.get(i);
3693 if (System.identityHashCode(w) == hashCode) {
3694 return w;
3695 }
3696 }
3697 }
3698
3699 return null;
3700 }
3701
3702 /*
3703 * Instruct the Activity Manager to fetch the current configuration and broadcast
3704 * that to config-changed listeners if appropriate.
3705 */
3706 void sendNewConfiguration() {
3707 try {
3708 mActivityManager.updateConfiguration(null);
3709 } catch (RemoteException e) {
3710 }
3711 }
Romain Guy06882f82009-06-10 13:36:04 -07003712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 public Configuration computeNewConfiguration() {
3714 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003715 return computeNewConfigurationLocked();
3716 }
3717 }
Romain Guy06882f82009-06-10 13:36:04 -07003718
Dianne Hackbornc485a602009-03-24 22:39:49 -07003719 Configuration computeNewConfigurationLocked() {
3720 Configuration config = new Configuration();
3721 if (!computeNewConfigurationLocked(config)) {
3722 return null;
3723 }
3724 Log.i(TAG, "Config changed: " + config);
3725 long now = SystemClock.uptimeMillis();
3726 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3727 if (mFreezeGcPending != 0) {
3728 if (now > (mFreezeGcPending+1000)) {
3729 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3730 mH.removeMessages(H.FORCE_GC);
3731 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 mFreezeGcPending = now;
3733 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003734 } else {
3735 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003737 return config;
3738 }
Romain Guy06882f82009-06-10 13:36:04 -07003739
Dianne Hackbornc485a602009-03-24 22:39:49 -07003740 boolean computeNewConfigurationLocked(Configuration config) {
3741 if (mDisplay == null) {
3742 return false;
3743 }
3744 mQueue.getInputConfiguration(config);
3745 final int dw = mDisplay.getWidth();
3746 final int dh = mDisplay.getHeight();
3747 int orientation = Configuration.ORIENTATION_SQUARE;
3748 if (dw < dh) {
3749 orientation = Configuration.ORIENTATION_PORTRAIT;
3750 } else if (dw > dh) {
3751 orientation = Configuration.ORIENTATION_LANDSCAPE;
3752 }
3753 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003754
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003755 DisplayMetrics dm = new DisplayMetrics();
3756 mDisplay.getMetrics(dm);
3757 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
3758
Dianne Hackborn723738c2009-06-25 19:48:04 -07003759 if (screenLayout == Configuration.SCREENLAYOUT_UNDEFINED) {
3760 // Note we only do this once because at this point we don't
3761 // expect the screen to change in this way at runtime, and want
3762 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07003763 int longSize = dw;
3764 int shortSize = dh;
3765 if (longSize < shortSize) {
3766 int tmp = longSize;
3767 longSize = shortSize;
3768 shortSize = tmp;
3769 }
3770 longSize = (int)(longSize/dm.density);
3771 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003772
Dianne Hackborn723738c2009-06-25 19:48:04 -07003773 // These semi-magic numbers define our compatibility modes for
3774 // applications with different screens. Don't change unless you
3775 // make sure to test lots and lots of apps!
3776 if (longSize < 470) {
3777 // This is shorter than an HVGA normal density screen (which
3778 // is 480 pixels on its long side).
3779 screenLayout = Configuration.SCREENLAYOUT_SMALL;
3780 } else if (longSize > 490 && shortSize > 330) {
3781 // This is larger than an HVGA normal density screen (which
3782 // is 480x320 pixels).
3783 screenLayout = Configuration.SCREENLAYOUT_LARGE;
3784 } else {
3785 screenLayout = Configuration.SCREENLAYOUT_NORMAL;
3786 }
3787 }
3788 config.screenLayout = screenLayout;
3789
Dianne Hackbornc485a602009-03-24 22:39:49 -07003790 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3791 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3792 mPolicy.adjustConfigurationLw(config);
3793 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 }
Romain Guy06882f82009-06-10 13:36:04 -07003795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 // -------------------------------------------------------------
3797 // Input Events and Focus Management
3798 // -------------------------------------------------------------
3799
3800 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07003801 long curTime = SystemClock.uptimeMillis();
3802
Michael Chane10de972009-05-18 11:24:50 -07003803 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07003804 if (mLastTouchEventType == eventType &&
3805 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
3806 return;
3807 }
3808 mLastUserActivityCallTime = curTime;
3809 mLastTouchEventType = eventType;
3810 }
3811
3812 if (targetWin == null
3813 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
3814 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 }
3816 }
3817
3818 // tells if it's a cheek event or not -- this function is stateful
3819 private static final int EVENT_NONE = 0;
3820 private static final int EVENT_UNKNOWN = 0;
3821 private static final int EVENT_CHEEK = 0;
3822 private static final int EVENT_IGNORE_DURATION = 300; // ms
3823 private static final float CHEEK_THRESHOLD = 0.6f;
3824 private int mEventState = EVENT_NONE;
3825 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 private int eventType(MotionEvent ev) {
3828 float size = ev.getSize();
3829 switch (ev.getAction()) {
3830 case MotionEvent.ACTION_DOWN:
3831 mEventSize = size;
3832 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
3833 case MotionEvent.ACTION_UP:
3834 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003835 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 case MotionEvent.ACTION_MOVE:
3837 final int N = ev.getHistorySize();
3838 if (size > mEventSize) mEventSize = size;
3839 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3840 for (int i=0; i<N; i++) {
3841 size = ev.getHistoricalSize(i);
3842 if (size > mEventSize) mEventSize = size;
3843 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3844 }
3845 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
3846 return TOUCH_EVENT;
3847 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003848 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 }
3850 default:
3851 // not good
3852 return OTHER_EVENT;
3853 }
3854 }
3855
3856 /**
3857 * @return Returns true if event was dispatched, false if it was dropped for any reason
3858 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07003859 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
3861 "dispatchPointer " + ev);
3862
Michael Chan53071d62009-05-13 17:29:48 -07003863 if (MEASURE_LATENCY) {
3864 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
3865 }
3866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07003868 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869
Michael Chan53071d62009-05-13 17:29:48 -07003870 if (MEASURE_LATENCY) {
3871 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
3872 }
3873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07003875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 if (action == MotionEvent.ACTION_UP) {
3877 // let go of our target
3878 mKeyWaiter.mMotionTarget = null;
3879 mPowerManager.logPointerUpEvent();
3880 } else if (action == MotionEvent.ACTION_DOWN) {
3881 mPowerManager.logPointerDownEvent();
3882 }
3883
3884 if (targetObj == null) {
3885 // In this case we are either dropping the event, or have received
3886 // a move or up without a down. It is common to receive move
3887 // events in such a way, since this means the user is moving the
3888 // pointer without actually pressing down. All other cases should
3889 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07003890 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
3892 }
3893 if (qev != null) {
3894 mQueue.recycleEvent(qev);
3895 }
3896 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003897 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 }
3899 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
3900 if (qev != null) {
3901 mQueue.recycleEvent(qev);
3902 }
3903 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003904 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 }
Romain Guy06882f82009-06-10 13:36:04 -07003906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07003908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07003910 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911
3912 //Log.i(TAG, "Sending " + ev + " to " + target);
3913
3914 if (uid != 0 && uid != target.mSession.mUid) {
3915 if (mContext.checkPermission(
3916 android.Manifest.permission.INJECT_EVENTS, pid, uid)
3917 != PackageManager.PERMISSION_GRANTED) {
3918 Log.w(TAG, "Permission denied: injecting pointer event from pid "
3919 + pid + " uid " + uid + " to window " + target
3920 + " owned by uid " + target.mSession.mUid);
3921 if (qev != null) {
3922 mQueue.recycleEvent(qev);
3923 }
3924 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003925 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 }
3927 }
3928
Michael Chan53071d62009-05-13 17:29:48 -07003929 if (MEASURE_LATENCY) {
3930 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
3931 }
3932
Romain Guy06882f82009-06-10 13:36:04 -07003933 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
3935 //target wants to ignore fat touch events
3936 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
3937 //explicit flag to return without processing event further
3938 boolean returnFlag = false;
3939 if((action == MotionEvent.ACTION_DOWN)) {
3940 mFatTouch = false;
3941 if(cheekPress) {
3942 mFatTouch = true;
3943 returnFlag = true;
3944 }
3945 } else {
3946 if(action == MotionEvent.ACTION_UP) {
3947 if(mFatTouch) {
3948 //earlier even was invalid doesnt matter if current up is cheekpress or not
3949 mFatTouch = false;
3950 returnFlag = true;
3951 } else if(cheekPress) {
3952 //cancel the earlier event
3953 ev.setAction(MotionEvent.ACTION_CANCEL);
3954 action = MotionEvent.ACTION_CANCEL;
3955 }
3956 } else if(action == MotionEvent.ACTION_MOVE) {
3957 if(mFatTouch) {
3958 //two cases here
3959 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07003960 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 returnFlag = true;
3962 } else if(cheekPress) {
3963 //valid down followed by invalid moves
3964 //an invalid move have to cancel earlier action
3965 ev.setAction(MotionEvent.ACTION_CANCEL);
3966 action = MotionEvent.ACTION_CANCEL;
3967 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
3968 //note that the subsequent invalid moves will not get here
3969 mFatTouch = true;
3970 }
3971 }
3972 } //else if action
3973 if(returnFlag) {
3974 //recycle que, ev
3975 if (qev != null) {
3976 mQueue.recycleEvent(qev);
3977 }
3978 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003979 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 }
3981 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07003982
3983 // TODO remove once we settle on a value or make it app specific
3984 if (action == MotionEvent.ACTION_DOWN) {
3985 int max_events_per_sec = 35;
3986 try {
3987 max_events_per_sec = Integer.parseInt(SystemProperties
3988 .get("windowsmgr.max_events_per_sec"));
3989 if (max_events_per_sec < 1) {
3990 max_events_per_sec = 35;
3991 }
3992 } catch (NumberFormatException e) {
3993 }
3994 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
3995 }
3996
3997 /*
3998 * Throttle events to minimize CPU usage when there's a flood of events
3999 * e.g. constant contact with the screen
4000 */
4001 if (action == MotionEvent.ACTION_MOVE) {
4002 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4003 long now = SystemClock.uptimeMillis();
4004 if (now < nextEventTime) {
4005 try {
4006 Thread.sleep(nextEventTime - now);
4007 } catch (InterruptedException e) {
4008 }
4009 mLastTouchEventTime = nextEventTime;
4010 } else {
4011 mLastTouchEventTime = now;
4012 }
4013 }
4014
Michael Chan53071d62009-05-13 17:29:48 -07004015 if (MEASURE_LATENCY) {
4016 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4017 }
4018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 synchronized(mWindowMap) {
4020 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4021 mKeyWaiter.bindTargetWindowLocked(target,
4022 KeyWaiter.RETURN_PENDING_POINTER, qev);
4023 ev = null;
4024 } else {
4025 if (action == MotionEvent.ACTION_DOWN) {
4026 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4027 if (out != null) {
4028 MotionEvent oev = MotionEvent.obtain(ev);
4029 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4030 do {
4031 final Rect frame = out.mFrame;
4032 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4033 try {
4034 out.mClient.dispatchPointer(oev, eventTime);
4035 } catch (android.os.RemoteException e) {
4036 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4037 }
4038 oev.offsetLocation((float)frame.left, (float)frame.top);
4039 out = out.mNextOutsideTouch;
4040 } while (out != null);
4041 mKeyWaiter.mOutsideTouchTargets = null;
4042 }
4043 }
4044 final Rect frame = target.mFrame;
4045 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4046 mKeyWaiter.bindTargetWindowLocked(target);
4047 }
4048 }
Romain Guy06882f82009-06-10 13:36:04 -07004049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 // finally offset the event to the target's coordinate system and
4051 // dispatch the event.
4052 try {
4053 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4054 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4055 }
Michael Chan53071d62009-05-13 17:29:48 -07004056
4057 if (MEASURE_LATENCY) {
4058 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4059 }
4060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 target.mClient.dispatchPointer(ev, eventTime);
Michael Chan53071d62009-05-13 17:29:48 -07004062
4063 if (MEASURE_LATENCY) {
4064 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4065 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004066 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 } catch (android.os.RemoteException e) {
4068 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4069 mKeyWaiter.mMotionTarget = null;
4070 try {
4071 removeWindow(target.mSession, target.mClient);
4072 } catch (java.util.NoSuchElementException ex) {
4073 // This will happen if the window has already been
4074 // removed.
4075 }
4076 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004077 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 }
Romain Guy06882f82009-06-10 13:36:04 -07004079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 /**
4081 * @return Returns true if event was dispatched, false if it was dropped for any reason
4082 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004083 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 if (DEBUG_INPUT) Log.v(
4085 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004088 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 if (focusObj == null) {
4090 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4091 if (qev != null) {
4092 mQueue.recycleEvent(qev);
4093 }
4094 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004095 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 }
4097 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4098 if (qev != null) {
4099 mQueue.recycleEvent(qev);
4100 }
4101 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004102 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 }
Romain Guy06882f82009-06-10 13:36:04 -07004104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 if (uid != 0 && uid != focus.mSession.mUid) {
4108 if (mContext.checkPermission(
4109 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4110 != PackageManager.PERMISSION_GRANTED) {
4111 Log.w(TAG, "Permission denied: injecting key event from pid "
4112 + pid + " uid " + uid + " to window " + focus
4113 + " owned by uid " + focus.mSession.mUid);
4114 if (qev != null) {
4115 mQueue.recycleEvent(qev);
4116 }
4117 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004118 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 }
4120 }
Romain Guy06882f82009-06-10 13:36:04 -07004121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 synchronized(mWindowMap) {
4125 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4126 mKeyWaiter.bindTargetWindowLocked(focus,
4127 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4128 // We don't deliver movement events to the client, we hold
4129 // them and wait for them to call back.
4130 ev = null;
4131 } else {
4132 mKeyWaiter.bindTargetWindowLocked(focus);
4133 }
4134 }
Romain Guy06882f82009-06-10 13:36:04 -07004135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 try {
4137 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004138 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 } catch (android.os.RemoteException e) {
4140 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4141 try {
4142 removeWindow(focus.mSession, focus.mClient);
4143 } catch (java.util.NoSuchElementException ex) {
4144 // This will happen if the window has already been
4145 // removed.
4146 }
4147 }
Romain Guy06882f82009-06-10 13:36:04 -07004148
Dianne Hackborncfaef692009-06-15 14:24:44 -07004149 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 }
Romain Guy06882f82009-06-10 13:36:04 -07004151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 /**
4153 * @return Returns true if event was dispatched, false if it was dropped for any reason
4154 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004155 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4157
4158 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004159 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 if (focusObj == null) {
4161 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004162 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 }
4164 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004165 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 }
Romain Guy06882f82009-06-10 13:36:04 -07004167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 if (DEBUG_INPUT) Log.v(
4171 TAG, "Dispatching to " + focus + ": " + event);
4172
4173 if (uid != 0 && uid != focus.mSession.mUid) {
4174 if (mContext.checkPermission(
4175 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4176 != PackageManager.PERMISSION_GRANTED) {
4177 Log.w(TAG, "Permission denied: injecting key event from pid "
4178 + pid + " uid " + uid + " to window " + focus
4179 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004180 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 }
4182 }
Romain Guy06882f82009-06-10 13:36:04 -07004183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184 synchronized(mWindowMap) {
4185 mKeyWaiter.bindTargetWindowLocked(focus);
4186 }
4187
4188 // NOSHIP extra state logging
4189 mKeyWaiter.recordDispatchState(event, focus);
4190 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 try {
4193 if (DEBUG_INPUT || DEBUG_FOCUS) {
4194 Log.v(TAG, "Delivering key " + event.getKeyCode()
4195 + " to " + focus);
4196 }
4197 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004198 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 } catch (android.os.RemoteException e) {
4200 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4201 try {
4202 removeWindow(focus.mSession, focus.mClient);
4203 } catch (java.util.NoSuchElementException ex) {
4204 // This will happen if the window has already been
4205 // removed.
4206 }
4207 }
Romain Guy06882f82009-06-10 13:36:04 -07004208
Dianne Hackborncfaef692009-06-15 14:24:44 -07004209 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 }
Romain Guy06882f82009-06-10 13:36:04 -07004211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 public void pauseKeyDispatching(IBinder _token) {
4213 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4214 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004215 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 }
4217
4218 synchronized (mWindowMap) {
4219 WindowToken token = mTokenMap.get(_token);
4220 if (token != null) {
4221 mKeyWaiter.pauseDispatchingLocked(token);
4222 }
4223 }
4224 }
4225
4226 public void resumeKeyDispatching(IBinder _token) {
4227 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4228 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004229 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 }
4231
4232 synchronized (mWindowMap) {
4233 WindowToken token = mTokenMap.get(_token);
4234 if (token != null) {
4235 mKeyWaiter.resumeDispatchingLocked(token);
4236 }
4237 }
4238 }
4239
4240 public void setEventDispatching(boolean enabled) {
4241 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4242 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004243 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 }
4245
4246 synchronized (mWindowMap) {
4247 mKeyWaiter.setEventDispatchingLocked(enabled);
4248 }
4249 }
Romain Guy06882f82009-06-10 13:36:04 -07004250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 /**
4252 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004253 *
4254 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 * {@link SystemClock#uptimeMillis()} as the timebase.)
4256 * @param sync If true, wait for the event to be completed before returning to the caller.
4257 * @return Returns true if event was dispatched, false if it was dropped for any reason
4258 */
4259 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4260 long downTime = ev.getDownTime();
4261 long eventTime = ev.getEventTime();
4262
4263 int action = ev.getAction();
4264 int code = ev.getKeyCode();
4265 int repeatCount = ev.getRepeatCount();
4266 int metaState = ev.getMetaState();
4267 int deviceId = ev.getDeviceId();
4268 int scancode = ev.getScanCode();
4269
4270 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4271 if (downTime == 0) downTime = eventTime;
4272
4273 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004274 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004276 final int pid = Binder.getCallingPid();
4277 final int uid = Binder.getCallingUid();
4278 final long ident = Binder.clearCallingIdentity();
4279 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004281 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004282 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004283 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004284 switch (result) {
4285 case INJECT_NO_PERMISSION:
4286 throw new SecurityException(
4287 "Injecting to another application requires INJECT_EVENT permission");
4288 case INJECT_SUCCEEDED:
4289 return true;
4290 }
4291 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 }
4293
4294 /**
4295 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004296 *
4297 * @param ev A motion event describing the pointer (touch) action. (As noted in
4298 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 * {@link SystemClock#uptimeMillis()} as the timebase.)
4300 * @param sync If true, wait for the event to be completed before returning to the caller.
4301 * @return Returns true if event was dispatched, false if it was dropped for any reason
4302 */
4303 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004304 final int pid = Binder.getCallingPid();
4305 final int uid = Binder.getCallingUid();
4306 final long ident = Binder.clearCallingIdentity();
4307 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004308 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004309 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004311 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004312 switch (result) {
4313 case INJECT_NO_PERMISSION:
4314 throw new SecurityException(
4315 "Injecting to another application requires INJECT_EVENT permission");
4316 case INJECT_SUCCEEDED:
4317 return true;
4318 }
4319 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 }
Romain Guy06882f82009-06-10 13:36:04 -07004321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 /**
4323 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004324 *
4325 * @param ev A motion event describing the trackball action. (As noted in
4326 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 * {@link SystemClock#uptimeMillis()} as the timebase.)
4328 * @param sync If true, wait for the event to be completed before returning to the caller.
4329 * @return Returns true if event was dispatched, false if it was dropped for any reason
4330 */
4331 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004332 final int pid = Binder.getCallingPid();
4333 final int uid = Binder.getCallingUid();
4334 final long ident = Binder.clearCallingIdentity();
4335 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004337 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004339 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004340 switch (result) {
4341 case INJECT_NO_PERMISSION:
4342 throw new SecurityException(
4343 "Injecting to another application requires INJECT_EVENT permission");
4344 case INJECT_SUCCEEDED:
4345 return true;
4346 }
4347 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 }
Romain Guy06882f82009-06-10 13:36:04 -07004349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 private WindowState getFocusedWindow() {
4351 synchronized (mWindowMap) {
4352 return getFocusedWindowLocked();
4353 }
4354 }
4355
4356 private WindowState getFocusedWindowLocked() {
4357 return mCurrentFocus;
4358 }
Romain Guy06882f82009-06-10 13:36:04 -07004359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 /**
4361 * This class holds the state for dispatching key events. This state
4362 * is protected by the KeyWaiter instance, NOT by the window lock. You
4363 * can be holding the main window lock while acquire the KeyWaiter lock,
4364 * but not the other way around.
4365 */
4366 final class KeyWaiter {
4367 // NOSHIP debugging
4368 public class DispatchState {
4369 private KeyEvent event;
4370 private WindowState focus;
4371 private long time;
4372 private WindowState lastWin;
4373 private IBinder lastBinder;
4374 private boolean finished;
4375 private boolean gotFirstWindow;
4376 private boolean eventDispatching;
4377 private long timeToSwitch;
4378 private boolean wasFrozen;
4379 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004380 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4383 focus = theFocus;
4384 event = theEvent;
4385 time = System.currentTimeMillis();
4386 // snapshot KeyWaiter state
4387 lastWin = mLastWin;
4388 lastBinder = mLastBinder;
4389 finished = mFinished;
4390 gotFirstWindow = mGotFirstWindow;
4391 eventDispatching = mEventDispatching;
4392 timeToSwitch = mTimeToSwitch;
4393 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004394 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 // cache the paused state at ctor time as well
4396 if (theFocus == null || theFocus.mToken == null) {
4397 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4398 focusPaused = false;
4399 } else {
4400 focusPaused = theFocus.mToken.paused;
4401 }
4402 }
Romain Guy06882f82009-06-10 13:36:04 -07004403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 public String toString() {
4405 return "{{" + event + " to " + focus + " @ " + time
4406 + " lw=" + lastWin + " lb=" + lastBinder
4407 + " fin=" + finished + " gfw=" + gotFirstWindow
4408 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004409 + " wf=" + wasFrozen + " fp=" + focusPaused
4410 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 }
4412 };
4413 private DispatchState mDispatchState = null;
4414 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4415 mDispatchState = new DispatchState(theEvent, theFocus);
4416 }
4417 // END NOSHIP
4418
4419 public static final int RETURN_NOTHING = 0;
4420 public static final int RETURN_PENDING_POINTER = 1;
4421 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 final Object SKIP_TARGET_TOKEN = new Object();
4424 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004426 private WindowState mLastWin = null;
4427 private IBinder mLastBinder = null;
4428 private boolean mFinished = true;
4429 private boolean mGotFirstWindow = false;
4430 private boolean mEventDispatching = true;
4431 private long mTimeToSwitch = 0;
4432 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 // Target of Motion events
4435 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437 // Windows above the target who would like to receive an "outside"
4438 // touch event for any down events outside of them.
4439 WindowState mOutsideTouchTargets;
4440
4441 /**
4442 * Wait for the last event dispatch to complete, then find the next
4443 * target that should receive the given event and wait for that one
4444 * to be ready to receive it.
4445 */
4446 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4447 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004448 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 long startTime = SystemClock.uptimeMillis();
4450 long keyDispatchingTimeout = 5 * 1000;
4451 long waitedFor = 0;
4452
4453 while (true) {
4454 // Figure out which window we care about. It is either the
4455 // last window we are waiting to have process the event or,
4456 // if none, then the next window we think the event should go
4457 // to. Note: we retrieve mLastWin outside of the lock, so
4458 // it may change before we lock. Thus we must check it again.
4459 WindowState targetWin = mLastWin;
4460 boolean targetIsNew = targetWin == null;
4461 if (DEBUG_INPUT) Log.v(
4462 TAG, "waitForLastKey: mFinished=" + mFinished +
4463 ", mLastWin=" + mLastWin);
4464 if (targetIsNew) {
4465 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004466 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467 if (target == SKIP_TARGET_TOKEN) {
4468 // The user has pressed a special key, and we are
4469 // dropping all pending events before it.
4470 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4471 + " " + nextMotion);
4472 return null;
4473 }
4474 if (target == CONSUMED_EVENT_TOKEN) {
4475 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4476 + " " + nextMotion);
4477 return target;
4478 }
4479 targetWin = (WindowState)target;
4480 }
Romain Guy06882f82009-06-10 13:36:04 -07004481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004484 // Now: is it okay to send the next event to this window?
4485 synchronized (this) {
4486 // First: did we come here based on the last window not
4487 // being null, but it changed by the time we got here?
4488 // If so, try again.
4489 if (!targetIsNew && mLastWin == null) {
4490 continue;
4491 }
Romain Guy06882f82009-06-10 13:36:04 -07004492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 // We never dispatch events if not finished with the
4494 // last one, or the display is frozen.
4495 if (mFinished && !mDisplayFrozen) {
4496 // If event dispatching is disabled, then we
4497 // just consume the events.
4498 if (!mEventDispatching) {
4499 if (DEBUG_INPUT) Log.v(TAG,
4500 "Skipping event; dispatching disabled: "
4501 + nextKey + " " + nextMotion);
4502 return null;
4503 }
4504 if (targetWin != null) {
4505 // If this is a new target, and that target is not
4506 // paused or unresponsive, then all looks good to
4507 // handle the event.
4508 if (targetIsNew && !targetWin.mToken.paused) {
4509 return targetWin;
4510 }
Romain Guy06882f82009-06-10 13:36:04 -07004511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 // If we didn't find a target window, and there is no
4513 // focused app window, then just eat the events.
4514 } else if (mFocusedApp == null) {
4515 if (DEBUG_INPUT) Log.v(TAG,
4516 "Skipping event; no focused app: "
4517 + nextKey + " " + nextMotion);
4518 return null;
4519 }
4520 }
Romain Guy06882f82009-06-10 13:36:04 -07004521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 if (DEBUG_INPUT) Log.v(
4523 TAG, "Waiting for last key in " + mLastBinder
4524 + " target=" + targetWin
4525 + " mFinished=" + mFinished
4526 + " mDisplayFrozen=" + mDisplayFrozen
4527 + " targetIsNew=" + targetIsNew
4528 + " paused="
4529 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004530 + " mFocusedApp=" + mFocusedApp
4531 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004533 targetApp = targetWin != null
4534 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 long curTimeout = keyDispatchingTimeout;
4537 if (mTimeToSwitch != 0) {
4538 long now = SystemClock.uptimeMillis();
4539 if (mTimeToSwitch <= now) {
4540 // If an app switch key has been pressed, and we have
4541 // waited too long for the current app to finish
4542 // processing keys, then wait no more!
4543 doFinishedKeyLocked(true);
4544 continue;
4545 }
4546 long switchTimeout = mTimeToSwitch - now;
4547 if (curTimeout > switchTimeout) {
4548 curTimeout = switchTimeout;
4549 }
4550 }
Romain Guy06882f82009-06-10 13:36:04 -07004551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552 try {
4553 // after that continue
4554 // processing keys, so we don't get stuck.
4555 if (DEBUG_INPUT) Log.v(
4556 TAG, "Waiting for key dispatch: " + curTimeout);
4557 wait(curTimeout);
4558 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4559 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004560 + startTime + " switchTime=" + mTimeToSwitch
4561 + " target=" + targetWin + " mLW=" + mLastWin
4562 + " mLB=" + mLastBinder + " fin=" + mFinished
4563 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004564 } catch (InterruptedException e) {
4565 }
4566 }
4567
4568 // If we were frozen during configuration change, restart the
4569 // timeout checks from now; otherwise look at whether we timed
4570 // out before awakening.
4571 if (mWasFrozen) {
4572 waitedFor = 0;
4573 mWasFrozen = false;
4574 } else {
4575 waitedFor = SystemClock.uptimeMillis() - startTime;
4576 }
4577
4578 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4579 IApplicationToken at = null;
4580 synchronized (this) {
4581 Log.w(TAG, "Key dispatching timed out sending to " +
4582 (targetWin != null ? targetWin.mAttrs.getTitle()
4583 : "<null>"));
4584 // NOSHIP debugging
4585 Log.w(TAG, "Dispatch state: " + mDispatchState);
4586 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4587 // END NOSHIP
4588 //dump();
4589 if (targetWin != null) {
4590 at = targetWin.getAppToken();
4591 } else if (targetApp != null) {
4592 at = targetApp.appToken;
4593 }
4594 }
4595
4596 boolean abort = true;
4597 if (at != null) {
4598 try {
4599 long timeout = at.getKeyDispatchingTimeout();
4600 if (timeout > waitedFor) {
4601 // we did not wait the proper amount of time for this application.
4602 // set the timeout to be the real timeout and wait again.
4603 keyDispatchingTimeout = timeout - waitedFor;
4604 continue;
4605 } else {
4606 abort = at.keyDispatchingTimedOut();
4607 }
4608 } catch (RemoteException ex) {
4609 }
4610 }
4611
4612 synchronized (this) {
4613 if (abort && (mLastWin == targetWin || targetWin == null)) {
4614 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004615 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 if (DEBUG_INPUT) Log.v(TAG,
4617 "Window " + mLastWin +
4618 " timed out on key input");
4619 if (mLastWin.mToken.paused) {
4620 Log.w(TAG, "Un-pausing dispatching to this window");
4621 mLastWin.mToken.paused = false;
4622 }
4623 }
4624 if (mMotionTarget == targetWin) {
4625 mMotionTarget = null;
4626 }
4627 mLastWin = null;
4628 mLastBinder = null;
4629 if (failIfTimeout || targetWin == null) {
4630 return null;
4631 }
4632 } else {
4633 Log.w(TAG, "Continuing to wait for key to be dispatched");
4634 startTime = SystemClock.uptimeMillis();
4635 }
4636 }
4637 }
4638 }
4639 }
Romain Guy06882f82009-06-10 13:36:04 -07004640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004642 MotionEvent nextMotion, boolean isPointerEvent,
4643 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 if (nextKey != null) {
4647 // Find the target window for a normal key event.
4648 final int keycode = nextKey.getKeyCode();
4649 final int repeatCount = nextKey.getRepeatCount();
4650 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4651 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004654 if (callingUid == 0 ||
4655 mContext.checkPermission(
4656 android.Manifest.permission.INJECT_EVENTS,
4657 callingPid, callingUid)
4658 == PackageManager.PERMISSION_GRANTED) {
4659 mPolicy.interceptKeyTi(null, keycode,
4660 nextKey.getMetaState(), down, repeatCount);
4661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004662 Log.w(TAG, "Event timeout during app switch: dropping "
4663 + nextKey);
4664 return SKIP_TARGET_TOKEN;
4665 }
Romain Guy06882f82009-06-10 13:36:04 -07004666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004667 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 WindowState focus = null;
4670 synchronized(mWindowMap) {
4671 focus = getFocusedWindowLocked();
4672 }
Romain Guy06882f82009-06-10 13:36:04 -07004673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004675
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004676 if (callingUid == 0 ||
4677 (focus != null && callingUid == focus.mSession.mUid) ||
4678 mContext.checkPermission(
4679 android.Manifest.permission.INJECT_EVENTS,
4680 callingPid, callingUid)
4681 == PackageManager.PERMISSION_GRANTED) {
4682 if (mPolicy.interceptKeyTi(focus,
4683 keycode, nextKey.getMetaState(), down, repeatCount)) {
4684 return CONSUMED_EVENT_TOKEN;
4685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 }
Romain Guy06882f82009-06-10 13:36:04 -07004687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 } else if (!isPointerEvent) {
4691 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4692 if (!dispatch) {
4693 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4694 + nextMotion);
4695 return SKIP_TARGET_TOKEN;
4696 }
Romain Guy06882f82009-06-10 13:36:04 -07004697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 WindowState focus = null;
4699 synchronized(mWindowMap) {
4700 focus = getFocusedWindowLocked();
4701 }
Romain Guy06882f82009-06-10 13:36:04 -07004702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4704 return focus;
4705 }
Romain Guy06882f82009-06-10 13:36:04 -07004706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 if (nextMotion == null) {
4708 return SKIP_TARGET_TOKEN;
4709 }
Romain Guy06882f82009-06-10 13:36:04 -07004710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4712 KeyEvent.KEYCODE_UNKNOWN);
4713 if (!dispatch) {
4714 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4715 + nextMotion);
4716 return SKIP_TARGET_TOKEN;
4717 }
Romain Guy06882f82009-06-10 13:36:04 -07004718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004719 // Find the target window for a pointer event.
4720 int action = nextMotion.getAction();
4721 final float xf = nextMotion.getX();
4722 final float yf = nextMotion.getY();
4723 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 final boolean screenWasOff = qev != null
4726 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07004727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004728 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07004729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004730 synchronized(mWindowMap) {
4731 synchronized (this) {
4732 if (action == MotionEvent.ACTION_DOWN) {
4733 if (mMotionTarget != null) {
4734 // this is weird, we got a pen down, but we thought it was
4735 // already down!
4736 // XXX: We should probably send an ACTION_UP to the current
4737 // target.
4738 Log.w(TAG, "Pointer down received while already down in: "
4739 + mMotionTarget);
4740 mMotionTarget = null;
4741 }
Romain Guy06882f82009-06-10 13:36:04 -07004742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004743 // ACTION_DOWN is special, because we need to lock next events to
4744 // the window we'll land onto.
4745 final int x = (int)xf;
4746 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07004747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004748 final ArrayList windows = mWindows;
4749 final int N = windows.size();
4750 WindowState topErrWindow = null;
4751 final Rect tmpRect = mTempRect;
4752 for (int i=N-1; i>=0; i--) {
4753 WindowState child = (WindowState)windows.get(i);
4754 //Log.i(TAG, "Checking dispatch to: " + child);
4755 final int flags = child.mAttrs.flags;
4756 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4757 if (topErrWindow == null) {
4758 topErrWindow = child;
4759 }
4760 }
4761 if (!child.isVisibleLw()) {
4762 //Log.i(TAG, "Not visible!");
4763 continue;
4764 }
4765 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4766 //Log.i(TAG, "Not touchable!");
4767 if ((flags & WindowManager.LayoutParams
4768 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4769 child.mNextOutsideTouch = mOutsideTouchTargets;
4770 mOutsideTouchTargets = child;
4771 }
4772 continue;
4773 }
4774 tmpRect.set(child.mFrame);
4775 if (child.mTouchableInsets == ViewTreeObserver
4776 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4777 // The touch is inside of the window if it is
4778 // inside the frame, AND the content part of that
4779 // frame that was given by the application.
4780 tmpRect.left += child.mGivenContentInsets.left;
4781 tmpRect.top += child.mGivenContentInsets.top;
4782 tmpRect.right -= child.mGivenContentInsets.right;
4783 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4784 } else if (child.mTouchableInsets == ViewTreeObserver
4785 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4786 // The touch is inside of the window if it is
4787 // inside the frame, AND the visible part of that
4788 // frame that was given by the application.
4789 tmpRect.left += child.mGivenVisibleInsets.left;
4790 tmpRect.top += child.mGivenVisibleInsets.top;
4791 tmpRect.right -= child.mGivenVisibleInsets.right;
4792 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
4793 }
4794 final int touchFlags = flags &
4795 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
4796 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
4797 if (tmpRect.contains(x, y) || touchFlags == 0) {
4798 //Log.i(TAG, "Using this target!");
4799 if (!screenWasOff || (flags &
4800 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
4801 mMotionTarget = child;
4802 } else {
4803 //Log.i(TAG, "Waking, skip!");
4804 mMotionTarget = null;
4805 }
4806 break;
4807 }
Romain Guy06882f82009-06-10 13:36:04 -07004808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004809 if ((flags & WindowManager.LayoutParams
4810 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4811 child.mNextOutsideTouch = mOutsideTouchTargets;
4812 mOutsideTouchTargets = child;
4813 //Log.i(TAG, "Adding to outside target list: " + child);
4814 }
4815 }
4816
4817 // if there's an error window but it's not accepting
4818 // focus (typically because it is not yet visible) just
4819 // wait for it -- any other focused window may in fact
4820 // be in ANR state.
4821 if (topErrWindow != null && mMotionTarget != topErrWindow) {
4822 mMotionTarget = null;
4823 }
4824 }
Romain Guy06882f82009-06-10 13:36:04 -07004825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826 target = mMotionTarget;
4827 }
4828 }
Romain Guy06882f82009-06-10 13:36:04 -07004829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07004831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 // Pointer events are a little different -- if there isn't a
4833 // target found for any event, then just drop it.
4834 return target != null ? target : SKIP_TARGET_TOKEN;
4835 }
Romain Guy06882f82009-06-10 13:36:04 -07004836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004837 boolean checkShouldDispatchKey(int keycode) {
4838 synchronized (this) {
4839 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
4840 mTimeToSwitch = 0;
4841 return true;
4842 }
4843 if (mTimeToSwitch != 0
4844 && mTimeToSwitch < SystemClock.uptimeMillis()) {
4845 return false;
4846 }
4847 return true;
4848 }
4849 }
Romain Guy06882f82009-06-10 13:36:04 -07004850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004851 void bindTargetWindowLocked(WindowState win,
4852 int pendingWhat, QueuedEvent pendingMotion) {
4853 synchronized (this) {
4854 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
4855 }
4856 }
Romain Guy06882f82009-06-10 13:36:04 -07004857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004858 void bindTargetWindowLocked(WindowState win) {
4859 synchronized (this) {
4860 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
4861 }
4862 }
4863
4864 void bindTargetWindowLockedLocked(WindowState win,
4865 int pendingWhat, QueuedEvent pendingMotion) {
4866 mLastWin = win;
4867 mLastBinder = win.mClient.asBinder();
4868 mFinished = false;
4869 if (pendingMotion != null) {
4870 final Session s = win.mSession;
4871 if (pendingWhat == RETURN_PENDING_POINTER) {
4872 releasePendingPointerLocked(s);
4873 s.mPendingPointerMove = pendingMotion;
4874 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07004875 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 "bindTargetToWindow " + s.mPendingPointerMove);
4877 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
4878 releasePendingTrackballLocked(s);
4879 s.mPendingTrackballMove = pendingMotion;
4880 s.mPendingTrackballWindow = win;
4881 }
4882 }
4883 }
Romain Guy06882f82009-06-10 13:36:04 -07004884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 void releasePendingPointerLocked(Session s) {
4886 if (DEBUG_INPUT) Log.v(TAG,
4887 "releasePendingPointer " + s.mPendingPointerMove);
4888 if (s.mPendingPointerMove != null) {
4889 mQueue.recycleEvent(s.mPendingPointerMove);
4890 s.mPendingPointerMove = null;
4891 }
4892 }
Romain Guy06882f82009-06-10 13:36:04 -07004893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 void releasePendingTrackballLocked(Session s) {
4895 if (s.mPendingTrackballMove != null) {
4896 mQueue.recycleEvent(s.mPendingTrackballMove);
4897 s.mPendingTrackballMove = null;
4898 }
4899 }
Romain Guy06882f82009-06-10 13:36:04 -07004900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 MotionEvent finishedKey(Session session, IWindow client, boolean force,
4902 int returnWhat) {
4903 if (DEBUG_INPUT) Log.v(
4904 TAG, "finishedKey: client=" + client + ", force=" + force);
4905
4906 if (client == null) {
4907 return null;
4908 }
4909
4910 synchronized (this) {
4911 if (DEBUG_INPUT) Log.v(
4912 TAG, "finishedKey: client=" + client.asBinder()
4913 + ", force=" + force + ", last=" + mLastBinder
4914 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
4915
4916 QueuedEvent qev = null;
4917 WindowState win = null;
4918 if (returnWhat == RETURN_PENDING_POINTER) {
4919 qev = session.mPendingPointerMove;
4920 win = session.mPendingPointerWindow;
4921 session.mPendingPointerMove = null;
4922 session.mPendingPointerWindow = null;
4923 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
4924 qev = session.mPendingTrackballMove;
4925 win = session.mPendingTrackballWindow;
4926 session.mPendingTrackballMove = null;
4927 session.mPendingTrackballWindow = null;
4928 }
Romain Guy06882f82009-06-10 13:36:04 -07004929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 if (mLastBinder == client.asBinder()) {
4931 if (DEBUG_INPUT) Log.v(
4932 TAG, "finishedKey: last paused="
4933 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
4934 if (mLastWin != null && (!mLastWin.mToken.paused || force
4935 || !mEventDispatching)) {
4936 doFinishedKeyLocked(false);
4937 } else {
4938 // Make sure to wake up anyone currently waiting to
4939 // dispatch a key, so they can re-evaluate their
4940 // current situation.
4941 mFinished = true;
4942 notifyAll();
4943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 }
Romain Guy06882f82009-06-10 13:36:04 -07004945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004946 if (qev != null) {
4947 MotionEvent res = (MotionEvent)qev.event;
4948 if (DEBUG_INPUT) Log.v(TAG,
4949 "Returning pending motion: " + res);
4950 mQueue.recycleEvent(qev);
4951 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
4952 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
4953 }
4954 return res;
4955 }
4956 return null;
4957 }
4958 }
4959
4960 void tickle() {
4961 synchronized (this) {
4962 notifyAll();
4963 }
4964 }
Romain Guy06882f82009-06-10 13:36:04 -07004965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 void handleNewWindowLocked(WindowState newWindow) {
4967 if (!newWindow.canReceiveKeys()) {
4968 return;
4969 }
4970 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004971 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004972 TAG, "New key dispatch window: win="
4973 + newWindow.mClient.asBinder()
4974 + ", last=" + mLastBinder
4975 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
4976 + "), finished=" + mFinished + ", paused="
4977 + newWindow.mToken.paused);
4978
4979 // Displaying a window implicitly causes dispatching to
4980 // be unpaused. (This is to protect against bugs if someone
4981 // pauses dispatching but forgets to resume.)
4982 newWindow.mToken.paused = false;
4983
4984 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985
4986 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
4987 if (DEBUG_INPUT) Log.v(TAG,
4988 "New SYSTEM_ERROR window; resetting state");
4989 mLastWin = null;
4990 mLastBinder = null;
4991 mMotionTarget = null;
4992 mFinished = true;
4993 } else if (mLastWin != null) {
4994 // If the new window is above the window we are
4995 // waiting on, then stop waiting and let key dispatching
4996 // start on the new guy.
4997 if (DEBUG_INPUT) Log.v(
4998 TAG, "Last win layer=" + mLastWin.mLayer
4999 + ", new win layer=" + newWindow.mLayer);
5000 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005001 // The new window is above the old; finish pending input to the last
5002 // window and start directing it to the new one.
5003 mLastWin.mToken.paused = false;
5004 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005005 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005006 // Either the new window is lower, so there is no need to wake key waiters,
5007 // or we just finished key input to the previous window, which implicitly
5008 // notified the key waiters. In both cases, we don't need to issue the
5009 // notification here.
5010 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 }
5012
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005013 // Now that we've put a new window state in place, make the event waiter
5014 // take notice and retarget its attentions.
5015 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 }
5017 }
5018
5019 void pauseDispatchingLocked(WindowToken token) {
5020 synchronized (this)
5021 {
5022 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5023 token.paused = true;
5024
5025 /*
5026 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5027 mPaused = true;
5028 } else {
5029 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005030 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005032 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005034 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 }
5036 }
5037 */
5038 }
5039 }
5040
5041 void resumeDispatchingLocked(WindowToken token) {
5042 synchronized (this) {
5043 if (token.paused) {
5044 if (DEBUG_INPUT) Log.v(
5045 TAG, "Resuming WindowToken " + token
5046 + ", last=" + mLastBinder
5047 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5048 + "), finished=" + mFinished + ", paused="
5049 + token.paused);
5050 token.paused = false;
5051 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5052 doFinishedKeyLocked(true);
5053 } else {
5054 notifyAll();
5055 }
5056 }
5057 }
5058 }
5059
5060 void setEventDispatchingLocked(boolean enabled) {
5061 synchronized (this) {
5062 mEventDispatching = enabled;
5063 notifyAll();
5064 }
5065 }
Romain Guy06882f82009-06-10 13:36:04 -07005066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005067 void appSwitchComing() {
5068 synchronized (this) {
5069 // Don't wait for more than .5 seconds for app to finish
5070 // processing the pending events.
5071 long now = SystemClock.uptimeMillis() + 500;
5072 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5073 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5074 mTimeToSwitch = now;
5075 }
5076 notifyAll();
5077 }
5078 }
Romain Guy06882f82009-06-10 13:36:04 -07005079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 private final void doFinishedKeyLocked(boolean doRecycle) {
5081 if (mLastWin != null) {
5082 releasePendingPointerLocked(mLastWin.mSession);
5083 releasePendingTrackballLocked(mLastWin.mSession);
5084 }
Romain Guy06882f82009-06-10 13:36:04 -07005085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 if (mLastWin == null || !mLastWin.mToken.paused
5087 || !mLastWin.isVisibleLw()) {
5088 // If the current window has been paused, we aren't -really-
5089 // finished... so let the waiters still wait.
5090 mLastWin = null;
5091 mLastBinder = null;
5092 }
5093 mFinished = true;
5094 notifyAll();
5095 }
5096 }
5097
5098 private class KeyQ extends KeyInputQueue
5099 implements KeyInputQueue.FilterCallback {
5100 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005102 KeyQ() {
5103 super(mContext);
5104 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5105 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5106 "KEEP_SCREEN_ON_FLAG");
5107 mHoldingScreen.setReferenceCounted(false);
5108 }
5109
5110 @Override
5111 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5112 if (mPolicy.preprocessInputEventTq(event)) {
5113 return true;
5114 }
Romain Guy06882f82009-06-10 13:36:04 -07005115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 switch (event.type) {
5117 case RawInputEvent.EV_KEY: {
5118 // XXX begin hack
5119 if (DEBUG) {
5120 if (event.keycode == KeyEvent.KEYCODE_G) {
5121 if (event.value != 0) {
5122 // G down
5123 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5124 }
5125 return false;
5126 }
5127 if (event.keycode == KeyEvent.KEYCODE_D) {
5128 if (event.value != 0) {
5129 //dump();
5130 }
5131 return false;
5132 }
5133 }
5134 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005136 boolean screenIsOff = !mPowerManager.screenIsOn();
5137 boolean screenIsDim = !mPowerManager.screenIsBright();
5138 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005140 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5141 mPowerManager.goToSleep(event.when);
5142 }
5143
5144 if (screenIsOff) {
5145 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5146 }
5147 if (screenIsDim) {
5148 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5149 }
5150 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5151 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005152 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153 }
Romain Guy06882f82009-06-10 13:36:04 -07005154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5156 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5157 filterQueue(this);
5158 mKeyWaiter.appSwitchComing();
5159 }
5160 return true;
5161 } else {
5162 return false;
5163 }
5164 }
Romain Guy06882f82009-06-10 13:36:04 -07005165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 case RawInputEvent.EV_REL: {
5167 boolean screenIsOff = !mPowerManager.screenIsOn();
5168 boolean screenIsDim = !mPowerManager.screenIsBright();
5169 if (screenIsOff) {
5170 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5171 device.classes, event)) {
5172 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5173 return false;
5174 }
5175 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5176 }
5177 if (screenIsDim) {
5178 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5179 }
5180 return true;
5181 }
Romain Guy06882f82009-06-10 13:36:04 -07005182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005183 case RawInputEvent.EV_ABS: {
5184 boolean screenIsOff = !mPowerManager.screenIsOn();
5185 boolean screenIsDim = !mPowerManager.screenIsBright();
5186 if (screenIsOff) {
5187 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5188 device.classes, event)) {
5189 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5190 return false;
5191 }
5192 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5193 }
5194 if (screenIsDim) {
5195 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5196 }
5197 return true;
5198 }
Romain Guy06882f82009-06-10 13:36:04 -07005199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 default:
5201 return true;
5202 }
5203 }
5204
5205 public int filterEvent(QueuedEvent ev) {
5206 switch (ev.classType) {
5207 case RawInputEvent.CLASS_KEYBOARD:
5208 KeyEvent ke = (KeyEvent)ev.event;
5209 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5210 Log.w(TAG, "Dropping movement key during app switch: "
5211 + ke.getKeyCode() + ", action=" + ke.getAction());
5212 return FILTER_REMOVE;
5213 }
5214 return FILTER_ABORT;
5215 default:
5216 return FILTER_KEEP;
5217 }
5218 }
Romain Guy06882f82009-06-10 13:36:04 -07005219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 /**
5221 * Must be called with the main window manager lock held.
5222 */
5223 void setHoldScreenLocked(boolean holding) {
5224 boolean state = mHoldingScreen.isHeld();
5225 if (holding != state) {
5226 if (holding) {
5227 mHoldingScreen.acquire();
5228 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005229 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 mHoldingScreen.release();
5231 }
5232 }
5233 }
Michael Chan53071d62009-05-13 17:29:48 -07005234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005235
5236 public boolean detectSafeMode() {
5237 mSafeMode = mPolicy.detectSafeMode();
5238 return mSafeMode;
5239 }
Romain Guy06882f82009-06-10 13:36:04 -07005240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241 public void systemReady() {
5242 mPolicy.systemReady();
5243 }
Romain Guy06882f82009-06-10 13:36:04 -07005244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 private final class InputDispatcherThread extends Thread {
5246 // Time to wait when there is nothing to do: 9999 seconds.
5247 static final int LONG_WAIT=9999*1000;
5248
5249 public InputDispatcherThread() {
5250 super("InputDispatcher");
5251 }
Romain Guy06882f82009-06-10 13:36:04 -07005252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 @Override
5254 public void run() {
5255 while (true) {
5256 try {
5257 process();
5258 } catch (Exception e) {
5259 Log.e(TAG, "Exception in input dispatcher", e);
5260 }
5261 }
5262 }
Romain Guy06882f82009-06-10 13:36:04 -07005263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 private void process() {
5265 android.os.Process.setThreadPriority(
5266 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005268 // The last key event we saw
5269 KeyEvent lastKey = null;
5270
5271 // Last keydown time for auto-repeating keys
5272 long lastKeyTime = SystemClock.uptimeMillis();
5273 long nextKeyTime = lastKeyTime+LONG_WAIT;
5274
Romain Guy06882f82009-06-10 13:36:04 -07005275 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 int keyRepeatCount = 0;
5277
5278 // Need to report that configuration has changed?
5279 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 while (true) {
5282 long curTime = SystemClock.uptimeMillis();
5283
5284 if (DEBUG_INPUT) Log.v(
5285 TAG, "Waiting for next key: now=" + curTime
5286 + ", repeat @ " + nextKeyTime);
5287
5288 // Retrieve next event, waiting only as long as the next
5289 // repeat timeout. If the configuration has changed, then
5290 // don't wait at all -- we'll report the change as soon as
5291 // we have processed all events.
5292 QueuedEvent ev = mQueue.getEvent(
5293 (int)((!configChanged && curTime < nextKeyTime)
5294 ? (nextKeyTime-curTime) : 0));
5295
5296 if (DEBUG_INPUT && ev != null) Log.v(
5297 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5298
Michael Chan53071d62009-05-13 17:29:48 -07005299 if (MEASURE_LATENCY) {
5300 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5301 }
5302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005303 try {
5304 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005305 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 int eventType;
5307 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5308 eventType = eventType((MotionEvent)ev.event);
5309 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5310 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5311 eventType = LocalPowerManager.BUTTON_EVENT;
5312 } else {
5313 eventType = LocalPowerManager.OTHER_EVENT;
5314 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005315 try {
Michael Chan53071d62009-05-13 17:29:48 -07005316 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005317 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005318 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005319 mBatteryStats.noteInputEvent();
5320 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005321 } catch (RemoteException e) {
5322 // Ignore
5323 }
Michael Chane10de972009-05-18 11:24:50 -07005324
5325 if (eventType != TOUCH_EVENT
5326 && eventType != LONG_TOUCH_EVENT
5327 && eventType != CHEEK_EVENT) {
5328 mPowerManager.userActivity(curTime, false,
5329 eventType, false);
5330 } else if (mLastTouchEventType != eventType
5331 || (curTime - mLastUserActivityCallTime)
5332 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5333 mLastUserActivityCallTime = curTime;
5334 mLastTouchEventType = eventType;
5335 mPowerManager.userActivity(curTime, false,
5336 eventType, false);
5337 }
5338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 switch (ev.classType) {
5340 case RawInputEvent.CLASS_KEYBOARD:
5341 KeyEvent ke = (KeyEvent)ev.event;
5342 if (ke.isDown()) {
5343 lastKey = ke;
5344 keyRepeatCount = 0;
5345 lastKeyTime = curTime;
5346 nextKeyTime = lastKeyTime
5347 + KEY_REPEAT_FIRST_DELAY;
5348 if (DEBUG_INPUT) Log.v(
5349 TAG, "Received key down: first repeat @ "
5350 + nextKeyTime);
5351 } else {
5352 lastKey = null;
5353 // Arbitrary long timeout.
5354 lastKeyTime = curTime;
5355 nextKeyTime = curTime + LONG_WAIT;
5356 if (DEBUG_INPUT) Log.v(
5357 TAG, "Received key up: ignore repeat @ "
5358 + nextKeyTime);
5359 }
5360 dispatchKey((KeyEvent)ev.event, 0, 0);
5361 mQueue.recycleEvent(ev);
5362 break;
5363 case RawInputEvent.CLASS_TOUCHSCREEN:
5364 //Log.i(TAG, "Read next event " + ev);
5365 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5366 break;
5367 case RawInputEvent.CLASS_TRACKBALL:
5368 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5369 break;
5370 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5371 configChanged = true;
5372 break;
5373 default:
5374 mQueue.recycleEvent(ev);
5375 break;
5376 }
Romain Guy06882f82009-06-10 13:36:04 -07005377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005378 } else if (configChanged) {
5379 configChanged = false;
5380 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 } else if (lastKey != null) {
5383 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005385 // Timeout occurred while key was down. If it is at or
5386 // past the key repeat time, dispatch the repeat.
5387 if (DEBUG_INPUT) Log.v(
5388 TAG, "Key timeout: repeat=" + nextKeyTime
5389 + ", now=" + curTime);
5390 if (curTime < nextKeyTime) {
5391 continue;
5392 }
Romain Guy06882f82009-06-10 13:36:04 -07005393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 lastKeyTime = nextKeyTime;
5395 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5396 keyRepeatCount++;
5397 if (DEBUG_INPUT) Log.v(
5398 TAG, "Key repeat: count=" + keyRepeatCount
5399 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005400 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 } else {
5403 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 lastKeyTime = curTime;
5406 nextKeyTime = curTime + LONG_WAIT;
5407 }
Romain Guy06882f82009-06-10 13:36:04 -07005408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 } catch (Exception e) {
5410 Log.e(TAG,
5411 "Input thread received uncaught exception: " + e, e);
5412 }
5413 }
5414 }
5415 }
5416
5417 // -------------------------------------------------------------
5418 // Client Session State
5419 // -------------------------------------------------------------
5420
5421 private final class Session extends IWindowSession.Stub
5422 implements IBinder.DeathRecipient {
5423 final IInputMethodClient mClient;
5424 final IInputContext mInputContext;
5425 final int mUid;
5426 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005427 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 SurfaceSession mSurfaceSession;
5429 int mNumWindow = 0;
5430 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 /**
5433 * Current pointer move event being dispatched to client window... must
5434 * hold key lock to access.
5435 */
5436 QueuedEvent mPendingPointerMove;
5437 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 /**
5440 * Current trackball move event being dispatched to client window... must
5441 * hold key lock to access.
5442 */
5443 QueuedEvent mPendingTrackballMove;
5444 WindowState mPendingTrackballWindow;
5445
5446 public Session(IInputMethodClient client, IInputContext inputContext) {
5447 mClient = client;
5448 mInputContext = inputContext;
5449 mUid = Binder.getCallingUid();
5450 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005451 StringBuilder sb = new StringBuilder();
5452 sb.append("Session{");
5453 sb.append(Integer.toHexString(System.identityHashCode(this)));
5454 sb.append(" uid ");
5455 sb.append(mUid);
5456 sb.append("}");
5457 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 synchronized (mWindowMap) {
5460 if (mInputMethodManager == null && mHaveInputMethods) {
5461 IBinder b = ServiceManager.getService(
5462 Context.INPUT_METHOD_SERVICE);
5463 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5464 }
5465 }
5466 long ident = Binder.clearCallingIdentity();
5467 try {
5468 // Note: it is safe to call in to the input method manager
5469 // here because we are not holding our lock.
5470 if (mInputMethodManager != null) {
5471 mInputMethodManager.addClient(client, inputContext,
5472 mUid, mPid);
5473 } else {
5474 client.setUsingInputMethod(false);
5475 }
5476 client.asBinder().linkToDeath(this, 0);
5477 } catch (RemoteException e) {
5478 // The caller has died, so we can just forget about this.
5479 try {
5480 if (mInputMethodManager != null) {
5481 mInputMethodManager.removeClient(client);
5482 }
5483 } catch (RemoteException ee) {
5484 }
5485 } finally {
5486 Binder.restoreCallingIdentity(ident);
5487 }
5488 }
Romain Guy06882f82009-06-10 13:36:04 -07005489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 @Override
5491 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5492 throws RemoteException {
5493 try {
5494 return super.onTransact(code, data, reply, flags);
5495 } catch (RuntimeException e) {
5496 // Log all 'real' exceptions thrown to the caller
5497 if (!(e instanceof SecurityException)) {
5498 Log.e(TAG, "Window Session Crash", e);
5499 }
5500 throw e;
5501 }
5502 }
5503
5504 public void binderDied() {
5505 // Note: it is safe to call in to the input method manager
5506 // here because we are not holding our lock.
5507 try {
5508 if (mInputMethodManager != null) {
5509 mInputMethodManager.removeClient(mClient);
5510 }
5511 } catch (RemoteException e) {
5512 }
5513 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005514 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005515 mClientDead = true;
5516 killSessionLocked();
5517 }
5518 }
5519
5520 public int add(IWindow window, WindowManager.LayoutParams attrs,
5521 int viewVisibility, Rect outContentInsets) {
5522 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5523 }
Romain Guy06882f82009-06-10 13:36:04 -07005524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005525 public void remove(IWindow window) {
5526 removeWindow(this, window);
5527 }
Romain Guy06882f82009-06-10 13:36:04 -07005528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5530 int requestedWidth, int requestedHeight, int viewFlags,
5531 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5532 Rect outVisibleInsets, Surface outSurface) {
5533 return relayoutWindow(this, window, attrs,
5534 requestedWidth, requestedHeight, viewFlags, insetsPending,
5535 outFrame, outContentInsets, outVisibleInsets, outSurface);
5536 }
Romain Guy06882f82009-06-10 13:36:04 -07005537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 public void setTransparentRegion(IWindow window, Region region) {
5539 setTransparentRegionWindow(this, window, region);
5540 }
Romain Guy06882f82009-06-10 13:36:04 -07005541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 public void setInsets(IWindow window, int touchableInsets,
5543 Rect contentInsets, Rect visibleInsets) {
5544 setInsetsWindow(this, window, touchableInsets, contentInsets,
5545 visibleInsets);
5546 }
Romain Guy06882f82009-06-10 13:36:04 -07005547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5549 getWindowDisplayFrame(this, window, outDisplayFrame);
5550 }
Romain Guy06882f82009-06-10 13:36:04 -07005551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 public void finishDrawing(IWindow window) {
5553 if (localLOGV) Log.v(
5554 TAG, "IWindow finishDrawing called for " + window);
5555 finishDrawingWindow(this, window);
5556 }
5557
5558 public void finishKey(IWindow window) {
5559 if (localLOGV) Log.v(
5560 TAG, "IWindow finishKey called for " + window);
5561 mKeyWaiter.finishedKey(this, window, false,
5562 KeyWaiter.RETURN_NOTHING);
5563 }
5564
5565 public MotionEvent getPendingPointerMove(IWindow window) {
5566 if (localLOGV) Log.v(
5567 TAG, "IWindow getPendingMotionEvent called for " + window);
5568 return mKeyWaiter.finishedKey(this, window, false,
5569 KeyWaiter.RETURN_PENDING_POINTER);
5570 }
Romain Guy06882f82009-06-10 13:36:04 -07005571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 public MotionEvent getPendingTrackballMove(IWindow window) {
5573 if (localLOGV) Log.v(
5574 TAG, "IWindow getPendingMotionEvent called for " + window);
5575 return mKeyWaiter.finishedKey(this, window, false,
5576 KeyWaiter.RETURN_PENDING_TRACKBALL);
5577 }
5578
5579 public void setInTouchMode(boolean mode) {
5580 synchronized(mWindowMap) {
5581 mInTouchMode = mode;
5582 }
5583 }
5584
5585 public boolean getInTouchMode() {
5586 synchronized(mWindowMap) {
5587 return mInTouchMode;
5588 }
5589 }
5590
5591 public boolean performHapticFeedback(IWindow window, int effectId,
5592 boolean always) {
5593 synchronized(mWindowMap) {
5594 long ident = Binder.clearCallingIdentity();
5595 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005596 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005597 windowForClientLocked(this, window), effectId, always);
5598 } finally {
5599 Binder.restoreCallingIdentity(ident);
5600 }
5601 }
5602 }
Romain Guy06882f82009-06-10 13:36:04 -07005603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 void windowAddedLocked() {
5605 if (mSurfaceSession == null) {
5606 if (localLOGV) Log.v(
5607 TAG, "First window added to " + this + ", creating SurfaceSession");
5608 mSurfaceSession = new SurfaceSession();
5609 mSessions.add(this);
5610 }
5611 mNumWindow++;
5612 }
5613
5614 void windowRemovedLocked() {
5615 mNumWindow--;
5616 killSessionLocked();
5617 }
Romain Guy06882f82009-06-10 13:36:04 -07005618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 void killSessionLocked() {
5620 if (mNumWindow <= 0 && mClientDead) {
5621 mSessions.remove(this);
5622 if (mSurfaceSession != null) {
5623 if (localLOGV) Log.v(
5624 TAG, "Last window removed from " + this
5625 + ", destroying " + mSurfaceSession);
5626 try {
5627 mSurfaceSession.kill();
5628 } catch (Exception e) {
5629 Log.w(TAG, "Exception thrown when killing surface session "
5630 + mSurfaceSession + " in session " + this
5631 + ": " + e.toString());
5632 }
5633 mSurfaceSession = null;
5634 }
5635 }
5636 }
Romain Guy06882f82009-06-10 13:36:04 -07005637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005639 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5640 pw.print(" mClientDead="); pw.print(mClientDead);
5641 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5642 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5643 pw.print(prefix);
5644 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5645 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5646 }
5647 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5648 pw.print(prefix);
5649 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5650 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005652 }
5653
5654 @Override
5655 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005656 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 }
5658 }
5659
5660 // -------------------------------------------------------------
5661 // Client Window State
5662 // -------------------------------------------------------------
5663
5664 private final class WindowState implements WindowManagerPolicy.WindowState {
5665 final Session mSession;
5666 final IWindow mClient;
5667 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005668 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669 AppWindowToken mAppToken;
5670 AppWindowToken mTargetAppToken;
5671 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5672 final DeathRecipient mDeathRecipient;
5673 final WindowState mAttachedWindow;
5674 final ArrayList mChildWindows = new ArrayList();
5675 final int mBaseLayer;
5676 final int mSubLayer;
5677 final boolean mLayoutAttached;
5678 final boolean mIsImWindow;
5679 int mViewVisibility;
5680 boolean mPolicyVisibility = true;
5681 boolean mPolicyVisibilityAfterAnim = true;
5682 boolean mAppFreezing;
5683 Surface mSurface;
5684 boolean mAttachedHidden; // is our parent window hidden?
5685 boolean mLastHidden; // was this window last hidden?
5686 int mRequestedWidth;
5687 int mRequestedHeight;
5688 int mLastRequestedWidth;
5689 int mLastRequestedHeight;
5690 int mReqXPos;
5691 int mReqYPos;
5692 int mLayer;
5693 int mAnimLayer;
5694 int mLastLayer;
5695 boolean mHaveFrame;
5696
5697 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 // Actual frame shown on-screen (may be modified by animation)
5700 final Rect mShownFrame = new Rect();
5701 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 /**
5704 * Insets that determine the actually visible area
5705 */
5706 final Rect mVisibleInsets = new Rect();
5707 final Rect mLastVisibleInsets = new Rect();
5708 boolean mVisibleInsetsChanged;
5709
5710 /**
5711 * Insets that are covered by system windows
5712 */
5713 final Rect mContentInsets = new Rect();
5714 final Rect mLastContentInsets = new Rect();
5715 boolean mContentInsetsChanged;
5716
5717 /**
5718 * Set to true if we are waiting for this window to receive its
5719 * given internal insets before laying out other windows based on it.
5720 */
5721 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07005722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005723 /**
5724 * These are the content insets that were given during layout for
5725 * this window, to be applied to windows behind it.
5726 */
5727 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 /**
5730 * These are the visible insets that were given during layout for
5731 * this window, to be applied to windows behind it.
5732 */
5733 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 /**
5736 * Flag indicating whether the touchable region should be adjusted by
5737 * the visible insets; if false the area outside the visible insets is
5738 * NOT touchable, so we must use those to adjust the frame during hit
5739 * tests.
5740 */
5741 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07005742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 // Current transformation being applied.
5744 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5745 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5746 float mHScale=1, mVScale=1;
5747 float mLastHScale=1, mLastVScale=1;
5748 final Matrix mTmpMatrix = new Matrix();
5749
5750 // "Real" frame that the application sees.
5751 final Rect mFrame = new Rect();
5752 final Rect mLastFrame = new Rect();
5753
5754 final Rect mContainingFrame = new Rect();
5755 final Rect mDisplayFrame = new Rect();
5756 final Rect mContentFrame = new Rect();
5757 final Rect mVisibleFrame = new Rect();
5758
5759 float mShownAlpha = 1;
5760 float mAlpha = 1;
5761 float mLastAlpha = 1;
5762
5763 // Set to true if, when the window gets displayed, it should perform
5764 // an enter animation.
5765 boolean mEnterAnimationPending;
5766
5767 // Currently running animation.
5768 boolean mAnimating;
5769 boolean mLocalAnimating;
5770 Animation mAnimation;
5771 boolean mAnimationIsEntrance;
5772 boolean mHasTransformation;
5773 boolean mHasLocalTransformation;
5774 final Transformation mTransformation = new Transformation();
5775
5776 // This is set after IWindowSession.relayout() has been called at
5777 // least once for the window. It allows us to detect the situation
5778 // where we don't yet have a surface, but should have one soon, so
5779 // we can give the window focus before waiting for the relayout.
5780 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07005781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 // This is set after the Surface has been created but before the
5783 // window has been drawn. During this time the surface is hidden.
5784 boolean mDrawPending;
5785
5786 // This is set after the window has finished drawing for the first
5787 // time but before its surface is shown. The surface will be
5788 // displayed when the next layout is run.
5789 boolean mCommitDrawPending;
5790
5791 // This is set during the time after the window's drawing has been
5792 // committed, and before its surface is actually shown. It is used
5793 // to delay showing the surface until all windows in a token are ready
5794 // to be shown.
5795 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07005796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 // Set when the window has been shown in the screen the first time.
5798 boolean mHasDrawn;
5799
5800 // Currently running an exit animation?
5801 boolean mExiting;
5802
5803 // Currently on the mDestroySurface list?
5804 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07005805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 // Completely remove from window manager after exit animation?
5807 boolean mRemoveOnExit;
5808
5809 // Set when the orientation is changing and this window has not yet
5810 // been updated for the new orientation.
5811 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07005812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005813 // Is this window now (or just being) removed?
5814 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07005815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 WindowState(Session s, IWindow c, WindowToken token,
5817 WindowState attachedWindow, WindowManager.LayoutParams a,
5818 int viewVisibility) {
5819 mSession = s;
5820 mClient = c;
5821 mToken = token;
5822 mAttrs.copyFrom(a);
5823 mViewVisibility = viewVisibility;
5824 DeathRecipient deathRecipient = new DeathRecipient();
5825 mAlpha = a.alpha;
5826 if (localLOGV) Log.v(
5827 TAG, "Window " + this + " client=" + c.asBinder()
5828 + " token=" + token + " (" + mAttrs.token + ")");
5829 try {
5830 c.asBinder().linkToDeath(deathRecipient, 0);
5831 } catch (RemoteException e) {
5832 mDeathRecipient = null;
5833 mAttachedWindow = null;
5834 mLayoutAttached = false;
5835 mIsImWindow = false;
5836 mBaseLayer = 0;
5837 mSubLayer = 0;
5838 return;
5839 }
5840 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07005841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
5843 mAttrs.type <= LAST_SUB_WINDOW)) {
5844 // The multiplier here is to reserve space for multiple
5845 // windows in the same type layer.
5846 mBaseLayer = mPolicy.windowTypeToLayerLw(
5847 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
5848 + TYPE_LAYER_OFFSET;
5849 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
5850 mAttachedWindow = attachedWindow;
5851 mAttachedWindow.mChildWindows.add(this);
5852 mLayoutAttached = mAttrs.type !=
5853 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
5854 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
5855 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5856 } else {
5857 // The multiplier here is to reserve space for multiple
5858 // windows in the same type layer.
5859 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
5860 * TYPE_LAYER_MULTIPLIER
5861 + TYPE_LAYER_OFFSET;
5862 mSubLayer = 0;
5863 mAttachedWindow = null;
5864 mLayoutAttached = false;
5865 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
5866 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5867 }
5868
5869 WindowState appWin = this;
5870 while (appWin.mAttachedWindow != null) {
5871 appWin = mAttachedWindow;
5872 }
5873 WindowToken appToken = appWin.mToken;
5874 while (appToken.appWindowToken == null) {
5875 WindowToken parent = mTokenMap.get(appToken.token);
5876 if (parent == null || appToken == parent) {
5877 break;
5878 }
5879 appToken = parent;
5880 }
The Android Open Source Project10592532009-03-18 17:39:46 -07005881 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 mAppToken = appToken.appWindowToken;
5883
5884 mSurface = null;
5885 mRequestedWidth = 0;
5886 mRequestedHeight = 0;
5887 mLastRequestedWidth = 0;
5888 mLastRequestedHeight = 0;
5889 mReqXPos = 0;
5890 mReqYPos = 0;
5891 mLayer = 0;
5892 mAnimLayer = 0;
5893 mLastLayer = 0;
5894 }
5895
5896 void attach() {
5897 if (localLOGV) Log.v(
5898 TAG, "Attaching " + this + " token=" + mToken
5899 + ", list=" + mToken.windows);
5900 mSession.windowAddedLocked();
5901 }
5902
5903 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
5904 mHaveFrame = true;
5905
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005906 final Rect container = mContainingFrame;
5907 container.set(pf);
5908
5909 final Rect display = mDisplayFrame;
5910 display.set(df);
5911
5912 if ((mAttrs.flags & WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW) != 0) {
5913 container.intersect(mCompatibleScreenFrame);
5914 display.intersect(mCompatibleScreenFrame);
5915 }
5916
5917 final int pw = container.right - container.left;
5918 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919
5920 int w,h;
5921 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
5922 w = mAttrs.width < 0 ? pw : mAttrs.width;
5923 h = mAttrs.height< 0 ? ph : mAttrs.height;
5924 } else {
5925 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
5926 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
5927 }
Romain Guy06882f82009-06-10 13:36:04 -07005928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005929 final Rect content = mContentFrame;
5930 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07005931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932 final Rect visible = mVisibleFrame;
5933 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07005934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07005936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005937 //System.out.println("In: w=" + w + " h=" + h + " container=" +
5938 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
5939
5940 Gravity.apply(mAttrs.gravity, w, h, container,
5941 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
5942 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
5943
5944 //System.out.println("Out: " + mFrame);
5945
5946 // Now make sure the window fits in the overall display.
5947 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 // Make sure the content and visible frames are inside of the
5950 // final window frame.
5951 if (content.left < frame.left) content.left = frame.left;
5952 if (content.top < frame.top) content.top = frame.top;
5953 if (content.right > frame.right) content.right = frame.right;
5954 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
5955 if (visible.left < frame.left) visible.left = frame.left;
5956 if (visible.top < frame.top) visible.top = frame.top;
5957 if (visible.right > frame.right) visible.right = frame.right;
5958 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005960 final Rect contentInsets = mContentInsets;
5961 contentInsets.left = content.left-frame.left;
5962 contentInsets.top = content.top-frame.top;
5963 contentInsets.right = frame.right-content.right;
5964 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 final Rect visibleInsets = mVisibleInsets;
5967 visibleInsets.left = visible.left-frame.left;
5968 visibleInsets.top = visible.top-frame.top;
5969 visibleInsets.right = frame.right-visible.right;
5970 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 if (localLOGV) {
5973 //if ("com.google.android.youtube".equals(mAttrs.packageName)
5974 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
5975 Log.v(TAG, "Resolving (mRequestedWidth="
5976 + mRequestedWidth + ", mRequestedheight="
5977 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
5978 + "): frame=" + mFrame.toShortString()
5979 + " ci=" + contentInsets.toShortString()
5980 + " vi=" + visibleInsets.toShortString());
5981 //}
5982 }
5983 }
Romain Guy06882f82009-06-10 13:36:04 -07005984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 public Rect getFrameLw() {
5986 return mFrame;
5987 }
5988
5989 public Rect getShownFrameLw() {
5990 return mShownFrame;
5991 }
5992
5993 public Rect getDisplayFrameLw() {
5994 return mDisplayFrame;
5995 }
5996
5997 public Rect getContentFrameLw() {
5998 return mContentFrame;
5999 }
6000
6001 public Rect getVisibleFrameLw() {
6002 return mVisibleFrame;
6003 }
6004
6005 public boolean getGivenInsetsPendingLw() {
6006 return mGivenInsetsPending;
6007 }
6008
6009 public Rect getGivenContentInsetsLw() {
6010 return mGivenContentInsets;
6011 }
Romain Guy06882f82009-06-10 13:36:04 -07006012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006013 public Rect getGivenVisibleInsetsLw() {
6014 return mGivenVisibleInsets;
6015 }
Romain Guy06882f82009-06-10 13:36:04 -07006016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 public WindowManager.LayoutParams getAttrs() {
6018 return mAttrs;
6019 }
6020
6021 public int getSurfaceLayer() {
6022 return mLayer;
6023 }
Romain Guy06882f82009-06-10 13:36:04 -07006024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006025 public IApplicationToken getAppToken() {
6026 return mAppToken != null ? mAppToken.appToken : null;
6027 }
6028
6029 public boolean hasAppShownWindows() {
6030 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6031 }
6032
6033 public boolean hasAppStartingIcon() {
6034 return mAppToken != null ? (mAppToken.startingData != null) : false;
6035 }
6036
6037 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6038 return mAppToken != null ? mAppToken.startingWindow : null;
6039 }
6040
6041 public void setAnimation(Animation anim) {
6042 if (localLOGV) Log.v(
6043 TAG, "Setting animation in " + this + ": " + anim);
6044 mAnimating = false;
6045 mLocalAnimating = false;
6046 mAnimation = anim;
6047 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6048 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6049 }
6050
6051 public void clearAnimation() {
6052 if (mAnimation != null) {
6053 mAnimating = true;
6054 mLocalAnimating = false;
6055 mAnimation = null;
6056 }
6057 }
Romain Guy06882f82009-06-10 13:36:04 -07006058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006059 Surface createSurfaceLocked() {
6060 if (mSurface == null) {
6061 mDrawPending = true;
6062 mCommitDrawPending = false;
6063 mReadyToShow = false;
6064 if (mAppToken != null) {
6065 mAppToken.allDrawn = false;
6066 }
6067
6068 int flags = 0;
6069 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
6070 flags |= Surface.HARDWARE;
6071 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6072 flags |= Surface.GPU;
6073 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6074 flags |= Surface.PUSH_BUFFERS;
6075 }
6076
6077 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6078 flags |= Surface.SECURE;
6079 }
6080 if (DEBUG_VISIBILITY) Log.v(
6081 TAG, "Creating surface in session "
6082 + mSession.mSurfaceSession + " window " + this
6083 + " w=" + mFrame.width()
6084 + " h=" + mFrame.height() + " format="
6085 + mAttrs.format + " flags=" + flags);
6086
6087 int w = mFrame.width();
6088 int h = mFrame.height();
6089 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6090 // for a scaled surface, we always want the requested
6091 // size.
6092 w = mRequestedWidth;
6093 h = mRequestedHeight;
6094 }
6095
6096 try {
6097 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006098 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 0, w, h, mAttrs.format, flags);
6100 } catch (Surface.OutOfResourcesException e) {
6101 Log.w(TAG, "OutOfResourcesException creating surface");
6102 reclaimSomeSurfaceMemoryLocked(this, "create");
6103 return null;
6104 } catch (Exception e) {
6105 Log.e(TAG, "Exception creating surface", e);
6106 return null;
6107 }
Romain Guy06882f82009-06-10 13:36:04 -07006108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 if (localLOGV) Log.v(
6110 TAG, "Got surface: " + mSurface
6111 + ", set left=" + mFrame.left + " top=" + mFrame.top
6112 + ", animLayer=" + mAnimLayer);
6113 if (SHOW_TRANSACTIONS) {
6114 Log.i(TAG, ">>> OPEN TRANSACTION");
6115 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6116 + mAttrs.getTitle() + ") pos=(" +
6117 mFrame.left + "," + mFrame.top + ") (" +
6118 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6119 mAnimLayer + " HIDE");
6120 }
6121 Surface.openTransaction();
6122 try {
6123 try {
6124 mSurface.setPosition(mFrame.left, mFrame.top);
6125 mSurface.setLayer(mAnimLayer);
6126 mSurface.hide();
6127 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6128 mSurface.setFlags(Surface.SURFACE_DITHER,
6129 Surface.SURFACE_DITHER);
6130 }
6131 } catch (RuntimeException e) {
6132 Log.w(TAG, "Error creating surface in " + w, e);
6133 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6134 }
6135 mLastHidden = true;
6136 } finally {
6137 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6138 Surface.closeTransaction();
6139 }
6140 if (localLOGV) Log.v(
6141 TAG, "Created surface " + this);
6142 }
6143 return mSurface;
6144 }
Romain Guy06882f82009-06-10 13:36:04 -07006145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006146 void destroySurfaceLocked() {
6147 // Window is no longer on-screen, so can no longer receive
6148 // key events... if we were waiting for it to finish
6149 // handling a key event, the wait is over!
6150 mKeyWaiter.finishedKey(mSession, mClient, true,
6151 KeyWaiter.RETURN_NOTHING);
6152 mKeyWaiter.releasePendingPointerLocked(mSession);
6153 mKeyWaiter.releasePendingTrackballLocked(mSession);
6154
6155 if (mAppToken != null && this == mAppToken.startingWindow) {
6156 mAppToken.startingDisplayed = false;
6157 }
Romain Guy06882f82009-06-10 13:36:04 -07006158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 if (localLOGV) Log.v(
6160 TAG, "Window " + this
6161 + " destroying surface " + mSurface + ", session " + mSession);
6162 if (mSurface != null) {
6163 try {
6164 if (SHOW_TRANSACTIONS) {
6165 RuntimeException ex = new RuntimeException();
6166 ex.fillInStackTrace();
6167 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6168 + mAttrs.getTitle() + ")", ex);
6169 }
6170 mSurface.clear();
6171 } catch (RuntimeException e) {
6172 Log.w(TAG, "Exception thrown when destroying Window " + this
6173 + " surface " + mSurface + " session " + mSession
6174 + ": " + e.toString());
6175 }
6176 mSurface = null;
6177 mDrawPending = false;
6178 mCommitDrawPending = false;
6179 mReadyToShow = false;
6180
6181 int i = mChildWindows.size();
6182 while (i > 0) {
6183 i--;
6184 WindowState c = (WindowState)mChildWindows.get(i);
6185 c.mAttachedHidden = true;
6186 }
6187 }
6188 }
6189
6190 boolean finishDrawingLocked() {
6191 if (mDrawPending) {
6192 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6193 TAG, "finishDrawingLocked: " + mSurface);
6194 mCommitDrawPending = true;
6195 mDrawPending = false;
6196 return true;
6197 }
6198 return false;
6199 }
6200
6201 // This must be called while inside a transaction.
6202 void commitFinishDrawingLocked(long currentTime) {
6203 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6204 if (!mCommitDrawPending) {
6205 return;
6206 }
6207 mCommitDrawPending = false;
6208 mReadyToShow = true;
6209 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6210 final AppWindowToken atoken = mAppToken;
6211 if (atoken == null || atoken.allDrawn || starting) {
6212 performShowLocked();
6213 }
6214 }
6215
6216 // This must be called while inside a transaction.
6217 boolean performShowLocked() {
6218 if (DEBUG_VISIBILITY) {
6219 RuntimeException e = new RuntimeException();
6220 e.fillInStackTrace();
6221 Log.v(TAG, "performShow on " + this
6222 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6223 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6224 }
6225 if (mReadyToShow && isReadyForDisplay()) {
6226 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6227 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6228 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6229 + " during animation: policyVis=" + mPolicyVisibility
6230 + " attHidden=" + mAttachedHidden
6231 + " tok.hiddenRequested="
6232 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6233 + " tok.idden="
6234 + (mAppToken != null ? mAppToken.hidden : false)
6235 + " animating=" + mAnimating
6236 + " tok animating="
6237 + (mAppToken != null ? mAppToken.animating : false));
6238 if (!showSurfaceRobustlyLocked(this)) {
6239 return false;
6240 }
6241 mLastAlpha = -1;
6242 mHasDrawn = true;
6243 mLastHidden = false;
6244 mReadyToShow = false;
6245 enableScreenIfNeededLocked();
6246
6247 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006249 int i = mChildWindows.size();
6250 while (i > 0) {
6251 i--;
6252 WindowState c = (WindowState)mChildWindows.get(i);
6253 if (c.mSurface != null && c.mAttachedHidden) {
6254 c.mAttachedHidden = false;
6255 c.performShowLocked();
6256 }
6257 }
Romain Guy06882f82009-06-10 13:36:04 -07006258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006259 if (mAttrs.type != TYPE_APPLICATION_STARTING
6260 && mAppToken != null) {
6261 mAppToken.firstWindowDrawn = true;
6262 if (mAnimation == null && mAppToken.startingData != null) {
6263 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6264 + mToken
6265 + ": first real window is shown, no animation");
6266 mFinishedStarting.add(mAppToken);
6267 mH.sendEmptyMessage(H.FINISHED_STARTING);
6268 }
6269 mAppToken.updateReportedVisibilityLocked();
6270 }
6271 }
6272 return true;
6273 }
Romain Guy06882f82009-06-10 13:36:04 -07006274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006275 // This must be called while inside a transaction. Returns true if
6276 // there is more animation to run.
6277 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6278 if (!mDisplayFrozen) {
6279 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006281 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6282 mHasTransformation = true;
6283 mHasLocalTransformation = true;
6284 if (!mLocalAnimating) {
6285 if (DEBUG_ANIM) Log.v(
6286 TAG, "Starting animation in " + this +
6287 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6288 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6289 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6290 mAnimation.setStartTime(currentTime);
6291 mLocalAnimating = true;
6292 mAnimating = true;
6293 }
6294 mTransformation.clear();
6295 final boolean more = mAnimation.getTransformation(
6296 currentTime, mTransformation);
6297 if (DEBUG_ANIM) Log.v(
6298 TAG, "Stepped animation in " + this +
6299 ": more=" + more + ", xform=" + mTransformation);
6300 if (more) {
6301 // we're not done!
6302 return true;
6303 }
6304 if (DEBUG_ANIM) Log.v(
6305 TAG, "Finished animation in " + this +
6306 " @ " + currentTime);
6307 mAnimation = null;
6308 //WindowManagerService.this.dump();
6309 }
6310 mHasLocalTransformation = false;
6311 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6312 && mAppToken.hasTransformation) {
6313 // When our app token is animating, we kind-of pretend like
6314 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6315 // part of this check means that we will only do this if
6316 // our window is not currently exiting, or it is not
6317 // locally animating itself. The idea being that one that
6318 // is exiting and doing a local animation should be removed
6319 // once that animation is done.
6320 mAnimating = true;
6321 mHasTransformation = true;
6322 mTransformation.clear();
6323 return false;
6324 } else if (mHasTransformation) {
6325 // Little trick to get through the path below to act like
6326 // we have finished an animation.
6327 mAnimating = true;
6328 } else if (isAnimating()) {
6329 mAnimating = true;
6330 }
6331 } else if (mAnimation != null) {
6332 // If the display is frozen, and there is a pending animation,
6333 // clear it and make sure we run the cleanup code.
6334 mAnimating = true;
6335 mLocalAnimating = true;
6336 mAnimation = null;
6337 }
Romain Guy06882f82009-06-10 13:36:04 -07006338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 if (!mAnimating && !mLocalAnimating) {
6340 return false;
6341 }
6342
6343 if (DEBUG_ANIM) Log.v(
6344 TAG, "Animation done in " + this + ": exiting=" + mExiting
6345 + ", reportedVisible="
6346 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006348 mAnimating = false;
6349 mLocalAnimating = false;
6350 mAnimation = null;
6351 mAnimLayer = mLayer;
6352 if (mIsImWindow) {
6353 mAnimLayer += mInputMethodAnimLayerAdjustment;
6354 }
6355 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6356 + " anim layer: " + mAnimLayer);
6357 mHasTransformation = false;
6358 mHasLocalTransformation = false;
6359 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6360 mTransformation.clear();
6361 if (mHasDrawn
6362 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6363 && mAppToken != null
6364 && mAppToken.firstWindowDrawn
6365 && mAppToken.startingData != null) {
6366 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6367 + mToken + ": first real window done animating");
6368 mFinishedStarting.add(mAppToken);
6369 mH.sendEmptyMessage(H.FINISHED_STARTING);
6370 }
Romain Guy06882f82009-06-10 13:36:04 -07006371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006372 finishExit();
6373
6374 if (mAppToken != null) {
6375 mAppToken.updateReportedVisibilityLocked();
6376 }
6377
6378 return false;
6379 }
6380
6381 void finishExit() {
6382 if (DEBUG_ANIM) Log.v(
6383 TAG, "finishExit in " + this
6384 + ": exiting=" + mExiting
6385 + " remove=" + mRemoveOnExit
6386 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006388 final int N = mChildWindows.size();
6389 for (int i=0; i<N; i++) {
6390 ((WindowState)mChildWindows.get(i)).finishExit();
6391 }
Romain Guy06882f82009-06-10 13:36:04 -07006392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006393 if (!mExiting) {
6394 return;
6395 }
Romain Guy06882f82009-06-10 13:36:04 -07006396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006397 if (isWindowAnimating()) {
6398 return;
6399 }
6400
6401 if (localLOGV) Log.v(
6402 TAG, "Exit animation finished in " + this
6403 + ": remove=" + mRemoveOnExit);
6404 if (mSurface != null) {
6405 mDestroySurface.add(this);
6406 mDestroying = true;
6407 if (SHOW_TRANSACTIONS) Log.i(
6408 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6409 try {
6410 mSurface.hide();
6411 } catch (RuntimeException e) {
6412 Log.w(TAG, "Error hiding surface in " + this, e);
6413 }
6414 mLastHidden = true;
6415 mKeyWaiter.releasePendingPointerLocked(mSession);
6416 }
6417 mExiting = false;
6418 if (mRemoveOnExit) {
6419 mPendingRemove.add(this);
6420 mRemoveOnExit = false;
6421 }
6422 }
Romain Guy06882f82009-06-10 13:36:04 -07006423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006424 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6425 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6426 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6427 if (dtdx < -.000001f || dtdx > .000001f) return false;
6428 if (dsdy < -.000001f || dsdy > .000001f) return false;
6429 return true;
6430 }
Romain Guy06882f82009-06-10 13:36:04 -07006431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006432 void computeShownFrameLocked() {
6433 final boolean selfTransformation = mHasLocalTransformation;
6434 Transformation attachedTransformation =
6435 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6436 ? mAttachedWindow.mTransformation : null;
6437 Transformation appTransformation =
6438 (mAppToken != null && mAppToken.hasTransformation)
6439 ? mAppToken.transformation : null;
6440 if (selfTransformation || attachedTransformation != null
6441 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006442 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006443 final Rect frame = mFrame;
6444 final float tmpFloats[] = mTmpFloats;
6445 final Matrix tmpMatrix = mTmpMatrix;
6446
6447 // Compute the desired transformation.
6448 tmpMatrix.setTranslate(frame.left, frame.top);
6449 if (selfTransformation) {
6450 tmpMatrix.preConcat(mTransformation.getMatrix());
6451 }
6452 if (attachedTransformation != null) {
6453 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6454 }
6455 if (appTransformation != null) {
6456 tmpMatrix.preConcat(appTransformation.getMatrix());
6457 }
6458
6459 // "convert" it into SurfaceFlinger's format
6460 // (a 2x2 matrix + an offset)
6461 // Here we must not transform the position of the surface
6462 // since it is already included in the transformation.
6463 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006465 tmpMatrix.getValues(tmpFloats);
6466 mDsDx = tmpFloats[Matrix.MSCALE_X];
6467 mDtDx = tmpFloats[Matrix.MSKEW_X];
6468 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6469 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6470 int x = (int)tmpFloats[Matrix.MTRANS_X];
6471 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6472 int w = frame.width();
6473 int h = frame.height();
6474 mShownFrame.set(x, y, x+w, y+h);
6475
6476 // Now set the alpha... but because our current hardware
6477 // can't do alpha transformation on a non-opaque surface,
6478 // turn it off if we are running an animation that is also
6479 // transforming since it is more important to have that
6480 // animation be smooth.
6481 mShownAlpha = mAlpha;
6482 if (!mLimitedAlphaCompositing
6483 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6484 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6485 && x == frame.left && y == frame.top))) {
6486 //Log.i(TAG, "Applying alpha transform");
6487 if (selfTransformation) {
6488 mShownAlpha *= mTransformation.getAlpha();
6489 }
6490 if (attachedTransformation != null) {
6491 mShownAlpha *= attachedTransformation.getAlpha();
6492 }
6493 if (appTransformation != null) {
6494 mShownAlpha *= appTransformation.getAlpha();
6495 }
6496 } else {
6497 //Log.i(TAG, "Not applying alpha transform");
6498 }
Romain Guy06882f82009-06-10 13:36:04 -07006499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500 if (localLOGV) Log.v(
6501 TAG, "Continuing animation in " + this +
6502 ": " + mShownFrame +
6503 ", alpha=" + mTransformation.getAlpha());
6504 return;
6505 }
Romain Guy06882f82009-06-10 13:36:04 -07006506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006507 mShownFrame.set(mFrame);
6508 mShownAlpha = mAlpha;
6509 mDsDx = 1;
6510 mDtDx = 0;
6511 mDsDy = 0;
6512 mDtDy = 1;
6513 }
Romain Guy06882f82009-06-10 13:36:04 -07006514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006515 /**
6516 * Is this window visible? It is not visible if there is no
6517 * surface, or we are in the process of running an exit animation
6518 * that will remove the surface, or its app token has been hidden.
6519 */
6520 public boolean isVisibleLw() {
6521 final AppWindowToken atoken = mAppToken;
6522 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6523 && (atoken == null || !atoken.hiddenRequested)
6524 && !mExiting && !mDestroying;
6525 }
6526
6527 /**
6528 * Is this window visible, ignoring its app token? It is not visible
6529 * if there is no surface, or we are in the process of running an exit animation
6530 * that will remove the surface.
6531 */
6532 public boolean isWinVisibleLw() {
6533 final AppWindowToken atoken = mAppToken;
6534 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6535 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6536 && !mExiting && !mDestroying;
6537 }
6538
6539 /**
6540 * The same as isVisible(), but follows the current hidden state of
6541 * the associated app token, not the pending requested hidden state.
6542 */
6543 boolean isVisibleNow() {
6544 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006545 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006546 }
6547
6548 /**
6549 * Same as isVisible(), but we also count it as visible between the
6550 * call to IWindowSession.add() and the first relayout().
6551 */
6552 boolean isVisibleOrAdding() {
6553 final AppWindowToken atoken = mAppToken;
6554 return (mSurface != null
6555 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6556 && mPolicyVisibility && !mAttachedHidden
6557 && (atoken == null || !atoken.hiddenRequested)
6558 && !mExiting && !mDestroying;
6559 }
6560
6561 /**
6562 * Is this window currently on-screen? It is on-screen either if it
6563 * is visible or it is currently running an animation before no longer
6564 * being visible.
6565 */
6566 boolean isOnScreen() {
6567 final AppWindowToken atoken = mAppToken;
6568 if (atoken != null) {
6569 return mSurface != null && mPolicyVisibility && !mDestroying
6570 && ((!mAttachedHidden && !atoken.hiddenRequested)
6571 || mAnimating || atoken.animating);
6572 } else {
6573 return mSurface != null && mPolicyVisibility && !mDestroying
6574 && (!mAttachedHidden || mAnimating);
6575 }
6576 }
Romain Guy06882f82009-06-10 13:36:04 -07006577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006578 /**
6579 * Like isOnScreen(), but we don't return true if the window is part
6580 * of a transition that has not yet been started.
6581 */
6582 boolean isReadyForDisplay() {
6583 final AppWindowToken atoken = mAppToken;
6584 final boolean animating = atoken != null ? atoken.animating : false;
6585 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006586 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006587 || mAnimating || animating);
6588 }
6589
6590 /** Is the window or its container currently animating? */
6591 boolean isAnimating() {
6592 final WindowState attached = mAttachedWindow;
6593 final AppWindowToken atoken = mAppToken;
6594 return mAnimation != null
6595 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006596 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 (atoken.animation != null
6598 || atoken.inPendingTransaction));
6599 }
6600
6601 /** Is this window currently animating? */
6602 boolean isWindowAnimating() {
6603 return mAnimation != null;
6604 }
6605
6606 /**
6607 * Like isOnScreen, but returns false if the surface hasn't yet
6608 * been drawn.
6609 */
6610 public boolean isDisplayedLw() {
6611 final AppWindowToken atoken = mAppToken;
6612 return mSurface != null && mPolicyVisibility && !mDestroying
6613 && !mDrawPending && !mCommitDrawPending
6614 && ((!mAttachedHidden &&
6615 (atoken == null || !atoken.hiddenRequested))
6616 || mAnimating);
6617 }
6618
6619 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6620 boolean shownFrame, boolean onlyOpaque) {
6621 if (mSurface == null) {
6622 return false;
6623 }
6624 if (mAppToken != null && !mAppToken.appFullscreen) {
6625 return false;
6626 }
6627 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6628 return false;
6629 }
6630 final Rect frame = shownFrame ? mShownFrame : mFrame;
6631 if (frame.left <= 0 && frame.top <= 0
6632 && frame.right >= screenWidth
6633 && frame.bottom >= screenHeight) {
6634 return true;
6635 }
6636 return false;
6637 }
Romain Guy06882f82009-06-10 13:36:04 -07006638
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006639 /**
6640 * Return true if the window is opaque and fully drawn.
6641 */
6642 boolean isOpaqueDrawn() {
6643 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
6644 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
6645 }
6646
6647 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
6648 return
6649 // only if the application is requesting compatible window
6650 (mAttrs.flags & mAttrs.FLAG_COMPATIBLE_WINDOW) != 0 &&
6651 // and only if the application wanted to fill the screen
6652 mAttrs.width == mAttrs.FILL_PARENT &&
6653 mAttrs.height == mAttrs.FILL_PARENT &&
6654 // and only if the screen is bigger
6655 ((mFrame.right - mFrame.right) < screenWidth ||
6656 (mFrame.bottom - mFrame.top) < screenHeight);
6657 }
6658
6659 boolean isFullscreen(int screenWidth, int screenHeight) {
6660 return mFrame.left <= 0 && mFrame.top <= 0 &&
6661 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006662 }
6663
6664 void removeLocked() {
6665 if (mAttachedWindow != null) {
6666 mAttachedWindow.mChildWindows.remove(this);
6667 }
6668 destroySurfaceLocked();
6669 mSession.windowRemovedLocked();
6670 try {
6671 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6672 } catch (RuntimeException e) {
6673 // Ignore if it has already been removed (usually because
6674 // we are doing this as part of processing a death note.)
6675 }
6676 }
6677
6678 private class DeathRecipient implements IBinder.DeathRecipient {
6679 public void binderDied() {
6680 try {
6681 synchronized(mWindowMap) {
6682 WindowState win = windowForClientLocked(mSession, mClient);
6683 Log.i(TAG, "WIN DEATH: " + win);
6684 if (win != null) {
6685 removeWindowLocked(mSession, win);
6686 }
6687 }
6688 } catch (IllegalArgumentException ex) {
6689 // This will happen if the window has already been
6690 // removed.
6691 }
6692 }
6693 }
6694
6695 /** Returns true if this window desires key events. */
6696 public final boolean canReceiveKeys() {
6697 return isVisibleOrAdding()
6698 && (mViewVisibility == View.VISIBLE)
6699 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6700 }
6701
6702 public boolean hasDrawnLw() {
6703 return mHasDrawn;
6704 }
6705
6706 public boolean showLw(boolean doAnimation) {
6707 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6708 mPolicyVisibility = true;
6709 mPolicyVisibilityAfterAnim = true;
6710 if (doAnimation) {
6711 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6712 }
6713 requestAnimationLocked(0);
6714 return true;
6715 }
6716 return false;
6717 }
6718
6719 public boolean hideLw(boolean doAnimation) {
6720 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6721 : mPolicyVisibility;
6722 if (current) {
6723 if (doAnimation) {
6724 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6725 if (mAnimation == null) {
6726 doAnimation = false;
6727 }
6728 }
6729 if (doAnimation) {
6730 mPolicyVisibilityAfterAnim = false;
6731 } else {
6732 mPolicyVisibilityAfterAnim = false;
6733 mPolicyVisibility = false;
6734 }
6735 requestAnimationLocked(0);
6736 return true;
6737 }
6738 return false;
6739 }
6740
6741 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006742 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07006743
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006744 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6745 pw.print(" mClient="); pw.println(mClient.asBinder());
6746 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6747 if (mAttachedWindow != null || mLayoutAttached) {
6748 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6749 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6750 }
6751 if (mIsImWindow) {
6752 pw.print(prefix); pw.print("mIsImWindow="); pw.println(mIsImWindow);
6753 }
6754 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
6755 pw.print(" mSubLayer="); pw.print(mSubLayer);
6756 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
6757 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
6758 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
6759 pw.print("="); pw.print(mAnimLayer);
6760 pw.print(" mLastLayer="); pw.println(mLastLayer);
6761 if (mSurface != null) {
6762 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
6763 }
6764 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
6765 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
6766 if (mAppToken != null) {
6767 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
6768 }
6769 if (mTargetAppToken != null) {
6770 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
6771 }
6772 pw.print(prefix); pw.print("mViewVisibility=0x");
6773 pw.print(Integer.toHexString(mViewVisibility));
6774 pw.print(" mLastHidden="); pw.print(mLastHidden);
6775 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
6776 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
6777 pw.print(prefix); pw.print("mPolicyVisibility=");
6778 pw.print(mPolicyVisibility);
6779 pw.print(" mPolicyVisibilityAfterAnim=");
6780 pw.print(mPolicyVisibilityAfterAnim);
6781 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
6782 }
6783 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
6784 pw.print(" h="); pw.print(mRequestedHeight);
6785 pw.print(" x="); pw.print(mReqXPos);
6786 pw.print(" y="); pw.println(mReqYPos);
6787 pw.print(prefix); pw.print("mGivenContentInsets=");
6788 mGivenContentInsets.printShortString(pw);
6789 pw.print(" mGivenVisibleInsets=");
6790 mGivenVisibleInsets.printShortString(pw);
6791 pw.println();
6792 if (mTouchableInsets != 0 || mGivenInsetsPending) {
6793 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
6794 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
6795 }
6796 pw.print(prefix); pw.print("mShownFrame=");
6797 mShownFrame.printShortString(pw);
6798 pw.print(" last="); mLastShownFrame.printShortString(pw);
6799 pw.println();
6800 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
6801 pw.print(" last="); mLastFrame.printShortString(pw);
6802 pw.println();
6803 pw.print(prefix); pw.print("mContainingFrame=");
6804 mContainingFrame.printShortString(pw);
6805 pw.print(" mDisplayFrame=");
6806 mDisplayFrame.printShortString(pw);
6807 pw.println();
6808 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
6809 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
6810 pw.println();
6811 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
6812 pw.print(" last="); mLastContentInsets.printShortString(pw);
6813 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
6814 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
6815 pw.println();
6816 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
6817 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
6818 pw.print(" mAlpha="); pw.print(mAlpha);
6819 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
6820 }
6821 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
6822 || mAnimation != null) {
6823 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
6824 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
6825 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
6826 pw.print(" mAnimation="); pw.println(mAnimation);
6827 }
6828 if (mHasTransformation || mHasLocalTransformation) {
6829 pw.print(prefix); pw.print("XForm: has=");
6830 pw.print(mHasTransformation);
6831 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
6832 pw.print(" "); mTransformation.printShortString(pw);
6833 pw.println();
6834 }
6835 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
6836 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
6837 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
6838 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
6839 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
6840 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
6841 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
6842 pw.print(" mDestroying="); pw.print(mDestroying);
6843 pw.print(" mRemoved="); pw.println(mRemoved);
6844 }
6845 if (mOrientationChanging || mAppFreezing) {
6846 pw.print(prefix); pw.print("mOrientationChanging=");
6847 pw.print(mOrientationChanging);
6848 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
6849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006850 }
6851
6852 @Override
6853 public String toString() {
6854 return "Window{"
6855 + Integer.toHexString(System.identityHashCode(this))
6856 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
6857 }
6858 }
Romain Guy06882f82009-06-10 13:36:04 -07006859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006860 // -------------------------------------------------------------
6861 // Window Token State
6862 // -------------------------------------------------------------
6863
6864 class WindowToken {
6865 // The actual token.
6866 final IBinder token;
6867
6868 // The type of window this token is for, as per WindowManager.LayoutParams.
6869 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07006870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 // Set if this token was explicitly added by a client, so should
6872 // not be removed when all windows are removed.
6873 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07006874
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006875 // For printing.
6876 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07006877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006878 // If this is an AppWindowToken, this is non-null.
6879 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07006880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006881 // All of the windows associated with this token.
6882 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
6883
6884 // Is key dispatching paused for this token?
6885 boolean paused = false;
6886
6887 // Should this token's windows be hidden?
6888 boolean hidden;
6889
6890 // Temporary for finding which tokens no longer have visible windows.
6891 boolean hasVisible;
6892
6893 WindowToken(IBinder _token, int type, boolean _explicit) {
6894 token = _token;
6895 windowType = type;
6896 explicit = _explicit;
6897 }
6898
6899 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006900 pw.print(prefix); pw.print("token="); pw.println(token);
6901 pw.print(prefix); pw.print("windows="); pw.println(windows);
6902 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
6903 pw.print(" hidden="); pw.print(hidden);
6904 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006905 }
6906
6907 @Override
6908 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006909 if (stringName == null) {
6910 StringBuilder sb = new StringBuilder();
6911 sb.append("WindowToken{");
6912 sb.append(Integer.toHexString(System.identityHashCode(this)));
6913 sb.append(" token="); sb.append(token); sb.append('}');
6914 stringName = sb.toString();
6915 }
6916 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917 }
6918 };
6919
6920 class AppWindowToken extends WindowToken {
6921 // Non-null only for application tokens.
6922 final IApplicationToken appToken;
6923
6924 // All of the windows and child windows that are included in this
6925 // application token. Note this list is NOT sorted!
6926 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
6927
6928 int groupId = -1;
6929 boolean appFullscreen;
6930 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07006931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 // These are used for determining when all windows associated with
6933 // an activity have been drawn, so they can be made visible together
6934 // at the same time.
6935 int lastTransactionSequence = mTransactionSequence-1;
6936 int numInterestingWindows;
6937 int numDrawnWindows;
6938 boolean inPendingTransaction;
6939 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07006940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006941 // Is this token going to be hidden in a little while? If so, it
6942 // won't be taken into account for setting the screen orientation.
6943 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 // Is this window's surface needed? This is almost like hidden, except
6946 // it will sometimes be true a little earlier: when the token has
6947 // been shown, but is still waiting for its app transition to execute
6948 // before making its windows shown.
6949 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07006950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006951 // Have we told the window clients to hide themselves?
6952 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006954 // Last visibility state we reported to the app token.
6955 boolean reportedVisible;
6956
6957 // Set to true when the token has been removed from the window mgr.
6958 boolean removed;
6959
6960 // Have we been asked to have this token keep the screen frozen?
6961 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006963 boolean animating;
6964 Animation animation;
6965 boolean hasTransformation;
6966 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07006967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 // Offset to the window of all layers in the token, for use by
6969 // AppWindowToken animations.
6970 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07006971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006972 // Information about an application starting window if displayed.
6973 StartingData startingData;
6974 WindowState startingWindow;
6975 View startingView;
6976 boolean startingDisplayed;
6977 boolean startingMoved;
6978 boolean firstWindowDrawn;
6979
6980 AppWindowToken(IApplicationToken _token) {
6981 super(_token.asBinder(),
6982 WindowManager.LayoutParams.TYPE_APPLICATION, true);
6983 appWindowToken = this;
6984 appToken = _token;
6985 }
Romain Guy06882f82009-06-10 13:36:04 -07006986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006987 public void setAnimation(Animation anim) {
6988 if (localLOGV) Log.v(
6989 TAG, "Setting animation in " + this + ": " + anim);
6990 animation = anim;
6991 animating = false;
6992 anim.restrictDuration(MAX_ANIMATION_DURATION);
6993 anim.scaleCurrentDuration(mTransitionAnimationScale);
6994 int zorder = anim.getZAdjustment();
6995 int adj = 0;
6996 if (zorder == Animation.ZORDER_TOP) {
6997 adj = TYPE_LAYER_OFFSET;
6998 } else if (zorder == Animation.ZORDER_BOTTOM) {
6999 adj = -TYPE_LAYER_OFFSET;
7000 }
Romain Guy06882f82009-06-10 13:36:04 -07007001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 if (animLayerAdjustment != adj) {
7003 animLayerAdjustment = adj;
7004 updateLayers();
7005 }
7006 }
Romain Guy06882f82009-06-10 13:36:04 -07007007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 public void setDummyAnimation() {
7009 if (animation == null) {
7010 if (localLOGV) Log.v(
7011 TAG, "Setting dummy animation in " + this);
7012 animation = sDummyAnimation;
7013 }
7014 }
7015
7016 public void clearAnimation() {
7017 if (animation != null) {
7018 animation = null;
7019 animating = true;
7020 }
7021 }
Romain Guy06882f82009-06-10 13:36:04 -07007022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007023 void updateLayers() {
7024 final int N = allAppWindows.size();
7025 final int adj = animLayerAdjustment;
7026 for (int i=0; i<N; i++) {
7027 WindowState w = allAppWindows.get(i);
7028 w.mAnimLayer = w.mLayer + adj;
7029 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7030 + w.mAnimLayer);
7031 if (w == mInputMethodTarget) {
7032 setInputMethodAnimLayerAdjustment(adj);
7033 }
7034 }
7035 }
Romain Guy06882f82009-06-10 13:36:04 -07007036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007037 void sendAppVisibilityToClients() {
7038 final int N = allAppWindows.size();
7039 for (int i=0; i<N; i++) {
7040 WindowState win = allAppWindows.get(i);
7041 if (win == startingWindow && clientHidden) {
7042 // Don't hide the starting window.
7043 continue;
7044 }
7045 try {
7046 if (DEBUG_VISIBILITY) Log.v(TAG,
7047 "Setting visibility of " + win + ": " + (!clientHidden));
7048 win.mClient.dispatchAppVisibility(!clientHidden);
7049 } catch (RemoteException e) {
7050 }
7051 }
7052 }
Romain Guy06882f82009-06-10 13:36:04 -07007053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 void showAllWindowsLocked() {
7055 final int NW = allAppWindows.size();
7056 for (int i=0; i<NW; i++) {
7057 WindowState w = allAppWindows.get(i);
7058 if (DEBUG_VISIBILITY) Log.v(TAG,
7059 "performing show on: " + w);
7060 w.performShowLocked();
7061 }
7062 }
Romain Guy06882f82009-06-10 13:36:04 -07007063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 // This must be called while inside a transaction.
7065 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7066 if (!mDisplayFrozen) {
7067 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 if (animation == sDummyAnimation) {
7070 // This guy is going to animate, but not yet. For now count
7071 // it is not animating for purposes of scheduling transactions;
7072 // when it is really time to animate, this will be set to
7073 // a real animation and the next call will execute normally.
7074 return false;
7075 }
Romain Guy06882f82009-06-10 13:36:04 -07007076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007077 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7078 if (!animating) {
7079 if (DEBUG_ANIM) Log.v(
7080 TAG, "Starting animation in " + this +
7081 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7082 + " scale=" + mTransitionAnimationScale
7083 + " allDrawn=" + allDrawn + " animating=" + animating);
7084 animation.initialize(dw, dh, dw, dh);
7085 animation.setStartTime(currentTime);
7086 animating = true;
7087 }
7088 transformation.clear();
7089 final boolean more = animation.getTransformation(
7090 currentTime, transformation);
7091 if (DEBUG_ANIM) Log.v(
7092 TAG, "Stepped animation in " + this +
7093 ": more=" + more + ", xform=" + transformation);
7094 if (more) {
7095 // we're done!
7096 hasTransformation = true;
7097 return true;
7098 }
7099 if (DEBUG_ANIM) Log.v(
7100 TAG, "Finished animation in " + this +
7101 " @ " + currentTime);
7102 animation = null;
7103 }
7104 } else if (animation != null) {
7105 // If the display is frozen, and there is a pending animation,
7106 // clear it and make sure we run the cleanup code.
7107 animating = true;
7108 animation = null;
7109 }
7110
7111 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 if (!animating) {
7114 return false;
7115 }
7116
7117 clearAnimation();
7118 animating = false;
7119 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7120 moveInputMethodWindowsIfNeededLocked(true);
7121 }
Romain Guy06882f82009-06-10 13:36:04 -07007122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 if (DEBUG_ANIM) Log.v(
7124 TAG, "Animation done in " + this
7125 + ": reportedVisible=" + reportedVisible);
7126
7127 transformation.clear();
7128 if (animLayerAdjustment != 0) {
7129 animLayerAdjustment = 0;
7130 updateLayers();
7131 }
Romain Guy06882f82009-06-10 13:36:04 -07007132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 final int N = windows.size();
7134 for (int i=0; i<N; i++) {
7135 ((WindowState)windows.get(i)).finishExit();
7136 }
7137 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007139 return false;
7140 }
7141
7142 void updateReportedVisibilityLocked() {
7143 if (appToken == null) {
7144 return;
7145 }
Romain Guy06882f82009-06-10 13:36:04 -07007146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007147 int numInteresting = 0;
7148 int numVisible = 0;
7149 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7152 final int N = allAppWindows.size();
7153 for (int i=0; i<N; i++) {
7154 WindowState win = allAppWindows.get(i);
7155 if (win == startingWindow || win.mAppFreezing) {
7156 continue;
7157 }
7158 if (DEBUG_VISIBILITY) {
7159 Log.v(TAG, "Win " + win + ": isDisplayed="
7160 + win.isDisplayedLw()
7161 + ", isAnimating=" + win.isAnimating());
7162 if (!win.isDisplayedLw()) {
7163 Log.v(TAG, "Not displayed: s=" + win.mSurface
7164 + " pv=" + win.mPolicyVisibility
7165 + " dp=" + win.mDrawPending
7166 + " cdp=" + win.mCommitDrawPending
7167 + " ah=" + win.mAttachedHidden
7168 + " th="
7169 + (win.mAppToken != null
7170 ? win.mAppToken.hiddenRequested : false)
7171 + " a=" + win.mAnimating);
7172 }
7173 }
7174 numInteresting++;
7175 if (win.isDisplayedLw()) {
7176 if (!win.isAnimating()) {
7177 numVisible++;
7178 }
7179 nowGone = false;
7180 } else if (win.isAnimating()) {
7181 nowGone = false;
7182 }
7183 }
Romain Guy06882f82009-06-10 13:36:04 -07007184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007185 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7186 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7187 + numInteresting + " visible=" + numVisible);
7188 if (nowVisible != reportedVisible) {
7189 if (DEBUG_VISIBILITY) Log.v(
7190 TAG, "Visibility changed in " + this
7191 + ": vis=" + nowVisible);
7192 reportedVisible = nowVisible;
7193 Message m = mH.obtainMessage(
7194 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7195 nowVisible ? 1 : 0,
7196 nowGone ? 1 : 0,
7197 this);
7198 mH.sendMessage(m);
7199 }
7200 }
Romain Guy06882f82009-06-10 13:36:04 -07007201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007202 void dump(PrintWriter pw, String prefix) {
7203 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007204 if (appToken != null) {
7205 pw.print(prefix); pw.println("app=true");
7206 }
7207 if (allAppWindows.size() > 0) {
7208 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7209 }
7210 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7211 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7212 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7213 pw.print(" clientHidden="); pw.print(clientHidden);
7214 pw.print(" willBeHidden="); pw.print(willBeHidden);
7215 pw.print(" reportedVisible="); pw.println(reportedVisible);
7216 if (paused || freezingScreen) {
7217 pw.print(prefix); pw.print("paused="); pw.print(paused);
7218 pw.print(" freezingScreen="); pw.println(freezingScreen);
7219 }
7220 if (numInterestingWindows != 0 || numDrawnWindows != 0
7221 || inPendingTransaction || allDrawn) {
7222 pw.print(prefix); pw.print("numInterestingWindows=");
7223 pw.print(numInterestingWindows);
7224 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7225 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7226 pw.print(" allDrawn="); pw.println(allDrawn);
7227 }
7228 if (animating || animation != null) {
7229 pw.print(prefix); pw.print("animating="); pw.print(animating);
7230 pw.print(" animation="); pw.println(animation);
7231 }
7232 if (animLayerAdjustment != 0) {
7233 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7234 }
7235 if (hasTransformation) {
7236 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7237 pw.print(" transformation="); transformation.printShortString(pw);
7238 pw.println();
7239 }
7240 if (startingData != null || removed || firstWindowDrawn) {
7241 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7242 pw.print(" removed="); pw.print(removed);
7243 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7244 }
7245 if (startingWindow != null || startingView != null
7246 || startingDisplayed || startingMoved) {
7247 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7248 pw.print(" startingView="); pw.print(startingView);
7249 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7250 pw.print(" startingMoved"); pw.println(startingMoved);
7251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007252 }
7253
7254 @Override
7255 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007256 if (stringName == null) {
7257 StringBuilder sb = new StringBuilder();
7258 sb.append("AppWindowToken{");
7259 sb.append(Integer.toHexString(System.identityHashCode(this)));
7260 sb.append(" token="); sb.append(token); sb.append('}');
7261 stringName = sb.toString();
7262 }
7263 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 }
7265 }
Romain Guy06882f82009-06-10 13:36:04 -07007266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007267 public static WindowManager.LayoutParams findAnimations(
7268 ArrayList<AppWindowToken> order,
7269 ArrayList<AppWindowToken> tokenList1,
7270 ArrayList<AppWindowToken> tokenList2) {
7271 // We need to figure out which animation to use...
7272 WindowManager.LayoutParams animParams = null;
7273 int animSrc = 0;
Romain Guy06882f82009-06-10 13:36:04 -07007274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007275 //Log.i(TAG, "Looking for animations...");
7276 for (int i=order.size()-1; i>=0; i--) {
7277 AppWindowToken wtoken = order.get(i);
7278 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
7279 if (tokenList1.contains(wtoken) || tokenList2.contains(wtoken)) {
7280 int j = wtoken.windows.size();
7281 while (j > 0) {
7282 j--;
7283 WindowState win = wtoken.windows.get(j);
7284 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7285 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7286 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7287 //Log.i(TAG, "Found base or application window, done!");
7288 if (wtoken.appFullscreen) {
7289 return win.mAttrs;
7290 }
7291 if (animSrc < 2) {
7292 animParams = win.mAttrs;
7293 animSrc = 2;
7294 }
7295 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7296 //Log.i(TAG, "Found normal window, we may use this...");
7297 animParams = win.mAttrs;
7298 animSrc = 1;
7299 }
7300 }
7301 }
7302 }
Romain Guy06882f82009-06-10 13:36:04 -07007303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007304 return animParams;
7305 }
Romain Guy06882f82009-06-10 13:36:04 -07007306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007307 // -------------------------------------------------------------
7308 // DummyAnimation
7309 // -------------------------------------------------------------
7310
7311 // This is an animation that does nothing: it just immediately finishes
7312 // itself every time it is called. It is used as a stub animation in cases
7313 // where we want to synchronize multiple things that may be animating.
7314 static final class DummyAnimation extends Animation {
7315 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7316 return false;
7317 }
7318 }
7319 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 // -------------------------------------------------------------
7322 // Async Handler
7323 // -------------------------------------------------------------
7324
7325 static final class StartingData {
7326 final String pkg;
7327 final int theme;
7328 final CharSequence nonLocalizedLabel;
7329 final int labelRes;
7330 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007332 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7333 int _labelRes, int _icon) {
7334 pkg = _pkg;
7335 theme = _theme;
7336 nonLocalizedLabel = _nonLocalizedLabel;
7337 labelRes = _labelRes;
7338 icon = _icon;
7339 }
7340 }
7341
7342 private final class H extends Handler {
7343 public static final int REPORT_FOCUS_CHANGE = 2;
7344 public static final int REPORT_LOSING_FOCUS = 3;
7345 public static final int ANIMATE = 4;
7346 public static final int ADD_STARTING = 5;
7347 public static final int REMOVE_STARTING = 6;
7348 public static final int FINISHED_STARTING = 7;
7349 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007350 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7351 public static final int HOLD_SCREEN_CHANGED = 12;
7352 public static final int APP_TRANSITION_TIMEOUT = 13;
7353 public static final int PERSIST_ANIMATION_SCALE = 14;
7354 public static final int FORCE_GC = 15;
7355 public static final int ENABLE_SCREEN = 16;
7356 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007357 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007361 public H() {
7362 }
Romain Guy06882f82009-06-10 13:36:04 -07007363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007364 @Override
7365 public void handleMessage(Message msg) {
7366 switch (msg.what) {
7367 case REPORT_FOCUS_CHANGE: {
7368 WindowState lastFocus;
7369 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007371 synchronized(mWindowMap) {
7372 lastFocus = mLastFocus;
7373 newFocus = mCurrentFocus;
7374 if (lastFocus == newFocus) {
7375 // Focus is not changing, so nothing to do.
7376 return;
7377 }
7378 mLastFocus = newFocus;
7379 //Log.i(TAG, "Focus moving from " + lastFocus
7380 // + " to " + newFocus);
7381 if (newFocus != null && lastFocus != null
7382 && !newFocus.isDisplayedLw()) {
7383 //Log.i(TAG, "Delaying loss of focus...");
7384 mLosingFocus.add(lastFocus);
7385 lastFocus = null;
7386 }
7387 }
7388
7389 if (lastFocus != newFocus) {
7390 //System.out.println("Changing focus from " + lastFocus
7391 // + " to " + newFocus);
7392 if (newFocus != null) {
7393 try {
7394 //Log.i(TAG, "Gaining focus: " + newFocus);
7395 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7396 } catch (RemoteException e) {
7397 // Ignore if process has died.
7398 }
7399 }
7400
7401 if (lastFocus != null) {
7402 try {
7403 //Log.i(TAG, "Losing focus: " + lastFocus);
7404 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7405 } catch (RemoteException e) {
7406 // Ignore if process has died.
7407 }
7408 }
7409 }
7410 } break;
7411
7412 case REPORT_LOSING_FOCUS: {
7413 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007415 synchronized(mWindowMap) {
7416 losers = mLosingFocus;
7417 mLosingFocus = new ArrayList<WindowState>();
7418 }
7419
7420 final int N = losers.size();
7421 for (int i=0; i<N; i++) {
7422 try {
7423 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7424 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7425 } catch (RemoteException e) {
7426 // Ignore if process has died.
7427 }
7428 }
7429 } break;
7430
7431 case ANIMATE: {
7432 synchronized(mWindowMap) {
7433 mAnimationPending = false;
7434 performLayoutAndPlaceSurfacesLocked();
7435 }
7436 } break;
7437
7438 case ADD_STARTING: {
7439 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7440 final StartingData sd = wtoken.startingData;
7441
7442 if (sd == null) {
7443 // Animation has been canceled... do nothing.
7444 return;
7445 }
Romain Guy06882f82009-06-10 13:36:04 -07007446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007447 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7448 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 View view = null;
7451 try {
7452 view = mPolicy.addStartingWindow(
7453 wtoken.token, sd.pkg,
7454 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7455 sd.icon);
7456 } catch (Exception e) {
7457 Log.w(TAG, "Exception when adding starting window", e);
7458 }
7459
7460 if (view != null) {
7461 boolean abort = false;
7462
7463 synchronized(mWindowMap) {
7464 if (wtoken.removed || wtoken.startingData == null) {
7465 // If the window was successfully added, then
7466 // we need to remove it.
7467 if (wtoken.startingWindow != null) {
7468 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7469 "Aborted starting " + wtoken
7470 + ": removed=" + wtoken.removed
7471 + " startingData=" + wtoken.startingData);
7472 wtoken.startingWindow = null;
7473 wtoken.startingData = null;
7474 abort = true;
7475 }
7476 } else {
7477 wtoken.startingView = view;
7478 }
7479 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7480 "Added starting " + wtoken
7481 + ": startingWindow="
7482 + wtoken.startingWindow + " startingView="
7483 + wtoken.startingView);
7484 }
7485
7486 if (abort) {
7487 try {
7488 mPolicy.removeStartingWindow(wtoken.token, view);
7489 } catch (Exception e) {
7490 Log.w(TAG, "Exception when removing starting window", e);
7491 }
7492 }
7493 }
7494 } break;
7495
7496 case REMOVE_STARTING: {
7497 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7498 IBinder token = null;
7499 View view = null;
7500 synchronized (mWindowMap) {
7501 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7502 + wtoken + ": startingWindow="
7503 + wtoken.startingWindow + " startingView="
7504 + wtoken.startingView);
7505 if (wtoken.startingWindow != null) {
7506 view = wtoken.startingView;
7507 token = wtoken.token;
7508 wtoken.startingData = null;
7509 wtoken.startingView = null;
7510 wtoken.startingWindow = null;
7511 }
7512 }
7513 if (view != null) {
7514 try {
7515 mPolicy.removeStartingWindow(token, view);
7516 } catch (Exception e) {
7517 Log.w(TAG, "Exception when removing starting window", e);
7518 }
7519 }
7520 } break;
7521
7522 case FINISHED_STARTING: {
7523 IBinder token = null;
7524 View view = null;
7525 while (true) {
7526 synchronized (mWindowMap) {
7527 final int N = mFinishedStarting.size();
7528 if (N <= 0) {
7529 break;
7530 }
7531 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7532
7533 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7534 "Finished starting " + wtoken
7535 + ": startingWindow=" + wtoken.startingWindow
7536 + " startingView=" + wtoken.startingView);
7537
7538 if (wtoken.startingWindow == null) {
7539 continue;
7540 }
7541
7542 view = wtoken.startingView;
7543 token = wtoken.token;
7544 wtoken.startingData = null;
7545 wtoken.startingView = null;
7546 wtoken.startingWindow = null;
7547 }
7548
7549 try {
7550 mPolicy.removeStartingWindow(token, view);
7551 } catch (Exception e) {
7552 Log.w(TAG, "Exception when removing starting window", e);
7553 }
7554 }
7555 } break;
7556
7557 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7558 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7559
7560 boolean nowVisible = msg.arg1 != 0;
7561 boolean nowGone = msg.arg2 != 0;
7562
7563 try {
7564 if (DEBUG_VISIBILITY) Log.v(
7565 TAG, "Reporting visible in " + wtoken
7566 + " visible=" + nowVisible
7567 + " gone=" + nowGone);
7568 if (nowVisible) {
7569 wtoken.appToken.windowsVisible();
7570 } else {
7571 wtoken.appToken.windowsGone();
7572 }
7573 } catch (RemoteException ex) {
7574 }
7575 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 case WINDOW_FREEZE_TIMEOUT: {
7578 synchronized (mWindowMap) {
7579 Log.w(TAG, "Window freeze timeout expired.");
7580 int i = mWindows.size();
7581 while (i > 0) {
7582 i--;
7583 WindowState w = (WindowState)mWindows.get(i);
7584 if (w.mOrientationChanging) {
7585 w.mOrientationChanging = false;
7586 Log.w(TAG, "Force clearing orientation change: " + w);
7587 }
7588 }
7589 performLayoutAndPlaceSurfacesLocked();
7590 }
7591 break;
7592 }
Romain Guy06882f82009-06-10 13:36:04 -07007593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007594 case HOLD_SCREEN_CHANGED: {
7595 Session oldHold;
7596 Session newHold;
7597 synchronized (mWindowMap) {
7598 oldHold = mLastReportedHold;
7599 newHold = (Session)msg.obj;
7600 mLastReportedHold = newHold;
7601 }
Romain Guy06882f82009-06-10 13:36:04 -07007602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007603 if (oldHold != newHold) {
7604 try {
7605 if (oldHold != null) {
7606 mBatteryStats.noteStopWakelock(oldHold.mUid,
7607 "window",
7608 BatteryStats.WAKE_TYPE_WINDOW);
7609 }
7610 if (newHold != null) {
7611 mBatteryStats.noteStartWakelock(newHold.mUid,
7612 "window",
7613 BatteryStats.WAKE_TYPE_WINDOW);
7614 }
7615 } catch (RemoteException e) {
7616 }
7617 }
7618 break;
7619 }
Romain Guy06882f82009-06-10 13:36:04 -07007620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007621 case APP_TRANSITION_TIMEOUT: {
7622 synchronized (mWindowMap) {
7623 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7624 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7625 "*** APP TRANSITION TIMEOUT");
7626 mAppTransitionReady = true;
7627 mAppTransitionTimeout = true;
7628 performLayoutAndPlaceSurfacesLocked();
7629 }
7630 }
7631 break;
7632 }
Romain Guy06882f82009-06-10 13:36:04 -07007633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 case PERSIST_ANIMATION_SCALE: {
7635 Settings.System.putFloat(mContext.getContentResolver(),
7636 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7637 Settings.System.putFloat(mContext.getContentResolver(),
7638 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7639 break;
7640 }
Romain Guy06882f82009-06-10 13:36:04 -07007641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007642 case FORCE_GC: {
7643 synchronized(mWindowMap) {
7644 if (mAnimationPending) {
7645 // If we are animating, don't do the gc now but
7646 // delay a bit so we don't interrupt the animation.
7647 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7648 2000);
7649 return;
7650 }
7651 // If we are currently rotating the display, it will
7652 // schedule a new message when done.
7653 if (mDisplayFrozen) {
7654 return;
7655 }
7656 mFreezeGcPending = 0;
7657 }
7658 Runtime.getRuntime().gc();
7659 break;
7660 }
Romain Guy06882f82009-06-10 13:36:04 -07007661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007662 case ENABLE_SCREEN: {
7663 performEnableScreen();
7664 break;
7665 }
Romain Guy06882f82009-06-10 13:36:04 -07007666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 case APP_FREEZE_TIMEOUT: {
7668 synchronized (mWindowMap) {
7669 Log.w(TAG, "App freeze timeout expired.");
7670 int i = mAppTokens.size();
7671 while (i > 0) {
7672 i--;
7673 AppWindowToken tok = mAppTokens.get(i);
7674 if (tok.freezingScreen) {
7675 Log.w(TAG, "Force clearing freeze: " + tok);
7676 unsetAppFreezingScreenLocked(tok, true, true);
7677 }
7678 }
7679 }
7680 break;
7681 }
Romain Guy06882f82009-06-10 13:36:04 -07007682
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007683 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07007684 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007685 sendNewConfiguration();
7686 }
7687 break;
7688 }
Romain Guy06882f82009-06-10 13:36:04 -07007689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007690 }
7691 }
7692 }
7693
7694 // -------------------------------------------------------------
7695 // IWindowManager API
7696 // -------------------------------------------------------------
7697
7698 public IWindowSession openSession(IInputMethodClient client,
7699 IInputContext inputContext) {
7700 if (client == null) throw new IllegalArgumentException("null client");
7701 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7702 return new Session(client, inputContext);
7703 }
7704
7705 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7706 synchronized (mWindowMap) {
7707 // The focus for the client is the window immediately below
7708 // where we would place the input method window.
7709 int idx = findDesiredInputMethodWindowIndexLocked(false);
7710 WindowState imFocus;
7711 if (idx > 0) {
7712 imFocus = (WindowState)mWindows.get(idx-1);
7713 if (imFocus != null) {
7714 if (imFocus.mSession.mClient != null &&
7715 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7716 return true;
7717 }
7718 }
7719 }
7720 }
7721 return false;
7722 }
Romain Guy06882f82009-06-10 13:36:04 -07007723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007724 // -------------------------------------------------------------
7725 // Internals
7726 // -------------------------------------------------------------
7727
7728 final WindowState windowForClientLocked(Session session, IWindow client) {
7729 return windowForClientLocked(session, client.asBinder());
7730 }
Romain Guy06882f82009-06-10 13:36:04 -07007731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007732 final WindowState windowForClientLocked(Session session, IBinder client) {
7733 WindowState win = mWindowMap.get(client);
7734 if (localLOGV) Log.v(
7735 TAG, "Looking up client " + client + ": " + win);
7736 if (win == null) {
7737 RuntimeException ex = new RuntimeException();
7738 Log.w(TAG, "Requested window " + client + " does not exist", ex);
7739 return null;
7740 }
7741 if (session != null && win.mSession != session) {
7742 RuntimeException ex = new RuntimeException();
7743 Log.w(TAG, "Requested window " + client + " is in session " +
7744 win.mSession + ", not " + session, ex);
7745 return null;
7746 }
7747
7748 return win;
7749 }
7750
7751 private final void assignLayersLocked() {
7752 int N = mWindows.size();
7753 int curBaseLayer = 0;
7754 int curLayer = 0;
7755 int i;
Romain Guy06882f82009-06-10 13:36:04 -07007756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 for (i=0; i<N; i++) {
7758 WindowState w = (WindowState)mWindows.get(i);
7759 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow) {
7760 curLayer += WINDOW_LAYER_MULTIPLIER;
7761 w.mLayer = curLayer;
7762 } else {
7763 curBaseLayer = curLayer = w.mBaseLayer;
7764 w.mLayer = curLayer;
7765 }
7766 if (w.mTargetAppToken != null) {
7767 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
7768 } else if (w.mAppToken != null) {
7769 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
7770 } else {
7771 w.mAnimLayer = w.mLayer;
7772 }
7773 if (w.mIsImWindow) {
7774 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
7775 }
7776 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
7777 + w.mAnimLayer);
7778 //System.out.println(
7779 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
7780 }
7781 }
7782
7783 private boolean mInLayout = false;
7784 private final void performLayoutAndPlaceSurfacesLocked() {
7785 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07007786 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007787 throw new RuntimeException("Recursive call!");
7788 }
7789 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
7790 return;
7791 }
7792
7793 boolean recoveringMemory = false;
7794 if (mForceRemoves != null) {
7795 recoveringMemory = true;
7796 // Wait a little it for things to settle down, and off we go.
7797 for (int i=0; i<mForceRemoves.size(); i++) {
7798 WindowState ws = mForceRemoves.get(i);
7799 Log.i(TAG, "Force removing: " + ws);
7800 removeWindowInnerLocked(ws.mSession, ws);
7801 }
7802 mForceRemoves = null;
7803 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
7804 Object tmp = new Object();
7805 synchronized (tmp) {
7806 try {
7807 tmp.wait(250);
7808 } catch (InterruptedException e) {
7809 }
7810 }
7811 }
Romain Guy06882f82009-06-10 13:36:04 -07007812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 mInLayout = true;
7814 try {
7815 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07007816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 int i = mPendingRemove.size()-1;
7818 if (i >= 0) {
7819 while (i >= 0) {
7820 WindowState w = mPendingRemove.get(i);
7821 removeWindowInnerLocked(w.mSession, w);
7822 i--;
7823 }
7824 mPendingRemove.clear();
7825
7826 mInLayout = false;
7827 assignLayersLocked();
7828 mLayoutNeeded = true;
7829 performLayoutAndPlaceSurfacesLocked();
7830
7831 } else {
7832 mInLayout = false;
7833 if (mLayoutNeeded) {
7834 requestAnimationLocked(0);
7835 }
7836 }
7837 } catch (RuntimeException e) {
7838 mInLayout = false;
7839 Log.e(TAG, "Unhandled exception while layout out windows", e);
7840 }
7841 }
7842
7843 private final void performLayoutLockedInner() {
7844 final int dw = mDisplay.getWidth();
7845 final int dh = mDisplay.getHeight();
7846
7847 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007848 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 int i;
7850
7851 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07007852
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007853 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007854 mPolicy.beginLayoutLw(dw, dh);
7855
7856 // First perform layout of any root windows (not attached
7857 // to another window).
7858 int topAttached = -1;
7859 for (i = N-1; i >= 0; i--) {
7860 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007861
7862 // Don't do layout of a window if it is not visible, or
7863 // soon won't be visible, to avoid wasting time and funky
7864 // changes while a window is animating away.
7865 final AppWindowToken atoken = win.mAppToken;
7866 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007868 || win.mRootToken.hidden
7869 || (atoken != null && atoken.hiddenRequested)
7870 || !win.mPolicyVisibility
7871 || win.mAttachedHidden
7872 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007873
7874 // If this view is GONE, then skip it -- keep the current
7875 // frame, and let the caller know so they can ignore it
7876 // if they want. (We do the normal layout for INVISIBLE
7877 // windows, since that means "perform layout as normal,
7878 // just don't display").
7879 if (!gone || !win.mHaveFrame) {
7880 if (!win.mLayoutAttached) {
7881 mPolicy.layoutWindowLw(win, win.mAttrs, null);
7882 } else {
7883 if (topAttached < 0) topAttached = i;
7884 }
7885 }
7886 }
Romain Guy06882f82009-06-10 13:36:04 -07007887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007888 // Now perform layout of attached windows, which usually
7889 // depend on the position of the window they are attached to.
7890 // XXX does not deal with windows that are attached to windows
7891 // that are themselves attached.
7892 for (i = topAttached; i >= 0; i--) {
7893 WindowState win = (WindowState) mWindows.get(i);
7894
7895 // If this view is GONE, then skip it -- keep the current
7896 // frame, and let the caller know so they can ignore it
7897 // if they want. (We do the normal layout for INVISIBLE
7898 // windows, since that means "perform layout as normal,
7899 // just don't display").
7900 if (win.mLayoutAttached) {
7901 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
7902 || !win.mHaveFrame) {
7903 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
7904 }
7905 }
7906 }
7907
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007908 if (!mPolicy.finishLayoutLw()) {
7909 mLayoutNeeded = false;
7910 } else if (repeats > 2) {
7911 Log.w(TAG, "Layout repeat aborted after too many iterations");
7912 mLayoutNeeded = false;
7913 } else {
7914 repeats++;
7915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007916 }
7917 }
Romain Guy06882f82009-06-10 13:36:04 -07007918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007919 private final void performLayoutAndPlaceSurfacesLockedInner(
7920 boolean recoveringMemory) {
7921 final long currentTime = SystemClock.uptimeMillis();
7922 final int dw = mDisplay.getWidth();
7923 final int dh = mDisplay.getHeight();
7924
7925 final int N = mWindows.size();
7926 int i;
7927
7928 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007929 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07007930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931 if (mFxSession == null) {
7932 mFxSession = new SurfaceSession();
7933 }
Romain Guy06882f82009-06-10 13:36:04 -07007934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
7936
7937 // Initialize state of exiting tokens.
7938 for (i=mExitingTokens.size()-1; i>=0; i--) {
7939 mExitingTokens.get(i).hasVisible = false;
7940 }
7941
7942 // Initialize state of exiting applications.
7943 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
7944 mExitingAppTokens.get(i).hasVisible = false;
7945 }
7946
7947 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007948 boolean orientationChangeComplete = true;
7949 Session holdScreen = null;
7950 float screenBrightness = -1;
7951 boolean focusDisplayed = false;
7952 boolean animating = false;
7953
7954 Surface.openTransaction();
7955 try {
7956 boolean restart;
7957
7958 do {
7959 final int transactionSequence = ++mTransactionSequence;
7960
7961 // Update animations of all applications, including those
7962 // associated with exiting/removed apps
7963 boolean tokensAnimating = false;
7964 final int NAT = mAppTokens.size();
7965 for (i=0; i<NAT; i++) {
7966 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7967 tokensAnimating = true;
7968 }
7969 }
7970 final int NEAT = mExitingAppTokens.size();
7971 for (i=0; i<NEAT; i++) {
7972 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7973 tokensAnimating = true;
7974 }
7975 }
7976
7977 animating = tokensAnimating;
7978 restart = false;
7979
7980 boolean tokenMayBeDrawn = false;
7981
7982 mPolicy.beginAnimationLw(dw, dh);
7983
7984 for (i=N-1; i>=0; i--) {
7985 WindowState w = (WindowState)mWindows.get(i);
7986
7987 final WindowManager.LayoutParams attrs = w.mAttrs;
7988
7989 if (w.mSurface != null) {
7990 // Execute animation.
7991 w.commitFinishDrawingLocked(currentTime);
7992 if (w.stepAnimationLocked(currentTime, dw, dh)) {
7993 animating = true;
7994 //w.dump(" ");
7995 }
7996
7997 mPolicy.animatingWindowLw(w, attrs);
7998 }
7999
8000 final AppWindowToken atoken = w.mAppToken;
8001 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8002 if (atoken.lastTransactionSequence != transactionSequence) {
8003 atoken.lastTransactionSequence = transactionSequence;
8004 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8005 atoken.startingDisplayed = false;
8006 }
8007 if ((w.isOnScreen() || w.mAttrs.type
8008 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8009 && !w.mExiting && !w.mDestroying) {
8010 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8011 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8012 + w.isDisplayedLw()
8013 + ", isAnimating=" + w.isAnimating());
8014 if (!w.isDisplayedLw()) {
8015 Log.v(TAG, "Not displayed: s=" + w.mSurface
8016 + " pv=" + w.mPolicyVisibility
8017 + " dp=" + w.mDrawPending
8018 + " cdp=" + w.mCommitDrawPending
8019 + " ah=" + w.mAttachedHidden
8020 + " th=" + atoken.hiddenRequested
8021 + " a=" + w.mAnimating);
8022 }
8023 }
8024 if (w != atoken.startingWindow) {
8025 if (!atoken.freezingScreen || !w.mAppFreezing) {
8026 atoken.numInterestingWindows++;
8027 if (w.isDisplayedLw()) {
8028 atoken.numDrawnWindows++;
8029 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8030 "tokenMayBeDrawn: " + atoken
8031 + " freezingScreen=" + atoken.freezingScreen
8032 + " mAppFreezing=" + w.mAppFreezing);
8033 tokenMayBeDrawn = true;
8034 }
8035 }
8036 } else if (w.isDisplayedLw()) {
8037 atoken.startingDisplayed = true;
8038 }
8039 }
8040 } else if (w.mReadyToShow) {
8041 w.performShowLocked();
8042 }
8043 }
8044
8045 if (mPolicy.finishAnimationLw()) {
8046 restart = true;
8047 }
8048
8049 if (tokenMayBeDrawn) {
8050 // See if any windows have been drawn, so they (and others
8051 // associated with them) can now be shown.
8052 final int NT = mTokenList.size();
8053 for (i=0; i<NT; i++) {
8054 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8055 if (wtoken == null) {
8056 continue;
8057 }
8058 if (wtoken.freezingScreen) {
8059 int numInteresting = wtoken.numInterestingWindows;
8060 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8061 if (DEBUG_VISIBILITY) Log.v(TAG,
8062 "allDrawn: " + wtoken
8063 + " interesting=" + numInteresting
8064 + " drawn=" + wtoken.numDrawnWindows);
8065 wtoken.showAllWindowsLocked();
8066 unsetAppFreezingScreenLocked(wtoken, false, true);
8067 orientationChangeComplete = true;
8068 }
8069 } else if (!wtoken.allDrawn) {
8070 int numInteresting = wtoken.numInterestingWindows;
8071 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8072 if (DEBUG_VISIBILITY) Log.v(TAG,
8073 "allDrawn: " + wtoken
8074 + " interesting=" + numInteresting
8075 + " drawn=" + wtoken.numDrawnWindows);
8076 wtoken.allDrawn = true;
8077 restart = true;
8078
8079 // We can now show all of the drawn windows!
8080 if (!mOpeningApps.contains(wtoken)) {
8081 wtoken.showAllWindowsLocked();
8082 }
8083 }
8084 }
8085 }
8086 }
8087
8088 // If we are ready to perform an app transition, check through
8089 // all of the app tokens to be shown and see if they are ready
8090 // to go.
8091 if (mAppTransitionReady) {
8092 int NN = mOpeningApps.size();
8093 boolean goodToGo = true;
8094 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8095 "Checking " + NN + " opening apps (frozen="
8096 + mDisplayFrozen + " timeout="
8097 + mAppTransitionTimeout + ")...");
8098 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8099 // If the display isn't frozen, wait to do anything until
8100 // all of the apps are ready. Otherwise just go because
8101 // we'll unfreeze the display when everyone is ready.
8102 for (i=0; i<NN && goodToGo; i++) {
8103 AppWindowToken wtoken = mOpeningApps.get(i);
8104 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8105 "Check opening app" + wtoken + ": allDrawn="
8106 + wtoken.allDrawn + " startingDisplayed="
8107 + wtoken.startingDisplayed);
8108 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8109 && !wtoken.startingMoved) {
8110 goodToGo = false;
8111 }
8112 }
8113 }
8114 if (goodToGo) {
8115 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8116 int transit = mNextAppTransition;
8117 if (mSkipAppTransitionAnimation) {
8118 transit = WindowManagerPolicy.TRANSIT_NONE;
8119 }
8120 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8121 mAppTransitionReady = false;
8122 mAppTransitionTimeout = false;
8123 mStartingIconInTransition = false;
8124 mSkipAppTransitionAnimation = false;
8125
8126 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8127
8128 // We need to figure out which animation to use...
8129 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8130 mOpeningApps, mClosingApps);
8131
8132 NN = mOpeningApps.size();
8133 for (i=0; i<NN; i++) {
8134 AppWindowToken wtoken = mOpeningApps.get(i);
8135 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8136 "Now opening app" + wtoken);
8137 wtoken.reportedVisible = false;
8138 wtoken.inPendingTransaction = false;
8139 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8140 wtoken.updateReportedVisibilityLocked();
8141 wtoken.showAllWindowsLocked();
8142 }
8143 NN = mClosingApps.size();
8144 for (i=0; i<NN; i++) {
8145 AppWindowToken wtoken = mClosingApps.get(i);
8146 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8147 "Now closing app" + wtoken);
8148 wtoken.inPendingTransaction = false;
8149 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8150 wtoken.updateReportedVisibilityLocked();
8151 // Force the allDrawn flag, because we want to start
8152 // this guy's animations regardless of whether it's
8153 // gotten drawn.
8154 wtoken.allDrawn = true;
8155 }
8156
8157 mOpeningApps.clear();
8158 mClosingApps.clear();
8159
8160 // This has changed the visibility of windows, so perform
8161 // a new layout to get them all up-to-date.
8162 mLayoutNeeded = true;
8163 moveInputMethodWindowsIfNeededLocked(true);
8164 performLayoutLockedInner();
8165 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8166
8167 restart = true;
8168 }
8169 }
8170 } while (restart);
8171
8172 // THIRD LOOP: Update the surfaces of all windows.
8173
8174 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8175
8176 boolean obscured = false;
8177 boolean blurring = false;
8178 boolean dimming = false;
8179 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008180 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008181 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182
8183 for (i=N-1; i>=0; i--) {
8184 WindowState w = (WindowState)mWindows.get(i);
8185
8186 boolean displayed = false;
8187 final WindowManager.LayoutParams attrs = w.mAttrs;
8188 final int attrFlags = attrs.flags;
8189
8190 if (w.mSurface != null) {
8191 w.computeShownFrameLocked();
8192 if (localLOGV) Log.v(
8193 TAG, "Placing surface #" + i + " " + w.mSurface
8194 + ": new=" + w.mShownFrame + ", old="
8195 + w.mLastShownFrame);
8196
8197 boolean resize;
8198 int width, height;
8199 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8200 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8201 w.mLastRequestedHeight != w.mRequestedHeight;
8202 // for a scaled surface, we just want to use
8203 // the requested size.
8204 width = w.mRequestedWidth;
8205 height = w.mRequestedHeight;
8206 w.mLastRequestedWidth = width;
8207 w.mLastRequestedHeight = height;
8208 w.mLastShownFrame.set(w.mShownFrame);
8209 try {
8210 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8211 } catch (RuntimeException e) {
8212 Log.w(TAG, "Error positioning surface in " + w, e);
8213 if (!recoveringMemory) {
8214 reclaimSomeSurfaceMemoryLocked(w, "position");
8215 }
8216 }
8217 } else {
8218 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8219 width = w.mShownFrame.width();
8220 height = w.mShownFrame.height();
8221 w.mLastShownFrame.set(w.mShownFrame);
8222 if (resize) {
8223 if (SHOW_TRANSACTIONS) Log.i(
8224 TAG, " SURFACE " + w.mSurface + ": ("
8225 + w.mShownFrame.left + ","
8226 + w.mShownFrame.top + ") ("
8227 + w.mShownFrame.width() + "x"
8228 + w.mShownFrame.height() + ")");
8229 }
8230 }
8231
8232 if (resize) {
8233 if (width < 1) width = 1;
8234 if (height < 1) height = 1;
8235 if (w.mSurface != null) {
8236 try {
8237 w.mSurface.setSize(width, height);
8238 w.mSurface.setPosition(w.mShownFrame.left,
8239 w.mShownFrame.top);
8240 } catch (RuntimeException e) {
8241 // If something goes wrong with the surface (such
8242 // as running out of memory), don't take down the
8243 // entire system.
8244 Log.e(TAG, "Failure updating surface of " + w
8245 + "size=(" + width + "x" + height
8246 + "), pos=(" + w.mShownFrame.left
8247 + "," + w.mShownFrame.top + ")", e);
8248 if (!recoveringMemory) {
8249 reclaimSomeSurfaceMemoryLocked(w, "size");
8250 }
8251 }
8252 }
8253 }
8254 if (!w.mAppFreezing) {
8255 w.mContentInsetsChanged =
8256 !w.mLastContentInsets.equals(w.mContentInsets);
8257 w.mVisibleInsetsChanged =
8258 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008259 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008260 || w.mContentInsetsChanged
8261 || w.mVisibleInsetsChanged) {
8262 w.mLastFrame.set(w.mFrame);
8263 w.mLastContentInsets.set(w.mContentInsets);
8264 w.mLastVisibleInsets.set(w.mVisibleInsets);
8265 // If the orientation is changing, then we need to
8266 // hold off on unfreezing the display until this
8267 // window has been redrawn; to do that, we need
8268 // to go through the process of getting informed
8269 // by the application when it has finished drawing.
8270 if (w.mOrientationChanging) {
8271 if (DEBUG_ORIENTATION) Log.v(TAG,
8272 "Orientation start waiting for draw in "
8273 + w + ", surface " + w.mSurface);
8274 w.mDrawPending = true;
8275 w.mCommitDrawPending = false;
8276 w.mReadyToShow = false;
8277 if (w.mAppToken != null) {
8278 w.mAppToken.allDrawn = false;
8279 }
8280 }
Romain Guy06882f82009-06-10 13:36:04 -07008281 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 "Resizing window " + w + " to " + w.mFrame);
8283 mResizingWindows.add(w);
8284 } else if (w.mOrientationChanging) {
8285 if (!w.mDrawPending && !w.mCommitDrawPending) {
8286 if (DEBUG_ORIENTATION) Log.v(TAG,
8287 "Orientation not waiting for draw in "
8288 + w + ", surface " + w.mSurface);
8289 w.mOrientationChanging = false;
8290 }
8291 }
8292 }
8293
8294 if (w.mAttachedHidden) {
8295 if (!w.mLastHidden) {
8296 //dump();
8297 w.mLastHidden = true;
8298 if (SHOW_TRANSACTIONS) Log.i(
8299 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8300 if (w.mSurface != null) {
8301 try {
8302 w.mSurface.hide();
8303 } catch (RuntimeException e) {
8304 Log.w(TAG, "Exception hiding surface in " + w);
8305 }
8306 }
8307 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8308 }
8309 // If we are waiting for this window to handle an
8310 // orientation change, well, it is hidden, so
8311 // doesn't really matter. Note that this does
8312 // introduce a potential glitch if the window
8313 // becomes unhidden before it has drawn for the
8314 // new orientation.
8315 if (w.mOrientationChanging) {
8316 w.mOrientationChanging = false;
8317 if (DEBUG_ORIENTATION) Log.v(TAG,
8318 "Orientation change skips hidden " + w);
8319 }
8320 } else if (!w.isReadyForDisplay()) {
8321 if (!w.mLastHidden) {
8322 //dump();
8323 w.mLastHidden = true;
8324 if (SHOW_TRANSACTIONS) Log.i(
8325 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8326 if (w.mSurface != null) {
8327 try {
8328 w.mSurface.hide();
8329 } catch (RuntimeException e) {
8330 Log.w(TAG, "Exception exception hiding surface in " + w);
8331 }
8332 }
8333 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8334 }
8335 // If we are waiting for this window to handle an
8336 // orientation change, well, it is hidden, so
8337 // doesn't really matter. Note that this does
8338 // introduce a potential glitch if the window
8339 // becomes unhidden before it has drawn for the
8340 // new orientation.
8341 if (w.mOrientationChanging) {
8342 w.mOrientationChanging = false;
8343 if (DEBUG_ORIENTATION) Log.v(TAG,
8344 "Orientation change skips hidden " + w);
8345 }
8346 } else if (w.mLastLayer != w.mAnimLayer
8347 || w.mLastAlpha != w.mShownAlpha
8348 || w.mLastDsDx != w.mDsDx
8349 || w.mLastDtDx != w.mDtDx
8350 || w.mLastDsDy != w.mDsDy
8351 || w.mLastDtDy != w.mDtDy
8352 || w.mLastHScale != w.mHScale
8353 || w.mLastVScale != w.mVScale
8354 || w.mLastHidden) {
8355 displayed = true;
8356 w.mLastAlpha = w.mShownAlpha;
8357 w.mLastLayer = w.mAnimLayer;
8358 w.mLastDsDx = w.mDsDx;
8359 w.mLastDtDx = w.mDtDx;
8360 w.mLastDsDy = w.mDsDy;
8361 w.mLastDtDy = w.mDtDy;
8362 w.mLastHScale = w.mHScale;
8363 w.mLastVScale = w.mVScale;
8364 if (SHOW_TRANSACTIONS) Log.i(
8365 TAG, " SURFACE " + w.mSurface + ": alpha="
8366 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8367 if (w.mSurface != null) {
8368 try {
8369 w.mSurface.setAlpha(w.mShownAlpha);
8370 w.mSurface.setLayer(w.mAnimLayer);
8371 w.mSurface.setMatrix(
8372 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8373 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8374 } catch (RuntimeException e) {
8375 Log.w(TAG, "Error updating surface in " + w, e);
8376 if (!recoveringMemory) {
8377 reclaimSomeSurfaceMemoryLocked(w, "update");
8378 }
8379 }
8380 }
8381
8382 if (w.mLastHidden && !w.mDrawPending
8383 && !w.mCommitDrawPending
8384 && !w.mReadyToShow) {
8385 if (SHOW_TRANSACTIONS) Log.i(
8386 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8387 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8388 + " during relayout");
8389 if (showSurfaceRobustlyLocked(w)) {
8390 w.mHasDrawn = true;
8391 w.mLastHidden = false;
8392 } else {
8393 w.mOrientationChanging = false;
8394 }
8395 }
8396 if (w.mSurface != null) {
8397 w.mToken.hasVisible = true;
8398 }
8399 } else {
8400 displayed = true;
8401 }
8402
8403 if (displayed) {
8404 if (!covered) {
8405 if (attrs.width == LayoutParams.FILL_PARENT
8406 && attrs.height == LayoutParams.FILL_PARENT) {
8407 covered = true;
8408 }
8409 }
8410 if (w.mOrientationChanging) {
8411 if (w.mDrawPending || w.mCommitDrawPending) {
8412 orientationChangeComplete = false;
8413 if (DEBUG_ORIENTATION) Log.v(TAG,
8414 "Orientation continue waiting for draw in " + w);
8415 } else {
8416 w.mOrientationChanging = false;
8417 if (DEBUG_ORIENTATION) Log.v(TAG,
8418 "Orientation change complete in " + w);
8419 }
8420 }
8421 w.mToken.hasVisible = true;
8422 }
8423 } else if (w.mOrientationChanging) {
8424 if (DEBUG_ORIENTATION) Log.v(TAG,
8425 "Orientation change skips hidden " + w);
8426 w.mOrientationChanging = false;
8427 }
8428
8429 final boolean canBeSeen = w.isDisplayedLw();
8430
8431 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8432 focusDisplayed = true;
8433 }
8434
8435 // Update effect.
8436 if (!obscured) {
8437 if (w.mSurface != null) {
8438 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8439 holdScreen = w.mSession;
8440 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008441 if (!syswin && w.mAttrs.screenBrightness >= 0
8442 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008443 screenBrightness = w.mAttrs.screenBrightness;
8444 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008445 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8446 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8447 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8448 syswin = true;
8449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008450 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008451
8452 boolean opaqueDrawn = w.isOpaqueDrawn();
8453 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008454 // This window completely covers everything behind it,
8455 // so we want to leave all of them as unblurred (for
8456 // performance reasons).
8457 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008458 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8459 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
8460 // This window is in compatibility mode, and needs background filler.
8461 obscured = true;
8462 if (mBackgroundFillerSurface == null) {
8463 try {
8464 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8465 0, dw, dh,
8466 PixelFormat.OPAQUE,
8467 Surface.FX_SURFACE_NORMAL);
8468 } catch (Exception e) {
8469 Log.e(TAG, "Exception creating filler surface", e);
8470 }
8471 }
8472 try {
8473 mBackgroundFillerSurface.setPosition(0, 0);
8474 mBackgroundFillerSurface.setSize(dw, dh);
8475 // Using the same layer as Dim because they will never be shown at the
8476 // same time.
8477 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8478 mBackgroundFillerSurface.show();
8479 } catch (RuntimeException e) {
8480 Log.e(TAG, "Exception showing filler surface");
8481 }
8482 backgroundFillerShown = true;
8483 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008484 } else if (canBeSeen && !obscured &&
8485 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8486 if (localLOGV) Log.v(TAG, "Win " + w
8487 + ": blurring=" + blurring
8488 + " obscured=" + obscured
8489 + " displayed=" + displayed);
8490 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8491 if (!dimming) {
8492 //Log.i(TAG, "DIM BEHIND: " + w);
8493 dimming = true;
8494 mDimShown = true;
8495 if (mDimSurface == null) {
8496 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8497 + mDimSurface + ": CREATE");
8498 try {
Romain Guy06882f82009-06-10 13:36:04 -07008499 mDimSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008500 -1, 16, 16,
8501 PixelFormat.OPAQUE,
8502 Surface.FX_SURFACE_DIM);
8503 } catch (Exception e) {
8504 Log.e(TAG, "Exception creating Dim surface", e);
8505 }
8506 }
8507 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8508 + mDimSurface + ": SHOW pos=(0,0) (" +
8509 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8510 if (mDimSurface != null) {
8511 try {
8512 mDimSurface.setPosition(0, 0);
8513 mDimSurface.setSize(dw, dh);
8514 mDimSurface.show();
8515 } catch (RuntimeException e) {
8516 Log.w(TAG, "Failure showing dim surface", e);
8517 }
8518 }
8519 }
8520 mDimSurface.setLayer(w.mAnimLayer-1);
8521 final float target = w.mExiting ? 0 : attrs.dimAmount;
8522 if (mDimTargetAlpha != target) {
8523 // If the desired dim level has changed, then
8524 // start an animation to it.
8525 mLastDimAnimTime = currentTime;
8526 long duration = (w.mAnimating && w.mAnimation != null)
8527 ? w.mAnimation.computeDurationHint()
8528 : DEFAULT_DIM_DURATION;
8529 if (target > mDimTargetAlpha) {
8530 // This is happening behind the activity UI,
8531 // so we can make it run a little longer to
8532 // give a stronger impression without disrupting
8533 // the user.
8534 duration *= DIM_DURATION_MULTIPLIER;
8535 }
8536 if (duration < 1) {
8537 // Don't divide by zero
8538 duration = 1;
8539 }
8540 mDimTargetAlpha = target;
8541 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha)
8542 / duration;
8543 }
8544 }
8545 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8546 if (!blurring) {
8547 //Log.i(TAG, "BLUR BEHIND: " + w);
8548 blurring = true;
8549 mBlurShown = true;
8550 if (mBlurSurface == null) {
8551 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8552 + mBlurSurface + ": CREATE");
8553 try {
Romain Guy06882f82009-06-10 13:36:04 -07008554 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 -1, 16, 16,
8556 PixelFormat.OPAQUE,
8557 Surface.FX_SURFACE_BLUR);
8558 } catch (Exception e) {
8559 Log.e(TAG, "Exception creating Blur surface", e);
8560 }
8561 }
8562 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8563 + mBlurSurface + ": SHOW pos=(0,0) (" +
8564 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8565 if (mBlurSurface != null) {
8566 mBlurSurface.setPosition(0, 0);
8567 mBlurSurface.setSize(dw, dh);
8568 try {
8569 mBlurSurface.show();
8570 } catch (RuntimeException e) {
8571 Log.w(TAG, "Failure showing blur surface", e);
8572 }
8573 }
8574 }
8575 mBlurSurface.setLayer(w.mAnimLayer-2);
8576 }
8577 }
8578 }
8579 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008580
8581 if (backgroundFillerShown == false && mBackgroundFillerShown) {
8582 mBackgroundFillerShown = false;
8583 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
8584 try {
8585 mBackgroundFillerSurface.hide();
8586 } catch (RuntimeException e) {
8587 Log.e(TAG, "Exception hiding filler surface", e);
8588 }
8589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008590
8591 if (!dimming && mDimShown) {
8592 // Time to hide the dim surface... start fading.
8593 if (mDimTargetAlpha != 0) {
8594 mLastDimAnimTime = currentTime;
8595 mDimTargetAlpha = 0;
8596 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
8597 }
8598 }
8599
8600 if (mDimShown && mLastDimAnimTime != 0) {
8601 mDimCurrentAlpha += mDimDeltaPerMs
8602 * (currentTime-mLastDimAnimTime);
8603 boolean more = true;
8604 if (mDisplayFrozen) {
8605 // If the display is frozen, there is no reason to animate.
8606 more = false;
8607 } else if (mDimDeltaPerMs > 0) {
8608 if (mDimCurrentAlpha > mDimTargetAlpha) {
8609 more = false;
8610 }
8611 } else if (mDimDeltaPerMs < 0) {
8612 if (mDimCurrentAlpha < mDimTargetAlpha) {
8613 more = false;
8614 }
8615 } else {
8616 more = false;
8617 }
Romain Guy06882f82009-06-10 13:36:04 -07008618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008619 // Do we need to continue animating?
8620 if (more) {
8621 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8622 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
8623 mLastDimAnimTime = currentTime;
8624 mDimSurface.setAlpha(mDimCurrentAlpha);
8625 animating = true;
8626 } else {
8627 mDimCurrentAlpha = mDimTargetAlpha;
8628 mLastDimAnimTime = 0;
8629 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
8630 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
8631 mDimSurface.setAlpha(mDimCurrentAlpha);
8632 if (!dimming) {
8633 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
8634 + ": HIDE");
8635 try {
8636 mDimSurface.hide();
8637 } catch (RuntimeException e) {
8638 Log.w(TAG, "Illegal argument exception hiding dim surface");
8639 }
8640 mDimShown = false;
8641 }
8642 }
8643 }
Romain Guy06882f82009-06-10 13:36:04 -07008644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008645 if (!blurring && mBlurShown) {
8646 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8647 + ": HIDE");
8648 try {
8649 mBlurSurface.hide();
8650 } catch (IllegalArgumentException e) {
8651 Log.w(TAG, "Illegal argument exception hiding blur surface");
8652 }
8653 mBlurShown = false;
8654 }
8655
8656 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8657 } catch (RuntimeException e) {
8658 Log.e(TAG, "Unhandled exception in Window Manager", e);
8659 }
8660
8661 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008663 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8664 "With display frozen, orientationChangeComplete="
8665 + orientationChangeComplete);
8666 if (orientationChangeComplete) {
8667 if (mWindowsFreezingScreen) {
8668 mWindowsFreezingScreen = false;
8669 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8670 }
8671 if (mAppsFreezingScreen == 0) {
8672 stopFreezingDisplayLocked();
8673 }
8674 }
Romain Guy06882f82009-06-10 13:36:04 -07008675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008676 i = mResizingWindows.size();
8677 if (i > 0) {
8678 do {
8679 i--;
8680 WindowState win = mResizingWindows.get(i);
8681 try {
8682 win.mClient.resized(win.mFrame.width(),
8683 win.mFrame.height(), win.mLastContentInsets,
8684 win.mLastVisibleInsets, win.mDrawPending);
8685 win.mContentInsetsChanged = false;
8686 win.mVisibleInsetsChanged = false;
8687 } catch (RemoteException e) {
8688 win.mOrientationChanging = false;
8689 }
8690 } while (i > 0);
8691 mResizingWindows.clear();
8692 }
Romain Guy06882f82009-06-10 13:36:04 -07008693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008694 // Destroy the surface of any windows that are no longer visible.
8695 i = mDestroySurface.size();
8696 if (i > 0) {
8697 do {
8698 i--;
8699 WindowState win = mDestroySurface.get(i);
8700 win.mDestroying = false;
8701 if (mInputMethodWindow == win) {
8702 mInputMethodWindow = null;
8703 }
8704 win.destroySurfaceLocked();
8705 } while (i > 0);
8706 mDestroySurface.clear();
8707 }
8708
8709 // Time to remove any exiting tokens?
8710 for (i=mExitingTokens.size()-1; i>=0; i--) {
8711 WindowToken token = mExitingTokens.get(i);
8712 if (!token.hasVisible) {
8713 mExitingTokens.remove(i);
8714 }
8715 }
8716
8717 // Time to remove any exiting applications?
8718 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8719 AppWindowToken token = mExitingAppTokens.get(i);
8720 if (!token.hasVisible && !mClosingApps.contains(token)) {
8721 mAppTokens.remove(token);
8722 mExitingAppTokens.remove(i);
8723 }
8724 }
8725
8726 if (focusDisplayed) {
8727 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8728 }
8729 if (animating) {
8730 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8731 }
8732 mQueue.setHoldScreenLocked(holdScreen != null);
8733 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8734 mPowerManager.setScreenBrightnessOverride(-1);
8735 } else {
8736 mPowerManager.setScreenBrightnessOverride((int)
8737 (screenBrightness * Power.BRIGHTNESS_ON));
8738 }
8739 if (holdScreen != mHoldingScreenOn) {
8740 mHoldingScreenOn = holdScreen;
8741 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8742 mH.sendMessage(m);
8743 }
8744 }
8745
8746 void requestAnimationLocked(long delay) {
8747 if (!mAnimationPending) {
8748 mAnimationPending = true;
8749 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8750 }
8751 }
Romain Guy06882f82009-06-10 13:36:04 -07008752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 /**
8754 * Have the surface flinger show a surface, robustly dealing with
8755 * error conditions. In particular, if there is not enough memory
8756 * to show the surface, then we will try to get rid of other surfaces
8757 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07008758 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008759 * @return Returns true if the surface was successfully shown.
8760 */
8761 boolean showSurfaceRobustlyLocked(WindowState win) {
8762 try {
8763 if (win.mSurface != null) {
8764 win.mSurface.show();
8765 }
8766 return true;
8767 } catch (RuntimeException e) {
8768 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8769 }
Romain Guy06882f82009-06-10 13:36:04 -07008770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008771 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07008772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008773 return false;
8774 }
Romain Guy06882f82009-06-10 13:36:04 -07008775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008776 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8777 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07008778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008779 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8780 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07008781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008782 if (mForceRemoves == null) {
8783 mForceRemoves = new ArrayList<WindowState>();
8784 }
Romain Guy06882f82009-06-10 13:36:04 -07008785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008786 long callingIdentity = Binder.clearCallingIdentity();
8787 try {
8788 // There was some problem... first, do a sanity check of the
8789 // window list to make sure we haven't left any dangling surfaces
8790 // around.
8791 int N = mWindows.size();
8792 boolean leakedSurface = false;
8793 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8794 for (int i=0; i<N; i++) {
8795 WindowState ws = (WindowState)mWindows.get(i);
8796 if (ws.mSurface != null) {
8797 if (!mSessions.contains(ws.mSession)) {
8798 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8799 + ws + " surface=" + ws.mSurface
8800 + " token=" + win.mToken
8801 + " pid=" + ws.mSession.mPid
8802 + " uid=" + ws.mSession.mUid);
8803 ws.mSurface.clear();
8804 ws.mSurface = null;
8805 mForceRemoves.add(ws);
8806 i--;
8807 N--;
8808 leakedSurface = true;
8809 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
8810 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
8811 + ws + " surface=" + ws.mSurface
8812 + " token=" + win.mAppToken);
8813 ws.mSurface.clear();
8814 ws.mSurface = null;
8815 leakedSurface = true;
8816 }
8817 }
8818 }
Romain Guy06882f82009-06-10 13:36:04 -07008819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 boolean killedApps = false;
8821 if (!leakedSurface) {
8822 Log.w(TAG, "No leaked surfaces; killing applicatons!");
8823 SparseIntArray pidCandidates = new SparseIntArray();
8824 for (int i=0; i<N; i++) {
8825 WindowState ws = (WindowState)mWindows.get(i);
8826 if (ws.mSurface != null) {
8827 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
8828 }
8829 }
8830 if (pidCandidates.size() > 0) {
8831 int[] pids = new int[pidCandidates.size()];
8832 for (int i=0; i<pids.length; i++) {
8833 pids[i] = pidCandidates.keyAt(i);
8834 }
8835 try {
8836 if (mActivityManager.killPidsForMemory(pids)) {
8837 killedApps = true;
8838 }
8839 } catch (RemoteException e) {
8840 }
8841 }
8842 }
Romain Guy06882f82009-06-10 13:36:04 -07008843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008844 if (leakedSurface || killedApps) {
8845 // We managed to reclaim some memory, so get rid of the trouble
8846 // surface and ask the app to request another one.
8847 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
8848 if (surface != null) {
8849 surface.clear();
8850 win.mSurface = null;
8851 }
Romain Guy06882f82009-06-10 13:36:04 -07008852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 try {
8854 win.mClient.dispatchGetNewSurface();
8855 } catch (RemoteException e) {
8856 }
8857 }
8858 } finally {
8859 Binder.restoreCallingIdentity(callingIdentity);
8860 }
8861 }
Romain Guy06882f82009-06-10 13:36:04 -07008862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008863 private boolean updateFocusedWindowLocked(int mode) {
8864 WindowState newFocus = computeFocusedWindowLocked();
8865 if (mCurrentFocus != newFocus) {
8866 // This check makes sure that we don't already have the focus
8867 // change message pending.
8868 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
8869 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
8870 if (localLOGV) Log.v(
8871 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
8872 final WindowState oldFocus = mCurrentFocus;
8873 mCurrentFocus = newFocus;
8874 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07008875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008876 final WindowState imWindow = mInputMethodWindow;
8877 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008878 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008879 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008880 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
8881 mLayoutNeeded = true;
8882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008883 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
8884 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008885 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
8886 // Client will do the layout, but we need to assign layers
8887 // for handleNewWindowLocked() below.
8888 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 }
8890 }
Romain Guy06882f82009-06-10 13:36:04 -07008891
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008892 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
8893 mKeyWaiter.handleNewWindowLocked(newFocus);
8894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008895 return true;
8896 }
8897 return false;
8898 }
8899
8900 private WindowState computeFocusedWindowLocked() {
8901 WindowState result = null;
8902 WindowState win;
8903
8904 int i = mWindows.size() - 1;
8905 int nextAppIndex = mAppTokens.size()-1;
8906 WindowToken nextApp = nextAppIndex >= 0
8907 ? mAppTokens.get(nextAppIndex) : null;
8908
8909 while (i >= 0) {
8910 win = (WindowState)mWindows.get(i);
8911
8912 if (localLOGV || DEBUG_FOCUS) Log.v(
8913 TAG, "Looking for focus: " + i
8914 + " = " + win
8915 + ", flags=" + win.mAttrs.flags
8916 + ", canReceive=" + win.canReceiveKeys());
8917
8918 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07008919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008920 // If this window's application has been removed, just skip it.
8921 if (thisApp != null && thisApp.removed) {
8922 i--;
8923 continue;
8924 }
Romain Guy06882f82009-06-10 13:36:04 -07008925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008926 // If there is a focused app, don't allow focus to go to any
8927 // windows below it. If this is an application window, step
8928 // through the app tokens until we find its app.
8929 if (thisApp != null && nextApp != null && thisApp != nextApp
8930 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
8931 int origAppIndex = nextAppIndex;
8932 while (nextAppIndex > 0) {
8933 if (nextApp == mFocusedApp) {
8934 // Whoops, we are below the focused app... no focus
8935 // for you!
8936 if (localLOGV || DEBUG_FOCUS) Log.v(
8937 TAG, "Reached focused app: " + mFocusedApp);
8938 return null;
8939 }
8940 nextAppIndex--;
8941 nextApp = mAppTokens.get(nextAppIndex);
8942 if (nextApp == thisApp) {
8943 break;
8944 }
8945 }
8946 if (thisApp != nextApp) {
8947 // Uh oh, the app token doesn't exist! This shouldn't
8948 // happen, but if it does we can get totally hosed...
8949 // so restart at the original app.
8950 nextAppIndex = origAppIndex;
8951 nextApp = mAppTokens.get(nextAppIndex);
8952 }
8953 }
8954
8955 // Dispatch to this window if it is wants key events.
8956 if (win.canReceiveKeys()) {
8957 if (DEBUG_FOCUS) Log.v(
8958 TAG, "Found focus @ " + i + " = " + win);
8959 result = win;
8960 break;
8961 }
8962
8963 i--;
8964 }
8965
8966 return result;
8967 }
8968
8969 private void startFreezingDisplayLocked() {
8970 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07008971 // Freezing the display also suspends key event delivery, to
8972 // keep events from going astray while the display is reconfigured.
8973 // If someone has changed orientation again while the screen is
8974 // still frozen, the events will continue to be blocked while the
8975 // successive orientation change is processed. To prevent spurious
8976 // ANRs, we reset the event dispatch timeout in this case.
8977 synchronized (mKeyWaiter) {
8978 mKeyWaiter.mWasFrozen = true;
8979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008980 return;
8981 }
Romain Guy06882f82009-06-10 13:36:04 -07008982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008983 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07008984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008985 long now = SystemClock.uptimeMillis();
8986 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
8987 if (mFreezeGcPending != 0) {
8988 if (now > (mFreezeGcPending+1000)) {
8989 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
8990 mH.removeMessages(H.FORCE_GC);
8991 Runtime.getRuntime().gc();
8992 mFreezeGcPending = now;
8993 }
8994 } else {
8995 mFreezeGcPending = now;
8996 }
Romain Guy06882f82009-06-10 13:36:04 -07008997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008998 mDisplayFrozen = true;
8999 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9000 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9001 mAppTransitionReady = true;
9002 }
Romain Guy06882f82009-06-10 13:36:04 -07009003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009004 if (PROFILE_ORIENTATION) {
9005 File file = new File("/data/system/frozen");
9006 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9007 }
9008 Surface.freezeDisplay(0);
9009 }
Romain Guy06882f82009-06-10 13:36:04 -07009010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009011 private void stopFreezingDisplayLocked() {
9012 if (!mDisplayFrozen) {
9013 return;
9014 }
Romain Guy06882f82009-06-10 13:36:04 -07009015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009016 mDisplayFrozen = false;
9017 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9018 if (PROFILE_ORIENTATION) {
9019 Debug.stopMethodTracing();
9020 }
9021 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009022
Chris Tate2ad63a92009-03-25 17:36:48 -07009023 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9024 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009025 synchronized (mKeyWaiter) {
9026 mKeyWaiter.mWasFrozen = true;
9027 mKeyWaiter.notifyAll();
9028 }
9029
9030 // A little kludge: a lot could have happened while the
9031 // display was frozen, so now that we are coming back we
9032 // do a gc so that any remote references the system
9033 // processes holds on others can be released if they are
9034 // no longer needed.
9035 mH.removeMessages(H.FORCE_GC);
9036 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9037 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009039 mScreenFrozenLock.release();
9040 }
Romain Guy06882f82009-06-10 13:36:04 -07009041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009042 @Override
9043 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9044 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9045 != PackageManager.PERMISSION_GRANTED) {
9046 pw.println("Permission Denial: can't dump WindowManager from from pid="
9047 + Binder.getCallingPid()
9048 + ", uid=" + Binder.getCallingUid());
9049 return;
9050 }
Romain Guy06882f82009-06-10 13:36:04 -07009051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009052 synchronized(mWindowMap) {
9053 pw.println("Current Window Manager state:");
9054 for (int i=mWindows.size()-1; i>=0; i--) {
9055 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009056 pw.print(" Window #"); pw.print(i); pw.print(' ');
9057 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009058 w.dump(pw, " ");
9059 }
9060 if (mInputMethodDialogs.size() > 0) {
9061 pw.println(" ");
9062 pw.println(" Input method dialogs:");
9063 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9064 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009065 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009066 }
9067 }
9068 if (mPendingRemove.size() > 0) {
9069 pw.println(" ");
9070 pw.println(" Remove pending for:");
9071 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9072 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009073 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9074 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009075 w.dump(pw, " ");
9076 }
9077 }
9078 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9079 pw.println(" ");
9080 pw.println(" Windows force removing:");
9081 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9082 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009083 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9084 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009085 w.dump(pw, " ");
9086 }
9087 }
9088 if (mDestroySurface.size() > 0) {
9089 pw.println(" ");
9090 pw.println(" Windows waiting to destroy their surface:");
9091 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9092 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009093 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9094 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 w.dump(pw, " ");
9096 }
9097 }
9098 if (mLosingFocus.size() > 0) {
9099 pw.println(" ");
9100 pw.println(" Windows losing focus:");
9101 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9102 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009103 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9104 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009105 w.dump(pw, " ");
9106 }
9107 }
9108 if (mSessions.size() > 0) {
9109 pw.println(" ");
9110 pw.println(" All active sessions:");
9111 Iterator<Session> it = mSessions.iterator();
9112 while (it.hasNext()) {
9113 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009114 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 s.dump(pw, " ");
9116 }
9117 }
9118 if (mTokenMap.size() > 0) {
9119 pw.println(" ");
9120 pw.println(" All tokens:");
9121 Iterator<WindowToken> it = mTokenMap.values().iterator();
9122 while (it.hasNext()) {
9123 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009124 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009125 token.dump(pw, " ");
9126 }
9127 }
9128 if (mTokenList.size() > 0) {
9129 pw.println(" ");
9130 pw.println(" Window token list:");
9131 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009132 pw.print(" #"); pw.print(i); pw.print(": ");
9133 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009134 }
9135 }
9136 if (mAppTokens.size() > 0) {
9137 pw.println(" ");
9138 pw.println(" Application tokens in Z order:");
9139 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009140 pw.print(" App #"); pw.print(i); pw.print(": ");
9141 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009142 }
9143 }
9144 if (mFinishedStarting.size() > 0) {
9145 pw.println(" ");
9146 pw.println(" Finishing start of application tokens:");
9147 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9148 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009149 pw.print(" Finished Starting #"); pw.print(i);
9150 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009151 token.dump(pw, " ");
9152 }
9153 }
9154 if (mExitingTokens.size() > 0) {
9155 pw.println(" ");
9156 pw.println(" Exiting tokens:");
9157 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9158 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009159 pw.print(" Exiting #"); pw.print(i);
9160 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009161 token.dump(pw, " ");
9162 }
9163 }
9164 if (mExitingAppTokens.size() > 0) {
9165 pw.println(" ");
9166 pw.println(" Exiting application tokens:");
9167 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9168 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009169 pw.print(" Exiting App #"); pw.print(i);
9170 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009171 token.dump(pw, " ");
9172 }
9173 }
9174 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009175 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9176 pw.print(" mLastFocus="); pw.println(mLastFocus);
9177 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9178 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9179 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9180 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9181 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9182 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9183 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9184 pw.print(" mBlurShown="); pw.println(mBlurShown);
9185 pw.print(" mDimShown="); pw.print(mDimShown);
9186 pw.print(" current="); pw.print(mDimCurrentAlpha);
9187 pw.print(" target="); pw.print(mDimTargetAlpha);
9188 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9189 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9190 pw.print(" mInputMethodAnimLayerAdjustment=");
9191 pw.println(mInputMethodAnimLayerAdjustment);
9192 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9193 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9194 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9195 pw.print(" mRotation="); pw.print(mRotation);
9196 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9197 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9198 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9199 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9200 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9201 pw.print(" mNextAppTransition=0x");
9202 pw.print(Integer.toHexString(mNextAppTransition));
9203 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9204 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9205 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9206 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9207 if (mOpeningApps.size() > 0) {
9208 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9209 }
9210 if (mClosingApps.size() > 0) {
9211 pw.print(" mClosingApps="); pw.println(mClosingApps);
9212 }
9213 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9214 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009216 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9217 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9218 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9219 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9220 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9221 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009222 }
9223 }
9224
9225 public void monitor() {
9226 synchronized (mWindowMap) { }
9227 synchronized (mKeyguardDisabled) { }
9228 synchronized (mKeyWaiter) { }
9229 }
9230}