blob: 0be841790d871a9a333acabf006294acf3770123 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import static android.os.LocalPowerManager.CHEEK_EVENT;
20import static android.os.LocalPowerManager.OTHER_EVENT;
21import static android.os.LocalPowerManager.TOUCH_EVENT;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -070022import static android.os.LocalPowerManager.LONG_TOUCH_EVENT;
23import static android.os.LocalPowerManager.TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
25import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
26import static android.view.WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070027import static android.view.WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
29import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -070030import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
32import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
33import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
34import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
35import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
36import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_GPU;
37import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_HARDWARE;
38import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
39import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
40import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
41import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
42import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
43
44import com.android.internal.app.IBatteryStats;
45import com.android.internal.policy.PolicyManager;
46import com.android.internal.view.IInputContext;
47import com.android.internal.view.IInputMethodClient;
48import com.android.internal.view.IInputMethodManager;
49import com.android.server.KeyInputQueue.QueuedEvent;
50import com.android.server.am.BatteryStatsService;
51
52import android.Manifest;
53import android.app.ActivityManagerNative;
54import android.app.IActivityManager;
55import android.content.Context;
56import android.content.pm.ActivityInfo;
57import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070058import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.res.Configuration;
60import android.graphics.Matrix;
61import android.graphics.PixelFormat;
62import android.graphics.Rect;
63import android.graphics.Region;
64import android.os.BatteryStats;
65import android.os.Binder;
66import android.os.Debug;
67import android.os.Handler;
68import android.os.IBinder;
69import android.os.LocalPowerManager;
70import android.os.Looper;
71import android.os.Message;
72import android.os.Parcel;
73import android.os.ParcelFileDescriptor;
74import android.os.Power;
75import android.os.PowerManager;
76import android.os.Process;
77import android.os.RemoteException;
78import android.os.ServiceManager;
79import android.os.SystemClock;
80import android.os.SystemProperties;
81import android.os.TokenWatcher;
82import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070083import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.EventLog;
85import android.util.Log;
86import android.util.SparseIntArray;
87import android.view.Display;
88import android.view.Gravity;
89import android.view.IApplicationToken;
90import android.view.IOnKeyguardExitResult;
91import android.view.IRotationWatcher;
92import android.view.IWindow;
93import android.view.IWindowManager;
94import android.view.IWindowSession;
95import android.view.KeyEvent;
96import android.view.MotionEvent;
97import android.view.RawInputEvent;
98import android.view.Surface;
99import android.view.SurfaceSession;
100import android.view.View;
101import android.view.ViewTreeObserver;
102import android.view.WindowManager;
103import android.view.WindowManagerImpl;
104import android.view.WindowManagerPolicy;
105import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700106import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.view.animation.Animation;
108import android.view.animation.AnimationUtils;
109import android.view.animation.Transformation;
110
111import java.io.BufferedWriter;
112import java.io.File;
113import java.io.FileDescriptor;
114import java.io.IOException;
115import java.io.OutputStream;
116import java.io.OutputStreamWriter;
117import java.io.PrintWriter;
118import java.io.StringWriter;
119import java.net.Socket;
120import java.util.ArrayList;
121import java.util.HashMap;
122import java.util.HashSet;
123import java.util.Iterator;
124import java.util.List;
125
126/** {@hide} */
127public class WindowManagerService extends IWindowManager.Stub implements Watchdog.Monitor {
128 static final String TAG = "WindowManager";
129 static final boolean DEBUG = false;
130 static final boolean DEBUG_FOCUS = false;
131 static final boolean DEBUG_ANIM = false;
132 static final boolean DEBUG_LAYERS = false;
133 static final boolean DEBUG_INPUT = false;
134 static final boolean DEBUG_INPUT_METHOD = false;
135 static final boolean DEBUG_VISIBILITY = false;
136 static final boolean DEBUG_ORIENTATION = false;
137 static final boolean DEBUG_APP_TRANSITIONS = false;
138 static final boolean DEBUG_STARTING_WINDOW = false;
139 static final boolean DEBUG_REORDER = false;
140 static final boolean SHOW_TRANSACTIONS = false;
Romain Guy06882f82009-06-10 13:36:04 -0700141
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
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700178 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
179 * compatible windows.
180 */
181 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 /** Adjustment to time to perform a dim, to make it more dramatic.
184 */
185 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700186
Dianne Hackborncfaef692009-06-15 14:24:44 -0700187 static final int INJECT_FAILED = 0;
188 static final int INJECT_SUCCEEDED = 1;
189 static final int INJECT_NO_PERMISSION = -1;
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 static final int UPDATE_FOCUS_NORMAL = 0;
192 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
193 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
194 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700195
Michael Chane96440f2009-05-06 10:27:36 -0700196 /** The minimum time between dispatching touch events. */
197 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
198
199 // Last touch event time
200 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700201
Michael Chane96440f2009-05-06 10:27:36 -0700202 // Last touch event type
203 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700204
Michael Chane96440f2009-05-06 10:27:36 -0700205 // Time to wait before calling useractivity again. This saves CPU usage
206 // when we get a flood of touch events.
207 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
208
209 // Last time we call user activity
210 long mLastUserActivityCallTime = 0;
211
Romain Guy06882f82009-06-10 13:36:04 -0700212 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700213 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700216 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217
218 /**
219 * Condition waited on by {@link #reenableKeyguard} to know the call to
220 * the window policy has finished.
221 */
222 private boolean mWaitingUntilKeyguardReenabled = false;
223
224
225 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
226 new Handler(), "WindowManagerService.mKeyguardDisabled") {
227 public void acquired() {
228 mPolicy.enableKeyguard(false);
229 }
230 public void released() {
231 synchronized (mKeyguardDisabled) {
232 mPolicy.enableKeyguard(true);
233 mWaitingUntilKeyguardReenabled = false;
234 mKeyguardDisabled.notifyAll();
235 }
236 }
237 };
238
239 final Context mContext;
240
241 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
246
247 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 /**
252 * All currently active sessions with clients.
253 */
254 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * Mapping from an IWindow IBinder to the server's Window object.
258 * This is also used as the lock for all of our state.
259 */
260 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
261
262 /**
263 * Mapping from a token IBinder to a WindowToken object.
264 */
265 final HashMap<IBinder, WindowToken> mTokenMap =
266 new HashMap<IBinder, WindowToken>();
267
268 /**
269 * The same tokens as mTokenMap, stored in a list for efficient iteration
270 * over them.
271 */
272 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 /**
275 * Window tokens that are in the process of exiting, but still
276 * on screen for animations.
277 */
278 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
279
280 /**
281 * Z-ordered (bottom-most first) list of all application tokens, for
282 * controlling the ordering of windows in different applications. This
283 * contains WindowToken objects.
284 */
285 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
286
287 /**
288 * Application tokens that are in the process of exiting, but still
289 * on screen for animations.
290 */
291 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
292
293 /**
294 * List of window tokens that have finished starting their application,
295 * and now need to have the policy remove their windows.
296 */
297 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
298
299 /**
300 * Z-ordered (bottom-most first) list of all Window objects.
301 */
302 final ArrayList mWindows = new ArrayList();
303
304 /**
305 * Windows that are being resized. Used so we can tell the client about
306 * the resize after closing the transaction in which we resized the
307 * underlying surface.
308 */
309 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
310
311 /**
312 * Windows whose animations have ended and now must be removed.
313 */
314 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
315
316 /**
317 * Windows whose surface should be destroyed.
318 */
319 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
320
321 /**
322 * Windows that have lost input focus and are waiting for the new
323 * focus window to be displayed before they are told about this.
324 */
325 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
326
327 /**
328 * This is set when we have run out of memory, and will either be an empty
329 * list or contain windows that need to be force removed.
330 */
331 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700336 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 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 Hackbornc4db95c2009-07-21 17:46:02 -0700423 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_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) {
515 mContext = context;
516 mHaveInputMethods = haveInputMethods;
517 mLimitedAlphaCompositing = context.getResources().getBoolean(
518 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 mPowerManager = pm;
521 mPowerManager.setPolicy(mPolicy);
522 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
523 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
524 "SCREEN_FROZEN");
525 mScreenFrozenLock.setReferenceCounted(false);
526
527 mActivityManager = ActivityManagerNative.getDefault();
528 mBatteryStats = BatteryStatsService.getService();
529
530 // Get persisted window scale setting
531 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
532 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
533 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
534 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 mQueue = new KeyQ();
537
538 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
541 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 synchronized (thr) {
544 while (!thr.mRunning) {
545 try {
546 thr.wait();
547 } catch (InterruptedException e) {
548 }
549 }
550 }
Romain Guy06882f82009-06-10 13:36:04 -0700551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 // Add ourself to the Watchdog monitors.
555 Watchdog.getInstance().addMonitor(this);
556 }
557
558 @Override
559 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
560 throws RemoteException {
561 try {
562 return super.onTransact(code, data, reply, flags);
563 } catch (RuntimeException e) {
564 // The window manager only throws security exceptions, so let's
565 // log all others.
566 if (!(e instanceof SecurityException)) {
567 Log.e(TAG, "Window Manager Crash", e);
568 }
569 throw e;
570 }
571 }
572
573 private void placeWindowAfter(Object pos, WindowState window) {
574 final int i = mWindows.indexOf(pos);
575 if (localLOGV || DEBUG_FOCUS) Log.v(
576 TAG, "Adding window " + window + " at "
577 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
578 mWindows.add(i+1, window);
579 }
580
581 private void placeWindowBefore(Object pos, WindowState window) {
582 final int i = mWindows.indexOf(pos);
583 if (localLOGV || DEBUG_FOCUS) Log.v(
584 TAG, "Adding window " + window + " at "
585 + i + " of " + mWindows.size() + " (before " + pos + ")");
586 mWindows.add(i, window);
587 }
588
589 //This method finds out the index of a window that has the same app token as
590 //win. used for z ordering the windows in mWindows
591 private int findIdxBasedOnAppTokens(WindowState win) {
592 //use a local variable to cache mWindows
593 ArrayList localmWindows = mWindows;
594 int jmax = localmWindows.size();
595 if(jmax == 0) {
596 return -1;
597 }
598 for(int j = (jmax-1); j >= 0; j--) {
599 WindowState wentry = (WindowState)localmWindows.get(j);
600 if(wentry.mAppToken == win.mAppToken) {
601 return j;
602 }
603 }
604 return -1;
605 }
Romain Guy06882f82009-06-10 13:36:04 -0700606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
608 final IWindow client = win.mClient;
609 final WindowToken token = win.mToken;
610 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 final int N = localmWindows.size();
613 final WindowState attached = win.mAttachedWindow;
614 int i;
615 if (attached == null) {
616 int tokenWindowsPos = token.windows.size();
617 if (token.appWindowToken != null) {
618 int index = tokenWindowsPos-1;
619 if (index >= 0) {
620 // If this application has existing windows, we
621 // simply place the new window on top of them... but
622 // keep the starting window on top.
623 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
624 // Base windows go behind everything else.
625 placeWindowBefore(token.windows.get(0), win);
626 tokenWindowsPos = 0;
627 } else {
628 AppWindowToken atoken = win.mAppToken;
629 if (atoken != null &&
630 token.windows.get(index) == atoken.startingWindow) {
631 placeWindowBefore(token.windows.get(index), win);
632 tokenWindowsPos--;
633 } else {
634 int newIdx = findIdxBasedOnAppTokens(win);
635 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700636 //there is a window above this one associated with the same
637 //apptoken note that the window could be a floating window
638 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 //windows associated with this token.
640 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643 }
644 } else {
645 if (localLOGV) Log.v(
646 TAG, "Figuring out where to add app window "
647 + client.asBinder() + " (token=" + token + ")");
648 // Figure out where the window should go, based on the
649 // order of applications.
650 final int NA = mAppTokens.size();
651 Object pos = null;
652 for (i=NA-1; i>=0; i--) {
653 AppWindowToken t = mAppTokens.get(i);
654 if (t == token) {
655 i--;
656 break;
657 }
658 if (t.windows.size() > 0) {
659 pos = t.windows.get(0);
660 }
661 }
662 // We now know the index into the apps. If we found
663 // an app window above, that gives us the position; else
664 // we need to look some more.
665 if (pos != null) {
666 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700667 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 mTokenMap.get(((WindowState)pos).mClient.asBinder());
669 if (atoken != null) {
670 final int NC = atoken.windows.size();
671 if (NC > 0) {
672 WindowState bottom = atoken.windows.get(0);
673 if (bottom.mSubLayer < 0) {
674 pos = bottom;
675 }
676 }
677 }
678 placeWindowBefore(pos, win);
679 } else {
680 while (i >= 0) {
681 AppWindowToken t = mAppTokens.get(i);
682 final int NW = t.windows.size();
683 if (NW > 0) {
684 pos = t.windows.get(NW-1);
685 break;
686 }
687 i--;
688 }
689 if (pos != null) {
690 // Move in front of any windows attached to this
691 // one.
692 WindowToken atoken =
693 mTokenMap.get(((WindowState)pos).mClient.asBinder());
694 if (atoken != null) {
695 final int NC = atoken.windows.size();
696 if (NC > 0) {
697 WindowState top = atoken.windows.get(NC-1);
698 if (top.mSubLayer >= 0) {
699 pos = top;
700 }
701 }
702 }
703 placeWindowAfter(pos, win);
704 } else {
705 // Just search for the start of this layer.
706 final int myLayer = win.mBaseLayer;
707 for (i=0; i<N; i++) {
708 WindowState w = (WindowState)localmWindows.get(i);
709 if (w.mBaseLayer > myLayer) {
710 break;
711 }
712 }
713 if (localLOGV || DEBUG_FOCUS) Log.v(
714 TAG, "Adding window " + win + " at "
715 + i + " of " + N);
716 localmWindows.add(i, win);
717 }
718 }
719 }
720 } else {
721 // Figure out where window should go, based on layer.
722 final int myLayer = win.mBaseLayer;
723 for (i=N-1; i>=0; i--) {
724 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
725 i++;
726 break;
727 }
728 }
729 if (i < 0) i = 0;
730 if (localLOGV || DEBUG_FOCUS) Log.v(
731 TAG, "Adding window " + win + " at "
732 + i + " of " + N);
733 localmWindows.add(i, win);
734 }
735 if (addToToken) {
736 token.windows.add(tokenWindowsPos, win);
737 }
738
739 } else {
740 // Figure out this window's ordering relative to the window
741 // it is attached to.
742 final int NA = token.windows.size();
743 final int sublayer = win.mSubLayer;
744 int largestSublayer = Integer.MIN_VALUE;
745 WindowState windowWithLargestSublayer = null;
746 for (i=0; i<NA; i++) {
747 WindowState w = token.windows.get(i);
748 final int wSublayer = w.mSubLayer;
749 if (wSublayer >= largestSublayer) {
750 largestSublayer = wSublayer;
751 windowWithLargestSublayer = w;
752 }
753 if (sublayer < 0) {
754 // For negative sublayers, we go below all windows
755 // in the same sublayer.
756 if (wSublayer >= sublayer) {
757 if (addToToken) {
758 token.windows.add(i, win);
759 }
760 placeWindowBefore(
761 wSublayer >= 0 ? attached : w, win);
762 break;
763 }
764 } else {
765 // For positive sublayers, we go above all windows
766 // in the same sublayer.
767 if (wSublayer > sublayer) {
768 if (addToToken) {
769 token.windows.add(i, win);
770 }
771 placeWindowBefore(w, win);
772 break;
773 }
774 }
775 }
776 if (i >= NA) {
777 if (addToToken) {
778 token.windows.add(win);
779 }
780 if (sublayer < 0) {
781 placeWindowBefore(attached, win);
782 } else {
783 placeWindowAfter(largestSublayer >= 0
784 ? windowWithLargestSublayer
785 : attached,
786 win);
787 }
788 }
789 }
Romain Guy06882f82009-06-10 13:36:04 -0700790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 if (win.mAppToken != null && addToToken) {
792 win.mAppToken.allAppWindows.add(win);
793 }
794 }
Romain Guy06882f82009-06-10 13:36:04 -0700795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 static boolean canBeImeTarget(WindowState w) {
797 final int fl = w.mAttrs.flags
798 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
799 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
800 return w.isVisibleOrAdding();
801 }
802 return false;
803 }
Romain Guy06882f82009-06-10 13:36:04 -0700804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
806 final ArrayList localmWindows = mWindows;
807 final int N = localmWindows.size();
808 WindowState w = null;
809 int i = N;
810 while (i > 0) {
811 i--;
812 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
815 // + Integer.toHexString(w.mAttrs.flags));
816 if (canBeImeTarget(w)) {
817 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 // Yet more tricksyness! If this window is a "starting"
820 // window, we do actually want to be on top of it, but
821 // it is not -really- where input will go. So if the caller
822 // is not actually looking to move the IME, look down below
823 // for a real window to target...
824 if (!willMove
825 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
826 && i > 0) {
827 WindowState wb = (WindowState)localmWindows.get(i-1);
828 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
829 i--;
830 w = wb;
831 }
832 }
833 break;
834 }
835 }
Romain Guy06882f82009-06-10 13:36:04 -0700836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
840 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 if (willMove && w != null) {
843 final WindowState curTarget = mInputMethodTarget;
844 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 // Now some fun for dealing with window animations that
847 // modify the Z order. We need to look at all windows below
848 // the current target that are in this app, finding the highest
849 // visible one in layering.
850 AppWindowToken token = curTarget.mAppToken;
851 WindowState highestTarget = null;
852 int highestPos = 0;
853 if (token.animating || token.animation != null) {
854 int pos = 0;
855 pos = localmWindows.indexOf(curTarget);
856 while (pos >= 0) {
857 WindowState win = (WindowState)localmWindows.get(pos);
858 if (win.mAppToken != token) {
859 break;
860 }
861 if (!win.mRemoved) {
862 if (highestTarget == null || win.mAnimLayer >
863 highestTarget.mAnimLayer) {
864 highestTarget = win;
865 highestPos = pos;
866 }
867 }
868 pos--;
869 }
870 }
Romain Guy06882f82009-06-10 13:36:04 -0700871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700873 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 + mNextAppTransition + " " + highestTarget
875 + " animating=" + highestTarget.isAnimating()
876 + " layer=" + highestTarget.mAnimLayer
877 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
880 // If we are currently setting up for an animation,
881 // hold everything until we can find out what will happen.
882 mInputMethodTargetWaitingAnim = true;
883 mInputMethodTarget = highestTarget;
884 return highestPos + 1;
885 } else if (highestTarget.isAnimating() &&
886 highestTarget.mAnimLayer > w.mAnimLayer) {
887 // If the window we are currently targeting is involved
888 // with an animation, and it is on top of the next target
889 // we will be over, then hold off on moving until
890 // that is done.
891 mInputMethodTarget = highestTarget;
892 return highestPos + 1;
893 }
894 }
895 }
896 }
Romain Guy06882f82009-06-10 13:36:04 -0700897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 //Log.i(TAG, "Placing input method @" + (i+1));
899 if (w != null) {
900 if (willMove) {
901 RuntimeException e = new RuntimeException();
902 e.fillInStackTrace();
903 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
904 + mInputMethodTarget + " to " + w, e);
905 mInputMethodTarget = w;
906 if (w.mAppToken != null) {
907 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
908 } else {
909 setInputMethodAnimLayerAdjustment(0);
910 }
911 }
912 return i+1;
913 }
914 if (willMove) {
915 RuntimeException e = new RuntimeException();
916 e.fillInStackTrace();
917 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
918 + mInputMethodTarget + " to null", e);
919 mInputMethodTarget = null;
920 setInputMethodAnimLayerAdjustment(0);
921 }
922 return -1;
923 }
Romain Guy06882f82009-06-10 13:36:04 -0700924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 void addInputMethodWindowToListLocked(WindowState win) {
926 int pos = findDesiredInputMethodWindowIndexLocked(true);
927 if (pos >= 0) {
928 win.mTargetAppToken = mInputMethodTarget.mAppToken;
929 mWindows.add(pos, win);
930 moveInputMethodDialogsLocked(pos+1);
931 return;
932 }
933 win.mTargetAppToken = null;
934 addWindowToListInOrderLocked(win, true);
935 moveInputMethodDialogsLocked(pos);
936 }
Romain Guy06882f82009-06-10 13:36:04 -0700937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 void setInputMethodAnimLayerAdjustment(int adj) {
939 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
940 mInputMethodAnimLayerAdjustment = adj;
941 WindowState imw = mInputMethodWindow;
942 if (imw != null) {
943 imw.mAnimLayer = imw.mLayer + adj;
944 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
945 + " anim layer: " + imw.mAnimLayer);
946 int wi = imw.mChildWindows.size();
947 while (wi > 0) {
948 wi--;
949 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
950 cw.mAnimLayer = cw.mLayer + adj;
951 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
952 + " anim layer: " + cw.mAnimLayer);
953 }
954 }
955 int di = mInputMethodDialogs.size();
956 while (di > 0) {
957 di --;
958 imw = mInputMethodDialogs.get(di);
959 imw.mAnimLayer = imw.mLayer + adj;
960 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
961 + " anim layer: " + imw.mAnimLayer);
962 }
963 }
Romain Guy06882f82009-06-10 13:36:04 -0700964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
966 int wpos = mWindows.indexOf(win);
967 if (wpos >= 0) {
968 if (wpos < interestingPos) interestingPos--;
969 mWindows.remove(wpos);
970 int NC = win.mChildWindows.size();
971 while (NC > 0) {
972 NC--;
973 WindowState cw = (WindowState)win.mChildWindows.get(NC);
974 int cpos = mWindows.indexOf(cw);
975 if (cpos >= 0) {
976 if (cpos < interestingPos) interestingPos--;
977 mWindows.remove(cpos);
978 }
979 }
980 }
981 return interestingPos;
982 }
Romain Guy06882f82009-06-10 13:36:04 -0700983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 private void reAddWindowToListInOrderLocked(WindowState win) {
985 addWindowToListInOrderLocked(win, false);
986 // This is a hack to get all of the child windows added as well
987 // at the right position. Child windows should be rare and
988 // this case should be rare, so it shouldn't be that big a deal.
989 int wpos = mWindows.indexOf(win);
990 if (wpos >= 0) {
991 mWindows.remove(wpos);
992 reAddWindowLocked(wpos, win);
993 }
994 }
Romain Guy06882f82009-06-10 13:36:04 -0700995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 void logWindowList(String prefix) {
997 int N = mWindows.size();
998 while (N > 0) {
999 N--;
1000 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1001 }
1002 }
Romain Guy06882f82009-06-10 13:36:04 -07001003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 void moveInputMethodDialogsLocked(int pos) {
1005 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 final int N = dialogs.size();
1008 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1009 for (int i=0; i<N; i++) {
1010 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1011 }
1012 if (DEBUG_INPUT_METHOD) {
1013 Log.v(TAG, "Window list w/pos=" + pos);
1014 logWindowList(" ");
1015 }
Romain Guy06882f82009-06-10 13:36:04 -07001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 if (pos >= 0) {
1018 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1019 if (pos < mWindows.size()) {
1020 WindowState wp = (WindowState)mWindows.get(pos);
1021 if (wp == mInputMethodWindow) {
1022 pos++;
1023 }
1024 }
1025 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1026 for (int i=0; i<N; i++) {
1027 WindowState win = dialogs.get(i);
1028 win.mTargetAppToken = targetAppToken;
1029 pos = reAddWindowLocked(pos, win);
1030 }
1031 if (DEBUG_INPUT_METHOD) {
1032 Log.v(TAG, "Final window list:");
1033 logWindowList(" ");
1034 }
1035 return;
1036 }
1037 for (int i=0; i<N; i++) {
1038 WindowState win = dialogs.get(i);
1039 win.mTargetAppToken = null;
1040 reAddWindowToListInOrderLocked(win);
1041 if (DEBUG_INPUT_METHOD) {
1042 Log.v(TAG, "No IM target, final list:");
1043 logWindowList(" ");
1044 }
1045 }
1046 }
Romain Guy06882f82009-06-10 13:36:04 -07001047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1049 final WindowState imWin = mInputMethodWindow;
1050 final int DN = mInputMethodDialogs.size();
1051 if (imWin == null && DN == 0) {
1052 return false;
1053 }
Romain Guy06882f82009-06-10 13:36:04 -07001054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1056 if (imPos >= 0) {
1057 // In this case, the input method windows are to be placed
1058 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 // First check to see if the input method windows are already
1061 // located here, and contiguous.
1062 final int N = mWindows.size();
1063 WindowState firstImWin = imPos < N
1064 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 // Figure out the actual input method window that should be
1067 // at the bottom of their stack.
1068 WindowState baseImWin = imWin != null
1069 ? imWin : mInputMethodDialogs.get(0);
1070 if (baseImWin.mChildWindows.size() > 0) {
1071 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1072 if (cw.mSubLayer < 0) baseImWin = cw;
1073 }
Romain Guy06882f82009-06-10 13:36:04 -07001074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 if (firstImWin == baseImWin) {
1076 // The windows haven't moved... but are they still contiguous?
1077 // First find the top IM window.
1078 int pos = imPos+1;
1079 while (pos < N) {
1080 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1081 break;
1082 }
1083 pos++;
1084 }
1085 pos++;
1086 // Now there should be no more input method windows above.
1087 while (pos < N) {
1088 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1089 break;
1090 }
1091 pos++;
1092 }
1093 if (pos >= N) {
1094 // All is good!
1095 return false;
1096 }
1097 }
Romain Guy06882f82009-06-10 13:36:04 -07001098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 if (imWin != null) {
1100 if (DEBUG_INPUT_METHOD) {
1101 Log.v(TAG, "Moving IM from " + imPos);
1102 logWindowList(" ");
1103 }
1104 imPos = tmpRemoveWindowLocked(imPos, imWin);
1105 if (DEBUG_INPUT_METHOD) {
1106 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1107 logWindowList(" ");
1108 }
1109 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1110 reAddWindowLocked(imPos, imWin);
1111 if (DEBUG_INPUT_METHOD) {
1112 Log.v(TAG, "List after moving IM to " + imPos + ":");
1113 logWindowList(" ");
1114 }
1115 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1116 } else {
1117 moveInputMethodDialogsLocked(imPos);
1118 }
Romain Guy06882f82009-06-10 13:36:04 -07001119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 } else {
1121 // In this case, the input method windows go in a fixed layer,
1122 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 if (imWin != null) {
1125 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1126 tmpRemoveWindowLocked(0, imWin);
1127 imWin.mTargetAppToken = null;
1128 reAddWindowToListInOrderLocked(imWin);
1129 if (DEBUG_INPUT_METHOD) {
1130 Log.v(TAG, "List with no IM target:");
1131 logWindowList(" ");
1132 }
1133 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1134 } else {
1135 moveInputMethodDialogsLocked(-1);;
1136 }
Romain Guy06882f82009-06-10 13:36:04 -07001137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
Romain Guy06882f82009-06-10 13:36:04 -07001139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 if (needAssignLayers) {
1141 assignLayersLocked();
1142 }
Romain Guy06882f82009-06-10 13:36:04 -07001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 return true;
1145 }
Romain Guy06882f82009-06-10 13:36:04 -07001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 void adjustInputMethodDialogsLocked() {
1148 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1149 }
Romain Guy06882f82009-06-10 13:36:04 -07001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 public int addWindow(Session session, IWindow client,
1152 WindowManager.LayoutParams attrs, int viewVisibility,
1153 Rect outContentInsets) {
1154 int res = mPolicy.checkAddPermission(attrs);
1155 if (res != WindowManagerImpl.ADD_OKAY) {
1156 return res;
1157 }
Romain Guy06882f82009-06-10 13:36:04 -07001158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 boolean reportNewConfig = false;
1160 WindowState attachedWindow = null;
1161 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 synchronized(mWindowMap) {
1164 // Instantiating a Display requires talking with the simulator,
1165 // so don't do it until we know the system is mostly up and
1166 // running.
1167 if (mDisplay == null) {
1168 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1169 mDisplay = wm.getDefaultDisplay();
1170 mQueue.setDisplay(mDisplay);
1171 reportNewConfig = true;
1172 }
Romain Guy06882f82009-06-10 13:36:04 -07001173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 if (mWindowMap.containsKey(client.asBinder())) {
1175 Log.w(TAG, "Window " + client + " is already added");
1176 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1177 }
1178
1179 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001180 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 if (attachedWindow == null) {
1182 Log.w(TAG, "Attempted to add window with token that is not a window: "
1183 + attrs.token + ". Aborting.");
1184 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1185 }
1186 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1187 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1188 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1189 + attrs.token + ". Aborting.");
1190 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1191 }
1192 }
1193
1194 boolean addToken = false;
1195 WindowToken token = mTokenMap.get(attrs.token);
1196 if (token == null) {
1197 if (attrs.type >= FIRST_APPLICATION_WINDOW
1198 && attrs.type <= LAST_APPLICATION_WINDOW) {
1199 Log.w(TAG, "Attempted to add application window with unknown token "
1200 + attrs.token + ". Aborting.");
1201 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1202 }
1203 if (attrs.type == TYPE_INPUT_METHOD) {
1204 Log.w(TAG, "Attempted to add input method window with unknown token "
1205 + attrs.token + ". Aborting.");
1206 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1207 }
1208 token = new WindowToken(attrs.token, -1, false);
1209 addToken = true;
1210 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1211 && attrs.type <= LAST_APPLICATION_WINDOW) {
1212 AppWindowToken atoken = token.appWindowToken;
1213 if (atoken == null) {
1214 Log.w(TAG, "Attempted to add window with non-application token "
1215 + token + ". Aborting.");
1216 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1217 } else if (atoken.removed) {
1218 Log.w(TAG, "Attempted to add window with exiting application token "
1219 + token + ". Aborting.");
1220 return WindowManagerImpl.ADD_APP_EXITING;
1221 }
1222 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1223 // No need for this guy!
1224 if (localLOGV) Log.v(
1225 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1226 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1227 }
1228 } else if (attrs.type == TYPE_INPUT_METHOD) {
1229 if (token.windowType != TYPE_INPUT_METHOD) {
1230 Log.w(TAG, "Attempted to add input method window with bad token "
1231 + attrs.token + ". Aborting.");
1232 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1233 }
1234 }
1235
1236 win = new WindowState(session, client, token,
1237 attachedWindow, attrs, viewVisibility);
1238 if (win.mDeathRecipient == null) {
1239 // Client has apparently died, so there is no reason to
1240 // continue.
1241 Log.w(TAG, "Adding window client " + client.asBinder()
1242 + " that is dead, aborting.");
1243 return WindowManagerImpl.ADD_APP_EXITING;
1244 }
1245
1246 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 res = mPolicy.prepareAddWindowLw(win, attrs);
1249 if (res != WindowManagerImpl.ADD_OKAY) {
1250 return res;
1251 }
1252
1253 // From now on, no exceptions or errors allowed!
1254
1255 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 if (addToken) {
1260 mTokenMap.put(attrs.token, token);
1261 mTokenList.add(token);
1262 }
1263 win.attach();
1264 mWindowMap.put(client.asBinder(), win);
1265
1266 if (attrs.type == TYPE_APPLICATION_STARTING &&
1267 token.appWindowToken != null) {
1268 token.appWindowToken.startingWindow = win;
1269 }
1270
1271 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 if (attrs.type == TYPE_INPUT_METHOD) {
1274 mInputMethodWindow = win;
1275 addInputMethodWindowToListLocked(win);
1276 imMayMove = false;
1277 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1278 mInputMethodDialogs.add(win);
1279 addWindowToListInOrderLocked(win, true);
1280 adjustInputMethodDialogsLocked();
1281 imMayMove = false;
1282 } else {
1283 addWindowToListInOrderLocked(win, true);
1284 }
Romain Guy06882f82009-06-10 13:36:04 -07001285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 if (mInTouchMode) {
1291 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1292 }
1293 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1294 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1295 }
Romain Guy06882f82009-06-10 13:36:04 -07001296
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001297 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001299 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1300 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 imMayMove = false;
1302 }
1303 }
Romain Guy06882f82009-06-10 13:36:04 -07001304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001306 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 }
Romain Guy06882f82009-06-10 13:36:04 -07001308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 assignLayersLocked();
1310 // Don't do layout here, the window must call
1311 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 //dump();
1314
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001315 if (focusChanged) {
1316 if (mCurrentFocus != null) {
1317 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1318 }
1319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 if (localLOGV) Log.v(
1321 TAG, "New client " + client.asBinder()
1322 + ": window=" + win);
1323 }
1324
1325 // sendNewConfiguration() checks caller permissions so we must call it with
1326 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1327 // identity anyway, so it's safe to just clear & restore around this whole
1328 // block.
1329 final long origId = Binder.clearCallingIdentity();
1330 if (reportNewConfig) {
1331 sendNewConfiguration();
1332 } else {
1333 // Update Orientation after adding a window, only if the window needs to be
1334 // displayed right away
1335 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001336 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 sendNewConfiguration();
1338 }
1339 }
1340 }
1341 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 return res;
1344 }
Romain Guy06882f82009-06-10 13:36:04 -07001345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 public void removeWindow(Session session, IWindow client) {
1347 synchronized(mWindowMap) {
1348 WindowState win = windowForClientLocked(session, client);
1349 if (win == null) {
1350 return;
1351 }
1352 removeWindowLocked(session, win);
1353 }
1354 }
Romain Guy06882f82009-06-10 13:36:04 -07001355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 public void removeWindowLocked(Session session, WindowState win) {
1357
1358 if (localLOGV || DEBUG_FOCUS) Log.v(
1359 TAG, "Remove " + win + " client="
1360 + Integer.toHexString(System.identityHashCode(
1361 win.mClient.asBinder()))
1362 + ", surface=" + win.mSurface);
1363
1364 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 if (DEBUG_APP_TRANSITIONS) Log.v(
1367 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1368 + " mExiting=" + win.mExiting
1369 + " isAnimating=" + win.isAnimating()
1370 + " app-animation="
1371 + (win.mAppToken != null ? win.mAppToken.animation : null)
1372 + " inPendingTransaction="
1373 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1374 + " mDisplayFrozen=" + mDisplayFrozen);
1375 // Visibility of the removed window. Will be used later to update orientation later on.
1376 boolean wasVisible = false;
1377 // First, see if we need to run an animation. If we do, we have
1378 // to hold off on removing the window until the animation is done.
1379 // If the display is frozen, just remove immediately, since the
1380 // animation wouldn't be seen.
1381 if (win.mSurface != null && !mDisplayFrozen) {
1382 // If we are not currently running the exit animation, we
1383 // need to see about starting one.
1384 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1387 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1388 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1389 }
1390 // Try starting an animation.
1391 if (applyAnimationLocked(win, transit, false)) {
1392 win.mExiting = true;
1393 }
1394 }
1395 if (win.mExiting || win.isAnimating()) {
1396 // The exit animation is running... wait for it!
1397 //Log.i(TAG, "*** Running exit animation...");
1398 win.mExiting = true;
1399 win.mRemoveOnExit = true;
1400 mLayoutNeeded = true;
1401 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1402 performLayoutAndPlaceSurfacesLocked();
1403 if (win.mAppToken != null) {
1404 win.mAppToken.updateReportedVisibilityLocked();
1405 }
1406 //dump();
1407 Binder.restoreCallingIdentity(origId);
1408 return;
1409 }
1410 }
1411
1412 removeWindowInnerLocked(session, win);
1413 // Removing a visible window will effect the computed orientation
1414 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001415 if (wasVisible && computeForcedAppOrientationLocked()
1416 != mForcedAppOrientation) {
1417 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
1419 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1420 Binder.restoreCallingIdentity(origId);
1421 }
Romain Guy06882f82009-06-10 13:36:04 -07001422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 private void removeWindowInnerLocked(Session session, WindowState win) {
1424 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1425 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 if (mInputMethodTarget == win) {
1430 moveInputMethodWindowsIfNeededLocked(false);
1431 }
Romain Guy06882f82009-06-10 13:36:04 -07001432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 mPolicy.removeWindowLw(win);
1434 win.removeLocked();
1435
1436 mWindowMap.remove(win.mClient.asBinder());
1437 mWindows.remove(win);
1438
1439 if (mInputMethodWindow == win) {
1440 mInputMethodWindow = null;
1441 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1442 mInputMethodDialogs.remove(win);
1443 }
Romain Guy06882f82009-06-10 13:36:04 -07001444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 final WindowToken token = win.mToken;
1446 final AppWindowToken atoken = win.mAppToken;
1447 token.windows.remove(win);
1448 if (atoken != null) {
1449 atoken.allAppWindows.remove(win);
1450 }
1451 if (localLOGV) Log.v(
1452 TAG, "**** Removing window " + win + ": count="
1453 + token.windows.size());
1454 if (token.windows.size() == 0) {
1455 if (!token.explicit) {
1456 mTokenMap.remove(token.token);
1457 mTokenList.remove(token);
1458 } else if (atoken != null) {
1459 atoken.firstWindowDrawn = false;
1460 }
1461 }
1462
1463 if (atoken != null) {
1464 if (atoken.startingWindow == win) {
1465 atoken.startingWindow = null;
1466 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1467 // If this is the last window and we had requested a starting
1468 // transition window, well there is no point now.
1469 atoken.startingData = null;
1470 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1471 // If this is the last window except for a starting transition
1472 // window, we need to get rid of the starting transition.
1473 if (DEBUG_STARTING_WINDOW) {
1474 Log.v(TAG, "Schedule remove starting " + token
1475 + ": no more real windows");
1476 }
1477 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1478 mH.sendMessage(m);
1479 }
1480 }
Romain Guy06882f82009-06-10 13:36:04 -07001481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 if (!mInLayout) {
1483 assignLayersLocked();
1484 mLayoutNeeded = true;
1485 performLayoutAndPlaceSurfacesLocked();
1486 if (win.mAppToken != null) {
1487 win.mAppToken.updateReportedVisibilityLocked();
1488 }
1489 }
1490 }
1491
1492 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1493 long origId = Binder.clearCallingIdentity();
1494 try {
1495 synchronized (mWindowMap) {
1496 WindowState w = windowForClientLocked(session, client);
1497 if ((w != null) && (w.mSurface != null)) {
1498 Surface.openTransaction();
1499 try {
1500 w.mSurface.setTransparentRegionHint(region);
1501 } finally {
1502 Surface.closeTransaction();
1503 }
1504 }
1505 }
1506 } finally {
1507 Binder.restoreCallingIdentity(origId);
1508 }
1509 }
1510
1511 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001512 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 Rect visibleInsets) {
1514 long origId = Binder.clearCallingIdentity();
1515 try {
1516 synchronized (mWindowMap) {
1517 WindowState w = windowForClientLocked(session, client);
1518 if (w != null) {
1519 w.mGivenInsetsPending = false;
1520 w.mGivenContentInsets.set(contentInsets);
1521 w.mGivenVisibleInsets.set(visibleInsets);
1522 w.mTouchableInsets = touchableInsets;
1523 mLayoutNeeded = true;
1524 performLayoutAndPlaceSurfacesLocked();
1525 }
1526 }
1527 } finally {
1528 Binder.restoreCallingIdentity(origId);
1529 }
1530 }
Romain Guy06882f82009-06-10 13:36:04 -07001531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 public void getWindowDisplayFrame(Session session, IWindow client,
1533 Rect outDisplayFrame) {
1534 synchronized(mWindowMap) {
1535 WindowState win = windowForClientLocked(session, client);
1536 if (win == null) {
1537 outDisplayFrame.setEmpty();
1538 return;
1539 }
1540 outDisplayFrame.set(win.mDisplayFrame);
1541 }
1542 }
1543
1544 public int relayoutWindow(Session session, IWindow client,
1545 WindowManager.LayoutParams attrs, int requestedWidth,
1546 int requestedHeight, int viewVisibility, boolean insetsPending,
1547 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1548 Surface outSurface) {
1549 boolean displayed = false;
1550 boolean inTouchMode;
1551 Configuration newConfig = null;
1552 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 synchronized(mWindowMap) {
1555 WindowState win = windowForClientLocked(session, client);
1556 if (win == null) {
1557 return 0;
1558 }
1559 win.mRequestedWidth = requestedWidth;
1560 win.mRequestedHeight = requestedHeight;
1561
1562 if (attrs != null) {
1563 mPolicy.adjustWindowParamsLw(attrs);
1564 }
Romain Guy06882f82009-06-10 13:36:04 -07001565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 int attrChanges = 0;
1567 int flagChanges = 0;
1568 if (attrs != null) {
1569 flagChanges = win.mAttrs.flags ^= attrs.flags;
1570 attrChanges = win.mAttrs.copyFrom(attrs);
1571 }
1572
1573 if (localLOGV) Log.v(
1574 TAG, "Relayout given client " + client.asBinder()
1575 + " (" + win.mAttrs.getTitle() + ")");
1576
1577
1578 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1579 win.mAlpha = attrs.alpha;
1580 }
1581
1582 final boolean scaledWindow =
1583 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
1584
1585 if (scaledWindow) {
1586 // requested{Width|Height} Surface's physical size
1587 // attrs.{width|height} Size on screen
1588 win.mHScale = (attrs.width != requestedWidth) ?
1589 (attrs.width / (float)requestedWidth) : 1.0f;
1590 win.mVScale = (attrs.height != requestedHeight) ?
1591 (attrs.height / (float)requestedHeight) : 1.0f;
1592 }
1593
1594 boolean imMayMove = (flagChanges&(
1595 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
1596 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07001597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 boolean focusMayChange = win.mViewVisibility != viewVisibility
1599 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
1600 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07001601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 win.mRelayoutCalled = true;
1603 final int oldVisibility = win.mViewVisibility;
1604 win.mViewVisibility = viewVisibility;
1605 if (viewVisibility == View.VISIBLE &&
1606 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
1607 displayed = !win.isVisibleLw();
1608 if (win.mExiting) {
1609 win.mExiting = false;
1610 win.mAnimation = null;
1611 }
1612 if (win.mDestroying) {
1613 win.mDestroying = false;
1614 mDestroySurface.remove(win);
1615 }
1616 if (oldVisibility == View.GONE) {
1617 win.mEnterAnimationPending = true;
1618 }
1619 if (displayed && win.mSurface != null && !win.mDrawPending
1620 && !win.mCommitDrawPending && !mDisplayFrozen) {
1621 applyEnterAnimationLocked(win);
1622 }
1623 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
1624 // To change the format, we need to re-build the surface.
1625 win.destroySurfaceLocked();
1626 displayed = true;
1627 }
1628 try {
1629 Surface surface = win.createSurfaceLocked();
1630 if (surface != null) {
1631 outSurface.copyFrom(surface);
1632 } else {
1633 outSurface.clear();
1634 }
1635 } catch (Exception e) {
1636 Log.w(TAG, "Exception thrown when creating surface for client "
1637 + client + " (" + win.mAttrs.getTitle() + ")",
1638 e);
1639 Binder.restoreCallingIdentity(origId);
1640 return 0;
1641 }
1642 if (displayed) {
1643 focusMayChange = true;
1644 }
1645 if (win.mAttrs.type == TYPE_INPUT_METHOD
1646 && mInputMethodWindow == null) {
1647 mInputMethodWindow = win;
1648 imMayMove = true;
1649 }
1650 } else {
1651 win.mEnterAnimationPending = false;
1652 if (win.mSurface != null) {
1653 // If we are not currently running the exit animation, we
1654 // need to see about starting one.
1655 if (!win.mExiting) {
1656 // Try starting an animation; if there isn't one, we
1657 // can destroy the surface right away.
1658 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1659 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1660 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1661 }
1662 if (win.isWinVisibleLw() &&
1663 applyAnimationLocked(win, transit, false)) {
1664 win.mExiting = true;
1665 mKeyWaiter.finishedKey(session, client, true,
1666 KeyWaiter.RETURN_NOTHING);
1667 } else if (win.isAnimating()) {
1668 // Currently in a hide animation... turn this into
1669 // an exit.
1670 win.mExiting = true;
1671 } else {
1672 if (mInputMethodWindow == win) {
1673 mInputMethodWindow = null;
1674 }
1675 win.destroySurfaceLocked();
1676 }
1677 }
1678 }
1679 outSurface.clear();
1680 }
1681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 if (focusMayChange) {
1683 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
1684 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 imMayMove = false;
1686 }
1687 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
1688 }
Romain Guy06882f82009-06-10 13:36:04 -07001689
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001690 // updateFocusedWindowLocked() already assigned layers so we only need to
1691 // reassign them at this point if the IM window state gets shuffled
1692 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07001693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 if (imMayMove) {
1695 if (moveInputMethodWindowsIfNeededLocked(false)) {
1696 assignLayers = true;
1697 }
1698 }
Romain Guy06882f82009-06-10 13:36:04 -07001699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 mLayoutNeeded = true;
1701 win.mGivenInsetsPending = insetsPending;
1702 if (assignLayers) {
1703 assignLayersLocked();
1704 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001705 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 performLayoutAndPlaceSurfacesLocked();
1707 if (win.mAppToken != null) {
1708 win.mAppToken.updateReportedVisibilityLocked();
1709 }
1710 outFrame.set(win.mFrame);
1711 outContentInsets.set(win.mContentInsets);
1712 outVisibleInsets.set(win.mVisibleInsets);
1713 if (localLOGV) Log.v(
1714 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07001715 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 + ", requestedHeight=" + requestedHeight
1717 + ", viewVisibility=" + viewVisibility
1718 + "\nRelayout returning frame=" + outFrame
1719 + ", surface=" + outSurface);
1720
1721 if (localLOGV || DEBUG_FOCUS) Log.v(
1722 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
1723
1724 inTouchMode = mInTouchMode;
1725 }
1726
1727 if (newConfig != null) {
1728 sendNewConfiguration();
1729 }
Romain Guy06882f82009-06-10 13:36:04 -07001730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
1734 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
1735 }
1736
1737 public void finishDrawingWindow(Session session, IWindow client) {
1738 final long origId = Binder.clearCallingIdentity();
1739 synchronized(mWindowMap) {
1740 WindowState win = windowForClientLocked(session, client);
1741 if (win != null && win.finishDrawingLocked()) {
1742 mLayoutNeeded = true;
1743 performLayoutAndPlaceSurfacesLocked();
1744 }
1745 }
1746 Binder.restoreCallingIdentity(origId);
1747 }
1748
1749 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
1750 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
1751 + (lp != null ? lp.packageName : null)
1752 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
1753 if (lp != null && lp.windowAnimations != 0) {
1754 // If this is a system resource, don't try to load it from the
1755 // application resources. It is nice to avoid loading application
1756 // resources if we can.
1757 String packageName = lp.packageName != null ? lp.packageName : "android";
1758 int resId = lp.windowAnimations;
1759 if ((resId&0xFF000000) == 0x01000000) {
1760 packageName = "android";
1761 }
1762 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
1763 + packageName);
1764 return AttributeCache.instance().get(packageName, resId,
1765 com.android.internal.R.styleable.WindowAnimation);
1766 }
1767 return null;
1768 }
Romain Guy06882f82009-06-10 13:36:04 -07001769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 private void applyEnterAnimationLocked(WindowState win) {
1771 int transit = WindowManagerPolicy.TRANSIT_SHOW;
1772 if (win.mEnterAnimationPending) {
1773 win.mEnterAnimationPending = false;
1774 transit = WindowManagerPolicy.TRANSIT_ENTER;
1775 }
1776
1777 applyAnimationLocked(win, transit, true);
1778 }
1779
1780 private boolean applyAnimationLocked(WindowState win,
1781 int transit, boolean isEntrance) {
1782 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
1783 // If we are trying to apply an animation, but already running
1784 // an animation of the same type, then just leave that one alone.
1785 return true;
1786 }
Romain Guy06882f82009-06-10 13:36:04 -07001787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 // Only apply an animation if the display isn't frozen. If it is
1789 // frozen, there is no reason to animate and it can cause strange
1790 // artifacts when we unfreeze the display if some different animation
1791 // is running.
1792 if (!mDisplayFrozen) {
1793 int anim = mPolicy.selectAnimationLw(win, transit);
1794 int attr = -1;
1795 Animation a = null;
1796 if (anim != 0) {
1797 a = AnimationUtils.loadAnimation(mContext, anim);
1798 } else {
1799 switch (transit) {
1800 case WindowManagerPolicy.TRANSIT_ENTER:
1801 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1802 break;
1803 case WindowManagerPolicy.TRANSIT_EXIT:
1804 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1805 break;
1806 case WindowManagerPolicy.TRANSIT_SHOW:
1807 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1808 break;
1809 case WindowManagerPolicy.TRANSIT_HIDE:
1810 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1811 break;
1812 }
1813 if (attr >= 0) {
1814 a = loadAnimation(win.mAttrs, attr);
1815 }
1816 }
1817 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
1818 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1819 + " mAnimation=" + win.mAnimation
1820 + " isEntrance=" + isEntrance);
1821 if (a != null) {
1822 if (DEBUG_ANIM) {
1823 RuntimeException e = new RuntimeException();
1824 e.fillInStackTrace();
1825 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
1826 }
1827 win.setAnimation(a);
1828 win.mAnimationIsEntrance = isEntrance;
1829 }
1830 } else {
1831 win.clearAnimation();
1832 }
1833
1834 return win.mAnimation != null;
1835 }
1836
1837 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
1838 int anim = 0;
1839 Context context = mContext;
1840 if (animAttr >= 0) {
1841 AttributeCache.Entry ent = getCachedAnimations(lp);
1842 if (ent != null) {
1843 context = ent.context;
1844 anim = ent.array.getResourceId(animAttr, 0);
1845 }
1846 }
1847 if (anim != 0) {
1848 return AnimationUtils.loadAnimation(context, anim);
1849 }
1850 return null;
1851 }
Romain Guy06882f82009-06-10 13:36:04 -07001852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 private boolean applyAnimationLocked(AppWindowToken wtoken,
1854 WindowManager.LayoutParams lp, int transit, boolean enter) {
1855 // Only apply an animation if the display isn't frozen. If it is
1856 // frozen, there is no reason to animate and it can cause strange
1857 // artifacts when we unfreeze the display if some different animation
1858 // is running.
1859 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001860 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07001861 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001862 a = new FadeInOutAnimation(enter);
1863 if (DEBUG_ANIM) Log.v(TAG,
1864 "applying FadeInOutAnimation for a window in compatibility mode");
1865 } else {
1866 int animAttr = 0;
1867 switch (transit) {
1868 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
1869 animAttr = enter
1870 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
1871 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
1872 break;
1873 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
1874 animAttr = enter
1875 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
1876 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
1877 break;
1878 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
1879 animAttr = enter
1880 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
1881 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
1882 break;
1883 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
1884 animAttr = enter
1885 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
1886 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
1887 break;
1888 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
1889 animAttr = enter
1890 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
1891 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
1892 break;
1893 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
1894 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07001895 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07001896 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
1897 break;
1898 }
1899 a = loadAnimation(lp, animAttr);
1900 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
1901 + " anim=" + a
1902 + " animAttr=0x" + Integer.toHexString(animAttr)
1903 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 if (a != null) {
1906 if (DEBUG_ANIM) {
1907 RuntimeException e = new RuntimeException();
1908 e.fillInStackTrace();
1909 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
1910 }
1911 wtoken.setAnimation(a);
1912 }
1913 } else {
1914 wtoken.clearAnimation();
1915 }
1916
1917 return wtoken.animation != null;
1918 }
1919
1920 // -------------------------------------------------------------
1921 // Application Window Tokens
1922 // -------------------------------------------------------------
1923
1924 public void validateAppTokens(List tokens) {
1925 int v = tokens.size()-1;
1926 int m = mAppTokens.size()-1;
1927 while (v >= 0 && m >= 0) {
1928 AppWindowToken wtoken = mAppTokens.get(m);
1929 if (wtoken.removed) {
1930 m--;
1931 continue;
1932 }
1933 if (tokens.get(v) != wtoken.token) {
1934 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
1935 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
1936 }
1937 v--;
1938 m--;
1939 }
1940 while (v >= 0) {
1941 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
1942 v--;
1943 }
1944 while (m >= 0) {
1945 AppWindowToken wtoken = mAppTokens.get(m);
1946 if (!wtoken.removed) {
1947 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
1948 }
1949 m--;
1950 }
1951 }
1952
1953 boolean checkCallingPermission(String permission, String func) {
1954 // Quick check: if the calling permission is me, it's all okay.
1955 if (Binder.getCallingPid() == Process.myPid()) {
1956 return true;
1957 }
Romain Guy06882f82009-06-10 13:36:04 -07001958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 if (mContext.checkCallingPermission(permission)
1960 == PackageManager.PERMISSION_GRANTED) {
1961 return true;
1962 }
1963 String msg = "Permission Denial: " + func + " from pid="
1964 + Binder.getCallingPid()
1965 + ", uid=" + Binder.getCallingUid()
1966 + " requires " + permission;
1967 Log.w(TAG, msg);
1968 return false;
1969 }
Romain Guy06882f82009-06-10 13:36:04 -07001970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 AppWindowToken findAppWindowToken(IBinder token) {
1972 WindowToken wtoken = mTokenMap.get(token);
1973 if (wtoken == null) {
1974 return null;
1975 }
1976 return wtoken.appWindowToken;
1977 }
Romain Guy06882f82009-06-10 13:36:04 -07001978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 public void addWindowToken(IBinder token, int type) {
1980 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1981 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001982 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
Romain Guy06882f82009-06-10 13:36:04 -07001984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 synchronized(mWindowMap) {
1986 WindowToken wtoken = mTokenMap.get(token);
1987 if (wtoken != null) {
1988 Log.w(TAG, "Attempted to add existing input method token: " + token);
1989 return;
1990 }
1991 wtoken = new WindowToken(token, type, true);
1992 mTokenMap.put(token, wtoken);
1993 mTokenList.add(wtoken);
1994 }
1995 }
Romain Guy06882f82009-06-10 13:36:04 -07001996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 public void removeWindowToken(IBinder token) {
1998 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
1999 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002000 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
2002
2003 final long origId = Binder.clearCallingIdentity();
2004 synchronized(mWindowMap) {
2005 WindowToken wtoken = mTokenMap.remove(token);
2006 mTokenList.remove(wtoken);
2007 if (wtoken != null) {
2008 boolean delayed = false;
2009 if (!wtoken.hidden) {
2010 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 final int N = wtoken.windows.size();
2013 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 for (int i=0; i<N; i++) {
2016 WindowState win = wtoken.windows.get(i);
2017
2018 if (win.isAnimating()) {
2019 delayed = true;
2020 }
Romain Guy06882f82009-06-10 13:36:04 -07002021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 if (win.isVisibleNow()) {
2023 applyAnimationLocked(win,
2024 WindowManagerPolicy.TRANSIT_EXIT, false);
2025 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2026 KeyWaiter.RETURN_NOTHING);
2027 changed = true;
2028 }
2029 }
2030
2031 if (changed) {
2032 mLayoutNeeded = true;
2033 performLayoutAndPlaceSurfacesLocked();
2034 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2035 }
Romain Guy06882f82009-06-10 13:36:04 -07002036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 if (delayed) {
2038 mExitingTokens.add(wtoken);
2039 }
2040 }
Romain Guy06882f82009-06-10 13:36:04 -07002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 } else {
2043 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2044 }
2045 }
2046 Binder.restoreCallingIdentity(origId);
2047 }
2048
2049 public void addAppToken(int addPos, IApplicationToken token,
2050 int groupId, int requestedOrientation, boolean fullscreen) {
2051 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2052 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002053 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 }
Romain Guy06882f82009-06-10 13:36:04 -07002055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 synchronized(mWindowMap) {
2057 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2058 if (wtoken != null) {
2059 Log.w(TAG, "Attempted to add existing app token: " + token);
2060 return;
2061 }
2062 wtoken = new AppWindowToken(token);
2063 wtoken.groupId = groupId;
2064 wtoken.appFullscreen = fullscreen;
2065 wtoken.requestedOrientation = requestedOrientation;
2066 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002067 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 mTokenMap.put(token.asBinder(), wtoken);
2069 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 // Application tokens start out hidden.
2072 wtoken.hidden = true;
2073 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 //dump();
2076 }
2077 }
Romain Guy06882f82009-06-10 13:36:04 -07002078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 public void setAppGroupId(IBinder token, int groupId) {
2080 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2081 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002082 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
2084
2085 synchronized(mWindowMap) {
2086 AppWindowToken wtoken = findAppWindowToken(token);
2087 if (wtoken == null) {
2088 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2089 return;
2090 }
2091 wtoken.groupId = groupId;
2092 }
2093 }
Romain Guy06882f82009-06-10 13:36:04 -07002094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 public int getOrientationFromWindowsLocked() {
2096 int pos = mWindows.size() - 1;
2097 while (pos >= 0) {
2098 WindowState wtoken = (WindowState) mWindows.get(pos);
2099 pos--;
2100 if (wtoken.mAppToken != null) {
2101 // We hit an application window. so the orientation will be determined by the
2102 // app window. No point in continuing further.
2103 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2104 }
2105 if (!wtoken.isVisibleLw()) {
2106 continue;
2107 }
2108 int req = wtoken.mAttrs.screenOrientation;
2109 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2110 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2111 continue;
2112 } else {
2113 return req;
2114 }
2115 }
2116 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2117 }
Romain Guy06882f82009-06-10 13:36:04 -07002118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 public int getOrientationFromAppTokensLocked() {
2120 int pos = mAppTokens.size() - 1;
2121 int curGroup = 0;
2122 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002123 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002125 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 while (pos >= 0) {
2127 AppWindowToken wtoken = mAppTokens.get(pos);
2128 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002129 // if we're about to tear down this window and not seek for
2130 // the behind activity, don't use it for orientation
2131 if (!findingBehind
2132 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002133 continue;
2134 }
2135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 if (!haveGroup) {
2137 // We ignore any hidden applications on the top.
2138 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2139 continue;
2140 }
2141 haveGroup = true;
2142 curGroup = wtoken.groupId;
2143 lastOrientation = wtoken.requestedOrientation;
2144 } else if (curGroup != wtoken.groupId) {
2145 // If we have hit a new application group, and the bottom
2146 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002147 // the orientation behind it, and the last app was
2148 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002150 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2151 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 return lastOrientation;
2153 }
2154 }
2155 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002156 // If this application is fullscreen, and didn't explicitly say
2157 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002159 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002160 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002161 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 return or;
2163 }
2164 // If this application has requested an explicit orientation,
2165 // then use it.
2166 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2167 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2168 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2169 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2170 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2171 return or;
2172 }
Owen Lin3413b892009-05-01 17:12:32 -07002173 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 }
2175 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2176 }
Romain Guy06882f82009-06-10 13:36:04 -07002177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002179 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002180 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2181 "updateOrientationFromAppTokens()")) {
2182 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2183 }
2184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 Configuration config;
2186 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002187 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2188 freezeThisOneIfNeeded);
2189 Binder.restoreCallingIdentity(ident);
2190 return config;
2191 }
2192
2193 Configuration updateOrientationFromAppTokensUnchecked(
2194 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2195 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002197 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 }
2199 if (config != null) {
2200 mLayoutNeeded = true;
2201 performLayoutAndPlaceSurfacesLocked();
2202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 return config;
2204 }
Romain Guy06882f82009-06-10 13:36:04 -07002205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 /*
2207 * The orientation is computed from non-application windows first. If none of
2208 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002209 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2211 * android.os.IBinder)
2212 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002213 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002214 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 long ident = Binder.clearCallingIdentity();
2217 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002218 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 if (req != mForcedAppOrientation) {
2221 changed = true;
2222 mForcedAppOrientation = req;
2223 //send a message to Policy indicating orientation change to take
2224 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002225 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 }
Romain Guy06882f82009-06-10 13:36:04 -07002227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 if (changed) {
2229 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002230 WindowManagerPolicy.USE_LAST_ROTATION,
2231 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 if (changed) {
2233 if (freezeThisOneIfNeeded != null) {
2234 AppWindowToken wtoken = findAppWindowToken(
2235 freezeThisOneIfNeeded);
2236 if (wtoken != null) {
2237 startAppFreezingScreenLocked(wtoken,
2238 ActivityInfo.CONFIG_ORIENTATION);
2239 }
2240 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002241 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 }
2243 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002244
2245 // No obvious action we need to take, but if our current
2246 // state mismatches the activity maanager's, update it
2247 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002248 mTempConfiguration.setToDefaults();
2249 if (computeNewConfigurationLocked(mTempConfiguration)) {
2250 if (appConfig.diff(mTempConfiguration) != 0) {
2251 Log.i(TAG, "Config changed: " + mTempConfiguration);
2252 return new Configuration(mTempConfiguration);
2253 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002254 }
2255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 } finally {
2257 Binder.restoreCallingIdentity(ident);
2258 }
Romain Guy06882f82009-06-10 13:36:04 -07002259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 return null;
2261 }
Romain Guy06882f82009-06-10 13:36:04 -07002262
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002263 int computeForcedAppOrientationLocked() {
2264 int req = getOrientationFromWindowsLocked();
2265 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2266 req = getOrientationFromAppTokensLocked();
2267 }
2268 return req;
2269 }
Romain Guy06882f82009-06-10 13:36:04 -07002270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2272 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2273 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002274 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 }
Romain Guy06882f82009-06-10 13:36:04 -07002276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 synchronized(mWindowMap) {
2278 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2279 if (wtoken == null) {
2280 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2281 return;
2282 }
Romain Guy06882f82009-06-10 13:36:04 -07002283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 wtoken.requestedOrientation = requestedOrientation;
2285 }
2286 }
Romain Guy06882f82009-06-10 13:36:04 -07002287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 public int getAppOrientation(IApplicationToken token) {
2289 synchronized(mWindowMap) {
2290 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2291 if (wtoken == null) {
2292 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2293 }
Romain Guy06882f82009-06-10 13:36:04 -07002294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 return wtoken.requestedOrientation;
2296 }
2297 }
Romain Guy06882f82009-06-10 13:36:04 -07002298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2300 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2301 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002302 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
2304
2305 synchronized(mWindowMap) {
2306 boolean changed = false;
2307 if (token == null) {
2308 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2309 changed = mFocusedApp != null;
2310 mFocusedApp = null;
2311 mKeyWaiter.tickle();
2312 } else {
2313 AppWindowToken newFocus = findAppWindowToken(token);
2314 if (newFocus == null) {
2315 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2316 return;
2317 }
2318 changed = mFocusedApp != newFocus;
2319 mFocusedApp = newFocus;
2320 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2321 mKeyWaiter.tickle();
2322 }
2323
2324 if (moveFocusNow && changed) {
2325 final long origId = Binder.clearCallingIdentity();
2326 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2327 Binder.restoreCallingIdentity(origId);
2328 }
2329 }
2330 }
2331
2332 public void prepareAppTransition(int transit) {
2333 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2334 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002335 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
Romain Guy06882f82009-06-10 13:36:04 -07002337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 synchronized(mWindowMap) {
2339 if (DEBUG_APP_TRANSITIONS) Log.v(
2340 TAG, "Prepare app transition: transit=" + transit
2341 + " mNextAppTransition=" + mNextAppTransition);
2342 if (!mDisplayFrozen) {
2343 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2344 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002345 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2346 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2347 // Opening a new task always supersedes a close for the anim.
2348 mNextAppTransition = transit;
2349 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2350 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2351 // Opening a new activity always supersedes a close for the anim.
2352 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 }
2354 mAppTransitionReady = false;
2355 mAppTransitionTimeout = false;
2356 mStartingIconInTransition = false;
2357 mSkipAppTransitionAnimation = false;
2358 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2359 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2360 5000);
2361 }
2362 }
2363 }
2364
2365 public int getPendingAppTransition() {
2366 return mNextAppTransition;
2367 }
Romain Guy06882f82009-06-10 13:36:04 -07002368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 public void executeAppTransition() {
2370 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2371 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002372 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 }
Romain Guy06882f82009-06-10 13:36:04 -07002374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 synchronized(mWindowMap) {
2376 if (DEBUG_APP_TRANSITIONS) Log.v(
2377 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2378 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2379 mAppTransitionReady = true;
2380 final long origId = Binder.clearCallingIdentity();
2381 performLayoutAndPlaceSurfacesLocked();
2382 Binder.restoreCallingIdentity(origId);
2383 }
2384 }
2385 }
2386
2387 public void setAppStartingWindow(IBinder token, String pkg,
2388 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2389 IBinder transferFrom, boolean createIfNeeded) {
2390 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2391 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002392 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 }
2394
2395 synchronized(mWindowMap) {
2396 if (DEBUG_STARTING_WINDOW) Log.v(
2397 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2398 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 AppWindowToken wtoken = findAppWindowToken(token);
2401 if (wtoken == null) {
2402 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2403 return;
2404 }
2405
2406 // If the display is frozen, we won't do anything until the
2407 // actual window is displayed so there is no reason to put in
2408 // the starting window.
2409 if (mDisplayFrozen) {
2410 return;
2411 }
Romain Guy06882f82009-06-10 13:36:04 -07002412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 if (wtoken.startingData != null) {
2414 return;
2415 }
Romain Guy06882f82009-06-10 13:36:04 -07002416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 if (transferFrom != null) {
2418 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2419 if (ttoken != null) {
2420 WindowState startingWindow = ttoken.startingWindow;
2421 if (startingWindow != null) {
2422 if (mStartingIconInTransition) {
2423 // In this case, the starting icon has already
2424 // been displayed, so start letting windows get
2425 // shown immediately without any more transitions.
2426 mSkipAppTransitionAnimation = true;
2427 }
2428 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2429 "Moving existing starting from " + ttoken
2430 + " to " + wtoken);
2431 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 // Transfer the starting window over to the new
2434 // token.
2435 wtoken.startingData = ttoken.startingData;
2436 wtoken.startingView = ttoken.startingView;
2437 wtoken.startingWindow = startingWindow;
2438 ttoken.startingData = null;
2439 ttoken.startingView = null;
2440 ttoken.startingWindow = null;
2441 ttoken.startingMoved = true;
2442 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002443 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 startingWindow.mAppToken = wtoken;
2445 mWindows.remove(startingWindow);
2446 ttoken.windows.remove(startingWindow);
2447 ttoken.allAppWindows.remove(startingWindow);
2448 addWindowToListInOrderLocked(startingWindow, true);
2449 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 // Propagate other interesting state between the
2452 // tokens. If the old token is displayed, we should
2453 // immediately force the new one to be displayed. If
2454 // it is animating, we need to move that animation to
2455 // the new one.
2456 if (ttoken.allDrawn) {
2457 wtoken.allDrawn = true;
2458 }
2459 if (ttoken.firstWindowDrawn) {
2460 wtoken.firstWindowDrawn = true;
2461 }
2462 if (!ttoken.hidden) {
2463 wtoken.hidden = false;
2464 wtoken.hiddenRequested = false;
2465 wtoken.willBeHidden = false;
2466 }
2467 if (wtoken.clientHidden != ttoken.clientHidden) {
2468 wtoken.clientHidden = ttoken.clientHidden;
2469 wtoken.sendAppVisibilityToClients();
2470 }
2471 if (ttoken.animation != null) {
2472 wtoken.animation = ttoken.animation;
2473 wtoken.animating = ttoken.animating;
2474 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2475 ttoken.animation = null;
2476 ttoken.animLayerAdjustment = 0;
2477 wtoken.updateLayers();
2478 ttoken.updateLayers();
2479 }
Romain Guy06882f82009-06-10 13:36:04 -07002480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 mLayoutNeeded = true;
2483 performLayoutAndPlaceSurfacesLocked();
2484 Binder.restoreCallingIdentity(origId);
2485 return;
2486 } else if (ttoken.startingData != null) {
2487 // The previous app was getting ready to show a
2488 // starting window, but hasn't yet done so. Steal it!
2489 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2490 "Moving pending starting from " + ttoken
2491 + " to " + wtoken);
2492 wtoken.startingData = ttoken.startingData;
2493 ttoken.startingData = null;
2494 ttoken.startingMoved = true;
2495 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2496 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2497 // want to process the message ASAP, before any other queued
2498 // messages.
2499 mH.sendMessageAtFrontOfQueue(m);
2500 return;
2501 }
2502 }
2503 }
2504
2505 // There is no existing starting window, and the caller doesn't
2506 // want us to create one, so that's it!
2507 if (!createIfNeeded) {
2508 return;
2509 }
Romain Guy06882f82009-06-10 13:36:04 -07002510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 mStartingIconInTransition = true;
2512 wtoken.startingData = new StartingData(
2513 pkg, theme, nonLocalizedLabel,
2514 labelRes, icon);
2515 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2516 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2517 // want to process the message ASAP, before any other queued
2518 // messages.
2519 mH.sendMessageAtFrontOfQueue(m);
2520 }
2521 }
2522
2523 public void setAppWillBeHidden(IBinder token) {
2524 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2525 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002526 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 }
2528
2529 AppWindowToken wtoken;
2530
2531 synchronized(mWindowMap) {
2532 wtoken = findAppWindowToken(token);
2533 if (wtoken == null) {
2534 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2535 return;
2536 }
2537 wtoken.willBeHidden = true;
2538 }
2539 }
Romain Guy06882f82009-06-10 13:36:04 -07002540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2542 boolean visible, int transit, boolean performLayout) {
2543 boolean delayed = false;
2544
2545 if (wtoken.clientHidden == visible) {
2546 wtoken.clientHidden = !visible;
2547 wtoken.sendAppVisibilityToClients();
2548 }
Romain Guy06882f82009-06-10 13:36:04 -07002549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 wtoken.willBeHidden = false;
2551 if (wtoken.hidden == visible) {
2552 final int N = wtoken.allAppWindows.size();
2553 boolean changed = false;
2554 if (DEBUG_APP_TRANSITIONS) Log.v(
2555 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
2556 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07002557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07002559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
2561 if (wtoken.animation == sDummyAnimation) {
2562 wtoken.animation = null;
2563 }
2564 applyAnimationLocked(wtoken, lp, transit, visible);
2565 changed = true;
2566 if (wtoken.animation != null) {
2567 delayed = runningAppAnimation = true;
2568 }
2569 }
Romain Guy06882f82009-06-10 13:36:04 -07002570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 for (int i=0; i<N; i++) {
2572 WindowState win = wtoken.allAppWindows.get(i);
2573 if (win == wtoken.startingWindow) {
2574 continue;
2575 }
2576
2577 if (win.isAnimating()) {
2578 delayed = true;
2579 }
Romain Guy06882f82009-06-10 13:36:04 -07002580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
2582 //win.dump(" ");
2583 if (visible) {
2584 if (!win.isVisibleNow()) {
2585 if (!runningAppAnimation) {
2586 applyAnimationLocked(win,
2587 WindowManagerPolicy.TRANSIT_ENTER, true);
2588 }
2589 changed = true;
2590 }
2591 } else if (win.isVisibleNow()) {
2592 if (!runningAppAnimation) {
2593 applyAnimationLocked(win,
2594 WindowManagerPolicy.TRANSIT_EXIT, false);
2595 }
2596 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2597 KeyWaiter.RETURN_NOTHING);
2598 changed = true;
2599 }
2600 }
2601
2602 wtoken.hidden = wtoken.hiddenRequested = !visible;
2603 if (!visible) {
2604 unsetAppFreezingScreenLocked(wtoken, true, true);
2605 } else {
2606 // If we are being set visible, and the starting window is
2607 // not yet displayed, then make sure it doesn't get displayed.
2608 WindowState swin = wtoken.startingWindow;
2609 if (swin != null && (swin.mDrawPending
2610 || swin.mCommitDrawPending)) {
2611 swin.mPolicyVisibility = false;
2612 swin.mPolicyVisibilityAfterAnim = false;
2613 }
2614 }
Romain Guy06882f82009-06-10 13:36:04 -07002615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
2617 + ": hidden=" + wtoken.hidden + " hiddenRequested="
2618 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07002619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 if (changed && performLayout) {
2621 mLayoutNeeded = true;
2622 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 performLayoutAndPlaceSurfacesLocked();
2624 }
2625 }
2626
2627 if (wtoken.animation != null) {
2628 delayed = true;
2629 }
Romain Guy06882f82009-06-10 13:36:04 -07002630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 return delayed;
2632 }
2633
2634 public void setAppVisibility(IBinder token, boolean visible) {
2635 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2636 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002637 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 }
2639
2640 AppWindowToken wtoken;
2641
2642 synchronized(mWindowMap) {
2643 wtoken = findAppWindowToken(token);
2644 if (wtoken == null) {
2645 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
2646 return;
2647 }
2648
2649 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
2650 RuntimeException e = new RuntimeException();
2651 e.fillInStackTrace();
2652 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
2653 + "): mNextAppTransition=" + mNextAppTransition
2654 + " hidden=" + wtoken.hidden
2655 + " hiddenRequested=" + wtoken.hiddenRequested, e);
2656 }
Romain Guy06882f82009-06-10 13:36:04 -07002657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 // If we are preparing an app transition, then delay changing
2659 // the visibility of this token until we execute that transition.
2660 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2661 // Already in requested state, don't do anything more.
2662 if (wtoken.hiddenRequested != visible) {
2663 return;
2664 }
2665 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07002666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 if (DEBUG_APP_TRANSITIONS) Log.v(
2668 TAG, "Setting dummy animation on: " + wtoken);
2669 wtoken.setDummyAnimation();
2670 mOpeningApps.remove(wtoken);
2671 mClosingApps.remove(wtoken);
2672 wtoken.inPendingTransaction = true;
2673 if (visible) {
2674 mOpeningApps.add(wtoken);
2675 wtoken.allDrawn = false;
2676 wtoken.startingDisplayed = false;
2677 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07002678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 if (wtoken.clientHidden) {
2680 // In the case where we are making an app visible
2681 // but holding off for a transition, we still need
2682 // to tell the client to make its windows visible so
2683 // they get drawn. Otherwise, we will wait on
2684 // performing the transition until all windows have
2685 // been drawn, they never will be, and we are sad.
2686 wtoken.clientHidden = false;
2687 wtoken.sendAppVisibilityToClients();
2688 }
2689 } else {
2690 mClosingApps.add(wtoken);
2691 }
2692 return;
2693 }
Romain Guy06882f82009-06-10 13:36:04 -07002694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 final long origId = Binder.clearCallingIdentity();
2696 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
2697 wtoken.updateReportedVisibilityLocked();
2698 Binder.restoreCallingIdentity(origId);
2699 }
2700 }
2701
2702 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
2703 boolean unfreezeSurfaceNow, boolean force) {
2704 if (wtoken.freezingScreen) {
2705 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
2706 + " force=" + force);
2707 final int N = wtoken.allAppWindows.size();
2708 boolean unfrozeWindows = false;
2709 for (int i=0; i<N; i++) {
2710 WindowState w = wtoken.allAppWindows.get(i);
2711 if (w.mAppFreezing) {
2712 w.mAppFreezing = false;
2713 if (w.mSurface != null && !w.mOrientationChanging) {
2714 w.mOrientationChanging = true;
2715 }
2716 unfrozeWindows = true;
2717 }
2718 }
2719 if (force || unfrozeWindows) {
2720 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
2721 wtoken.freezingScreen = false;
2722 mAppsFreezingScreen--;
2723 }
2724 if (unfreezeSurfaceNow) {
2725 if (unfrozeWindows) {
2726 mLayoutNeeded = true;
2727 performLayoutAndPlaceSurfacesLocked();
2728 }
2729 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
2730 stopFreezingDisplayLocked();
2731 }
2732 }
2733 }
2734 }
Romain Guy06882f82009-06-10 13:36:04 -07002735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
2737 int configChanges) {
2738 if (DEBUG_ORIENTATION) {
2739 RuntimeException e = new RuntimeException();
2740 e.fillInStackTrace();
2741 Log.i(TAG, "Set freezing of " + wtoken.appToken
2742 + ": hidden=" + wtoken.hidden + " freezing="
2743 + wtoken.freezingScreen, e);
2744 }
2745 if (!wtoken.hiddenRequested) {
2746 if (!wtoken.freezingScreen) {
2747 wtoken.freezingScreen = true;
2748 mAppsFreezingScreen++;
2749 if (mAppsFreezingScreen == 1) {
2750 startFreezingDisplayLocked();
2751 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
2752 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
2753 5000);
2754 }
2755 }
2756 final int N = wtoken.allAppWindows.size();
2757 for (int i=0; i<N; i++) {
2758 WindowState w = wtoken.allAppWindows.get(i);
2759 w.mAppFreezing = true;
2760 }
2761 }
2762 }
Romain Guy06882f82009-06-10 13:36:04 -07002763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 public void startAppFreezingScreen(IBinder token, int configChanges) {
2765 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2766 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002767 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 }
2769
2770 synchronized(mWindowMap) {
2771 if (configChanges == 0 && !mDisplayFrozen) {
2772 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
2773 return;
2774 }
Romain Guy06882f82009-06-10 13:36:04 -07002775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 AppWindowToken wtoken = findAppWindowToken(token);
2777 if (wtoken == null || wtoken.appToken == null) {
2778 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
2779 return;
2780 }
2781 final long origId = Binder.clearCallingIdentity();
2782 startAppFreezingScreenLocked(wtoken, configChanges);
2783 Binder.restoreCallingIdentity(origId);
2784 }
2785 }
Romain Guy06882f82009-06-10 13:36:04 -07002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 public void stopAppFreezingScreen(IBinder token, boolean force) {
2788 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2789 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002790 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 }
2792
2793 synchronized(mWindowMap) {
2794 AppWindowToken wtoken = findAppWindowToken(token);
2795 if (wtoken == null || wtoken.appToken == null) {
2796 return;
2797 }
2798 final long origId = Binder.clearCallingIdentity();
2799 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
2800 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
2801 unsetAppFreezingScreenLocked(wtoken, true, force);
2802 Binder.restoreCallingIdentity(origId);
2803 }
2804 }
Romain Guy06882f82009-06-10 13:36:04 -07002805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 public void removeAppToken(IBinder token) {
2807 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2808 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002809 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 }
2811
2812 AppWindowToken wtoken = null;
2813 AppWindowToken startingToken = null;
2814 boolean delayed = false;
2815
2816 final long origId = Binder.clearCallingIdentity();
2817 synchronized(mWindowMap) {
2818 WindowToken basewtoken = mTokenMap.remove(token);
2819 mTokenList.remove(basewtoken);
2820 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
2821 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
2822 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
2823 wtoken.inPendingTransaction = false;
2824 mOpeningApps.remove(wtoken);
2825 if (mClosingApps.contains(wtoken)) {
2826 delayed = true;
2827 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2828 mClosingApps.add(wtoken);
2829 delayed = true;
2830 }
2831 if (DEBUG_APP_TRANSITIONS) Log.v(
2832 TAG, "Removing app " + wtoken + " delayed=" + delayed
2833 + " animation=" + wtoken.animation
2834 + " animating=" + wtoken.animating);
2835 if (delayed) {
2836 // set the token aside because it has an active animation to be finished
2837 mExitingAppTokens.add(wtoken);
2838 }
2839 mAppTokens.remove(wtoken);
2840 wtoken.removed = true;
2841 if (wtoken.startingData != null) {
2842 startingToken = wtoken;
2843 }
2844 unsetAppFreezingScreenLocked(wtoken, true, true);
2845 if (mFocusedApp == wtoken) {
2846 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
2847 mFocusedApp = null;
2848 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2849 mKeyWaiter.tickle();
2850 }
2851 } else {
2852 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
2853 }
Romain Guy06882f82009-06-10 13:36:04 -07002854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 if (!delayed && wtoken != null) {
2856 wtoken.updateReportedVisibilityLocked();
2857 }
2858 }
2859 Binder.restoreCallingIdentity(origId);
2860
2861 if (startingToken != null) {
2862 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
2863 + startingToken + ": app token removed");
2864 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
2865 mH.sendMessage(m);
2866 }
2867 }
2868
2869 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
2870 final int NW = token.windows.size();
2871 for (int i=0; i<NW; i++) {
2872 WindowState win = token.windows.get(i);
2873 mWindows.remove(win);
2874 int j = win.mChildWindows.size();
2875 while (j > 0) {
2876 j--;
2877 mWindows.remove(win.mChildWindows.get(j));
2878 }
2879 }
2880 return NW > 0;
2881 }
2882
2883 void dumpAppTokensLocked() {
2884 for (int i=mAppTokens.size()-1; i>=0; i--) {
2885 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
2886 }
2887 }
Romain Guy06882f82009-06-10 13:36:04 -07002888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 void dumpWindowsLocked() {
2890 for (int i=mWindows.size()-1; i>=0; i--) {
2891 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
2892 }
2893 }
Romain Guy06882f82009-06-10 13:36:04 -07002894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 private int findWindowOffsetLocked(int tokenPos) {
2896 final int NW = mWindows.size();
2897
2898 if (tokenPos >= mAppTokens.size()) {
2899 int i = NW;
2900 while (i > 0) {
2901 i--;
2902 WindowState win = (WindowState)mWindows.get(i);
2903 if (win.getAppToken() != null) {
2904 return i+1;
2905 }
2906 }
2907 }
2908
2909 while (tokenPos > 0) {
2910 // Find the first app token below the new position that has
2911 // a window displayed.
2912 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
2913 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
2914 + tokenPos + " -- " + wtoken.token);
2915 int i = wtoken.windows.size();
2916 while (i > 0) {
2917 i--;
2918 WindowState win = wtoken.windows.get(i);
2919 int j = win.mChildWindows.size();
2920 while (j > 0) {
2921 j--;
2922 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2923 if (cwin.mSubLayer >= 0 ) {
2924 for (int pos=NW-1; pos>=0; pos--) {
2925 if (mWindows.get(pos) == cwin) {
2926 if (DEBUG_REORDER) Log.v(TAG,
2927 "Found child win @" + (pos+1));
2928 return pos+1;
2929 }
2930 }
2931 }
2932 }
2933 for (int pos=NW-1; pos>=0; pos--) {
2934 if (mWindows.get(pos) == win) {
2935 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
2936 return pos+1;
2937 }
2938 }
2939 }
2940 tokenPos--;
2941 }
2942
2943 return 0;
2944 }
2945
2946 private final int reAddWindowLocked(int index, WindowState win) {
2947 final int NCW = win.mChildWindows.size();
2948 boolean added = false;
2949 for (int j=0; j<NCW; j++) {
2950 WindowState cwin = (WindowState)win.mChildWindows.get(j);
2951 if (!added && cwin.mSubLayer >= 0) {
2952 mWindows.add(index, win);
2953 index++;
2954 added = true;
2955 }
2956 mWindows.add(index, cwin);
2957 index++;
2958 }
2959 if (!added) {
2960 mWindows.add(index, win);
2961 index++;
2962 }
2963 return index;
2964 }
Romain Guy06882f82009-06-10 13:36:04 -07002965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 private final int reAddAppWindowsLocked(int index, WindowToken token) {
2967 final int NW = token.windows.size();
2968 for (int i=0; i<NW; i++) {
2969 index = reAddWindowLocked(index, token.windows.get(i));
2970 }
2971 return index;
2972 }
2973
2974 public void moveAppToken(int index, IBinder token) {
2975 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2976 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002977 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 }
2979
2980 synchronized(mWindowMap) {
2981 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
2982 if (DEBUG_REORDER) dumpAppTokensLocked();
2983 final AppWindowToken wtoken = findAppWindowToken(token);
2984 if (wtoken == null || !mAppTokens.remove(wtoken)) {
2985 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
2986 + token + " (" + wtoken + ")");
2987 return;
2988 }
2989 mAppTokens.add(index, wtoken);
2990 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
2991 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 final long origId = Binder.clearCallingIdentity();
2994 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
2995 if (DEBUG_REORDER) dumpWindowsLocked();
2996 if (tmpRemoveAppWindowsLocked(wtoken)) {
2997 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
2998 if (DEBUG_REORDER) dumpWindowsLocked();
2999 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3000 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3001 if (DEBUG_REORDER) dumpWindowsLocked();
3002 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 mLayoutNeeded = true;
3004 performLayoutAndPlaceSurfacesLocked();
3005 }
3006 Binder.restoreCallingIdentity(origId);
3007 }
3008 }
3009
3010 private void removeAppTokensLocked(List<IBinder> tokens) {
3011 // XXX This should be done more efficiently!
3012 // (take advantage of the fact that both lists should be
3013 // ordered in the same way.)
3014 int N = tokens.size();
3015 for (int i=0; i<N; i++) {
3016 IBinder token = tokens.get(i);
3017 final AppWindowToken wtoken = findAppWindowToken(token);
3018 if (!mAppTokens.remove(wtoken)) {
3019 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3020 + token + " (" + wtoken + ")");
3021 i--;
3022 N--;
3023 }
3024 }
3025 }
3026
3027 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3028 // First remove all of the windows from the list.
3029 final int N = tokens.size();
3030 int i;
3031 for (i=0; i<N; i++) {
3032 WindowToken token = mTokenMap.get(tokens.get(i));
3033 if (token != null) {
3034 tmpRemoveAppWindowsLocked(token);
3035 }
3036 }
3037
3038 // Where to start adding?
3039 int pos = findWindowOffsetLocked(tokenPos);
3040
3041 // And now add them back at the correct place.
3042 for (i=0; i<N; i++) {
3043 WindowToken token = mTokenMap.get(tokens.get(i));
3044 if (token != null) {
3045 pos = reAddAppWindowsLocked(pos, token);
3046 }
3047 }
3048
3049 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 mLayoutNeeded = true;
3051 performLayoutAndPlaceSurfacesLocked();
3052
3053 //dump();
3054 }
3055
3056 public void moveAppTokensToTop(List<IBinder> tokens) {
3057 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3058 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003059 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 }
3061
3062 final long origId = Binder.clearCallingIdentity();
3063 synchronized(mWindowMap) {
3064 removeAppTokensLocked(tokens);
3065 final int N = tokens.size();
3066 for (int i=0; i<N; i++) {
3067 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3068 if (wt != null) {
3069 mAppTokens.add(wt);
3070 }
3071 }
3072 moveAppWindowsLocked(tokens, mAppTokens.size());
3073 }
3074 Binder.restoreCallingIdentity(origId);
3075 }
3076
3077 public void moveAppTokensToBottom(List<IBinder> tokens) {
3078 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3079 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003080 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 }
3082
3083 final long origId = Binder.clearCallingIdentity();
3084 synchronized(mWindowMap) {
3085 removeAppTokensLocked(tokens);
3086 final int N = tokens.size();
3087 int pos = 0;
3088 for (int i=0; i<N; i++) {
3089 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3090 if (wt != null) {
3091 mAppTokens.add(pos, wt);
3092 pos++;
3093 }
3094 }
3095 moveAppWindowsLocked(tokens, 0);
3096 }
3097 Binder.restoreCallingIdentity(origId);
3098 }
3099
3100 // -------------------------------------------------------------
3101 // Misc IWindowSession methods
3102 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 public void disableKeyguard(IBinder token, String tag) {
3105 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3106 != PackageManager.PERMISSION_GRANTED) {
3107 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3108 }
3109 mKeyguardDisabled.acquire(token, tag);
3110 }
3111
3112 public void reenableKeyguard(IBinder token) {
3113 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3114 != PackageManager.PERMISSION_GRANTED) {
3115 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3116 }
3117 synchronized (mKeyguardDisabled) {
3118 mKeyguardDisabled.release(token);
3119
3120 if (!mKeyguardDisabled.isAcquired()) {
3121 // if we are the last one to reenable the keyguard wait until
3122 // we have actaully finished reenabling until returning
3123 mWaitingUntilKeyguardReenabled = true;
3124 while (mWaitingUntilKeyguardReenabled) {
3125 try {
3126 mKeyguardDisabled.wait();
3127 } catch (InterruptedException e) {
3128 Thread.currentThread().interrupt();
3129 }
3130 }
3131 }
3132 }
3133 }
3134
3135 /**
3136 * @see android.app.KeyguardManager#exitKeyguardSecurely
3137 */
3138 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3139 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3140 != PackageManager.PERMISSION_GRANTED) {
3141 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3142 }
3143 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3144 public void onKeyguardExitResult(boolean success) {
3145 try {
3146 callback.onKeyguardExitResult(success);
3147 } catch (RemoteException e) {
3148 // Client has died, we don't care.
3149 }
3150 }
3151 });
3152 }
3153
3154 public boolean inKeyguardRestrictedInputMode() {
3155 return mPolicy.inKeyguardRestrictedKeyInputMode();
3156 }
Romain Guy06882f82009-06-10 13:36:04 -07003157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 static float fixScale(float scale) {
3159 if (scale < 0) scale = 0;
3160 else if (scale > 20) scale = 20;
3161 return Math.abs(scale);
3162 }
Romain Guy06882f82009-06-10 13:36:04 -07003163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 public void setAnimationScale(int which, float scale) {
3165 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3166 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003167 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 }
3169
3170 if (scale < 0) scale = 0;
3171 else if (scale > 20) scale = 20;
3172 scale = Math.abs(scale);
3173 switch (which) {
3174 case 0: mWindowAnimationScale = fixScale(scale); break;
3175 case 1: mTransitionAnimationScale = fixScale(scale); break;
3176 }
Romain Guy06882f82009-06-10 13:36:04 -07003177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 // Persist setting
3179 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3180 }
Romain Guy06882f82009-06-10 13:36:04 -07003181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 public void setAnimationScales(float[] scales) {
3183 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3184 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003185 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 }
3187
3188 if (scales != null) {
3189 if (scales.length >= 1) {
3190 mWindowAnimationScale = fixScale(scales[0]);
3191 }
3192 if (scales.length >= 2) {
3193 mTransitionAnimationScale = fixScale(scales[1]);
3194 }
3195 }
Romain Guy06882f82009-06-10 13:36:04 -07003196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 // Persist setting
3198 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3199 }
Romain Guy06882f82009-06-10 13:36:04 -07003200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 public float getAnimationScale(int which) {
3202 switch (which) {
3203 case 0: return mWindowAnimationScale;
3204 case 1: return mTransitionAnimationScale;
3205 }
3206 return 0;
3207 }
Romain Guy06882f82009-06-10 13:36:04 -07003208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 public float[] getAnimationScales() {
3210 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3211 }
Romain Guy06882f82009-06-10 13:36:04 -07003212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 public int getSwitchState(int sw) {
3214 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3215 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003216 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 }
3218 return KeyInputQueue.getSwitchState(sw);
3219 }
Romain Guy06882f82009-06-10 13:36:04 -07003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 public int getSwitchStateForDevice(int devid, int sw) {
3222 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3223 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003224 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 }
3226 return KeyInputQueue.getSwitchState(devid, sw);
3227 }
Romain Guy06882f82009-06-10 13:36:04 -07003228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 public int getScancodeState(int sw) {
3230 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3231 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003232 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 }
3234 return KeyInputQueue.getScancodeState(sw);
3235 }
Romain Guy06882f82009-06-10 13:36:04 -07003236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 public int getScancodeStateForDevice(int devid, int sw) {
3238 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3239 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003240 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 }
3242 return KeyInputQueue.getScancodeState(devid, sw);
3243 }
Romain Guy06882f82009-06-10 13:36:04 -07003244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 public int getKeycodeState(int sw) {
3246 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3247 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003248 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 }
3250 return KeyInputQueue.getKeycodeState(sw);
3251 }
Romain Guy06882f82009-06-10 13:36:04 -07003252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 public int getKeycodeStateForDevice(int devid, int sw) {
3254 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3255 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003256 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 }
3258 return KeyInputQueue.getKeycodeState(devid, sw);
3259 }
Romain Guy06882f82009-06-10 13:36:04 -07003260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3262 return KeyInputQueue.hasKeys(keycodes, keyExists);
3263 }
Romain Guy06882f82009-06-10 13:36:04 -07003264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 public void enableScreenAfterBoot() {
3266 synchronized(mWindowMap) {
3267 if (mSystemBooted) {
3268 return;
3269 }
3270 mSystemBooted = true;
3271 }
Romain Guy06882f82009-06-10 13:36:04 -07003272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 performEnableScreen();
3274 }
Romain Guy06882f82009-06-10 13:36:04 -07003275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 public void enableScreenIfNeededLocked() {
3277 if (mDisplayEnabled) {
3278 return;
3279 }
3280 if (!mSystemBooted) {
3281 return;
3282 }
3283 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3284 }
Romain Guy06882f82009-06-10 13:36:04 -07003285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 public void performEnableScreen() {
3287 synchronized(mWindowMap) {
3288 if (mDisplayEnabled) {
3289 return;
3290 }
3291 if (!mSystemBooted) {
3292 return;
3293 }
Romain Guy06882f82009-06-10 13:36:04 -07003294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 // Don't enable the screen until all existing windows
3296 // have been drawn.
3297 final int N = mWindows.size();
3298 for (int i=0; i<N; i++) {
3299 WindowState w = (WindowState)mWindows.get(i);
3300 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3301 return;
3302 }
3303 }
Romain Guy06882f82009-06-10 13:36:04 -07003304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 mDisplayEnabled = true;
3306 if (false) {
3307 Log.i(TAG, "ENABLING SCREEN!");
3308 StringWriter sw = new StringWriter();
3309 PrintWriter pw = new PrintWriter(sw);
3310 this.dump(null, pw, null);
3311 Log.i(TAG, sw.toString());
3312 }
3313 try {
3314 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3315 if (surfaceFlinger != null) {
3316 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3317 Parcel data = Parcel.obtain();
3318 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3319 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3320 data, null, 0);
3321 data.recycle();
3322 }
3323 } catch (RemoteException ex) {
3324 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3325 }
3326 }
Romain Guy06882f82009-06-10 13:36:04 -07003327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003331 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3332 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 }
Romain Guy06882f82009-06-10 13:36:04 -07003334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 public void setInTouchMode(boolean mode) {
3336 synchronized(mWindowMap) {
3337 mInTouchMode = mode;
3338 }
3339 }
3340
Romain Guy06882f82009-06-10 13:36:04 -07003341 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003342 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003344 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003345 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 }
3347
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003348 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 }
Romain Guy06882f82009-06-10 13:36:04 -07003350
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003351 public void setRotationUnchecked(int rotation,
3352 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 if(DEBUG_ORIENTATION) Log.v(TAG,
3354 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 long origId = Binder.clearCallingIdentity();
3357 boolean changed;
3358 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003359 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 }
Romain Guy06882f82009-06-10 13:36:04 -07003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 if (changed) {
3363 sendNewConfiguration();
3364 synchronized(mWindowMap) {
3365 mLayoutNeeded = true;
3366 performLayoutAndPlaceSurfacesLocked();
3367 }
3368 } else if (alwaysSendConfiguration) {
3369 //update configuration ignoring orientation change
3370 sendNewConfiguration();
3371 }
Romain Guy06882f82009-06-10 13:36:04 -07003372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 Binder.restoreCallingIdentity(origId);
3374 }
Romain Guy06882f82009-06-10 13:36:04 -07003375
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003376 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 boolean changed;
3378 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3379 rotation = mRequestedRotation;
3380 } else {
3381 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003382 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 }
3384 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003385 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 mRotation, mDisplayEnabled);
3387 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3388 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003391 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 "Rotation changed to " + rotation
3393 + " from " + mRotation
3394 + " (forceApp=" + mForcedAppOrientation
3395 + ", req=" + mRequestedRotation + ")");
3396 mRotation = rotation;
3397 mWindowsFreezingScreen = true;
3398 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3399 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3400 2000);
3401 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003402 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 mQueue.setOrientation(rotation);
3404 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003405 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 }
3407 for (int i=mWindows.size()-1; i>=0; i--) {
3408 WindowState w = (WindowState)mWindows.get(i);
3409 if (w.mSurface != null) {
3410 w.mOrientationChanging = true;
3411 }
3412 }
3413 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3414 try {
3415 mRotationWatchers.get(i).onRotationChanged(rotation);
3416 } catch (RemoteException e) {
3417 }
3418 }
3419 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 return changed;
3422 }
Romain Guy06882f82009-06-10 13:36:04 -07003423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 public int getRotation() {
3425 return mRotation;
3426 }
3427
3428 public int watchRotation(IRotationWatcher watcher) {
3429 final IBinder watcherBinder = watcher.asBinder();
3430 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3431 public void binderDied() {
3432 synchronized (mWindowMap) {
3433 for (int i=0; i<mRotationWatchers.size(); i++) {
3434 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003435 IRotationWatcher removed = mRotationWatchers.remove(i);
3436 if (removed != null) {
3437 removed.asBinder().unlinkToDeath(this, 0);
3438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 i--;
3440 }
3441 }
3442 }
3443 }
3444 };
Romain Guy06882f82009-06-10 13:36:04 -07003445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 synchronized (mWindowMap) {
3447 try {
3448 watcher.asBinder().linkToDeath(dr, 0);
3449 mRotationWatchers.add(watcher);
3450 } catch (RemoteException e) {
3451 // Client died, no cleanup needed.
3452 }
Romain Guy06882f82009-06-10 13:36:04 -07003453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 return mRotation;
3455 }
3456 }
3457
3458 /**
3459 * Starts the view server on the specified port.
3460 *
3461 * @param port The port to listener to.
3462 *
3463 * @return True if the server was successfully started, false otherwise.
3464 *
3465 * @see com.android.server.ViewServer
3466 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3467 */
3468 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003469 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 return false;
3471 }
3472
3473 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3474 return false;
3475 }
3476
3477 if (port < 1024) {
3478 return false;
3479 }
3480
3481 if (mViewServer != null) {
3482 if (!mViewServer.isRunning()) {
3483 try {
3484 return mViewServer.start();
3485 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003486 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
3488 }
3489 return false;
3490 }
3491
3492 try {
3493 mViewServer = new ViewServer(this, port);
3494 return mViewServer.start();
3495 } catch (IOException e) {
3496 Log.w(TAG, "View server did not start");
3497 }
3498 return false;
3499 }
3500
Romain Guy06882f82009-06-10 13:36:04 -07003501 private boolean isSystemSecure() {
3502 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3503 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3504 }
3505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 /**
3507 * Stops the view server if it exists.
3508 *
3509 * @return True if the server stopped, false if it wasn't started or
3510 * couldn't be stopped.
3511 *
3512 * @see com.android.server.ViewServer
3513 */
3514 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003515 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 return false;
3517 }
3518
3519 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3520 return false;
3521 }
3522
3523 if (mViewServer != null) {
3524 return mViewServer.stop();
3525 }
3526 return false;
3527 }
3528
3529 /**
3530 * Indicates whether the view server is running.
3531 *
3532 * @return True if the server is running, false otherwise.
3533 *
3534 * @see com.android.server.ViewServer
3535 */
3536 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003537 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 return false;
3539 }
3540
3541 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3542 return false;
3543 }
3544
3545 return mViewServer != null && mViewServer.isRunning();
3546 }
3547
3548 /**
3549 * Lists all availble windows in the system. The listing is written in the
3550 * specified Socket's output stream with the following syntax:
3551 * windowHashCodeInHexadecimal windowName
3552 * Each line of the ouput represents a different window.
3553 *
3554 * @param client The remote client to send the listing to.
3555 * @return False if an error occured, true otherwise.
3556 */
3557 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07003558 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 return false;
3560 }
3561
3562 boolean result = true;
3563
3564 Object[] windows;
3565 synchronized (mWindowMap) {
3566 windows = new Object[mWindows.size()];
3567 //noinspection unchecked
3568 windows = mWindows.toArray(windows);
3569 }
3570
3571 BufferedWriter out = null;
3572
3573 // Any uncaught exception will crash the system process
3574 try {
3575 OutputStream clientStream = client.getOutputStream();
3576 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
3577
3578 final int count = windows.length;
3579 for (int i = 0; i < count; i++) {
3580 final WindowState w = (WindowState) windows[i];
3581 out.write(Integer.toHexString(System.identityHashCode(w)));
3582 out.write(' ');
3583 out.append(w.mAttrs.getTitle());
3584 out.write('\n');
3585 }
3586
3587 out.write("DONE.\n");
3588 out.flush();
3589 } catch (Exception e) {
3590 result = false;
3591 } finally {
3592 if (out != null) {
3593 try {
3594 out.close();
3595 } catch (IOException e) {
3596 result = false;
3597 }
3598 }
3599 }
3600
3601 return result;
3602 }
3603
3604 /**
3605 * Sends a command to a target window. The result of the command, if any, will be
3606 * written in the output stream of the specified socket.
3607 *
3608 * The parameters must follow this syntax:
3609 * windowHashcode extra
3610 *
3611 * Where XX is the length in characeters of the windowTitle.
3612 *
3613 * The first parameter is the target window. The window with the specified hashcode
3614 * will be the target. If no target can be found, nothing happens. The extra parameters
3615 * will be delivered to the target window and as parameters to the command itself.
3616 *
3617 * @param client The remote client to sent the result, if any, to.
3618 * @param command The command to execute.
3619 * @param parameters The command parameters.
3620 *
3621 * @return True if the command was successfully delivered, false otherwise. This does
3622 * not indicate whether the command itself was successful.
3623 */
3624 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07003625 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 return false;
3627 }
3628
3629 boolean success = true;
3630 Parcel data = null;
3631 Parcel reply = null;
3632
3633 // Any uncaught exception will crash the system process
3634 try {
3635 // Find the hashcode of the window
3636 int index = parameters.indexOf(' ');
3637 if (index == -1) {
3638 index = parameters.length();
3639 }
3640 final String code = parameters.substring(0, index);
3641 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
3642
3643 // Extract the command's parameter after the window description
3644 if (index < parameters.length()) {
3645 parameters = parameters.substring(index + 1);
3646 } else {
3647 parameters = "";
3648 }
3649
3650 final WindowManagerService.WindowState window = findWindow(hashCode);
3651 if (window == null) {
3652 return false;
3653 }
3654
3655 data = Parcel.obtain();
3656 data.writeInterfaceToken("android.view.IWindow");
3657 data.writeString(command);
3658 data.writeString(parameters);
3659 data.writeInt(1);
3660 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
3661
3662 reply = Parcel.obtain();
3663
3664 final IBinder binder = window.mClient.asBinder();
3665 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
3666 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
3667
3668 reply.readException();
3669
3670 } catch (Exception e) {
3671 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
3672 success = false;
3673 } finally {
3674 if (data != null) {
3675 data.recycle();
3676 }
3677 if (reply != null) {
3678 reply.recycle();
3679 }
3680 }
3681
3682 return success;
3683 }
3684
3685 private WindowState findWindow(int hashCode) {
3686 if (hashCode == -1) {
3687 return getFocusedWindow();
3688 }
3689
3690 synchronized (mWindowMap) {
3691 final ArrayList windows = mWindows;
3692 final int count = windows.size();
3693
3694 for (int i = 0; i < count; i++) {
3695 WindowState w = (WindowState) windows.get(i);
3696 if (System.identityHashCode(w) == hashCode) {
3697 return w;
3698 }
3699 }
3700 }
3701
3702 return null;
3703 }
3704
3705 /*
3706 * Instruct the Activity Manager to fetch the current configuration and broadcast
3707 * that to config-changed listeners if appropriate.
3708 */
3709 void sendNewConfiguration() {
3710 try {
3711 mActivityManager.updateConfiguration(null);
3712 } catch (RemoteException e) {
3713 }
3714 }
Romain Guy06882f82009-06-10 13:36:04 -07003715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 public Configuration computeNewConfiguration() {
3717 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07003718 return computeNewConfigurationLocked();
3719 }
3720 }
Romain Guy06882f82009-06-10 13:36:04 -07003721
Dianne Hackbornc485a602009-03-24 22:39:49 -07003722 Configuration computeNewConfigurationLocked() {
3723 Configuration config = new Configuration();
3724 if (!computeNewConfigurationLocked(config)) {
3725 return null;
3726 }
3727 Log.i(TAG, "Config changed: " + config);
3728 long now = SystemClock.uptimeMillis();
3729 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
3730 if (mFreezeGcPending != 0) {
3731 if (now > (mFreezeGcPending+1000)) {
3732 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
3733 mH.removeMessages(H.FORCE_GC);
3734 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 mFreezeGcPending = now;
3736 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003737 } else {
3738 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07003740 return config;
3741 }
Romain Guy06882f82009-06-10 13:36:04 -07003742
Dianne Hackbornc485a602009-03-24 22:39:49 -07003743 boolean computeNewConfigurationLocked(Configuration config) {
3744 if (mDisplay == null) {
3745 return false;
3746 }
3747 mQueue.getInputConfiguration(config);
3748 final int dw = mDisplay.getWidth();
3749 final int dh = mDisplay.getHeight();
3750 int orientation = Configuration.ORIENTATION_SQUARE;
3751 if (dw < dh) {
3752 orientation = Configuration.ORIENTATION_PORTRAIT;
3753 } else if (dw > dh) {
3754 orientation = Configuration.ORIENTATION_LANDSCAPE;
3755 }
3756 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003757
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003758 DisplayMetrics dm = new DisplayMetrics();
3759 mDisplay.getMetrics(dm);
3760 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
3761
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003762 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07003763 // Note we only do this once because at this point we don't
3764 // expect the screen to change in this way at runtime, and want
3765 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07003766 int longSize = dw;
3767 int shortSize = dh;
3768 if (longSize < shortSize) {
3769 int tmp = longSize;
3770 longSize = shortSize;
3771 shortSize = tmp;
3772 }
3773 longSize = (int)(longSize/dm.density);
3774 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07003775
Dianne Hackborn723738c2009-06-25 19:48:04 -07003776 // These semi-magic numbers define our compatibility modes for
3777 // applications with different screens. Don't change unless you
3778 // make sure to test lots and lots of apps!
3779 if (longSize < 470) {
3780 // This is shorter than an HVGA normal density screen (which
3781 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003782 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
3783 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003784 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003785 // Is this a large screen?
3786 if (longSize > 640 && shortSize >= 480) {
3787 // VGA or larger screens at medium density are the point
3788 // at which we consider it to be a large screen.
3789 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
3790 } else {
3791 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
3792
3793 // If this screen is wider than normal HVGA, or taller
3794 // than FWVGA, then for old apps we want to run in size
3795 // compatibility mode.
3796 if (shortSize > 321 || longSize > 570) {
3797 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
3798 }
3799 }
3800
3801 // Is this a long screen?
3802 if (((longSize*3)/5) >= (shortSize-1)) {
3803 // Anything wider than WVGA (5:3) is considering to be long.
3804 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
3805 } else {
3806 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
3807 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07003808 }
3809 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07003810 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07003811
Dianne Hackbornc485a602009-03-24 22:39:49 -07003812 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
3813 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
3814 mPolicy.adjustConfigurationLw(config);
3815 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 }
Romain Guy06882f82009-06-10 13:36:04 -07003817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 // -------------------------------------------------------------
3819 // Input Events and Focus Management
3820 // -------------------------------------------------------------
3821
3822 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07003823 long curTime = SystemClock.uptimeMillis();
3824
3825 if (eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
3826 if (mLastTouchEventType == eventType &&
3827 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
3828 return;
3829 }
3830 mLastUserActivityCallTime = curTime;
3831 mLastTouchEventType = eventType;
3832 }
3833
3834 if (targetWin == null
3835 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
3836 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 }
3838 }
3839
3840 // tells if it's a cheek event or not -- this function is stateful
3841 private static final int EVENT_NONE = 0;
3842 private static final int EVENT_UNKNOWN = 0;
3843 private static final int EVENT_CHEEK = 0;
3844 private static final int EVENT_IGNORE_DURATION = 300; // ms
3845 private static final float CHEEK_THRESHOLD = 0.6f;
3846 private int mEventState = EVENT_NONE;
3847 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 private int eventType(MotionEvent ev) {
3850 float size = ev.getSize();
3851 switch (ev.getAction()) {
3852 case MotionEvent.ACTION_DOWN:
3853 mEventSize = size;
3854 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
3855 case MotionEvent.ACTION_UP:
3856 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003857 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 case MotionEvent.ACTION_MOVE:
3859 final int N = ev.getHistorySize();
3860 if (size > mEventSize) mEventSize = size;
3861 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3862 for (int i=0; i<N; i++) {
3863 size = ev.getHistoricalSize(i);
3864 if (size > mEventSize) mEventSize = size;
3865 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
3866 }
3867 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
3868 return TOUCH_EVENT;
3869 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07003870 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 }
3872 default:
3873 // not good
3874 return OTHER_EVENT;
3875 }
3876 }
3877
3878 /**
3879 * @return Returns true if event was dispatched, false if it was dropped for any reason
3880 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07003881 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
3883 "dispatchPointer " + ev);
3884
3885 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07003886 ev, true, false, pid, uid);
Romain Guy06882f82009-06-10 13:36:04 -07003887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07003889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 if (action == MotionEvent.ACTION_UP) {
3891 // let go of our target
3892 mKeyWaiter.mMotionTarget = null;
3893 mPowerManager.logPointerUpEvent();
3894 } else if (action == MotionEvent.ACTION_DOWN) {
3895 mPowerManager.logPointerDownEvent();
3896 }
3897
3898 if (targetObj == null) {
3899 // In this case we are either dropping the event, or have received
3900 // a move or up without a down. It is common to receive move
3901 // events in such a way, since this means the user is moving the
3902 // pointer without actually pressing down. All other cases should
3903 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07003904 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
3906 }
3907 if (qev != null) {
3908 mQueue.recycleEvent(qev);
3909 }
3910 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003911 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 }
3913 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
3914 if (qev != null) {
3915 mQueue.recycleEvent(qev);
3916 }
3917 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003918 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 }
Romain Guy06882f82009-06-10 13:36:04 -07003920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07003922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07003924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 //Log.i(TAG, "Sending " + ev + " to " + target);
3926
3927 if (uid != 0 && uid != target.mSession.mUid) {
3928 if (mContext.checkPermission(
3929 android.Manifest.permission.INJECT_EVENTS, pid, uid)
3930 != PackageManager.PERMISSION_GRANTED) {
3931 Log.w(TAG, "Permission denied: injecting pointer event from pid "
3932 + pid + " uid " + uid + " to window " + target
3933 + " owned by uid " + target.mSession.mUid);
3934 if (qev != null) {
3935 mQueue.recycleEvent(qev);
3936 }
3937 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003938 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 }
3940 }
Romain Guy06882f82009-06-10 13:36:04 -07003941
3942 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
3944 //target wants to ignore fat touch events
3945 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
3946 //explicit flag to return without processing event further
3947 boolean returnFlag = false;
3948 if((action == MotionEvent.ACTION_DOWN)) {
3949 mFatTouch = false;
3950 if(cheekPress) {
3951 mFatTouch = true;
3952 returnFlag = true;
3953 }
3954 } else {
3955 if(action == MotionEvent.ACTION_UP) {
3956 if(mFatTouch) {
3957 //earlier even was invalid doesnt matter if current up is cheekpress or not
3958 mFatTouch = false;
3959 returnFlag = true;
3960 } else if(cheekPress) {
3961 //cancel the earlier event
3962 ev.setAction(MotionEvent.ACTION_CANCEL);
3963 action = MotionEvent.ACTION_CANCEL;
3964 }
3965 } else if(action == MotionEvent.ACTION_MOVE) {
3966 if(mFatTouch) {
3967 //two cases here
3968 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07003969 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 returnFlag = true;
3971 } else if(cheekPress) {
3972 //valid down followed by invalid moves
3973 //an invalid move have to cancel earlier action
3974 ev.setAction(MotionEvent.ACTION_CANCEL);
3975 action = MotionEvent.ACTION_CANCEL;
3976 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
3977 //note that the subsequent invalid moves will not get here
3978 mFatTouch = true;
3979 }
3980 }
3981 } //else if action
3982 if(returnFlag) {
3983 //recycle que, ev
3984 if (qev != null) {
3985 mQueue.recycleEvent(qev);
3986 }
3987 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07003988 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 }
3990 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07003991
3992 // TODO remove once we settle on a value or make it app specific
3993 if (action == MotionEvent.ACTION_DOWN) {
3994 int max_events_per_sec = 35;
3995 try {
3996 max_events_per_sec = Integer.parseInt(SystemProperties
3997 .get("windowsmgr.max_events_per_sec"));
3998 if (max_events_per_sec < 1) {
3999 max_events_per_sec = 35;
4000 }
4001 } catch (NumberFormatException e) {
4002 }
4003 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4004 }
4005
4006 /*
4007 * Throttle events to minimize CPU usage when there's a flood of events
4008 * e.g. constant contact with the screen
4009 */
4010 if (action == MotionEvent.ACTION_MOVE) {
4011 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4012 long now = SystemClock.uptimeMillis();
4013 if (now < nextEventTime) {
4014 try {
4015 Thread.sleep(nextEventTime - now);
4016 } catch (InterruptedException e) {
4017 }
4018 mLastTouchEventTime = nextEventTime;
4019 } else {
4020 mLastTouchEventTime = now;
4021 }
4022 }
4023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 synchronized(mWindowMap) {
4025 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4026 mKeyWaiter.bindTargetWindowLocked(target,
4027 KeyWaiter.RETURN_PENDING_POINTER, qev);
4028 ev = null;
4029 } else {
4030 if (action == MotionEvent.ACTION_DOWN) {
4031 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4032 if (out != null) {
4033 MotionEvent oev = MotionEvent.obtain(ev);
4034 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4035 do {
4036 final Rect frame = out.mFrame;
4037 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4038 try {
4039 out.mClient.dispatchPointer(oev, eventTime);
4040 } catch (android.os.RemoteException e) {
4041 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4042 }
4043 oev.offsetLocation((float)frame.left, (float)frame.top);
4044 out = out.mNextOutsideTouch;
4045 } while (out != null);
4046 mKeyWaiter.mOutsideTouchTargets = null;
4047 }
4048 }
4049 final Rect frame = target.mFrame;
4050 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4051 mKeyWaiter.bindTargetWindowLocked(target);
4052 }
4053 }
Romain Guy06882f82009-06-10 13:36:04 -07004054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 // finally offset the event to the target's coordinate system and
4056 // dispatch the event.
4057 try {
4058 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4059 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4060 }
4061 target.mClient.dispatchPointer(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004062 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 } catch (android.os.RemoteException e) {
4064 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4065 mKeyWaiter.mMotionTarget = null;
4066 try {
4067 removeWindow(target.mSession, target.mClient);
4068 } catch (java.util.NoSuchElementException ex) {
4069 // This will happen if the window has already been
4070 // removed.
4071 }
4072 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004073 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 }
Romain Guy06882f82009-06-10 13:36:04 -07004075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076 /**
4077 * @return Returns true if event was dispatched, false if it was dropped for any reason
4078 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004079 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 if (DEBUG_INPUT) Log.v(
4081 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004084 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 if (focusObj == null) {
4086 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4087 if (qev != null) {
4088 mQueue.recycleEvent(qev);
4089 }
4090 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004091 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 }
4093 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4094 if (qev != null) {
4095 mQueue.recycleEvent(qev);
4096 }
4097 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004098 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 }
Romain Guy06882f82009-06-10 13:36:04 -07004100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 if (uid != 0 && uid != focus.mSession.mUid) {
4104 if (mContext.checkPermission(
4105 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4106 != PackageManager.PERMISSION_GRANTED) {
4107 Log.w(TAG, "Permission denied: injecting key event from pid "
4108 + pid + " uid " + uid + " to window " + focus
4109 + " owned by uid " + focus.mSession.mUid);
4110 if (qev != null) {
4111 mQueue.recycleEvent(qev);
4112 }
4113 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004114 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 }
4116 }
Romain Guy06882f82009-06-10 13:36:04 -07004117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120 synchronized(mWindowMap) {
4121 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4122 mKeyWaiter.bindTargetWindowLocked(focus,
4123 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4124 // We don't deliver movement events to the client, we hold
4125 // them and wait for them to call back.
4126 ev = null;
4127 } else {
4128 mKeyWaiter.bindTargetWindowLocked(focus);
4129 }
4130 }
Romain Guy06882f82009-06-10 13:36:04 -07004131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 try {
4133 focus.mClient.dispatchTrackball(ev, eventTime);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004134 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 } catch (android.os.RemoteException e) {
4136 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4137 try {
4138 removeWindow(focus.mSession, focus.mClient);
4139 } catch (java.util.NoSuchElementException ex) {
4140 // This will happen if the window has already been
4141 // removed.
4142 }
4143 }
Romain Guy06882f82009-06-10 13:36:04 -07004144
Dianne Hackborncfaef692009-06-15 14:24:44 -07004145 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 }
Romain Guy06882f82009-06-10 13:36:04 -07004147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 /**
4149 * @return Returns true if event was dispatched, false if it was dropped for any reason
4150 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004151 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4153
4154 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004155 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 if (focusObj == null) {
4157 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004158 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 }
4160 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004161 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 }
Romain Guy06882f82009-06-10 13:36:04 -07004163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 if (DEBUG_INPUT) Log.v(
4167 TAG, "Dispatching to " + focus + ": " + event);
4168
4169 if (uid != 0 && uid != focus.mSession.mUid) {
4170 if (mContext.checkPermission(
4171 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4172 != PackageManager.PERMISSION_GRANTED) {
4173 Log.w(TAG, "Permission denied: injecting key event from pid "
4174 + pid + " uid " + uid + " to window " + focus
4175 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004176 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 }
4178 }
Romain Guy06882f82009-06-10 13:36:04 -07004179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 synchronized(mWindowMap) {
4181 mKeyWaiter.bindTargetWindowLocked(focus);
4182 }
4183
4184 // NOSHIP extra state logging
4185 mKeyWaiter.recordDispatchState(event, focus);
4186 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 try {
4189 if (DEBUG_INPUT || DEBUG_FOCUS) {
4190 Log.v(TAG, "Delivering key " + event.getKeyCode()
4191 + " to " + focus);
4192 }
4193 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004194 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 } catch (android.os.RemoteException e) {
4196 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4197 try {
4198 removeWindow(focus.mSession, focus.mClient);
4199 } catch (java.util.NoSuchElementException ex) {
4200 // This will happen if the window has already been
4201 // removed.
4202 }
4203 }
Romain Guy06882f82009-06-10 13:36:04 -07004204
Dianne Hackborncfaef692009-06-15 14:24:44 -07004205 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 }
Romain Guy06882f82009-06-10 13:36:04 -07004207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 public void pauseKeyDispatching(IBinder _token) {
4209 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4210 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004211 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 }
4213
4214 synchronized (mWindowMap) {
4215 WindowToken token = mTokenMap.get(_token);
4216 if (token != null) {
4217 mKeyWaiter.pauseDispatchingLocked(token);
4218 }
4219 }
4220 }
4221
4222 public void resumeKeyDispatching(IBinder _token) {
4223 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4224 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004225 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 }
4227
4228 synchronized (mWindowMap) {
4229 WindowToken token = mTokenMap.get(_token);
4230 if (token != null) {
4231 mKeyWaiter.resumeDispatchingLocked(token);
4232 }
4233 }
4234 }
4235
4236 public void setEventDispatching(boolean enabled) {
4237 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4238 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004239 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 }
4241
4242 synchronized (mWindowMap) {
4243 mKeyWaiter.setEventDispatchingLocked(enabled);
4244 }
4245 }
Romain Guy06882f82009-06-10 13:36:04 -07004246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 /**
4248 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004249 *
4250 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 * {@link SystemClock#uptimeMillis()} as the timebase.)
4252 * @param sync If true, wait for the event to be completed before returning to the caller.
4253 * @return Returns true if event was dispatched, false if it was dropped for any reason
4254 */
4255 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4256 long downTime = ev.getDownTime();
4257 long eventTime = ev.getEventTime();
4258
4259 int action = ev.getAction();
4260 int code = ev.getKeyCode();
4261 int repeatCount = ev.getRepeatCount();
4262 int metaState = ev.getMetaState();
4263 int deviceId = ev.getDeviceId();
4264 int scancode = ev.getScanCode();
4265
4266 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4267 if (downTime == 0) downTime = eventTime;
4268
4269 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004270 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004271
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004272 final int pid = Binder.getCallingPid();
4273 final int uid = Binder.getCallingUid();
4274 final long ident = Binder.clearCallingIdentity();
4275 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004277 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004279 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004280 switch (result) {
4281 case INJECT_NO_PERMISSION:
4282 throw new SecurityException(
4283 "Injecting to another application requires INJECT_EVENT permission");
4284 case INJECT_SUCCEEDED:
4285 return true;
4286 }
4287 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 }
4289
4290 /**
4291 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004292 *
4293 * @param ev A motion event describing the pointer (touch) action. (As noted in
4294 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 * {@link SystemClock#uptimeMillis()} as the timebase.)
4296 * @param sync If true, wait for the event to be completed before returning to the caller.
4297 * @return Returns true if event was dispatched, false if it was dropped for any reason
4298 */
4299 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004300 final int pid = Binder.getCallingPid();
4301 final int uid = Binder.getCallingUid();
4302 final long ident = Binder.clearCallingIdentity();
4303 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004305 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004307 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004308 switch (result) {
4309 case INJECT_NO_PERMISSION:
4310 throw new SecurityException(
4311 "Injecting to another application requires INJECT_EVENT permission");
4312 case INJECT_SUCCEEDED:
4313 return true;
4314 }
4315 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 }
Romain Guy06882f82009-06-10 13:36:04 -07004317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 /**
4319 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004320 *
4321 * @param ev A motion event describing the trackball action. (As noted in
4322 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 * {@link SystemClock#uptimeMillis()} as the timebase.)
4324 * @param sync If true, wait for the event to be completed before returning to the caller.
4325 * @return Returns true if event was dispatched, false if it was dropped for any reason
4326 */
4327 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004328 final int pid = Binder.getCallingPid();
4329 final int uid = Binder.getCallingUid();
4330 final long ident = Binder.clearCallingIdentity();
4331 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004333 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004335 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004336 switch (result) {
4337 case INJECT_NO_PERMISSION:
4338 throw new SecurityException(
4339 "Injecting to another application requires INJECT_EVENT permission");
4340 case INJECT_SUCCEEDED:
4341 return true;
4342 }
4343 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 }
Romain Guy06882f82009-06-10 13:36:04 -07004345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 private WindowState getFocusedWindow() {
4347 synchronized (mWindowMap) {
4348 return getFocusedWindowLocked();
4349 }
4350 }
4351
4352 private WindowState getFocusedWindowLocked() {
4353 return mCurrentFocus;
4354 }
Romain Guy06882f82009-06-10 13:36:04 -07004355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 /**
4357 * This class holds the state for dispatching key events. This state
4358 * is protected by the KeyWaiter instance, NOT by the window lock. You
4359 * can be holding the main window lock while acquire the KeyWaiter lock,
4360 * but not the other way around.
4361 */
4362 final class KeyWaiter {
4363 // NOSHIP debugging
4364 public class DispatchState {
4365 private KeyEvent event;
4366 private WindowState focus;
4367 private long time;
4368 private WindowState lastWin;
4369 private IBinder lastBinder;
4370 private boolean finished;
4371 private boolean gotFirstWindow;
4372 private boolean eventDispatching;
4373 private long timeToSwitch;
4374 private boolean wasFrozen;
4375 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004376 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4379 focus = theFocus;
4380 event = theEvent;
4381 time = System.currentTimeMillis();
4382 // snapshot KeyWaiter state
4383 lastWin = mLastWin;
4384 lastBinder = mLastBinder;
4385 finished = mFinished;
4386 gotFirstWindow = mGotFirstWindow;
4387 eventDispatching = mEventDispatching;
4388 timeToSwitch = mTimeToSwitch;
4389 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004390 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 // cache the paused state at ctor time as well
4392 if (theFocus == null || theFocus.mToken == null) {
4393 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4394 focusPaused = false;
4395 } else {
4396 focusPaused = theFocus.mToken.paused;
4397 }
4398 }
Romain Guy06882f82009-06-10 13:36:04 -07004399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 public String toString() {
4401 return "{{" + event + " to " + focus + " @ " + time
4402 + " lw=" + lastWin + " lb=" + lastBinder
4403 + " fin=" + finished + " gfw=" + gotFirstWindow
4404 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004405 + " wf=" + wasFrozen + " fp=" + focusPaused
4406 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 }
4408 };
4409 private DispatchState mDispatchState = null;
4410 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4411 mDispatchState = new DispatchState(theEvent, theFocus);
4412 }
4413 // END NOSHIP
4414
4415 public static final int RETURN_NOTHING = 0;
4416 public static final int RETURN_PENDING_POINTER = 1;
4417 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 final Object SKIP_TARGET_TOKEN = new Object();
4420 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 private WindowState mLastWin = null;
4423 private IBinder mLastBinder = null;
4424 private boolean mFinished = true;
4425 private boolean mGotFirstWindow = false;
4426 private boolean mEventDispatching = true;
4427 private long mTimeToSwitch = 0;
4428 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 // Target of Motion events
4431 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 // Windows above the target who would like to receive an "outside"
4434 // touch event for any down events outside of them.
4435 WindowState mOutsideTouchTargets;
4436
4437 /**
4438 * Wait for the last event dispatch to complete, then find the next
4439 * target that should receive the given event and wait for that one
4440 * to be ready to receive it.
4441 */
4442 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4443 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004444 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004445 long startTime = SystemClock.uptimeMillis();
4446 long keyDispatchingTimeout = 5 * 1000;
4447 long waitedFor = 0;
4448
4449 while (true) {
4450 // Figure out which window we care about. It is either the
4451 // last window we are waiting to have process the event or,
4452 // if none, then the next window we think the event should go
4453 // to. Note: we retrieve mLastWin outside of the lock, so
4454 // it may change before we lock. Thus we must check it again.
4455 WindowState targetWin = mLastWin;
4456 boolean targetIsNew = targetWin == null;
4457 if (DEBUG_INPUT) Log.v(
4458 TAG, "waitForLastKey: mFinished=" + mFinished +
4459 ", mLastWin=" + mLastWin);
4460 if (targetIsNew) {
4461 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004462 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 if (target == SKIP_TARGET_TOKEN) {
4464 // The user has pressed a special key, and we are
4465 // dropping all pending events before it.
4466 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4467 + " " + nextMotion);
4468 return null;
4469 }
4470 if (target == CONSUMED_EVENT_TOKEN) {
4471 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4472 + " " + nextMotion);
4473 return target;
4474 }
4475 targetWin = (WindowState)target;
4476 }
Romain Guy06882f82009-06-10 13:36:04 -07004477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004480 // Now: is it okay to send the next event to this window?
4481 synchronized (this) {
4482 // First: did we come here based on the last window not
4483 // being null, but it changed by the time we got here?
4484 // If so, try again.
4485 if (!targetIsNew && mLastWin == null) {
4486 continue;
4487 }
Romain Guy06882f82009-06-10 13:36:04 -07004488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 // We never dispatch events if not finished with the
4490 // last one, or the display is frozen.
4491 if (mFinished && !mDisplayFrozen) {
4492 // If event dispatching is disabled, then we
4493 // just consume the events.
4494 if (!mEventDispatching) {
4495 if (DEBUG_INPUT) Log.v(TAG,
4496 "Skipping event; dispatching disabled: "
4497 + nextKey + " " + nextMotion);
4498 return null;
4499 }
4500 if (targetWin != null) {
4501 // If this is a new target, and that target is not
4502 // paused or unresponsive, then all looks good to
4503 // handle the event.
4504 if (targetIsNew && !targetWin.mToken.paused) {
4505 return targetWin;
4506 }
Romain Guy06882f82009-06-10 13:36:04 -07004507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 // If we didn't find a target window, and there is no
4509 // focused app window, then just eat the events.
4510 } else if (mFocusedApp == null) {
4511 if (DEBUG_INPUT) Log.v(TAG,
4512 "Skipping event; no focused app: "
4513 + nextKey + " " + nextMotion);
4514 return null;
4515 }
4516 }
Romain Guy06882f82009-06-10 13:36:04 -07004517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 if (DEBUG_INPUT) Log.v(
4519 TAG, "Waiting for last key in " + mLastBinder
4520 + " target=" + targetWin
4521 + " mFinished=" + mFinished
4522 + " mDisplayFrozen=" + mDisplayFrozen
4523 + " targetIsNew=" + targetIsNew
4524 + " paused="
4525 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004526 + " mFocusedApp=" + mFocusedApp
4527 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07004528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 targetApp = targetWin != null
4530 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07004531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 long curTimeout = keyDispatchingTimeout;
4533 if (mTimeToSwitch != 0) {
4534 long now = SystemClock.uptimeMillis();
4535 if (mTimeToSwitch <= now) {
4536 // If an app switch key has been pressed, and we have
4537 // waited too long for the current app to finish
4538 // processing keys, then wait no more!
4539 doFinishedKeyLocked(true);
4540 continue;
4541 }
4542 long switchTimeout = mTimeToSwitch - now;
4543 if (curTimeout > switchTimeout) {
4544 curTimeout = switchTimeout;
4545 }
4546 }
Romain Guy06882f82009-06-10 13:36:04 -07004547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 try {
4549 // after that continue
4550 // processing keys, so we don't get stuck.
4551 if (DEBUG_INPUT) Log.v(
4552 TAG, "Waiting for key dispatch: " + curTimeout);
4553 wait(curTimeout);
4554 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
4555 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004556 + startTime + " switchTime=" + mTimeToSwitch
4557 + " target=" + targetWin + " mLW=" + mLastWin
4558 + " mLB=" + mLastBinder + " fin=" + mFinished
4559 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004560 } catch (InterruptedException e) {
4561 }
4562 }
4563
4564 // If we were frozen during configuration change, restart the
4565 // timeout checks from now; otherwise look at whether we timed
4566 // out before awakening.
4567 if (mWasFrozen) {
4568 waitedFor = 0;
4569 mWasFrozen = false;
4570 } else {
4571 waitedFor = SystemClock.uptimeMillis() - startTime;
4572 }
4573
4574 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
4575 IApplicationToken at = null;
4576 synchronized (this) {
4577 Log.w(TAG, "Key dispatching timed out sending to " +
4578 (targetWin != null ? targetWin.mAttrs.getTitle()
4579 : "<null>"));
4580 // NOSHIP debugging
4581 Log.w(TAG, "Dispatch state: " + mDispatchState);
4582 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
4583 // END NOSHIP
4584 //dump();
4585 if (targetWin != null) {
4586 at = targetWin.getAppToken();
4587 } else if (targetApp != null) {
4588 at = targetApp.appToken;
4589 }
4590 }
4591
4592 boolean abort = true;
4593 if (at != null) {
4594 try {
4595 long timeout = at.getKeyDispatchingTimeout();
4596 if (timeout > waitedFor) {
4597 // we did not wait the proper amount of time for this application.
4598 // set the timeout to be the real timeout and wait again.
4599 keyDispatchingTimeout = timeout - waitedFor;
4600 continue;
4601 } else {
4602 abort = at.keyDispatchingTimedOut();
4603 }
4604 } catch (RemoteException ex) {
4605 }
4606 }
4607
4608 synchronized (this) {
4609 if (abort && (mLastWin == targetWin || targetWin == null)) {
4610 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07004611 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 if (DEBUG_INPUT) Log.v(TAG,
4613 "Window " + mLastWin +
4614 " timed out on key input");
4615 if (mLastWin.mToken.paused) {
4616 Log.w(TAG, "Un-pausing dispatching to this window");
4617 mLastWin.mToken.paused = false;
4618 }
4619 }
4620 if (mMotionTarget == targetWin) {
4621 mMotionTarget = null;
4622 }
4623 mLastWin = null;
4624 mLastBinder = null;
4625 if (failIfTimeout || targetWin == null) {
4626 return null;
4627 }
4628 } else {
4629 Log.w(TAG, "Continuing to wait for key to be dispatched");
4630 startTime = SystemClock.uptimeMillis();
4631 }
4632 }
4633 }
4634 }
4635 }
Romain Guy06882f82009-06-10 13:36:04 -07004636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004638 MotionEvent nextMotion, boolean isPointerEvent,
4639 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07004641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004642 if (nextKey != null) {
4643 // Find the target window for a normal key event.
4644 final int keycode = nextKey.getKeyCode();
4645 final int repeatCount = nextKey.getRepeatCount();
4646 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
4647 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004649 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004650 if (callingUid == 0 ||
4651 mContext.checkPermission(
4652 android.Manifest.permission.INJECT_EVENTS,
4653 callingPid, callingUid)
4654 == PackageManager.PERMISSION_GRANTED) {
4655 mPolicy.interceptKeyTi(null, keycode,
4656 nextKey.getMetaState(), down, repeatCount);
4657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658 Log.w(TAG, "Event timeout during app switch: dropping "
4659 + nextKey);
4660 return SKIP_TARGET_TOKEN;
4661 }
Romain Guy06882f82009-06-10 13:36:04 -07004662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07004664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004665 WindowState focus = null;
4666 synchronized(mWindowMap) {
4667 focus = getFocusedWindowLocked();
4668 }
Romain Guy06882f82009-06-10 13:36:04 -07004669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07004671
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004672 if (callingUid == 0 ||
4673 (focus != null && callingUid == focus.mSession.mUid) ||
4674 mContext.checkPermission(
4675 android.Manifest.permission.INJECT_EVENTS,
4676 callingPid, callingUid)
4677 == PackageManager.PERMISSION_GRANTED) {
4678 if (mPolicy.interceptKeyTi(focus,
4679 keycode, nextKey.getMetaState(), down, repeatCount)) {
4680 return CONSUMED_EVENT_TOKEN;
4681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682 }
Romain Guy06882f82009-06-10 13:36:04 -07004683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07004685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 } else if (!isPointerEvent) {
4687 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
4688 if (!dispatch) {
4689 Log.w(TAG, "Event timeout during app switch: dropping trackball "
4690 + nextMotion);
4691 return SKIP_TARGET_TOKEN;
4692 }
Romain Guy06882f82009-06-10 13:36:04 -07004693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 WindowState focus = null;
4695 synchronized(mWindowMap) {
4696 focus = getFocusedWindowLocked();
4697 }
Romain Guy06882f82009-06-10 13:36:04 -07004698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
4700 return focus;
4701 }
Romain Guy06882f82009-06-10 13:36:04 -07004702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 if (nextMotion == null) {
4704 return SKIP_TARGET_TOKEN;
4705 }
Romain Guy06882f82009-06-10 13:36:04 -07004706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
4708 KeyEvent.KEYCODE_UNKNOWN);
4709 if (!dispatch) {
4710 Log.w(TAG, "Event timeout during app switch: dropping pointer "
4711 + nextMotion);
4712 return SKIP_TARGET_TOKEN;
4713 }
Romain Guy06882f82009-06-10 13:36:04 -07004714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 // Find the target window for a pointer event.
4716 int action = nextMotion.getAction();
4717 final float xf = nextMotion.getX();
4718 final float yf = nextMotion.getY();
4719 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004721 final boolean screenWasOff = qev != null
4722 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07004723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07004725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004726 synchronized(mWindowMap) {
4727 synchronized (this) {
4728 if (action == MotionEvent.ACTION_DOWN) {
4729 if (mMotionTarget != null) {
4730 // this is weird, we got a pen down, but we thought it was
4731 // already down!
4732 // XXX: We should probably send an ACTION_UP to the current
4733 // target.
4734 Log.w(TAG, "Pointer down received while already down in: "
4735 + mMotionTarget);
4736 mMotionTarget = null;
4737 }
Romain Guy06882f82009-06-10 13:36:04 -07004738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 // ACTION_DOWN is special, because we need to lock next events to
4740 // the window we'll land onto.
4741 final int x = (int)xf;
4742 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07004743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004744 final ArrayList windows = mWindows;
4745 final int N = windows.size();
4746 WindowState topErrWindow = null;
4747 final Rect tmpRect = mTempRect;
4748 for (int i=N-1; i>=0; i--) {
4749 WindowState child = (WindowState)windows.get(i);
4750 //Log.i(TAG, "Checking dispatch to: " + child);
4751 final int flags = child.mAttrs.flags;
4752 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
4753 if (topErrWindow == null) {
4754 topErrWindow = child;
4755 }
4756 }
4757 if (!child.isVisibleLw()) {
4758 //Log.i(TAG, "Not visible!");
4759 continue;
4760 }
4761 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
4762 //Log.i(TAG, "Not touchable!");
4763 if ((flags & WindowManager.LayoutParams
4764 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4765 child.mNextOutsideTouch = mOutsideTouchTargets;
4766 mOutsideTouchTargets = child;
4767 }
4768 continue;
4769 }
4770 tmpRect.set(child.mFrame);
4771 if (child.mTouchableInsets == ViewTreeObserver
4772 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
4773 // The touch is inside of the window if it is
4774 // inside the frame, AND the content part of that
4775 // frame that was given by the application.
4776 tmpRect.left += child.mGivenContentInsets.left;
4777 tmpRect.top += child.mGivenContentInsets.top;
4778 tmpRect.right -= child.mGivenContentInsets.right;
4779 tmpRect.bottom -= child.mGivenContentInsets.bottom;
4780 } else if (child.mTouchableInsets == ViewTreeObserver
4781 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
4782 // The touch is inside of the window if it is
4783 // inside the frame, AND the visible part of that
4784 // frame that was given by the application.
4785 tmpRect.left += child.mGivenVisibleInsets.left;
4786 tmpRect.top += child.mGivenVisibleInsets.top;
4787 tmpRect.right -= child.mGivenVisibleInsets.right;
4788 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
4789 }
4790 final int touchFlags = flags &
4791 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
4792 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
4793 if (tmpRect.contains(x, y) || touchFlags == 0) {
4794 //Log.i(TAG, "Using this target!");
4795 if (!screenWasOff || (flags &
4796 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
4797 mMotionTarget = child;
4798 } else {
4799 //Log.i(TAG, "Waking, skip!");
4800 mMotionTarget = null;
4801 }
4802 break;
4803 }
Romain Guy06882f82009-06-10 13:36:04 -07004804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004805 if ((flags & WindowManager.LayoutParams
4806 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
4807 child.mNextOutsideTouch = mOutsideTouchTargets;
4808 mOutsideTouchTargets = child;
4809 //Log.i(TAG, "Adding to outside target list: " + child);
4810 }
4811 }
4812
4813 // if there's an error window but it's not accepting
4814 // focus (typically because it is not yet visible) just
4815 // wait for it -- any other focused window may in fact
4816 // be in ANR state.
4817 if (topErrWindow != null && mMotionTarget != topErrWindow) {
4818 mMotionTarget = null;
4819 }
4820 }
Romain Guy06882f82009-06-10 13:36:04 -07004821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004822 target = mMotionTarget;
4823 }
4824 }
Romain Guy06882f82009-06-10 13:36:04 -07004825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07004827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 // Pointer events are a little different -- if there isn't a
4829 // target found for any event, then just drop it.
4830 return target != null ? target : SKIP_TARGET_TOKEN;
4831 }
Romain Guy06882f82009-06-10 13:36:04 -07004832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 boolean checkShouldDispatchKey(int keycode) {
4834 synchronized (this) {
4835 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
4836 mTimeToSwitch = 0;
4837 return true;
4838 }
4839 if (mTimeToSwitch != 0
4840 && mTimeToSwitch < SystemClock.uptimeMillis()) {
4841 return false;
4842 }
4843 return true;
4844 }
4845 }
Romain Guy06882f82009-06-10 13:36:04 -07004846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847 void bindTargetWindowLocked(WindowState win,
4848 int pendingWhat, QueuedEvent pendingMotion) {
4849 synchronized (this) {
4850 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
4851 }
4852 }
Romain Guy06882f82009-06-10 13:36:04 -07004853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004854 void bindTargetWindowLocked(WindowState win) {
4855 synchronized (this) {
4856 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
4857 }
4858 }
4859
4860 void bindTargetWindowLockedLocked(WindowState win,
4861 int pendingWhat, QueuedEvent pendingMotion) {
4862 mLastWin = win;
4863 mLastBinder = win.mClient.asBinder();
4864 mFinished = false;
4865 if (pendingMotion != null) {
4866 final Session s = win.mSession;
4867 if (pendingWhat == RETURN_PENDING_POINTER) {
4868 releasePendingPointerLocked(s);
4869 s.mPendingPointerMove = pendingMotion;
4870 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07004871 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004872 "bindTargetToWindow " + s.mPendingPointerMove);
4873 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
4874 releasePendingTrackballLocked(s);
4875 s.mPendingTrackballMove = pendingMotion;
4876 s.mPendingTrackballWindow = win;
4877 }
4878 }
4879 }
Romain Guy06882f82009-06-10 13:36:04 -07004880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004881 void releasePendingPointerLocked(Session s) {
4882 if (DEBUG_INPUT) Log.v(TAG,
4883 "releasePendingPointer " + s.mPendingPointerMove);
4884 if (s.mPendingPointerMove != null) {
4885 mQueue.recycleEvent(s.mPendingPointerMove);
4886 s.mPendingPointerMove = null;
4887 }
4888 }
Romain Guy06882f82009-06-10 13:36:04 -07004889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 void releasePendingTrackballLocked(Session s) {
4891 if (s.mPendingTrackballMove != null) {
4892 mQueue.recycleEvent(s.mPendingTrackballMove);
4893 s.mPendingTrackballMove = null;
4894 }
4895 }
Romain Guy06882f82009-06-10 13:36:04 -07004896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004897 MotionEvent finishedKey(Session session, IWindow client, boolean force,
4898 int returnWhat) {
4899 if (DEBUG_INPUT) Log.v(
4900 TAG, "finishedKey: client=" + client + ", force=" + force);
4901
4902 if (client == null) {
4903 return null;
4904 }
4905
4906 synchronized (this) {
4907 if (DEBUG_INPUT) Log.v(
4908 TAG, "finishedKey: client=" + client.asBinder()
4909 + ", force=" + force + ", last=" + mLastBinder
4910 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
4911
4912 QueuedEvent qev = null;
4913 WindowState win = null;
4914 if (returnWhat == RETURN_PENDING_POINTER) {
4915 qev = session.mPendingPointerMove;
4916 win = session.mPendingPointerWindow;
4917 session.mPendingPointerMove = null;
4918 session.mPendingPointerWindow = null;
4919 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
4920 qev = session.mPendingTrackballMove;
4921 win = session.mPendingTrackballWindow;
4922 session.mPendingTrackballMove = null;
4923 session.mPendingTrackballWindow = null;
4924 }
Romain Guy06882f82009-06-10 13:36:04 -07004925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 if (mLastBinder == client.asBinder()) {
4927 if (DEBUG_INPUT) Log.v(
4928 TAG, "finishedKey: last paused="
4929 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
4930 if (mLastWin != null && (!mLastWin.mToken.paused || force
4931 || !mEventDispatching)) {
4932 doFinishedKeyLocked(false);
4933 } else {
4934 // Make sure to wake up anyone currently waiting to
4935 // dispatch a key, so they can re-evaluate their
4936 // current situation.
4937 mFinished = true;
4938 notifyAll();
4939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 }
Romain Guy06882f82009-06-10 13:36:04 -07004941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942 if (qev != null) {
4943 MotionEvent res = (MotionEvent)qev.event;
4944 if (DEBUG_INPUT) Log.v(TAG,
4945 "Returning pending motion: " + res);
4946 mQueue.recycleEvent(qev);
4947 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
4948 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
4949 }
4950 return res;
4951 }
4952 return null;
4953 }
4954 }
4955
4956 void tickle() {
4957 synchronized (this) {
4958 notifyAll();
4959 }
4960 }
Romain Guy06882f82009-06-10 13:36:04 -07004961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962 void handleNewWindowLocked(WindowState newWindow) {
4963 if (!newWindow.canReceiveKeys()) {
4964 return;
4965 }
4966 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004967 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004968 TAG, "New key dispatch window: win="
4969 + newWindow.mClient.asBinder()
4970 + ", last=" + mLastBinder
4971 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
4972 + "), finished=" + mFinished + ", paused="
4973 + newWindow.mToken.paused);
4974
4975 // Displaying a window implicitly causes dispatching to
4976 // be unpaused. (This is to protect against bugs if someone
4977 // pauses dispatching but forgets to resume.)
4978 newWindow.mToken.paused = false;
4979
4980 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004981
4982 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
4983 if (DEBUG_INPUT) Log.v(TAG,
4984 "New SYSTEM_ERROR window; resetting state");
4985 mLastWin = null;
4986 mLastBinder = null;
4987 mMotionTarget = null;
4988 mFinished = true;
4989 } else if (mLastWin != null) {
4990 // If the new window is above the window we are
4991 // waiting on, then stop waiting and let key dispatching
4992 // start on the new guy.
4993 if (DEBUG_INPUT) Log.v(
4994 TAG, "Last win layer=" + mLastWin.mLayer
4995 + ", new win layer=" + newWindow.mLayer);
4996 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004997 // The new window is above the old; finish pending input to the last
4998 // window and start directing it to the new one.
4999 mLastWin.mToken.paused = false;
5000 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005002 // Either the new window is lower, so there is no need to wake key waiters,
5003 // or we just finished key input to the previous window, which implicitly
5004 // notified the key waiters. In both cases, we don't need to issue the
5005 // notification here.
5006 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 }
5008
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005009 // Now that we've put a new window state in place, make the event waiter
5010 // take notice and retarget its attentions.
5011 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 }
5013 }
5014
5015 void pauseDispatchingLocked(WindowToken token) {
5016 synchronized (this)
5017 {
5018 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5019 token.paused = true;
5020
5021 /*
5022 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5023 mPaused = true;
5024 } else {
5025 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005026 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005028 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005029 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005030 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 }
5032 }
5033 */
5034 }
5035 }
5036
5037 void resumeDispatchingLocked(WindowToken token) {
5038 synchronized (this) {
5039 if (token.paused) {
5040 if (DEBUG_INPUT) Log.v(
5041 TAG, "Resuming WindowToken " + token
5042 + ", last=" + mLastBinder
5043 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5044 + "), finished=" + mFinished + ", paused="
5045 + token.paused);
5046 token.paused = false;
5047 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5048 doFinishedKeyLocked(true);
5049 } else {
5050 notifyAll();
5051 }
5052 }
5053 }
5054 }
5055
5056 void setEventDispatchingLocked(boolean enabled) {
5057 synchronized (this) {
5058 mEventDispatching = enabled;
5059 notifyAll();
5060 }
5061 }
Romain Guy06882f82009-06-10 13:36:04 -07005062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 void appSwitchComing() {
5064 synchronized (this) {
5065 // Don't wait for more than .5 seconds for app to finish
5066 // processing the pending events.
5067 long now = SystemClock.uptimeMillis() + 500;
5068 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5069 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5070 mTimeToSwitch = now;
5071 }
5072 notifyAll();
5073 }
5074 }
Romain Guy06882f82009-06-10 13:36:04 -07005075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 private final void doFinishedKeyLocked(boolean doRecycle) {
5077 if (mLastWin != null) {
5078 releasePendingPointerLocked(mLastWin.mSession);
5079 releasePendingTrackballLocked(mLastWin.mSession);
5080 }
Romain Guy06882f82009-06-10 13:36:04 -07005081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 if (mLastWin == null || !mLastWin.mToken.paused
5083 || !mLastWin.isVisibleLw()) {
5084 // If the current window has been paused, we aren't -really-
5085 // finished... so let the waiters still wait.
5086 mLastWin = null;
5087 mLastBinder = null;
5088 }
5089 mFinished = true;
5090 notifyAll();
5091 }
5092 }
5093
5094 private class KeyQ extends KeyInputQueue
5095 implements KeyInputQueue.FilterCallback {
5096 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005098 KeyQ() {
5099 super(mContext);
5100 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5101 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5102 "KEEP_SCREEN_ON_FLAG");
5103 mHoldingScreen.setReferenceCounted(false);
5104 }
5105
5106 @Override
5107 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5108 if (mPolicy.preprocessInputEventTq(event)) {
5109 return true;
5110 }
Romain Guy06882f82009-06-10 13:36:04 -07005111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 switch (event.type) {
5113 case RawInputEvent.EV_KEY: {
5114 // XXX begin hack
5115 if (DEBUG) {
5116 if (event.keycode == KeyEvent.KEYCODE_G) {
5117 if (event.value != 0) {
5118 // G down
5119 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5120 }
5121 return false;
5122 }
5123 if (event.keycode == KeyEvent.KEYCODE_D) {
5124 if (event.value != 0) {
5125 //dump();
5126 }
5127 return false;
5128 }
5129 }
5130 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 boolean screenIsOff = !mPowerManager.screenIsOn();
5133 boolean screenIsDim = !mPowerManager.screenIsBright();
5134 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005136 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5137 mPowerManager.goToSleep(event.when);
5138 }
5139
5140 if (screenIsOff) {
5141 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5142 }
5143 if (screenIsDim) {
5144 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5145 }
5146 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5147 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005148 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 }
Romain Guy06882f82009-06-10 13:36:04 -07005150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005151 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5152 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5153 filterQueue(this);
5154 mKeyWaiter.appSwitchComing();
5155 }
5156 return true;
5157 } else {
5158 return false;
5159 }
5160 }
Romain Guy06882f82009-06-10 13:36:04 -07005161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 case RawInputEvent.EV_REL: {
5163 boolean screenIsOff = !mPowerManager.screenIsOn();
5164 boolean screenIsDim = !mPowerManager.screenIsBright();
5165 if (screenIsOff) {
5166 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5167 device.classes, event)) {
5168 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5169 return false;
5170 }
5171 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5172 }
5173 if (screenIsDim) {
5174 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5175 }
5176 return true;
5177 }
Romain Guy06882f82009-06-10 13:36:04 -07005178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 case RawInputEvent.EV_ABS: {
5180 boolean screenIsOff = !mPowerManager.screenIsOn();
5181 boolean screenIsDim = !mPowerManager.screenIsBright();
5182 if (screenIsOff) {
5183 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5184 device.classes, event)) {
5185 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5186 return false;
5187 }
5188 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5189 }
5190 if (screenIsDim) {
5191 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5192 }
5193 return true;
5194 }
Romain Guy06882f82009-06-10 13:36:04 -07005195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 default:
5197 return true;
5198 }
5199 }
5200
5201 public int filterEvent(QueuedEvent ev) {
5202 switch (ev.classType) {
5203 case RawInputEvent.CLASS_KEYBOARD:
5204 KeyEvent ke = (KeyEvent)ev.event;
5205 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5206 Log.w(TAG, "Dropping movement key during app switch: "
5207 + ke.getKeyCode() + ", action=" + ke.getAction());
5208 return FILTER_REMOVE;
5209 }
5210 return FILTER_ABORT;
5211 default:
5212 return FILTER_KEEP;
5213 }
5214 }
Romain Guy06882f82009-06-10 13:36:04 -07005215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 /**
5217 * Must be called with the main window manager lock held.
5218 */
5219 void setHoldScreenLocked(boolean holding) {
5220 boolean state = mHoldingScreen.isHeld();
5221 if (holding != state) {
5222 if (holding) {
5223 mHoldingScreen.acquire();
5224 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005225 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 mHoldingScreen.release();
5227 }
5228 }
5229 }
5230 };
5231
5232 public boolean detectSafeMode() {
5233 mSafeMode = mPolicy.detectSafeMode();
5234 return mSafeMode;
5235 }
Romain Guy06882f82009-06-10 13:36:04 -07005236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 public void systemReady() {
5238 mPolicy.systemReady();
5239 }
Romain Guy06882f82009-06-10 13:36:04 -07005240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241 private final class InputDispatcherThread extends Thread {
5242 // Time to wait when there is nothing to do: 9999 seconds.
5243 static final int LONG_WAIT=9999*1000;
5244
5245 public InputDispatcherThread() {
5246 super("InputDispatcher");
5247 }
Romain Guy06882f82009-06-10 13:36:04 -07005248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005249 @Override
5250 public void run() {
5251 while (true) {
5252 try {
5253 process();
5254 } catch (Exception e) {
5255 Log.e(TAG, "Exception in input dispatcher", e);
5256 }
5257 }
5258 }
Romain Guy06882f82009-06-10 13:36:04 -07005259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260 private void process() {
5261 android.os.Process.setThreadPriority(
5262 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 // The last key event we saw
5265 KeyEvent lastKey = null;
5266
5267 // Last keydown time for auto-repeating keys
5268 long lastKeyTime = SystemClock.uptimeMillis();
5269 long nextKeyTime = lastKeyTime+LONG_WAIT;
5270
Romain Guy06882f82009-06-10 13:36:04 -07005271 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005272 int keyRepeatCount = 0;
5273
5274 // Need to report that configuration has changed?
5275 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 while (true) {
5278 long curTime = SystemClock.uptimeMillis();
5279
5280 if (DEBUG_INPUT) Log.v(
5281 TAG, "Waiting for next key: now=" + curTime
5282 + ", repeat @ " + nextKeyTime);
5283
5284 // Retrieve next event, waiting only as long as the next
5285 // repeat timeout. If the configuration has changed, then
5286 // don't wait at all -- we'll report the change as soon as
5287 // we have processed all events.
5288 QueuedEvent ev = mQueue.getEvent(
5289 (int)((!configChanged && curTime < nextKeyTime)
5290 ? (nextKeyTime-curTime) : 0));
5291
5292 if (DEBUG_INPUT && ev != null) Log.v(
5293 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5294
5295 try {
5296 if (ev != null) {
5297 curTime = ev.when;
5298 int eventType;
5299 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5300 eventType = eventType((MotionEvent)ev.event);
5301 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5302 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5303 eventType = LocalPowerManager.BUTTON_EVENT;
5304 } else {
5305 eventType = LocalPowerManager.OTHER_EVENT;
5306 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005307 try {
Michael Chane96440f2009-05-06 10:27:36 -07005308 long now = SystemClock.uptimeMillis();
5309
5310 if ((now - mLastBatteryStatsCallTime)
5311 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5312 mLastBatteryStatsCallTime = now;
5313 mBatteryStats.noteInputEvent();
5314 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005315 } catch (RemoteException e) {
5316 // Ignore
5317 }
Michael Chane96440f2009-05-06 10:27:36 -07005318 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 switch (ev.classType) {
5320 case RawInputEvent.CLASS_KEYBOARD:
5321 KeyEvent ke = (KeyEvent)ev.event;
5322 if (ke.isDown()) {
5323 lastKey = ke;
5324 keyRepeatCount = 0;
5325 lastKeyTime = curTime;
5326 nextKeyTime = lastKeyTime
5327 + KEY_REPEAT_FIRST_DELAY;
5328 if (DEBUG_INPUT) Log.v(
5329 TAG, "Received key down: first repeat @ "
5330 + nextKeyTime);
5331 } else {
5332 lastKey = null;
5333 // Arbitrary long timeout.
5334 lastKeyTime = curTime;
5335 nextKeyTime = curTime + LONG_WAIT;
5336 if (DEBUG_INPUT) Log.v(
5337 TAG, "Received key up: ignore repeat @ "
5338 + nextKeyTime);
5339 }
5340 dispatchKey((KeyEvent)ev.event, 0, 0);
5341 mQueue.recycleEvent(ev);
5342 break;
5343 case RawInputEvent.CLASS_TOUCHSCREEN:
5344 //Log.i(TAG, "Read next event " + ev);
5345 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5346 break;
5347 case RawInputEvent.CLASS_TRACKBALL:
5348 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5349 break;
5350 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5351 configChanged = true;
5352 break;
5353 default:
5354 mQueue.recycleEvent(ev);
5355 break;
5356 }
Romain Guy06882f82009-06-10 13:36:04 -07005357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005358 } else if (configChanged) {
5359 configChanged = false;
5360 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 } else if (lastKey != null) {
5363 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365 // Timeout occurred while key was down. If it is at or
5366 // past the key repeat time, dispatch the repeat.
5367 if (DEBUG_INPUT) Log.v(
5368 TAG, "Key timeout: repeat=" + nextKeyTime
5369 + ", now=" + curTime);
5370 if (curTime < nextKeyTime) {
5371 continue;
5372 }
Romain Guy06882f82009-06-10 13:36:04 -07005373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005374 lastKeyTime = nextKeyTime;
5375 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5376 keyRepeatCount++;
5377 if (DEBUG_INPUT) Log.v(
5378 TAG, "Key repeat: count=" + keyRepeatCount
5379 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005380 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 } else {
5383 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005385 lastKeyTime = curTime;
5386 nextKeyTime = curTime + LONG_WAIT;
5387 }
Romain Guy06882f82009-06-10 13:36:04 -07005388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 } catch (Exception e) {
5390 Log.e(TAG,
5391 "Input thread received uncaught exception: " + e, e);
5392 }
5393 }
5394 }
5395 }
5396
5397 // -------------------------------------------------------------
5398 // Client Session State
5399 // -------------------------------------------------------------
5400
5401 private final class Session extends IWindowSession.Stub
5402 implements IBinder.DeathRecipient {
5403 final IInputMethodClient mClient;
5404 final IInputContext mInputContext;
5405 final int mUid;
5406 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005407 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005408 SurfaceSession mSurfaceSession;
5409 int mNumWindow = 0;
5410 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 /**
5413 * Current pointer move event being dispatched to client window... must
5414 * hold key lock to access.
5415 */
5416 QueuedEvent mPendingPointerMove;
5417 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 /**
5420 * Current trackball move event being dispatched to client window... must
5421 * hold key lock to access.
5422 */
5423 QueuedEvent mPendingTrackballMove;
5424 WindowState mPendingTrackballWindow;
5425
5426 public Session(IInputMethodClient client, IInputContext inputContext) {
5427 mClient = client;
5428 mInputContext = inputContext;
5429 mUid = Binder.getCallingUid();
5430 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005431 StringBuilder sb = new StringBuilder();
5432 sb.append("Session{");
5433 sb.append(Integer.toHexString(System.identityHashCode(this)));
5434 sb.append(" uid ");
5435 sb.append(mUid);
5436 sb.append("}");
5437 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 synchronized (mWindowMap) {
5440 if (mInputMethodManager == null && mHaveInputMethods) {
5441 IBinder b = ServiceManager.getService(
5442 Context.INPUT_METHOD_SERVICE);
5443 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5444 }
5445 }
5446 long ident = Binder.clearCallingIdentity();
5447 try {
5448 // Note: it is safe to call in to the input method manager
5449 // here because we are not holding our lock.
5450 if (mInputMethodManager != null) {
5451 mInputMethodManager.addClient(client, inputContext,
5452 mUid, mPid);
5453 } else {
5454 client.setUsingInputMethod(false);
5455 }
5456 client.asBinder().linkToDeath(this, 0);
5457 } catch (RemoteException e) {
5458 // The caller has died, so we can just forget about this.
5459 try {
5460 if (mInputMethodManager != null) {
5461 mInputMethodManager.removeClient(client);
5462 }
5463 } catch (RemoteException ee) {
5464 }
5465 } finally {
5466 Binder.restoreCallingIdentity(ident);
5467 }
5468 }
Romain Guy06882f82009-06-10 13:36:04 -07005469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005470 @Override
5471 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5472 throws RemoteException {
5473 try {
5474 return super.onTransact(code, data, reply, flags);
5475 } catch (RuntimeException e) {
5476 // Log all 'real' exceptions thrown to the caller
5477 if (!(e instanceof SecurityException)) {
5478 Log.e(TAG, "Window Session Crash", e);
5479 }
5480 throw e;
5481 }
5482 }
5483
5484 public void binderDied() {
5485 // Note: it is safe to call in to the input method manager
5486 // here because we are not holding our lock.
5487 try {
5488 if (mInputMethodManager != null) {
5489 mInputMethodManager.removeClient(mClient);
5490 }
5491 } catch (RemoteException e) {
5492 }
5493 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005494 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005495 mClientDead = true;
5496 killSessionLocked();
5497 }
5498 }
5499
5500 public int add(IWindow window, WindowManager.LayoutParams attrs,
5501 int viewVisibility, Rect outContentInsets) {
5502 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
5503 }
Romain Guy06882f82009-06-10 13:36:04 -07005504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005505 public void remove(IWindow window) {
5506 removeWindow(this, window);
5507 }
Romain Guy06882f82009-06-10 13:36:04 -07005508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
5510 int requestedWidth, int requestedHeight, int viewFlags,
5511 boolean insetsPending, Rect outFrame, Rect outContentInsets,
5512 Rect outVisibleInsets, Surface outSurface) {
5513 return relayoutWindow(this, window, attrs,
5514 requestedWidth, requestedHeight, viewFlags, insetsPending,
5515 outFrame, outContentInsets, outVisibleInsets, outSurface);
5516 }
Romain Guy06882f82009-06-10 13:36:04 -07005517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 public void setTransparentRegion(IWindow window, Region region) {
5519 setTransparentRegionWindow(this, window, region);
5520 }
Romain Guy06882f82009-06-10 13:36:04 -07005521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 public void setInsets(IWindow window, int touchableInsets,
5523 Rect contentInsets, Rect visibleInsets) {
5524 setInsetsWindow(this, window, touchableInsets, contentInsets,
5525 visibleInsets);
5526 }
Romain Guy06882f82009-06-10 13:36:04 -07005527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
5529 getWindowDisplayFrame(this, window, outDisplayFrame);
5530 }
Romain Guy06882f82009-06-10 13:36:04 -07005531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 public void finishDrawing(IWindow window) {
5533 if (localLOGV) Log.v(
5534 TAG, "IWindow finishDrawing called for " + window);
5535 finishDrawingWindow(this, window);
5536 }
5537
5538 public void finishKey(IWindow window) {
5539 if (localLOGV) Log.v(
5540 TAG, "IWindow finishKey called for " + window);
5541 mKeyWaiter.finishedKey(this, window, false,
5542 KeyWaiter.RETURN_NOTHING);
5543 }
5544
5545 public MotionEvent getPendingPointerMove(IWindow window) {
5546 if (localLOGV) Log.v(
5547 TAG, "IWindow getPendingMotionEvent called for " + window);
5548 return mKeyWaiter.finishedKey(this, window, false,
5549 KeyWaiter.RETURN_PENDING_POINTER);
5550 }
Romain Guy06882f82009-06-10 13:36:04 -07005551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 public MotionEvent getPendingTrackballMove(IWindow window) {
5553 if (localLOGV) Log.v(
5554 TAG, "IWindow getPendingMotionEvent called for " + window);
5555 return mKeyWaiter.finishedKey(this, window, false,
5556 KeyWaiter.RETURN_PENDING_TRACKBALL);
5557 }
5558
5559 public void setInTouchMode(boolean mode) {
5560 synchronized(mWindowMap) {
5561 mInTouchMode = mode;
5562 }
5563 }
5564
5565 public boolean getInTouchMode() {
5566 synchronized(mWindowMap) {
5567 return mInTouchMode;
5568 }
5569 }
5570
5571 public boolean performHapticFeedback(IWindow window, int effectId,
5572 boolean always) {
5573 synchronized(mWindowMap) {
5574 long ident = Binder.clearCallingIdentity();
5575 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005576 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 windowForClientLocked(this, window), effectId, always);
5578 } finally {
5579 Binder.restoreCallingIdentity(ident);
5580 }
5581 }
5582 }
Romain Guy06882f82009-06-10 13:36:04 -07005583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005584 void windowAddedLocked() {
5585 if (mSurfaceSession == null) {
5586 if (localLOGV) Log.v(
5587 TAG, "First window added to " + this + ", creating SurfaceSession");
5588 mSurfaceSession = new SurfaceSession();
5589 mSessions.add(this);
5590 }
5591 mNumWindow++;
5592 }
5593
5594 void windowRemovedLocked() {
5595 mNumWindow--;
5596 killSessionLocked();
5597 }
Romain Guy06882f82009-06-10 13:36:04 -07005598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005599 void killSessionLocked() {
5600 if (mNumWindow <= 0 && mClientDead) {
5601 mSessions.remove(this);
5602 if (mSurfaceSession != null) {
5603 if (localLOGV) Log.v(
5604 TAG, "Last window removed from " + this
5605 + ", destroying " + mSurfaceSession);
5606 try {
5607 mSurfaceSession.kill();
5608 } catch (Exception e) {
5609 Log.w(TAG, "Exception thrown when killing surface session "
5610 + mSurfaceSession + " in session " + this
5611 + ": " + e.toString());
5612 }
5613 mSurfaceSession = null;
5614 }
5615 }
5616 }
Romain Guy06882f82009-06-10 13:36:04 -07005617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005619 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
5620 pw.print(" mClientDead="); pw.print(mClientDead);
5621 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
5622 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
5623 pw.print(prefix);
5624 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
5625 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
5626 }
5627 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
5628 pw.print(prefix);
5629 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
5630 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
5631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 }
5633
5634 @Override
5635 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005636 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005637 }
5638 }
5639
5640 // -------------------------------------------------------------
5641 // Client Window State
5642 // -------------------------------------------------------------
5643
5644 private final class WindowState implements WindowManagerPolicy.WindowState {
5645 final Session mSession;
5646 final IWindow mClient;
5647 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07005648 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649 AppWindowToken mAppToken;
5650 AppWindowToken mTargetAppToken;
5651 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
5652 final DeathRecipient mDeathRecipient;
5653 final WindowState mAttachedWindow;
5654 final ArrayList mChildWindows = new ArrayList();
5655 final int mBaseLayer;
5656 final int mSubLayer;
5657 final boolean mLayoutAttached;
5658 final boolean mIsImWindow;
5659 int mViewVisibility;
5660 boolean mPolicyVisibility = true;
5661 boolean mPolicyVisibilityAfterAnim = true;
5662 boolean mAppFreezing;
5663 Surface mSurface;
5664 boolean mAttachedHidden; // is our parent window hidden?
5665 boolean mLastHidden; // was this window last hidden?
5666 int mRequestedWidth;
5667 int mRequestedHeight;
5668 int mLastRequestedWidth;
5669 int mLastRequestedHeight;
5670 int mReqXPos;
5671 int mReqYPos;
5672 int mLayer;
5673 int mAnimLayer;
5674 int mLastLayer;
5675 boolean mHaveFrame;
5676
5677 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07005678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005679 // Actual frame shown on-screen (may be modified by animation)
5680 final Rect mShownFrame = new Rect();
5681 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005683 /**
5684 * Insets that determine the actually visible area
5685 */
5686 final Rect mVisibleInsets = new Rect();
5687 final Rect mLastVisibleInsets = new Rect();
5688 boolean mVisibleInsetsChanged;
5689
5690 /**
5691 * Insets that are covered by system windows
5692 */
5693 final Rect mContentInsets = new Rect();
5694 final Rect mLastContentInsets = new Rect();
5695 boolean mContentInsetsChanged;
5696
5697 /**
5698 * Set to true if we are waiting for this window to receive its
5699 * given internal insets before laying out other windows based on it.
5700 */
5701 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07005702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 /**
5704 * These are the content insets that were given during layout for
5705 * this window, to be applied to windows behind it.
5706 */
5707 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 /**
5710 * These are the visible insets that were given during layout for
5711 * this window, to be applied to windows behind it.
5712 */
5713 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07005714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 /**
5716 * Flag indicating whether the touchable region should be adjusted by
5717 * the visible insets; if false the area outside the visible insets is
5718 * NOT touchable, so we must use those to adjust the frame during hit
5719 * tests.
5720 */
5721 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07005722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005723 // Current transformation being applied.
5724 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
5725 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
5726 float mHScale=1, mVScale=1;
5727 float mLastHScale=1, mLastVScale=1;
5728 final Matrix mTmpMatrix = new Matrix();
5729
5730 // "Real" frame that the application sees.
5731 final Rect mFrame = new Rect();
5732 final Rect mLastFrame = new Rect();
5733
5734 final Rect mContainingFrame = new Rect();
5735 final Rect mDisplayFrame = new Rect();
5736 final Rect mContentFrame = new Rect();
5737 final Rect mVisibleFrame = new Rect();
5738
5739 float mShownAlpha = 1;
5740 float mAlpha = 1;
5741 float mLastAlpha = 1;
5742
5743 // Set to true if, when the window gets displayed, it should perform
5744 // an enter animation.
5745 boolean mEnterAnimationPending;
5746
5747 // Currently running animation.
5748 boolean mAnimating;
5749 boolean mLocalAnimating;
5750 Animation mAnimation;
5751 boolean mAnimationIsEntrance;
5752 boolean mHasTransformation;
5753 boolean mHasLocalTransformation;
5754 final Transformation mTransformation = new Transformation();
5755
5756 // This is set after IWindowSession.relayout() has been called at
5757 // least once for the window. It allows us to detect the situation
5758 // where we don't yet have a surface, but should have one soon, so
5759 // we can give the window focus before waiting for the relayout.
5760 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07005761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 // This is set after the Surface has been created but before the
5763 // window has been drawn. During this time the surface is hidden.
5764 boolean mDrawPending;
5765
5766 // This is set after the window has finished drawing for the first
5767 // time but before its surface is shown. The surface will be
5768 // displayed when the next layout is run.
5769 boolean mCommitDrawPending;
5770
5771 // This is set during the time after the window's drawing has been
5772 // committed, and before its surface is actually shown. It is used
5773 // to delay showing the surface until all windows in a token are ready
5774 // to be shown.
5775 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07005776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 // Set when the window has been shown in the screen the first time.
5778 boolean mHasDrawn;
5779
5780 // Currently running an exit animation?
5781 boolean mExiting;
5782
5783 // Currently on the mDestroySurface list?
5784 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07005785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 // Completely remove from window manager after exit animation?
5787 boolean mRemoveOnExit;
5788
5789 // Set when the orientation is changing and this window has not yet
5790 // been updated for the new orientation.
5791 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07005792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 // Is this window now (or just being) removed?
5794 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07005795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796 WindowState(Session s, IWindow c, WindowToken token,
5797 WindowState attachedWindow, WindowManager.LayoutParams a,
5798 int viewVisibility) {
5799 mSession = s;
5800 mClient = c;
5801 mToken = token;
5802 mAttrs.copyFrom(a);
5803 mViewVisibility = viewVisibility;
5804 DeathRecipient deathRecipient = new DeathRecipient();
5805 mAlpha = a.alpha;
5806 if (localLOGV) Log.v(
5807 TAG, "Window " + this + " client=" + c.asBinder()
5808 + " token=" + token + " (" + mAttrs.token + ")");
5809 try {
5810 c.asBinder().linkToDeath(deathRecipient, 0);
5811 } catch (RemoteException e) {
5812 mDeathRecipient = null;
5813 mAttachedWindow = null;
5814 mLayoutAttached = false;
5815 mIsImWindow = false;
5816 mBaseLayer = 0;
5817 mSubLayer = 0;
5818 return;
5819 }
5820 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07005821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
5823 mAttrs.type <= LAST_SUB_WINDOW)) {
5824 // The multiplier here is to reserve space for multiple
5825 // windows in the same type layer.
5826 mBaseLayer = mPolicy.windowTypeToLayerLw(
5827 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
5828 + TYPE_LAYER_OFFSET;
5829 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
5830 mAttachedWindow = attachedWindow;
5831 mAttachedWindow.mChildWindows.add(this);
5832 mLayoutAttached = mAttrs.type !=
5833 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
5834 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
5835 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5836 } else {
5837 // The multiplier here is to reserve space for multiple
5838 // windows in the same type layer.
5839 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
5840 * TYPE_LAYER_MULTIPLIER
5841 + TYPE_LAYER_OFFSET;
5842 mSubLayer = 0;
5843 mAttachedWindow = null;
5844 mLayoutAttached = false;
5845 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
5846 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
5847 }
5848
5849 WindowState appWin = this;
5850 while (appWin.mAttachedWindow != null) {
5851 appWin = mAttachedWindow;
5852 }
5853 WindowToken appToken = appWin.mToken;
5854 while (appToken.appWindowToken == null) {
5855 WindowToken parent = mTokenMap.get(appToken.token);
5856 if (parent == null || appToken == parent) {
5857 break;
5858 }
5859 appToken = parent;
5860 }
The Android Open Source Project10592532009-03-18 17:39:46 -07005861 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 mAppToken = appToken.appWindowToken;
5863
5864 mSurface = null;
5865 mRequestedWidth = 0;
5866 mRequestedHeight = 0;
5867 mLastRequestedWidth = 0;
5868 mLastRequestedHeight = 0;
5869 mReqXPos = 0;
5870 mReqYPos = 0;
5871 mLayer = 0;
5872 mAnimLayer = 0;
5873 mLastLayer = 0;
5874 }
5875
5876 void attach() {
5877 if (localLOGV) Log.v(
5878 TAG, "Attaching " + this + " token=" + mToken
5879 + ", list=" + mToken.windows);
5880 mSession.windowAddedLocked();
5881 }
5882
5883 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
5884 mHaveFrame = true;
5885
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005886 final Rect container = mContainingFrame;
5887 container.set(pf);
5888
5889 final Rect display = mDisplayFrame;
5890 display.set(df);
5891
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07005892 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005893 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07005894 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
5895 display.intersect(mCompatibleScreenFrame);
5896 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005897 }
5898
5899 final int pw = container.right - container.left;
5900 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005901
5902 int w,h;
5903 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
5904 w = mAttrs.width < 0 ? pw : mAttrs.width;
5905 h = mAttrs.height< 0 ? ph : mAttrs.height;
5906 } else {
5907 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
5908 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
5909 }
Romain Guy06882f82009-06-10 13:36:04 -07005910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 final Rect content = mContentFrame;
5912 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07005913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005914 final Rect visible = mVisibleFrame;
5915 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07005916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 final Rect frame = mFrame;
Romain Guy06882f82009-06-10 13:36:04 -07005918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 //System.out.println("In: w=" + w + " h=" + h + " container=" +
5920 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
5921
5922 Gravity.apply(mAttrs.gravity, w, h, container,
5923 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
5924 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
5925
5926 //System.out.println("Out: " + mFrame);
5927
5928 // Now make sure the window fits in the overall display.
5929 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07005930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 // Make sure the content and visible frames are inside of the
5932 // final window frame.
5933 if (content.left < frame.left) content.left = frame.left;
5934 if (content.top < frame.top) content.top = frame.top;
5935 if (content.right > frame.right) content.right = frame.right;
5936 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
5937 if (visible.left < frame.left) visible.left = frame.left;
5938 if (visible.top < frame.top) visible.top = frame.top;
5939 if (visible.right > frame.right) visible.right = frame.right;
5940 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 final Rect contentInsets = mContentInsets;
5943 contentInsets.left = content.left-frame.left;
5944 contentInsets.top = content.top-frame.top;
5945 contentInsets.right = frame.right-content.right;
5946 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005948 final Rect visibleInsets = mVisibleInsets;
5949 visibleInsets.left = visible.left-frame.left;
5950 visibleInsets.top = visible.top-frame.top;
5951 visibleInsets.right = frame.right-visible.right;
5952 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07005953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 if (localLOGV) {
5955 //if ("com.google.android.youtube".equals(mAttrs.packageName)
5956 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
5957 Log.v(TAG, "Resolving (mRequestedWidth="
5958 + mRequestedWidth + ", mRequestedheight="
5959 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
5960 + "): frame=" + mFrame.toShortString()
5961 + " ci=" + contentInsets.toShortString()
5962 + " vi=" + visibleInsets.toShortString());
5963 //}
5964 }
5965 }
Romain Guy06882f82009-06-10 13:36:04 -07005966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 public Rect getFrameLw() {
5968 return mFrame;
5969 }
5970
5971 public Rect getShownFrameLw() {
5972 return mShownFrame;
5973 }
5974
5975 public Rect getDisplayFrameLw() {
5976 return mDisplayFrame;
5977 }
5978
5979 public Rect getContentFrameLw() {
5980 return mContentFrame;
5981 }
5982
5983 public Rect getVisibleFrameLw() {
5984 return mVisibleFrame;
5985 }
5986
5987 public boolean getGivenInsetsPendingLw() {
5988 return mGivenInsetsPending;
5989 }
5990
5991 public Rect getGivenContentInsetsLw() {
5992 return mGivenContentInsets;
5993 }
Romain Guy06882f82009-06-10 13:36:04 -07005994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 public Rect getGivenVisibleInsetsLw() {
5996 return mGivenVisibleInsets;
5997 }
Romain Guy06882f82009-06-10 13:36:04 -07005998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005999 public WindowManager.LayoutParams getAttrs() {
6000 return mAttrs;
6001 }
6002
6003 public int getSurfaceLayer() {
6004 return mLayer;
6005 }
Romain Guy06882f82009-06-10 13:36:04 -07006006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006007 public IApplicationToken getAppToken() {
6008 return mAppToken != null ? mAppToken.appToken : null;
6009 }
6010
6011 public boolean hasAppShownWindows() {
6012 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6013 }
6014
6015 public boolean hasAppStartingIcon() {
6016 return mAppToken != null ? (mAppToken.startingData != null) : false;
6017 }
6018
6019 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6020 return mAppToken != null ? mAppToken.startingWindow : null;
6021 }
6022
6023 public void setAnimation(Animation anim) {
6024 if (localLOGV) Log.v(
6025 TAG, "Setting animation in " + this + ": " + anim);
6026 mAnimating = false;
6027 mLocalAnimating = false;
6028 mAnimation = anim;
6029 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6030 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6031 }
6032
6033 public void clearAnimation() {
6034 if (mAnimation != null) {
6035 mAnimating = true;
6036 mLocalAnimating = false;
6037 mAnimation = null;
6038 }
6039 }
Romain Guy06882f82009-06-10 13:36:04 -07006040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 Surface createSurfaceLocked() {
6042 if (mSurface == null) {
6043 mDrawPending = true;
6044 mCommitDrawPending = false;
6045 mReadyToShow = false;
6046 if (mAppToken != null) {
6047 mAppToken.allDrawn = false;
6048 }
6049
6050 int flags = 0;
6051 if (mAttrs.memoryType == MEMORY_TYPE_HARDWARE) {
6052 flags |= Surface.HARDWARE;
6053 } else if (mAttrs.memoryType == MEMORY_TYPE_GPU) {
6054 flags |= Surface.GPU;
6055 } else if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
6056 flags |= Surface.PUSH_BUFFERS;
6057 }
6058
6059 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6060 flags |= Surface.SECURE;
6061 }
6062 if (DEBUG_VISIBILITY) Log.v(
6063 TAG, "Creating surface in session "
6064 + mSession.mSurfaceSession + " window " + this
6065 + " w=" + mFrame.width()
6066 + " h=" + mFrame.height() + " format="
6067 + mAttrs.format + " flags=" + flags);
6068
6069 int w = mFrame.width();
6070 int h = mFrame.height();
6071 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6072 // for a scaled surface, we always want the requested
6073 // size.
6074 w = mRequestedWidth;
6075 h = mRequestedHeight;
6076 }
6077
6078 try {
6079 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006080 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006081 0, w, h, mAttrs.format, flags);
6082 } catch (Surface.OutOfResourcesException e) {
6083 Log.w(TAG, "OutOfResourcesException creating surface");
6084 reclaimSomeSurfaceMemoryLocked(this, "create");
6085 return null;
6086 } catch (Exception e) {
6087 Log.e(TAG, "Exception creating surface", e);
6088 return null;
6089 }
Romain Guy06882f82009-06-10 13:36:04 -07006090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 if (localLOGV) Log.v(
6092 TAG, "Got surface: " + mSurface
6093 + ", set left=" + mFrame.left + " top=" + mFrame.top
6094 + ", animLayer=" + mAnimLayer);
6095 if (SHOW_TRANSACTIONS) {
6096 Log.i(TAG, ">>> OPEN TRANSACTION");
6097 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6098 + mAttrs.getTitle() + ") pos=(" +
6099 mFrame.left + "," + mFrame.top + ") (" +
6100 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6101 mAnimLayer + " HIDE");
6102 }
6103 Surface.openTransaction();
6104 try {
6105 try {
6106 mSurface.setPosition(mFrame.left, mFrame.top);
6107 mSurface.setLayer(mAnimLayer);
6108 mSurface.hide();
6109 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6110 mSurface.setFlags(Surface.SURFACE_DITHER,
6111 Surface.SURFACE_DITHER);
6112 }
6113 } catch (RuntimeException e) {
6114 Log.w(TAG, "Error creating surface in " + w, e);
6115 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6116 }
6117 mLastHidden = true;
6118 } finally {
6119 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6120 Surface.closeTransaction();
6121 }
6122 if (localLOGV) Log.v(
6123 TAG, "Created surface " + this);
6124 }
6125 return mSurface;
6126 }
Romain Guy06882f82009-06-10 13:36:04 -07006127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006128 void destroySurfaceLocked() {
6129 // Window is no longer on-screen, so can no longer receive
6130 // key events... if we were waiting for it to finish
6131 // handling a key event, the wait is over!
6132 mKeyWaiter.finishedKey(mSession, mClient, true,
6133 KeyWaiter.RETURN_NOTHING);
6134 mKeyWaiter.releasePendingPointerLocked(mSession);
6135 mKeyWaiter.releasePendingTrackballLocked(mSession);
6136
6137 if (mAppToken != null && this == mAppToken.startingWindow) {
6138 mAppToken.startingDisplayed = false;
6139 }
Romain Guy06882f82009-06-10 13:36:04 -07006140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 if (localLOGV) Log.v(
6142 TAG, "Window " + this
6143 + " destroying surface " + mSurface + ", session " + mSession);
6144 if (mSurface != null) {
6145 try {
6146 if (SHOW_TRANSACTIONS) {
6147 RuntimeException ex = new RuntimeException();
6148 ex.fillInStackTrace();
6149 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6150 + mAttrs.getTitle() + ")", ex);
6151 }
6152 mSurface.clear();
6153 } catch (RuntimeException e) {
6154 Log.w(TAG, "Exception thrown when destroying Window " + this
6155 + " surface " + mSurface + " session " + mSession
6156 + ": " + e.toString());
6157 }
6158 mSurface = null;
6159 mDrawPending = false;
6160 mCommitDrawPending = false;
6161 mReadyToShow = false;
6162
6163 int i = mChildWindows.size();
6164 while (i > 0) {
6165 i--;
6166 WindowState c = (WindowState)mChildWindows.get(i);
6167 c.mAttachedHidden = true;
6168 }
6169 }
6170 }
6171
6172 boolean finishDrawingLocked() {
6173 if (mDrawPending) {
6174 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6175 TAG, "finishDrawingLocked: " + mSurface);
6176 mCommitDrawPending = true;
6177 mDrawPending = false;
6178 return true;
6179 }
6180 return false;
6181 }
6182
6183 // This must be called while inside a transaction.
6184 void commitFinishDrawingLocked(long currentTime) {
6185 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6186 if (!mCommitDrawPending) {
6187 return;
6188 }
6189 mCommitDrawPending = false;
6190 mReadyToShow = true;
6191 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6192 final AppWindowToken atoken = mAppToken;
6193 if (atoken == null || atoken.allDrawn || starting) {
6194 performShowLocked();
6195 }
6196 }
6197
6198 // This must be called while inside a transaction.
6199 boolean performShowLocked() {
6200 if (DEBUG_VISIBILITY) {
6201 RuntimeException e = new RuntimeException();
6202 e.fillInStackTrace();
6203 Log.v(TAG, "performShow on " + this
6204 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6205 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6206 }
6207 if (mReadyToShow && isReadyForDisplay()) {
6208 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6209 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6210 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6211 + " during animation: policyVis=" + mPolicyVisibility
6212 + " attHidden=" + mAttachedHidden
6213 + " tok.hiddenRequested="
6214 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6215 + " tok.idden="
6216 + (mAppToken != null ? mAppToken.hidden : false)
6217 + " animating=" + mAnimating
6218 + " tok animating="
6219 + (mAppToken != null ? mAppToken.animating : false));
6220 if (!showSurfaceRobustlyLocked(this)) {
6221 return false;
6222 }
6223 mLastAlpha = -1;
6224 mHasDrawn = true;
6225 mLastHidden = false;
6226 mReadyToShow = false;
6227 enableScreenIfNeededLocked();
6228
6229 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 int i = mChildWindows.size();
6232 while (i > 0) {
6233 i--;
6234 WindowState c = (WindowState)mChildWindows.get(i);
6235 if (c.mSurface != null && c.mAttachedHidden) {
6236 c.mAttachedHidden = false;
6237 c.performShowLocked();
6238 }
6239 }
Romain Guy06882f82009-06-10 13:36:04 -07006240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006241 if (mAttrs.type != TYPE_APPLICATION_STARTING
6242 && mAppToken != null) {
6243 mAppToken.firstWindowDrawn = true;
6244 if (mAnimation == null && mAppToken.startingData != null) {
6245 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6246 + mToken
6247 + ": first real window is shown, no animation");
6248 mFinishedStarting.add(mAppToken);
6249 mH.sendEmptyMessage(H.FINISHED_STARTING);
6250 }
6251 mAppToken.updateReportedVisibilityLocked();
6252 }
6253 }
6254 return true;
6255 }
Romain Guy06882f82009-06-10 13:36:04 -07006256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 // This must be called while inside a transaction. Returns true if
6258 // there is more animation to run.
6259 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6260 if (!mDisplayFrozen) {
6261 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006263 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6264 mHasTransformation = true;
6265 mHasLocalTransformation = true;
6266 if (!mLocalAnimating) {
6267 if (DEBUG_ANIM) Log.v(
6268 TAG, "Starting animation in " + this +
6269 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6270 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6271 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6272 mAnimation.setStartTime(currentTime);
6273 mLocalAnimating = true;
6274 mAnimating = true;
6275 }
6276 mTransformation.clear();
6277 final boolean more = mAnimation.getTransformation(
6278 currentTime, mTransformation);
6279 if (DEBUG_ANIM) Log.v(
6280 TAG, "Stepped animation in " + this +
6281 ": more=" + more + ", xform=" + mTransformation);
6282 if (more) {
6283 // we're not done!
6284 return true;
6285 }
6286 if (DEBUG_ANIM) Log.v(
6287 TAG, "Finished animation in " + this +
6288 " @ " + currentTime);
6289 mAnimation = null;
6290 //WindowManagerService.this.dump();
6291 }
6292 mHasLocalTransformation = false;
6293 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6294 && mAppToken.hasTransformation) {
6295 // When our app token is animating, we kind-of pretend like
6296 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6297 // part of this check means that we will only do this if
6298 // our window is not currently exiting, or it is not
6299 // locally animating itself. The idea being that one that
6300 // is exiting and doing a local animation should be removed
6301 // once that animation is done.
6302 mAnimating = true;
6303 mHasTransformation = true;
6304 mTransformation.clear();
6305 return false;
6306 } else if (mHasTransformation) {
6307 // Little trick to get through the path below to act like
6308 // we have finished an animation.
6309 mAnimating = true;
6310 } else if (isAnimating()) {
6311 mAnimating = true;
6312 }
6313 } else if (mAnimation != null) {
6314 // If the display is frozen, and there is a pending animation,
6315 // clear it and make sure we run the cleanup code.
6316 mAnimating = true;
6317 mLocalAnimating = true;
6318 mAnimation = null;
6319 }
Romain Guy06882f82009-06-10 13:36:04 -07006320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006321 if (!mAnimating && !mLocalAnimating) {
6322 return false;
6323 }
6324
6325 if (DEBUG_ANIM) Log.v(
6326 TAG, "Animation done in " + this + ": exiting=" + mExiting
6327 + ", reportedVisible="
6328 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006330 mAnimating = false;
6331 mLocalAnimating = false;
6332 mAnimation = null;
6333 mAnimLayer = mLayer;
6334 if (mIsImWindow) {
6335 mAnimLayer += mInputMethodAnimLayerAdjustment;
6336 }
6337 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6338 + " anim layer: " + mAnimLayer);
6339 mHasTransformation = false;
6340 mHasLocalTransformation = false;
6341 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6342 mTransformation.clear();
6343 if (mHasDrawn
6344 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6345 && mAppToken != null
6346 && mAppToken.firstWindowDrawn
6347 && mAppToken.startingData != null) {
6348 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6349 + mToken + ": first real window done animating");
6350 mFinishedStarting.add(mAppToken);
6351 mH.sendEmptyMessage(H.FINISHED_STARTING);
6352 }
Romain Guy06882f82009-06-10 13:36:04 -07006353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006354 finishExit();
6355
6356 if (mAppToken != null) {
6357 mAppToken.updateReportedVisibilityLocked();
6358 }
6359
6360 return false;
6361 }
6362
6363 void finishExit() {
6364 if (DEBUG_ANIM) Log.v(
6365 TAG, "finishExit in " + this
6366 + ": exiting=" + mExiting
6367 + " remove=" + mRemoveOnExit
6368 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006370 final int N = mChildWindows.size();
6371 for (int i=0; i<N; i++) {
6372 ((WindowState)mChildWindows.get(i)).finishExit();
6373 }
Romain Guy06882f82009-06-10 13:36:04 -07006374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006375 if (!mExiting) {
6376 return;
6377 }
Romain Guy06882f82009-06-10 13:36:04 -07006378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006379 if (isWindowAnimating()) {
6380 return;
6381 }
6382
6383 if (localLOGV) Log.v(
6384 TAG, "Exit animation finished in " + this
6385 + ": remove=" + mRemoveOnExit);
6386 if (mSurface != null) {
6387 mDestroySurface.add(this);
6388 mDestroying = true;
6389 if (SHOW_TRANSACTIONS) Log.i(
6390 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6391 try {
6392 mSurface.hide();
6393 } catch (RuntimeException e) {
6394 Log.w(TAG, "Error hiding surface in " + this, e);
6395 }
6396 mLastHidden = true;
6397 mKeyWaiter.releasePendingPointerLocked(mSession);
6398 }
6399 mExiting = false;
6400 if (mRemoveOnExit) {
6401 mPendingRemove.add(this);
6402 mRemoveOnExit = false;
6403 }
6404 }
Romain Guy06882f82009-06-10 13:36:04 -07006405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6407 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6408 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6409 if (dtdx < -.000001f || dtdx > .000001f) return false;
6410 if (dsdy < -.000001f || dsdy > .000001f) return false;
6411 return true;
6412 }
Romain Guy06882f82009-06-10 13:36:04 -07006413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 void computeShownFrameLocked() {
6415 final boolean selfTransformation = mHasLocalTransformation;
6416 Transformation attachedTransformation =
6417 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6418 ? mAttachedWindow.mTransformation : null;
6419 Transformation appTransformation =
6420 (mAppToken != null && mAppToken.hasTransformation)
6421 ? mAppToken.transformation : null;
6422 if (selfTransformation || attachedTransformation != null
6423 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006424 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 final Rect frame = mFrame;
6426 final float tmpFloats[] = mTmpFloats;
6427 final Matrix tmpMatrix = mTmpMatrix;
6428
6429 // Compute the desired transformation.
6430 tmpMatrix.setTranslate(frame.left, frame.top);
6431 if (selfTransformation) {
6432 tmpMatrix.preConcat(mTransformation.getMatrix());
6433 }
6434 if (attachedTransformation != null) {
6435 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6436 }
6437 if (appTransformation != null) {
6438 tmpMatrix.preConcat(appTransformation.getMatrix());
6439 }
6440
6441 // "convert" it into SurfaceFlinger's format
6442 // (a 2x2 matrix + an offset)
6443 // Here we must not transform the position of the surface
6444 // since it is already included in the transformation.
6445 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07006446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006447 tmpMatrix.getValues(tmpFloats);
6448 mDsDx = tmpFloats[Matrix.MSCALE_X];
6449 mDtDx = tmpFloats[Matrix.MSKEW_X];
6450 mDsDy = tmpFloats[Matrix.MSKEW_Y];
6451 mDtDy = tmpFloats[Matrix.MSCALE_Y];
6452 int x = (int)tmpFloats[Matrix.MTRANS_X];
6453 int y = (int)tmpFloats[Matrix.MTRANS_Y];
6454 int w = frame.width();
6455 int h = frame.height();
6456 mShownFrame.set(x, y, x+w, y+h);
6457
6458 // Now set the alpha... but because our current hardware
6459 // can't do alpha transformation on a non-opaque surface,
6460 // turn it off if we are running an animation that is also
6461 // transforming since it is more important to have that
6462 // animation be smooth.
6463 mShownAlpha = mAlpha;
6464 if (!mLimitedAlphaCompositing
6465 || (!PixelFormat.formatHasAlpha(mAttrs.format)
6466 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
6467 && x == frame.left && y == frame.top))) {
6468 //Log.i(TAG, "Applying alpha transform");
6469 if (selfTransformation) {
6470 mShownAlpha *= mTransformation.getAlpha();
6471 }
6472 if (attachedTransformation != null) {
6473 mShownAlpha *= attachedTransformation.getAlpha();
6474 }
6475 if (appTransformation != null) {
6476 mShownAlpha *= appTransformation.getAlpha();
6477 }
6478 } else {
6479 //Log.i(TAG, "Not applying alpha transform");
6480 }
Romain Guy06882f82009-06-10 13:36:04 -07006481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482 if (localLOGV) Log.v(
6483 TAG, "Continuing animation in " + this +
6484 ": " + mShownFrame +
6485 ", alpha=" + mTransformation.getAlpha());
6486 return;
6487 }
Romain Guy06882f82009-06-10 13:36:04 -07006488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 mShownFrame.set(mFrame);
6490 mShownAlpha = mAlpha;
6491 mDsDx = 1;
6492 mDtDx = 0;
6493 mDsDy = 0;
6494 mDtDy = 1;
6495 }
Romain Guy06882f82009-06-10 13:36:04 -07006496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006497 /**
6498 * Is this window visible? It is not visible if there is no
6499 * surface, or we are in the process of running an exit animation
6500 * that will remove the surface, or its app token has been hidden.
6501 */
6502 public boolean isVisibleLw() {
6503 final AppWindowToken atoken = mAppToken;
6504 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6505 && (atoken == null || !atoken.hiddenRequested)
6506 && !mExiting && !mDestroying;
6507 }
6508
6509 /**
6510 * Is this window visible, ignoring its app token? It is not visible
6511 * if there is no surface, or we are in the process of running an exit animation
6512 * that will remove the surface.
6513 */
6514 public boolean isWinVisibleLw() {
6515 final AppWindowToken atoken = mAppToken;
6516 return mSurface != null && mPolicyVisibility && !mAttachedHidden
6517 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
6518 && !mExiting && !mDestroying;
6519 }
6520
6521 /**
6522 * The same as isVisible(), but follows the current hidden state of
6523 * the associated app token, not the pending requested hidden state.
6524 */
6525 boolean isVisibleNow() {
6526 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07006527 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006528 }
6529
6530 /**
6531 * Same as isVisible(), but we also count it as visible between the
6532 * call to IWindowSession.add() and the first relayout().
6533 */
6534 boolean isVisibleOrAdding() {
6535 final AppWindowToken atoken = mAppToken;
6536 return (mSurface != null
6537 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
6538 && mPolicyVisibility && !mAttachedHidden
6539 && (atoken == null || !atoken.hiddenRequested)
6540 && !mExiting && !mDestroying;
6541 }
6542
6543 /**
6544 * Is this window currently on-screen? It is on-screen either if it
6545 * is visible or it is currently running an animation before no longer
6546 * being visible.
6547 */
6548 boolean isOnScreen() {
6549 final AppWindowToken atoken = mAppToken;
6550 if (atoken != null) {
6551 return mSurface != null && mPolicyVisibility && !mDestroying
6552 && ((!mAttachedHidden && !atoken.hiddenRequested)
6553 || mAnimating || atoken.animating);
6554 } else {
6555 return mSurface != null && mPolicyVisibility && !mDestroying
6556 && (!mAttachedHidden || mAnimating);
6557 }
6558 }
Romain Guy06882f82009-06-10 13:36:04 -07006559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 /**
6561 * Like isOnScreen(), but we don't return true if the window is part
6562 * of a transition that has not yet been started.
6563 */
6564 boolean isReadyForDisplay() {
6565 final AppWindowToken atoken = mAppToken;
6566 final boolean animating = atoken != null ? atoken.animating : false;
6567 return mSurface != null && mPolicyVisibility && !mDestroying
The Android Open Source Project10592532009-03-18 17:39:46 -07006568 && ((!mAttachedHidden && !mRootToken.hidden)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006569 || mAnimating || animating);
6570 }
6571
6572 /** Is the window or its container currently animating? */
6573 boolean isAnimating() {
6574 final WindowState attached = mAttachedWindow;
6575 final AppWindowToken atoken = mAppToken;
6576 return mAnimation != null
6577 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07006578 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 (atoken.animation != null
6580 || atoken.inPendingTransaction));
6581 }
6582
6583 /** Is this window currently animating? */
6584 boolean isWindowAnimating() {
6585 return mAnimation != null;
6586 }
6587
6588 /**
6589 * Like isOnScreen, but returns false if the surface hasn't yet
6590 * been drawn.
6591 */
6592 public boolean isDisplayedLw() {
6593 final AppWindowToken atoken = mAppToken;
6594 return mSurface != null && mPolicyVisibility && !mDestroying
6595 && !mDrawPending && !mCommitDrawPending
6596 && ((!mAttachedHidden &&
6597 (atoken == null || !atoken.hiddenRequested))
6598 || mAnimating);
6599 }
6600
6601 public boolean fillsScreenLw(int screenWidth, int screenHeight,
6602 boolean shownFrame, boolean onlyOpaque) {
6603 if (mSurface == null) {
6604 return false;
6605 }
6606 if (mAppToken != null && !mAppToken.appFullscreen) {
6607 return false;
6608 }
6609 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
6610 return false;
6611 }
6612 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006613
6614 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
6615 return frame.left <= mCompatibleScreenFrame.left &&
6616 frame.top <= mCompatibleScreenFrame.top &&
6617 frame.right >= mCompatibleScreenFrame.right &&
6618 frame.bottom >= mCompatibleScreenFrame.bottom;
6619 } else {
6620 return frame.left <= 0 && frame.top <= 0
6621 && frame.right >= screenWidth
6622 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 }
Romain Guy06882f82009-06-10 13:36:04 -07006625
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006626 /**
6627 * Return true if the window is opaque and fully drawn.
6628 */
6629 boolean isOpaqueDrawn() {
6630 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
6631 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
6632 }
6633
6634 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
6635 return
6636 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006637 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
6638 // only if it's visible
6639 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006640 // and only if the application fills the compatible screen
6641 mFrame.left <= mCompatibleScreenFrame.left &&
6642 mFrame.top <= mCompatibleScreenFrame.top &&
6643 mFrame.right >= mCompatibleScreenFrame.right &&
6644 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006645 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006646 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006647 }
6648
6649 boolean isFullscreen(int screenWidth, int screenHeight) {
6650 return mFrame.left <= 0 && mFrame.top <= 0 &&
6651 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 }
6653
6654 void removeLocked() {
6655 if (mAttachedWindow != null) {
6656 mAttachedWindow.mChildWindows.remove(this);
6657 }
6658 destroySurfaceLocked();
6659 mSession.windowRemovedLocked();
6660 try {
6661 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
6662 } catch (RuntimeException e) {
6663 // Ignore if it has already been removed (usually because
6664 // we are doing this as part of processing a death note.)
6665 }
6666 }
6667
6668 private class DeathRecipient implements IBinder.DeathRecipient {
6669 public void binderDied() {
6670 try {
6671 synchronized(mWindowMap) {
6672 WindowState win = windowForClientLocked(mSession, mClient);
6673 Log.i(TAG, "WIN DEATH: " + win);
6674 if (win != null) {
6675 removeWindowLocked(mSession, win);
6676 }
6677 }
6678 } catch (IllegalArgumentException ex) {
6679 // This will happen if the window has already been
6680 // removed.
6681 }
6682 }
6683 }
6684
6685 /** Returns true if this window desires key events. */
6686 public final boolean canReceiveKeys() {
6687 return isVisibleOrAdding()
6688 && (mViewVisibility == View.VISIBLE)
6689 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
6690 }
6691
6692 public boolean hasDrawnLw() {
6693 return mHasDrawn;
6694 }
6695
6696 public boolean showLw(boolean doAnimation) {
6697 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
6698 mPolicyVisibility = true;
6699 mPolicyVisibilityAfterAnim = true;
6700 if (doAnimation) {
6701 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
6702 }
6703 requestAnimationLocked(0);
6704 return true;
6705 }
6706 return false;
6707 }
6708
6709 public boolean hideLw(boolean doAnimation) {
6710 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
6711 : mPolicyVisibility;
6712 if (current) {
6713 if (doAnimation) {
6714 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
6715 if (mAnimation == null) {
6716 doAnimation = false;
6717 }
6718 }
6719 if (doAnimation) {
6720 mPolicyVisibilityAfterAnim = false;
6721 } else {
6722 mPolicyVisibilityAfterAnim = false;
6723 mPolicyVisibility = false;
6724 }
6725 requestAnimationLocked(0);
6726 return true;
6727 }
6728 return false;
6729 }
6730
6731 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006732 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07006733
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006734 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
6735 pw.print(" mClient="); pw.println(mClient.asBinder());
6736 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
6737 if (mAttachedWindow != null || mLayoutAttached) {
6738 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
6739 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
6740 }
6741 if (mIsImWindow) {
6742 pw.print(prefix); pw.print("mIsImWindow="); pw.println(mIsImWindow);
6743 }
6744 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
6745 pw.print(" mSubLayer="); pw.print(mSubLayer);
6746 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
6747 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
6748 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
6749 pw.print("="); pw.print(mAnimLayer);
6750 pw.print(" mLastLayer="); pw.println(mLastLayer);
6751 if (mSurface != null) {
6752 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
6753 }
6754 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
6755 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
6756 if (mAppToken != null) {
6757 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
6758 }
6759 if (mTargetAppToken != null) {
6760 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
6761 }
6762 pw.print(prefix); pw.print("mViewVisibility=0x");
6763 pw.print(Integer.toHexString(mViewVisibility));
6764 pw.print(" mLastHidden="); pw.print(mLastHidden);
6765 pw.print(" mHaveFrame="); pw.println(mHaveFrame);
6766 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
6767 pw.print(prefix); pw.print("mPolicyVisibility=");
6768 pw.print(mPolicyVisibility);
6769 pw.print(" mPolicyVisibilityAfterAnim=");
6770 pw.print(mPolicyVisibilityAfterAnim);
6771 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
6772 }
6773 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
6774 pw.print(" h="); pw.print(mRequestedHeight);
6775 pw.print(" x="); pw.print(mReqXPos);
6776 pw.print(" y="); pw.println(mReqYPos);
6777 pw.print(prefix); pw.print("mGivenContentInsets=");
6778 mGivenContentInsets.printShortString(pw);
6779 pw.print(" mGivenVisibleInsets=");
6780 mGivenVisibleInsets.printShortString(pw);
6781 pw.println();
6782 if (mTouchableInsets != 0 || mGivenInsetsPending) {
6783 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
6784 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
6785 }
6786 pw.print(prefix); pw.print("mShownFrame=");
6787 mShownFrame.printShortString(pw);
6788 pw.print(" last="); mLastShownFrame.printShortString(pw);
6789 pw.println();
6790 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
6791 pw.print(" last="); mLastFrame.printShortString(pw);
6792 pw.println();
6793 pw.print(prefix); pw.print("mContainingFrame=");
6794 mContainingFrame.printShortString(pw);
6795 pw.print(" mDisplayFrame=");
6796 mDisplayFrame.printShortString(pw);
6797 pw.println();
6798 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
6799 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
6800 pw.println();
6801 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
6802 pw.print(" last="); mLastContentInsets.printShortString(pw);
6803 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
6804 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
6805 pw.println();
6806 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
6807 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
6808 pw.print(" mAlpha="); pw.print(mAlpha);
6809 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
6810 }
6811 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
6812 || mAnimation != null) {
6813 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
6814 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
6815 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
6816 pw.print(" mAnimation="); pw.println(mAnimation);
6817 }
6818 if (mHasTransformation || mHasLocalTransformation) {
6819 pw.print(prefix); pw.print("XForm: has=");
6820 pw.print(mHasTransformation);
6821 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
6822 pw.print(" "); mTransformation.printShortString(pw);
6823 pw.println();
6824 }
6825 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
6826 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
6827 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
6828 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
6829 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
6830 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
6831 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
6832 pw.print(" mDestroying="); pw.print(mDestroying);
6833 pw.print(" mRemoved="); pw.println(mRemoved);
6834 }
6835 if (mOrientationChanging || mAppFreezing) {
6836 pw.print(prefix); pw.print("mOrientationChanging=");
6837 pw.print(mOrientationChanging);
6838 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
6839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006840 }
6841
6842 @Override
6843 public String toString() {
6844 return "Window{"
6845 + Integer.toHexString(System.identityHashCode(this))
6846 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
6847 }
6848 }
Romain Guy06882f82009-06-10 13:36:04 -07006849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006850 // -------------------------------------------------------------
6851 // Window Token State
6852 // -------------------------------------------------------------
6853
6854 class WindowToken {
6855 // The actual token.
6856 final IBinder token;
6857
6858 // The type of window this token is for, as per WindowManager.LayoutParams.
6859 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07006860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 // Set if this token was explicitly added by a client, so should
6862 // not be removed when all windows are removed.
6863 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07006864
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006865 // For printing.
6866 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07006867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006868 // If this is an AppWindowToken, this is non-null.
6869 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07006870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 // All of the windows associated with this token.
6872 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
6873
6874 // Is key dispatching paused for this token?
6875 boolean paused = false;
6876
6877 // Should this token's windows be hidden?
6878 boolean hidden;
6879
6880 // Temporary for finding which tokens no longer have visible windows.
6881 boolean hasVisible;
6882
6883 WindowToken(IBinder _token, int type, boolean _explicit) {
6884 token = _token;
6885 windowType = type;
6886 explicit = _explicit;
6887 }
6888
6889 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006890 pw.print(prefix); pw.print("token="); pw.println(token);
6891 pw.print(prefix); pw.print("windows="); pw.println(windows);
6892 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
6893 pw.print(" hidden="); pw.print(hidden);
6894 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895 }
6896
6897 @Override
6898 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006899 if (stringName == null) {
6900 StringBuilder sb = new StringBuilder();
6901 sb.append("WindowToken{");
6902 sb.append(Integer.toHexString(System.identityHashCode(this)));
6903 sb.append(" token="); sb.append(token); sb.append('}');
6904 stringName = sb.toString();
6905 }
6906 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 }
6908 };
6909
6910 class AppWindowToken extends WindowToken {
6911 // Non-null only for application tokens.
6912 final IApplicationToken appToken;
6913
6914 // All of the windows and child windows that are included in this
6915 // application token. Note this list is NOT sorted!
6916 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
6917
6918 int groupId = -1;
6919 boolean appFullscreen;
6920 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07006921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 // These are used for determining when all windows associated with
6923 // an activity have been drawn, so they can be made visible together
6924 // at the same time.
6925 int lastTransactionSequence = mTransactionSequence-1;
6926 int numInterestingWindows;
6927 int numDrawnWindows;
6928 boolean inPendingTransaction;
6929 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07006930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006931 // Is this token going to be hidden in a little while? If so, it
6932 // won't be taken into account for setting the screen orientation.
6933 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 // Is this window's surface needed? This is almost like hidden, except
6936 // it will sometimes be true a little earlier: when the token has
6937 // been shown, but is still waiting for its app transition to execute
6938 // before making its windows shown.
6939 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07006940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006941 // Have we told the window clients to hide themselves?
6942 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07006943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006944 // Last visibility state we reported to the app token.
6945 boolean reportedVisible;
6946
6947 // Set to true when the token has been removed from the window mgr.
6948 boolean removed;
6949
6950 // Have we been asked to have this token keep the screen frozen?
6951 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 boolean animating;
6954 Animation animation;
6955 boolean hasTransformation;
6956 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07006957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 // Offset to the window of all layers in the token, for use by
6959 // AppWindowToken animations.
6960 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07006961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 // Information about an application starting window if displayed.
6963 StartingData startingData;
6964 WindowState startingWindow;
6965 View startingView;
6966 boolean startingDisplayed;
6967 boolean startingMoved;
6968 boolean firstWindowDrawn;
6969
6970 AppWindowToken(IApplicationToken _token) {
6971 super(_token.asBinder(),
6972 WindowManager.LayoutParams.TYPE_APPLICATION, true);
6973 appWindowToken = this;
6974 appToken = _token;
6975 }
Romain Guy06882f82009-06-10 13:36:04 -07006976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977 public void setAnimation(Animation anim) {
6978 if (localLOGV) Log.v(
6979 TAG, "Setting animation in " + this + ": " + anim);
6980 animation = anim;
6981 animating = false;
6982 anim.restrictDuration(MAX_ANIMATION_DURATION);
6983 anim.scaleCurrentDuration(mTransitionAnimationScale);
6984 int zorder = anim.getZAdjustment();
6985 int adj = 0;
6986 if (zorder == Animation.ZORDER_TOP) {
6987 adj = TYPE_LAYER_OFFSET;
6988 } else if (zorder == Animation.ZORDER_BOTTOM) {
6989 adj = -TYPE_LAYER_OFFSET;
6990 }
Romain Guy06882f82009-06-10 13:36:04 -07006991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006992 if (animLayerAdjustment != adj) {
6993 animLayerAdjustment = adj;
6994 updateLayers();
6995 }
6996 }
Romain Guy06882f82009-06-10 13:36:04 -07006997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006998 public void setDummyAnimation() {
6999 if (animation == null) {
7000 if (localLOGV) Log.v(
7001 TAG, "Setting dummy animation in " + this);
7002 animation = sDummyAnimation;
7003 }
7004 }
7005
7006 public void clearAnimation() {
7007 if (animation != null) {
7008 animation = null;
7009 animating = true;
7010 }
7011 }
Romain Guy06882f82009-06-10 13:36:04 -07007012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007013 void updateLayers() {
7014 final int N = allAppWindows.size();
7015 final int adj = animLayerAdjustment;
7016 for (int i=0; i<N; i++) {
7017 WindowState w = allAppWindows.get(i);
7018 w.mAnimLayer = w.mLayer + adj;
7019 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7020 + w.mAnimLayer);
7021 if (w == mInputMethodTarget) {
7022 setInputMethodAnimLayerAdjustment(adj);
7023 }
7024 }
7025 }
Romain Guy06882f82009-06-10 13:36:04 -07007026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007027 void sendAppVisibilityToClients() {
7028 final int N = allAppWindows.size();
7029 for (int i=0; i<N; i++) {
7030 WindowState win = allAppWindows.get(i);
7031 if (win == startingWindow && clientHidden) {
7032 // Don't hide the starting window.
7033 continue;
7034 }
7035 try {
7036 if (DEBUG_VISIBILITY) Log.v(TAG,
7037 "Setting visibility of " + win + ": " + (!clientHidden));
7038 win.mClient.dispatchAppVisibility(!clientHidden);
7039 } catch (RemoteException e) {
7040 }
7041 }
7042 }
Romain Guy06882f82009-06-10 13:36:04 -07007043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007044 void showAllWindowsLocked() {
7045 final int NW = allAppWindows.size();
7046 for (int i=0; i<NW; i++) {
7047 WindowState w = allAppWindows.get(i);
7048 if (DEBUG_VISIBILITY) Log.v(TAG,
7049 "performing show on: " + w);
7050 w.performShowLocked();
7051 }
7052 }
Romain Guy06882f82009-06-10 13:36:04 -07007053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 // This must be called while inside a transaction.
7055 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7056 if (!mDisplayFrozen) {
7057 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 if (animation == sDummyAnimation) {
7060 // This guy is going to animate, but not yet. For now count
7061 // it is not animating for purposes of scheduling transactions;
7062 // when it is really time to animate, this will be set to
7063 // a real animation and the next call will execute normally.
7064 return false;
7065 }
Romain Guy06882f82009-06-10 13:36:04 -07007066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7068 if (!animating) {
7069 if (DEBUG_ANIM) Log.v(
7070 TAG, "Starting animation in " + this +
7071 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7072 + " scale=" + mTransitionAnimationScale
7073 + " allDrawn=" + allDrawn + " animating=" + animating);
7074 animation.initialize(dw, dh, dw, dh);
7075 animation.setStartTime(currentTime);
7076 animating = true;
7077 }
7078 transformation.clear();
7079 final boolean more = animation.getTransformation(
7080 currentTime, transformation);
7081 if (DEBUG_ANIM) Log.v(
7082 TAG, "Stepped animation in " + this +
7083 ": more=" + more + ", xform=" + transformation);
7084 if (more) {
7085 // we're done!
7086 hasTransformation = true;
7087 return true;
7088 }
7089 if (DEBUG_ANIM) Log.v(
7090 TAG, "Finished animation in " + this +
7091 " @ " + currentTime);
7092 animation = null;
7093 }
7094 } else if (animation != null) {
7095 // If the display is frozen, and there is a pending animation,
7096 // clear it and make sure we run the cleanup code.
7097 animating = true;
7098 animation = null;
7099 }
7100
7101 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007103 if (!animating) {
7104 return false;
7105 }
7106
7107 clearAnimation();
7108 animating = false;
7109 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7110 moveInputMethodWindowsIfNeededLocked(true);
7111 }
Romain Guy06882f82009-06-10 13:36:04 -07007112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 if (DEBUG_ANIM) Log.v(
7114 TAG, "Animation done in " + this
7115 + ": reportedVisible=" + reportedVisible);
7116
7117 transformation.clear();
7118 if (animLayerAdjustment != 0) {
7119 animLayerAdjustment = 0;
7120 updateLayers();
7121 }
Romain Guy06882f82009-06-10 13:36:04 -07007122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 final int N = windows.size();
7124 for (int i=0; i<N; i++) {
7125 ((WindowState)windows.get(i)).finishExit();
7126 }
7127 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 return false;
7130 }
7131
7132 void updateReportedVisibilityLocked() {
7133 if (appToken == null) {
7134 return;
7135 }
Romain Guy06882f82009-06-10 13:36:04 -07007136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007137 int numInteresting = 0;
7138 int numVisible = 0;
7139 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007141 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7142 final int N = allAppWindows.size();
7143 for (int i=0; i<N; i++) {
7144 WindowState win = allAppWindows.get(i);
7145 if (win == startingWindow || win.mAppFreezing) {
7146 continue;
7147 }
7148 if (DEBUG_VISIBILITY) {
7149 Log.v(TAG, "Win " + win + ": isDisplayed="
7150 + win.isDisplayedLw()
7151 + ", isAnimating=" + win.isAnimating());
7152 if (!win.isDisplayedLw()) {
7153 Log.v(TAG, "Not displayed: s=" + win.mSurface
7154 + " pv=" + win.mPolicyVisibility
7155 + " dp=" + win.mDrawPending
7156 + " cdp=" + win.mCommitDrawPending
7157 + " ah=" + win.mAttachedHidden
7158 + " th="
7159 + (win.mAppToken != null
7160 ? win.mAppToken.hiddenRequested : false)
7161 + " a=" + win.mAnimating);
7162 }
7163 }
7164 numInteresting++;
7165 if (win.isDisplayedLw()) {
7166 if (!win.isAnimating()) {
7167 numVisible++;
7168 }
7169 nowGone = false;
7170 } else if (win.isAnimating()) {
7171 nowGone = false;
7172 }
7173 }
Romain Guy06882f82009-06-10 13:36:04 -07007174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007175 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7176 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7177 + numInteresting + " visible=" + numVisible);
7178 if (nowVisible != reportedVisible) {
7179 if (DEBUG_VISIBILITY) Log.v(
7180 TAG, "Visibility changed in " + this
7181 + ": vis=" + nowVisible);
7182 reportedVisible = nowVisible;
7183 Message m = mH.obtainMessage(
7184 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7185 nowVisible ? 1 : 0,
7186 nowGone ? 1 : 0,
7187 this);
7188 mH.sendMessage(m);
7189 }
7190 }
Romain Guy06882f82009-06-10 13:36:04 -07007191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007192 void dump(PrintWriter pw, String prefix) {
7193 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007194 if (appToken != null) {
7195 pw.print(prefix); pw.println("app=true");
7196 }
7197 if (allAppWindows.size() > 0) {
7198 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7199 }
7200 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7201 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7202 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7203 pw.print(" clientHidden="); pw.print(clientHidden);
7204 pw.print(" willBeHidden="); pw.print(willBeHidden);
7205 pw.print(" reportedVisible="); pw.println(reportedVisible);
7206 if (paused || freezingScreen) {
7207 pw.print(prefix); pw.print("paused="); pw.print(paused);
7208 pw.print(" freezingScreen="); pw.println(freezingScreen);
7209 }
7210 if (numInterestingWindows != 0 || numDrawnWindows != 0
7211 || inPendingTransaction || allDrawn) {
7212 pw.print(prefix); pw.print("numInterestingWindows=");
7213 pw.print(numInterestingWindows);
7214 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7215 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7216 pw.print(" allDrawn="); pw.println(allDrawn);
7217 }
7218 if (animating || animation != null) {
7219 pw.print(prefix); pw.print("animating="); pw.print(animating);
7220 pw.print(" animation="); pw.println(animation);
7221 }
7222 if (animLayerAdjustment != 0) {
7223 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7224 }
7225 if (hasTransformation) {
7226 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7227 pw.print(" transformation="); transformation.printShortString(pw);
7228 pw.println();
7229 }
7230 if (startingData != null || removed || firstWindowDrawn) {
7231 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7232 pw.print(" removed="); pw.print(removed);
7233 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7234 }
7235 if (startingWindow != null || startingView != null
7236 || startingDisplayed || startingMoved) {
7237 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7238 pw.print(" startingView="); pw.print(startingView);
7239 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7240 pw.print(" startingMoved"); pw.println(startingMoved);
7241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007242 }
7243
7244 @Override
7245 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007246 if (stringName == null) {
7247 StringBuilder sb = new StringBuilder();
7248 sb.append("AppWindowToken{");
7249 sb.append(Integer.toHexString(System.identityHashCode(this)));
7250 sb.append(" token="); sb.append(token); sb.append('}');
7251 stringName = sb.toString();
7252 }
7253 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007254 }
7255 }
Romain Guy06882f82009-06-10 13:36:04 -07007256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 public static WindowManager.LayoutParams findAnimations(
7258 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007259 ArrayList<AppWindowToken> openingTokenList1,
7260 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007261 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007262
7263 // First, check if there is a compatible window in opening/closing
7264 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007265 WindowManager.LayoutParams animParams = null;
7266 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007267 animParams = findCompatibleWindowParams(openingTokenList1);
7268 if (animParams == null) {
7269 animParams = findCompatibleWindowParams(closingTokenList2);
7270 }
7271 if (animParams != null) {
7272 return animParams;
7273 }
7274
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");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007279 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 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
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007307 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7308 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7309 AppWindowToken wtoken = tokenList.get(appCount);
7310 // Just checking one window is sufficient as all windows have the compatible flag
7311 // if the application is in compatibility mode.
7312 if (wtoken.windows.size() > 0) {
7313 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7314 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7315 return params;
7316 }
7317 }
7318 }
7319 return null;
7320 }
7321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007322 // -------------------------------------------------------------
7323 // DummyAnimation
7324 // -------------------------------------------------------------
7325
7326 // This is an animation that does nothing: it just immediately finishes
7327 // itself every time it is called. It is used as a stub animation in cases
7328 // where we want to synchronize multiple things that may be animating.
7329 static final class DummyAnimation extends Animation {
7330 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7331 return false;
7332 }
7333 }
7334 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007336 // -------------------------------------------------------------
7337 // Async Handler
7338 // -------------------------------------------------------------
7339
7340 static final class StartingData {
7341 final String pkg;
7342 final int theme;
7343 final CharSequence nonLocalizedLabel;
7344 final int labelRes;
7345 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007347 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7348 int _labelRes, int _icon) {
7349 pkg = _pkg;
7350 theme = _theme;
7351 nonLocalizedLabel = _nonLocalizedLabel;
7352 labelRes = _labelRes;
7353 icon = _icon;
7354 }
7355 }
7356
7357 private final class H extends Handler {
7358 public static final int REPORT_FOCUS_CHANGE = 2;
7359 public static final int REPORT_LOSING_FOCUS = 3;
7360 public static final int ANIMATE = 4;
7361 public static final int ADD_STARTING = 5;
7362 public static final int REMOVE_STARTING = 6;
7363 public static final int FINISHED_STARTING = 7;
7364 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7366 public static final int HOLD_SCREEN_CHANGED = 12;
7367 public static final int APP_TRANSITION_TIMEOUT = 13;
7368 public static final int PERSIST_ANIMATION_SCALE = 14;
7369 public static final int FORCE_GC = 15;
7370 public static final int ENABLE_SCREEN = 16;
7371 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007372 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 public H() {
7377 }
Romain Guy06882f82009-06-10 13:36:04 -07007378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007379 @Override
7380 public void handleMessage(Message msg) {
7381 switch (msg.what) {
7382 case REPORT_FOCUS_CHANGE: {
7383 WindowState lastFocus;
7384 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 synchronized(mWindowMap) {
7387 lastFocus = mLastFocus;
7388 newFocus = mCurrentFocus;
7389 if (lastFocus == newFocus) {
7390 // Focus is not changing, so nothing to do.
7391 return;
7392 }
7393 mLastFocus = newFocus;
7394 //Log.i(TAG, "Focus moving from " + lastFocus
7395 // + " to " + newFocus);
7396 if (newFocus != null && lastFocus != null
7397 && !newFocus.isDisplayedLw()) {
7398 //Log.i(TAG, "Delaying loss of focus...");
7399 mLosingFocus.add(lastFocus);
7400 lastFocus = null;
7401 }
7402 }
7403
7404 if (lastFocus != newFocus) {
7405 //System.out.println("Changing focus from " + lastFocus
7406 // + " to " + newFocus);
7407 if (newFocus != null) {
7408 try {
7409 //Log.i(TAG, "Gaining focus: " + newFocus);
7410 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7411 } catch (RemoteException e) {
7412 // Ignore if process has died.
7413 }
7414 }
7415
7416 if (lastFocus != null) {
7417 try {
7418 //Log.i(TAG, "Losing focus: " + lastFocus);
7419 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
7420 } catch (RemoteException e) {
7421 // Ignore if process has died.
7422 }
7423 }
7424 }
7425 } break;
7426
7427 case REPORT_LOSING_FOCUS: {
7428 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07007429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007430 synchronized(mWindowMap) {
7431 losers = mLosingFocus;
7432 mLosingFocus = new ArrayList<WindowState>();
7433 }
7434
7435 final int N = losers.size();
7436 for (int i=0; i<N; i++) {
7437 try {
7438 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
7439 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
7440 } catch (RemoteException e) {
7441 // Ignore if process has died.
7442 }
7443 }
7444 } break;
7445
7446 case ANIMATE: {
7447 synchronized(mWindowMap) {
7448 mAnimationPending = false;
7449 performLayoutAndPlaceSurfacesLocked();
7450 }
7451 } break;
7452
7453 case ADD_STARTING: {
7454 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7455 final StartingData sd = wtoken.startingData;
7456
7457 if (sd == null) {
7458 // Animation has been canceled... do nothing.
7459 return;
7460 }
Romain Guy06882f82009-06-10 13:36:04 -07007461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
7463 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07007464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 View view = null;
7466 try {
7467 view = mPolicy.addStartingWindow(
7468 wtoken.token, sd.pkg,
7469 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
7470 sd.icon);
7471 } catch (Exception e) {
7472 Log.w(TAG, "Exception when adding starting window", e);
7473 }
7474
7475 if (view != null) {
7476 boolean abort = false;
7477
7478 synchronized(mWindowMap) {
7479 if (wtoken.removed || wtoken.startingData == null) {
7480 // If the window was successfully added, then
7481 // we need to remove it.
7482 if (wtoken.startingWindow != null) {
7483 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7484 "Aborted starting " + wtoken
7485 + ": removed=" + wtoken.removed
7486 + " startingData=" + wtoken.startingData);
7487 wtoken.startingWindow = null;
7488 wtoken.startingData = null;
7489 abort = true;
7490 }
7491 } else {
7492 wtoken.startingView = view;
7493 }
7494 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
7495 "Added starting " + wtoken
7496 + ": startingWindow="
7497 + wtoken.startingWindow + " startingView="
7498 + wtoken.startingView);
7499 }
7500
7501 if (abort) {
7502 try {
7503 mPolicy.removeStartingWindow(wtoken.token, view);
7504 } catch (Exception e) {
7505 Log.w(TAG, "Exception when removing starting window", e);
7506 }
7507 }
7508 }
7509 } break;
7510
7511 case REMOVE_STARTING: {
7512 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7513 IBinder token = null;
7514 View view = null;
7515 synchronized (mWindowMap) {
7516 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
7517 + wtoken + ": startingWindow="
7518 + wtoken.startingWindow + " startingView="
7519 + wtoken.startingView);
7520 if (wtoken.startingWindow != null) {
7521 view = wtoken.startingView;
7522 token = wtoken.token;
7523 wtoken.startingData = null;
7524 wtoken.startingView = null;
7525 wtoken.startingWindow = null;
7526 }
7527 }
7528 if (view != null) {
7529 try {
7530 mPolicy.removeStartingWindow(token, view);
7531 } catch (Exception e) {
7532 Log.w(TAG, "Exception when removing starting window", e);
7533 }
7534 }
7535 } break;
7536
7537 case FINISHED_STARTING: {
7538 IBinder token = null;
7539 View view = null;
7540 while (true) {
7541 synchronized (mWindowMap) {
7542 final int N = mFinishedStarting.size();
7543 if (N <= 0) {
7544 break;
7545 }
7546 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
7547
7548 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
7549 "Finished starting " + wtoken
7550 + ": startingWindow=" + wtoken.startingWindow
7551 + " startingView=" + wtoken.startingView);
7552
7553 if (wtoken.startingWindow == null) {
7554 continue;
7555 }
7556
7557 view = wtoken.startingView;
7558 token = wtoken.token;
7559 wtoken.startingData = null;
7560 wtoken.startingView = null;
7561 wtoken.startingWindow = null;
7562 }
7563
7564 try {
7565 mPolicy.removeStartingWindow(token, view);
7566 } catch (Exception e) {
7567 Log.w(TAG, "Exception when removing starting window", e);
7568 }
7569 }
7570 } break;
7571
7572 case REPORT_APPLICATION_TOKEN_WINDOWS: {
7573 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
7574
7575 boolean nowVisible = msg.arg1 != 0;
7576 boolean nowGone = msg.arg2 != 0;
7577
7578 try {
7579 if (DEBUG_VISIBILITY) Log.v(
7580 TAG, "Reporting visible in " + wtoken
7581 + " visible=" + nowVisible
7582 + " gone=" + nowGone);
7583 if (nowVisible) {
7584 wtoken.appToken.windowsVisible();
7585 } else {
7586 wtoken.appToken.windowsGone();
7587 }
7588 } catch (RemoteException ex) {
7589 }
7590 } break;
Romain Guy06882f82009-06-10 13:36:04 -07007591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007592 case WINDOW_FREEZE_TIMEOUT: {
7593 synchronized (mWindowMap) {
7594 Log.w(TAG, "Window freeze timeout expired.");
7595 int i = mWindows.size();
7596 while (i > 0) {
7597 i--;
7598 WindowState w = (WindowState)mWindows.get(i);
7599 if (w.mOrientationChanging) {
7600 w.mOrientationChanging = false;
7601 Log.w(TAG, "Force clearing orientation change: " + w);
7602 }
7603 }
7604 performLayoutAndPlaceSurfacesLocked();
7605 }
7606 break;
7607 }
Romain Guy06882f82009-06-10 13:36:04 -07007608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609 case HOLD_SCREEN_CHANGED: {
7610 Session oldHold;
7611 Session newHold;
7612 synchronized (mWindowMap) {
7613 oldHold = mLastReportedHold;
7614 newHold = (Session)msg.obj;
7615 mLastReportedHold = newHold;
7616 }
Romain Guy06882f82009-06-10 13:36:04 -07007617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 if (oldHold != newHold) {
7619 try {
7620 if (oldHold != null) {
7621 mBatteryStats.noteStopWakelock(oldHold.mUid,
7622 "window",
7623 BatteryStats.WAKE_TYPE_WINDOW);
7624 }
7625 if (newHold != null) {
7626 mBatteryStats.noteStartWakelock(newHold.mUid,
7627 "window",
7628 BatteryStats.WAKE_TYPE_WINDOW);
7629 }
7630 } catch (RemoteException e) {
7631 }
7632 }
7633 break;
7634 }
Romain Guy06882f82009-06-10 13:36:04 -07007635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 case APP_TRANSITION_TIMEOUT: {
7637 synchronized (mWindowMap) {
7638 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7639 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
7640 "*** APP TRANSITION TIMEOUT");
7641 mAppTransitionReady = true;
7642 mAppTransitionTimeout = true;
7643 performLayoutAndPlaceSurfacesLocked();
7644 }
7645 }
7646 break;
7647 }
Romain Guy06882f82009-06-10 13:36:04 -07007648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 case PERSIST_ANIMATION_SCALE: {
7650 Settings.System.putFloat(mContext.getContentResolver(),
7651 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
7652 Settings.System.putFloat(mContext.getContentResolver(),
7653 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
7654 break;
7655 }
Romain Guy06882f82009-06-10 13:36:04 -07007656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 case FORCE_GC: {
7658 synchronized(mWindowMap) {
7659 if (mAnimationPending) {
7660 // If we are animating, don't do the gc now but
7661 // delay a bit so we don't interrupt the animation.
7662 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
7663 2000);
7664 return;
7665 }
7666 // If we are currently rotating the display, it will
7667 // schedule a new message when done.
7668 if (mDisplayFrozen) {
7669 return;
7670 }
7671 mFreezeGcPending = 0;
7672 }
7673 Runtime.getRuntime().gc();
7674 break;
7675 }
Romain Guy06882f82009-06-10 13:36:04 -07007676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 case ENABLE_SCREEN: {
7678 performEnableScreen();
7679 break;
7680 }
Romain Guy06882f82009-06-10 13:36:04 -07007681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007682 case APP_FREEZE_TIMEOUT: {
7683 synchronized (mWindowMap) {
7684 Log.w(TAG, "App freeze timeout expired.");
7685 int i = mAppTokens.size();
7686 while (i > 0) {
7687 i--;
7688 AppWindowToken tok = mAppTokens.get(i);
7689 if (tok.freezingScreen) {
7690 Log.w(TAG, "Force clearing freeze: " + tok);
7691 unsetAppFreezingScreenLocked(tok, true, true);
7692 }
7693 }
7694 }
7695 break;
7696 }
Romain Guy06882f82009-06-10 13:36:04 -07007697
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007698 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07007699 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007700 sendNewConfiguration();
7701 }
7702 break;
7703 }
Romain Guy06882f82009-06-10 13:36:04 -07007704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007705 }
7706 }
7707 }
7708
7709 // -------------------------------------------------------------
7710 // IWindowManager API
7711 // -------------------------------------------------------------
7712
7713 public IWindowSession openSession(IInputMethodClient client,
7714 IInputContext inputContext) {
7715 if (client == null) throw new IllegalArgumentException("null client");
7716 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
7717 return new Session(client, inputContext);
7718 }
7719
7720 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
7721 synchronized (mWindowMap) {
7722 // The focus for the client is the window immediately below
7723 // where we would place the input method window.
7724 int idx = findDesiredInputMethodWindowIndexLocked(false);
7725 WindowState imFocus;
7726 if (idx > 0) {
7727 imFocus = (WindowState)mWindows.get(idx-1);
7728 if (imFocus != null) {
7729 if (imFocus.mSession.mClient != null &&
7730 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
7731 return true;
7732 }
7733 }
7734 }
7735 }
7736 return false;
7737 }
Romain Guy06882f82009-06-10 13:36:04 -07007738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007739 // -------------------------------------------------------------
7740 // Internals
7741 // -------------------------------------------------------------
7742
7743 final WindowState windowForClientLocked(Session session, IWindow client) {
7744 return windowForClientLocked(session, client.asBinder());
7745 }
Romain Guy06882f82009-06-10 13:36:04 -07007746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007747 final WindowState windowForClientLocked(Session session, IBinder client) {
7748 WindowState win = mWindowMap.get(client);
7749 if (localLOGV) Log.v(
7750 TAG, "Looking up client " + client + ": " + win);
7751 if (win == null) {
7752 RuntimeException ex = new RuntimeException();
7753 Log.w(TAG, "Requested window " + client + " does not exist", ex);
7754 return null;
7755 }
7756 if (session != null && win.mSession != session) {
7757 RuntimeException ex = new RuntimeException();
7758 Log.w(TAG, "Requested window " + client + " is in session " +
7759 win.mSession + ", not " + session, ex);
7760 return null;
7761 }
7762
7763 return win;
7764 }
7765
7766 private final void assignLayersLocked() {
7767 int N = mWindows.size();
7768 int curBaseLayer = 0;
7769 int curLayer = 0;
7770 int i;
Romain Guy06882f82009-06-10 13:36:04 -07007771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007772 for (i=0; i<N; i++) {
7773 WindowState w = (WindowState)mWindows.get(i);
7774 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow) {
7775 curLayer += WINDOW_LAYER_MULTIPLIER;
7776 w.mLayer = curLayer;
7777 } else {
7778 curBaseLayer = curLayer = w.mBaseLayer;
7779 w.mLayer = curLayer;
7780 }
7781 if (w.mTargetAppToken != null) {
7782 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
7783 } else if (w.mAppToken != null) {
7784 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
7785 } else {
7786 w.mAnimLayer = w.mLayer;
7787 }
7788 if (w.mIsImWindow) {
7789 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
7790 }
7791 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
7792 + w.mAnimLayer);
7793 //System.out.println(
7794 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
7795 }
7796 }
7797
7798 private boolean mInLayout = false;
7799 private final void performLayoutAndPlaceSurfacesLocked() {
7800 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07007801 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 throw new RuntimeException("Recursive call!");
7803 }
7804 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
7805 return;
7806 }
7807
7808 boolean recoveringMemory = false;
7809 if (mForceRemoves != null) {
7810 recoveringMemory = true;
7811 // Wait a little it for things to settle down, and off we go.
7812 for (int i=0; i<mForceRemoves.size(); i++) {
7813 WindowState ws = mForceRemoves.get(i);
7814 Log.i(TAG, "Force removing: " + ws);
7815 removeWindowInnerLocked(ws.mSession, ws);
7816 }
7817 mForceRemoves = null;
7818 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
7819 Object tmp = new Object();
7820 synchronized (tmp) {
7821 try {
7822 tmp.wait(250);
7823 } catch (InterruptedException e) {
7824 }
7825 }
7826 }
Romain Guy06882f82009-06-10 13:36:04 -07007827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 mInLayout = true;
7829 try {
7830 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07007831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 int i = mPendingRemove.size()-1;
7833 if (i >= 0) {
7834 while (i >= 0) {
7835 WindowState w = mPendingRemove.get(i);
7836 removeWindowInnerLocked(w.mSession, w);
7837 i--;
7838 }
7839 mPendingRemove.clear();
7840
7841 mInLayout = false;
7842 assignLayersLocked();
7843 mLayoutNeeded = true;
7844 performLayoutAndPlaceSurfacesLocked();
7845
7846 } else {
7847 mInLayout = false;
7848 if (mLayoutNeeded) {
7849 requestAnimationLocked(0);
7850 }
7851 }
7852 } catch (RuntimeException e) {
7853 mInLayout = false;
7854 Log.e(TAG, "Unhandled exception while layout out windows", e);
7855 }
7856 }
7857
7858 private final void performLayoutLockedInner() {
7859 final int dw = mDisplay.getWidth();
7860 final int dh = mDisplay.getHeight();
7861
7862 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007863 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 int i;
7865
7866 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07007867
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007868 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 mPolicy.beginLayoutLw(dw, dh);
7870
7871 // First perform layout of any root windows (not attached
7872 // to another window).
7873 int topAttached = -1;
7874 for (i = N-1; i >= 0; i--) {
7875 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007876
7877 // Don't do layout of a window if it is not visible, or
7878 // soon won't be visible, to avoid wasting time and funky
7879 // changes while a window is animating away.
7880 final AppWindowToken atoken = win.mAppToken;
7881 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007882 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007883 || win.mRootToken.hidden
7884 || (atoken != null && atoken.hiddenRequested)
7885 || !win.mPolicyVisibility
7886 || win.mAttachedHidden
7887 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007888
7889 // If this view is GONE, then skip it -- keep the current
7890 // frame, and let the caller know so they can ignore it
7891 // if they want. (We do the normal layout for INVISIBLE
7892 // windows, since that means "perform layout as normal,
7893 // just don't display").
7894 if (!gone || !win.mHaveFrame) {
7895 if (!win.mLayoutAttached) {
7896 mPolicy.layoutWindowLw(win, win.mAttrs, null);
7897 } else {
7898 if (topAttached < 0) topAttached = i;
7899 }
7900 }
7901 }
Romain Guy06882f82009-06-10 13:36:04 -07007902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 // Now perform layout of attached windows, which usually
7904 // depend on the position of the window they are attached to.
7905 // XXX does not deal with windows that are attached to windows
7906 // that are themselves attached.
7907 for (i = topAttached; i >= 0; i--) {
7908 WindowState win = (WindowState) mWindows.get(i);
7909
7910 // If this view is GONE, then skip it -- keep the current
7911 // frame, and let the caller know so they can ignore it
7912 // if they want. (We do the normal layout for INVISIBLE
7913 // windows, since that means "perform layout as normal,
7914 // just don't display").
7915 if (win.mLayoutAttached) {
7916 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
7917 || !win.mHaveFrame) {
7918 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
7919 }
7920 }
7921 }
7922
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007923 if (!mPolicy.finishLayoutLw()) {
7924 mLayoutNeeded = false;
7925 } else if (repeats > 2) {
7926 Log.w(TAG, "Layout repeat aborted after too many iterations");
7927 mLayoutNeeded = false;
7928 } else {
7929 repeats++;
7930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931 }
7932 }
Romain Guy06882f82009-06-10 13:36:04 -07007933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007934 private final void performLayoutAndPlaceSurfacesLockedInner(
7935 boolean recoveringMemory) {
7936 final long currentTime = SystemClock.uptimeMillis();
7937 final int dw = mDisplay.getWidth();
7938 final int dh = mDisplay.getHeight();
7939
7940 final int N = mWindows.size();
7941 int i;
7942
7943 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007944 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07007945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946 if (mFxSession == null) {
7947 mFxSession = new SurfaceSession();
7948 }
Romain Guy06882f82009-06-10 13:36:04 -07007949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007950 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
7951
7952 // Initialize state of exiting tokens.
7953 for (i=mExitingTokens.size()-1; i>=0; i--) {
7954 mExitingTokens.get(i).hasVisible = false;
7955 }
7956
7957 // Initialize state of exiting applications.
7958 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
7959 mExitingAppTokens.get(i).hasVisible = false;
7960 }
7961
7962 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007963 boolean orientationChangeComplete = true;
7964 Session holdScreen = null;
7965 float screenBrightness = -1;
7966 boolean focusDisplayed = false;
7967 boolean animating = false;
7968
7969 Surface.openTransaction();
7970 try {
7971 boolean restart;
7972
7973 do {
7974 final int transactionSequence = ++mTransactionSequence;
7975
7976 // Update animations of all applications, including those
7977 // associated with exiting/removed apps
7978 boolean tokensAnimating = false;
7979 final int NAT = mAppTokens.size();
7980 for (i=0; i<NAT; i++) {
7981 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7982 tokensAnimating = true;
7983 }
7984 }
7985 final int NEAT = mExitingAppTokens.size();
7986 for (i=0; i<NEAT; i++) {
7987 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
7988 tokensAnimating = true;
7989 }
7990 }
7991
7992 animating = tokensAnimating;
7993 restart = false;
7994
7995 boolean tokenMayBeDrawn = false;
7996
7997 mPolicy.beginAnimationLw(dw, dh);
7998
7999 for (i=N-1; i>=0; i--) {
8000 WindowState w = (WindowState)mWindows.get(i);
8001
8002 final WindowManager.LayoutParams attrs = w.mAttrs;
8003
8004 if (w.mSurface != null) {
8005 // Execute animation.
8006 w.commitFinishDrawingLocked(currentTime);
8007 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8008 animating = true;
8009 //w.dump(" ");
8010 }
8011
8012 mPolicy.animatingWindowLw(w, attrs);
8013 }
8014
8015 final AppWindowToken atoken = w.mAppToken;
8016 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8017 if (atoken.lastTransactionSequence != transactionSequence) {
8018 atoken.lastTransactionSequence = transactionSequence;
8019 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8020 atoken.startingDisplayed = false;
8021 }
8022 if ((w.isOnScreen() || w.mAttrs.type
8023 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8024 && !w.mExiting && !w.mDestroying) {
8025 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8026 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8027 + w.isDisplayedLw()
8028 + ", isAnimating=" + w.isAnimating());
8029 if (!w.isDisplayedLw()) {
8030 Log.v(TAG, "Not displayed: s=" + w.mSurface
8031 + " pv=" + w.mPolicyVisibility
8032 + " dp=" + w.mDrawPending
8033 + " cdp=" + w.mCommitDrawPending
8034 + " ah=" + w.mAttachedHidden
8035 + " th=" + atoken.hiddenRequested
8036 + " a=" + w.mAnimating);
8037 }
8038 }
8039 if (w != atoken.startingWindow) {
8040 if (!atoken.freezingScreen || !w.mAppFreezing) {
8041 atoken.numInterestingWindows++;
8042 if (w.isDisplayedLw()) {
8043 atoken.numDrawnWindows++;
8044 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8045 "tokenMayBeDrawn: " + atoken
8046 + " freezingScreen=" + atoken.freezingScreen
8047 + " mAppFreezing=" + w.mAppFreezing);
8048 tokenMayBeDrawn = true;
8049 }
8050 }
8051 } else if (w.isDisplayedLw()) {
8052 atoken.startingDisplayed = true;
8053 }
8054 }
8055 } else if (w.mReadyToShow) {
8056 w.performShowLocked();
8057 }
8058 }
8059
8060 if (mPolicy.finishAnimationLw()) {
8061 restart = true;
8062 }
8063
8064 if (tokenMayBeDrawn) {
8065 // See if any windows have been drawn, so they (and others
8066 // associated with them) can now be shown.
8067 final int NT = mTokenList.size();
8068 for (i=0; i<NT; i++) {
8069 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8070 if (wtoken == null) {
8071 continue;
8072 }
8073 if (wtoken.freezingScreen) {
8074 int numInteresting = wtoken.numInterestingWindows;
8075 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8076 if (DEBUG_VISIBILITY) Log.v(TAG,
8077 "allDrawn: " + wtoken
8078 + " interesting=" + numInteresting
8079 + " drawn=" + wtoken.numDrawnWindows);
8080 wtoken.showAllWindowsLocked();
8081 unsetAppFreezingScreenLocked(wtoken, false, true);
8082 orientationChangeComplete = true;
8083 }
8084 } else if (!wtoken.allDrawn) {
8085 int numInteresting = wtoken.numInterestingWindows;
8086 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8087 if (DEBUG_VISIBILITY) Log.v(TAG,
8088 "allDrawn: " + wtoken
8089 + " interesting=" + numInteresting
8090 + " drawn=" + wtoken.numDrawnWindows);
8091 wtoken.allDrawn = true;
8092 restart = true;
8093
8094 // We can now show all of the drawn windows!
8095 if (!mOpeningApps.contains(wtoken)) {
8096 wtoken.showAllWindowsLocked();
8097 }
8098 }
8099 }
8100 }
8101 }
8102
8103 // If we are ready to perform an app transition, check through
8104 // all of the app tokens to be shown and see if they are ready
8105 // to go.
8106 if (mAppTransitionReady) {
8107 int NN = mOpeningApps.size();
8108 boolean goodToGo = true;
8109 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8110 "Checking " + NN + " opening apps (frozen="
8111 + mDisplayFrozen + " timeout="
8112 + mAppTransitionTimeout + ")...");
8113 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8114 // If the display isn't frozen, wait to do anything until
8115 // all of the apps are ready. Otherwise just go because
8116 // we'll unfreeze the display when everyone is ready.
8117 for (i=0; i<NN && goodToGo; i++) {
8118 AppWindowToken wtoken = mOpeningApps.get(i);
8119 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8120 "Check opening app" + wtoken + ": allDrawn="
8121 + wtoken.allDrawn + " startingDisplayed="
8122 + wtoken.startingDisplayed);
8123 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8124 && !wtoken.startingMoved) {
8125 goodToGo = false;
8126 }
8127 }
8128 }
8129 if (goodToGo) {
8130 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8131 int transit = mNextAppTransition;
8132 if (mSkipAppTransitionAnimation) {
8133 transit = WindowManagerPolicy.TRANSIT_NONE;
8134 }
8135 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8136 mAppTransitionReady = false;
8137 mAppTransitionTimeout = false;
8138 mStartingIconInTransition = false;
8139 mSkipAppTransitionAnimation = false;
8140
8141 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8142
8143 // We need to figure out which animation to use...
8144 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8145 mOpeningApps, mClosingApps);
8146
8147 NN = mOpeningApps.size();
8148 for (i=0; i<NN; i++) {
8149 AppWindowToken wtoken = mOpeningApps.get(i);
8150 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8151 "Now opening app" + wtoken);
8152 wtoken.reportedVisible = false;
8153 wtoken.inPendingTransaction = false;
8154 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8155 wtoken.updateReportedVisibilityLocked();
8156 wtoken.showAllWindowsLocked();
8157 }
8158 NN = mClosingApps.size();
8159 for (i=0; i<NN; i++) {
8160 AppWindowToken wtoken = mClosingApps.get(i);
8161 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8162 "Now closing app" + wtoken);
8163 wtoken.inPendingTransaction = false;
8164 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8165 wtoken.updateReportedVisibilityLocked();
8166 // Force the allDrawn flag, because we want to start
8167 // this guy's animations regardless of whether it's
8168 // gotten drawn.
8169 wtoken.allDrawn = true;
8170 }
8171
8172 mOpeningApps.clear();
8173 mClosingApps.clear();
8174
8175 // This has changed the visibility of windows, so perform
8176 // a new layout to get them all up-to-date.
8177 mLayoutNeeded = true;
8178 moveInputMethodWindowsIfNeededLocked(true);
8179 performLayoutLockedInner();
8180 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8181
8182 restart = true;
8183 }
8184 }
8185 } while (restart);
8186
8187 // THIRD LOOP: Update the surfaces of all windows.
8188
8189 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8190
8191 boolean obscured = false;
8192 boolean blurring = false;
8193 boolean dimming = false;
8194 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008195 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008196 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008197
8198 for (i=N-1; i>=0; i--) {
8199 WindowState w = (WindowState)mWindows.get(i);
8200
8201 boolean displayed = false;
8202 final WindowManager.LayoutParams attrs = w.mAttrs;
8203 final int attrFlags = attrs.flags;
8204
8205 if (w.mSurface != null) {
8206 w.computeShownFrameLocked();
8207 if (localLOGV) Log.v(
8208 TAG, "Placing surface #" + i + " " + w.mSurface
8209 + ": new=" + w.mShownFrame + ", old="
8210 + w.mLastShownFrame);
8211
8212 boolean resize;
8213 int width, height;
8214 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8215 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8216 w.mLastRequestedHeight != w.mRequestedHeight;
8217 // for a scaled surface, we just want to use
8218 // the requested size.
8219 width = w.mRequestedWidth;
8220 height = w.mRequestedHeight;
8221 w.mLastRequestedWidth = width;
8222 w.mLastRequestedHeight = height;
8223 w.mLastShownFrame.set(w.mShownFrame);
8224 try {
8225 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8226 } catch (RuntimeException e) {
8227 Log.w(TAG, "Error positioning surface in " + w, e);
8228 if (!recoveringMemory) {
8229 reclaimSomeSurfaceMemoryLocked(w, "position");
8230 }
8231 }
8232 } else {
8233 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8234 width = w.mShownFrame.width();
8235 height = w.mShownFrame.height();
8236 w.mLastShownFrame.set(w.mShownFrame);
8237 if (resize) {
8238 if (SHOW_TRANSACTIONS) Log.i(
8239 TAG, " SURFACE " + w.mSurface + ": ("
8240 + w.mShownFrame.left + ","
8241 + w.mShownFrame.top + ") ("
8242 + w.mShownFrame.width() + "x"
8243 + w.mShownFrame.height() + ")");
8244 }
8245 }
8246
8247 if (resize) {
8248 if (width < 1) width = 1;
8249 if (height < 1) height = 1;
8250 if (w.mSurface != null) {
8251 try {
8252 w.mSurface.setSize(width, height);
8253 w.mSurface.setPosition(w.mShownFrame.left,
8254 w.mShownFrame.top);
8255 } catch (RuntimeException e) {
8256 // If something goes wrong with the surface (such
8257 // as running out of memory), don't take down the
8258 // entire system.
8259 Log.e(TAG, "Failure updating surface of " + w
8260 + "size=(" + width + "x" + height
8261 + "), pos=(" + w.mShownFrame.left
8262 + "," + w.mShownFrame.top + ")", e);
8263 if (!recoveringMemory) {
8264 reclaimSomeSurfaceMemoryLocked(w, "size");
8265 }
8266 }
8267 }
8268 }
8269 if (!w.mAppFreezing) {
8270 w.mContentInsetsChanged =
8271 !w.mLastContentInsets.equals(w.mContentInsets);
8272 w.mVisibleInsetsChanged =
8273 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008274 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 || w.mContentInsetsChanged
8276 || w.mVisibleInsetsChanged) {
8277 w.mLastFrame.set(w.mFrame);
8278 w.mLastContentInsets.set(w.mContentInsets);
8279 w.mLastVisibleInsets.set(w.mVisibleInsets);
8280 // If the orientation is changing, then we need to
8281 // hold off on unfreezing the display until this
8282 // window has been redrawn; to do that, we need
8283 // to go through the process of getting informed
8284 // by the application when it has finished drawing.
8285 if (w.mOrientationChanging) {
8286 if (DEBUG_ORIENTATION) Log.v(TAG,
8287 "Orientation start waiting for draw in "
8288 + w + ", surface " + w.mSurface);
8289 w.mDrawPending = true;
8290 w.mCommitDrawPending = false;
8291 w.mReadyToShow = false;
8292 if (w.mAppToken != null) {
8293 w.mAppToken.allDrawn = false;
8294 }
8295 }
Romain Guy06882f82009-06-10 13:36:04 -07008296 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 "Resizing window " + w + " to " + w.mFrame);
8298 mResizingWindows.add(w);
8299 } else if (w.mOrientationChanging) {
8300 if (!w.mDrawPending && !w.mCommitDrawPending) {
8301 if (DEBUG_ORIENTATION) Log.v(TAG,
8302 "Orientation not waiting for draw in "
8303 + w + ", surface " + w.mSurface);
8304 w.mOrientationChanging = false;
8305 }
8306 }
8307 }
8308
8309 if (w.mAttachedHidden) {
8310 if (!w.mLastHidden) {
8311 //dump();
8312 w.mLastHidden = true;
8313 if (SHOW_TRANSACTIONS) Log.i(
8314 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8315 if (w.mSurface != null) {
8316 try {
8317 w.mSurface.hide();
8318 } catch (RuntimeException e) {
8319 Log.w(TAG, "Exception hiding surface in " + w);
8320 }
8321 }
8322 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8323 }
8324 // If we are waiting for this window to handle an
8325 // orientation change, well, it is hidden, so
8326 // doesn't really matter. Note that this does
8327 // introduce a potential glitch if the window
8328 // becomes unhidden before it has drawn for the
8329 // new orientation.
8330 if (w.mOrientationChanging) {
8331 w.mOrientationChanging = false;
8332 if (DEBUG_ORIENTATION) Log.v(TAG,
8333 "Orientation change skips hidden " + w);
8334 }
8335 } else if (!w.isReadyForDisplay()) {
8336 if (!w.mLastHidden) {
8337 //dump();
8338 w.mLastHidden = true;
8339 if (SHOW_TRANSACTIONS) Log.i(
8340 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8341 if (w.mSurface != null) {
8342 try {
8343 w.mSurface.hide();
8344 } catch (RuntimeException e) {
8345 Log.w(TAG, "Exception exception hiding surface in " + w);
8346 }
8347 }
8348 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8349 }
8350 // If we are waiting for this window to handle an
8351 // orientation change, well, it is hidden, so
8352 // doesn't really matter. Note that this does
8353 // introduce a potential glitch if the window
8354 // becomes unhidden before it has drawn for the
8355 // new orientation.
8356 if (w.mOrientationChanging) {
8357 w.mOrientationChanging = false;
8358 if (DEBUG_ORIENTATION) Log.v(TAG,
8359 "Orientation change skips hidden " + w);
8360 }
8361 } else if (w.mLastLayer != w.mAnimLayer
8362 || w.mLastAlpha != w.mShownAlpha
8363 || w.mLastDsDx != w.mDsDx
8364 || w.mLastDtDx != w.mDtDx
8365 || w.mLastDsDy != w.mDsDy
8366 || w.mLastDtDy != w.mDtDy
8367 || w.mLastHScale != w.mHScale
8368 || w.mLastVScale != w.mVScale
8369 || w.mLastHidden) {
8370 displayed = true;
8371 w.mLastAlpha = w.mShownAlpha;
8372 w.mLastLayer = w.mAnimLayer;
8373 w.mLastDsDx = w.mDsDx;
8374 w.mLastDtDx = w.mDtDx;
8375 w.mLastDsDy = w.mDsDy;
8376 w.mLastDtDy = w.mDtDy;
8377 w.mLastHScale = w.mHScale;
8378 w.mLastVScale = w.mVScale;
8379 if (SHOW_TRANSACTIONS) Log.i(
8380 TAG, " SURFACE " + w.mSurface + ": alpha="
8381 + w.mShownAlpha + " layer=" + w.mAnimLayer);
8382 if (w.mSurface != null) {
8383 try {
8384 w.mSurface.setAlpha(w.mShownAlpha);
8385 w.mSurface.setLayer(w.mAnimLayer);
8386 w.mSurface.setMatrix(
8387 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
8388 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
8389 } catch (RuntimeException e) {
8390 Log.w(TAG, "Error updating surface in " + w, e);
8391 if (!recoveringMemory) {
8392 reclaimSomeSurfaceMemoryLocked(w, "update");
8393 }
8394 }
8395 }
8396
8397 if (w.mLastHidden && !w.mDrawPending
8398 && !w.mCommitDrawPending
8399 && !w.mReadyToShow) {
8400 if (SHOW_TRANSACTIONS) Log.i(
8401 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
8402 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
8403 + " during relayout");
8404 if (showSurfaceRobustlyLocked(w)) {
8405 w.mHasDrawn = true;
8406 w.mLastHidden = false;
8407 } else {
8408 w.mOrientationChanging = false;
8409 }
8410 }
8411 if (w.mSurface != null) {
8412 w.mToken.hasVisible = true;
8413 }
8414 } else {
8415 displayed = true;
8416 }
8417
8418 if (displayed) {
8419 if (!covered) {
8420 if (attrs.width == LayoutParams.FILL_PARENT
8421 && attrs.height == LayoutParams.FILL_PARENT) {
8422 covered = true;
8423 }
8424 }
8425 if (w.mOrientationChanging) {
8426 if (w.mDrawPending || w.mCommitDrawPending) {
8427 orientationChangeComplete = false;
8428 if (DEBUG_ORIENTATION) Log.v(TAG,
8429 "Orientation continue waiting for draw in " + w);
8430 } else {
8431 w.mOrientationChanging = false;
8432 if (DEBUG_ORIENTATION) Log.v(TAG,
8433 "Orientation change complete in " + w);
8434 }
8435 }
8436 w.mToken.hasVisible = true;
8437 }
8438 } else if (w.mOrientationChanging) {
8439 if (DEBUG_ORIENTATION) Log.v(TAG,
8440 "Orientation change skips hidden " + w);
8441 w.mOrientationChanging = false;
8442 }
8443
8444 final boolean canBeSeen = w.isDisplayedLw();
8445
8446 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
8447 focusDisplayed = true;
8448 }
8449
8450 // Update effect.
8451 if (!obscured) {
8452 if (w.mSurface != null) {
8453 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
8454 holdScreen = w.mSession;
8455 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008456 if (!syswin && w.mAttrs.screenBrightness >= 0
8457 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008458 screenBrightness = w.mAttrs.screenBrightness;
8459 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008460 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
8461 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
8462 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
8463 syswin = true;
8464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008465 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008466
8467 boolean opaqueDrawn = w.isOpaqueDrawn();
8468 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008469 // This window completely covers everything behind it,
8470 // so we want to leave all of them as unblurred (for
8471 // performance reasons).
8472 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008473 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
8474 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008475 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008476 obscured = true;
8477 if (mBackgroundFillerSurface == null) {
8478 try {
8479 mBackgroundFillerSurface = new Surface(mFxSession, 0,
8480 0, dw, dh,
8481 PixelFormat.OPAQUE,
8482 Surface.FX_SURFACE_NORMAL);
8483 } catch (Exception e) {
8484 Log.e(TAG, "Exception creating filler surface", e);
8485 }
8486 }
8487 try {
8488 mBackgroundFillerSurface.setPosition(0, 0);
8489 mBackgroundFillerSurface.setSize(dw, dh);
8490 // Using the same layer as Dim because they will never be shown at the
8491 // same time.
8492 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
8493 mBackgroundFillerSurface.show();
8494 } catch (RuntimeException e) {
8495 Log.e(TAG, "Exception showing filler surface");
8496 }
8497 backgroundFillerShown = true;
8498 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 } else if (canBeSeen && !obscured &&
8500 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
8501 if (localLOGV) Log.v(TAG, "Win " + w
8502 + ": blurring=" + blurring
8503 + " obscured=" + obscured
8504 + " displayed=" + displayed);
8505 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
8506 if (!dimming) {
8507 //Log.i(TAG, "DIM BEHIND: " + w);
8508 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008509 if (mDimAnimator == null) {
8510 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008511 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008512 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008513 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008514 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008515 }
8516 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
8517 if (!blurring) {
8518 //Log.i(TAG, "BLUR BEHIND: " + w);
8519 blurring = true;
8520 mBlurShown = true;
8521 if (mBlurSurface == null) {
8522 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8523 + mBlurSurface + ": CREATE");
8524 try {
Romain Guy06882f82009-06-10 13:36:04 -07008525 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008526 -1, 16, 16,
8527 PixelFormat.OPAQUE,
8528 Surface.FX_SURFACE_BLUR);
8529 } catch (Exception e) {
8530 Log.e(TAG, "Exception creating Blur surface", e);
8531 }
8532 }
8533 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
8534 + mBlurSurface + ": SHOW pos=(0,0) (" +
8535 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
8536 if (mBlurSurface != null) {
8537 mBlurSurface.setPosition(0, 0);
8538 mBlurSurface.setSize(dw, dh);
8539 try {
8540 mBlurSurface.show();
8541 } catch (RuntimeException e) {
8542 Log.w(TAG, "Failure showing blur surface", e);
8543 }
8544 }
8545 }
8546 mBlurSurface.setLayer(w.mAnimLayer-2);
8547 }
8548 }
8549 }
8550 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008551
8552 if (backgroundFillerShown == false && mBackgroundFillerShown) {
8553 mBackgroundFillerShown = false;
8554 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
8555 try {
8556 mBackgroundFillerSurface.hide();
8557 } catch (RuntimeException e) {
8558 Log.e(TAG, "Exception hiding filler surface", e);
8559 }
8560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008561
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008562 if (mDimAnimator != null && mDimAnimator.mDimShown) {
8563 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008564 }
Romain Guy06882f82009-06-10 13:36:04 -07008565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 if (!blurring && mBlurShown) {
8567 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
8568 + ": HIDE");
8569 try {
8570 mBlurSurface.hide();
8571 } catch (IllegalArgumentException e) {
8572 Log.w(TAG, "Illegal argument exception hiding blur surface");
8573 }
8574 mBlurShown = false;
8575 }
8576
8577 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
8578 } catch (RuntimeException e) {
8579 Log.e(TAG, "Unhandled exception in Window Manager", e);
8580 }
8581
8582 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07008583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008584 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
8585 "With display frozen, orientationChangeComplete="
8586 + orientationChangeComplete);
8587 if (orientationChangeComplete) {
8588 if (mWindowsFreezingScreen) {
8589 mWindowsFreezingScreen = false;
8590 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
8591 }
8592 if (mAppsFreezingScreen == 0) {
8593 stopFreezingDisplayLocked();
8594 }
8595 }
Romain Guy06882f82009-06-10 13:36:04 -07008596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 i = mResizingWindows.size();
8598 if (i > 0) {
8599 do {
8600 i--;
8601 WindowState win = mResizingWindows.get(i);
8602 try {
8603 win.mClient.resized(win.mFrame.width(),
8604 win.mFrame.height(), win.mLastContentInsets,
8605 win.mLastVisibleInsets, win.mDrawPending);
8606 win.mContentInsetsChanged = false;
8607 win.mVisibleInsetsChanged = false;
8608 } catch (RemoteException e) {
8609 win.mOrientationChanging = false;
8610 }
8611 } while (i > 0);
8612 mResizingWindows.clear();
8613 }
Romain Guy06882f82009-06-10 13:36:04 -07008614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008615 // Destroy the surface of any windows that are no longer visible.
8616 i = mDestroySurface.size();
8617 if (i > 0) {
8618 do {
8619 i--;
8620 WindowState win = mDestroySurface.get(i);
8621 win.mDestroying = false;
8622 if (mInputMethodWindow == win) {
8623 mInputMethodWindow = null;
8624 }
8625 win.destroySurfaceLocked();
8626 } while (i > 0);
8627 mDestroySurface.clear();
8628 }
8629
8630 // Time to remove any exiting tokens?
8631 for (i=mExitingTokens.size()-1; i>=0; i--) {
8632 WindowToken token = mExitingTokens.get(i);
8633 if (!token.hasVisible) {
8634 mExitingTokens.remove(i);
8635 }
8636 }
8637
8638 // Time to remove any exiting applications?
8639 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8640 AppWindowToken token = mExitingAppTokens.get(i);
8641 if (!token.hasVisible && !mClosingApps.contains(token)) {
8642 mAppTokens.remove(token);
8643 mExitingAppTokens.remove(i);
8644 }
8645 }
8646
8647 if (focusDisplayed) {
8648 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8649 }
8650 if (animating) {
8651 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
8652 }
8653 mQueue.setHoldScreenLocked(holdScreen != null);
8654 if (screenBrightness < 0 || screenBrightness > 1.0f) {
8655 mPowerManager.setScreenBrightnessOverride(-1);
8656 } else {
8657 mPowerManager.setScreenBrightnessOverride((int)
8658 (screenBrightness * Power.BRIGHTNESS_ON));
8659 }
8660 if (holdScreen != mHoldingScreenOn) {
8661 mHoldingScreenOn = holdScreen;
8662 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
8663 mH.sendMessage(m);
8664 }
8665 }
8666
8667 void requestAnimationLocked(long delay) {
8668 if (!mAnimationPending) {
8669 mAnimationPending = true;
8670 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
8671 }
8672 }
Romain Guy06882f82009-06-10 13:36:04 -07008673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 /**
8675 * Have the surface flinger show a surface, robustly dealing with
8676 * error conditions. In particular, if there is not enough memory
8677 * to show the surface, then we will try to get rid of other surfaces
8678 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07008679 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008680 * @return Returns true if the surface was successfully shown.
8681 */
8682 boolean showSurfaceRobustlyLocked(WindowState win) {
8683 try {
8684 if (win.mSurface != null) {
8685 win.mSurface.show();
8686 }
8687 return true;
8688 } catch (RuntimeException e) {
8689 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
8690 }
Romain Guy06882f82009-06-10 13:36:04 -07008691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008692 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07008693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008694 return false;
8695 }
Romain Guy06882f82009-06-10 13:36:04 -07008696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
8698 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07008699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008700 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
8701 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07008702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008703 if (mForceRemoves == null) {
8704 mForceRemoves = new ArrayList<WindowState>();
8705 }
Romain Guy06882f82009-06-10 13:36:04 -07008706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707 long callingIdentity = Binder.clearCallingIdentity();
8708 try {
8709 // There was some problem... first, do a sanity check of the
8710 // window list to make sure we haven't left any dangling surfaces
8711 // around.
8712 int N = mWindows.size();
8713 boolean leakedSurface = false;
8714 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
8715 for (int i=0; i<N; i++) {
8716 WindowState ws = (WindowState)mWindows.get(i);
8717 if (ws.mSurface != null) {
8718 if (!mSessions.contains(ws.mSession)) {
8719 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
8720 + ws + " surface=" + ws.mSurface
8721 + " token=" + win.mToken
8722 + " pid=" + ws.mSession.mPid
8723 + " uid=" + ws.mSession.mUid);
8724 ws.mSurface.clear();
8725 ws.mSurface = null;
8726 mForceRemoves.add(ws);
8727 i--;
8728 N--;
8729 leakedSurface = true;
8730 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
8731 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
8732 + ws + " surface=" + ws.mSurface
8733 + " token=" + win.mAppToken);
8734 ws.mSurface.clear();
8735 ws.mSurface = null;
8736 leakedSurface = true;
8737 }
8738 }
8739 }
Romain Guy06882f82009-06-10 13:36:04 -07008740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008741 boolean killedApps = false;
8742 if (!leakedSurface) {
8743 Log.w(TAG, "No leaked surfaces; killing applicatons!");
8744 SparseIntArray pidCandidates = new SparseIntArray();
8745 for (int i=0; i<N; i++) {
8746 WindowState ws = (WindowState)mWindows.get(i);
8747 if (ws.mSurface != null) {
8748 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
8749 }
8750 }
8751 if (pidCandidates.size() > 0) {
8752 int[] pids = new int[pidCandidates.size()];
8753 for (int i=0; i<pids.length; i++) {
8754 pids[i] = pidCandidates.keyAt(i);
8755 }
8756 try {
8757 if (mActivityManager.killPidsForMemory(pids)) {
8758 killedApps = true;
8759 }
8760 } catch (RemoteException e) {
8761 }
8762 }
8763 }
Romain Guy06882f82009-06-10 13:36:04 -07008764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008765 if (leakedSurface || killedApps) {
8766 // We managed to reclaim some memory, so get rid of the trouble
8767 // surface and ask the app to request another one.
8768 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
8769 if (surface != null) {
8770 surface.clear();
8771 win.mSurface = null;
8772 }
Romain Guy06882f82009-06-10 13:36:04 -07008773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008774 try {
8775 win.mClient.dispatchGetNewSurface();
8776 } catch (RemoteException e) {
8777 }
8778 }
8779 } finally {
8780 Binder.restoreCallingIdentity(callingIdentity);
8781 }
8782 }
Romain Guy06882f82009-06-10 13:36:04 -07008783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008784 private boolean updateFocusedWindowLocked(int mode) {
8785 WindowState newFocus = computeFocusedWindowLocked();
8786 if (mCurrentFocus != newFocus) {
8787 // This check makes sure that we don't already have the focus
8788 // change message pending.
8789 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
8790 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
8791 if (localLOGV) Log.v(
8792 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
8793 final WindowState oldFocus = mCurrentFocus;
8794 mCurrentFocus = newFocus;
8795 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07008796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 final WindowState imWindow = mInputMethodWindow;
8798 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008799 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008800 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008801 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
8802 mLayoutNeeded = true;
8803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008804 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
8805 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008806 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
8807 // Client will do the layout, but we need to assign layers
8808 // for handleNewWindowLocked() below.
8809 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008810 }
8811 }
Romain Guy06882f82009-06-10 13:36:04 -07008812
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08008813 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
8814 mKeyWaiter.handleNewWindowLocked(newFocus);
8815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008816 return true;
8817 }
8818 return false;
8819 }
8820
8821 private WindowState computeFocusedWindowLocked() {
8822 WindowState result = null;
8823 WindowState win;
8824
8825 int i = mWindows.size() - 1;
8826 int nextAppIndex = mAppTokens.size()-1;
8827 WindowToken nextApp = nextAppIndex >= 0
8828 ? mAppTokens.get(nextAppIndex) : null;
8829
8830 while (i >= 0) {
8831 win = (WindowState)mWindows.get(i);
8832
8833 if (localLOGV || DEBUG_FOCUS) Log.v(
8834 TAG, "Looking for focus: " + i
8835 + " = " + win
8836 + ", flags=" + win.mAttrs.flags
8837 + ", canReceive=" + win.canReceiveKeys());
8838
8839 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07008840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008841 // If this window's application has been removed, just skip it.
8842 if (thisApp != null && thisApp.removed) {
8843 i--;
8844 continue;
8845 }
Romain Guy06882f82009-06-10 13:36:04 -07008846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008847 // If there is a focused app, don't allow focus to go to any
8848 // windows below it. If this is an application window, step
8849 // through the app tokens until we find its app.
8850 if (thisApp != null && nextApp != null && thisApp != nextApp
8851 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
8852 int origAppIndex = nextAppIndex;
8853 while (nextAppIndex > 0) {
8854 if (nextApp == mFocusedApp) {
8855 // Whoops, we are below the focused app... no focus
8856 // for you!
8857 if (localLOGV || DEBUG_FOCUS) Log.v(
8858 TAG, "Reached focused app: " + mFocusedApp);
8859 return null;
8860 }
8861 nextAppIndex--;
8862 nextApp = mAppTokens.get(nextAppIndex);
8863 if (nextApp == thisApp) {
8864 break;
8865 }
8866 }
8867 if (thisApp != nextApp) {
8868 // Uh oh, the app token doesn't exist! This shouldn't
8869 // happen, but if it does we can get totally hosed...
8870 // so restart at the original app.
8871 nextAppIndex = origAppIndex;
8872 nextApp = mAppTokens.get(nextAppIndex);
8873 }
8874 }
8875
8876 // Dispatch to this window if it is wants key events.
8877 if (win.canReceiveKeys()) {
8878 if (DEBUG_FOCUS) Log.v(
8879 TAG, "Found focus @ " + i + " = " + win);
8880 result = win;
8881 break;
8882 }
8883
8884 i--;
8885 }
8886
8887 return result;
8888 }
8889
8890 private void startFreezingDisplayLocked() {
8891 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07008892 // Freezing the display also suspends key event delivery, to
8893 // keep events from going astray while the display is reconfigured.
8894 // If someone has changed orientation again while the screen is
8895 // still frozen, the events will continue to be blocked while the
8896 // successive orientation change is processed. To prevent spurious
8897 // ANRs, we reset the event dispatch timeout in this case.
8898 synchronized (mKeyWaiter) {
8899 mKeyWaiter.mWasFrozen = true;
8900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008901 return;
8902 }
Romain Guy06882f82009-06-10 13:36:04 -07008903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07008905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008906 long now = SystemClock.uptimeMillis();
8907 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
8908 if (mFreezeGcPending != 0) {
8909 if (now > (mFreezeGcPending+1000)) {
8910 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
8911 mH.removeMessages(H.FORCE_GC);
8912 Runtime.getRuntime().gc();
8913 mFreezeGcPending = now;
8914 }
8915 } else {
8916 mFreezeGcPending = now;
8917 }
Romain Guy06882f82009-06-10 13:36:04 -07008918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008919 mDisplayFrozen = true;
8920 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8921 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8922 mAppTransitionReady = true;
8923 }
Romain Guy06882f82009-06-10 13:36:04 -07008924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008925 if (PROFILE_ORIENTATION) {
8926 File file = new File("/data/system/frozen");
8927 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
8928 }
8929 Surface.freezeDisplay(0);
8930 }
Romain Guy06882f82009-06-10 13:36:04 -07008931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008932 private void stopFreezingDisplayLocked() {
8933 if (!mDisplayFrozen) {
8934 return;
8935 }
Romain Guy06882f82009-06-10 13:36:04 -07008936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937 mDisplayFrozen = false;
8938 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
8939 if (PROFILE_ORIENTATION) {
8940 Debug.stopMethodTracing();
8941 }
8942 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07008943
Chris Tate2ad63a92009-03-25 17:36:48 -07008944 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
8945 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008946 synchronized (mKeyWaiter) {
8947 mKeyWaiter.mWasFrozen = true;
8948 mKeyWaiter.notifyAll();
8949 }
8950
8951 // A little kludge: a lot could have happened while the
8952 // display was frozen, so now that we are coming back we
8953 // do a gc so that any remote references the system
8954 // processes holds on others can be released if they are
8955 // no longer needed.
8956 mH.removeMessages(H.FORCE_GC);
8957 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8958 2000);
Romain Guy06882f82009-06-10 13:36:04 -07008959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008960 mScreenFrozenLock.release();
8961 }
Romain Guy06882f82009-06-10 13:36:04 -07008962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008963 @Override
8964 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
8965 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
8966 != PackageManager.PERMISSION_GRANTED) {
8967 pw.println("Permission Denial: can't dump WindowManager from from pid="
8968 + Binder.getCallingPid()
8969 + ", uid=" + Binder.getCallingUid());
8970 return;
8971 }
Romain Guy06882f82009-06-10 13:36:04 -07008972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008973 synchronized(mWindowMap) {
8974 pw.println("Current Window Manager state:");
8975 for (int i=mWindows.size()-1; i>=0; i--) {
8976 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008977 pw.print(" Window #"); pw.print(i); pw.print(' ');
8978 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008979 w.dump(pw, " ");
8980 }
8981 if (mInputMethodDialogs.size() > 0) {
8982 pw.println(" ");
8983 pw.println(" Input method dialogs:");
8984 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
8985 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008986 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008987 }
8988 }
8989 if (mPendingRemove.size() > 0) {
8990 pw.println(" ");
8991 pw.println(" Remove pending for:");
8992 for (int i=mPendingRemove.size()-1; i>=0; i--) {
8993 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008994 pw.print(" Remove #"); pw.print(i); pw.print(' ');
8995 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008996 w.dump(pw, " ");
8997 }
8998 }
8999 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9000 pw.println(" ");
9001 pw.println(" Windows force removing:");
9002 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9003 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009004 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9005 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009006 w.dump(pw, " ");
9007 }
9008 }
9009 if (mDestroySurface.size() > 0) {
9010 pw.println(" ");
9011 pw.println(" Windows waiting to destroy their surface:");
9012 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9013 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009014 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9015 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009016 w.dump(pw, " ");
9017 }
9018 }
9019 if (mLosingFocus.size() > 0) {
9020 pw.println(" ");
9021 pw.println(" Windows losing focus:");
9022 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9023 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009024 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9025 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009026 w.dump(pw, " ");
9027 }
9028 }
9029 if (mSessions.size() > 0) {
9030 pw.println(" ");
9031 pw.println(" All active sessions:");
9032 Iterator<Session> it = mSessions.iterator();
9033 while (it.hasNext()) {
9034 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009035 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009036 s.dump(pw, " ");
9037 }
9038 }
9039 if (mTokenMap.size() > 0) {
9040 pw.println(" ");
9041 pw.println(" All tokens:");
9042 Iterator<WindowToken> it = mTokenMap.values().iterator();
9043 while (it.hasNext()) {
9044 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009045 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009046 token.dump(pw, " ");
9047 }
9048 }
9049 if (mTokenList.size() > 0) {
9050 pw.println(" ");
9051 pw.println(" Window token list:");
9052 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009053 pw.print(" #"); pw.print(i); pw.print(": ");
9054 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009055 }
9056 }
9057 if (mAppTokens.size() > 0) {
9058 pw.println(" ");
9059 pw.println(" Application tokens in Z order:");
9060 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009061 pw.print(" App #"); pw.print(i); pw.print(": ");
9062 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009063 }
9064 }
9065 if (mFinishedStarting.size() > 0) {
9066 pw.println(" ");
9067 pw.println(" Finishing start of application tokens:");
9068 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9069 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009070 pw.print(" Finished Starting #"); pw.print(i);
9071 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009072 token.dump(pw, " ");
9073 }
9074 }
9075 if (mExitingTokens.size() > 0) {
9076 pw.println(" ");
9077 pw.println(" Exiting tokens:");
9078 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9079 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009080 pw.print(" Exiting #"); pw.print(i);
9081 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082 token.dump(pw, " ");
9083 }
9084 }
9085 if (mExitingAppTokens.size() > 0) {
9086 pw.println(" ");
9087 pw.println(" Exiting application tokens:");
9088 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9089 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009090 pw.print(" Exiting App #"); pw.print(i);
9091 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009092 token.dump(pw, " ");
9093 }
9094 }
9095 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009096 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9097 pw.print(" mLastFocus="); pw.println(mLastFocus);
9098 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9099 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9100 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
9101 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9102 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9103 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9104 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9105 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009106 if (mDimAnimator != null) {
9107 mDimAnimator.printTo(pw);
9108 } else {
9109 pw.print( " no DimAnimator ");
9110 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009111 pw.print(" mInputMethodAnimLayerAdjustment=");
9112 pw.println(mInputMethodAnimLayerAdjustment);
9113 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9114 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9115 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9116 pw.print(" mRotation="); pw.print(mRotation);
9117 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9118 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9119 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9120 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9121 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9122 pw.print(" mNextAppTransition=0x");
9123 pw.print(Integer.toHexString(mNextAppTransition));
9124 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9125 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9126 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9127 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9128 if (mOpeningApps.size() > 0) {
9129 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9130 }
9131 if (mClosingApps.size() > 0) {
9132 pw.print(" mClosingApps="); pw.println(mClosingApps);
9133 }
9134 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9135 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009136 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009137 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9138 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9139 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9140 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9141 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9142 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009143 }
9144 }
9145
9146 public void monitor() {
9147 synchronized (mWindowMap) { }
9148 synchronized (mKeyguardDisabled) { }
9149 synchronized (mKeyWaiter) { }
9150 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009151
9152 /**
9153 * DimAnimator class that controls the dim animation. This holds the surface and
9154 * all state used for dim animation.
9155 */
9156 private static class DimAnimator {
9157 Surface mDimSurface;
9158 boolean mDimShown = false;
9159 float mDimCurrentAlpha;
9160 float mDimTargetAlpha;
9161 float mDimDeltaPerMs;
9162 long mLastDimAnimTime;
9163
9164 DimAnimator (SurfaceSession session) {
9165 if (mDimSurface == null) {
9166 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9167 + mDimSurface + ": CREATE");
9168 try {
9169 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9170 Surface.FX_SURFACE_DIM);
9171 } catch (Exception e) {
9172 Log.e(TAG, "Exception creating Dim surface", e);
9173 }
9174 }
9175 }
9176
9177 /**
9178 * Show the dim surface.
9179 */
9180 void show(int dw, int dh) {
9181 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9182 dw + "x" + dh + ")");
9183 mDimShown = true;
9184 try {
9185 mDimSurface.setPosition(0, 0);
9186 mDimSurface.setSize(dw, dh);
9187 mDimSurface.show();
9188 } catch (RuntimeException e) {
9189 Log.w(TAG, "Failure showing dim surface", e);
9190 }
9191 }
9192
9193 /**
9194 * Set's the dim surface's layer and update dim parameters that will be used in
9195 * {@link updateSurface} after all windows are examined.
9196 */
9197 void updateParameters(WindowState w, long currentTime) {
9198 mDimSurface.setLayer(w.mAnimLayer-1);
9199
9200 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9201 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9202 if (mDimTargetAlpha != target) {
9203 // If the desired dim level has changed, then
9204 // start an animation to it.
9205 mLastDimAnimTime = currentTime;
9206 long duration = (w.mAnimating && w.mAnimation != null)
9207 ? w.mAnimation.computeDurationHint()
9208 : DEFAULT_DIM_DURATION;
9209 if (target > mDimTargetAlpha) {
9210 // This is happening behind the activity UI,
9211 // so we can make it run a little longer to
9212 // give a stronger impression without disrupting
9213 // the user.
9214 duration *= DIM_DURATION_MULTIPLIER;
9215 }
9216 if (duration < 1) {
9217 // Don't divide by zero
9218 duration = 1;
9219 }
9220 mDimTargetAlpha = target;
9221 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9222 }
9223 }
9224
9225 /**
9226 * Updating the surface's alpha. Returns true if the animation continues, or returns
9227 * false when the animation is finished and the dim surface is hidden.
9228 */
9229 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9230 if (!dimming) {
9231 if (mDimTargetAlpha != 0) {
9232 mLastDimAnimTime = currentTime;
9233 mDimTargetAlpha = 0;
9234 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9235 }
9236 }
9237
9238 boolean animating = false;
9239 if (mLastDimAnimTime != 0) {
9240 mDimCurrentAlpha += mDimDeltaPerMs
9241 * (currentTime-mLastDimAnimTime);
9242 boolean more = true;
9243 if (displayFrozen) {
9244 // If the display is frozen, there is no reason to animate.
9245 more = false;
9246 } else if (mDimDeltaPerMs > 0) {
9247 if (mDimCurrentAlpha > mDimTargetAlpha) {
9248 more = false;
9249 }
9250 } else if (mDimDeltaPerMs < 0) {
9251 if (mDimCurrentAlpha < mDimTargetAlpha) {
9252 more = false;
9253 }
9254 } else {
9255 more = false;
9256 }
9257
9258 // Do we need to continue animating?
9259 if (more) {
9260 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9261 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9262 mLastDimAnimTime = currentTime;
9263 mDimSurface.setAlpha(mDimCurrentAlpha);
9264 animating = true;
9265 } else {
9266 mDimCurrentAlpha = mDimTargetAlpha;
9267 mLastDimAnimTime = 0;
9268 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9269 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9270 mDimSurface.setAlpha(mDimCurrentAlpha);
9271 if (!dimming) {
9272 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9273 + ": HIDE");
9274 try {
9275 mDimSurface.hide();
9276 } catch (RuntimeException e) {
9277 Log.w(TAG, "Illegal argument exception hiding dim surface");
9278 }
9279 mDimShown = false;
9280 }
9281 }
9282 }
9283 return animating;
9284 }
9285
9286 public void printTo(PrintWriter pw) {
9287 pw.print(" mDimShown="); pw.print(mDimShown);
9288 pw.print(" current="); pw.print(mDimCurrentAlpha);
9289 pw.print(" target="); pw.print(mDimTargetAlpha);
9290 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9291 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9292 }
9293 }
9294
9295 /**
9296 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9297 * This is used for opening/closing transition for apps in compatible mode.
9298 */
9299 private static class FadeInOutAnimation extends Animation {
9300 int mWidth;
9301 boolean mFadeIn;
9302
9303 public FadeInOutAnimation(boolean fadeIn) {
9304 setInterpolator(new AccelerateInterpolator());
9305 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9306 mFadeIn = fadeIn;
9307 }
9308
9309 @Override
9310 protected void applyTransformation(float interpolatedTime, Transformation t) {
9311 float x = interpolatedTime;
9312 if (!mFadeIn) {
9313 x = 1.0f - x; // reverse the interpolation for fade out
9314 }
9315 if (x < 0.5) {
9316 // move the window out of the screen.
9317 t.getMatrix().setTranslate(mWidth, 0);
9318 } else {
9319 t.getMatrix().setTranslate(0, 0);// show
9320 t.setAlpha((x - 0.5f) * 2);
9321 }
9322 }
9323
9324 @Override
9325 public void initialize(int width, int height, int parentWidth, int parentHeight) {
9326 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
9327 mWidth = width;
9328 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009329
9330 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07009331 public int getZAdjustment() {
9332 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009333 }
9334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009335}